-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrolloffino.cpp
1183 lines (1092 loc) · 38 KB
/
rolloffino.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Edited version of the Dome Simulator
Copyright(c) 2014 Jasem Mutlaq. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*******************************************************************************/
/*
* Modified version of the rolloff roof simulator.
* Uses a simple text string protocol to send messages to an Arduino. The Arduino's USB connection is set
* by default to 38400 baud. The Arduino code determines which pins open/close a relay to start/stop the
* roof motor, and read state of switches indicating if the roof is opened or closed.
*/
#include "rolloffino.h"
#include "indicom.h"
#include "termios.h"
#include <cmath>
#include <cstring>
#include <ctime>
#include <memory>
#define ROLLOFF_DURATION 15 // Seconds until Roof is fully opened or closed
#define INACTIVE_STATUS 5 // Seconds between updating status lights
#define ROR_D_PRESS 1000 // Milliseconds after issuing command before expecting response
#define MAX_CNTRL_COM_ERR 10 // Maximum consecutive errors communicating with Arduino
// Read only
#define ROOF_OPENED_SWITCH "OPENED"
#define ROOF_CLOSED_SWITCH "CLOSED"
#define ROOF_LOCKED_SWITCH "LOCKED"
#define ROOF_AUX_SWITCH "AUXSTATE"
// Write only
#define ROOF_OPEN_RELAY "OPEN"
#define ROOF_CLOSE_RELAY "CLOSE"
#define ROOF_ABORT_RELAY "ABORT"
#define ROOF_LOCK_RELAY "LOCK"
#define ROOF_AUX_RELAY "AUXSET"
// Arduino controller interface limits
#define MAXINOCMD 15 // Command buffer
#define MAXINOTARGET 15 // Target buffer
#define MAXINOVAL 127 // Value bufffer, sized to contain NAK error strings
#define MAXINOLINE 63 // Sized to contain outgoing command requests
#define MAXINOBUF 255 // Sized for maximum overall input / output
#define MAXINOERR 255 // System call error message buffer
#define MAXINOWAIT 2 // seconds
// Driver version id
#define VERSION_ID "20211115"
// We declare an auto pointer to RollOffIno.
std::unique_ptr<RollOffIno> rollOffIno(new RollOffIno());
void ISPoll(void *p);
void ISGetProperties(const char *dev)
{
rollOffIno->ISGetProperties(dev);
}
void RollOffIno::ISGetProperties(const char *dev)
{
INDI::Dome::ISGetProperties(dev);
// Load Sync position
defineProperty(&RoofTimeoutNP);
loadConfig(true, "ENCODER_TICKS");
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
rollOffIno->ISNewSwitch(dev, name, states, names, n);
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
rollOffIno->ISNewText(dev, name, texts, names, n);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
rollOffIno->ISNewNumber(dev, name, values, names, n);
}
bool RollOffIno::ISNewNumber(const char *dev,const char *name,double values[],char *names[],int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (!strcmp(RoofTimeoutNP.name, name))
{
IUUpdateNumber(&RoofTimeoutNP, values, names, n);
RoofTimeoutNP.s = IPS_OK;
IDSetNumber(&RoofTimeoutNP, nullptr);
return true;
}
}
return INDI::Dome::ISNewNumber(dev,name,values,names,n);
}
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[],
char *names[], int n)
{
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(sizes);
INDI_UNUSED(blobsizes);
INDI_UNUSED(blobs);
INDI_UNUSED(formats);
INDI_UNUSED(names);
INDI_UNUSED(n);
}
void ISSnoopDevice(XMLEle *root)
{
rollOffIno->ISSnoopDevice(root);
}
bool RollOffIno::ISSnoopDevice(XMLEle *root)
{
return INDI::Dome::ISSnoopDevice(root);
}
RollOffIno::RollOffIno()
{
SetDomeCapability(DOME_CAN_ABORT | DOME_CAN_PARK); // Need the DOME_CAN_PARK capability for the scheduler
}
/**************************************************************************************
** INDI is asking us for our default device name.
** Check that it matches Ekos selection menu and ParkData.xml names
***************************************************************************************/
const char *RollOffIno::getDefaultName()
{
return (const char *)"RollOff ino";
}
/**************************************************************************************
** INDI request to init properties. Connected Define properties to Ekos
***************************************************************************************/
bool RollOffIno::initProperties()
{
INDI::Dome::initProperties();
IUFillSwitch(&LockS[LOCK_DISABLE], "LOCK_DISABLE", "Off", ISS_ON);
IUFillSwitch(&LockS[LOCK_ENABLE], "LOCK_ENABLE", "On", ISS_OFF);
IUFillSwitchVector(&LockSP, LockS, 2, getDeviceName(), "LOCK", "Lock", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
IUFillSwitch(&AuxS[AUX_DISABLE], "AUX_DISABLE", "Off", ISS_ON);
IUFillSwitch(&AuxS[AUX_ENABLE], "AUX_ENABLE", "On", ISS_OFF);
IUFillSwitchVector(&AuxSP, AuxS, 2, getDeviceName(), "AUX", "Auxiliary", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
IUFillLight(&RoofStatusL[ROOF_STATUS_OPENED], "ROOF_OPENED", "Opened", IPS_IDLE);
IUFillLight(&RoofStatusL[ROOF_STATUS_CLOSED], "ROOF_CLOSED", "Closed", IPS_IDLE);
IUFillLight(&RoofStatusL[ROOF_STATUS_MOVING], "ROOF_MOVING", "Moving", IPS_IDLE);
IUFillLight(&RoofStatusL[ROOF_STATUS_LOCKED], "ROOF_LOCK", "Roof Lock", IPS_IDLE);
IUFillLight(&RoofStatusL[ROOF_STATUS_AUXSTATE], "ROOF_AUXILIARY", "Roof Auxiliary", IPS_IDLE);
IUFillLightVector(&RoofStatusLP, RoofStatusL, 5, getDeviceName(), "ROOF STATUS", "Roof Status", MAIN_CONTROL_TAB, IPS_BUSY);
IUFillNumber(&RoofTimeoutN[0], "ROOF_TIMEOUT", "Timeout in Seconds", "%3.0f", 1, 300, 1, 15);
IUFillNumberVector(&RoofTimeoutNP, RoofTimeoutN, 1, getDeviceName(), "ROOF_MOVEMENT", "Roof Movement", OPTIONS_TAB, IP_RW,
60, IPS_IDLE);
SetParkDataType(PARK_NONE);
addAuxControls(); // This is for standard controls not the local auxiliary switch
return true;
}
/************************************************************************************
* Called from Dome, BaseDevice to establish contact with device
************************************************************************************/
bool RollOffIno::Handshake()
{
bool status = false;
LOGF_DEBUG("Driver id: %s", VERSION_ID);
if (PortFD <= 0)
DEBUG(INDI::Logger::DBG_WARNING,"The connection port has not been established");
else
{
if (!(status = initialContact()))
{
DEBUG(INDI::Logger::DBG_WARNING,"Initial controller contact failed, retrying");
msSleep(1000); // In case it is a Arduino still resetting from upload
status = initialContact();
}
if (!status)
LOG_ERROR("Unable to contact the roof controller");
}
return status;
}
/**************************************************************************************
** Client is asking us to establish connection to the device
***************************************************************************************/
bool RollOffIno::Connect()
{
bool status = INDI::Dome::Connect();
return status;
}
/**************************************************************************************
** Client is asking us to terminate connection to the device
***************************************************************************************/
bool RollOffIno::Disconnect()
{
bool status = INDI::Dome::Disconnect();
return status;
}
/********************************************************************************************
** INDI request to update the properties because there is a change in CONNECTION status
** This function is called whenever the device is connected or disconnected.
*********************************************************************************************/
bool RollOffIno::updateProperties()
{
INDI::Dome::updateProperties();
if (isConnected())
{
if (InitPark())
{
DEBUG(INDI::Logger::DBG_SESSION, "Dome parking data was obtained");
}
// If we do not have Dome parking data
else
{
DEBUG(INDI::Logger::DBG_SESSION,"Dome parking data was not obtained");
}
defineProperty(&LockSP); // Lock Switch,
defineProperty(&AuxSP); // Aux Switch,
defineProperty(&RoofStatusLP); // All the roof status lights
defineProperty(&RoofTimeoutNP);
setupConditions();
}
else
{
deleteProperty(RoofStatusLP.name); // Delete the roof status lights
deleteProperty(LockSP.name); // Delete the Lock Switch buttons
deleteProperty(AuxSP.name); // Delete the Auxiliary Switch buttons
deleteProperty(RoofTimeoutNP.name);
}
return true;
}
/********************************************************************************************
** Establish conditions on a connect.
*********************************************************************************************/
bool RollOffIno::setupConditions()
{
updateRoofStatus();
Dome::DomeState curState = getDomeState();
switch (curState)
{
case DOME_UNKNOWN:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_UNKNOWN");
break;
case DOME_ERROR:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_ERROR");
break;
case DOME_IDLE:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_IDLE ");
break;
case DOME_MOVING:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_MOVING");
break;
case DOME_SYNCED:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_SYNCED");
break;
case DOME_PARKING:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_PARKING");
break;
case DOME_UNPARKING:
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_UNPARKING");
break;
case DOME_PARKED:
if (isParked())
{
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_PARKED");
}
else
{
DEBUG(INDI::Logger::DBG_SESSION,"Dome state is DOME_PARKED but Dome status is unparked");
}
break;
case DOME_UNPARKED:
if (!isParked())
{
DEBUG(INDI::Logger::DBG_SESSION,"Dome state: DOME_UNPARKED");
}
else
{
DEBUG(INDI::Logger::DBG_SESSION,"Dome state is DOME_UNPARKED but Dome status is parked");
}
break;
}
// If the roof is clearly fully opened or fully closed, set the Dome::IsParked status to match.
// Otherwise if Dome:: park status different from roof status, o/p message (the roof might be or need to be operating manually)
// If park status is the same, and Dome:: state does not match, change the Dome:: state.
if (isParked())
{
if (fullyOpenedLimitSwitch == ISS_ON)
{
SetParked(false);
}
else if (fullyClosedLimitSwitch == ISS_OFF)
{
DEBUG(INDI::Logger::DBG_WARNING,"Dome indicates it is parked but roof closed switch not set, manual intervention needed");
}
else
{
// When Dome status agrees with roof status (closed), but Dome state differs, set Dome state to parked
if (curState != DOME_PARKED)
{
DEBUG(INDI::Logger::DBG_SESSION,"Setting Dome state to DOME_PARKED.");
setDomeState(DOME_PARKED);
}
}
}
else
{
if (fullyClosedLimitSwitch == ISS_ON)
{
SetParked(true);
}
else if (fullyOpenedLimitSwitch == ISS_OFF)
{
DEBUG(INDI::Logger::DBG_WARNING,"Dome indicates it is unparked but roof open switch is not set, manual intervention needed");
}
else
// When Dome status agrees with roof status (open), but Dome state differs, set Dome state to unparked
{
if (curState != DOME_UNPARKED)
{
DEBUG(INDI::Logger::DBG_SESSION,"Setting Dome state to DOME_UNPARKED.");
setDomeState(DOME_UNPARKED);
}
}
}
return true;
}
/********************************************************************************************
** Client has changed the state of a switch, update
*********************************************************************************************/
bool RollOffIno::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
bool switchOn = false;
// Make sure the call is for our device
if(dev != nullptr && strcmp(dev,getDeviceName()) == 0)
{
// Check if the call for our Lock switch
if (strcmp(name, LockSP.name) == 0)
{
// Find out which state is requested by the client
const char *actionName = IUFindOnSwitchName(states, names, n);
// If it is the same state as actionName, then we do nothing. i.e.
// if actionName is LOCK_ON and our Lock switch is already on, we return
int currentLockIndex = IUFindOnSwitchIndex(&LockSP);
DEBUGF(INDI::Logger::DBG_SESSION, "Lock state Requested: %s, Current: %s", actionName, LockS[currentLockIndex].name);
if (!strcmp(actionName, LockS[currentLockIndex].name))
{
DEBUGF(INDI::Logger::DBG_SESSION, "Lock switch is already %s", LockS[currentLockIndex].label);
LockSP.s = IPS_IDLE;
IDSetSwitch(&LockSP, NULL);
return true;
}
// Update the switch state
IUUpdateSwitch(&LockSP, states, names, n);
currentLockIndex = IUFindOnSwitchIndex(&LockSP);
LockSP.s = IPS_OK;
IDSetSwitch(&LockSP, nullptr);
if (strcmp(LockS[currentLockIndex].name, "LOCK_ENABLE") == 0)
switchOn = true;
setRoofLock(switchOn);
updateRoofStatus();
}
// Check if the call for our Aux switch
if (strcmp(name, AuxSP.name) == 0)
{
// Find out which state is requested by the client
const char *actionName = IUFindOnSwitchName(states, names, n);
// If it is the same state as actionName, then we do nothing. i.e.
// if actionName is AUX_ON and our Aux switch is already on, we return
int currentAuxIndex = IUFindOnSwitchIndex(&AuxSP);
DEBUGF(INDI::Logger::DBG_SESSION, "Auxiliary state Requested: %s, Current: %s", actionName, AuxS[currentAuxIndex].name);
if (!strcmp(actionName, AuxS[currentAuxIndex].name))
{
DEBUGF(INDI::Logger::DBG_SESSION, "Auxiliary switch is already %s", AuxS[currentAuxIndex].label);
AuxSP.s = IPS_IDLE;
IDSetSwitch(&AuxSP, NULL);
return true;
}
// Update the switch state
IUUpdateSwitch(&AuxSP, states, names, n);
currentAuxIndex = IUFindOnSwitchIndex(&AuxSP);
AuxSP.s = IPS_OK;
IDSetSwitch(&AuxSP, nullptr);
if (strcmp(AuxS[currentAuxIndex].name, "AUX_ENABLE") == 0)
switchOn = true;
setRoofAux(switchOn);
updateRoofStatus();
}
}
return INDI::Dome::ISNewSwitch(dev, name, states, names, n);
}
void RollOffIno::updateRoofStatus()
{
bool auxiliaryState = false;
bool lockedState = false;
bool openedState = false;
bool closedState = false;
getFullOpenedLimitSwitch(&openedState);
getFullClosedLimitSwitch(&closedState);
getRoofLockedSwitch(&lockedState);
getRoofAuxSwitch(&auxiliaryState);
if (!openedState && !closedState && !roofOpening && !roofClosing)
DEBUG(INDI::Logger::DBG_WARNING,"Roof stationary, neither opened or closed, adjust to match PARK button");
if (openedState && closedState)
DEBUG(INDI::Logger::DBG_WARNING,"Roof showing it is both opened and closed according to the controller");
RoofStatusL[ROOF_STATUS_AUXSTATE].s = IPS_IDLE;
RoofStatusL[ROOF_STATUS_LOCKED].s = IPS_IDLE;
RoofStatusL[ROOF_STATUS_OPENED].s = IPS_IDLE;
RoofStatusL[ROOF_STATUS_CLOSED].s = IPS_IDLE;
RoofStatusL[ROOF_STATUS_MOVING].s = IPS_IDLE;
RoofStatusLP.s = IPS_IDLE;
if (auxiliaryState)
{
RoofStatusL[ROOF_STATUS_AUXSTATE].s = IPS_OK;
}
if (lockedState)
{
RoofStatusL[ROOF_STATUS_LOCKED].s = IPS_ALERT; // Red to indicate lock is on
if (closedState)
{
RoofStatusL[ROOF_STATUS_CLOSED].s = IPS_OK; // Closed and locked roof status is normal
RoofStatusLP.s = IPS_OK; // Summary roof status
}
// An actual roof lock would not be expected unless roof was closed.
// However the controller might be using it to prevent motion for some other reason.
else if (openedState)
{
RoofStatusL[ROOF_STATUS_OPENED].s = IPS_OK; // Possible, rely on open/close lights to indicate situation
RoofStatusLP.s = IPS_OK;
}
else if (roofOpening || roofClosing)
{
RoofStatusLP.s = IPS_ALERT; // Summary roof status
RoofStatusL[ROOF_STATUS_MOVING].s = IPS_ALERT; // Should not be moving while locked
}
}
else
{
if (openedState || closedState)
{
if (openedState && !closedState)
{
roofOpening = false;
RoofStatusL[ROOF_STATUS_OPENED].s = IPS_OK;
RoofStatusLP.s = IPS_OK;
}
if (closedState && !openedState)
{
roofClosing = false;
RoofStatusL[ROOF_STATUS_CLOSED].s = IPS_OK;
RoofStatusLP.s = IPS_OK;
}
}
else if (roofOpening || roofClosing)
{
if (roofOpening)
{
RoofStatusL[ROOF_STATUS_OPENED].s = IPS_BUSY;
RoofStatusL[ROOF_STATUS_MOVING].s = IPS_BUSY;
}
else if (roofClosing)
{
RoofStatusL[ROOF_STATUS_CLOSED].s = IPS_BUSY;
RoofStatusL[ROOF_STATUS_MOVING].s = IPS_BUSY;
}
RoofStatusLP.s = IPS_BUSY;
}
// Roof is stationary, neither opened or closed
else
{
if (roofTimedOut == EXPIRED_OPEN)
RoofStatusL[ROOF_STATUS_OPENED].s = IPS_ALERT;
else if (roofTimedOut == EXPIRED_CLOSE)
RoofStatusL[ROOF_STATUS_CLOSED].s = IPS_ALERT;
RoofStatusLP.s = IPS_ALERT;
}
}
IDSetLight(&RoofStatusLP, nullptr);
}
/********************************************************************************************
** Each 1 second timer tick, if roof active
********************************************************************************************/
void RollOffIno::TimerHit()
{
double timeleft = CalcTimeLeft(MotionStart);
uint32_t delay = 1000 * INACTIVE_STATUS; // inactive timer setting to maintain roof status lights
if (!isConnected())
return; // No need to reset timer if we are not connected anymore
if (isSimulation())
{
if (timeleft -5 <= 0) // Use timeout approaching to set faux switch indicator
{
if (DomeMotionS[DOME_CW].s == ISS_ON) // Opening
{
simRoofOpen = true;
simRoofClosed = false;
}
else if (DomeMotionS[DOME_CCW].s == ISS_ON) // Closing
{
simRoofClosed = true;
simRoofOpen = false;
}
}
}
updateRoofStatus();
if (DomeMotionSP.s == IPS_BUSY)
{
// Abort called stop movement.
if (MotionRequest < 0)
{
DEBUG(INDI::Logger::DBG_WARNING,"Roof motion is stopped");
setDomeState(DOME_IDLE);
}
else
{
// Roll off is opening
if (DomeMotionS[DOME_CW].s == ISS_ON)
{
if (fullyOpenedLimitSwitch == ISS_ON)
{
DEBUG(INDI::Logger::DBG_DEBUG,"Roof is open");
SetParked(false);
}
// See if time to open has expired.
else if (timeleft <= 0)
{
LOG_WARN("Time allowed for opening the roof has expired?");
setDomeState(DOME_IDLE);
roofOpening = false;
roofTimedOut = EXPIRED_OPEN;
}
else
{
delay = 1000; // opening active
}
}
// Roll Off is closing
else if (DomeMotionS[DOME_CCW].s == ISS_ON)
{
if (fullyClosedLimitSwitch == ISS_ON)
{
DEBUG(INDI::Logger::DBG_DEBUG,"Roof is closed");
SetParked(true);
}
// See if time to open has expired.
else if (timeleft <= 0)
{
LOG_WARN("Time allowed for closing the roof has expired?");
setDomeState(DOME_IDLE);
roofClosing = false;
roofTimedOut = EXPIRED_CLOSE;
}
else
{
delay = 1000; // closing active
}
}
}
}
// Added to highlight WiFi issues, not able to recover lost connection without a reconnect
if (communicationErrors > MAX_CNTRL_COM_ERR)
{
LOG_ERROR("Too many errors communicating with Arduino");
LOG_ERROR("Try a fresh connect. Check communication equipment and operation of Arduino controller.");
INDI::Dome::Disconnect();
initProperties();
communicationErrors = 0;
}
// Even when no roof movement requested, will come through occasionally. Use timer to update roof status
// in case roof has been operated externally by a remote control, locks applied...
SetTimer(delay);
}
float RollOffIno::CalcTimeLeft(timeval start)
{
double timesince;
double timeleft;
struct timeval now { 0, 0 };
gettimeofday(&now, nullptr);
timesince =
(double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) - (double)(start.tv_sec * 1000.0 + start.tv_usec / 1000);
timesince = timesince / 1000;
timeleft = MotionRequest - timesince;
return timeleft;
}
bool RollOffIno::saveConfigItems(FILE *fp)
{
bool status = INDI::Dome::saveConfigItems(fp);
IUSaveConfigNumber(fp, &RoofTimeoutNP);
return status;
}
/*
* Direction: DOME_CW Clockwise = Open; DOME-CCW Counter clockwise = Close
* Operation: MOTION_START, | MOTION_STOP
*/
IPState RollOffIno::Move(DomeDirection dir, DomeMotionCommand operation)
{
updateRoofStatus();
if (operation == MOTION_START)
{
if (roofLockedSwitch)
{
LOG_WARN("Roof is externally locked, no movement possible");
return IPS_ALERT;
}
if (roofOpening)
{
LOG_WARN("Roof is in process of opening, wait for completion or abort current operation");
return IPS_OK;
}
if (roofClosing)
{
LOG_WARN("Roof is in process of closing, wait for completion or abort current operation");
return IPS_OK;
}
// Open Roof
// DOME_CW --> OPEN. If we are asked to "open" while we are fully opened as the
// limit switch indicates, then we simply return false.
if (dir == DOME_CW)
{
if (fullyOpenedLimitSwitch == ISS_ON)
{
LOG_WARN("DOME_CW directive received but roof is already fully opened");
SetParked(false);
return IPS_ALERT;
}
// getWeatherState is no longer available
//else if (getWeatherState() == IPS_ALERT)
//{
// LOG_WARN("Weather conditions are in the danger zone. Cannot open roof");
// return IPS_ALERT;
//}
// Initiate action
if (roofOpen())
{
roofOpening = true;
roofClosing = false;
LOG_INFO("Roof is opening...");
}
else
{
LOG_WARN("Failed to operate controller to open roof");
return IPS_ALERT;
}
}
// Close Roof
else if (dir == DOME_CCW)
{
if (fullyClosedLimitSwitch == ISS_ON)
{
SetParked(true);
LOG_WARN("DOME_CCW directive received but roof is already fully closed");
return IPS_ALERT;
}
else if (INDI::Dome::isLocked())
{
DEBUG(INDI::Logger::DBG_WARNING,
"Cannot close dome when mount is locking. See: Telescope parkng policy, in options tab");
return IPS_ALERT;
}
// Initiate action
if (roofClose())
{
roofClosing = true;
roofOpening = false;
LOG_INFO("Roof is closing...");
}
else
{
LOG_WARN("Failed to operate controller to close roof");
return IPS_ALERT;
}
}
roofTimedOut = EXPIRED_CLEAR;
MotionRequest = (int)RoofTimeoutN[0].value;
LOGF_DEBUG("Roof motion timeout setting: %d", (int)MotionRequest);
gettimeofday(&MotionStart, nullptr);
SetTimer(1000);
return IPS_BUSY;
}
return IPS_ALERT;
}
/*
* Close Roof
*
*/
IPState RollOffIno::Park()
{
IPState rc = INDI::Dome::Move(DOME_CCW, MOTION_START);
if (rc == IPS_BUSY)
{
LOG_INFO("RollOff ino is parking...");
return IPS_BUSY;
}
else
return IPS_ALERT;
}
/*
* Open Roof
*
*/
IPState RollOffIno::UnPark()
{
IPState rc = INDI::Dome::Move(DOME_CW, MOTION_START);
if (rc == IPS_BUSY)
{
LOG_INFO("RollOff ino is unparking...");
return IPS_BUSY;
}
else
return IPS_ALERT;
}
/*
* Abort motion
*/
bool RollOffIno::Abort()
{
bool lockState;
bool openState;
bool closeState;
updateRoofStatus();
lockState = (roofLockedSwitch == ISS_ON);
openState = (fullyOpenedLimitSwitch == ISS_ON);
closeState = (fullyClosedLimitSwitch == ISS_ON);
if (lockState)
{
LOG_WARN("Roof is externally locked, no action taken on abort request");
return true;
}
if (closeState && DomeMotionSP.s != IPS_BUSY)
{
LOG_WARN("Roof appears to be closed and stationary, no action taken on abort request");
return true;
}
else if (openState && DomeMotionSP.s != IPS_BUSY)
{
LOG_WARN("Roof appears to be open and stationary, no action taken on abort request");
return true;
}
else if (DomeMotionSP.s != IPS_BUSY)
{
LOG_WARN("Roof appears to be partially open and stationary, no action taken on abort request");
}
else if (DomeMotionSP.s == IPS_BUSY)
{
if (DomeMotionS[DOME_CW].s == ISS_ON)
{
LOG_WARN("Abort roof action requested while the roof was opening. Direction correction may be needed on the next move request.");
}
else if (DomeMotionS[DOME_CCW].s == ISS_ON)
{
LOG_WARN("Abort roof action requested while the roof was closing. Direction correction may be needed on the next move request.");
}
roofClosing = false;
roofOpening = false;
MotionRequest = -1;
roofAbort();
}
// If both limit switches are off, then we're neither parked nor unparked.
if (fullyOpenedLimitSwitch == ISS_OFF && fullyClosedLimitSwitch == ISS_OFF)
{
IUResetSwitch(&ParkSP);
ParkSP.s = IPS_IDLE;
IDSetSwitch(&ParkSP, nullptr);
}
return true;
}
bool RollOffIno::getFullOpenedLimitSwitch(bool* switchState)
{
if (isSimulation())
{
if (simRoofOpen)
{
fullyOpenedLimitSwitch = ISS_ON;
*switchState = true;
}
else
{
fullyOpenedLimitSwitch = ISS_OFF;
*switchState = false;
}
return true;
}
if (readRoofSwitch(ROOF_OPENED_SWITCH, switchState))
{
if (*switchState)
fullyOpenedLimitSwitch = ISS_ON;
else
fullyOpenedLimitSwitch = ISS_OFF;
return true;
}
else
{
LOG_WARN("Unable to obtain from the controller whether or not the roof is opened");
return false;
}
}
bool RollOffIno::getFullClosedLimitSwitch(bool* switchState)
{
if (isSimulation())
{
if (simRoofClosed)
{
fullyClosedLimitSwitch = ISS_ON;
*switchState = true;
}
else
{
fullyClosedLimitSwitch = ISS_OFF;
*switchState = false;
}
return true;
}
if (readRoofSwitch(ROOF_CLOSED_SWITCH, switchState))
{
if (*switchState)
fullyClosedLimitSwitch = ISS_ON;
else
fullyClosedLimitSwitch = ISS_OFF;
return true;
}
else
{
LOG_WARN("Unable to obtain from the controller whether or not the roof is closed");
return false;
}
}
bool RollOffIno::getRoofLockedSwitch(bool* switchState)
{
// If there is no lock switch, return success with status false
if (isSimulation())
{
roofLockedSwitch = ISS_OFF;
*switchState = false; // Not locked
return true;
}
if (readRoofSwitch(ROOF_LOCKED_SWITCH, switchState))
{
if (*switchState)
roofLockedSwitch = ISS_ON;
else
roofLockedSwitch = ISS_OFF;
return true;
}
else
{
LOG_WARN("Unable to obtain from the controller whether or not the roof is externally locked");
return false;
}
}
bool RollOffIno::getRoofAuxSwitch(bool* switchState)
{
// If there is no lock switch, return success with status false
if (isSimulation())
{
if (AuxS[AUX_ENABLE].s == ISS_OFF)
{
roofAuxiliarySwitch = ISS_OFF;
*switchState = false;
return true;
}
else
{
roofAuxiliarySwitch = ISS_ON;
*switchState = true;
return true;
}
}
if (readRoofSwitch(ROOF_AUX_SWITCH, switchState))
{
if (*switchState)
roofAuxiliarySwitch = ISS_ON;
else
roofAuxiliarySwitch = ISS_OFF;
return true;
}
else
{
LOG_WARN("Unable to obtain from the controller whether or not the obs Aux switch is being used");
return false;
}
}
/*
* -------------------------------------------------------------------------------------------
*
*/
bool RollOffIno::roofOpen()
{
if (isSimulation())
{
return true;
}
return pushRoofButton(ROOF_OPEN_RELAY, true, false);
}
bool RollOffIno::roofClose()
{
if (isSimulation())
{
return true;
}
return pushRoofButton(ROOF_CLOSE_RELAY, true, false);
}
bool RollOffIno::roofAbort()
{
if (isSimulation())
{
return true;
}
return pushRoofButton(ROOF_ABORT_RELAY, true, false);
}
bool RollOffIno::setRoofLock(bool switchOn)
{
if (isSimulation())
{
return false;
}
return pushRoofButton(ROOF_LOCK_RELAY, switchOn, true);
}
bool RollOffIno::setRoofAux(bool switchOn)
{
if (isSimulation())
{
return false;
}
return pushRoofButton(ROOF_AUX_RELAY, switchOn, true);
}
/*
* If unable to determine switch state due to errors, return false.
* If no errors return true. Return in result true if switch and false if switch off.
*/
bool RollOffIno::readRoofSwitch(const char* roofSwitchId, bool *result)