Instrument Neutral Distributed Interface INDI 2.2.5
scopedome_dome.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 ScopeDome Dome INDI Driver
3
4 Copyright(c) 2017-2024 Jarno Paananen. All rights reserved.
5
6 based on:
7
8 ScopeDome Windows ASCOM driver version 5.1.30
9
10 and
11
12 Baader Planetarium Dome INDI Driver
13
14 Copyright(c) 2014 Jasem Mutlaq. All rights reserved.
15
16 Baader Dome INDI Driver
17
18 This library is free software; you can redistribute it and/or
19 modify it under the terms of the GNU Library General Public
20 License version 2 as published by the Free Software Foundation.
21 .
22 This library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Library General Public License for more details.
26 .
27 You should have received a copy of the GNU Library General Public License
28 along with this library; see the file COPYING.LIB. If not, write to
29 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA.
31*******************************************************************************/
32
33#include "scopedome_dome.h"
34#include "scopedome_arduino.h"
35#include "scopedome_sim.h"
36#include "scopedome_usb21.h"
37
39#include "indicom.h"
40
41#include <cmath>
42#include <cstdio>
43#include <cstring>
44#include <memory>
45#include <termios.h>
46#include <unistd.h>
47#include <wordexp.h>
48
49// We declare an auto pointer to ScopeDome.
50std::unique_ptr<ScopeDome> scopeDome(new ScopeDome());
51
53{
54 setVersion(2, 1);
55 targetAz = 0;
60
62 // ScopeDome rotates to the park position first and closes the shutter afterwards (see TimerHit()).
64
65 stepsPerRevolution = ~0;
66
67 // Load dome inertia table if present
68 wordexp_t wexp;
69 if (wordexp("~/.indi/ScopeDome_DomeInertia_Table.txt", &wexp, 0) == 0)
70 {
71 FILE *inertia = fopen(wexp.we_wordv[0], "r");
72 if (inertia)
73 {
74 // skip UTF-8 marker bytes
75 fseek(inertia, 3, SEEK_SET);
76 char line[100];
77 int lineNum = 0;
78
79 while (fgets(line, sizeof(line), inertia))
80 {
81 int step, result;
82 // File format is "steps ; actual steps ;" but with varying number of spaces
83 char* splitter = strchr(line, ';');
84 if(splitter)
85 {
86 *splitter++ = 0;
87
88 step = atoi(line);
89 result = atoi(splitter);
90 if (step == lineNum)
91 {
92 inertiaTable.push_back(result);
93 }
94 }
95 lineNum++;
96 }
97 fclose(inertia);
98 LOGF_INFO("Read inertia file %s", wexp.we_wordv[0]);
99 }
100 else
101 {
102 LOG_WARN("Could not read inertia file, please generate one with Windows "
103 "driver setup and copy to "
104 "~/.indi/ScopeDome_DomeInertia_Table.txt");
105 }
106 }
107 wordfree(&wexp);
108}
109
111{
113
114 DomeHomePositionNP[0].fill("DH_POSITION", "AZ (deg)", "%6.2f", 0.0, 360.0, 1.0, 0.0);
115 DomeHomePositionNP.fill(getDeviceName(), "DOME_HOME_POSITION",
116 "Home sensor position", SITE_TAB, IP_RW, 60, IPS_OK);
117
118 HomeSensorPolaritySP[ScopeDomeCard::ACTIVE_HIGH].fill("ACTIVE_HIGH", "Active high", ISS_ON);
119 HomeSensorPolaritySP[ScopeDomeCard::ACTIVE_LOW].fill("ACTIVE_LOW", "Active low", ISS_OFF);
120 HomeSensorPolaritySP.fill(getDeviceName(), "HOME_SENSOR_POLARITY",
121 "Home sensor polarity", SITE_TAB, IP_RW, ISR_1OFMANY, 0, IPS_OK);
122
123 FindHomeSP[0].fill("START", "Start", ISS_OFF);
124 FindHomeSP.fill(getDeviceName(), "FIND_HOME", "Find home", MAIN_CONTROL_TAB, IP_RW,
125 ISR_ATMOST1, 0, IPS_OK);
126
127 DerotateSP[0].fill("START", "Start", ISS_OFF);
128 DerotateSP.fill(getDeviceName(), "DEROTATE", "Derotate", MAIN_CONTROL_TAB, IP_RW,
129 ISR_ATMOST1, 0, IPS_OK);
130
131 FirmwareVersionsNP[0].fill("MAIN", "Main part", "%2.2f", 0.0, 99.0, 1.0, 0.0);
132 FirmwareVersionsNP[1].fill("ROTARY", "Rotary part", "%2.1f", 0.0, 99.0, 1.0, 0.0);
133 FirmwareVersionsNP.fill(getDeviceName(), "FIRMWARE_VERSION",
134 "Firmware versions", INFO_TAB, IP_RO, 60, IPS_IDLE);
135
136 StepsPerRevolutionNP[0].fill("STEPS", "Steps per revolution", "%5.0f", 0.0, 99999.0, 1.0, 0.0);
137 StepsPerRevolutionNP.fill(getDeviceName(), "CALIBRATION_VALUES",
138 "Calibration values", SITE_TAB, IP_RO, 60, IPS_IDLE);
139
140 CalibrationNeededSP[0].fill("CALIBRATION_NEEDED", "Calibration needed", ISS_OFF);
141 CalibrationNeededSP.fill(getDeviceName(), "CALIBRATION_STATUS",
142 "Calibration status", SITE_TAB, IP_RO, ISR_ATMOST1, 0, IPS_IDLE);
143
144 StartCalibrationSP[0].fill("START", "Start", ISS_OFF);
145 StartCalibrationSP.fill(getDeviceName(), "RUN_CALIBRATION", "Run calibration",
147
148 char defaultUsername[MAXINDINAME] = "scopedome";
149 char defaultPassword[MAXINDINAME] = "default";
150 IUGetConfigText(getDeviceName(), "CREDENTIALS", "USERNAME", defaultUsername, MAXINDINAME);
151 IUGetConfigText(getDeviceName(), "CREDENTIALS", "PASSWORD", defaultPassword, MAXINDINAME);
152 CredentialsTP[0].fill("USERNAME", "User name", defaultUsername);
153 CredentialsTP[1].fill("PASSWORD", "Password", defaultPassword);
154 CredentialsTP.fill(getDeviceName(), "CREDENTIALS", "Credentials", CONNECTION_TAB, IP_RW, 0, IPS_OK);
155
156 ISState usbcard21 = ISS_ON;
157 ISState arduino = ISS_OFF;
158 IUGetConfigSwitch(getDeviceName(), "CARD_TYPE", "USB_CARD_21", &usbcard21);
159 IUGetConfigSwitch(getDeviceName(), "CARD_TYPE", "ARDUINO", &arduino);
160 CardTypeSP[SCOPEDOME_USB21].fill("USB_CARD_21", "USB Card 2.1", usbcard21);
161 CardTypeSP[SCOPEDOME_ARDUINO].fill("ARDUINO", "Arduino Card", arduino);
162 CardTypeSP.fill(getDeviceName(), "CARD_TYPE", "Card type",
164
165 switch(CardTypeSP.findOnSwitchIndex())
166 {
167 case SCOPEDOME_USB21:
168 default:
170 break;
172 serialConnection->setDefaultBaudRate(Connection::Serial::B_9600);
173 break;
174 }
175
178
181
182 setPollingPeriodRange(1000, 3000); // Device doesn't like too long interval
184 return true;
185}
186
187/************************************************************************************
188 *
189 * ***********************************************************************************/
191{
192 targetAz = 0;
193
194 stepsPerRevolution = interface->getStepsPerRevolution();
195 LOGF_INFO("Steps per revolution read as %d", stepsPerRevolution);
196 StepsPerRevolutionNP[0].setValue(stepsPerRevolution);
198 StepsPerRevolutionNP.apply();
199
200 if (UpdatePosition())
201 DomeAbsPosNP.apply();
202
204 DomeShutterSP.apply();
205
208
209 if (InitPark())
210 {
211 // If loading parking data is successful, we just set the default parking
212 // values.
214 }
215 else
216 {
217 // Otherwise, we set all parking data to default in case no parking data is
218 // found.
219 SetAxis1Park(0);
221 }
222
223 bool calibrationNeeded = interface->isCalibrationNeeded();
224 CalibrationNeededSP[0].setState(calibrationNeeded ? ISS_ON : ISS_OFF);
225 CalibrationNeededSP.setState(IPS_OK);
226 CalibrationNeededSP.apply();
227
228 double fwMain, fwRotary;
229 interface->getFirmwareVersions(fwMain, fwRotary);
230 FirmwareVersionsNP[0].setValue(fwMain);
231 FirmwareVersionsNP[1].setValue(fwRotary);
232 FirmwareVersionsNP.setState(IPS_OK);
233 FirmwareVersionsNP.apply();
234
235 rotaryLinkEstablished = interface->getInputState(ScopeDomeCard::ROTARY_LINK);
236 if(rotaryLinkEstablished == ISS_OFF)
237 {
238 LOG_WARN("Rotary link not established, shutter control not possible");
239 }
240 return true;
241}
242
243/************************************************************************************
244 *
245 ************************************************************************************/
247{
248 if(reconnecting)
249 return true;
250
251 sim = isSimulation();
252
253 if (sim)
254 {
255 interface.reset(static_cast<ScopeDomeCard *>(new ScopeDomeSim()));
256 }
257 else
258 {
259 switch(CardTypeSP.findOnSwitchIndex())
260 {
261 case SCOPEDOME_USB21:
262 default:
263 interface.reset(static_cast<ScopeDomeCard *>(new ScopeDomeUSB21(this, PortFD)));
264 break;
266 interface.reset(static_cast<ScopeDomeCard *>(new ScopeDomeArduino(this, getActiveConnection())));
267 break;
268 }
269 }
270 if (interface->detect())
271 {
272 return true;
273 }
274 LOG_ERROR("Can't identify the card, check card type and baud rate (115200 for USB Card 2.1, 9600 for Arduino Card)");
275 return false;
276}
277
278/************************************************************************************
279 *
280 ************************************************************************************/
282{
283 return (const char *)"ScopeDome Dome";
284}
285
286/************************************************************************************
287 *
288 ************************************************************************************/
290{
292
293 if (isConnected())
294 {
295 // Initialize dynamic properties
296 {
297 size_t numSensors = interface->getNumberOfSensors();
298 SensorsNP.resize(numSensors);
299 for(size_t i = 0; i < numSensors; i++)
300 {
301 auto info = interface->getSensorInfo(i);
302 SensorsNP[i].fill(info.propName.c_str(), info.label.c_str(),
303 info.format.c_str(), info.minValue, info.maxValue, 1.0, 0.0);
304 }
305 SensorsNP.fill(getDeviceName(), "SCOPEDOME_SENSORS",
306 "Sensors", INFO_TAB, IP_RO, 60, IPS_IDLE);
307 }
308
309 {
310 size_t numRelays = interface->getNumberOfRelays();
311 RelaysSP.resize(numRelays);
312 for(size_t i = 0; i < numRelays; i++)
313 {
314 auto info = interface->getRelayInfo(i);
315 RelaysSP[i].fill(info.propName.c_str(), info.label.c_str(), ISS_OFF);
316 }
317 RelaysSP.fill(getDeviceName(), "RELAYS", "Relays", MAIN_CONTROL_TAB, IP_RW,
319 0, IPS_IDLE);
320 }
321
322 {
323 size_t numInputs = interface->getNumberOfInputs();
324 InputsSP.resize(numInputs);
325 for(size_t i = 0; i < numInputs; i++)
326 {
327 auto info = interface->getInputInfo(i);
328 InputsSP[i].fill(info.propName.c_str(), info.label.c_str(), ISS_OFF);
329 }
330 InputsSP.fill(getDeviceName(), "INPUTS", "Inputs", INFO_TAB, IP_RO,
332 }
333
345 SetupParms();
346 }
347 else
348 {
349 deleteProperty(FindHomeSP.getName());
350 deleteProperty(DerotateSP.getName());
353 deleteProperty(SensorsNP.getName());
354 deleteProperty(RelaysSP.getName());
355 deleteProperty(InputsSP.getName());
360 }
361
362 return true;
363}
364
365/************************************************************************************
366 *
367 * ***********************************************************************************/
368bool ScopeDome::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
369{
370 // ignore if not ours
371 if (dev == nullptr || strcmp(dev, getDeviceName()))
372 return false;
373
374 if (FindHomeSP.isNameMatch(name))
375 {
376 if (status != DOME_HOMING)
377 {
378 LOG_INFO("Finding home sensor");
380 FindHomeSP.reset();
381 DomeAbsPosNP.setState(IPS_BUSY);
382 FindHomeSP.setState(IPS_BUSY);
383 FindHomeSP.apply();
384 interface->findHome();
385 }
386 return true;
387 }
388
389 if (DerotateSP.isNameMatch(name))
390 {
391 if (status != DOME_DEROTATING)
392 {
393 LOG_INFO("De-rotating started");
395 DerotateSP.reset();
396 DomeAbsPosNP.setState(IPS_BUSY);
397 DerotateSP.setState(IPS_BUSY);
398 DerotateSP.apply();
399 }
400 return true;
401 }
402
403 if (StartCalibrationSP.isNameMatch(name))
404 {
406 {
407 LOG_INFO("Calibration started");
409 StartCalibrationSP.reset();
410 DomeAbsPosNP.setState(IPS_BUSY);
412 StartCalibrationSP.apply();
413 interface->calibrate();
414 }
415 return true;
416 }
417
418 if (RelaysSP.isNameMatch(name))
419 {
420 RelaysSP.update(states, names, n);
421 for(int i = 0; i < n; i++)
422 {
423 interface->setRelayState(i, RelaysSP[i].getState());
424 }
425 RelaysSP.apply();
426 return true;
427 }
428
429 if (CardTypeSP.isNameMatch(name))
430 {
431 CardTypeSP.update(states, names, n);
432 CardTypeSP.apply();
433
434 if(CardTypeSP[0].getState() == ISS_ON)
435 {
436 // USB Card 2.1 uses 115200 bauds
439 }
440 else
441 {
442 // Arduino Card uses 9600 bauds
443 serialConnection->setDefaultBaudRate(Connection::Serial::B_9600);
445 }
446 return true;
447 }
448 if (HomeSensorPolaritySP.isNameMatch(name))
449 {
450 HomeSensorPolaritySP.update(states, names, n);
451 HomeSensorPolaritySP.apply();
452
454 {
455 interface->setHomeSensorPolarity(ScopeDomeCard::ACTIVE_LOW);
456 LOG_DEBUG("Home sensor polarity set to ACTIVE_LOW");
457 }
458 else
459 {
460 interface->setHomeSensorPolarity(ScopeDomeCard::ACTIVE_HIGH);
461 LOG_DEBUG("Home sensor polarity set to ACTIVE_HIGH");
462 }
463 return true;
464 }
465
466 // Pass to base class
467 return INDI::Dome::ISNewSwitch(dev, name, states, names, n);
468}
469
470bool ScopeDome::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
471{
472 // ignore if not ours
473 if (dev == nullptr || strcmp(dev, getDeviceName()))
474 return false;
475
476 if (DomeHomePositionNP.isNameMatch(name))
477 {
478 DomeHomePositionNP.update(values, names, n);
479 DomeHomePositionNP.setState(IPS_OK);
480 DomeHomePositionNP.apply();
481 LOGF_DEBUG("Dome home position set to %f", DomeHomePositionNP[0].getValue());
482 return true;
483 }
484 return INDI::Dome::ISNewNumber(dev, name, values, names, n);
485}
486
487bool ScopeDome::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
488{
489 // ignore if not ours
490 if (dev == nullptr || strcmp(dev, getDeviceName()))
491 return false;
492
493 if (CredentialsTP.isNameMatch(name))
494 {
495 CredentialsTP.update(texts, names, n);
496 CredentialsTP.apply();
497 return true;
498 }
499 return INDI::Dome::ISNewText(dev, name, texts, names, n);
500}
501
502/************************************************************************************
503 *
504 * ***********************************************************************************/
506{
507 int rc = 0;
508 if (rc != 0)
509 {
510 LOGF_ERROR("Error reading input state: %d", rc);
511 return false;
512 }
513
514 for(size_t i = 0; i < InputsSP.size(); i++)
515 {
516 InputsSP[i].setState(interface->getInputValue(i) ? ISS_ON : ISS_OFF);
517 }
518 InputsSP.setState(IPS_OK);
519 InputsSP.apply();
520
522 {
523 if (interface->getInputState(ScopeDomeCard::OPEN1) == ISS_ON) // shutter open switch triggered
524 {
526 {
528 interface->controlShutter(ScopeDomeCard::STOP_SHUTTER);
530 {
531 SetParked(false);
532 }
534 }
535 }
536 else if (interface->getInputState(ScopeDomeCard::CLOSED1) == ISS_ON) // shutter closed switch triggered
537 {
539 {
541 interface->controlShutter(ScopeDomeCard::STOP_SHUTTER);
542
543 if (getDomeState() == DOME_PARKING && DomeAbsPosNP.getState() != IPS_BUSY)
544 {
545 SetParked(true);
546 }
548 }
549 }
550 }
551 else
552 {
553 if (interface->getInputState(ScopeDomeCard::OPEN1) == ISS_ON) // shutter open switch triggered
554 {
556 }
557 else if (interface->getInputState(ScopeDomeCard::CLOSED1) == ISS_ON) // shutter closed switch triggered
558 {
560 }
561 }
562
563 ISState link = interface->getInputState(ScopeDomeCard::ROTARY_LINK);
564 if (link != rotaryLinkEstablished)
565 {
566 rotaryLinkEstablished = link;
567 if(rotaryLinkEstablished == ISS_OFF)
568 {
569 LOG_WARN("Rotary link not established, shutter control not possible");
570 }
571 else
572 {
573 LOG_WARN("Rotary link established, shutter control now possible");
574
575 if (reconnected && m_ShutterState == SHUTTER_MOVING)
576 {
577 // We have just reconnected the serial port, possibly due to controller reset.
578 // If we were shutting or opening the shutter, retry that operation now that we have shutter link.
580 {
581 LOG_WARN("Retrying closing shutter");
582 interface->controlShutter(ScopeDomeCard::CLOSE_SHUTTER);
583 }
584 else if (targetShutter == SHUTTER_OPEN)
585 {
586 LOG_WARN("Retrying opening shutter");
587 interface->controlShutter(ScopeDomeCard::OPEN_SHUTTER);
588 }
589 reconnected = false;
590 }
591 }
592 }
593 return true;
594}
595
596/************************************************************************************
597 *
598 * ***********************************************************************************/
600{
601 rotationCounter = interface->getRotationCounter();
602
603 // We assume counter value 0 is at home sensor position and grows counter clockwise as this is
604 // how USB Card 2.1 works.
605 double az = ((double)rotationCounter * -360.0 / stepsPerRevolution) + DomeHomePositionNP[0].getValue();
606 az = fmod(az, 360.0);
607 if (az < 0.0)
608 {
609 az += 360.0;
610 }
611 DomeAbsPosNP[0].setValue(az);
612 LOGF_DEBUG("Dome position %f, step count %d", az, rotationCounter);
613 return true;
614}
615
616/************************************************************************************
617 *
618 * ***********************************************************************************/
620{
621 for (size_t i = 0; i < SensorsNP.size(); ++i)
622 {
623 SensorsNP[i].setValue(interface->getSensorValue(i));
624 }
625 // SensorsN[10].value = getDewPoint(SensorsN[8].value, SensorsN[7].value);
626 SensorsNP.setState(IPS_OK);
627 SensorsNP.apply();
628 return true;
629}
630
631/************************************************************************************
632 *
633 * ***********************************************************************************/
635{
636 for(size_t i = 0; i < RelaysSP.size(); i++)
637 {
638 RelaysSP[i].setState(interface->getRelayState(i));
639 }
640 RelaysSP.setState(IPS_OK);
641 RelaysSP.apply();
642 return true;
643}
644
645/************************************************************************************
646 *
647 * ***********************************************************************************/
649{
650 if (!isConnected())
651 return; // No need to reset timer if we are not connected anymore
652
653 interface->updateState();
654
655 currentStatus = interface->getStatus();
656
659 DomeShutterSP.apply();
660
662
663 if (status == DOME_HOMING)
664 {
665 if ((currentStatus & (ScopeDomeCard::STATUS_HOMING | ScopeDomeCard::STATUS_MOVING)) == 0)
666 {
667 double azDiff = DomeHomePositionNP[0].getValue() - DomeAbsPosNP[0].getValue();
668
669 if (azDiff > 180)
670 {
671 azDiff -= 360;
672 }
673 if (azDiff < -180)
674 {
675 azDiff += 360;
676 }
677
678 if (interface->getInputState(ScopeDomeCard::HOME) || fabs(azDiff) <= DomeParamNP[0].getValue())
679 {
680 // Found home (or close enough)
681 LOG_INFO("Home sensor found");
683 targetAz = DomeHomePositionNP[0].getValue();
684
685 // Reset counters
686 interface->resetCounter();
687
688 FindHomeSP.setState(IPS_OK);
689 FindHomeSP.apply();
691 }
692 else
693 {
694 // We overshoot, go closer
695 MoveAbs(DomeHomePositionNP[0].getValue());
696 }
697 }
698 DomeAbsPosNP.apply();
699 }
700 else if (status == DOME_DEROTATING)
701 {
702 if ((currentStatus & ScopeDomeCard::STATUS_MOVING) == 0)
703 {
704 currentRotation = interface->getRotationCounterExt();
705 LOGF_INFO("Current rotation is %d", currentRotation);
706 if (abs(currentRotation) < 100)
707 {
708 // Close enough
709 LOG_INFO("De-rotation complete");
711 DerotateSP.setState(IPS_OK);
712 DerotateSP.apply();
714 }
715 else
716 {
717 interface->move(currentRotation);
718 }
719 }
720 DomeAbsPosNP.apply();
721 }
722 else if (status == DOME_CALIBRATING)
723 {
725 {
726 stepsPerRevolution = interface->getStepsPerRevolution();
727 LOGF_INFO("Calibration complete, steps per revolution read as %d", stepsPerRevolution);
728 StepsPerRevolutionNP[0].setValue(stepsPerRevolution);
730 StepsPerRevolutionNP.apply();
731 StartCalibrationSP.setState(IPS_OK);
732 StartCalibrationSP.apply();
735 }
736 }
737 else if (DomeAbsPosNP.getState() == IPS_BUSY)
738 {
739 if ((currentStatus & ScopeDomeCard::STATUS_MOVING) == 0)
740 {
741 // Rotation idle, are we close enough?
742 double azDiff = targetAz - DomeAbsPosNP[0].getValue();
743
744 if (azDiff > 180)
745 {
746 azDiff -= 360;
747 }
748 if (azDiff < -180)
749 {
750 azDiff += 360;
751 }
752 if (!refineMove || fabs(azDiff) <= DomeParamNP[0].getValue())
753 {
754 if (refineMove)
755 {
756 DomeAbsPosNP[0].setValue(targetAz);
757 }
758 DomeAbsPosNP.setState(IPS_OK);
759 LOG_INFO("Dome reached requested azimuth angle.");
760
761 if (getDomeState() == DOME_PARKING)
762 {
764 && interface->getInputState(ScopeDomeCard::CLOSED1) == ISS_OFF)
765 {
767 }
768 else
769 {
770 SetParked(true);
771 }
772 }
773 else if (getDomeState() == DOME_UNPARKING)
774 {
775 SetParked(false);
776 }
777 else
778 {
780 }
781 }
782 else
783 {
784 // Refine azimuth
786 }
787 }
788 DomeAbsPosNP.apply();
789 }
790 else
791 {
792 DomeAbsPosNP.apply();
793 }
794
795 // Read temperatures only every 10th time
796 static int tmpCounter = 0;
797 if (--tmpCounter <= 0)
798 {
800 tmpCounter = 10;
801 }
802
804}
805
806/************************************************************************************
807 *
808 * ***********************************************************************************/
810{
811 LOGF_DEBUG("MoveAbs (%f)", az);
812 targetAz = az;
813 double azDiff = az - DomeAbsPosNP[0].getValue();
814 LOGF_DEBUG("azDiff = %f", azDiff);
815
816 // Make relative (-180 - 180) regardless if it passes az 0
817 if (azDiff > 180)
818 {
819 azDiff -= 360;
820 }
821 if (azDiff < -180)
822 {
823 azDiff += 360;
824 }
825
826 LOGF_DEBUG("azDiff rel = %f", azDiff);
827
828 refineMove = true;
829 return sendMove(azDiff);
830}
831
832/************************************************************************************
833 *
834 * ***********************************************************************************/
836{
837 refineMove = false;
838 return sendMove(azDiff);
839}
840
841/************************************************************************************
842 *
843 * ***********************************************************************************/
844IPState ScopeDome::sendMove(double azDiff)
845{
846 if (azDiff < 0)
847 {
848 int steps = static_cast<int>(-azDiff * stepsPerRevolution / 360.0);
849 LOGF_DEBUG("CCW (%d)", steps);
850 if (steps <= 0)
851 {
852 return IPS_OK;
853 }
854 steps = compensateInertia(steps);
855 LOGF_DEBUG("CCW inertia (%d)", steps);
856 if (steps <= 0)
857 {
858 return IPS_OK;
859 }
860 interface->move(-steps);
861 }
862 else
863 {
864 int steps = static_cast<int>(azDiff * stepsPerRevolution / 360.0);
865 LOGF_DEBUG("CW (%d)", steps);
866 if (steps <= 0)
867 {
868 return IPS_OK;
869 }
870 steps = compensateInertia(steps);
871 LOGF_DEBUG("CW inertia (%d)", steps);
872 if (steps <= 0)
873 {
874 return IPS_OK;
875 }
876 interface->move(steps);
877 }
878 return IPS_BUSY;
879}
880
881/************************************************************************************
882 *
883 * ***********************************************************************************/
885{
886 // Map to button outputs
887 if (operation == MOTION_START)
888 {
889 refineMove = false;
890 if (dir == DOME_CW)
891 {
892 interface->setOutputState(ScopeDomeCard::CCW, ISS_OFF);
893 interface->setOutputState(ScopeDomeCard::CW, ISS_ON);
894 }
895 else
896 {
897 interface->setOutputState(ScopeDomeCard::CW, ISS_OFF);
898 interface->setOutputState(ScopeDomeCard::CCW, ISS_ON);
899 }
900 return IPS_BUSY;
901 }
902 interface->setOutputState(ScopeDomeCard::CW, ISS_OFF);
903 interface->setOutputState(ScopeDomeCard::CCW, ISS_OFF);
904 return IPS_OK;
905}
906
907/************************************************************************************
908 *
909 * ***********************************************************************************/
911{
912 // First move to park position and then optionally close shutter
915 if (s == IPS_OK && ShutterParkPolicySP[SHUTTER_CLOSE_ON_PARK].getState() == ISS_ON)
916 {
917 // Already at park position, just close if needed
919 }
920 return s;
921}
922
923/************************************************************************************
924 *
925 * ***********************************************************************************/
927{
929 {
931 }
932 return IPS_OK;
933}
934
935/************************************************************************************
936 *
937 * ***********************************************************************************/
939{
940 LOGF_INFO("Control shutter %d", (int)operation);
941 targetShutter = operation;
942 if (operation == SHUTTER_OPEN)
943 {
944 LOG_INFO("Opening shutter");
945 if (interface->getInputState(ScopeDomeCard::OPEN1))
946 {
947 LOG_INFO("Shutter already open");
948 return IPS_OK;
949 }
950 interface->controlShutter(ScopeDomeCard::OPEN_SHUTTER);
951 }
952 else
953 {
954 LOG_INFO("Closing shutter");
955 if (interface->getInputState(ScopeDomeCard::CLOSED1))
956 {
957 LOG_INFO("Shutter already closed");
958 return IPS_OK;
959 }
960 interface->controlShutter(ScopeDomeCard::CLOSE_SHUTTER);
961 }
962
964 return IPS_BUSY;
965}
966
967/************************************************************************************
968 *
969 * ***********************************************************************************/
971{
972 interface->abort();
975 reconnected = false; // just in case.
976 return true;
977}
978
979/************************************************************************************
980 *
981 * ***********************************************************************************/
983{
985
986 DomeHomePositionNP.save(fp);
987 HomeSensorPolaritySP.save(fp);
988 CardTypeSP.save(fp);
989 CredentialsTP.save(fp);
990 return true;
991}
992
993/************************************************************************************
994 *
995 * ***********************************************************************************/
997{
998 SetAxis1Park(DomeAbsPosNP[0].getValue());
999 return true;
1000}
1001/************************************************************************************
1002 *
1003 * ***********************************************************************************/
1004
1006{
1007 // By default set position to 90
1008 SetAxis1Park(90);
1009 return true;
1010}
1011
1012/************************************************************************************
1013 *
1014 * ***********************************************************************************/
1015void ScopeDome::reconnect()
1016{
1017 // Reconnect serial port after write error
1018 LOG_INFO("Reconnecting serial port");
1019 reconnecting = true;
1021 usleep(1000000); // 1s
1024 interface->setPortFD(PortFD);
1025 LOG_INFO("Reconnected");
1026 reconnecting = false;
1027 reconnected = true; // Retry shutter operation when link returns
1028}
1029
1030/*
1031 * Saturation Vapor Pressure formula for range -100..0 Deg. C.
1032 * This is taken from
1033 * ITS-90 Formulations for Vapor Pressure, Frostpoint Temperature,
1034 * Dewpoint Temperature, and Enhancement Factors in the Range 100 to +100 C
1035 * by Bob Hardy
1036 * as published in "The Proceedings of the Third International Symposium on
1037 * Humidity & Moisture",
1038 * Teddington, London, England, April 1998
1039 */
1040static const float k0 = -5.8666426e3;
1041static const float k1 = 2.232870244e1;
1042static const float k2 = 1.39387003e-2;
1043static const float k3 = -3.4262402e-5;
1044static const float k4 = 2.7040955e-8;
1045static const float k5 = 6.7063522e-1;
1046
1047static float pvsIce(float T)
1048{
1049 float lnP = k0 / T + k1 + (k2 + (k3 + (k4 * T)) * T) * T + k5 * log(T);
1050 return exp(lnP);
1051}
1052
1064
1065static const float n1 = 0.11670521452767e4;
1066static const float n6 = 0.14915108613530e2;
1067static const float n2 = -0.72421316703206e6;
1068static const float n7 = -0.48232657361591e4;
1069static const float n3 = -0.17073846940092e2;
1070static const float n8 = 0.40511340542057e6;
1071static const float n4 = 0.12020824702470e5;
1072static const float n9 = -0.23855557567849;
1073static const float n5 = -0.32325550322333e7;
1074static const float n10 = 0.65017534844798e3;
1075
1076static float pvsWater(float T)
1077{
1078 float th = T + n9 / (T - n10);
1079 float A = (th + n1) * th + n2;
1080 ;
1081 float B = (n3 * th + n4) * th + n5;
1082 float C = (n6 * th + n7) * th + n8;
1083 ;
1084
1085 float p = 2.0f * C / (-B + sqrt(B * B - 4.0f * A * C));
1086 p *= p;
1087 p *= p;
1088 return p * 1e6;
1089}
1090
1091static const float C_OFFSET = 273.15f;
1092static const float minT = 173; // -100 Deg. C.
1093static const float maxT = 678;
1094
1095static float PVS(float T)
1096{
1097 if (T < minT || T > maxT)
1098 return 0;
1099 else if (T < C_OFFSET)
1100 return pvsIce(T);
1101 else
1102 return pvsWater(T);
1103}
1104
1105static float solve(float (*f)(float), float y, float x0)
1106{
1107 float x = x0;
1108 int maxCount = 10;
1109 int count = 0;
1110 while (++count < maxCount)
1111 {
1112 float xNew;
1113 float dx = x / 1000;
1114 float z = f(x);
1115 xNew = x + dx * (y - z) / (f(x + dx) - z);
1116 if (fabs((xNew - x) / xNew) < 0.0001f)
1117 return xNew;
1118 x = xNew;
1119 }
1120 return 0;
1121}
1122
1123float ScopeDome::getDewPoint(float RH, float T)
1124{
1125 T = T + C_OFFSET;
1126 return solve(PVS, RH / 100 * PVS(T), T) - C_OFFSET;
1127}
1128
1129int ScopeDome::compensateInertia(int steps)
1130{
1131 if (inertiaTable.size() == 0)
1132 {
1133 LOGF_DEBUG("inertia passthrough %d", steps);
1134 return steps; // pass value as such if we don't have enough data
1135 }
1136
1137 for (uint16_t out = 0; out < inertiaTable.size(); out++)
1138 {
1139 if (inertiaTable[out] > steps)
1140 {
1141 LOGF_DEBUG("inertia %d -> %d", steps, out - 1);
1142 return out - 1;
1143 }
1144 }
1145 // Check difference from largest table entry and assume we have
1146 // similar inertia also after that
1147 int lastEntry = inertiaTable.size() - 1;
1148 int inertia = inertiaTable[lastEntry] - lastEntry;
1149 int movement = steps - inertia;
1150 LOGF_DEBUG("inertia %d -> %d", steps, movement);
1151 if (movement <= 0)
1152 return 0;
1153
1154 return movement;
1155}
void log(const std::string &log)
Definition Utils.cpp:161
std::thread th
virtual bool Disconnect() override
Disconnect Disconnect from device.
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
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.
bool isSimulation() const
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
Connection::Interface * getActiveConnection()
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
@ CONNECTION_SERIAL
Definition indidome.h:172
@ CONNECTION_TCP
Definition indidome.h:173
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
const char * GetShutterStatusString(ShutterState status)
getShutterStatusString
@ SHUTTER_OPEN_ON_UNPARK
Definition indidome.h:592
@ SHUTTER_CLOSE_ON_PARK
Definition indidome.h:591
INDI::PropertyNumber DomeParamNP
Definition indidome.h:564
@ DOME_UNPARKING
Definition indidome.h:135
ShutterState m_ShutterState
Definition indidome.h:634
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.
DomeMotionCommand
Definition indidome.h:97
@ MOTION_START
Definition indidome.h:98
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition indidome.cpp:108
double GetAxis1Park()
@ SHUTTER_MOVING
Definition indidome.h:149
@ 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_REL_MOVE
Definition indidome.h:158
@ 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 ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition indidome.cpp:508
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Dome Presets in the configuration file
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
Definition indidome.cpp:824
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.
INDI::PropertySwitch DomeShutterSP
Definition indidome.h:568
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition indidome.cpp:399
DomeState getDomeState() const
Definition indidome.h:285
friend class ScopeDomeUSB21
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
INDI::PropertySwitch DerotateSP
virtual const char * getDefaultName() override
virtual bool SetDefaultPark() override
SetDefaultPark Set default coordinates/encoders value as the desired parking position.
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
INDI::PropertyText CredentialsTP
ShutterState simShutterStatus
friend class ScopeDomeArduino
INDI::PropertySwitch FindHomeSP
ShutterOperation targetShutter
virtual bool SetCurrentPark() override
SetCurrentPark Set current coordinates/encoders value as the desired parking position.
double targetAz
INDI::PropertyNumber DomeHomePositionNP
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
DomeStatus status
bool UpdateSensorStatus()
INDI::PropertyNumber FirmwareVersionsNP
virtual IPState MoveRel(double azDiff) override
Move the Dome to an relative position.
virtual IPState ControlShutter(ShutterOperation operation) override
Open or Close shutter.
virtual IPState UnPark() override
UnPark dome. The action of the Unpark command is dome specific, but it may include opening the shutte...
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool Abort() override
Abort all dome motion.
INDI::PropertySwitch CardTypeSP
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool UpdateShutterStatus()
virtual bool Handshake() override
perform handshake with device to check communication
INDI::PropertySwitch StartCalibrationSP
INDI::PropertySwitch HomeSensorPolaritySP
INDI::PropertySwitch InputsSP
INDI::PropertySwitch RelaysSP
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Dome Presets in the configuration file
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
INDI::PropertyNumber SensorsNP
virtual IPState MoveAbs(double az) override
Move the Dome to an absolute azimuth.
INDI::PropertyNumber StepsPerRevolutionNP
INDI::PropertySwitch CalibrationNeededSP
bool UpdateRelayStatus()
bool UpdatePosition()
virtual IPState Move(DomeDirection dir, DomeMotionCommand operation) override
Move the Dome in a particular direction.
virtual IPState Park() override
Goto Park Position. The park position is an absolute azimuth value.
const char * CONNECTION_TAB
CONNECTION_TAB Where all device connection settings (serial, usb, ethernet) are defined and controlle...
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
const char * INFO_TAB
INFO_TAB Where all the properties for general information are located.
const char * SITE_TAB
SITE_TAB Where all site information setting are located.
ISState
Switch state.
Definition indiapi.h:148
@ ISS_OFF
Definition indiapi.h:149
@ ISS_ON
Definition indiapi.h:150
@ IP_RW
Definition indiapi.h:184
@ IP_RO
Definition indiapi.h:182
IPState
Property state.
Definition indiapi.h:158
@ IPS_BUSY
Definition indiapi.h:161
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
@ ISR_1OFMANY
Definition indiapi.h:171
@ ISR_NOFMANY
Definition indiapi.h:173
@ ISR_ATMOST1
Definition indiapi.h:172
#define MAXINDINAME
Definition indiapi.h:189
Implementations for common driver routines.
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
Definition indidriver.c:908
int IUGetConfigSwitch(const char *dev, const char *property, const char *member, ISState *value)
IUGetConfigSwitch Opens configuration file and reads single switch property.
Definition indidriver.c:657
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
Definition indilogger.h:82
#define LOG_DEBUG(txt)
Definition indilogger.h:75
#define LOG_WARN(txt)
Definition indilogger.h:73
#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
size_type count(const key_type &key) const
std::unique_ptr< ScopeDome > scopeDome(new ScopeDome())