Instrument Neutral Distributed Interface INDI 2.2.5
indipacinterface.cpp
Go to the documentation of this file.
1/*
2 PAC Interface (Polar Alignment Correction)
3 Copyright (C) 2026 Joaquin Rodriguez ([email protected])
4 Copyright (C) 2026 Jasem Mutlaq ([email protected])
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20*/
21
22#include "indipacinterface.h"
23#include "defaultdevice.h"
24#include "indilogger.h"
25
26#include <cstring>
27
28namespace INDI
29{
30
32 : m_DefaultDevice(defaultDevice)
33{
34}
35
36void PACInterface::initProperties(const char *group)
37{
38 // Manual adjustment: write non-zero value to move that axis immediately.
39 // AZ step: positive = East, negative = West.
40 // ALT step: positive = North (increase altitude), negative = South (decrease altitude).
41 ManualAdjustmentNP[MANUAL_AZ].fill("MANUAL_AZ_STEP", "Azimuth Step (deg)", "%.4f", -10, 10, 0.1, 0);
42 ManualAdjustmentNP[MANUAL_ALT].fill("MANUAL_ALT_STEP", "Altitude Step (deg)", "%.4f", -10, 10, 0.1, 0);
43 ManualAdjustmentNP.fill(m_DefaultDevice->getDeviceName(), "PAC_MANUAL_ADJUSTMENT", "Manual Adjustment",
44 group, IP_WO, 0, IPS_IDLE);
45
46 // Abort motion
47 AbortSP[0].fill("ABORT", "Abort", ISS_OFF);
48 AbortSP.fill(m_DefaultDevice->getDeviceName(), "PAC_ABORT_MOTION", "Abort Motion",
49 group, IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
50
51 // Current position (only shown when PAC_HAS_POSITION is set)
52 PositionNP[POSITION_AZ].fill("POSITION_AZ", "Azimuth (deg)", "%.4f", -360, 360, 0, 0);
53 PositionNP[POSITION_ALT].fill("POSITION_ALT", "Altitude (deg)", "%.4f", -90, 90, 0, 0);
54 PositionNP.fill(m_DefaultDevice->getDeviceName(), "PAC_POSITION", "Position",
55 group, IP_RO, 0, IPS_IDLE);
56
57 // Motor speed (only shown when PAC_HAS_SPEED is set)
58 SpeedNP[0].fill("PAC_SPEED_VALUE", "Speed", "%.0f", 1, 10, 1, 1);
59 SpeedNP.fill(m_DefaultDevice->getDeviceName(), "PAC_SPEED", "Speed", group, IP_RW, 60, IPS_OK);
60
61 // Azimuth reverse (only shown when PAC_CAN_REVERSE is set)
62 AZReverseSP[DefaultDevice::INDI_ENABLED].fill("INDI_ENABLED", "Enabled", ISS_OFF);
63 AZReverseSP[DefaultDevice::INDI_DISABLED].fill("INDI_DISABLED", "Disabled", ISS_ON);
64 AZReverseSP.fill(m_DefaultDevice->getDeviceName(), "PAC_AZ_REVERSE", "Azimuth Reverse", group, IP_RW,
66
67 // Altitude reverse (only shown when PAC_CAN_REVERSE is set)
68 ALTReverseSP[DefaultDevice::INDI_ENABLED].fill("INDI_ENABLED", "Enabled", ISS_OFF);
69 ALTReverseSP[DefaultDevice::INDI_DISABLED].fill("INDI_DISABLED", "Disabled", ISS_ON);
70 ALTReverseSP.fill(m_DefaultDevice->getDeviceName(), "PAC_ALT_REVERSE", "Altitude Reverse", group, IP_RW,
72
73 // Home management (only shown when PAC_CAN_HOME is set)
74 HomeSP[HOME_SET].fill("HOME_SET", "Set Home", ISS_OFF);
75 HomeSP[HOME_GO].fill("HOME_GO", "Return Home", ISS_OFF);
76 HomeSP[HOME_RESET].fill("HOME_RESET", "Reset Home", ISS_OFF);
77 HomeSP.fill(m_DefaultDevice->getDeviceName(), "PAC_HOME", "Home", group, IP_RW,
79
80 // Sync position (only shown when PAC_CAN_SYNC is set)
81 SyncNP[SYNC_AZ].fill("SYNC_AZ", "Azimuth (deg)", "%.4f", -360, 360, 0, 0);
82 SyncNP[SYNC_ALT].fill("SYNC_ALT", "Altitude (deg)", "%.4f", -90, 90, 0, 0);
83 SyncNP.fill(m_DefaultDevice->getDeviceName(), "PAC_SYNC", "Sync Position",
84 group, IP_RW, 60, IPS_IDLE);
85
86 // Backlash enable/disable (only shown when PAC_HAS_BACKLASH is set)
87 BacklashSP[DefaultDevice::INDI_ENABLED].fill("INDI_ENABLED", "Enabled", ISS_OFF);
88 BacklashSP[DefaultDevice::INDI_DISABLED].fill("INDI_DISABLED", "Disabled", ISS_ON);
89 BacklashSP.fill(m_DefaultDevice->getDeviceName(), "PAC_BACKLASH_ENABLED", "Backlash", group, IP_RW,
91
92 // Backlash steps (only shown when PAC_HAS_BACKLASH is set)
93 BacklashNP[BACKLASH_AZ].fill("BACKLASH_AZ", "Azimuth (steps)", "%.0f", 0, 10000, 1, 0);
94 BacklashNP[BACKLASH_ALT].fill("BACKLASH_ALT", "Altitude (steps)", "%.0f", 0, 10000, 1, 0);
95 BacklashNP.fill(m_DefaultDevice->getDeviceName(), "PAC_BACKLASH_STEPS", "Backlash Steps",
96 group, IP_RW, 60, IPS_IDLE);
97}
98
100{
101 if (m_DefaultDevice->isConnected())
102 {
103 m_DefaultDevice->defineProperty(ManualAdjustmentNP);
104 m_DefaultDevice->defineProperty(AbortSP);
105 if (HasPosition())
106 m_DefaultDevice->defineProperty(PositionNP);
107 if (HasSpeed())
108 m_DefaultDevice->defineProperty(SpeedNP);
109 if (CanReverse())
110 {
111 m_DefaultDevice->defineProperty(AZReverseSP);
112 m_DefaultDevice->defineProperty(ALTReverseSP);
113 }
114 if (CanHome())
115 m_DefaultDevice->defineProperty(HomeSP);
116 if (CanSync())
117 m_DefaultDevice->defineProperty(SyncNP);
118 if (HasBacklash())
119 {
120 m_DefaultDevice->defineProperty(BacklashSP);
121 m_DefaultDevice->defineProperty(BacklashNP);
122 }
123 }
124 else
125 {
126 m_DefaultDevice->deleteProperty(ManualAdjustmentNP);
127 m_DefaultDevice->deleteProperty(AbortSP);
128 if (HasPosition())
129 m_DefaultDevice->deleteProperty(PositionNP);
130 if (HasSpeed())
131 m_DefaultDevice->deleteProperty(SpeedNP);
132 if (CanReverse())
133 {
134 m_DefaultDevice->deleteProperty(AZReverseSP);
135 m_DefaultDevice->deleteProperty(ALTReverseSP);
136 }
137 if (CanHome())
138 m_DefaultDevice->deleteProperty(HomeSP);
139 if (CanSync())
140 m_DefaultDevice->deleteProperty(SyncNP);
141 if (HasBacklash())
142 {
143 m_DefaultDevice->deleteProperty(BacklashSP);
144 m_DefaultDevice->deleteProperty(BacklashNP);
145 }
146 }
147
148 return true;
149}
150
151bool PACInterface::processSwitch(const char *dev, const char *name,
152 ISState *states, char *names[], int n)
153{
154 INDI_UNUSED(dev);
155
156 if (AbortSP.isNameMatch(name))
157 {
158 AbortSP.reset();
159 if (AbortMotion())
160 {
161 AbortSP.setState(IPS_OK);
162 if (ManualAdjustmentNP.getState() == IPS_BUSY)
163 {
165 ManualAdjustmentNP.apply();
166 }
167 if (HasPosition() && PositionNP.getState() == IPS_BUSY)
168 {
169 PositionNP.setState(IPS_IDLE);
170 PositionNP.apply();
171 }
172 }
173 else
174 {
175 AbortSP.setState(IPS_ALERT);
176 }
177 AbortSP.apply();
178 return true;
179 }
180
181 if (AZReverseSP.isNameMatch(name))
182 {
183 m_DefaultDevice->updateProperty(AZReverseSP, states, names, n, [this, states, names, n]()
184 {
185 const char *onSwitch = IUFindOnSwitchName(states, names, n);
186 bool enabled = onSwitch != nullptr && AZReverseSP[DefaultDevice::INDI_ENABLED].isNameMatch(onSwitch);
187 return ReverseAZ(enabled);
188 }, true);
189 return true;
190 }
191
192 if (ALTReverseSP.isNameMatch(name))
193 {
194 m_DefaultDevice->updateProperty(ALTReverseSP, states, names, n, [this, states, names, n]()
195 {
196 const char *onSwitch = IUFindOnSwitchName(states, names, n);
197 bool enabled = onSwitch != nullptr && ALTReverseSP[DefaultDevice::INDI_ENABLED].isNameMatch(onSwitch);
198 return ReverseALT(enabled);
199 }, true);
200 return true;
201 }
202
203 if (CanHome() && HomeSP.isNameMatch(name))
204 {
205 HomeSP.update(states, names, n);
206 int cmd = HomeSP.findOnSwitchIndex();
207 HomeSP.reset();
208
209 if (cmd == HOME_SET)
210 {
211 if (SetHome())
212 {
213 HomeSP.setState(IPS_OK);
214 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_SESSION, "Home position set.");
215 }
216 else
217 {
218 HomeSP.setState(IPS_ALERT);
219 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR, "Failed to set home position.");
220 }
221 }
222 else if (cmd == HOME_GO)
223 {
224 IPState rc = GoHome();
225 HomeSP.setState(rc);
226 if (rc == IPS_BUSY)
227 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_SESSION, "Returning to home position...");
228 else if (rc == IPS_OK)
229 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_SESSION, "Returned to home position.");
230 else
231 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR, "Failed to return to home position.");
232 }
233 else if (cmd == HOME_RESET)
234 {
235 if (ResetHome())
236 {
237 HomeSP.setState(IPS_OK);
238 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_SESSION, "Home position reset.");
239 }
240 else
241 {
242 HomeSP.setState(IPS_ALERT);
243 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR, "Failed to reset home position.");
244 }
245 }
246
247 HomeSP.apply();
248 return true;
249 }
250
251 if (HasBacklash() && BacklashSP.isNameMatch(name))
252 {
253 m_DefaultDevice->updateProperty(BacklashSP, states, names, n, [this, states, names, n]()
254 {
255 const char *onSwitch = IUFindOnSwitchName(states, names, n);
256 bool enabled = onSwitch != nullptr && BacklashSP[DefaultDevice::INDI_ENABLED].isNameMatch(onSwitch);
257 return SetBacklashEnabled(enabled);
258 }, true);
259 return true;
260 }
261
262 return false;
263}
264
265bool PACInterface::processNumber(const char *dev, const char *name,
266 double values[], char *names[], int n)
267{
268 INDI_UNUSED(dev);
269
270 if (SpeedNP.isNameMatch(name))
271 {
272 m_DefaultDevice->updateProperty(SpeedNP, values, names, n, [this, values]()
273 {
274 return SetPACSpeed(static_cast<uint16_t>(values[0]));
275 }, true);
276 return true;
277 }
278
279 if (ManualAdjustmentNP.isNameMatch(name))
280 {
281 ManualAdjustmentNP.update(values, names, n);
282
283 const double azStep = ManualAdjustmentNP[MANUAL_AZ].getValue();
284 const double altStep = ManualAdjustmentNP[MANUAL_ALT].getValue();
285
286 IPState azState = IPS_OK;
287 IPState altState = IPS_OK;
288
289 if (azStep != 0.0 && altStep != 0.0)
290 {
291 // Both axes requested — delegate to the driver's coordinated move
292 IPState state = MoveBoth(azStep, altStep);
293 azState = altState = state;
294 }
295 else
296 {
297 if (azStep != 0.0) azState = MoveAZ(azStep);
298 if (altStep != 0.0) altState = MoveALT(altStep);
299 }
300
301 // Overall state: worst of the two axes.
302 if (azState == IPS_ALERT || altState == IPS_ALERT)
304 else if (azState == IPS_BUSY || altState == IPS_BUSY)
306 else
307 ManualAdjustmentNP.setState(IPS_OK);
308
309 ManualAdjustmentNP.apply();
310 return true;
311 }
312
313 if (CanSync() && SyncNP.isNameMatch(name))
314 {
315 m_DefaultDevice->updateProperty(SyncNP, values, names, n, [this, values, names, n]()
316 {
317 SyncNP.update(values, names, n);
318 bool azOk = SyncAZ(SyncNP[SYNC_AZ].getValue());
319 bool altOk = SyncALT(SyncNP[SYNC_ALT].getValue());
320 return (azOk && altOk);
321 }, true);
322 return true;
323 }
324
325 if (HasBacklash() && BacklashNP.isNameMatch(name))
326 {
327 m_DefaultDevice->updateProperty(BacklashNP, values, names, n, [this, values, names, n]()
328 {
329 BacklashNP.update(values, names, n);
330 bool azOk = SetBacklashAZ(static_cast<int32_t>(BacklashNP[BACKLASH_AZ].getValue()));
331 bool altOk = SetBacklashALT(static_cast<int32_t>(BacklashNP[BACKLASH_ALT].getValue()));
332 return (azOk && altOk);
333 }, true);
334 return true;
335 }
336
337 return false;
338}
339
341{
342 if (HasSpeed())
343 SpeedNP.save(fp);
344 if (CanReverse())
345 {
346 AZReverseSP.save(fp);
347 ALTReverseSP.save(fp);
348 }
349 if (CanHome())
350 HomeSP.save(fp);
351 if (CanSync())
352 SyncNP.save(fp);
353 if (HasBacklash())
354 {
355 BacklashSP.save(fp);
356 BacklashNP.save(fp);
357 }
358 return true;
359}
360
361// ---------------------------------------------------------------------------
362// Default virtual implementations
363// ---------------------------------------------------------------------------
364
366{
367 INDI_UNUSED(degrees);
368 return IPS_ALERT;
369}
370
372{
373 INDI_UNUSED(degrees);
374 return IPS_ALERT;
375}
376
377IPState PACInterface::MoveBoth(double azDegrees, double altDegrees)
378{
379 // Default: call individual axis moves in sequence.
380 // Drivers that support a single combined command should override this.
381 IPState azState = MoveAZ(azDegrees);
382 IPState altState = MoveALT(altDegrees);
383 if (azState == IPS_ALERT || altState == IPS_ALERT) return IPS_ALERT;
384 if (azState == IPS_BUSY || altState == IPS_BUSY) return IPS_BUSY;
385 return IPS_OK;
386}
387
389{
390 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
391 "PAC device does not support aborting motion.");
392 return false;
393}
394
395bool PACInterface::SetPACSpeed(uint16_t speed)
396{
397 INDI_UNUSED(speed);
398 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
399 "PAC device does not support variable speed.");
400 return false;
401}
402
403bool PACInterface::ReverseAZ(bool enabled)
404{
405 INDI_UNUSED(enabled);
406 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
407 "PAC device does not support reversing azimuth direction.");
408 return false;
409}
410
411bool PACInterface::ReverseALT(bool enabled)
412{
413 INDI_UNUSED(enabled);
414 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
415 "PAC device does not support reversing altitude direction.");
416 return false;
417}
418
420{
421 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
422 "PAC device does not support setting home.");
423 return false;
424}
425
427{
428 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
429 "PAC device does not support returning to home.");
430 return IPS_ALERT;
431}
432
434{
435 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
436 "PAC device does not support resetting home.");
437 return false;
438}
439
440bool PACInterface::SyncAZ(double degrees)
441{
442 INDI_UNUSED(degrees);
443 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
444 "PAC device does not support syncing azimuth position.");
445 return false;
446}
447
448bool PACInterface::SyncALT(double degrees)
449{
450 INDI_UNUSED(degrees);
451 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
452 "PAC device does not support syncing altitude position.");
453 return false;
454}
455
457{
458 INDI_UNUSED(enabled);
459 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
460 "PAC device does not support backlash compensation.");
461 return false;
462}
463
465{
466 INDI_UNUSED(steps);
467 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
468 "PAC device does not support setting azimuth backlash.");
469 return false;
470}
471
473{
474 INDI_UNUSED(steps);
475 DEBUGDEVICE(m_DefaultDevice->getDeviceName(), INDI::Logger::DBG_ERROR,
476 "PAC device does not support setting altitude backlash.");
477 return false;
478}
479
480}
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual bool SetPACSpeed(uint16_t speed)
SetPACSpeed Set the motor speed used for axis movements.
INDI::PropertyNumber SyncNP
virtual bool ReverseALT(bool enabled)
ReverseALT Reverse the direction of altitude axis movement.
virtual bool ResetHome()
ResetHome Clear the stored home reference without moving.
virtual bool SetBacklashEnabled(bool enabled)
SetBacklashEnabled Enable or disable hardware backlash compensation.
INDI::PropertySwitch BacklashSP
virtual IPState GoHome()
GoHome Slew both axes back to the home (0, 0) position.
PACInterface(DefaultDevice *device)
@ MANUAL_AZ
Azimuth step in degrees: positive = East, negative = West.
@ MANUAL_ALT
Altitude step in degrees: positive = North, negative = South.
bool saveConfigItems(FILE *fp)
saveConfigItems Save PAC interface properties to config file. Call this from your driver's saveConfig...
virtual bool SetHome()
SetHome Mark the current position as the home (zero) reference.
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process PAC number properties.
INDI::PropertyNumber SpeedNP
@ POSITION_ALT
Current altitude position in degrees.
@ POSITION_AZ
Current azimuth position in degrees.
void initProperties(const char *group)
initProperties Initialize PAC properties. Call this from your driver's initProperties().
virtual bool SetBacklashALT(int32_t steps)
SetBacklashALT Set the altitude axis backlash compensation value in steps.
virtual bool SetBacklashAZ(int32_t steps)
SetBacklashAZ Set the azimuth axis backlash compensation value in steps.
virtual bool AbortMotion()
AbortMotion Abort all axis motion immediately.
virtual bool ReverseAZ(bool enabled)
ReverseAZ Reverse the direction of azimuth axis movement.
INDI::PropertyNumber PositionNP
INDI::PropertyNumber ManualAdjustmentNP
virtual IPState MoveAZ(double degrees)
Move the azimuth axis by the given number of degrees.
bool processSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process PAC switch properties.
bool updateProperties()
updateProperties Define or delete properties based on connection state. Call this from your driver's ...
virtual bool SyncALT(double degrees)
SyncALT Sync the altitude position reference to the given value without moving.
INDI::PropertySwitch ALTReverseSP
virtual bool SyncAZ(double degrees)
SyncAZ Sync the azimuth position reference to the given value without moving.
virtual IPState MoveBoth(double azDegrees, double altDegrees)
MoveBoth Move both axes in a single coordinated operation.
virtual IPState MoveALT(double degrees)
MoveALT Move the altitude axis by the given number of degrees.
INDI::PropertySwitch HomeSP
INDI::PropertySwitch AZReverseSP
INDI::PropertySwitch AbortSP
INDI::PropertyNumber BacklashNP
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
@ IP_WO
Definition indiapi.h:183
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
@ ISR_ATMOST1
Definition indiapi.h:172
const char * IUFindOnSwitchName(ISState *states, char *names[], int n)
Returns the name of the first ON switch it finds in the supplied arguments.
Definition indidevapi.c:355
#define INDI_UNUSED(x)
Definition indidevapi.h:132
*brief user defined string literal for JSON values *sa std::size_t n
#define DEBUGDEVICE(device, priority, msg)
Definition indilogger.h:60
Namespace to encapsulate INDI client, drivers, and mediator classes.
uint8_t cmd[4]
Definition pwc-ioctl.h:2