Instrument Neutral Distributed Interface INDI 2.2.5
mxhd_telescope.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2026 Makoto Kasahara All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file LICENSE. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18#pragma once
19
20#include <atomic>
21#include <cstdint>
22#include <mutex>
23#include <string>
24
25#include <indicom.h>
26#include <inditelescope.h>
27
28class MXHDSerial;
29
31{
32public:
34 ~MXHDTelescope() override = default;
35
36 const char *getDefaultName() override;
37
38 bool initProperties() override;
39 bool updateProperties() override;
40
41 bool Handshake() override;
42
43 bool Connect() override;
44 bool Disconnect() override;
45
46 bool updateLocation(double latitude, double longitude, double elevation) override;
47 bool updateTime(ln_date *utc, double utc_offset) override;
48
49 // Mount control
50 bool Goto(double ra, double dec) override;
51 bool Sync(double ra, double dec) override;
52 bool Abort() override;
53
54 bool Park() override;
55 bool UnPark() override;
56
57 bool SetSlewRate(int index) override;
58 bool SetTrackMode(uint8_t mode) override;
59 bool SetTrackEnabled(bool enabled) override;
60
61 // Manual motion (NSEW buttons)
62 bool MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command) override;
63 bool MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command) override;
64
65 // Timed guide pulses (for PHD2 and other guider clients)
66 IPState GuideNorth(uint32_t ms);
67 IPState GuideSouth(uint32_t ms);
68 IPState GuideEast(uint32_t ms);
69 IPState GuideWest(uint32_t ms);
70
71 // INDI switch handler for tracking rate selection.
72 bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
73 bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
74
75protected:
76 void TimerHit() override;
77 bool ReadScopeStatus() override;
78
79private:
80 // Protocol helpers
81 bool mxhdQueryRaDec(double &raHours, double &decDeg);
82 bool mxhdSetTargetRaDec(double raHours, double decDeg);
83 bool mxhdApplyCachedSiteTime();
84 bool mxhdApplySiteCoordinates(double latitudeDeg, double longitudeDeg);
85 bool mxhdApplyTime(ln_date *utc, double utcOffsetHoursEast);
86 bool mxhdSendUtcOffset(double utcOffsetHoursEast, int timeoutMs = 10000);
87 bool mxhdSendLocalDateTime(ln_date *utc, double utcOffsetHoursEast, int timeoutMs = 10000);
88 void mxhdUpdatePierSide(double raHours);
89
90 bool mxhdSend(const std::string &cmd);
91 bool mxhdQueryHash(const std::string &cmd, std::string &resp);
92 bool mxhdQueryAck(const std::string &cmd, char &ack, int timeoutMs = 2000);
93 bool mxhdReadStatus(uint8_t &b1, uint8_t &b2, uint8_t &b3);
94
95 // Tracking rate commands (MX-HD specific)
96 enum class TrackRateMode
97 {
98 Sidereal = 0,
99 Solar,
100 Lunar
101 };
102
103 bool mxhdSetTrackRate(TrackRateMode mode);
104
105 // Status decode
106 void decodeStatus(uint8_t b1, uint8_t b2, uint8_t b3);
107
108 // Abort state machine (ported conceptually from ASCOM driver)
109 void maybeAdvanceAbortFinalize();
110 void startAbortFinalize(uint32_t delaySeconds);
111
112 // Pulse-guide helpers
113 enum class GuideAxis
114 {
115 RA = 0,
116 DEC
117 };
118
119 IPState startTimedGuidePulse(GuideAxis axis, int direction, uint32_t ms, const char *moveCmd);
120 void completeTimedGuidePulse(GuideAxis axis, uint64_t token, bool sendStop);
121 void setGuidePropertyIdle(GuideAxis axis, IPState state);
122
123private:
124 MXHDSerial *m_io {nullptr};
125 std::mutex m_ioMutex;
126 std::mutex m_siteTimeMutex;
127
128 // Cached status
129 uint8_t m_st1 {0}, m_st2 {0}, m_st3 {0};
130 bool m_isHoming {false};
131 bool m_isSlewing {false};
132 double m_voltageV {0.0};
133 bool m_parkCommandActive {false};
134 bool m_unparkCommandActive {false};
135 bool m_homeCommandActive {false};
136
137 // Tracking state
138 bool m_trackingEnabled {true};
139 TrackRateMode m_trackRateMode {TrackRateMode::Sidereal};
140
141 // Cached site/time information received from INDI clients.
142 bool m_hasCachedSiteCoordinates {false};
143 bool m_hasCachedTime {false};
144 double m_cachedSiteLatitudeDeg {0.0};
145 double m_cachedSiteLongitudeDeg {0.0};
146 ln_date m_cachedUtcDate {};
147 double m_cachedUtcOffsetHoursEast {0.0};
148 bool m_hasPierSideCache {false};
149 bool m_pierSideEast {false};
150
151 // Abort finalization scheduling
152 bool m_abortFinalizeActive {false};
153 time_t m_abortRequestedAt {0};
154 uint32_t m_abortDelaySeconds {0};
155 bool m_abortSawHomeCleared {false};
156 time_t m_abortHomeClearedAt {0};
157
158 // Pulse-guide state
159 std::mutex m_guideMutex;
160 std::atomic<uint64_t> m_guideTokenGen {0};
161 bool m_guideActive {false};
162 GuideAxis m_guideAxis {GuideAxis::RA};
163 int m_guideDirection {0};
164
165 // Properties
166 ISwitch HomeS[1];
168
169 INumber GuideNSN[2];
170 INumberVectorProperty GuideNSNP;
171
172 INumber GuideWEN[2];
173 INumberVectorProperty GuideWENP;
174};
int SetSlewRate(void)
void TimerHit() override
Called when setTimer() time is up.
IPState GuideWest(uint32_t ms)
bool Disconnect() override
Disconnect from device.
bool ReadScopeStatus() override
Read telescope status.
IPState GuideNorth(uint32_t ms)
bool Sync(double ra, double dec) override
Set the telescope current RA and DEC coordinates to the supplied RA and DEC coordinates.
bool updateLocation(double latitude, double longitude, double elevation) override
Update telescope location settings.
bool SetTrackMode(uint8_t mode) override
SetTrackMode Set active tracking mode. Do not change track state.
IPState GuideSouth(uint32_t ms)
bool Abort() override
Abort any telescope motion including tracking if possible.
bool Handshake() override
perform handshake with device to check communication
bool initProperties() override
Called to initialize basic properties required all the time.
bool updateTime(ln_date *utc, double utc_offset) override
Update telescope time, date, and UTC offset.
bool Park() override
Park the telescope to its home position.
const char * getDefaultName() override
bool MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command) override
Start or Stop the telescope motion in the direction dir.
IPState GuideEast(uint32_t ms)
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool SetTrackEnabled(bool enabled) override
SetTrackEnabled Engages or disengages mount tracking. If there are no tracking modes available,...
bool MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command) override
Move the telescope in the direction dir.
bool updateProperties() override
Called when connected state changes, to add/remove properties.
bool Goto(double ra, double dec) override
Move the scope to the supplied RA and DEC coordinates.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
~MXHDTelescope() override=default
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
bool UnPark() override
Unpark the telescope if already parked.
double ra
double dec
ISState
Switch state.
Definition indiapi.h:148
IPState
Property state.
Definition indiapi.h:158
INDI_DIR_WE
INDI_DIR_NS
Implementations for common driver routines.
uint8_t cmd[4]
Definition pwc-ioctl.h:2
One number descriptor.
One switch descriptor.
Number vector property descriptor.
Definition indiapi.h:317
Switch vector property descriptor.
Definition indiapi.h:365