Instrument Neutral Distributed Interface INDI 2.2.5
lx200_pegasus_nyx101.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2021 Chrysikos Efstathios. All rights reserved.
3
4 Pegasus NYX
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 The full GNU General Public License is included in this distribution in the
22 file called LICENSE.
23*******************************************************************************/
24
26#include "lx200driver.h"
27#include "indicom.h"
29#include <libnova/transform.h>
30
31#include <cmath>
32#include <cstring>
33#include <stdio.h>
34#include <termios.h>
35#include <unistd.h>
36#include <regex>
37
38const char *SETTINGS_TAB = "Settings";
39const char *STATUS_TAB = "Status";
40
61
63{
65
66 // The mount stores the park position itself; PARK_SIMPLE offers TELESCOPE_PARK_OPTION to set it.
70
71 // The last known mount type decides the pier side capability before connecting.
72 MountTypeSP.load();
73
76
77 // Overwrite TRACK_CUSTOM, with TRACK_KING
78 TrackModeSP[TRACK_KING].fill("TRACK_KING", "King", ISS_OFF);
79
80 // Elevation Limits
81 ElevationLimitNP[OVERHEAD].fill("ELEVATION_OVERHEAD", "Overhead", "%g", 60, 90, 1, 90);
82 ElevationLimitNP[HORIZON].fill("ELEVATION_HORIZON", "Horizon", "%g", -30, 0, 1, 0);
83 ElevationLimitNP.fill(getDeviceName(), "ELEVATION_LIMIT", "Elevation Limit", MAIN_CONTROL_TAB, IP_RW, 0,
84 IPS_IDLE);
85
86 // Meridian
87 MeridianLimitNP[0].fill("VALUE", "Degrees (+/- 120)", "%.f", -120, 120, 1, 0);
88 MeridianLimitNP.fill(getDeviceName(), "MERIDIAN_LIMIT", "Limit", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
89
90 // Flip
91 FlipSP[0].fill("Flip", "Flip", ISS_OFF);
92 FlipSP.fill(getDeviceName(), "FLIP", "Pier Side", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
93
94 // Refraction
95 RefractSP[REFRACT_ON].fill("REFRACTION_ON", "On", ISS_OFF);
96 RefractSP[REFRACT_OFF].fill("REFRACTION_OFF", "Off", ISS_OFF);
97 RefractSP.fill(getDeviceName(), "REFRACTION",
98 "Refraction", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
99
100 // Safety Limits
101 SafetyLimitSP[SET_SAFETY_LIMIT].fill("SET_SAFETY_LIMIT", "Set", ISS_OFF);
102 SafetyLimitSP[CLEAR_SAFETY_LIMIT].fill("CLEAR_SAFETY_LIMIT", "Clear", ISS_OFF);
103 SafetyLimitSP.fill(getDeviceName(), "SAFETY_LIMIT",
104 "Custom Limits", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
105 // Guide Rate: standard number property, so clients such as PHD2 can read it.
106 GuideRateNP[AXIS_RA].fill("GUIDE_RATE_WE", "W/E Rate", "%.2f", 0.25, 1.0, 0.25, 0.5);
107 GuideRateNP[AXIS_DE].fill("GUIDE_RATE_NS", "N/S Rate", "%.2f", 0.25, 1.0, 0.25, 0.5);
108 GuideRateNP.fill(getDeviceName(), "GUIDE_RATE", "Guide Rate", SETTINGS_TAB, IP_RW, 60, IPS_IDLE);
109
110 //Go Home
111 // HomeSP[0].fill("GO", "Go", ISS_OFF);
112 // HomeSP.fill(getDeviceName(), "TELESCOPE_HOME", "Home go", MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
113
114 // //Reset Home
115 // ResetHomeSP[0].fill("Home", "Reset", ISS_OFF);
116 // ResetHomeSP.fill(getDeviceName(), "HOME_RESET", "Home Reset", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
117
118 verboseReport = false;
119 VerboseReportSP[0].fill("On", "On", ISS_OFF);
120 VerboseReportSP[1].fill("Off", "Off", ISS_ON);
121 VerboseReportSP.fill(getDeviceName(), "REPORT_VERBOSE", "Verbose", STATUS_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
122
123 Report[0].fill("Report", "GU", "-");
124 Report.fill(getDeviceName(), "Report", "Report", STATUS_TAB, IP_RO, 60, IPS_IDLE);
125
126#ifdef DEBUG_NYX
127 DebugCommandTP[0].fill("Command", "", "");
128 DebugCommandTP.fill(getDeviceName(), "DebugCommand", "", MAIN_CONTROL_TAB, IP_RW, 0,
129 IPS_IDLE);
130#endif
131
132 IsTracking[0].fill("IsTracking", "n", "-");
133 IsTracking.fill(getDeviceName(), "IsTracking", "IsTracking", STATUS_TAB, IP_RO, 60, IPS_IDLE);
134
135 IsSlewCompleted[0].fill("IsSlewCompleted", "N", "-");
136 IsSlewCompleted.fill(getDeviceName(), "IsSlewCompleted", "IsSlewCompleted", STATUS_TAB, IP_RO, 60, IPS_IDLE);
137
138 IsParked[0].fill("IsParked", "p/P", "-");
139 IsParked.fill(getDeviceName(), "IsParked", "IsParked", STATUS_TAB, IP_RO, 60, IPS_IDLE);
140
141 IsParkginInProgress[0].fill("IsParkginInProgress", "I", "-");
142 IsParkginInProgress.fill(getDeviceName(), "IsParkginInProgress", "IsParkginInProgress", STATUS_TAB, IP_RO, 60, IPS_IDLE);
143
144 IsAtHomePosition[0].fill("IsAtHomePosition", "H", "-");
145 IsAtHomePosition.fill(getDeviceName(), "IsAtHomePosition", "IsAtHomePosition", STATUS_TAB, IP_RO, 60, IPS_IDLE);
146
147 MountAltAz[0].fill("MountAltAz", "A", "-");
148 MountAltAz.fill(getDeviceName(), "MountAltAz", "MountAltAz", STATUS_TAB, IP_RO, 60, IPS_IDLE);
149
150 MountEquatorial[0].fill("MountEquatorial", "E", "-");
151 MountEquatorial.fill(getDeviceName(), "MountEquatorial", "MountEquatorial", STATUS_TAB, IP_RO, 60, IPS_IDLE);
152
153 PierNone[0].fill("PierNone", "", "-");
154 PierNone.fill(getDeviceName(), "PierNone", "PierNone", STATUS_TAB, IP_RO, 60, IPS_IDLE);
155
156 PierEast[0].fill("PierEast", "T", "-");
157 PierEast.fill(getDeviceName(), "PierEast", "PierEast", STATUS_TAB, IP_RO, 60, IPS_IDLE);
158
159 PierWest[0].fill("PierWest", "W", "-");
160 PierWest.fill(getDeviceName(), "PierWest", "PierWest", STATUS_TAB, IP_RO, 60, IPS_IDLE);
161
162 DoesRefractionComp[0].fill("DoesRefractionComp", "r", "-");
163 DoesRefractionComp.fill(getDeviceName(), "DoesRefractionComp", "DoesRefractionComp", STATUS_TAB, IP_RO, 60, IPS_IDLE);
164
165 WaitingAtHome[0].fill("WaitingAtHome", "w", "-");
166 WaitingAtHome.fill(getDeviceName(), "WaitingAtHome", "WaitingAtHome", STATUS_TAB, IP_RO, 60, IPS_IDLE);
167
168 IsHomePaused[0].fill("IsHomePaused", "u", "-");
169 IsHomePaused.fill(getDeviceName(), "IsHomePaused", "IsHomePaused", STATUS_TAB, IP_RO, 60, IPS_IDLE);
170
171 ParkFailed[0].fill("ParkFailed", "F", "-");
172 ParkFailed.fill(getDeviceName(), "ParkFailed", "ParkFailed", STATUS_TAB, IP_RO, 60, IPS_IDLE);
173
174 SlewingHome[0].fill("SlewingHome", "h", "-");
175 SlewingHome.fill(getDeviceName(), "SlewingHome", "SlewingHome", STATUS_TAB, IP_RO, 60, IPS_IDLE);
176
177 // Reboot
178 RebootSP[0].fill("Reboot", "Reboot", ISS_OFF);
179 RebootSP.fill(getDeviceName(), "REBOOT", "Reboot", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
180
181 // Slew Rates
182 SlewRateSP[0].setLabel("2x");
183 SlewRateSP[1].setLabel("8x");
184 SlewRateSP[2].setLabel("16x");
185 SlewRateSP[3].setLabel("64x");
186 SlewRateSP[4].setLabel("128x");
187 SlewRateSP[5].setLabel("200x");
188 SlewRateSP[6].setLabel("300x");
189 SlewRateSP[7].setLabel("600x");
190 SlewRateSP[8].setLabel("900x");
191 SlewRateSP[9].setLabel("1200x");
192 SlewRateSP.reset();
193
194 SlewRateSP[9].setState(ISS_ON);
195
196
197 return true;
198}
199
201{
203
204 if (isConnected())
205 {
206 char status[DRIVER_LEN] = {0};
207 if (sendCommand(":GU#", status))
208 {
209 if(strchr(status, 'P'))
210 SetParked(true);
211 else
212 SetParked(false);
213
214 if (strchr(status, 'A'))
216 else if (strchr(status, 'E'))
218 }
219
220 if(sendCommand(":GX90#", status))
221 {
222 GuideRateNP[AXIS_RA].setValue(GUIDE_RATES[nearestGuideRate(std::strtod(status, nullptr))]);
223 GuideRateNP[AXIS_DE].setValue(GuideRateNP[AXIS_RA].getValue());
224 GuideRateNP.setState(IPS_OK);
225 }
226
227 if(sendCommand(":Go#", status))
228 {
229 std::string c = status;
230 ElevationLimitNP[OVERHEAD].value = std::stoi(c);
231 }
232
233 if(sendCommand(":Gh#", status))
234 {
235 std::string c = status;
236 ElevationLimitNP[HORIZON].value = std::stoi(c);
237 }
238
239 if(sendCommand(":GXE9#", status))
240 {
241 std::string c = status;
242 MeridianLimitNP[0].setValue(std::stoi(c));
243 }
244
245 defineProperty(GuideRateNP);
246 // defineProperty(HomeSP);
247 // defineProperty(ResetHomeSP);
248 defineProperty(Report);
249 defineProperty(FlipSP);
250 defineProperty(MeridianLimitNP);
251 defineProperty(ElevationLimitNP);
252 defineProperty(RefractSP);
253 defineProperty(SafetyLimitSP);
254#ifdef DEBUG_NYX
255 defineProperty(DebugCommandTP);
256#endif
257 defineProperty(RebootSP);
258 defineProperty(VerboseReportSP);
259 defineProperty(IsTracking);
260 defineProperty(IsSlewCompleted);
261 defineProperty(IsParked);
262 defineProperty(IsParkginInProgress);
263 defineProperty(IsAtHomePosition);
264 defineProperty(MountAltAz);
265 defineProperty(MountEquatorial);
266 defineProperty(PierNone);
267 defineProperty(PierEast);
268 defineProperty(PierWest);
269 defineProperty(DoesRefractionComp);
270 defineProperty(WaitingAtHome);
271 defineProperty(IsHomePaused);
272 defineProperty(ParkFailed);
273 defineProperty(SlewingHome);
274 }
275 else
276 {
277 deleteProperty(GuideRateNP);
278 //deleteProperty(HomeSP);
279 deleteProperty(MeridianLimitNP);
280 deleteProperty(FlipSP);
281 deleteProperty(ElevationLimitNP);
282 deleteProperty(SafetyLimitSP);
283 //deleteProperty(ResetHomeSP);
284 deleteProperty(Report);
285#ifdef DEBUG_NYX
286 deleteProperty(DebugCommandTP);
287#endif
288 deleteProperty(RebootSP);
289 deleteProperty(VerboseReportSP);
290 deleteProperty(RefractSP);
291 deleteProperty(IsTracking);
292 deleteProperty(IsSlewCompleted);
293 deleteProperty(IsParked);
294 deleteProperty(IsParkginInProgress);
295 deleteProperty(IsAtHomePosition);
296 deleteProperty(MountAltAz);
297 deleteProperty(MountEquatorial);
298 deleteProperty(PierNone);
299 deleteProperty(PierEast);
300 deleteProperty(PierWest);
301 deleteProperty(DoesRefractionComp);
302 deleteProperty(WaitingAtHome);
303 deleteProperty(IsHomePaused);
304 deleteProperty(ParkFailed);
305 deleteProperty(SlewingHome);
306
307 }
308
309 return true;
310}
311
312
314{
315 return "Pegasus NYX-101";
316}
317
318const char *ON = "ON";
319const char *OFF = "OFF";
320
321void LX200NYX101::SetPropertyText(INDI::PropertyText propertyTxt, IPState state)
322{
323 if(!verboseReport)
324 return;
325
326 if(state == IPS_OK)
327 {
328 propertyTxt[0].setText(ON);
329 }
330 else if(state == IPS_BUSY)
331 {
332 propertyTxt[0].setText(OFF);
333 }
334 else if(state == IPS_IDLE)
335 {
336 propertyTxt[0].setText("-");
337 }
338 propertyTxt.setState(state);
339 propertyTxt.apply();
340}
341
343{
344 if (!isConnected())
345 return false;
346
347 bool _IsTracking = true;
348 SetPropertyText(IsTracking, IPS_OK);
349
350 bool _IsSlewCompleted = false;
351 SetPropertyText(IsSlewCompleted, IPS_BUSY);
352
353 bool _IsParked = false;
354 SetPropertyText(IsParked, IPS_BUSY);
355
356 bool _IsParkingInProgress = false;
357 SetPropertyText(IsParkginInProgress, IPS_BUSY);
358
359 //bool _IsAtHomePosition = false;
360 SetPropertyText(IsAtHomePosition, IPS_BUSY);
361
362 NYXTelescopeTrackMode _TrackingMode = TRACK_SIDEREAL;
363
364 //MountType _MountType = Equatorial;
365
367
368 bool _DoesRefractionComp = false;
369 SetPropertyText(DoesRefractionComp, IPS_BUSY);
370
371 //bool _WaitingAtHome = false;
372 SetPropertyText(WaitingAtHome, IPS_BUSY);
373
374 //bool _IsHomePaused = false;
375 SetPropertyText(IsHomePaused, IPS_BUSY);
376
377 bool _ParkFailed = false;
378 SetPropertyText(ParkFailed, IPS_BUSY);
379
380 //bool _SlewingHome = false;
381 SetPropertyText(SlewingHome, IPS_BUSY);
382
383 char status[DRIVER_LEN] = {0};
384 const bool statusRead = sendCommand(":GU#", status);
385 if(statusRead)
386 {
387 Report[0].text = status;
388 Report.apply();
389 int index = 0;
390 while(true)
391 {
392 switch (status[index++])
393 {
394 case 'n':
395 _IsTracking = false;
396 SetPropertyText(IsTracking, IPS_BUSY);
397 continue;
398 case 'N':
399 _IsSlewCompleted = true;
400 SetPropertyText(IsSlewCompleted, IPS_OK);
401 continue;
402 case 'p':
403 _IsParked = false;
404 SetPropertyText(IsParked, IPS_BUSY);
405 continue;
406 case 'P':
407 _IsParked = true;
408 SetPropertyText(IsParked, IPS_OK);
409 continue;
410 case 'I':
411 _IsParkingInProgress = true;
412 SetPropertyText(IsParkginInProgress, IPS_OK);
413 continue;
414 case 'H':
415 //_IsAtHomePosition = true;
416 SetPropertyText(IsAtHomePosition, IPS_OK);
417 continue;
418 case '(':
419 _TrackingMode = TRACK_LUNAR;
420 continue;
421 case 'O':
422 _TrackingMode = TRACK_SOLAR;
423 continue;
424 case 'k':
425 _TrackingMode = TRACK_KING;
426 continue;
427 case 'A':
428 //_MountType = AltAz;
429 SetPropertyText(MountAltAz, IPS_OK);
430 SetPropertyText(MountEquatorial, IPS_BUSY);
431 continue;
432 case 'E':
433 //_MountType = Equatorial;
434 SetPropertyText(MountEquatorial, IPS_OK);
435 SetPropertyText(MountAltAz, IPS_BUSY);
436 continue;
437 case 'T':
438 _PierSide = PIER_EAST;
439 continue;
440 case 'W':
441 _PierSide = PIER_WEST;
442 continue;
443 case 'r':
444 _DoesRefractionComp = true;
445 SetPropertyText(DoesRefractionComp, IPS_OK);
446 continue;
447 case 'w':
448 //_WaitingAtHome = true;
449 SetPropertyText(WaitingAtHome, IPS_OK);
450 continue;
451 case 'u':
452 //_IsHomePaused = true;
453 SetPropertyText(IsHomePaused, IPS_OK);
454 continue;
455 case 'F':
456 _ParkFailed = true;
457 SetPropertyText(ParkFailed, IPS_OK);
458 continue;
459 case 'h':
460 //_SlewingHome = true;
461 SetPropertyText(SlewingHome, IPS_OK);
462 continue;
463 case '#':
464 break;
465 default:
466 continue;
467 }
468 break;
469 }
470 }
471 // Without a status the values above are only defaults, so leave the shown state alone.
472 if (statusRead)
473 {
474 if(_DoesRefractionComp )
475 {
476 RefractSP[REFRACT_ON].setState(ISS_ON);
477 RefractSP[REFRACT_OFF].setState(ISS_OFF);
478 RefractSP.setState(IPS_OK);
479 RefractSP.apply();
480 }
481 else
482 {
483 RefractSP[REFRACT_ON].setState(ISS_OFF);
484 RefractSP[REFRACT_OFF].setState(ISS_ON);
485 RefractSP.setState(IPS_OK);
486 RefractSP.apply();
487 }
488 TrackModeSP[TRACK_SIDEREAL].setState(ISS_OFF);
489 TrackModeSP[TRACK_LUNAR].setState(ISS_OFF);
490 TrackModeSP[TRACK_SOLAR].setState(ISS_OFF);
491 TrackModeSP[TRACK_KING].setState(ISS_OFF);
492 TrackModeSP[_TrackingMode].setState(ISS_ON);
493 TrackModeSP.setState(IPS_OK);
494 TrackModeSP.apply();
495
496 switch(_PierSide)
497 {
499 SetPropertyText(PierNone, IPS_OK);
500 SetPropertyText(PierEast, IPS_BUSY);
501 SetPropertyText(PierWest, IPS_BUSY);
502 break;
504 SetPropertyText(PierEast, IPS_OK);
505 SetPropertyText(PierNone, IPS_BUSY);
506 SetPropertyText(PierWest, IPS_BUSY);
507 break;
509 SetPropertyText(PierWest, IPS_OK);
510 SetPropertyText(PierEast, IPS_BUSY);
511 SetPropertyText(PierNone, IPS_BUSY);
512 break;
513 }
514
515
516 }
517
518 if (!statusRead)
519 {
520 // Without a status the flags above are only defaults; keep the current state.
521 }
522 else if (TrackState == SCOPE_SLEWING)
523 {
524 if (!_IsSlewCompleted)
525 m_SlewSeen = true;
526 else if (m_SlewSeen || std::chrono::steady_clock::now() - m_SlewRequested > SLEW_START_TIMEOUT)
527 {
529 LOG_INFO("Slew is complete. Tracking...");
530 }
531 }
532 else if (m_UnparkPending)
533 {
534 // The mount may still report parked for a moment after an unpark request.
535 if (!_IsParked)
536 m_UnparkPending = false;
537 else if (std::chrono::steady_clock::now() - m_UnparkRequested > UNPARK_TIMEOUT)
538 {
539 m_UnparkPending = false;
540 LOG_WARN("Mount is still parked after the unpark request.");
541 SetParked(true);
542 }
543 }
544 else if (TrackState == SCOPE_PARKING && !_IsParked)
545 {
546 // A mount that is not operational yet (no time and location received) ignores :hP# and
547 // never reports 'P'. Report that instead of waiting forever.
548 const char *reason = nullptr;
549 if (_ParkFailed)
550 reason = "Mount reports that parking failed.";
551 else if (!_IsParkingInProgress && _IsSlewCompleted &&
552 std::chrono::steady_clock::now() - m_ParkRequested > PARK_START_TIMEOUT)
553 reason = "Mount did not start parking. Check that it has received time and location, and has a park position.";
554
555 if (reason)
556 {
557 LOGF_ERROR("%s", reason);
558 TrackState = _IsTracking ? SCOPE_TRACKING : SCOPE_IDLE;
559 ParkSP.reset();
560 ParkSP[UNPARK].setState(ISS_ON);
561 ParkSP.setState(IPS_ALERT);
562 ParkSP.apply();
563 }
564 }
565 else if (TrackState != SCOPE_PARKED && _IsParked)
566 {
567 SetParked(true);
568 }
569 else if (TrackState == SCOPE_PARKED && !_IsParked)
570 {
571 // Unparked outside INDI, e.g. by the hand controller or app. Motion commands would be refused otherwise.
572 LOG_INFO("Mount reports unparked.");
573 SetParked(false);
574 }
575 else
576 {
577 auto wasTracking = TrackStateSP[INDI_ENABLED].getState() == ISS_ON;
578 if (wasTracking != _IsTracking)
579 TrackState = _IsTracking ? SCOPE_TRACKING : SCOPE_IDLE;
580 }
581
582
584 {
585 EqNP.setState(IPS_ALERT);
586 LOG_ERROR("Error reading Ra - Dec");
587 EqNP.apply();
588 return false;
589 }
590
591 if (HasPierSide())
592 {
593 char response[DRIVER_LEN] = {0};
594 if (sendCommand(":Gm#", response))
595 {
596 if (response[0] == 'W')
598 else if (response[0] == 'E')
600 else
602 }
603 }
604
606
607 return true;
608}
609
610bool LX200NYX101::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
611{
612 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
613 {
614 if (MeridianLimitNP.isNameMatch(name))
615 {
616 MeridianLimitNP.update(values, names, n);
617 if (!isSimulation())
618 {
619 std::string command = ":SXE9," + std::to_string(MeridianLimitNP[0].getValue()) + "#";;
620 sendCommand(command.c_str());
621 command = ":SXEA," + std::to_string(MeridianLimitNP[0].getValue()) + "#";;
622 sendCommand(command.c_str());
623 if (MeridianLimitNP.getState() == IPS_OK)
624 {
625 LOGF_INFO("Meridian!: ", MeridianLimitNP[0].getValue());
626
627 }
628 }
629 else
630 {
631 MeridianLimitNP.setState(IPS_OK);
632 }
633
634 MeridianLimitNP.apply();
635 return true;
636 }
637
638 if (GuideRateNP.isNameMatch(name))
639 {
640 // One rate for both axes: take the element the client changed.
641 double requested = values[0];
642 for (int i = 0; i < n; i++)
643 {
644 auto element = GuideRateNP.findWidgetByName(names[i]);
645 if (element && std::fabs(element->getValue() - values[i]) > 1e-6)
646 {
647 requested = values[i];
648 break;
649 }
650 }
651
652 const int index = nearestGuideRate(requested);
653 if (isConnected() && !setGuideRate(index))
654 {
655 GuideRateNP.setState(IPS_ALERT);
656 GuideRateNP.apply();
657 return true;
658 }
659
660 if (std::fabs(GUIDE_RATES[index] - requested) > 1e-6)
661 LOGF_INFO("NYX-101 supports guide rates 0.25, 0.50 and 1.00 only, using %.2f.", GUIDE_RATES[index]);
662 if (std::fabs(GUIDE_RATES[index] - GuideRateNP[AXIS_RA].getValue()) > 1e-6)
663 LOG_WARN("RA and DEC guide rate changed.");
664
665 setGuideRateValue(index);
666 return true;
667 }
668
669 if (ElevationLimitNP.isNameMatch(name))
670 {
671 if(ElevationLimitNP.update(values, names, n))
672 {
673 for(int i = 0; i < n; ++i)
674 {
675 if(ElevationLimitNP[OVERHEAD].isNameMatch(names[i]))
676 {
677 std::string command = ":So" + std::to_string(static_cast<int>(ElevationLimitNP[OVERHEAD].getValue())) + "#";;
678 sendCommand(command.c_str());
679 }
680 else if(ElevationLimitNP[HORIZON].isNameMatch(names[i]))
681 {
682 std::string command = ":Sh" + std::to_string(static_cast<int>(ElevationLimitNP[HORIZON].getValue())) + "#";;
683 sendCommand(command.c_str());
684 }
685 }
686 ElevationLimitNP.apply();
687 return true;
688 }
689 }
690 }
691
692 return LX200Generic::ISNewNumber(dev, name, values, names, n);
693}
694
695bool LX200NYX101::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
696{
697 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
698 {
699 if(FlipSP.isNameMatch(name))
700 {
701 FlipSP.update(states, names, n);
702 IPState state = IPS_OK;
703 if (isConnected())
704 {
705 FlipSP[0].setState(ISS_OFF);
706 sendCommand(":MN#");
707 }
708 FlipSP.setState(state);
709 FlipSP.apply();
710 return true;
711 }
712 else if(RebootSP.isNameMatch(name))
713 {
714 RebootSP.update(states, names, n);
715 IPState state = IPS_OK;
716 if (isConnected())
717 {
718 RebootSP[0].setState(ISS_OFF);
719 sendCommand(":ERESET#");
720 }
721 RebootSP.setState(state);
722 RebootSP.apply();
723 return true;
724 }
725 else if(SafetyLimitSP.isNameMatch(name))
726 {
727 SafetyLimitSP.update(states, names, n);
728 auto index = SafetyLimitSP.findOnSwitchIndex();
729 switch(index)
730 {
731 case SET_SAFETY_LIMIT:
732 sendCommand(":Sc1#");
733 sendCommand(":Sc#");
734 break;
735 case CLEAR_SAFETY_LIMIT:
736 sendCommand(":Sc0#");
737 sendCommand(":Sc#");
738 break;
739 }
740 SafetyLimitSP.apply();
741 return true;
742 }
743 else if(RefractSP.isNameMatch(name))
744 {
745 RefractSP.update(states, names, n);
746 auto index = RefractSP.findOnSwitchIndex();
747
748 switch(index)
749 {
750 case REFRACT_ON:
751 sendCommand(":Tr#");
752 break;
753 case REFRACT_OFF:
754 sendCommand(":Tn#");
755 break;
756 }
757 RefractSP.apply();
758 return true;
759 }
760 else if(VerboseReportSP.isNameMatch(name))
761 {
762 VerboseReportSP.update(states, names, n);
763 int index = VerboseReportSP.findOnSwitchIndex();
764 VerboseReportSP[index].setState(ISS_OFF);
765
766 if(index == 0)
767 verboseReport = true;
768 else
769 {
770
771 SetPropertyText(IsTracking, IPS_IDLE);
772 SetPropertyText(IsSlewCompleted, IPS_IDLE);
773 SetPropertyText(IsParked, IPS_IDLE);
774 SetPropertyText(IsParkginInProgress, IPS_IDLE);
775 SetPropertyText(IsAtHomePosition, IPS_IDLE);
776 SetPropertyText(MountAltAz, IPS_IDLE);
777 SetPropertyText(MountEquatorial, IPS_IDLE);
778 SetPropertyText(PierNone, IPS_IDLE);
779 SetPropertyText(PierEast, IPS_IDLE);
780 SetPropertyText(PierWest, IPS_IDLE);
781 SetPropertyText(DoesRefractionComp, IPS_IDLE);
782 SetPropertyText(WaitingAtHome, IPS_IDLE);
783 SetPropertyText(IsHomePaused, IPS_IDLE);
784 SetPropertyText(ParkFailed, IPS_IDLE);
785 SetPropertyText(SlewingHome, IPS_IDLE);
786 verboseReport = false;
787 }
788
789
790 VerboseReportSP.setState(index == 0 ? IPS_OK : IPS_IDLE);
791 VerboseReportSP.apply();
792 return true;
793 }
794 }
795 return LX200Generic::ISNewSwitch(dev, name, states, names, n);
796}
797
798#ifdef DEBUG_NYX
799bool LX200NYX101::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
800{
801 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
802 {
803 if (DebugCommandTP.isNameMatch(name))
804 {
805 DebugCommandTP.update(texts, names, n);
806 for(int i = 0; i < n; i++)
807 {
808 if(DebugCommandTP[0].isNameMatch(names[i]))
809 {
810 char status[DRIVER_LEN] = {0};
811 sendCommand(texts[i]);
812 i = n;
813 }
814 }
815 return true;
816 }
817 }
818 return LX200Generic::ISNewText(dev, name, texts, names, n);
819
820}
821#endif
822
823bool LX200NYX101::Goto(double ra, double dec)
824{
826 return false;
827
828 m_SlewRequested = std::chrono::steady_clock::now();
829 m_SlewSeen = false;
830 return true;
831}
832
834{
835 double value = 0.0;
836 switch(index)
837 {
838 case 0:
839 value = 0.01;
840 break;
841 case 1:
842 value = 0.03;
843 break;
844 case 2:
845 value = 0.07;
846 break;
847 case 3:
848 value = 0.27;
849 break;
850 case 4:
851 value = 0.50;
852 break;
853 case 5:
854 value = 0.65;
855 break;
856 case 6:
857 value = 0.80;
858 break;
859 case 7:
860 value = 1;
861 break;
862 case 8:
863 value = 2.5;
864 break;
865 case 9:
866 value = 5;
867 break;
868 };
869
870 char decCommand[DRIVER_LEN] = {0};
871 snprintf(decCommand, DRIVER_LEN, ":RE%f#", value);
872
873 char raCommand[DRIVER_LEN] = {0};
874 snprintf(raCommand, DRIVER_LEN, ":RA%f#", value);
875
876 return sendCommand(decCommand) && sendCommand(raCommand);
877}
878
879bool LX200NYX101::setGuideRate(int rate)
880{
881 char command[DRIVER_LEN] = {0};
882 snprintf(command, DRIVER_LEN, ":R%d#", rate);
883 return sendCommand(command);
884}
885
886int LX200NYX101::nearestGuideRate(double rate)
887{
888 int index = 0;
889 for (int i = 1; i < 3; i++)
890 if (std::fabs(GUIDE_RATES[i] - rate) < std::fabs(GUIDE_RATES[index] - rate))
891 index = i;
892 return index;
893}
894
895void LX200NYX101::setGuideRateValue(int index)
896{
897 GuideRateNP[AXIS_RA].setValue(GUIDE_RATES[index]);
898 GuideRateNP[AXIS_DE].setValue(GUIDE_RATES[index]);
899 GuideRateNP.setState(IPS_OK);
900 GuideRateNP.apply();
901}
902
904{
905 switch(mode)
906 {
907 case TRACK_SIDEREAL:
908 return sendCommand(":TQ#");
909 break;
910 case TRACK_SOLAR:
911 return sendCommand(":TS#");
912 break;
913 case TRACK_LUNAR:
914 return sendCommand(":TL#");
915 break;
916 case TRACK_KING:
917 return sendCommand(":TK#");
918 break;
919 }
920 return false;
921}
922
924{
925 if (type == MOUNT_EQ_FORK)
926 {
927 LOG_ERROR("NYX-101 supports Alt-Az and German equatorial modes only.");
928 return false;
929 }
930
931 if (!isConnected())
932 return true;
933
934 if (!sendCommand((type == MOUNT_EQ_GEM) ? ":SXEM,1#" : ":SXEM,3#"))
935 return false;
936
937 LOG_WARN("Restart mount in order to apply changes to Mount Type.");
938 return true;
939}
940
941bool LX200NYX101::goToPark()
942{
943 LOG_INFO("Park requested.");
944 return sendCommand(":hP#");
945}
946
947bool LX200NYX101::goToUnPark()
948{
949 // The mount answers 1, or 0 if it was not parked. Read the answer, otherwise it is left in the
950 // buffer and prepended to the next reply.
951 char response[DRIVER_LEN] = {0};
952 if (!sendCommand(":hR#", response, -1, 1))
953 return false;
954
955 if (response[0] != '1')
956 LOG_DEBUG("Mount answered 0 to unpark, it was not parked.");
957 return true;
958}
959
961{
962 // The mount answers 1 when it stored the current position as its park position, 0 when it refused.
963 char response[DRIVER_LEN] = {0};
964 if (!sendCommand(":hQ#", response, -1, 1))
965 {
966 LOG_ERROR("Failed to set the park position.");
967 return false;
968 }
969
970 if (response[0] != '1')
971 {
972 LOG_ERROR("Mount refused the park position. It must be unparked and stopped, and needs time and location.");
973 return false;
974 }
975
976 LOG_INFO("Current position is stored as the mount park position.");
977 return true;
978}
979
981{
982 LOG_WARN("The mount has no default park position. Move it to the park position and select Current.");
983 return false;
984}
985
987{
988 bool rc = goToPark();
989 if (rc)
990 {
991 m_UnparkPending = false;
992 m_ParkRequested = std::chrono::steady_clock::now();
994 }
995
996 return rc;
997}
998
1000{
1001 bool rc = goToUnPark();
1002 if(rc)
1003 {
1004 // ReadScopeStatus() waits for the mount to confirm, instead of parking again on a stale 'P'.
1005 m_UnparkRequested = std::chrono::steady_clock::now();
1006 m_UnparkPending = true;
1007 SetParked(false);
1008 }
1009
1010 return rc;
1011}
1012
1014{
1015 char response[DRIVER_LEN] = {0};
1016 bool rc = sendCommand(enabled ? ":Te#" : ":Td#", response, 4, 1);
1017 return rc && response[0] == '1';
1018}
1019
1020bool LX200NYX101::setUTCOffset(double offset)
1021{
1022 int h {0}, m {0}, s {0};
1023 char command[DRIVER_LEN] = {0};
1024 offset *= -1;
1025 getSexComponents(offset, &h, &m, &s);
1026
1027 snprintf(command, DRIVER_LEN, ":SG%c%02d:%02d#", offset >= 0 ? '+' : '-', std::abs(h), m);
1028 return setStandardProcedure(PortFD, command) == 0;
1029}
1030
1031bool LX200NYX101::setLocalDate(uint8_t days, uint8_t months, uint16_t years)
1032{
1033 char command[DRIVER_LEN] = {0};
1034 snprintf(command, DRIVER_LEN, ":SC%02d/%02d/%02d#", months, days, years % 100);
1035 return setStandardProcedure(PortFD, command) == 0;
1036}
1037
1038bool LX200NYX101::updateLocation(double latitude, double longitude, double elevation)
1039{
1040 INDI_UNUSED(elevation);
1041 int d{0}, m{0}, s{0};
1042
1043 // JM 2021-04-10: MUST convert from INDI longitude to standard longitude.
1044 // DO NOT REMOVE
1045 if (longitude > 180)
1046 longitude = longitude - 360;
1047
1048 char command[DRIVER_LEN] = {0};
1049 // Reverse as per Meade way
1050 longitude *= -1;
1051 getSexComponents(longitude, &d, &m, &s);
1052 snprintf(command, DRIVER_LEN, ":Sg%c%03d*%02d:%02d#", longitude >= 0 ? '+' : '-', std::abs(d), m, s);
1053 if (setStandardProcedure(PortFD, command) < 0)
1054 {
1055 LOG_ERROR("Error setting site longitude coordinates");
1056 return false;
1057 }
1058
1059 getSexComponents(latitude, &d, &m, &s);
1060 snprintf(command, DRIVER_LEN, ":St%c%02d*%02d:%02d#", latitude >= 0 ? '+' : '-', std::abs(d), m, s);
1061 if (setStandardProcedure(PortFD, command) < 0)
1062 {
1063 LOG_ERROR("Error setting site latitude coordinates");
1064 return false;
1065 }
1066
1067 return true;
1068}
1069
1070
1071bool LX200NYX101::sendCommand(const char * cmd, char * res, int cmd_len, int res_len)
1072{
1073 int nbytes_written = 0, nbytes_read = 0, rc = -1;
1074
1075 tcflush(PortFD, TCIOFLUSH);
1076
1077 if (cmd_len > 0)
1078 {
1079 char hex_cmd[DRIVER_LEN * 3] = {0};
1080 hexDump(hex_cmd, cmd, cmd_len);
1081 LOGF_DEBUG("CMD <%s>", hex_cmd);
1082 rc = tty_write(PortFD, cmd, cmd_len, &nbytes_written);
1083 }
1084 else
1085 {
1086 LOGF_DEBUG("CMD <%s>", cmd);
1087 rc = tty_write_string(PortFD, cmd, &nbytes_written);
1088 }
1089
1090 if (rc != TTY_OK)
1091 {
1092 char errstr[MAXRBUF] = {0};
1093 tty_error_msg(rc, errstr, MAXRBUF);
1094 LOGF_ERROR("Serial write error: %s.", errstr);
1095 return false;
1096 }
1097
1098 if (res == nullptr)
1099 {
1100 tcdrain(PortFD);
1101 return true;
1102 }
1103
1104 if (res_len > 0)
1105 rc = tty_read(PortFD, res, res_len, DRIVER_TIMEOUT, &nbytes_read);
1106 else
1107 rc = tty_nread_section(PortFD, res, DRIVER_LEN, DRIVER_STOP_CHAR, DRIVER_TIMEOUT, &nbytes_read);
1108
1109 if (rc != TTY_OK)
1110 {
1111 char errstr[MAXRBUF] = {0};
1112 tty_error_msg(rc, errstr, MAXRBUF);
1113 LOGF_ERROR("Serial read error: %s.", errstr);
1114 return false;
1115 }
1116
1117 if (res_len > 0)
1118 {
1119 char hex_res[DRIVER_LEN * 3] = {0};
1120 hexDump(hex_res, res, res_len);
1121 LOGF_DEBUG("RES <%s>", hex_res);
1122 }
1123 else
1124 {
1125 LOGF_DEBUG("RES <%s>", res);
1126 }
1127
1128 tcflush(PortFD, TCIOFLUSH);
1129
1130 return true;
1131}
1132
1133void LX200NYX101::hexDump(char * buf, const char * data, int size)
1134{
1135 for (int i = 0; i < size; i++)
1136 sprintf(buf + 3 * i, "%02X ", static_cast<uint8_t>(data[i]));
1137
1138 if (size > 0)
1139 buf[3 * size - 1] = '\0';
1140}
1141
1142// Homing
1144{
1145 switch (action)
1146 {
1147 case HOME_GO:
1148 sendCommand(":hC#");
1149 return IPS_BUSY;
1150
1151 case HOME_SET:
1152 sendCommand(":hF#");
1153 return IPS_OK;
1154
1155 default:
1156 return IPS_ALERT;
1157 }
1158
1159 return IPS_ALERT;
1160}
1161
1162std::vector<std::string> LX200NYX101::split(const std::string &input, const std::string &regex)
1163{
1164 // passing -1 as the submatch index parameter performs splitting
1165 std::regex re(regex);
1166 std::sregex_token_iterator
1167 first{input.begin(), input.end(), re, -1},
1168 last;
1169 return {first, last};
1170}
bool isConnected() const
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)
bool isSimulation() const
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
TelescopeStatus TrackState
TelescopeMountType getMountType() const
void SetTelescopeCapability(uint32_t cap, uint8_t slewRateCount)
SetTelescopeCapability sets the Telescope capabilities. All capabilities must be initialized.
Connection::Serial * serialConnection
virtual void SetParked(bool isparked)
SetParked Change the mount parking status. The data park file (stored in ~/.indi/ParkData....
TelescopeMountType
Mount geometry reported in the standard TELESCOPE_MOUNT_TYPE property. The values are the indices of ...
uint32_t GetTelescopeCapability() const
GetTelescopeCapability returns the capability of the Telescope.
void NewRaDec(double ra, double dec)
The child class calls this function when it has updates.
void setPierSide(TelescopePierSide side)
void setMountType(TelescopeMountType type)
setMountType Report the mount type in TELESCOPE_MOUNT_TYPE. Drivers call it when the type is fixed by...
void SetParkDataType(TelescopeParkData type)
setParkDataType Sets the type of parking data stored in the park data file and presented to the user.
virtual bool initProperties() override
Called to initialize basic properties required all the time.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool SetCurrentPark() override
SetCurrentPark Set current coordinates/encoders value as the desired parking position.
virtual bool setUTCOffset(double offset) override
virtual bool SetDefaultPark() override
SetDefaultPark Set default coordinates/encoders value as the desired parking position.
virtual bool UnPark() override
Unpark the telescope if already parked.
virtual bool SetMountType(TelescopeMountType type) override
SetMountType Called when the user selects a different mount type in TELESCOPE_MOUNT_TYPE....
virtual bool setLocalDate(uint8_t days, uint8_t months, uint16_t years) override
virtual bool Goto(double ra, double dec) override
Move the scope to the supplied RA and DEC coordinates.
virtual bool SetTrackMode(uint8_t mode) override
SetTrackMode Set active tracking mode. Do not change track state.
virtual bool SetSlewRate(int index) override
SetSlewRate Set desired slew rate index.
virtual bool updateLocation(double latitude, double longitude, double elevation) override
Update telescope location settings.
virtual bool Park() override
Park the telescope to its home position.
virtual const char * getDefaultName() override
virtual bool SetTrackEnabled(bool enabled) override
SetTrackEnabled Engages or disengages mount tracking. If there are no tracking modes available,...
virtual bool updateProperties() override
Called when connected state changes, to add/remove properties.
virtual bool ReadScopeStatus() override
Read telescope status.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual IPState ExecuteHomeAction(TelescopeHomeAction action) override
Execute a homing action.
virtual bool initProperties() override
Called to initialize basic properties required all the time.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool updateProperties() override
Called when connected state changes, to add/remove properties.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool Goto(double ra, double dec) override
Move the scope to the supplied RA and DEC coordinates.
void setLX200Capability(uint32_t cap)
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
#define STATUS_TAB
Definition gemini.cpp:38
INDI::PropertySwitch ParkSP
INDI::PropertyNumber EqNP
INDI::PropertySwitch TrackStateSP
INDI::PropertySwitch SlewRateSP
INDI::PropertySwitch TrackModeSP
INDI::PropertySwitch MountTypeSP
Standard TELESCOPE_MOUNT_TYPE property, indexed by TelescopeMountType. Writable if the driver sets TE...
double ra
double dec
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_ALERT
Definition indiapi.h:162
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
@ ISR_1OFMANY
Definition indiapi.h:171
@ AXIS_DE
@ AXIS_RA
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
Definition indicom.c:418
void getSexComponents(double value, int *d, int *m, int *s)
Definition indicom.c:243
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition indicom.c:548
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
Definition indicom.c:473
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Definition indicom.c:1243
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition indicom.c:759
Implementations for common driver routines.
@ TTY_OK
Definition indicom.h:54
#define INDI_UNUSED(x)
Definition indidevapi.h:132
#define MAXRBUF
Definition indidriver.c:52
*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
int setStandardProcedure(int fd, const char *data)
#define getLX200DEC(fd, x)
@ LX200_24
Definition lx200driver.h:64
#define getLX200RA(fd, x)
uint16_t type
Definition pwc-ioctl.h:0
uint8_t cmd[4]
Definition pwc-ioctl.h:2
#define SETTINGS_TAB
Definition robofocus.cpp:44
#define OFF
Definition stvdriver.h:26
#define ON
Definition stvdriver.h:27