-
Notifications
You must be signed in to change notification settings - Fork 379
/
paramount.cpp
1183 lines (953 loc) · 33.2 KB
/
paramount.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
/*******************************************************************************
Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
Driver for using TheSky6 Pro Scripted operations for mounts via the TCP server.
While this technically can operate any mount connected to the TheSky6 Pro, it is
intended for Paramount mounts control.
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.
*******************************************************************************/
#include "paramount.h"
#include "indicom.h"
#include <libnova/sidereal_time.h>
#include <libnova/transform.h>
#include <cmath>
#include <cstring>
#include <memory>
#include <regex>
// We declare an auto pointer to Paramount.
std::unique_ptr<Paramount> paramount_mount(new Paramount());
#define GOTO_RATE 5 /* slew rate, degrees/s */
#define SLEW_RATE 0.5 /* slew rate, degrees/s */
#define FINE_SLEW_RATE 0.1 /* slew rate, degrees/s */
#define GOTO_LIMIT 5.5 /* Move at GOTO_RATE until distance from target is GOTO_LIMIT degrees */
#define SLEW_LIMIT 1 /* Move at SLEW_LIMIT until distance from target is SLEW_LIMIT degrees */
#define PARAMOUNT_TIMEOUT 3 /* Timeout in seconds */
#define PARAMOUNT_NORTH 0
#define PARAMOUNT_SOUTH 1
#define PARAMOUNT_EAST 2
#define PARAMOUNT_WEST 3
#define RA_AXIS 0
#define DEC_AXIS 1
#define STELLAR_DAY 86164.098903691
#define TRACKRATE_SIDEREAL ((360.0 * 3600.0) / STELLAR_DAY)
#define SOLAR_DAY 86400
#define TRACKRATE_SOLAR ((360.0 * 3600.0) / SOLAR_DAY)
#define TRACKRATE_LUNAR 14.511415
/* Preset Slew Speeds */
#define SLEWMODES 9
const double slewspeeds[SLEWMODES] = { 1.0, 2.0, 4.0, 8.0, 32.0, 64.0, 128.0, 256.0, 512.0 };
void ISPoll(void *p);
void ISGetProperties(const char *dev)
{
paramount_mount->ISGetProperties(dev);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
paramount_mount->ISNewSwitch(dev, name, states, names, n);
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
paramount_mount->ISNewText(dev, name, texts, names, n);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
paramount_mount->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)
{
paramount_mount->ISSnoopDevice(root);
}
Paramount::Paramount()
{
setVersion(1, 0);
DBG_SCOPE = INDI::Logger::getInstance().addDebugLevel("Scope Verbose", "SCOPE");
SetTelescopeCapability(TELESCOPE_CAN_PARK | TELESCOPE_CAN_SYNC | TELESCOPE_CAN_GOTO | TELESCOPE_CAN_ABORT |
TELESCOPE_HAS_TIME | TELESCOPE_HAS_LOCATION | TELESCOPE_HAS_TRACK_MODE | TELESCOPE_HAS_TRACK_RATE |
TELESCOPE_CAN_CONTROL_TRACK | TELESCOPE_HAS_PIER_SIDE,
9);
setTelescopeConnection(CONNECTION_TCP);
}
const char *Paramount::getDefaultName()
{
return (const char *)"Paramount";
}
bool Paramount::initProperties()
{
/* Make sure to init parent properties first */
INDI::Telescope::initProperties();
for (int i = 0; i < SlewRateSP.nsp - 1; i++)
{
sprintf(SlewRateSP.sp[i].label, "%.fx", slewspeeds[i]);
SlewRateSP.sp[i].aux = (void *)&slewspeeds[i];
}
// Set 64x as default speed
SlewRateSP.sp[5].s = ISS_ON;
/* How fast do we guide compared to sidereal rate */
IUFillNumber(&JogRateN[RA_AXIS], "JOG_RATE_WE", "W/E Rate (arcmin)", "%g", 0, 600, 60, 30);
IUFillNumber(&JogRateN[DEC_AXIS], "JOG_RATE_NS", "N/S Rate (arcmin)", "%g", 0, 600, 60, 30);
IUFillNumberVector(&JogRateNP, JogRateN, 2, getDeviceName(), "JOG_RATE", "Jog Rate", MOTION_TAB, IP_RW, 0,
IPS_IDLE);
/* How fast do we guide compared to sidereal rate */
IUFillNumber(&GuideRateN[RA_AXIS], "GUIDE_RATE_WE", "W/E Rate", "%1.1f", 0.0, 1.0, 0.1, 0.5);
IUFillNumber(&GuideRateN[DEC_AXIS], "GUIDE_RATE_NS", "N/S Rate", "%1.1f", 0.0, 1.0, 0.1, 0.5);
IUFillNumberVector(&GuideRateNP, GuideRateN, 2, getDeviceName(), "GUIDE_RATE", "Guiding Rate", MOTION_TAB, IP_RW, 0,
IPS_IDLE);
// Homing
IUFillSwitch(&HomeS[0], "GO", "Go", ISS_OFF);
IUFillSwitchVector(&HomeSP, HomeS, 1, getDeviceName(), "TELESCOPE_HOME", "Homing", MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1, 60,
IPS_IDLE);
// Tracking Mode
#if 0
IUFillSwitch(&TrackModeS[TRACK_SIDEREAL], "TRACK_SIDEREAL", "Sidereal", ISS_OFF);
IUFillSwitch(&TrackModeS[TRACK_SOLAR], "TRACK_SOLAR", "Solar", ISS_OFF);
IUFillSwitch(&TrackModeS[TRACK_LUNAR], "TRACK_LUNAR", "Lunar", ISS_OFF);
IUFillSwitch(&TrackModeS[TRACK_CUSTOM], "TRACK_CUSTOM", "Custom", ISS_OFF);
IUFillSwitchVector(&TrackModeSP, TrackModeS, 4, getDeviceName(), "TELESCOPE_TRACK_MODE", "Track Mode",
MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1, 0, IPS_IDLE);
#endif
AddTrackMode("TRACK_SIDEREAL", "Sidereal", true);
AddTrackMode("TRACK_SOLAR", "Solar");
AddTrackMode("TRACK_LUNAR", "Lunar");
AddTrackMode("TRACK_CUSTOM", "Custom");
// Custom Tracking Rate
#if 0
IUFillNumber(&TrackRateN[0], "TRACK_RATE_RA", "RA (arcsecs/s)", "%.6f", -16384.0, 16384.0, 0.000001, 15.041067);
IUFillNumber(&TrackRateN[1], "TRACK_RATE_DE", "DE (arcsecs/s)", "%.6f", -16384.0, 16384.0, 0.000001, 0);
IUFillNumberVector(&TrackRateNP, TrackRateN, 2, getDeviceName(), "TELESCOPE_TRACK_RATE", "Track Rates",
MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
#endif
// Let's simulate it to be an F/7.5 120mm telescope with 50m 175mm guide scope
ScopeParametersN[0].value = 120;
ScopeParametersN[1].value = 900;
ScopeParametersN[2].value = 50;
ScopeParametersN[3].value = 175;
TrackState = SCOPE_IDLE;
SetParkDataType(PARK_HA_DEC);
initGuiderProperties(getDeviceName(), MOTION_TAB);
setDriverInterface(getDriverInterface() | GUIDER_INTERFACE);
addAuxControls();
double longitude = 0, latitude = 90;
// Get value from config file if it exists.
IUGetConfigNumber(getDeviceName(), "GEOGRAPHIC_COORD", "LONG", &longitude);
currentRA = get_local_sidereal_time(longitude);
IUGetConfigNumber(getDeviceName(), "GEOGRAPHIC_COORD", "LAT", &latitude);
currentDEC = latitude > 0 ? 90 : -90;
return true;
}
bool Paramount::updateProperties()
{
INDI::Telescope::updateProperties();
if (isConnected())
{
if (isTheSkyTracking())
{
IUResetSwitch(&TrackModeSP);
TrackModeS[TRACK_SIDEREAL].s = ISS_ON;
TrackState = SCOPE_TRACKING;
}
else
{
IUResetSwitch(&TrackModeSP);
TrackState = SCOPE_IDLE;
}
//defineProperty(&TrackModeSP);
//defineProperty(&TrackRateNP);
defineProperty(&JogRateNP);
defineProperty(&GuideNSNP);
defineProperty(&GuideWENP);
defineProperty(&GuideRateNP);
// Initial HA to 0 and currentDEC (+90 or -90)
if (InitPark())
{
// If loading parking data is successful, we just set the default parking values.
SetAxis1ParkDefault(0);
SetAxis2ParkDefault(currentDEC);
}
else
{
// Otherwise, we set all parking data to default in case no parking data is found.
SetAxis1Park(0);
SetAxis2Park(currentDEC);
SetAxis1ParkDefault(0);
SetAxis2ParkDefault(currentDEC);
}
SetParked(isTheSkyParked());
defineProperty(&HomeSP);
}
else
{
//deleteProperty(TrackModeSP.name);
//deleteProperty(TrackRateNP.name);
deleteProperty(JogRateNP.name);
deleteProperty(GuideNSNP.name);
deleteProperty(GuideWENP.name);
deleteProperty(GuideRateNP.name);
deleteProperty(HomeSP.name);
}
return true;
}
bool Paramount::Handshake()
{
if (isSimulation())
return true;
int rc = 0, nbytes_written = 0, nbytes_read = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"sky6RASCOMTele.ConnectAndDoNotUnpark();"
"Out = sky6RASCOMTele.IsConnected;",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return false;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return false;
}
LOGF_DEBUG("RES: %s", pRES);
int isTelescopeConnected = -1;
std::regex rgx(R"((\d+)\|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
if (std::regex_search(input, match, rgx))
isTelescopeConnected = atoi(match.str(1).c_str());
if (isTelescopeConnected <= 0)
{
LOGF_ERROR("Error connecting to telescope: %s (%d).", match.str(1).c_str(),
atoi(match.str(2).c_str()));
return false;
}
return true;
}
bool Paramount::getMountRADE()
{
int rc = 0, nbytes_written = 0, nbytes_read = 0, errorCode = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
//"if (sky6RASCOMTele.IsConnected==0) sky6RASCOMTele.Connect();"
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"sky6RASCOMTele.GetRaDec();"
"Out = String(sky6RASCOMTele.dRa) + ',' + String(sky6RASCOMTele.dDec);",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return false;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return false;
}
LOGF_DEBUG("RES: %s", pRES);
std::regex rgx(R"((.+),(.+)\|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
bool coordsOK = false;
if (std::regex_search(input, match, rgx))
{
errorCode = atoi(match.str(4).c_str());
if (errorCode == 0)
{
currentRA = atof(match.str(1).c_str());
currentDEC = atof(match.str(2).c_str());
coordsOK = true;
}
}
if (coordsOK)
return true;
LOGF_ERROR("Error reading coordinates %s (%d).", match.str(3).c_str(), errorCode);
return false;
}
INDI::Telescope::TelescopePierSide Paramount::getPierSide()
{
int rc = 0, nbytes_written = 0, nbytes_read = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
//"if (sky6RASCOMTele.IsConnected==0) sky6RASCOMTele.Connect();"
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"sky6RASCOMTele.DoCommand(11, \"Pier Side\");"
"Out = sky6RASCOMTele.DoCommandOutput",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return PIER_UNKNOWN;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return PIER_UNKNOWN;
}
LOGF_DEBUG("RES: %s", pRES);
std::regex rgx(R"((\d+)\|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
if (std::regex_search(input, match, rgx))
return std::stoi(match.str(1)) == 0 ? PIER_WEST : PIER_EAST;
return PIER_UNKNOWN;
}
bool Paramount::ReadScopeStatus()
{
if (isSimulation())
{
mountSim();
return true;
}
if (TrackState == SCOPE_SLEWING)
{
// Check if LX200 is done slewing
if (isSlewComplete())
{
TrackState = SCOPE_TRACKING;
if (HomeSP.s == IPS_BUSY)
{
IUResetSwitch(&HomeSP);
HomeSP.s = IPS_OK;
LOG_INFO("Finding home completed.");
}
else
LOG_INFO("Slew is complete. Tracking...");
}
}
else if (TrackState == SCOPE_PARKING)
{
if (isSlewComplete())
{
SetParked(true);
//LOG_INFO("Mount is parked. Disconnecting...");
//Disconnect();
}
//return true;
}
if (!getMountRADE())
return false;
char RAStr[64], DecStr[64];
fs_sexa(RAStr, currentRA, 2, 3600);
fs_sexa(DecStr, currentDEC, 2, 3600);
DEBUGF(DBG_SCOPE, "Current RA: %s Current DEC: %s", RAStr, DecStr);
setPierSide(getPierSide());
NewRaDec(currentRA, currentDEC);
return true;
}
bool Paramount::Goto(double r, double d)
{
targetRA = r;
targetDEC = d;
char RAStr[64], DecStr[64];
fs_sexa(RAStr, targetRA, 2, 3600);
fs_sexa(DecStr, targetDEC, 2, 3600);
ln_equ_posn lnradec { 0, 0 };
lnradec.ra = (currentRA * 360) / 24.0;
lnradec.dec = currentDEC;
//get_hrz_from_equ(&lnradec, &lnobserver, ln_get_julian_from_sys(), &lnaltaz);
/* libnova measures azimuth from south towards west */
// double current_az = range360(lnaltaz.az + 180);
//double current_alt =lnaltaz.alt;
char pCMD[MAXRBUF] = {0};
snprintf(pCMD, MAXRBUF,
"sky6RASCOMTele.Asynchronous = true;"
"sky6RASCOMTele.SlewToRaDec(%g, %g,'');",
targetRA, targetDEC);
if (!sendTheSkyOKCommand(pCMD, "Slewing to target"))
return false;
TrackState = SCOPE_SLEWING;
//EqNP.s = IPS_BUSY;
LOGF_INFO("Slewing to RA: %s - DEC: %s", RAStr, DecStr);
return true;
}
bool Paramount::isSlewComplete()
{
int rc = 0, nbytes_written = 0, nbytes_read = 0, errorCode = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"Out = sky6RASCOMTele.IsSlewComplete;",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return false;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return false;
}
LOGF_DEBUG("RES: %s", pRES);
std::regex rgx(R"((.+)|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
if (std::regex_search(input, match, rgx))
{
errorCode = atoi(match.str(3).c_str());
if (errorCode == 0)
{
int isComplete = atoi(match.str(1).c_str());
return (isComplete == 1);
}
}
LOGF_ERROR("Error reading isSlewComplete %s (%d).", match.str(2).c_str(), errorCode);
return false;
}
bool Paramount::isTheSkyParked()
{
int rc = 0, nbytes_written = 0, nbytes_read = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"Out = sky6RASCOMTele.IsParked();",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return false;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return false;
}
LOGF_DEBUG("RES: %s", pRES);
std::regex rgx(R"((.+)\|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
if (std::regex_search(input, match, rgx))
{
return strcmp("true", match.str(1).c_str()) == 0;
}
LOGF_ERROR("Error checking for park. Invalid response: %s", pRES);
return false;
}
bool Paramount::isTheSkyTracking()
{
int rc = 0, nbytes_written = 0, nbytes_read = 0;
char pCMD[MAXRBUF] = {0}, pRES[MAXRBUF] = {0};
strncpy(pCMD,
"/* Java Script */"
"var Out;"
"Out = sky6RASCOMTele.IsTracking;",
MAXRBUF);
LOGF_DEBUG("CMD: %s", pCMD);
if ((rc = tty_write_string(PortFD, pCMD, &nbytes_written)) != TTY_OK)
{
LOG_ERROR("Error writing to TheSky6 TCP server.");
return false;
}
// Should we read until we encounter string terminator? or what?
if (static_cast<int>(rc == tty_read_section(PortFD, pRES, '\0', PARAMOUNT_TIMEOUT, &nbytes_read)) != TTY_OK)
{
LOG_ERROR("Error reading from TheSky6 TCP server.");
return false;
}
LOGF_DEBUG("RES: %s", pRES);
std::regex rgx(R"((.+)\|(.+)\. Error = (\d+)\.)");
std::smatch match;
std::string input(pRES);
if (std::regex_search(input, match, rgx))
{
return strcmp("1", match.str(1).c_str()) == 0;
}
LOGF_ERROR("Error checking for tracking. Invalid response: %s", pRES);
return false;
}
bool Paramount::Sync(double ra, double dec)
{
char pCMD[MAXRBUF] = {0};
snprintf(pCMD, MAXRBUF, "sky6RASCOMTele.Sync(%g, %g,'');", targetRA, targetDEC);
if (!sendTheSkyOKCommand(pCMD, "Syncing to target"))
return false;
currentRA = ra;
currentDEC = dec;
LOG_INFO("Sync is successful.");
EqNP.s = IPS_OK;
NewRaDec(currentRA, currentDEC);
return true;
}
bool Paramount::Park()
{
double targetHA = GetAxis1Park();
targetRA = range24(get_local_sidereal_time(LocationN[LOCATION_LONGITUDE].value) - targetHA);
targetDEC = GetAxis2Park();
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.ParkAndDoNotDisconnect();", MAXRBUF);
if (!sendTheSkyOKCommand(pCMD, "Parking mount"))
return false;
TrackState = SCOPE_PARKING;
LOG_INFO("Parking telescope in progress...");
return true;
}
bool Paramount::UnPark()
{
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.Unpark();", MAXRBUF);
if (!sendTheSkyOKCommand(pCMD, "Unparking mount"))
return false;
SetParked(false);
return true;
}
bool Paramount::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
// first check if it's for our device
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (strcmp(name, "JOG_RATE") == 0)
{
IUUpdateNumber(&JogRateNP, values, names, n);
JogRateNP.s = IPS_OK;
IDSetNumber(&JogRateNP, nullptr);
return true;
}
// Guiding Rate
if (strcmp(name, GuideRateNP.name) == 0)
{
IUUpdateNumber(&GuideRateNP, values, names, n);
GuideRateNP.s = IPS_OK;
IDSetNumber(&GuideRateNP, nullptr);
return true;
}
if (strcmp(name, GuideNSNP.name) == 0 || strcmp(name, GuideWENP.name) == 0)
{
processGuiderProperties(name, values, names, n);
return true;
}
}
// if we didn't process it, continue up the chain, let somebody else
// give it a shot
return INDI::Telescope::ISNewNumber(dev, name, values, names, n);
}
bool Paramount::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (!strcmp(HomeSP.name, name))
{
if (findHome())
{
HomeS[0].s = ISS_ON;
TrackState = SCOPE_SLEWING;
HomeSP.s = IPS_BUSY;
LOG_INFO("Finding Home position...");
}
else
{
HomeS[0].s = ISS_OFF;
HomeSP.s = IPS_ALERT;
}
IDSetSwitch(&HomeSP, nullptr);
return true;
}
}
return INDI::Telescope::ISNewSwitch(dev, name, states, names, n);
}
bool Paramount::Abort()
{
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.Abort();", MAXRBUF);
return sendTheSkyOKCommand(pCMD, "Abort mount slew");
}
bool Paramount::findHome()
{
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.FindHome();", MAXRBUF);
return sendTheSkyOKCommand(pCMD, "Find home");
}
bool Paramount::MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command)
{
if (TrackState == SCOPE_PARKED)
{
LOG_ERROR("Please unpark the mount before issuing any motion commands.");
return false;
}
int motion = (dir == DIRECTION_NORTH) ? PARAMOUNT_NORTH : PARAMOUNT_SOUTH;
//int rate = IUFindOnSwitchIndex(&SlewRateSP);
int rate = slewspeeds[IUFindOnSwitchIndex(&SlewRateSP)];
switch (command)
{
case MOTION_START:
if (!isSimulation() && !startOpenLoopMotion(motion, rate))
{
LOG_ERROR("Error setting N/S motion direction.");
return false;
}
else
LOGF_INFO("Moving toward %s.", (motion == PARAMOUNT_NORTH) ? "North" : "South");
break;
case MOTION_STOP:
if (!isSimulation() && !stopOpenLoopMotion())
{
LOG_ERROR("Error stopping N/S motion.");
return false;
}
else
LOGF_INFO("Moving toward %s halted.",
(motion == PARAMOUNT_NORTH) ? "North" : "South");
break;
}
return true;
}
bool Paramount::MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command)
{
if (TrackState == SCOPE_PARKED)
{
LOG_ERROR("Please unpark the mount before issuing any motion commands.");
return false;
}
int motion = (dir == DIRECTION_WEST) ? PARAMOUNT_WEST : PARAMOUNT_EAST;
int rate = IUFindOnSwitchIndex(&SlewRateSP);
switch (command)
{
case MOTION_START:
if (!isSimulation() && !startOpenLoopMotion(motion, rate))
{
LOG_ERROR("Error setting W/E motion direction.");
return false;
}
else
LOGF_INFO("Moving toward %s.", (motion == PARAMOUNT_WEST) ? "West" : "East");
break;
case MOTION_STOP:
if (!isSimulation() && !stopOpenLoopMotion())
{
LOG_ERROR("Error stopping W/E motion.");
return false;
}
else
LOGF_INFO("Movement toward %s halted.",
(motion == PARAMOUNT_WEST) ? "West" : "East");
break;
}
return true;
}
bool Paramount::startOpenLoopMotion(uint8_t motion, uint16_t rate)
{
char pCMD[MAXRBUF] = {0};
snprintf(pCMD, MAXRBUF, "sky6RASCOMTele.DoCommand(9,'%d|%d');", motion, rate);
return sendTheSkyOKCommand(pCMD, "Starting open loop motion");
}
bool Paramount::stopOpenLoopMotion()
{
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.DoCommand(10,'');", MAXRBUF);
return sendTheSkyOKCommand(pCMD, "Stopping open loop motion");
}
bool Paramount::updateLocation(double latitude, double longitude, double elevation)
{
INDI_UNUSED(elevation);
// JM: INDI Longitude is 0 to 360 increasing EAST. libnova East is Positive, West is negative
lnobserver.lng = longitude;
if (lnobserver.lng > 180)
lnobserver.lng -= 360;
lnobserver.lat = latitude;
LOGF_INFO("Location updated: Longitude (%g) Latitude (%g)", lnobserver.lng, lnobserver.lat);
return true;
}
bool Paramount::updateTime(ln_date *utc, double utc_offset)
{
INDI_UNUSED(utc);
INDI_UNUSED(utc_offset);
return true;
}
bool Paramount::SetCurrentPark()
{
char pCMD[MAXRBUF] = {0};
strncpy(pCMD, "sky6RASCOMTele.SetParkPosition();", MAXRBUF);
if (!sendTheSkyOKCommand(pCMD, "Setting Park Position"))
return false;
double lst = get_local_sidereal_time(LocationN[LOCATION_LONGITUDE].value);
double ha = get_local_hour_angle(lst, currentRA);
SetAxis1Park(ha);
SetAxis2Park(currentDEC);
return true;
}
bool Paramount::SetDefaultPark()
{
// By default set HA to 0
SetAxis1Park(0);
// Set DEC to 90 or -90 depending on the hemisphere
SetAxis2Park((LocationN[LOCATION_LATITUDE].value > 0) ? 90 : -90);
return true;
}
bool Paramount::SetParkPosition(double Axis1Value, double Axis2Value)
{
INDI_UNUSED(Axis1Value);
INDI_UNUSED(Axis2Value);
LOG_ERROR("Setting custom parking position directly is not supported. Slew to the desired "
"parking position and click Current.");
return false;
}
void Paramount::mountSim()
{
static struct timeval ltv
{
0, 0
};
struct timeval tv
{
0, 0
};
double dt, dx, da_ra = 0, da_dec = 0;
int nlocked;
/* update elapsed time since last poll, don't presume exactly POLLMS */
gettimeofday(&tv, nullptr);
if (ltv.tv_sec == 0 && ltv.tv_usec == 0)
ltv = tv;
dt = tv.tv_sec - ltv.tv_sec + (tv.tv_usec - ltv.tv_usec) / 1e6;
ltv = tv;
if (fabs(targetRA - currentRA) * 15. >= GOTO_LIMIT)
da_ra = GOTO_RATE * dt;
else if (fabs(targetRA - currentRA) * 15. >= SLEW_LIMIT)
da_ra = SLEW_RATE * dt;
else
da_ra = FINE_SLEW_RATE * dt;
if (fabs(targetDEC - currentDEC) >= GOTO_LIMIT)
da_dec = GOTO_RATE * dt;
else if (fabs(targetDEC - currentDEC) >= SLEW_LIMIT)
da_dec = SLEW_RATE * dt;
else
da_dec = FINE_SLEW_RATE * dt;
double motionRate = 0;
if (MovementNSSP.s == IPS_BUSY)
motionRate = JogRateN[0].value;
else if (MovementWESP.s == IPS_BUSY)
motionRate = JogRateN[1].value;
if (motionRate != 0)
{
da_ra = motionRate * dt * 0.05;
da_dec = motionRate * dt * 0.05;
switch (MovementNSSP.s)
{
case IPS_BUSY:
if (MovementNSS[DIRECTION_NORTH].s == ISS_ON)
currentDEC += da_dec;
else if (MovementNSS[DIRECTION_SOUTH].s == ISS_ON)
currentDEC -= da_dec;
break;
default:
break;
}
switch (MovementWESP.s)
{
case IPS_BUSY:
if (MovementWES[DIRECTION_WEST].s == ISS_ON)
currentRA += da_ra / 15.;
else if (MovementWES[DIRECTION_EAST].s == ISS_ON)
currentRA -= da_ra / 15.;
break;
default:
break;
}
NewRaDec(currentRA, currentDEC);
return;
}
/* Process per current state. We check the state of EQUATORIAL_COORDS and act acoordingly */
switch (TrackState)
{
case SCOPE_IDLE:
/* RA moves at sidereal, Dec stands still */
currentRA += (TRACKRATE_SIDEREAL / 3600.0 * dt / 15.);
break;
case SCOPE_SLEWING:
case SCOPE_PARKING:
/* slewing - nail it when both within one pulse @ SLEWRATE */
nlocked = 0;
dx = targetRA - currentRA;
// Take shortest path
if (fabs(dx) > 12)
dx *= -1;
if (fabs(dx) <= da_ra)
{
currentRA = targetRA;
nlocked++;