Instrument Neutral Distributed Interface INDI 2.2.5
wanderer_rotator_base.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2025 Frank Wang & Jérémie Klein. All rights reserved.
3
4 WandererRotator Lite V1
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 "indicom.h"
28#include <cmath>
29#include <cstring>
30#include <memory>
31#include <termios.h>
32#include <unistd.h>
33#include <stdio.h>
34#include <sstream>
35#include <iostream>
36
37
38
45{
46
48
50
52 // Calibrate
53 SetZeroSP[0].fill("Set_Zero", "Mechanical Zero", ISS_OFF);
54 SetZeroSP.fill(getDeviceName(), "Set_Zero", "Set Current As", MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
55
56 // BACKLASH
57 BacklashNP[BACKLASH].fill( "BACKLASH", "Degree", "%.2f", 0, 3, 0.1, 0);
58 BacklashNP.fill(getDeviceName(), "BACKLASH", "Backlash", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
59
60 // ACCURACY
61 AccuracyNP[0].fill("ACCURACY", "Degree (0=off)", "%.2f", 0, 10, 0.1, 0);
62 AccuracyNP.fill(getDeviceName(), "ACCURACY", "Accuracy", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
63 AccuracyNP.load();
64
65 // DEVICE ID
66 DeviceIDNP[0].fill("DEVICE_ID", "ID", "%.0f", 1, 10, 1, 1);
67 DeviceIDNP.fill(getDeviceName(), "DEVICE_ID", "Device ID", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
68
70
71
72 return true;
73}
74
95
96bool WandererRotatorBase::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
97{
98
99 if (dev && !strcmp(dev, getDeviceName()))
100 {
101 if (SetZeroSP.isNameMatch(name))
102 {
103 SetZeroSP.setState(sendCommand("1500002") ? IPS_OK : IPS_ALERT);
104 SetZeroSP.apply();
105 GotoRotatorNP[0].setValue(0);
106 GotoRotatorNP.apply();
107 LOG_INFO("Virtual Mechanical Angle is set to zero.");
108 return true;
109 }
110 }
111 return Rotator::ISNewSwitch(dev, name, states, names, n);
112}
113
114bool WandererRotatorBase::ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n)
115{
116 if (dev && !strcmp(dev, getDeviceName()))
117 {
118 // backlash
119 if (BacklashNP.isNameMatch(name))
120 {
121 bool rc1 = false;
122 BacklashNP.update(values, names, n);
124
125 char cmd[16];
126 snprintf(cmd, 16, "%d", (int)(backlash * 10 + 1600000));
127 rc1 = sendCommand(cmd);
128
129 BacklashNP.setState( (rc1) ? IPS_OK : IPS_ALERT);
130 if (BacklashNP.getState() == IPS_OK)
131 BacklashNP.update(values, names, n);
132 BacklashNP.apply();
133 LOG_INFO("Backlash Set");
134 return true;
135 }
136
137 // accuracy
138 if (AccuracyNP.isNameMatch(name))
139 {
140 return updateProperty(AccuracyNP, values, names, n, [this, values]()
141 {
142 accuracy = values[0];
143 return true;
144 });
145 }
146
147 // device id
148 if (DeviceIDNP.isNameMatch(name))
149 {
150 return updateProperty(DeviceIDNP, values, names, n, [this, values]()
151 {
152 int id = static_cast<int>(values[0]);
153 char cmd[16];
154 snprintf(cmd, 16, "%d", 2200000 + id);
155 if (!sendCommand(cmd))
156 return false;
157 char resp[64] = {0};
158 int nbytes_read = 0;
159 if (tty_read_section(PortFD, resp, '\n', 3, &nbytes_read) != TTY_OK ||
160 strstr(resp, "Device ID set to") == nullptr)
161 {
162 LOG_ERROR("Device did not confirm the new ID.");
163 return false;
164 }
165 deviceID = id;
166 LOGF_INFO("Device ID set to %d.", id);
167 return true;
168 });
169 }
170
171 }
172 return Rotator::ISNewNumber(dev, name, values, names, n);
173}
174
176{
178 AccuracyNP.save(fp);
179 return true;
180}
181
183{
184 PortFD = serialConnection->getPortFD();
185 tcflush(PortFD, TCIOFLUSH);
186 int nbytes_read_name = 0, nbytes_written = 0, rc = -1;
187 char name[64] = {0};
188
189 if ((rc = tty_write_string(PortFD, "1500001", &nbytes_written)) != TTY_OK)
190 {
191 char errorMessage[MAXRBUF];
192 tty_error_msg(rc, errorMessage, MAXRBUF);
193 LOGF_ERROR("Serial write error: %s", errorMessage);
194 return false;
195 }
196
197 //Device Model//////////////////////////////////////////////////////////////////////////////////////////////////////
198 if ((rc = tty_read_section(PortFD, name, 'A', 3, &nbytes_read_name)) != TTY_OK)
199 {
200 tcflush(PortFD, TCIOFLUSH);
201 if ((rc = tty_write_string(PortFD, "1500001", &nbytes_written)) != TTY_OK)
202 {
203 char errorMessage[MAXRBUF];
204 tty_error_msg(rc, errorMessage, MAXRBUF);
205 LOGF_ERROR("Serial write error: %s", errorMessage);
206 return false;
207 }
208 if ((rc = tty_read_section(PortFD, name, 'A', 3, &nbytes_read_name)) != TTY_OK)
209 {
210 char errorMessage[MAXRBUF];
211 tty_error_msg(rc, errorMessage, MAXRBUF);
212 LOG_INFO("No data received, the device may not be WandererRotator, please check the serial port!");
213 LOGF_ERROR("Device read error: %s", errorMessage);
214 return false;
215 }
216 }
217 name[nbytes_read_name - 1] = '\0';
218 if(strcmp(name, getRotatorHandshakeName()) != 0)
219 {
220 LOGF_ERROR("The device is not %s", getDefaultName());
221 LOGF_INFO("The device is %s.", name);
222 return false;
223 }
224 // Frimware version/////////////////////////////////////////////////////////////////////////////////////////////
225 int nbytes_read_version = 0;
226 char version[64] = {0};
227 tty_read_section(PortFD, version, 'A', 5, &nbytes_read_version);
228
229 version[nbytes_read_version - 1] = '\0';
230 LOGF_INFO("Firmware Version:%s", version);
231 firmware = std::atoi(version);
233 {
234 LOG_ERROR("The firmware is outdated, please upgrade to the latest firmware!");
235 LOGF_ERROR("The current firmware is %d.", firmware);
236 return false;
237 }
239 {
240 LOG_WARN("The firmware is outdated, please upgrade as soon as possible.");
241 LOG_WARN("Firmware Upgrade Tool for Linux/Mac is now available for download on the WandererAstro website.");
242 LOGF_WARN("The current firmware is %d.", firmware);
243 }
244
245 // Angle//////////////////////////////////////////////////////////////////////////////////////////
246 char M_angle[64] = {0};
247 int nbytes_read_M_angle = 0;
248 tty_read_section(PortFD, M_angle, 'A', 5, &nbytes_read_M_angle);
249 M_angle[nbytes_read_M_angle - 1] = '\0';
250 M_angleread = std::strtod(M_angle, NULL);
251
252 if(std::fabs(M_angleread) > 400000)
253 {
254 rc = sendCommand("1500002");
255 LOG_WARN("Virtual Mechanical Angle is too large, it is now set to zero!");
256 rc = sendCommand("1500001");
257 rc = tty_read_section(PortFD, M_angle, 'A', 5, &nbytes_read_M_angle);
258 rc = tty_read_section(PortFD, M_angle, 'A', 5, &nbytes_read_M_angle);
259 rc = tty_read_section(PortFD, M_angle, 'A', 5, &nbytes_read_M_angle);
260 M_angle[nbytes_read_M_angle - 1] = '\0';
261 M_angleread = std::strtod(M_angle, NULL);
262 }
263 GotoRotatorNP[0].setValue(std::fabs(M_angleread / 1000));
264 //backlash/////////////////////////////////////////////////////////////////////
265 char M_backlash[64] = {0};
266 int nbytes_read_M_backlash = 0;
267 tty_read_section(PortFD, M_backlash, 'A', 5, &nbytes_read_M_backlash);
268 M_backlash[nbytes_read_M_backlash - 1] = '\0';
269 M_backlashread = std::strtod(M_backlash, NULL);
270
272 BacklashNP.setState(IPS_OK);
273 BacklashNP.apply();
274 //reverse/////////////////////////////////////////////////////////////////////
275 char M_reverse[64] = {0};
276 int nbytes_read_M_reverse = 0;
277 tty_read_section(PortFD, M_reverse, 'A', 5, &nbytes_read_M_reverse);
278 M_reverse[nbytes_read_M_reverse - 1] = '\0';
279 M_reverseread = std::strtod(M_reverse, NULL);
280 if(M_reverseread == 0)
281 {
282 ReverseRotator(false);
283 }
284 else
285 {
286 ReverseRotator(true);
287 }
288
289 char M_id[64] = {0};
290 int nbytes_read_M_id = 0;
291 if (tty_read_section(PortFD, M_id, 'A', 2, &nbytes_read_M_id) == TTY_OK && nbytes_read_M_id > 1)
292 {
293 M_id[nbytes_read_M_id - 1] = '\0';
294 deviceID = std::strtod(M_id, NULL);
295 DeviceIDNP[0].setValue(deviceID);
296 }
297 DeviceIDNP.apply();
298
299 tcflush(PortFD, TCIOFLUSH);
300 return true;
301}
302
303
305{
306 if (accuracy > 0)
307 angle = std::round(angle / accuracy) * accuracy;
308
309 angle = angle - GotoRotatorNP[0].getValue();
310
311 // Gate on the actual step delta the firmware will see. Anything that
312 // rounds to zero steps is a no-op the firmware won't acknowledge, so
313 // return immediately instead of blocking in TimerHit ("not powered").
314 int steps = static_cast<int>(std::round(angle * getStepsPerDegree()));
315 if (steps == 0)
316 return IPS_OK;
317
318 char cmd[16];
319 int position = steps + getMovePositionOffset();
320 positionhistory = angle;
321 snprintf(cmd, 16, "%d", position);
322 Move(cmd);
323
324 return IPS_BUSY;
325}
326
327
329{
330
331
332 if (GotoRotatorNP.getState() == IPS_BUSY)
333 {
334 haltcommand = true;
335 int nbytes_written = 0, rc = -1;
336 if ((rc = tty_write_string(PortFD, "Stop", &nbytes_written)) != TTY_OK)
337 {
338 char errorMessage[MAXRBUF];
339 tty_error_msg(rc, errorMessage, MAXRBUF);
340 LOGF_ERROR("Serial write error: %s", errorMessage);
341 return false;
342 }
343 SetTimer(100);
344 }
345 return true;
346}
347
353{
354 if(GotoRotatorNP[0].getValue() != 0)
355 {
356 double angle = -1 * GotoRotatorNP[0].getValue();
357 positionhistory = angle;
358 char cmd[16];
359 int position = (int)(angle * getStepsPerDegree() + getMovePositionOffset());
360 snprintf(cmd, 16, "%d", position);
361 GotoRotatorNP.setState(IPS_BUSY);
362 Move(cmd);
363 LOG_INFO("Moving to zero...");
364 }
365 return IPS_OK;
366}
367
368
370{
371
372 if (enabled)
373 {
374 char cmd[16];
375 snprintf(cmd, 16, "%d", 1700001);
376 if (sendCommand(cmd) != true)
377 {
378 LOG_ERROR("Serial write error.");
379 return false;
380 }
381 ReverseState = true;
382 return true;
383 }
384 else
385 {
386 char cmd[16];
387 snprintf(cmd, 16, "%d", 1700000);
388 if (sendCommand(cmd) != true)
389 {
390 LOG_ERROR("Serial write error.");
391 return false;
392 }
393 ReverseState = false;
394 return true;
395 }
396 return false;
397}
398
399
400
402{
403
404 if (GotoRotatorNP.getState() == IPS_BUSY || haltcommand == true)
405 {
406
407 if(nowtime < estime && haltcommand == false)
408 {
409 GotoRotatorNP[0].setValue(GotoRotatorNP[0].getValue() + std::copysign(1.0, positionhistory));
410 GotoRotatorNP.apply();
411 nowtime = nowtime + 240;
412 SetTimer(240);
413 return;
414 }
415 else
416 {
417 int rc = -1;
418 estime = 0;
419 nowtime = 0;
420 char resp[128] = {0};
421 int nbytes_read = 0;
422 rc = tty_read_section(PortFD, resp, '\n', 15, &nbytes_read);
423 if (rc != TTY_OK)
424 {
425 LOG_ERROR("No response from rotator.");
426 GotoRotatorNP[0].setValue(initangle);
427 GotoRotatorNP.setState(IPS_ALERT);
428 GotoRotatorNP.apply();
429 }
430 else if (strncmp(resp, "NP", 2) == 0)
431 {
432 LOG_ERROR("Rotator not powered!");
433 GotoRotatorNP[0].setValue(initangle);
434 GotoRotatorNP.setState(IPS_ALERT);
435 GotoRotatorNP.apply();
436 }
437 else if (strncmp(resp, "LIMIT", 5) == 0)
438 {
439 LOG_ERROR("Requested position has reached the limit.");
440 GotoRotatorNP[0].setValue(initangle);
441 GotoRotatorNP.setState(IPS_ALERT);
442 GotoRotatorNP.apply();
443 }
444 else
445 {
446 char *lastA = strrchr(resp, 'A');
447 if (lastA)
448 *lastA = '\0';
449 char *angleStr = strrchr(resp, 'A');
450 angleStr = angleStr ? angleStr + 1 : resp;
451 M_angleread = std::strtod(angleStr, NULL);
452 GotoRotatorNP[0].setValue(std::fabs(M_angleread / 1000));
453 GotoRotatorNP.setState(IPS_OK);
454 GotoRotatorNP.apply();
455 }
456 haltcommand = false;
457 }
458 }
459
460
461 SetTimer(2000);
462}
463
464
465
467{
468 initangle = GotoRotatorNP[0].getValue();
469 int nbytes_written = 0, rc = -1;
470 LOGF_DEBUG("CMD <%s>", cmd);
471 if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
472 {
473 char errorMessage[MAXRBUF];
474 tty_error_msg(rc, errorMessage, MAXRBUF);
475 LOGF_ERROR("Serial write error: %s", errorMessage);
476 return false;
477 }
478 SetTimer(2000);
479 nowtime = 0;
480 estime = abs((std::atoi(cmd) - getMovePositionOffset()) / getStepsPerDegree() * 240);
481 return true;
482}
483
484
485bool WandererRotatorBase::sendCommand(std::string command)
486{
487 int nbytes_written = 0, rc = -1;
488 std::string command_termination = "\n";
489 LOGF_DEBUG("CMD: %s", command.c_str());
490 if ((rc = tty_write_string(PortFD, (command + command_termination).c_str(), &nbytes_written)) != TTY_OK)
491 {
492 char errorMessage[MAXRBUF];
493 tty_error_msg(rc, errorMessage, MAXRBUF);
494 LOGF_ERROR("Serial write error: %s", errorMessage);
495 return false;
496 }
497 return true;
498}
499
500
bool isConnected() const
bool updateProperty(PropertyType &property, ValueType *values, char *names[], int n, std::function< bool()> updater, bool saveConfig=false)
Generic helper function to update an INDI property based on an external operation.
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)
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void SetCapability(uint32_t cap)
SetRotatorCapability sets the Rotator capabilities. All capabilities must be initialized.
INDI::PropertyNumber GotoRotatorNP
Connection::Serial * serialConnection
Definition indirotator.h:95
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the reverse direction property in the configuration file
INDI::PropertyNumber BacklashNP
bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the reverse direction property in the configuration file
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
bool Handshake() override
perform handshake with device to check communication
bool ReverseRotator(bool enabled) override
ReverseRotator Reverse the direction of the rotator. CW is usually the normal direction,...
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual const char * getRotatorHandshakeName()=0
bool AbortRotator() override
AbortRotator Abort all motion.
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
IPState MoveRotator(double angle) override
MoveRotator Go to specific angle.
INDI::PropertyNumber AccuracyNP
IPState HomeRotator() override
WandererRotatorBase::HomeRotator.
INDI::PropertyNumber DeviceIDNP
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual const char * getDefaultName() override=0
virtual int getMinimumCompatibleFirmwareVersion()=0
virtual int getMovePositionOffset()
virtual int getStepsPerDegree()=0
bool Move(const char *cmd)
bool sendCommand(std::string command)
virtual int getLatestFirmwareVersion()
INDI::PropertySwitch SetZeroSP
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
ISState
Switch state.
Definition indiapi.h:148
@ ISS_OFF
Definition indiapi.h:149
@ IP_RW
Definition indiapi.h:184
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_ATMOST1
Definition indiapi.h:172
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition indicom.c:653
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
Implementations for common driver routines.
@ TTY_OK
Definition indicom.h:54
#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 LOGF_WARN(fmt,...)
Definition indilogger.h:81
#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
uint8_t cmd[4]
Definition pwc-ioctl.h:2