Debian Codegraph @

nex_dome.cpp

raw
1 /*******************************************************************************
2 NexDome
3
4 Copyright(c) 2019 Jasem Mutlaq. All rights reserved.
5
6 NexDome Driver for Firmware v3+
7
8 Change Log:
9
10 2019.10.07: Driver is completely re-written to work with Firmware v3 since
11 Firmware v1 is obsolete from NexDome.
12 2017.01.01: Driver for Firmware v1 is developed by Rozeware Development Ltd.
13
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Library General Public
16 License version 2 as published by the Free Software Foundation.
17 .
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Library General Public License for more details.
22 .
23 You should have received a copy of the GNU Library General Public License
24 along with this library; see the file COPYING.LIB. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 Boston, MA 02110-1301, USA.
27 *******************************************************************************/
28 #include "nex_dome.h"
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <memory>
34 #include <regex>
35 #include <termios.h>
36
37 #include <indicom.h>
38 #include <cmath>
39
40 #include "config.h"
41
42 static std::unique_ptr<NexDome> nexDome(new NexDome());
43
44 //////////////////////////////////////////////////////////////////////////////
45 ///
46 //////////////////////////////////////////////////////////////////////////////
47 NexDome::NexDome()
48 {
49 setVersion(INDI_NEXDOME_VERSION_MAJOR, INDI_NEXDOME_VERSION_MINOR);
50
51 SetDomeCapability(DOME_CAN_ABORT |
52 DOME_CAN_ABS_MOVE |
53 DOME_CAN_PARK |
54 DOME_CAN_SYNC);
55 }
56
57 //////////////////////////////////////////////////////////////////////////////
58 ///
59 //////////////////////////////////////////////////////////////////////////////
60 bool NexDome::initProperties()
61 {
62 INDI::Dome::initProperties();
63
64 SetParkDataType(PARK_AZ);
65
66 ///////////////////////////////////////////////////////////////////////////////
67 /// Homeing
68 ///////////////////////////////////////////////////////////////////////////////
69 IUFillSwitch(&GoHomeS[HOME_FIND], "HOME_FIND", "Find", ISS_OFF);
70 IUFillSwitch(&GoHomeS[HOME_GOTO], "HOME_GOTO", "Go", ISS_OFF);
71 IUFillSwitchVector(&GoHomeSP, GoHomeS, 2, getDeviceName(), "DOME_HOMING", "Home", MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1,
72 60, IPS_IDLE);
73
74 ///////////////////////////////////////////////////////////////////////////////
75 /// Home Position
76 ///////////////////////////////////////////////////////////////////////////////
77 IUFillNumber(&HomePositionN[0], "POSITON", "degrees", "%.2f", 0.0, 360.0, 0.0, 0);
78 IUFillNumberVector(&HomePositionNP, HomePositionN, 1, getDeviceName(), "HOME_POSITION", "Home Az", MAIN_CONTROL_TAB, IP_RW,
79 60, IPS_IDLE);
80
81 ///////////////////////////////////////////////////////////////////////////////
82 /// Battery
83 ///////////////////////////////////////////////////////////////////////////////
84 IUFillNumber(&ShutterBatteryLevelN[ND::ROTATOR], "VOLTS", "Voltage", "%.2f", 0.0, 16.0, 0.0, 0);
85 IUFillNumberVector(&ShutterBatteryLevelNP, ShutterBatteryLevelN, 1, getDeviceName(), "BATTERY", "Battery Level",
86 ND::SHUTTER_TAB.c_str(), IP_RO, 60, IPS_IDLE);
87
88 ///////////////////////////////////////////////////////////////////////////////
89 /// Rotator Firmware Info
90 ///////////////////////////////////////////////////////////////////////////////
91 IUFillText(&RotatorFirmwareVersionT[0], "FIRMWARE_VERSION", "Version", "");
92 IUFillTextVector(&RotatorFirmwareVersionTP, RotatorFirmwareVersionT, 1, getDeviceName(), "FIRMWARE", "Firmware",
93 ND::ROTATOR_TAB.c_str(), IP_RO, 60, IPS_IDLE);
94
95 ///////////////////////////////////////////////////////////////////////////////
96 /// Shutter Firmware Info
97 ///////////////////////////////////////////////////////////////////////////////
98 IUFillText(&ShutterFirmwareVersionT[0], "FIRMWARE_VERSION", "Version", "");
99 IUFillTextVector(&ShutterFirmwareVersionTP, ShutterFirmwareVersionT, 1, getDeviceName(), "FIRMWARE", "Firmware",
100 ND::SHUTTER_TAB.c_str(), IP_RO, 60, IPS_IDLE);
101
102 ///////////////////////////////////////////////////////////////////////////////
103 /// Rotator Settings
104 ///////////////////////////////////////////////////////////////////////////////
105 IUFillNumber(&RotatorSettingsN[S_RAMP], "S_RAMP", "Acceleration Ramp (ms)", "%.f", 0.0, 5000, 1000.0, 0);
106 IUFillNumber(&RotatorSettingsN[S_VELOCITY], "S_VELOCITY", "Velocity (steps/s)", "%.f", 0.0, 5000, 1000.0, 0);
107 IUFillNumber(&RotatorSettingsN[S_ZONE], "S_ZONE", "Dead Zone (steps)", "%.f", 0.0, 32000, 1000.0, 2400);
108 IUFillNumber(&RotatorSettingsN[S_RANGE], "S_RANGE", "Travel Range (steps)", "%.f", 0.0, 55080, 1000.0, 55080);
109 IUFillNumberVector(&RotatorSettingsNP, RotatorSettingsN, 4, getDeviceName(), "ROTATOR_SETTINGS", "Rotator",
110 ND::ROTATOR_TAB.c_str(), IP_RW, 60, IPS_IDLE);
111
112
113 ///////////////////////////////////////////////////////////////////////////////
114 /// Rotator Sync
115 ///////////////////////////////////////////////////////////////////////////////
116 IUFillNumber(&RotatorSyncN[0], "ROTATOR_SYNC_VALUE", "Steps", "%.f", 0.0, 55080, 1000.0, 0);
117 IUFillNumberVector(&RotatorSyncNP, RotatorSyncN, 1, getDeviceName(), "ROTATOR_SYNC", "Sync",
118 ND::ROTATOR_TAB.c_str(), IP_RW, 60, IPS_IDLE);
119
120 ///////////////////////////////////////////////////////////////////////////////
121 /// Shutter Settings
122 ///////////////////////////////////////////////////////////////////////////////
123 IUFillNumber(&ShutterSettingsN[S_RAMP], "S_RAMP", "Acceleration Ramp (ms)", "%.f", 0.0, 5000, 1000.0, 0);
124 IUFillNumber(&ShutterSettingsN[S_VELOCITY], "S_VELOCITY", "Velocity (step/s)", "%.f", 0.0, 5000, 1000.0, 0);
125 IUFillNumberVector(&ShutterSettingsNP, ShutterSettingsN, 2, getDeviceName(), "SHUTTER_SETTINGS", "Shutter",
126 ND::SHUTTER_TAB.c_str(), IP_RW, 60, IPS_IDLE);
127
128 ///////////////////////////////////////////////////////////////////////////////
129 /// Shutter Sync
130 ///////////////////////////////////////////////////////////////////////////////
131 IUFillNumber(&ShutterSyncN[0], "SHUTTER_SYNC_VALUE", "Steps", "%.f", 0.0, 46000, 1000.0, 0);
132 IUFillNumberVector(&ShutterSyncNP, ShutterSyncN, 1, getDeviceName(), "SHUTTER_SYNC", "Sync",
133 ND::SHUTTER_TAB.c_str(), IP_RW, 60, IPS_IDLE);
134 ///////////////////////////////////////////////////////////////////////////////
135 /// Rotator Factory Settings
136 ///////////////////////////////////////////////////////////////////////////////
137 IUFillSwitch(&RotatorFactoryS[FACTORY_DEFAULTS], "FACTORY_DEFAULTS", "Defaults", ISS_OFF);
138 IUFillSwitch(&RotatorFactoryS[FACTORY_LOAD], "FACTORY_LOAD", "Load", ISS_OFF);
139 IUFillSwitch(&RotatorFactoryS[FACTORY_SAVE], "FACTORY_SAVE", "Save", ISS_OFF);
140 IUFillSwitchVector(&RotatorFactorySP, RotatorFactoryS, 3, getDeviceName(), "ROTATOR_FACTORY_SETTINGS", "Factory",
141 ND::ROTATOR_TAB.c_str(), IP_RW, ISR_ATMOST1, 0, IPS_IDLE);
142
143 ///////////////////////////////////////////////////////////////////////////////
144 /// Shutter Factory Settings
145 ///////////////////////////////////////////////////////////////////////////////
146 IUFillSwitch(&ShutterFactoryS[FACTORY_DEFAULTS], "FACTORY_DEFAULTS", "Defaults", ISS_OFF);
147 IUFillSwitch(&ShutterFactoryS[FACTORY_LOAD], "FACTORY_LOAD", "Load", ISS_OFF);
148 IUFillSwitch(&ShutterFactoryS[FACTORY_SAVE], "FACTORY_SAVE", "Save", ISS_OFF);
149 IUFillSwitchVector(&ShutterFactorySP, ShutterFactoryS, 3, getDeviceName(), "SHUTTER_FACTORY_SETTINGS", "Factory",
150 ND::SHUTTER_TAB.c_str(), IP_RW, ISR_ATMOST1, 0, IPS_IDLE);
151
152 // Check every 250ms.
153 setDefaultPollingPeriod(250);
154
155 return true;
156 }
157
158 //////////////////////////////////////////////////////////////////////////////
159 ///
160 //////////////////////////////////////////////////////////////////////////////
161 bool NexDome::Handshake()
162 {
163 std::string value;
164 bool rotatorOK = false;
165
166 if (getParameter(ND::SEMANTIC_VERSION, ND::ROTATOR, value))
167 {
168 LOGF_INFO("Detected rotator firmware version %s", value.c_str());
169 if (value < ND::MINIMUM_VERSION)
170 {
171 LOGF_ERROR("Rotator version %s is not supported. Please upgrade to version %s or higher.", value.c_str(),
172 ND::MINIMUM_VERSION.c_str());
173 return false;
174 }
175
176 rotatorOK = true;
177 RotatorFirmwareVersionTP.s = IPS_OK;
178 IUSaveText(&RotatorFirmwareVersionT[0], value.c_str());
179 }
180
181 if (getParameter(ND::SEMANTIC_VERSION, ND::SHUTTER, value))
182 {
183 LOGF_INFO("Detected shutter firmware version %s", value.c_str());
184 if (value < ND::MINIMUM_VERSION)
185 {
186 LOGF_ERROR("Shutter version %s is not supported. Please upgrade to version %s or higher.", value.c_str(),
187 ND::MINIMUM_VERSION.c_str());
188 return false;
189 }
190
191 SetDomeCapability(GetDomeCapability() | DOME_HAS_SHUTTER);
192
193 ShutterFirmwareVersionTP.s = IPS_OK;
194 IUSaveText(&ShutterFirmwareVersionT[0], value.c_str());
195 }
196 else
197 LOG_WARN("No shutter detected.");
198
199 return rotatorOK;
200 }
201
202 //////////////////////////////////////////////////////////////////////////////
203 ///
204 //////////////////////////////////////////////////////////////////////////////
205 const char * NexDome::getDefaultName()
206 {
207 return "NexDome";
208 }
209
210 //////////////////////////////////////////////////////////////////////////////
211 ///
212 //////////////////////////////////////////////////////////////////////////////
213 bool NexDome::updateProperties()
214 {
215 INDI::Dome::updateProperties();
216
217 if (isConnected())
218 {
219 getStartupValues();
220
221 defineProperty(&GoHomeSP);
222 defineProperty(&HomePositionNP);
223
224 // Rotator
225 defineProperty(&RotatorSettingsNP);
226 defineProperty(&RotatorSyncNP);
227 defineProperty(&RotatorFactorySP);
228 defineProperty(&RotatorFirmwareVersionTP);
229
230 // Shutter
231 if (HasShutter())
232 {
233 defineProperty(&ShutterSettingsNP);
234 defineProperty(&ShutterSyncNP);
235 defineProperty(&ShutterBatteryLevelNP);
236 defineProperty(&ShutterFactorySP);
237 defineProperty(&ShutterFirmwareVersionTP);
238 }
239 }
240 else
241 {
242 deleteProperty(GoHomeSP.name);
243 deleteProperty(HomePositionNP.name);
244
245 // Rotator
246 deleteProperty(RotatorSettingsNP.name);
247 deleteProperty(RotatorSyncNP.name);
248 deleteProperty(RotatorFactorySP.name);
249 deleteProperty(RotatorFirmwareVersionTP.name);
250
251 // Shutter
252 if (HasShutter())
253 {
254 deleteProperty(ShutterSettingsNP.name);
255 deleteProperty(RotatorSyncNP.name);
256 deleteProperty(ShutterBatteryLevelNP.name);
257 deleteProperty(ShutterFactorySP.name);
258 deleteProperty(ShutterFirmwareVersionTP.name);
259 }
260 }
261
262 return true;
263 }
264
265 //////////////////////////////////////////////////////////////////////////////
266 /// Switch handling
267 //////////////////////////////////////////////////////////////////////////////
268 bool NexDome::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
269 {
270 if(!strcmp(dev, getDeviceName()))
271 {
272 ///////////////////////////////////////////////////////////////////////////////
273 /// Home Command
274 ///////////////////////////////////////////////////////////////////////////////
275 if(!strcmp(name, GoHomeSP.name))
276 {
277 IUUpdateSwitch(&GoHomeSP, states, names, n);
278 if (GoHomeS[HOME_FIND].s == ISS_ON)
279 {
280 if (setParameter(ND::GOTO_HOME, ND::ROTATOR))
281 {
282 setDomeState(DOME_MOVING);
283 GoHomeSP.s = IPS_BUSY;
284 LOG_INFO("Finding home position...");
285 }
286 else
287 GoHomeSP.s = IPS_ALERT;
288 }
289 else if (GoHomeS[HOME_GOTO].s == ISS_ON)
290 {
291 if (MoveAbs(HomePositionN[0].value) == IPS_BUSY)
292 {
293
294 setDomeState(DOME_MOVING);
295 GoHomeSP.s = IPS_BUSY;
296 LOGF_INFO("Going to home position %.2f degrees.", HomePositionN[0].value);
297 }
298 else
299 GoHomeSP.s = IPS_ALERT;
300 }
301
302 IDSetSwitch(&GoHomeSP, nullptr);
303 return true;
304 }
305
306 ///////////////////////////////////////////////////////////////////////////////
307 /// Rotator Factory
308 ///////////////////////////////////////////////////////////////////////////////
309 if (!strcmp(name, RotatorFactorySP.name))
310 {
311 const char *requestedOP = IUFindOnSwitchName(states, names, n);
312 bool rc = false;
313 if (!strcmp(requestedOP, RotatorFactoryS[FACTORY_DEFAULTS].name))
314 rc = executeFactoryCommand(FACTORY_DEFAULTS, ND::ROTATOR);
315 else if (!strcmp(requestedOP, RotatorFactoryS[FACTORY_LOAD].name))
316 {
317 rc = executeFactoryCommand(FACTORY_LOAD, ND::ROTATOR);
318 try
319 {
320 std::string value;
321 if (getParameter(ND::ACCELERATION_RAMP, ND::ROTATOR, value))
322 RotatorSettingsN[S_RAMP].value = std::stoi(value);
323 if (getParameter(ND::VELOCITY, ND::ROTATOR, value))
324 RotatorSettingsN[S_VELOCITY].value = std::stoi(value);
325 if (getParameter(ND::DEAD_ZONE, ND::ROTATOR, value))
326 {
327 RotatorSettingsN[S_ZONE].value = std::stoi(value);
328 }
329 if (getParameter(ND::RANGE, ND::ROTATOR, value))
330 {
331 RotatorSettingsN[S_RANGE].value = std::stoi(value);
332 RotatorSyncN[0].max = RotatorSettingsN[S_RANGE].value;
333 StepsPerDegree = RotatorSettingsN[S_RANGE].value / 360.0;
334 }
335
336 IDSetNumber(&RotatorSettingsNP, nullptr);
337 }
338 catch (...)
339 {
340 LOG_WARN("Failed to parse rotator settings.");
341 }
342
343 }
344 else if (!strcmp(requestedOP, RotatorFactoryS[FACTORY_SAVE].name))
345 rc = executeFactoryCommand(FACTORY_SAVE, ND::ROTATOR);
346
347 RotatorFactorySP.s = (rc ? IPS_OK : IPS_ALERT);
348 IDSetSwitch(&RotatorFactorySP, nullptr);
349 return true;
350 }
351
352 ///////////////////////////////////////////////////////////////////////////////
353 /// Shutter Factory
354 ///////////////////////////////////////////////////////////////////////////////
355 if (!strcmp(name, ShutterFactorySP.name))
356 {
357 const char *requestedOP = IUFindOnSwitchName(states, names, n);
358 bool rc = false;
359 if (!strcmp(requestedOP, ShutterFactoryS[FACTORY_DEFAULTS].name))
360 rc = executeFactoryCommand(FACTORY_DEFAULTS, ND::SHUTTER);
361 else if (!strcmp(requestedOP, ShutterFactoryS[FACTORY_LOAD].name))
362 {
363 rc = executeFactoryCommand(FACTORY_LOAD, ND::SHUTTER);
364 try
365 {
366 std::string value;
367 if (getParameter(ND::ACCELERATION_RAMP, ND::SHUTTER, value))
368 ShutterSettingsN[S_RAMP].value = std::stoi(value);
369 if (getParameter(ND::VELOCITY, ND::SHUTTER, value))
370 ShutterSettingsN[S_VELOCITY].value = std::stoi(value);
371 IDSetNumber(&ShutterSettingsNP, nullptr);
372 }
373 catch (...)
374 {
375 LOG_WARN("Failed to parse shutter settings.");
376 }
377 }
378 else if (!strcmp(requestedOP, ShutterFactoryS[FACTORY_SAVE].name))
379 rc = executeFactoryCommand(FACTORY_SAVE, ND::SHUTTER);
380
381 ShutterFactorySP.s = (rc ? IPS_OK : IPS_ALERT);
382 IDSetSwitch(&ShutterFactorySP, nullptr);
383 return true;
384 }
385 }
386 return INDI::Dome::ISNewSwitch(dev, name, states, names, n);
387 }
388
389 //////////////////////////////////////////////////////////////////////////////
390 /// Number handling
391 //////////////////////////////////////////////////////////////////////////////
392 bool NexDome::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
393 {
394 if(!strcmp(dev, getDeviceName()))
395 {
396 ///////////////////////////////////////////////////////////////////////////////
397 /// Home Position
398 ///////////////////////////////////////////////////////////////////////////////
399 if (!strcmp(name, HomePositionNP.name))
400 {
401 if (setParameter(ND::HOME_POSITION, ND::ROTATOR, values[0] * StepsPerDegree))
402 {
403 LOGF_INFO("Home position is updated to %.2f degrees.", values[0]);
404 HomePositionN[0].value = values[0];
405 HomePositionNP.s = IPS_OK;
406 }
407 else
408 HomePositionNP.s = IPS_ALERT;
409
410 IDSetNumber(&HomePositionNP, nullptr);
411 return true;
412 }
413
414 ///////////////////////////////////////////////////////////////////////////////
415 /// Dome Autosync Threshold
416 /// Override INDI::Dome implementaion since we need to update dead-zone
417 /// To be compatible with this.
418 ///////////////////////////////////////////////////////////////////////////////
419 else if (!strcmp(name, DomeParamNP.name))
420 {
421 IUUpdateNumber(&DomeParamNP, values, names, n);
422 DomeParamNP.s = IPS_OK;
423 IDSetNumber(&DomeParamNP, nullptr);
424
425 double minDeadZone = round((DomeParamN[0].value - 0.1) * StepsPerDegree);
426 if (minDeadZone < RotatorSettingsN[S_ZONE].value)
427 {
428 if (setParameter(ND::DEAD_ZONE, ND::ROTATOR, minDeadZone))
429 {
430 RotatorSettingsN[S_ZONE].value = minDeadZone;
431 LOGF_INFO("Updating dead-zone to %.f steps since autosync threshold was set to %.2f degrees.", minDeadZone,
432 DomeParamN[0].value);
433 IDSetNumber(&RotatorSettingsNP, nullptr);
434 }
435 }
436 return true;
437 }
438
439 ///////////////////////////////////////////////////////////////////////////////
440 /// Rotator Settings
441 ///////////////////////////////////////////////////////////////////////////////
442 else if (!strcmp(name, RotatorSettingsNP.name))
443 {
444 std::vector<double> currentSettings(RotatorSettingsNP.nnp);
445 std::vector<bool> rc(RotatorSettingsNP.nnp, true);
446 for (int i = 0; i < RotatorSettingsNP.nnp; i++)
447 currentSettings[i] = RotatorSettingsN[i].value;
448
449 for (int i = 0; i < RotatorSettingsNP.nnp; i++)
450 {
451 if (std::fabs(values[i] - currentSettings[i]) > 0)
452 {
453 switch (i)
454 {
455 case S_RAMP:
456 rc[i] = setParameter(ND::ACCELERATION_RAMP, ND::ROTATOR, values[i]);
457 break;
458
459 case S_VELOCITY:
460 rc[i] = setParameter(ND::VELOCITY, ND::ROTATOR, values[i]);
461 break;
462
463 case S_ZONE:
464 rc[i] = true;
465 LOG_INFO("Cannot directly change dead-zone to prevent conflict with Autosync threshold in Slaving tab.");
466 //rc[i] = setParameter(ND::DEAD_ZONE, ND::ROTATOR, values[i]);
467 break;
468
469 case S_RANGE:
470 rc[i] = setParameter(ND::RANGE, ND::ROTATOR, values[i]);
471 break;
472 }
473 }
474 }
475
476 bool result = true;
477 for (int i = 0; i < RotatorSettingsNP.nnp; i++)
478 result &= rc[i];
479
480 if (result)
481 {
482 IUUpdateNumber(&RotatorSettingsNP, values, names, n);
483 RotatorSettingsNP.s = IPS_OK;
484 }
485 else
486 RotatorSettingsNP.s = IPS_ALERT;
487
488 if (std::abs(RotatorSettingsN[S_RANGE].value - RotatorSyncN[0].max) > 0)
489 {
490 RotatorSyncN[0].max = RotatorSettingsN[S_RANGE].value;
491 StepsPerDegree = RotatorSettingsN[S_RANGE].value / 360.0;
492 IUUpdateMinMax(&RotatorSyncNP);
493 }
494
495 IDSetNumber(&RotatorSettingsNP, nullptr);
496 return true;
497 }
498
499 ///////////////////////////////////////////////////////////////////////////////
500 /// Rotator Sync
501 ///////////////////////////////////////////////////////////////////////////////
502 else if (!strcmp(name, RotatorSyncNP.name))
503 {
504 if (setParameter(ND::POSITION, ND::ROTATOR, values[0]))
505 {
506 RotatorSyncN[0].value = values[0];
507 RotatorSyncNP.s = IPS_OK;
508 }
509 else
510 RotatorSyncNP.s = IPS_ALERT;
511
512 IDSetNumber(&RotatorSyncNP, nullptr);
513 return true;
514 }
515
516 ///////////////////////////////////////////////////////////////////////////////
517 /// Shutter Sync
518 ///////////////////////////////////////////////////////////////////////////////
519 else if (!strcmp(name, ShutterSyncNP.name))
520 {
521 if (setParameter(ND::POSITION, ND::SHUTTER, values[0]))
522 {
523 ShutterSyncN[0].value = values[0];
524 ShutterSyncNP.s = IPS_OK;
525 }
526 else
527 ShutterSyncNP.s = IPS_ALERT;
528
529 IDSetNumber(&RotatorSyncNP, nullptr);
530 return true;
531 }
532
533 ///////////////////////////////////////////////////////////////////////////////
534 /// Shutter Settings
535 ///////////////////////////////////////////////////////////////////////////////
536 else if (!strcmp(name, ShutterSettingsNP.name))
537 {
538 std::vector<double> currentSettings(ShutterSettingsNP.nnp);
539 std::vector<bool> rc(ShutterSettingsNP.nnp, true);
540 for (int i = 0; i < ShutterSettingsNP.nnp; i++)
541 currentSettings[i] = ShutterSettingsN[i].value;
542
543
544 for (int i = 0; i < ShutterSettingsNP.nnp; i++)
545 {
546 if (std::fabs(values[i] - currentSettings[i]) > 0)
547 {
548 switch (i)
549 {
550 case S_RAMP:
551 rc[i] = setParameter(ND::ACCELERATION_RAMP, ND::SHUTTER, values[i]);
552 break;
553
554 case S_VELOCITY:
555 rc[i] = setParameter(ND::VELOCITY, ND::SHUTTER, values[i]);
556 break;
557 }
558 }
559 }
560
561 bool result = true;
562 for (int i = 0; i < ShutterSettingsNP.nnp; i++)
563 result &= rc[i];
564
565 if (result)
566 {
567 IUUpdateNumber(&ShutterSettingsNP, values, names, n);
568 ShutterSettingsNP.s = IPS_OK;
569 }
570 else
571 ShutterSettingsNP.s = IPS_ALERT;
572
573 IDSetNumber(&ShutterSettingsNP, nullptr);
574 return true;
575 }
576 }
577
578 return INDI::Dome::ISNewNumber(dev, name, values, names, n);
579 }
580
581 ///////////////////////////////////////////////////////////////////////////////
582 /// Sync
583 ///////////////////////////////////////////////////////////////////////////////
584 bool NexDome::Sync(double az)
585 {
586 return setParameter(ND::POSITION, ND::ROTATOR, az * StepsPerDegree);
587 }
588
589 ///////////////////////////////////////////////////////////////////////////////
590 /// Timer
591 ///////////////////////////////////////////////////////////////////////////////
592 void NexDome::TimerHit()
593 {
594 std::string response;
595
596 if (checkEvents(response))
597 processEvent(response);
598
599 if (getDomeState() == DOME_MOVING || getDomeState() == DOME_PARKING)
600 {
601 std::string value;
602 if (getParameter(ND::REPORT, ND::ROTATOR, value))
603 processEvent(value);
604 }
605
606 if (HasShutter() && getShutterState() == SHUTTER_MOVING)
607 {
608 std::string value;
609 if (getParameter(ND::POSITION, ND::SHUTTER, value))
610 processEvent(value);
611 }
612
613
614 SetTimer(getCurrentPollingPeriod());
615 }
616
617 //////////////////////////////////////////////////////////////////////////////
618 ///
619 //////////////////////////////////////////////////////////////////////////////
620 IPState NexDome::MoveAbs(double az)
621 {
622 int32_t target = static_cast<uint32_t>(round(az * StepsPerDegree));
623 if (setParameter(ND::GOTO_STEP, ND::ROTATOR, target))
624 //if (setParameter(ND::GOTO_AZ, ND::ROTATOR, static_cast<int32_t>(round(az))))
625 {
626 m_TargetAZSteps = target;
627 return IPS_BUSY;
628 }
629 else
630 return IPS_ALERT;
631 }
632
633 IPState NexDome::Move(DomeDirection dir, DomeMotionCommand operation)
634 {
635 if (operation == MOTION_START)
636 {
637 double nextTarget = range360(DomeAbsPosN[0].value + (dir == DOME_CW ? 10 : -10));
638 LOGF_INFO("Moving %s by 10 degrees...", (dir == DOME_CW ? "CW" : "CCW"));
639 return MoveAbs(nextTarget);
640 }
641 else
642 {
643 return (Abort() ? IPS_OK : IPS_ALERT);
644 }
645 }
646
647 //////////////////////////////////////////////////////////////////////////////
648 ///
649 //////////////////////////////////////////////////////////////////////////////
650 IPState NexDome::Park()
651 {
652 MoveAbs(GetAxis1Park());
653
654 LOGF_INFO("Parking to %.2f azimuth...", GetAxis1Park());
655
656 if (HasShutter() && ShutterParkPolicyS[SHUTTER_CLOSE_ON_PARK].s == ISS_ON)
657 {
658 LOG_INFO("Closing shutter on parking...");
659 ControlShutter(ShutterOperation::SHUTTER_CLOSE);
660 DomeShutterS[SHUTTER_OPEN].s = ISS_OFF;
661 DomeShutterS[SHUTTER_CLOSE].s = ISS_ON;
662 setShutterState(SHUTTER_MOVING);
663 }
664
665 return IPS_BUSY;
666 }
667
668 //////////////////////////////////////////////////////////////////////////////
669 ///
670 //////////////////////////////////////////////////////////////////////////////
671 IPState NexDome::UnPark()
672 {
673 if (HasShutter() && ShutterParkPolicyS[SHUTTER_OPEN_ON_UNPARK].s == ISS_ON)
674 {
675 LOG_INFO("Opening shutter on unparking...");
676 ControlShutter(ShutterOperation::SHUTTER_OPEN);
677 DomeShutterS[SHUTTER_OPEN].s = ISS_ON;
678 DomeShutterS[SHUTTER_CLOSE].s = ISS_OFF;
679 setShutterState(SHUTTER_MOVING);
680 return IPS_BUSY;
681 }
682 else
683 {
684 SetParked(false);
685 return IPS_OK;
686 }
687 }
688
689 //////////////////////////////////////////////////////////////////////////////
690 ///
691 //////////////////////////////////////////////////////////////////////////////
692 IPState NexDome::ControlShutter(ShutterOperation operation)
693 {
694 if (!m_ShutterConnected)
695 {
696 LOG_ERROR("Shutter is not connected. Check battery and XBEE connection.");
697 return IPS_ALERT;
698 }
699
700 // Check if shutter is open or close.
701 switch (operation)
702 {
703 case SHUTTER_OPEN:
704 if (setParameter(ND::OPEN_SHUTTER, ND::SHUTTER))
705 {
706 LOG_INFO("Shutter is opening...");
707 return IPS_BUSY;
708 }
709 break;
710
711 case SHUTTER_CLOSE:
712 if (setParameter(ND::CLOSE_SHUTTER, ND::SHUTTER))
713 {
714 LOG_INFO("Shutter is closing...");
715 return IPS_BUSY;
716 }
717 break;
718 }
719
720 return IPS_ALERT;
721 }
722
723 //////////////////////////////////////////////////////////////////////////////
724 ///
725 //////////////////////////////////////////////////////////////////////////////
726 bool NexDome::Abort()
727 {
728 bool rcRotator = setParameter(ND::EMERGENCY_STOP, ND::ROTATOR);
729 if (rcRotator && GoHomeSP.s == IPS_BUSY)
730 {
731 GoHomeS[0].s = ISS_OFF;
732 GoHomeSP.s = IPS_IDLE;
733 IDSetSwitch(&GoHomeSP, nullptr);
734 }
735
736 bool rcShutter = setParameter(ND::EMERGENCY_STOP, ND::SHUTTER);
737 if (rcShutter && getShutterState() == SHUTTER_MOVING)
738 setShutterState(SHUTTER_UNKNOWN);
739
740 return rcRotator && rcShutter;
741 }
742
743 //////////////////////////////////////////////////////////////////////////////
744 ///
745 //////////////////////////////////////////////////////////////////////////////
746 bool NexDome::SetCurrentPark()
747 {
748 SetAxis1Park(DomeAbsPosN[0].value);
749 return true;
750 }
751
752 //////////////////////////////////////////////////////////////////////////////
753 ///
754 //////////////////////////////////////////////////////////////////////////////
755 bool NexDome::SetDefaultPark()
756 {
757 // default park position is pointed south
758 SetAxis1Park(0);
759 return true;
760 }
761
762 //////////////////////////////////////////////////////////////////////////////
763 ///
764 //////////////////////////////////////////////////////////////////////////////
765 bool NexDome::getStartupValues()
766 {
767 std::string value;
768
769 try
770 {
771 // Rotator Position
772 if (getParameter(ND::POSITION, ND::ROTATOR, value))
773 RotatorSyncN[0].value = std::stoi(value);
774
775 // Rotator Settings
776 if (getParameter(ND::ACCELERATION_RAMP, ND::ROTATOR, value))
777 RotatorSettingsN[S_RAMP].value = std::stoi(value);
778 if (getParameter(ND::VELOCITY, ND::ROTATOR, value))
779 RotatorSettingsN[S_VELOCITY].value = std::stoi(value);
780 if (getParameter(ND::DEAD_ZONE, ND::ROTATOR, value))
781 {
782 RotatorSettingsN[S_ZONE].value = std::stoi(value);
783 // double minAutoSyncThreshold = RotatorSettingsN[S_ZONE].value / StepsPerDegree;
784 // if (DomeParamN[0].value < minAutoSyncThreshold)
785 // {
786 // // Add 0.1 degrees as buffer so that dome doesn't get stuck thinking it's
787 // // still within the dead-zone.
788 // DomeParamN[0].value = minAutoSyncThreshold + 0.1;
789 // LOGF_INFO("Setting Autosync threshold to %.2f degrees since the dead-zone limit is set at %.f steps.",
790 // DomeParamN[0].value, RotatorSettingsN[S_ZONE].value);
791 // }
792 }
793 if (getParameter(ND::RANGE, ND::ROTATOR, value))
794 {
795 RotatorSettingsN[S_RANGE].value = std::stoi(value);
796 RotatorSyncN[0].max = RotatorSettingsN[S_RANGE].value;
797 StepsPerDegree = RotatorSettingsN[S_RANGE].value / 360.0;
798 }
799
800 // Shutter Settings
801 if (HasShutter())
802 {
803 if (getParameter(ND::POSITION, ND::SHUTTER, value))
804 ShutterSyncN[0].value = std::stoi(value);
805
806 if (getParameter(ND::ACCELERATION_RAMP, ND::SHUTTER, value))
807 ShutterSettingsN[S_RAMP].value = std::stoi(value);
808 if (getParameter(ND::VELOCITY, ND::SHUTTER, value))
809 ShutterSettingsN[S_VELOCITY].value = std::stoi(value);
810 }
811
812 // Home Setting
813 if (getParameter(ND::HOME_POSITION, ND::ROTATOR, value))
814 HomePositionN[0].value = std::stoi(value) / StepsPerDegree;
815
816 }
817 catch(...)
818 {
819 return false;
820 }
821
822 // Rotator State
823 if (getParameter(ND::REPORT, ND::ROTATOR, value))
824 processEvent(value);
825
826 // Shutter State
827 if (HasShutter())
828 {
829 if (getParameter(ND::REPORT, ND::SHUTTER, value))
830 processEvent(value);
831 }
832
833 if (InitPark())
834 {
835 // If loading parking data is successful, we just set the default parking values.
836 SetAxis1ParkDefault(0);
837 }
838 else
839 {
840 // Otherwise, we set all parking data to default in case no parking data is found.
841 SetAxis1Park(0);
842 SetAxis1ParkDefault(0);
843 }
844
845 return false;
846 }
847
848 //////////////////////////////////////////////////////////////////////////////
849 ///
850 //////////////////////////////////////////////////////////////////////////////
851 bool NexDome::saveConfigItems(FILE * fp)
852 {
853 INDI::Dome::saveConfigItems(fp);
854
855 IUSaveConfigNumber(fp, &RotatorSettingsNP);
856 IUSaveConfigNumber(fp, &ShutterSettingsNP);
857 return true;
858 }
859
860 //////////////////////////////////////////////////////////////////////////////
861 ///
862 //////////////////////////////////////////////////////////////////////////////
863 bool NexDome::setParameter(ND::Commands command, ND::Targets target, int32_t value)
864 {
865 std::string verb = ND::CommandsMap.at(command);
866 std::ostringstream cmd;
867 cmd << "@";
868 cmd << verb;
869 // Commands with two letters do not need Write (W)
870 if (verb.size() == 1)
871 cmd << "W";
872 cmd << ((target == ND::ROTATOR) ? "R" : "S");
873
874 if (value != -1e6)
875 {
876 cmd << ",";
877 cmd << value;
878 }
879
880 char res[ND::DRIVER_LEN] = {0};
881 return sendCommand(cmd.str().c_str(), res);
882 }
883
884 //////////////////////////////////////////////////////////////////////////////
885 ///
886 //////////////////////////////////////////////////////////////////////////////
887 bool NexDome::executeFactoryCommand(uint8_t command, ND::Targets target)
888 {
889 std::ostringstream cmd;
890 cmd << "@";
891 switch (command)
892 {
893 case FACTORY_DEFAULTS:
894 cmd << "ZD";
895 LOGF_INFO("%s: Loading factory defaults...", (target == ND::ROTATOR) ? "Rotator" : "Shutter");
896 break;
897
898 case FACTORY_LOAD:
899 cmd << "ZR";
900 LOGF_INFO("%s: Loading EEPROM settings...", (target == ND::ROTATOR) ? "Rotator" : "Shutter");
901 break;
902
903 case FACTORY_SAVE:
904 cmd << "ZW";
905 LOGF_INFO("%s: Saving settings to EEPROM...", (target == ND::ROTATOR) ? "Rotator" : "Shutter");
906 break;
907 }
908
909 cmd << ((target == ND::ROTATOR) ? "R" : "S");
910
911 return sendCommand(cmd.str().c_str());
912 }
913
914 //////////////////////////////////////////////////////////////////////////////
915 ///
916 //////////////////////////////////////////////////////////////////////////////
917 bool NexDome::getParameter(ND::Commands command, ND::Targets target, std::string &value)
918 {
919 char res[ND::DRIVER_LEN] = {0};
920 bool response_found = false;
921
922 std::string verb = ND::CommandsMap.at(command) + "R";
923
924 std::ostringstream cmd;
925 // Magic start character
926 cmd << "@";
927 // Command verb
928 cmd << verb;
929 // Target (Rotator or Shutter)
930 cmd << ((target == ND::ROTATOR) ? "R" : "S");
931
932 if (sendCommand(cmd.str().c_str(), res))
933 {
934 std::string response(res);
935
936 // Since we can get many unrelated responses from the firmware
937 // i.e. events, we need to parse all responses, and see which
938 // one is related to our get command.
939 std::vector<std::string> all = split(response, "\r\n");
940
941 // Let's find our match using this regex
942 std::regex re;
943
944 // Firmware is exception since the response does not include the target
945 // for everything else, the echo back includes the target.
946 if (command != ND::SEMANTIC_VERSION)
947 re = (verb + ((target == ND::ROTATOR) ? "R" : "S") + "([^#]+)");
948 else
949 re = (verb + "([^#]+)");
950
951 std::smatch match;
952
953 // Not iterate over all responses
954 for (auto &oneEvent : all)
955 {
956 std::string trimmedEvent = trim(oneEvent);
957
958 // If we find the match, tag it.
959 if (std::regex_search(trimmedEvent, match, re))
960 {
961 value = match.str(1);
962 response_found = true;
963 }
964 // Otherwise process the event
965 else
966 processEvent(trimmedEvent);
967 }
968 }
969
970 return response_found;
971 }
972
973 //////////////////////////////////////////////////////////////////////////////
974 ///
975 //////////////////////////////////////////////////////////////////////////////
976 bool NexDome::checkEvents(std::string &response)
977 {
978 int nbytes_read = 0;
979 char res[ND::DRIVER_LEN] = {0};
980
981 int rc = tty_nread_section(PortFD, res, ND::DRIVER_LEN, ND::DRIVER_EVENT_CHAR, ND::DRIVER_EVENT_TIMEOUT, &nbytes_read);
982
983 if (rc != TTY_OK || nbytes_read < 3)
984 return false;
985
986 std::string raw_response = res;
987
988 // Trim
989 response = trim(raw_response);
990
991 return true;
992 }
993
994 //////////////////////////////////////////////////////////////////////////////
995 ///
996 //////////////////////////////////////////////////////////////////////////////
997 bool NexDome::processEvent(const std::string &event)
998 {
999 for (const auto &kv : ND::EventsMap)
1000 {
1001 std::regex re(kv.second + "([^#]+)");
1002 std::smatch match;
1003 std::string value;
1004
1005 if (event == kv.second)
1006 value = event;
1007 else if (std::regex_search(event, match, re))
1008 value = match.str(1);
1009 else
1010 continue;
1011
1012 LOGF_DEBUG("Processing event <%s> with value <%s>", event.c_str(), value.c_str());
1013
1014 switch (kv.first)
1015 {
1016 case ND::XBEE_STATE:
1017 if (!m_ShutterConnected && value == "Online")
1018 {
1019 m_ShutterConnected = true;
1020 LOG_INFO("Shutter is connected.");
1021 }
1022 else if (m_ShutterConnected && value != "Online")
1023 {
1024 m_ShutterConnected = false;
1025 LOG_WARN("Lost connection to the shutter!");
1026 }
1027 return true;
1028
1029 case ND::ROTATOR_POSITION:
1030 {
1031 try
1032 {
1033 // 153 = full_steps_circumference / 360 = 55080 / 360
1034 double newAngle = range360(std::stoi(value) / StepsPerDegree);
1035 if (std::fabs(DomeAbsPosN[0].value - newAngle) > 0.001)
1036 {
1037 DomeAbsPosN[0].value = newAngle;
1038 IDSetNumber(&DomeAbsPosNP, nullptr);
1039 }
1040 }
1041 catch (...)
1042 {
1043 return false;
1044 }
1045 }
1046 return true;
1047
1048 case ND::SHUTTER_POSITION:
1049 {
1050 try
1051 {
1052 int32_t position = std::stoi(value);
1053 if (std::abs(position - ShutterSyncN[0].value) > 0)
1054 {
1055 ShutterSyncN[0].value = position;
1056 IDSetNumber(&ShutterSyncNP, nullptr);
1057 }
1058 }
1059 catch (...)
1060 {
1061 return false;
1062 }
1063 }
1064 return true;
1065
1066 case ND::ROTATOR_REPORT:
1067 return processRotatorReport(value);
1068
1069 case ND::SHUTTER_REPORT:
1070 return processShutterReport(value);
1071
1072 case ND::ROTATOR_LEFT:
1073 case ND::ROTATOR_RIGHT:
1074 if (getDomeState() != DOME_MOVING && getDomeState() != DOME_PARKING)
1075 {
1076 setDomeState(DOME_MOVING);
1077 LOGF_INFO("Dome is rotating %s.", ((kv.first == ND::ROTATOR_LEFT) ? "counter-clock wise" : "clock-wise"));
1078 }
1079 return true;
1080
1081 case ND::ROTATOR_STOPPED:
1082 if (getDomeState() == DOME_MOVING)
1083 {
1084 LOG_INFO("Dome reached target position.");
1085 setDomeState(DOME_SYNCED);
1086 }
1087 else if (getDomeState() == DOME_PARKING)
1088 {
1089 LOG_INFO("Dome is parked.");
1090 setDomeState(DOME_PARKED);
1091 }
1092 else
1093 setDomeState(DOME_IDLE);
1094 return true;
1095
1096 case ND::SHUTTER_OPENING:
1097 if (getShutterState() != SHUTTER_MOVING)
1098 {
1099 setShutterState(SHUTTER_MOVING);
1100 LOG_INFO("Shutter is opening...");
1101 break;
1102 }
1103 return true;
1104
1105 case ND::SHUTTER_CLOSING:
1106 if (getShutterState() != SHUTTER_MOVING)
1107 {
1108 setShutterState(SHUTTER_MOVING);
1109 LOG_INFO("Shutter is closing...");
1110 break;
1111 }
1112 return true;
1113
1114 case ND::SHUTTER_BATTERY:
1115 {
1116 try
1117 {
1118 uint32_t battery_adu = std::stoul(value);
1119 double vref = battery_adu * ND::ADU_TO_VREF;
1120 if (std::fabs(vref - ShutterBatteryLevelN[0].value) > 0.01)
1121 {
1122 ShutterBatteryLevelN[0].value = vref;
1123 // TODO: Must check if batter is OK, warning, or in critical level
1124 ShutterBatteryLevelNP.s = IPS_OK;
1125 IDSetNumber(&ShutterBatteryLevelNP, nullptr);
1126 }
1127 }
1128 catch(...)
1129 {
1130 return false;
1131 }
1132 }
1133 break;
1134
1135 default:
1136 LOGF_DEBUG("Unhandled event: %s", value.c_str());
1137 break;
1138 }
1139 }
1140
1141 return false;
1142 }
1143
1144 //////////////////////////////////////////////////////////////////////
1145 ///
1146 //////////////////////////////////////////////////////////////////////
1147 bool NexDome::processRotatorReport(const std::string &report)
1148 {
1149 std::regex re(R"((\d+),(\d+),(\d+),(\d+),(\d+))");
1150 std::smatch match;
1151 if (std::regex_search(report, match, re))
1152 {
1153 try
1154 {
1155 uint32_t position = std::stoul(match.str(1));
1156 uint32_t at_home = std::stoul(match.str(2));
1157 uint32_t cirumference = std::stoul(match.str(3));
1158 uint32_t home_position = std::stoul(match.str(4));
1159 uint32_t dead_zone = std::stoul(match.str(5));
1160
1161 double newStepsPerDegree = cirumference / 360.0;
1162 if (std::abs(newStepsPerDegree - StepsPerDegree) > 0.01)
1163 StepsPerDegree = newStepsPerDegree;
1164
1165 if (std::abs(position - RotatorSyncN[0].value) > 0)
1166 {
1167 RotatorSyncN[0].value = position;
1168 IDSetNumber(&RotatorSyncNP, nullptr);
1169 }
1170
1171 double posAngle = range360(position / StepsPerDegree);
1172 if (std::fabs(posAngle - DomeAbsPosN[0].value) > 0.01)
1173 {
1174 DomeAbsPosN[0].value = posAngle;
1175 IDSetNumber(&DomeAbsPosNP, nullptr);
1176 }
1177
1178 double homeAngle = range360(home_position / StepsPerDegree);
1179 if (std::fabs(homeAngle - HomePositionN[0].value) > 0.01)
1180 {
1181 HomePositionN[0].value = homeAngle;
1182 IDSetNumber(&HomePositionNP, nullptr);
1183 }
1184
1185 double homeDiff = std::abs(homeAngle - posAngle);
1186 if (GoHomeSP.s == IPS_BUSY &&
1187 ((GoHomeS[HOME_FIND].s == ISS_ON && at_home == 1) ||
1188 (GoHomeS[HOME_GOTO].s == ISS_ON && homeDiff <= 0.1)))
1189 {
1190 LOG_INFO("Rotator reached home position.");
1191 IUResetSwitch(&GoHomeSP);
1192 GoHomeSP.s = IPS_OK;
1193 IDSetSwitch(&GoHomeSP, nullptr);
1194 }
1195
1196 if (dead_zone != static_cast<uint32_t>(RotatorSettingsN[S_ZONE].value))
1197 {
1198 RotatorSettingsN[S_ZONE].value = dead_zone;
1199 IDSetNumber(&RotatorSettingsNP, nullptr);
1200 }
1201
1202 // update to fix issue with movement across 0 degrees
1203 // for example, if the dead zone is 0.5 degrees, then NexDome won't move if going from 0.1 to 359.9 degrees.
1204 // however driver expects response from rotator unless difference calculation is modified, and movement stalls
1205 // e.g. the angles 0.1 and -0.1 should be compared instead
1206 int a = position;
1207 int b = m_TargetAZSteps;
1208
1209 if (getDomeState() == DOME_MOVING || getDomeState() == DOME_PARKING)
1210 {
1211 // if a > 0 and b < 360 and both within dead zone, make b negative equivalent angle
1212 if(a >= 0 && a <= int(dead_zone) && b >= (int(cirumference) - int(dead_zone)))
1213 {
1214 b -= int(cirumference);
1215 } // if opposite case then make a the negative angle equivalent
1216 else if(b >= 0 && b <=int(dead_zone) && a >= (int(cirumference) - int(dead_zone)))
1217 {
1218 a -= int(cirumference);
1219 }
1220
1221 // If we reach target position. (now calculation is correct)
1222 if (std::abs(a - b) <= int(dead_zone) )
1223 {
1224 if (getDomeState() == DOME_MOVING)
1225 {
1226 LOG_INFO("Dome reached target position.");
1227 setDomeState(DOME_SYNCED);
1228 }
1229 else if (getDomeState() == DOME_PARKING)
1230 {
1231 LOG_INFO("Dome is parked.");
1232 SetParked(true);
1233 //setDomeState(DOME_PARKED);
1234 }
1235 }
1236 }
1237 }
1238 catch(...)
1239 {
1240 return false;
1241 }
1242 }
1243
1244 return true;
1245 }
1246
1247 //////////////////////////////////////////////////////////////////////
1248 ///
1249 //////////////////////////////////////////////////////////////////////
1250 bool NexDome::processShutterReport(const std::string &report)
1251 {
1252 std::regex re(R"((-?\d+),(\d+),(\d+),(\d+))");
1253 std::smatch match;
1254 if (std::regex_search(report, match, re))
1255 {
1256 try
1257 {
1258 int32_t position = std::stoi(match.str(1));
1259 int32_t travel_limit = std::stoi(match.str(2));
1260 bool open_limit_switch = std::stoul(match.str(3)) == 1;
1261 bool close_limit_switch = std::stoul(match.str(4)) == 1;
1262
1263 if (std::abs(position - ShutterSyncN[0].value) > 0)
1264 {
1265 ShutterSyncN[0].value = position;
1266 IDSetNumber(&ShutterSyncNP, nullptr);
1267 }
1268
1269 INDI_UNUSED(travel_limit);
1270
1271 if (getShutterState() == SHUTTER_MOVING || getShutterState() == SHUTTER_UNKNOWN)
1272 {
1273 //if (position == travel_limit || open_limit_switch)
1274 if (open_limit_switch)
1275 {
1276 setShutterState(SHUTTER_OPENED);
1277 LOG_INFO("Shutter is fully opened.");
1278
1279 if (getDomeState() == DOME_UNPARKING)
1280 SetParked(false);
1281 }
1282 //else if (position == 0 || close_limit_switch)
1283 else if (close_limit_switch)
1284 {
1285 setShutterState(SHUTTER_CLOSED);
1286 LOG_INFO("Shutter is fully closed.");
1287 }
1288
1289 }
1290
1291 }
1292 catch(...)
1293 {
1294 return false;
1295 }
1296 }
1297
1298 return true;
1299 }
1300
1301
1302 //////////////////////////////////////////////////////////////////////////////
1303 ///
1304 //////////////////////////////////////////////////////////////////////////////
1305 bool NexDome::sendCommand(const char * cmd, char * res, int cmd_len, int res_len)
1306 {
1307 int nbytes_written = 0, nbytes_read = 0, rc = -1;
1308
1309 tcflush(PortFD, TCIOFLUSH);
1310
1311 if (cmd_len > 0)
1312 {
1313 char hex_cmd[ND::DRIVER_LEN * 3] = {0};
1314 hexDump(hex_cmd, cmd, cmd_len);
1315 LOGF_DEBUG("CMD <%s>", hex_cmd);
1316 rc = tty_write(PortFD, cmd, cmd_len, &nbytes_written);
1317 }
1318 else
1319 {
1320 LOGF_DEBUG("CMD <%s>", cmd);
1321 char cmd_terminated[ND::DRIVER_LEN * 2] = {0};
1322 snprintf(cmd_terminated, ND::DRIVER_LEN * 2, "%s\r\n", cmd);
1323 rc = tty_write_string(PortFD, cmd_terminated, &nbytes_written);
1324 }
1325
1326 if (rc != TTY_OK)
1327 {
1328 char errstr[MAXRBUF] = {0};
1329 tty_error_msg(rc, errstr, MAXRBUF);
1330 LOGF_ERROR("Serial write error: %s.", errstr);
1331 return false;
1332 }
1333
1334 if (res == nullptr)
1335 return true;
1336
1337 if (res_len > 0)
1338 rc = tty_read(PortFD, res, res_len, ND::DRIVER_TIMEOUT, &nbytes_read);
1339 else
1340 rc = tty_nread_section(PortFD, res, ND::DRIVER_LEN, ND::DRIVER_STOP_CHAR, ND::DRIVER_TIMEOUT, &nbytes_read);
1341
1342 if (rc != TTY_OK)
1343 {
1344 char errstr[MAXRBUF] = {0};
1345 tty_error_msg(rc, errstr, MAXRBUF);
1346 LOGF_ERROR("Serial read error: %s.", errstr);
1347 return false;
1348 }
1349
1350 if (res_len > 0)
1351 {
1352 char hex_res[ND::DRIVER_LEN * 3] = {0};
1353 hexDump(hex_res, res, res_len);
1354 LOGF_DEBUG("RES <%s>", hex_res);
1355 }
1356 else
1357 {
1358 res[nbytes_read - 1] = 0;
1359 LOGF_DEBUG("RES <%s>", res);
1360 }
1361
1362 tcflush(PortFD, TCIOFLUSH);
1363
1364 return true;
1365 }
1366
1367 //////////////////////////////////////////////////////////////////////////////
1368 ///
1369 //////////////////////////////////////////////////////////////////////////////
1370 void NexDome::hexDump(char * buf, const char * data, int size)
1371 {
1372 for (int i = 0; i < size; i++)
1373 sprintf(buf + 3 * i, "%02X ", static_cast<uint8_t>(data[i]));
1374
1375 if (size > 0)
1376 buf[3 * size - 1] = '\0';
1377 }
1378
1379 //////////////////////////////////////////////////////////////////////
1380 ///
1381 //////////////////////////////////////////////////////////////////////
1382 std::vector<std::string> NexDome::split(const std::string &input, const std::string &regex)
1383 {
1384 // passing -1 as the submatch index parameter performs splitting
1385 std::regex re(regex);
1386 std::sregex_token_iterator
1387 first{input.begin(), input.end(), re, -1},
1388 last;
1389 return {first, last};
1390 }
1391
1392 //////////////////////////////////////////////////////////////////////
1393 ///
1394 //////////////////////////////////////////////////////////////////////
1395 std::string &NexDome::ltrim(std::string &str, const std::string &chars)
1396 {
1397 str.erase(0, str.find_first_not_of(chars));
1398 return str;
1399 }
1400
1401 //////////////////////////////////////////////////////////////////////
1402 ///
1403 //////////////////////////////////////////////////////////////////////
1404 std::string &NexDome::rtrim(std::string &str, const std::string &chars)
1405 {
1406 str.erase(str.find_last_not_of(chars) + 1);
1407 return str;
1408 }
1409
1410 //////////////////////////////////////////////////////////////////////
1411 ///
1412 //////////////////////////////////////////////////////////////////////
1413 std::string &NexDome::trim(std::string &str, const std::string &chars)
1414 {
1415 return ltrim(rtrim(str, chars), chars);
1416 }