Instrument Neutral Distributed Interface INDI 2.2.5
ddw_dome.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 DDW Dome INDI Driver
3
4 Copyright(c) 2020-2023 Jarno Paananen. All rights reserved.
5
6 based on:
7
8 ScopeDome Dome INDI Driver
9
10 Copyright(c) 2017-2023 Jarno Paananen. All rights reserved.
11
12 and
13
14 Baader Planetarium Dome INDI Driver
15
16 Copyright(c) 2014 Jasem Mutlaq. All rights reserved.
17
18 Baader Dome INDI Driver
19
20 This library is free software; you can redistribute it and/or
21 modify it under the terms of the GNU Library General Public
22 License version 2 as published by the Free Software Foundation.
23 .
24 This library is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 Library General Public License for more details.
28 .
29 You should have received a copy of the GNU Library General Public License
30 along with this library; see the file COPYING.LIB. If not, write to
31 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 Boston, MA 02110-1301, USA.
33*******************************************************************************/
34
35#include "ddw_dome.h"
36
38#include "indicom.h"
39
40#define DDW_TIMEOUT 2
41
42// We declare an auto pointer to DDW.
43std::unique_ptr<DDW> ddw(new DDW());
44
46{
47 setVersion(1, 0);
49 // DDW runs one command at a time, and closing the shutter first rotates the dome to home,
50 // so Park()/UnPark() sequence the shutter themselves.
53}
54
56{
58
59 FirmwareVersionNP[0].fill("VERSION", "Version", "%2.0f", 0.0, 99.0, 1.0, 0.0);
60 FirmwareVersionNP.fill(getDeviceName(), "FIRMWARE", "Firmware", INFO_TAB, IP_RO, 60, IPS_IDLE);
61
63
65
66 // Set serial parameters
68
69 setPollingPeriodRange(1000, 3000); // Device doesn't like too long interval
71 return true;
72}
73
74/************************************************************************************
75 *
76* ***********************************************************************************/
78{
79 if (InitPark())
80 {
81 // If loading parking data is successful, we just set the default parking
82 // values.
84 }
85 else
86 {
87 // Otherwise, we set all parking data to default in case no parking data is
88 // found.
91 }
92
93 FirmwareVersionNP[0].setValue(fwVersion);
94 FirmwareVersionNP.setState(IPS_OK);
95 FirmwareVersionNP.apply();
96 return true;
97}
98
99/************************************************************************************
100 *
101* ***********************************************************************************/
103{
104 // Send GINF command and check if the controller responds
105 int rc = writeCmd("GINF");
106 if (rc != TTY_OK)
107 return false;
108
109 std::string response;
110 rc = readStatus(response);
111 if (rc != TTY_OK)
112 return false;
113
114 LOGF_DEBUG("Initial response: %s", response.c_str());
115
116 // Response should start with V
117 if (response[0] != 'V')
118 return false;
119
120 parseGINF(response.c_str());
121 cmdState = IDLE;
122 DomeAbsPosNP.setState(IPS_OK);
123 DomeAbsPosNP.apply();
124 return true;
125}
126
127/************************************************************************************
128 *
129* ***********************************************************************************/
131{
132 return "DDW Dome";
133}
134
135/************************************************************************************
136 *
137* ***********************************************************************************/
139{
141
142 if (isConnected())
143 {
145 SetupParms();
146 }
147 else
148 {
150 }
151
152 return true;
153}
154
155/************************************************************************************
156 *
157* ***********************************************************************************/
158int DDW::writeCmd(const char *cmd)
159{
160 int nbytes_written = 0, rc = -1;
161 char errstr[MAXRBUF];
162
163 if ((rc = tty_write(PortFD, cmd, strlen(cmd), &nbytes_written)) != TTY_OK)
164 {
165 tty_error_msg(rc, errstr, MAXRBUF);
166 LOGF_ERROR("Error writing command: %s. Cmd: %s", errstr, cmd);
167 }
168 return rc;
169}
170
171int DDW::readStatus(std::string &status)
172{
173 int nbytes_read = 0, rc = -1;
174 char errstr[MAXRBUF];
175
176 // Read response and parse it
177 char response[256] = { 0 };
178
179 // Read buffer
180 if ((rc = tty_nread_section(PortFD, response, sizeof(response) - 1, '\r', DDW_TIMEOUT, &nbytes_read)) != TTY_OK)
181 {
182 tty_error_msg(rc, errstr, MAXRBUF);
183 LOGF_ERROR("Error reading: %s.", errstr);
184 }
185 status = response;
186 return rc;
187}
188
189void DDW::parseGINF(const char *response)
190{
191 /* GINF packet structure:
192
193 Field Content Note (each datum is separated by comma)
194 1 V# Denotes Version Data. E.g., V1
195 2 Dticks DTICKS is dome circumference in ticks 0-32767. Value is sent as a string of characters, e.g., 457. Leading zeros not transmitted.
196 3 Home1 Azimuth location of the HOME position in ticks 0-32767
197 4 Coast Coast value in ticks (0-255)
198 5 ADAZ Current dome azimuth in Ticks 0-32767
199 6 Slave Slave Status 0=slave off 1=slave on
200 7 Shutter Shutter status 0=indeterminate, 1=closed, 2=open
201 8 DSR status DSR Status 0=indet, 1=closed, 2=open
202 9 Home Home sensor 0=home, 1=not home
203 10 HTICK_CCLK Azimuth ticks of counterclockwise edge of Home position
204 11 HTICK_CLK Azimuth ticks of clockwise edge of Home position
205 12 UPINS Status of all user digital output pins
206 13 WEAAGE Age of weather info in minutes 0 to 255 (255 means expired)
207 14 WINDDIR 0-255 wind direction (use (n/255)*359 to compute actual direction), subtract dome azimuth if weather module is mounted on dome top.
208 15 WINDSPD Windspeed 0-255 miles per hour
209 16 TEMP Temperature 0-255, representing -100 to 155 degrees F
210 17 HUMID Humidity 0-100% relative
211 18 WETNESS Wetness 0 (dry) to 100 (soaking wet)
212 19 SNOW Snow 0 (none) to 100 (sensor covered)
213 20 WIND PEAK Windspeed Peak level over session 0-255 miles per hour
214 21 SCOPEAZ Scope azimuth from LX-200 (999 if not available)
215 22 INTDZ Internal “deadzone”- angular displacement around the dome opening centerline within which desired dome azimuth can change without causing dome movement.
216 23 INTOFF Internal offset- angular distance DDW will add to the desired azimuth, causing the dome to preceed the telescope’s position when a slaved goto occurs.
217 24 car ret
218 25 car ret
219 */
220
221 int version;
222 int dticks;
223 int homepos;
224 int coast;
225 int azimuth;
226 int slave;
227 int shutter;
228 int dsr;
229 int home;
230 int htick_cclk;
231 int htick_clk;
232 int upins;
233 int weatherAge;
234 int windDir;
235 int windSpd;
236 int temperature;
237 int humidity;
238 int wetness;
239 int snow;
240 int windPeak;
241 int scopeAzimuth;
242 int deadzone;
243 int offset;
244
245 int num = sscanf(response, "V%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &version,
246 &dticks, &homepos, &coast, &azimuth, &slave, &shutter, &dsr, &home, &htick_cclk, &htick_clk,
247 &upins, &weatherAge, &windDir, &windSpd, &temperature, &humidity, &wetness, &snow, &windPeak,
248 &scopeAzimuth, &deadzone, &offset);
249
250 if (num == 23)
251 {
252 fwVersion = version;
253 ticksPerRev = dticks;
254 homeAz = 360.0 * homepos / ticksPerRev;
255
256 DomeAbsPosNP[0].setValue(360.0 * azimuth / ticksPerRev);
257
258 ShutterState newState;
259 switch (shutter)
260 {
261 case 1:
262 newState = SHUTTER_CLOSED;
263 break;
264 case 2:
265 newState = SHUTTER_OPENED;
266 break;
267 default:
268 newState = SHUTTER_UNKNOWN;
269 break;
270 }
271
272 // Only send if changed to avoid spam
273 if (getShutterState() != newState)
274 {
275 setShutterState(newState);
276 }
277 }
278}
279
280/************************************************************************************
281 *
282* ***********************************************************************************/
284{
285 if (!isConnected())
286 return; // No need to reset timer if we are not connected anymore
287
288 // The controller may write responses at any time due to hand controller usage
289 // and command responses, so need to poll for them here
290 while(true)
291 {
292 char errstr[MAXRBUF];
293 char response[256] = { 0 };
294 int nr;
295 int rc = tty_read(PortFD, response, 1, 0, &nr);
296 if (rc != TTY_OK || nr != 1)
297 {
298 // Nothing more to read
299 if (++watchdog * getCurrentPollingPeriod() > 60 * 1000)
300 {
301 // If we haven't heard from the controller for 60s, send GINF command to keep DDW watchdog from triggering
302 writeCmd("GINF");
303 watchdog = 0; // prevent from triggering immediately again
304 }
305 break; // exit loop
306 }
307
308 watchdog = 0;
309 switch (response[0])
310 {
311 case 'L':
312 LOG_DEBUG("Moving left");
313 break;
314 case 'R':
315 LOG_DEBUG("Moving right");
316 break;
317 case 'T':
318 LOG_DEBUG("Move tick");
319 break;
320 case 'P':
321 {
322 // Read tick count
323 if ((rc = tty_nread_section(PortFD, response + 1, sizeof(response) - 2, '\r', DDW_TIMEOUT, &nr)) !=
324 TTY_OK)
325 {
326 tty_error_msg(rc, errstr, MAXRBUF);
327 LOGF_ERROR("Error reading: %s.", errstr);
328 }
329 int tick = -1;
330 sscanf(response, "P%d", &tick);
331 LOGF_DEBUG("Tick counter %d", tick);
332
333 // Update current position
334 DomeAbsPosNP[0].setValue(359.0 * tick / ticksPerRev);
335 DomeAbsPosNP.apply();
336 break;
337 }
338 case 'O':
339 LOG_DEBUG("Opening shutter");
340 break;
341 case 'C':
342 LOG_DEBUG("Closing shutter");
343 break;
344 case 'S':
345 LOG_DEBUG("Shutter tick");
346 break;
347 case 'Z':
348 {
349 if ((rc = tty_nread_section(PortFD, response + 1, sizeof(response) - 2, '\r', DDW_TIMEOUT, &nr)) !=
350 TTY_OK)
351 {
352 tty_error_msg(rc, errstr, MAXRBUF);
353 LOGF_ERROR("Error reading: %s.", errstr);
354 }
355 int tick = -1;
356 sscanf(response, "Z%d", &tick);
357 LOGF_DEBUG("Shutter encoder %d", tick);
358 break;
359 }
360 case 'V':
361 {
362 // Move finished, read rest of GINF packet and parse it
363 // Read buffer
364 if ((rc = tty_nread_section(PortFD, response + 1, sizeof(response) - 2, '\r', DDW_TIMEOUT, &nr)) !=
365 TTY_OK)
366 {
367 tty_error_msg(rc, errstr, MAXRBUF);
368 LOGF_ERROR("Error reading: %s.", errstr);
369 }
370
371 LOGF_DEBUG("GINF packet: %s", response);
372 parseGINF(response);
373
374 auto prevState = cmdState;
375 cmdState = IDLE;
376
377 // Check which operation was completed
378 switch (getDomeState())
379 {
380 case DOME_PARKING:
381 if(prevState == SHUTTER_OPERATION)
382 {
383 // First phase of parking done, now move to park position
384 DomeAbsPosNP.setState(MoveAbs(GetAxis1Park()));
385 }
386 else
387 {
388 SetParked(true);
389 }
390 break;
391 case DOME_UNPARKING:
392 SetParked(false);
393 break;
394 default:
395 if(prevState == MOVING)
396 {
397 if (gotoPending)
398 {
399 LOGF_DEBUG("Performing pending goto to %f", gotoTarget);
400 DomeAbsPosNP.setState(MoveAbs(gotoTarget));
401 gotoPending = false;
402 }
403 else
404 {
405 LOG_DEBUG("Move complete");
407 }
408 }
409 break;
410 }
411 DomeAbsPosNP.apply();
412 break;
413 }
414 default:
415 LOGF_ERROR("Unknown response character %c", response[0]);
416 break;
417 }
418 }
420}
421
422/************************************************************************************
423 *
424* ***********************************************************************************/
426{
427 LOGF_DEBUG("MoveAbs (%f)", az);
428
429 if (cmdState == MOVING)
430 {
431 LOG_DEBUG("Dome already moving, latching the new move command");
432 gotoTarget = az;
433 gotoPending = true;
434 return IPS_BUSY;
435 }
436 else if (cmdState != IDLE)
437 {
438 LOG_ERROR("Dome needs to be idle to issue moves");
439 return IPS_ALERT;
440 }
441
442 // Construct move command
443 int rounded = round(az); // Controller only supports degrees
444 char cmd[5];
445 snprintf(cmd, sizeof(cmd), "G%03d", rounded);
446 cmdState = MOVING;
447 writeCmd(cmd);
448 return IPS_BUSY;
449}
450
451/************************************************************************************
452 *
453* ***********************************************************************************/
455{
456 if (cmdState != IDLE)
457 {
458 LOG_ERROR("Dome needs to be idle to issue park");
459 return IPS_ALERT;
460 }
461
462 // First close the shutter if wanted (moves to home position), then move to park position
465
466 return MoveAbs(GetAxis1Park());
467}
468
469/************************************************************************************
470 *
471* ***********************************************************************************/
473{
474 if (cmdState != IDLE)
475 {
476 LOG_ERROR("Dome needs to be idle to issue unpark");
477 return IPS_ALERT;
478 }
479
481 {
483 }
484 return IPS_OK;
485}
486
487/************************************************************************************
488 *
489* ***********************************************************************************/
491{
492 if (cmdState != IDLE)
493 {
494 LOG_ERROR("Dome needs to be idle for shutter operations");
495 return IPS_ALERT;
496 }
497 if (operation == SHUTTER_OPEN)
498 {
499 LOG_INFO("Opening shutter");
500 cmdState = SHUTTER_OPERATION;
501 if (writeCmd("GOPN") != TTY_OK)
502 return IPS_ALERT;
503 }
504 else
505 {
506 LOG_INFO("Closing shutter");
507 cmdState = SHUTTER_OPERATION;
508 if (writeCmd("GCLS") != TTY_OK)
509 return IPS_ALERT;
510 }
511 return IPS_BUSY;
512}
513
514/************************************************************************************
515 *
516* ***********************************************************************************/
518{
519 writeCmd("!!"); // Any two characters not used for command will do
520 cmdState = IDLE;
521 return true;
522}
523
524/************************************************************************************
525 *
526* ***********************************************************************************/
528{
530}
531
532/************************************************************************************
533 *
534* ***********************************************************************************/
536{
537 SetAxis1Park(DomeAbsPosNP[0].getValue());
538 return true;
539}
540/************************************************************************************
541 *
542* ***********************************************************************************/
543
545{
546 // By default set park position to home position
547 SetAxis1Park(homeAz);
548 return true;
549}
double humidity
double temperature
Definition ddw_dome.h:39
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition ddw_dome.cpp:55
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Dome Presets in the configuration file
Definition ddw_dome.cpp:527
virtual IPState Park() override
Goto Park Position. The park position is an absolute azimuth value.
Definition ddw_dome.cpp:454
virtual bool Handshake() override
perform handshake with device to check communication
Definition ddw_dome.cpp:102
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition ddw_dome.cpp:138
DDW()
Definition ddw_dome.cpp:45
INDI::PropertyNumber FirmwareVersionNP
Definition ddw_dome.h:66
virtual bool SetCurrentPark() override
SetCurrentPark Set current coordinates/encoders value as the desired parking position.
Definition ddw_dome.cpp:535
virtual IPState ControlShutter(ShutterOperation operation) override
Open or Close shutter.
Definition ddw_dome.cpp:490
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
Definition ddw_dome.cpp:283
virtual IPState UnPark() override
UnPark dome. The action of the Unpark command is dome specific, but it may include opening the shutte...
Definition ddw_dome.cpp:472
virtual IPState MoveAbs(double az) override
Move the Dome to an absolute azimuth.
Definition ddw_dome.cpp:425
virtual bool Abort() override
Abort all dome motion.
Definition ddw_dome.cpp:517
virtual bool SetDefaultPark() override
SetDefaultPark Set default coordinates/encoders value as the desired parking position.
Definition ddw_dome.cpp:544
bool SetupParms()
Definition ddw_dome.cpp:77
virtual const char * getDefaultName() override
Definition ddw_dome.cpp:130
bool isConnected() const
void setPollingPeriodRange(uint32_t minimum, uint32_t maximum)
setPollingPeriodRange Set the range permitted by the polling range in milliseconds
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
@ CONNECTION_SERIAL
Definition indidome.h:172
void SetParked(bool isparked)
SetParked Change the mount parking status. The data park file (stored in ~/.indi/ParkData....
void SetDomeCapability(uint32_t cap)
SetDomeCapability set the dome capabilities. All capabilities must be initialized.
void setDomeConnection(const uint8_t &value)
setDomeConnection Set Dome connection mode. Child class should call this in the constructor before Do...
INDI::PropertySwitch ShutterParkPolicySP
Definition indidome.h:588
@ SHUTTER_OPEN_ON_UNPARK
Definition indidome.h:592
@ SHUTTER_CLOSE_ON_PARK
Definition indidome.h:591
@ DOME_UNPARKING
Definition indidome.h:135
void SetAxis1Park(double value)
SetRAPark Set current AZ parking position. The data park file (stored in ~/.indi/ParkData....
ShutterOperation
Shutter operation command.
Definition indidome.h:114
@ SHUTTER_CLOSE
Definition indidome.h:116
void SetAxis1ParkDefault(double steps)
SetAxis1Park Set default AZ parking position.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition indidome.cpp:108
double GetAxis1Park()
@ SHUTTER_UNKNOWN
Definition indidome.h:150
@ SHUTTER_OPENED
Definition indidome.h:147
@ SHUTTER_CLOSED
Definition indidome.h:148
@ DOME_CAN_PARK
Definition indidome.h:159
@ DOME_CAN_ABS_MOVE
Definition indidome.h:157
@ DOME_HAS_SHUTTER
Definition indidome.h:161
@ DOME_CAN_ABORT
Definition indidome.h:156
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition indidome.cpp:287
INDI::PropertyNumber DomeAbsPosNP
Definition indidome.h:558
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Dome Presets in the configuration file
void setShutterState(const ShutterState &value)
void setDomeState(const DomeState &value)
void SetShutterParkPolicyHandledByDriver(bool enabled)
SetShutterParkPolicyHandledByDriver By default, INDI::Dome applies the shutter park policy (DOME_SHUT...
Definition indidome.h:393
bool InitPark()
InitPark Loads parking data (stored in ~/.indi/ParkData.xml) that contains parking status and parking...
Connection::Serial * serialConnection
Definition indidome.h:629
void SetParkDataType(DomeParkData type)
setParkDataType Sets the type of parking data stored in the park data file and presented to the user.
ShutterState getShutterState() const
Definition indidome.h:291
DomeState getDomeState() const
Definition indidome.h:285
#define DDW_TIMEOUT
Definition ddw_dome.cpp:40
std::unique_ptr< DDW > ddw(new DDW())
const char * INFO_TAB
INFO_TAB Where all the properties for general information are located.
@ ISS_ON
Definition indiapi.h:150
@ IP_RO
Definition indiapi.h:182
IPState
Property state.
Definition indiapi.h:158
@ IPS_BUSY
Definition indiapi.h:161
@ IPS_ALERT
Definition indiapi.h:162
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
Definition indicom.c:418
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition indicom.c:548
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Definition indicom.c:1243
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition indicom.c:759
Implementations for common driver routines.
@ TTY_OK
Definition indicom.h:54
#define MAXRBUF
Definition indidriver.c:52
#define LOG_DEBUG(txt)
Definition indilogger.h:75
#define LOGF_DEBUG(fmt,...)
Definition indilogger.h:83
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
Definition indilogger.h:72
#define LOGF_ERROR(fmt,...)
Definition indilogger.h:80
#define LOG_INFO(txt)
Definition indilogger.h:74
#define getDeviceName
uint8_t cmd[4]
Definition pwc-ioctl.h:2
double round(double value, int decimal_places)