Debian Codegraph @

maxdomeii.cpp

raw
1 /*
2 MaxDome II
3 Copyright (C) 2009 Ferran Casarramona ([email protected])
4
5 Migrated to INDI::Dome by Jasem Mutlaq (2014)
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #include "maxdomeii.h"
24 #include "maxdomeiidriver.h" // MaxDome II Command Set
25
26 #include <connectionplugins/connectionserial.h>
27
28 #include <memory>
29 #include <math.h>
30 #include <string.h>
31 #include <unistd.h>
32
33
34 // We declare an auto pointer to dome.
35 std::unique_ptr<MaxDomeII> dome(new MaxDomeII());
36
37 MaxDomeII::MaxDomeII()
38 {
39 nTicksPerTurn = 360;
40 nCurrentTicks = 0;
41 nParkPosition = 0.0;
42 nHomeAzimuth = 0.0;
43 nHomeTicks = 0;
44 nCloseShutterBeforePark = 0;
45 nTimeSinceShutterStart = -1; // No movement has started
46 nTimeSinceAzimuthStart = -1; // No movement has started
47 nTargetAzimuth = -1; //Target azimuth not established
48 nTimeSinceLastCommunication = 0;
49
50 SetDomeCapability(DOME_CAN_ABORT | DOME_CAN_ABS_MOVE | DOME_HAS_SHUTTER);
51
52 setVersion(INDI_MAXDOMEII_VERSION_MAJOR, INDI_MAXDOMEII_VERSION_MINOR);
53 }
54
55 bool MaxDomeII::SetupParms()
56 {
57 DomeAbsPosN[0].value = 0;
58
59 IDSetNumber(&DomeAbsPosNP, nullptr);
60 IDSetNumber(&DomeParamNP, nullptr);
61
62 return true;
63 }
64
65 bool MaxDomeII::Handshake()
66 {
67 driver.SetDevice(getDeviceName());
68 driver.SetPortFD(PortFD);
69
70 return !driver.Ack();
71 }
72
73 MaxDomeII::~MaxDomeII()
74 {
75 prev_az = prev_alt = 0;
76 }
77
78 const char *MaxDomeII::getDefaultName()
79 {
80 return (char *)"MaxDome II";
81 }
82
83 bool MaxDomeII::initProperties()
84 {
85 INDI::Dome::initProperties();
86
87 IUFillNumber(&HomeAzimuthN[0], "HOME_AZIMUTH", "Home azimuth", "%5.2f", 0., 360., 0., nHomeAzimuth);
88 IUFillNumberVector(&HomeAzimuthNP, HomeAzimuthN, NARRAY(HomeAzimuthN), getDeviceName(), "HOME_AZIMUTH",
89 "Home azimuth", OPTIONS_TAB, IP_RW, 0, IPS_IDLE);
90
91 // Ticks per turn
92 IUFillNumber(&TicksPerTurnN[0], "TICKS_PER_TURN", "Ticks per turn", "%5.2f", 100., 2000., 0., nTicksPerTurn);
93 IUFillNumberVector(&TicksPerTurnNP, TicksPerTurnN, NARRAY(TicksPerTurnN), getDeviceName(), "TICKS_PER_TURN",
94 "Ticks per turn", OPTIONS_TAB, IP_RW, 0, IPS_IDLE);
95
96 // Park position
97 IUFillNumber(&ShutterOperationAzimuthN[0], "SOp_AZIMUTH", "Azimuth", "%5.2f", 0., 360., 0., nParkPosition);
98 IUFillNumberVector(&ShutterOperationAzimuthNP, ShutterOperationAzimuthN, NARRAY(ShutterOperationAzimuthN),
99 getDeviceName(), "SHUTTER_OPERATION_AZIMUTH", "Shutter operation azimuth", OPTIONS_TAB, IP_RW, 0,
100 IPS_IDLE);
101
102 // Park on shutter
103 IUFillSwitch(&ShutterConflictS[0], "MOVE", "Move", ISS_ON);
104 IUFillSwitch(&ShutterConflictS[1], "NO_MOVE", "No move", ISS_OFF);
105 IUFillSwitchVector(&ShutterConflictSP, ShutterConflictS, NARRAY(ShutterConflictS), getDeviceName(),
106 "AZIMUTH_ON_SHUTTER", "Azimuth on operating shutter", OPTIONS_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
107
108 // Shutter mode
109 IUFillSwitch(&ShutterModeS[0], "FULL", "Open full", ISS_ON);
110 IUFillSwitch(&ShutterModeS[1], "UPPER", "Open upper only", ISS_OFF);
111 IUFillSwitchVector(&ShutterModeSP, ShutterModeS, NARRAY(ShutterModeS), getDeviceName(),
112 "SHUTTER_MODE", "Shutter open mode", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
113
114 // Home - Home command
115 IUFillSwitch(&HomeS[0], "HOME", "Home", ISS_OFF);
116 IUFillSwitchVector(&HomeSP, HomeS, NARRAY(HomeS), getDeviceName(), "HOME_MOTION", "Home dome", MAIN_CONTROL_TAB,
117 IP_RW, ISR_ATMOST1, 0, IPS_IDLE);
118
119 // Watch Dog
120 IUFillNumber(&WatchDogN[0], "WATCH_DOG_TIME", "Watch dog time", "%5.2f", 0., 3600., 0., 0.);
121 IUFillNumberVector(&WatchDogNP, WatchDogN, NARRAY(WatchDogN), getDeviceName(), "WATCH_DOG_TIME_SET",
122 "Watch dog time set", OPTIONS_TAB, IP_RW, 0, IPS_IDLE);
123
124 // Set default baud rate to 19200
125 serialConnection->setDefaultBaudRate(Connection::Serial::B_19200);
126
127 return true;
128 }
129
130 bool MaxDomeII::updateProperties()
131 {
132 INDI::Dome::updateProperties();
133
134 if (isConnected())
135 {
136 defineProperty(&HomeAzimuthNP);
137 defineProperty(&TicksPerTurnNP);
138 defineProperty(&ShutterOperationAzimuthNP);
139 defineProperty(&ShutterConflictSP);
140 defineProperty(&ShutterModeSP);
141 defineProperty(&HomeSP);
142 defineProperty(&WatchDogNP);
143
144 SetupParms();
145 }
146 else
147 {
148 deleteProperty(HomeAzimuthNP.name);
149 deleteProperty(TicksPerTurnNP.name);
150 deleteProperty(ShutterOperationAzimuthNP.name);
151 deleteProperty(ShutterConflictSP.name);
152 deleteProperty(ShutterModeSP.name);
153 deleteProperty(HomeSP.name);
154 deleteProperty(WatchDogNP.name);
155 }
156
157 return true;
158 }
159
160 bool MaxDomeII::saveConfigItems(FILE *fp)
161 {
162 IUSaveConfigNumber(fp, &HomeAzimuthNP);
163 IUSaveConfigNumber(fp, &TicksPerTurnNP);
164 IUSaveConfigNumber(fp, &ShutterOperationAzimuthNP);
165 IUSaveConfigSwitch(fp, &ShutterConflictSP);
166 IUSaveConfigSwitch(fp, &ShutterModeSP);
167
168 return INDI::Dome::saveConfigItems(fp);
169 }
170
171 bool MaxDomeII::Disconnect()
172 {
173 driver.Disconnect();
174
175 return INDI::Dome::Disconnect();
176 }
177
178 void MaxDomeII::TimerHit()
179 {
180 ShStatus shutterSt;
181 AzStatus nAzimuthStatus;
182 unsigned nHomePosition;
183 float nAz;
184 int nError;
185 int nRetry = 1;
186
187 if (isConnected() == false)
188 return; // No need to reset timer if we are not connected anymore
189
190 nError = driver.Status(&shutterSt, &nAzimuthStatus, &nCurrentTicks, &nHomePosition);
191 handle_driver_error(&nError, &nRetry); // This is a timer, we will not repeat in order to not delay the execution.
192
193 // Increment movment time counter
194 if (nTimeSinceShutterStart >= 0)
195 nTimeSinceShutterStart++;
196 if (nTimeSinceAzimuthStart >= 0)
197 nTimeSinceAzimuthStart++;
198
199 // Watch dog
200 nTimeSinceLastCommunication++;
201 if (WatchDogNP.np[0].value > 0 && WatchDogNP.np[0].value <= nTimeSinceLastCommunication)
202 {
203 // Close Shutter if it is not
204 if (shutterSt != SS_CLOSED)
205 {
206 DomeShutterSP.s = ControlShutter(SHUTTER_CLOSE);
207 IDSetSwitch(&DomeShutterSP, "Closing shutter due watch dog");
208 }
209 }
210
211 // if (getWeatherState() == IPS_ALERT)
212 // {
213 // // Close Shutter if it is not
214 // if (shutterSt != SS_CLOSED)
215 // {
216 // DomeShutterSP.s = ControlShutter(SHUTTER_CLOSE);
217 // IDSetSwitch(&DomeShutterSP, "Closing shutter due Weather conditions");
218 // }
219 // }
220
221 if (!nError)
222 {
223 // Shutter
224 switch (shutterSt)
225 {
226 case SS_CLOSED:
227 if (DomeShutterS[1].s == ISS_ON) // Close Shutter
228 {
229 if (DomeShutterSP.s == IPS_BUSY || DomeShutterSP.s == IPS_ALERT)
230 {
231 DomeShutterSP.s = IPS_OK; // Shutter close movement ends.
232 nTimeSinceShutterStart = -1;
233 IDSetSwitch(&DomeShutterSP, "Shutter is closed");
234 }
235 }
236 else
237 {
238 if (nTimeSinceShutterStart >= 0)
239 {
240 // A movement has started. Warn but don't change
241 if (nTimeSinceShutterStart >= 4)
242 {
243 DomeShutterSP.s = IPS_ALERT; // Shutter close movement ends.
244 IDSetSwitch(&DomeShutterSP, "Shutter still closed");
245 }
246 }
247 else
248 {
249 // For some reason (manual operation?) the shutter has closed
250 DomeShutterSP.s = IPS_IDLE;
251 DomeShutterS[1].s = ISS_ON;
252 DomeShutterS[0].s = ISS_OFF;
253 IDSetSwitch(&DomeShutterSP, "Unexpected shutter closed");
254 }
255 }
256 break;
257 case SS_OPENING:
258 if (DomeShutterS[0].s == ISS_OFF) // not opening Shutter
259 {
260 // For some reason the shutter is opening (manual operation?)
261 DomeShutterSP.s = IPS_ALERT;
262 DomeShutterS[1].s = ISS_OFF;
263 DomeShutterS[0].s = ISS_OFF;
264 IDSetSwitch(&DomeShutterSP, "Unexpected shutter opening");
265 }
266 else if (nTimeSinceShutterStart < 0)
267 {
268 // For some reason the shutter is opening (manual operation?)
269 DomeShutterSP.s = IPS_ALERT;
270 nTimeSinceShutterStart = 0;
271 IDSetSwitch(&DomeShutterSP, "Unexpected shutter opening");
272 }
273 else if (DomeShutterSP.s == IPS_ALERT)
274 {
275 // The alert has corrected
276 DomeShutterSP.s = IPS_BUSY;
277 IDSetSwitch(&DomeShutterSP, "Shutter is opening");
278 }
279 break;
280 case SS_OPEN:
281 if (DomeShutterS[0].s == ISS_ON) // Open Shutter
282 {
283 if (DomeShutterSP.s == IPS_BUSY || DomeShutterSP.s == IPS_ALERT)
284 {
285 DomeShutterSP.s = IPS_OK; // Shutter open movement ends.
286 nTimeSinceShutterStart = -1;
287 IDSetSwitch(&DomeShutterSP, "Shutter is open");
288 }
289 }
290 else
291 {
292 if (nTimeSinceShutterStart >= 0)
293 {
294 // A movement has started. Warn but don't change
295 if (nTimeSinceShutterStart >= 4)
296 {
297 DomeShutterSP.s = IPS_ALERT; // Shutter open movement alert.
298 IDSetSwitch(&DomeShutterSP, "Shutter still open");
299 }
300 }
301 else
302 {
303 // For some reason (manual operation?) the shutter has open
304 DomeShutterSP.s = IPS_IDLE;
305 DomeShutterS[1].s = ISS_ON;
306 DomeShutterS[0].s = ISS_OFF;
307 IDSetSwitch(&DomeShutterSP, "Unexpected shutter open");
308 }
309 }
310 break;
311 case SS_CLOSING:
312 if (DomeShutterS[1].s == ISS_OFF) // Not closing Shutter
313 {
314 // For some reason the shutter is closing (manual operation?)
315 DomeShutterSP.s = IPS_ALERT;
316 DomeShutterS[1].s = ISS_ON;
317 DomeShutterS[0].s = ISS_OFF;
318 IDSetSwitch(&DomeShutterSP, "Unexpected shutter closing");
319 }
320 else if (nTimeSinceShutterStart < 0)
321 {
322 // For some reason the shutter is opening (manual operation?)
323 DomeShutterSP.s = IPS_ALERT;
324 nTimeSinceShutterStart = 0;
325 IDSetSwitch(&DomeShutterSP, "Unexpected shutter closing");
326 }
327 else if (DomeShutterSP.s == IPS_ALERT)
328 {
329 // The alert has corrected
330 DomeShutterSP.s = IPS_BUSY;
331 IDSetSwitch(&DomeShutterSP, "Shutter is closing");
332 }
333 break;
334 case SS_ERROR:
335 DomeShutterSP.s = IPS_ALERT;
336 DomeShutterS[1].s = ISS_OFF;
337 DomeShutterS[0].s = ISS_OFF;
338 IDSetSwitch(&DomeShutterSP, "Shutter error");
339 break;
340 case SS_ABORTED:
341 default:
342 if (nTimeSinceShutterStart >= 0)
343 {
344 DomeShutterSP.s = IPS_ALERT; // Shutter movement aborted.
345 DomeShutterS[1].s = ISS_OFF;
346 DomeShutterS[0].s = ISS_OFF;
347 nTimeSinceShutterStart = -1;
348 IDSetSwitch(&DomeShutterSP, "Unknown shutter status");
349 }
350 break;
351 }
352
353 // Azimuth
354 nAz = TicksToAzimuth(nCurrentTicks);
355 if (DomeAbsPosN[0].value != nAz)
356 {
357 // Only refresh position if it changed
358 DomeAbsPosN[0].value = nAz;
359 //sprintf(buf,"%d", nCurrentTicks);
360 IDSetNumber(&DomeAbsPosNP, nullptr);
361 }
362
363 switch (nAzimuthStatus)
364 {
365 case AS_IDLE:
366 case AS_IDLE2:
367 if (nTimeSinceAzimuthStart > 3)
368 {
369 if (nTargetAzimuth >= 0 &&
370 AzimuthDistance(nTargetAzimuth, nCurrentTicks) > 3) // Maximum difference allowed: 3 ticks
371 {
372 DomeAbsPosNP.s = IPS_ALERT;
373 nTimeSinceAzimuthStart = -1;
374 IDSetNumber(&DomeAbsPosNP, "Could not position right");
375 }
376 else
377 {
378 // Succesfull end of movement
379 if (DomeAbsPosNP.s != IPS_OK)
380 {
381 setDomeState(DOME_SYNCED);
382 nTimeSinceAzimuthStart = -1;
383 LOG_INFO("Dome is on target position");
384 }
385 if (HomeS[0].s == ISS_ON)
386 {
387 HomeS[0].s = ISS_OFF;
388 HomeSP.s = IPS_OK;
389 nTimeSinceAzimuthStart = -1;
390 IDSetSwitch(&HomeSP, "Dome is homed");
391 }
392 }
393 }
394 break;
395 case AS_MOVING_WE:
396 case AS_MOVING_EW:
397 if (nTimeSinceAzimuthStart < 0)
398 {
399 nTimeSinceAzimuthStart = 0;
400 nTargetAzimuth = -1;
401 DomeAbsPosNP.s = IPS_ALERT;
402 IDSetNumber(&DomeAbsPosNP, "Unexpected dome moving");
403 }
404 break;
405 case AS_ERROR:
406 if (nTimeSinceAzimuthStart >= 0)
407 {
408 DomeAbsPosNP.s = IPS_ALERT;
409 nTimeSinceAzimuthStart = -1;
410 nTargetAzimuth = -1;
411 IDSetNumber(&DomeAbsPosNP, "Dome Error");
412 }
413 default:
414 break;
415 }
416 }
417 else
418 {
419 LOGF_DEBUG("Error: %s. Please reconnect and try again.", ErrorMessages[-nError]);
420 return;
421 }
422
423 SetTimer(getCurrentPollingPeriod());
424 return;
425 }
426
427 IPState MaxDomeII::MoveAbs(double newAZ)
428 {
429 double currAZ = 0;
430 int newPos = 0, nDir = 0;
431 int error;
432 int nRetry = 3;
433
434 currAZ = DomeAbsPosN[0].value;
435
436 // Take the shortest path
437 if (newAZ > currAZ)
438 {
439 if (newAZ - currAZ > 180.0)
440 nDir = MAXDOMEII_WE_DIR;
441 else
442 nDir = MAXDOMEII_EW_DIR;
443 }
444 else
445 {
446 if (currAZ - newAZ > 180.0)
447 nDir = MAXDOMEII_EW_DIR;
448 else
449 nDir = MAXDOMEII_WE_DIR;
450 }
451
452 newPos = AzimuthToTicks(newAZ);
453
454 while (nRetry)
455 {
456 error = driver.GotoAzimuth(nDir, newPos);
457 handle_driver_error(&error, &nRetry);
458 }
459
460 if (error != 0)
461 return IPS_ALERT;
462
463 nTargetAzimuth = newPos;
464 nTimeSinceAzimuthStart = 0; // Init movement timer
465
466 // It will take a few cycles to reach final position
467 return IPS_BUSY;
468 }
469
470 IPState MaxDomeII::Move(DomeDirection dir, DomeMotionCommand operation)
471 {
472 int error;
473 int nRetry = 3;
474
475 if (operation == MOTION_START)
476 {
477 LOGF_DEBUG("Move dir=%d", dir);
478 double currAZ = DomeAbsPosN[0].value;
479 double newAZ = currAZ > 180 ? currAZ - 180 : currAZ + 180;
480 int newPos = AzimuthToTicks(newAZ);
481 int nDir = dir ? MAXDOMEII_WE_DIR : MAXDOMEII_EW_DIR;
482
483 while (nRetry)
484 {
485 error = driver.GotoAzimuth(nDir, newPos);
486 handle_driver_error(&error, &nRetry);
487 }
488
489 if (error != 0)
490 return IPS_ALERT;
491
492 nTargetAzimuth = newPos;
493 nTimeSinceAzimuthStart = 0; // Init movement timer
494 return IPS_BUSY;
495 }
496 else
497 {
498 LOG_DEBUG("Stop movement");
499 while (nRetry)
500 {
501 error = driver.AbortAzimuth();
502 handle_driver_error(&error, &nRetry);
503 }
504
505 if (error != 0)
506 return IPS_ALERT;
507
508 DomeAbsPosNP.s = IPS_IDLE;
509 IDSetNumber(&DomeAbsPosNP, NULL);
510 nTimeSinceAzimuthStart = -1;
511 }
512
513 return IPS_OK;
514 }
515
516 bool MaxDomeII::Abort()
517 {
518 int error = 0;
519 int nRetry = 3;
520
521 while (nRetry)
522 {
523 error = driver.AbortAzimuth();
524 handle_driver_error(&error, &nRetry);
525 }
526
527 while (nRetry)
528 {
529 error = driver.AbortShutter();
530 handle_driver_error(&error, &nRetry);
531 }
532
533 DomeAbsPosNP.s = IPS_IDLE;
534 IDSetNumber(&DomeAbsPosNP, nullptr);
535
536 // If we abort while in the middle of opening/closing shutter, alert.
537 if (DomeShutterSP.s == IPS_BUSY)
538 {
539 DomeShutterSP.s = IPS_ALERT;
540 IDSetSwitch(&DomeShutterSP, "Shutter operation aborted.");
541 return false;
542 }
543
544 return true;
545 }
546
547 /**************************************************************************************
548 **
549 ***************************************************************************************/
550 bool MaxDomeII::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
551 {
552 // Ignore if not ours
553 if (strcmp(dev, getDeviceName()))
554 return false;
555
556 nTimeSinceLastCommunication = 0;
557
558 // ===================================
559 // TicksPerTurn
560 // ===================================
561 if (!strcmp(name, TicksPerTurnNP.name))
562 {
563 double nVal;
564 char cLog[255];
565 int error;
566 int nRetry = 3;
567
568 if (IUUpdateNumber(&TicksPerTurnNP, values, names, n) < 0)
569 return false;
570
571 nVal = values[0];
572 if (nVal >= 100 && nVal <= 2000)
573 {
574 while (nRetry)
575 {
576 error = driver.SetTicksPerTurn(nVal);
577 handle_driver_error(&error, &nRetry);
578 }
579 if (error >= 0)
580 {
581 sprintf(cLog, "New Ticks Per Turn set: %lf", nVal);
582 nTicksPerTurn = nVal;
583 nHomeTicks = floor(0.5 + nHomeAzimuth * nTicksPerTurn / 360.0); // Calculate Home ticks again
584 TicksPerTurnNP.s = IPS_OK;
585 TicksPerTurnNP.np[0].value = nVal;
586 IDSetNumber(&TicksPerTurnNP, "%s", cLog);
587 return true;
588 }
589 else
590 {
591 LOGF_ERROR("MAX DOME II: %s", ErrorMessages[-error]);
592 TicksPerTurnNP.s = IPS_ALERT;
593 IDSetNumber(&TicksPerTurnNP, nullptr);
594 }
595
596 return false;
597 }
598
599 // Incorrect value.
600 TicksPerTurnNP.s = IPS_ALERT;
601 IDSetNumber(&TicksPerTurnNP, "Invalid Ticks Per Turn");
602
603 return false;
604 }
605
606 // ===================================
607 // HomeAzimuth
608 // ===================================
609 if (!strcmp(name, HomeAzimuthNP.name))
610 {
611 double nVal;
612 char cLog[255];
613
614 if (IUUpdateNumber(&HomeAzimuthNP, values, names, n) < 0)
615 return false;
616
617 nVal = values[0];
618 if (nVal >= 0 && nVal <= 360)
619 {
620 sprintf(cLog, "New home azimuth set: %lf", nVal);
621 nHomeAzimuth = nVal;
622 nHomeTicks = floor(0.5 + nHomeAzimuth * nTicksPerTurn / 360.0);
623 HomeAzimuthNP.s = IPS_OK;
624 HomeAzimuthNP.np[0].value = nVal;
625 IDSetNumber(&HomeAzimuthNP, "%s", cLog);
626 return true;
627 }
628 // Incorrect value.
629 HomeAzimuthNP.s = IPS_ALERT;
630 IDSetNumber(&HomeAzimuthNP, "Invalid home azimuth");
631
632 return false;
633 }
634
635 // ===================================
636 // Watch dog
637 // ===================================
638 if (!strcmp(name, WatchDogNP.name))
639 {
640 double nVal;
641 char cLog[255];
642
643 if (IUUpdateNumber(&WatchDogNP, values, names, n) < 0)
644 return false;
645
646 nVal = values[0];
647 if (nVal >= 0 && nVal <= 3600)
648 {
649 sprintf(cLog, "New watch dog set: %lf", nVal);
650 WatchDogNP.s = IPS_OK;
651 WatchDogNP.np[0].value = nVal;
652 IDSetNumber(&WatchDogNP, "%s", cLog);
653 return true;
654 }
655 // Incorrect value.
656 WatchDogNP.s = IPS_ALERT;
657 IDSetNumber(&WatchDogNP, "Invalid watch dog time");
658
659 return false;
660 }
661
662 // ===================================
663 // Shutter operation azimuth
664 // ===================================
665 if (!strcmp(name, ShutterOperationAzimuthNP.name))
666 {
667 double nVal;
668 IPState error;
669
670 if (IUUpdateNumber(&ShutterOperationAzimuthNP, values, names, n) < 0)
671 return false;
672
673 nVal = values[0];
674 if (nVal >= 0 && nVal < 360)
675 {
676 error = ConfigurePark(nCloseShutterBeforePark, nVal);
677
678 if (error == IPS_OK)
679 {
680 nParkPosition = nVal;
681 ShutterOperationAzimuthNP.s = IPS_OK;
682 ShutterOperationAzimuthNP.np[0].value = nVal;
683 IDSetNumber(&ShutterOperationAzimuthNP, "New shutter operation azimuth set");
684 }
685 else
686 {
687 ShutterOperationAzimuthNP.s = IPS_ALERT;
688 IDSetNumber(&ShutterOperationAzimuthNP, "%s", ErrorMessages[-error]);
689 }
690
691 return true;
692 }
693 // Incorrect value.
694 ShutterOperationAzimuthNP.s = IPS_ALERT;
695 IDSetNumber(&ShutterOperationAzimuthNP, "Invalid shutter operation azimuth position");
696
697 return false;
698 }
699
700 return INDI::Dome::ISNewNumber(dev, name, values, names, n);
701 }
702
703 /**************************************************************************************
704 **
705 ***************************************************************************************/
706 bool MaxDomeII::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
707 {
708 // ignore if not ours //
709 if (strcmp(getDeviceName(), dev))
710 return false;
711
712 nTimeSinceLastCommunication = 0;
713
714 // ===================================
715 // Home
716 // ===================================
717 if (!strcmp(name, HomeSP.name))
718 {
719 if (IUUpdateSwitch(&HomeSP, states, names, n) < 0)
720 return false;
721
722 int error;
723 int nRetry = 3;
724
725 while (nRetry)
726 {
727 error = driver.HomeAzimuth();
728 handle_driver_error(&error, &nRetry);
729 }
730 nTimeSinceAzimuthStart = 0;
731 nTargetAzimuth = -1;
732 if (error)
733 {
734 LOGF_ERROR("Error Homing Azimuth (%s).", ErrorMessages[-error]);
735 HomeSP.s = IPS_ALERT;
736 IDSetSwitch(&HomeSP, "Error Homing Azimuth");
737 return false;
738 }
739 HomeSP.s = IPS_BUSY;
740 IDSetSwitch(&HomeSP, "Homing dome");
741
742 return true;
743 }
744
745 // ===================================
746 // Conflict on Shutter operation
747 // ===================================
748 if (!strcmp(name, ShutterConflictSP.name))
749 {
750 if (IUUpdateSwitch(&ShutterConflictSP, states, names, n) < 0)
751 return false;
752
753 int nCSBP = ShutterConflictS[0].s == ISS_ON ? 1 : 0;
754 int error = ConfigurePark(nCSBP, nParkPosition);
755
756 if (error == IPS_OK)
757 {
758 ShutterConflictSP.s = IPS_OK;
759 IDSetSwitch(&ShutterConflictSP, "New shutter operation conflict set");
760 }
761 else
762 {
763 ShutterConflictSP.s = IPS_ALERT;
764 IDSetSwitch(&ShutterConflictSP, "%s", ErrorMessages[-error]);
765 }
766 return true;
767 }
768
769 if (!strcmp(name, ShutterModeSP.name))
770 {
771 if (IUUpdateSwitch(&ShutterModeSP, states, names, n) < 0)
772 return false;
773
774 ShutterModeSP.s = IPS_OK;
775 IDSetSwitch(&ShutterModeSP, "Shutter opening mode set");
776 return true;
777 }
778
779 return INDI::Dome::ISNewSwitch(dev, name, states, names, n);
780 }
781
782 /**************************************************************************************
783 **
784 ***************************************************************************************/
785 int MaxDomeII::AzimuthDistance(int nPos1, int nPos2)
786 {
787 int nDif = std::abs(nPos1 - nPos2);
788
789 if (nDif > nTicksPerTurn / 2)
790 nDif = nTicksPerTurn - nDif;
791
792 return nDif;
793 }
794
795 /**************************************************************************************
796 **
797 ***************************************************************************************/
798 double MaxDomeII::TicksToAzimuth(int nTicks)
799 {
800 double nAz;
801
802 nAz = nHomeAzimuth + nTicks * 360.0 / nTicksPerTurn;
803 while (nAz < 0)
804 nAz += 360;
805 while (nAz >= 360)
806 nAz -= 360;
807
808 return nAz;
809 }
810 /**************************************************************************************
811 **
812 ***************************************************************************************/
813 int MaxDomeII::AzimuthToTicks(double nAzimuth)
814 {
815 int nTicks;
816
817 nTicks = floor(0.5 + (nAzimuth - nHomeAzimuth) * nTicksPerTurn / 360.0);
818 while (nTicks > nTicksPerTurn)
819 nTicks -= nTicksPerTurn;
820 while (nTicks < 0)
821 nTicks += nTicksPerTurn;
822
823 return nTicks;
824 }
825
826 /**************************************************************************************
827 **
828 ***************************************************************************************/
829
830 int MaxDomeII::handle_driver_error(int *error, int *nRetry)
831 {
832 (*nRetry)--;
833 switch (*error)
834 {
835 case 0: // No error. Continue
836 *nRetry = 0;
837 break;
838 case -5: // can't connect
839 // This error can happen if port connection is lost, i.e. a usb-serial reconnection
840 // Reconnect
841 LOG_ERROR("MAX DOME II: Reconnecting ...");
842 Connect();
843 if (PortFD < 0)
844 *nRetry = 0; // Can't open the port. Don't retry anymore.
845 break;
846
847 default: // Do nothing in all other errors.
848 LOGF_ERROR("Error on command: (%s).", ErrorMessages[-*error]);
849 break;
850 }
851
852 return *nRetry;
853 }
854
855 /************************************************************************************
856 *
857 * ***********************************************************************************/
858 IPState MaxDomeII::ConfigurePark(int nCSBP, double ParkAzimuth)
859 {
860 int error = 0;
861 int nRetry = 3;
862
863 // Only update park position if there is change
864 if (ParkAzimuth != nParkPosition || nCSBP != nCloseShutterBeforePark)
865 {
866 while (nRetry)
867 {
868 error = driver.SetPark(nCSBP, AzimuthToTicks(ParkAzimuth));
869 handle_driver_error(&error, &nRetry);
870 }
871 if (error >= 0)
872 {
873 nParkPosition = ParkAzimuth;
874 nCloseShutterBeforePark = nCSBP;
875 LOGF_INFO("New park position set. %d %d", nCSBP, AzimuthToTicks(ParkAzimuth));
876 }
877 else
878 {
879 LOGF_ERROR("MAX DOME II: %s", ErrorMessages[-error]);
880 return IPS_ALERT;
881 }
882 }
883
884 return IPS_OK;
885 }
886
887 /************************************************************************************
888 *
889 * ***********************************************************************************/
890 bool MaxDomeII::SetCurrentPark()
891 {
892 SetAxis1Park(DomeAbsPosN[0].value);
893 return true;
894 }
895 /************************************************************************************
896 *
897 * ***********************************************************************************/
898
899 bool MaxDomeII::SetDefaultPark()
900 {
901 // By default set position to 0
902 SetAxis1Park(0);
903 return true;
904 }
905
906 /************************************************************************************
907 *
908 * ***********************************************************************************/
909 IPState MaxDomeII::ControlShutter(ShutterOperation operation)
910 {
911 int error = 0;
912 int nRetry = 3;
913
914 if (operation == SHUTTER_CLOSE)
915 {
916 while (nRetry)
917 {
918 error = driver.CloseShutter();
919 handle_driver_error(&error, &nRetry);
920 }
921 nTimeSinceShutterStart = 0; // Init movement timer
922 if (error)
923 {
924 LOGF_ERROR("Error closing shutter (%s).", ErrorMessages[-error]);
925 return IPS_ALERT;
926 }
927 return IPS_BUSY;
928 }
929 else
930 {
931 if (ShutterModeS[0].s == ISS_ON)
932 {
933 // Open Shutter
934 while (nRetry)
935 {
936 error = driver.OpenShutter();
937 handle_driver_error(&error, &nRetry);
938 }
939 nTimeSinceShutterStart = 0; // Init movement timer
940 if (error)
941 {
942 LOGF_ERROR("Error opening shutter (%s).", ErrorMessages[-error]);
943 return IPS_ALERT;
944 }
945 return IPS_BUSY;
946 }
947 else
948 {
949 // Open upper shutter only
950 while (nRetry)
951 {
952 error = driver.OpenUpperShutterOnly();
953 handle_driver_error(&error, &nRetry);
954 }
955 nTimeSinceShutterStart = 0; // Init movement timer
956 if (error)
957 {
958 LOGF_ERROR("Error opening upper shutter only (%s).", ErrorMessages[-error]);
959 return IPS_ALERT;
960 }
961 return IPS_BUSY;
962 }
963 }
964
965 return IPS_ALERT;
966 }