Instrument Neutral Distributed Interface INDI 2.2.5
indiimu.h
Go to the documentation of this file.
1/*
2 IMU Driver Base Class
3 Copyright (C) 2025 Jasem Mutlaq ([email protected])
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19*/
20
21#pragma once
22
23#include "defaultdevice.h"
24#include "indiimuinterface.h"
25#include "imusensorfusion.h"
26
27#include <chrono>
28#include <memory>
29
30namespace Connection
31{
32class Serial;
33class I2C;
34}
35
36namespace INDI
37{
38
39class IMU : public DefaultDevice, public IMUInterface
40{
41 public:
42 IMU();
43 virtual ~IMU();
44
48 enum
49 {
50 CONNECTION_NONE = 1 << 0,
52 CONNECTION_I2C = 1 << 2
54
60 void setSupportedConnections(const uint8_t &value);
61
67 {
68 return imuConnection;
69 }
70
71 virtual const char *getDefaultName() override
72 {
73 return "IMU";
74 }
75
76 virtual bool initProperties() override;
77 virtual bool updateProperties() override;
78
86 virtual bool Handshake();
87
88 protected:
89 // Helper function to convert quaternion to Euler angles (roll, pitch, yaw) in radians
90 void QuaternionToEuler(double i, double j, double k, double w, double &roll, double &pitch, double &yaw);
91
92 // Helper function to convert Euler angles (roll, pitch, yaw) in radians to a quaternion
93 void EulerToQuaternion(double roll, double pitch, double yaw, double &i, double &j, double &k, double &w);
94
95 virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
96 virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
97 virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
98 virtual bool saveConfigItems(FILE *fp) override;
99
100 // Called by the concrete class
101 bool SetOrientationData(double roll, double pitch, double yaw, double w = 0.0) override;
102 bool SetAccelerationData(double x, double y, double z) override;
103 bool SetGyroscopeData(double x, double y, double z) override;
104 bool SetMagnetometerData(double x, double y, double z) override;
105
106 protected:
107 // Virtual functions from IMUInterface to be implemented by concrete drivers
108 virtual bool StartCalibration() override;
109 virtual bool SaveCalibrationData() override;
110 virtual bool LoadCalibrationData() override;
111 virtual bool ResetCalibration() override;
112 virtual bool SetPowerMode(const std::string &mode) override;
113 virtual bool SetOperationMode(const std::string &mode) override;
114 virtual bool SetDistanceUnits(bool metric) override;
115 virtual bool SetAngularUnits(bool degrees) override;
116 virtual bool SetUpdateRate(double rate) override;
117
118 // Additional properties for astronomical coordinates and mount alignment at the driver level
119 enum
120 {
124 };
125
126 enum
127 {
128 AXIS1 = 0,
129 AXIS2
130 };
131
132 enum
133 {
136 };
137
139 {
140 ENU = 0,
142 SWU
143 };
144
151
153 INDI::PropertySwitch AstroCoordsTypeSP {2}; // Equatorial/Alt-Az
155
156 // New properties for IMU orientation adjustment
167
168 // New properties for Sync Axis
170
171 // New properties for Telescope pointing vector
179
180 // Geographic Location
182 // Magnetic Declination
184
185 // ── Sensor Fusion properties ──────────────────────────────────────────
186 //
187 // These are shown in the IMU tab when the driver does NOT provide its own
188 // hardware orientation output (i.e., when !HasOrientation()).
189 // Drivers that perform on-chip fusion (e.g. BNO08x) call SetOrientationData()
190 // directly and do not need these properties.
191
197
203
209
216
219
220 int PortFD = -1;
221
222 private:
223 bool callHandshake();
224 uint8_t imuConnection = CONNECTION_SERIAL | CONNECTION_I2C;
225
226 // Last known quaternion values for recalculation
227 double last_q_i = 0.0;
228 double last_q_j = 0.0;
229 double last_q_k = 0.0;
230 double last_q_w = 1.0; // Default to identity quaternion
231
232 // Last known raw quaternion values from the sensor
233 double last_raw_q_i = 0.0;
234 double last_raw_q_j = 0.0;
235 double last_raw_q_k = 0.0;
236 double last_raw_q_w = 1.0; // Default to identity quaternion
237
238 // ── Software sensor fusion ────────────────────────────────────────────
239 std::unique_ptr<IMUSensorFusion> m_SensorFusion;
240
242 struct RawSensorData
243 {
244 double x {0.0}, y {0.0}, z {0.0};
245 bool valid {false};
246 };
247 RawSensorData m_RawAccel, m_RawGyro, m_RawMag;
248
250 std::chrono::steady_clock::time_point m_LastFusionTime;
251 bool m_FusionTimerInitialized {false};
252
254 bool isFusionEnabled() const;
255
257 void recreateFusionFilter();
258
259 protected:
260 // Function to recalculate astronomical coordinates using last known quaternion
262};
263
264}
The I2C class manages connection with I2C devices on Linux systems. I2C communication is commonly use...
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual bool SetOperationMode(const std::string &mode) override
SetOperationMode Set the operation mode of the IMU sensor.
Definition indiimu.cpp:1349
INDI::PropertySwitch IMUFrameSP
Definition indiimu.h:154
virtual const char * getDefaultName() override
Definition indiimu.h:71
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition indiimu.cpp:518
INDI::PropertySwitch AstroCoordsTypeSP
Definition indiimu.h:153
bool SetAccelerationData(double x, double y, double z) override
SetAccelerationData Set the linear acceleration data.
Definition indiimu.cpp:1207
INDI::PropertyNumber MagneticDeclinationNP
Definition indiimu.h:183
INDI::PropertyNumber FusionParamsNP
Algorithm tuning parameters (β for Madgwick; Kp & Ki for Mahony)
Definition indiimu.h:215
virtual bool SetUpdateRate(double rate) override
SetUpdateRate Set the sensor polling frequency.
Definition indiimu.cpp:1364
void RecalculateAstroCoordinates()
Definition indiimu.cpp:668
uint8_t getSupportedConnections() const
Get the currently configured supported connection modes.
Definition indiimu.h:66
Connection::Serial * serialConnection
Definition indiimu.h:217
INDI::PropertySwitch FusionTypeSP
Select fusion algorithm: Madgwick or Mahony.
Definition indiimu.h:213
OrientationAdjustments
Definition indiimu.h:158
@ PITCH_MULTIPLIER
Definition indiimu.h:160
@ YAW_MULTIPLIER
Definition indiimu.h:161
@ YAW_OFFSET
Definition indiimu.h:164
@ ROLL_OFFSET
Definition indiimu.h:162
@ PITCH_OFFSET
Definition indiimu.h:163
@ ROLL_MULTIPLIER
Definition indiimu.h:159
int PortFD
Definition indiimu.h:220
@ CONNECTION_I2C
Definition indiimu.h:52
@ CONNECTION_SERIAL
Definition indiimu.h:51
@ CONNECTION_NONE
Definition indiimu.h:50
@ LOCATION_LATITUDE
Definition indiimu.h:147
@ LOCATION_ELEVATION
Definition indiimu.h:149
@ LOCATION_LONGITUDE
Definition indiimu.h:148
virtual ~IMU()
Definition indiimu.cpp:41
bool SetGyroscopeData(double x, double y, double z) override
SetGyroscopeData Set the angular velocity data.
Definition indiimu.cpp:1220
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition indiimu.cpp:587
SensorFusionSwitch
Definition indiimu.h:193
@ FUSION_DISABLE
Definition indiimu.h:195
@ FUSION_ENABLE
Definition indiimu.h:194
void EulerToQuaternion(double roll, double pitch, double yaw, double &i, double &j, double &k, double &w)
Definition indiimu.cpp:653
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition indiimu.cpp:241
Connection::I2C * i2cConnection
Definition indiimu.h:218
virtual bool LoadCalibrationData() override
LoadCalibrationData Load previously saved calibration data.
Definition indiimu.cpp:1336
@ AXIS2_OFFSET
Definition indiimu.h:122
@ AXIS1_OFFSET
Definition indiimu.h:121
@ ROTATION_OFFSET
Definition indiimu.h:123
virtual bool SaveCalibrationData() override
SaveCalibrationData Save the current calibration data.
Definition indiimu.cpp:1332
virtual bool StartCalibration() override
StartCalibration Initiate the calibration sequence.
Definition indiimu.cpp:1328
bool SetMagnetometerData(double x, double y, double z) override
SetMagnetometerData Set the magnetic field strength data.
Definition indiimu.cpp:1282
FusionTypeSwitch
Definition indiimu.h:199
@ FUSION_MADGWICK
Definition indiimu.h:200
@ FUSION_MAHONY
Definition indiimu.h:201
void QuaternionToEuler(double i, double j, double k, double w, double &roll, double &pitch, double &yaw)
Definition indiimu.cpp:632
enum INDI::IMU::@450 IMUConnection
INDI::PropertyNumber GeographicCoordNP
Definition indiimu.h:181
virtual bool Handshake()
Performs a device-specific handshake after a connection is established. This method should be overrid...
Definition indiimu.cpp:610
TelescopeVector
Definition indiimu.h:173
@ TELESCOPE_VECTOR_X
Definition indiimu.h:174
@ TELESCOPE_VECTOR_Z
Definition indiimu.h:176
@ TELESCOPE_VECTOR_Y
Definition indiimu.h:175
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition indiimu.cpp:187
@ COORD_ALTAZ
Definition indiimu.h:135
@ COORD_EQUATORIAL
Definition indiimu.h:134
virtual bool SetDistanceUnits(bool metric) override
SetUnits Set the units for sensor data.
Definition indiimu.cpp:1354
INDI::PropertyNumber TelescopeVectorNP
Definition indiimu.h:178
virtual bool SetPowerMode(const std::string &mode) override
SetPowerMode Set the power mode of the IMU sensor.
Definition indiimu.cpp:1344
INDI::PropertyNumber AstroCoordinatesNP
Definition indiimu.h:152
void setSupportedConnections(const uint8_t &value)
Definition indiimu.cpp:47
INDI::PropertyNumber SyncAxisNP
Definition indiimu.h:169
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition indiimu.cpp:60
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
Definition indiimu.cpp:579
INDI::PropertySwitch SensorFusionSP
Enable / Disable software sensor fusion.
Definition indiimu.h:211
INDI::PropertyNumber OrientationAdjustmentsNP
Definition indiimu.h:166
virtual bool ResetCalibration() override
ResetCalibration Reset calibration data.
Definition indiimu.cpp:1340
virtual bool SetAngularUnits(bool degrees) override
SetAngularUnits Set the angular units for sensor data.
Definition indiimu.cpp:1359
bool SetOrientationData(double roll, double pitch, double yaw, double w=0.0) override
SetOrientationData Set the orientation data (Roll, Pitch, Yaw, Quaternion W)
Definition indiimu.cpp:1025
@ FUSION_PARAM_1
β for Madgwick; Kp for Mahony
Definition indiimu.h:206
@ FUSION_PARAM_2
unused for Madgwick; Ki for Mahony
Definition indiimu.h:207
ISState
Switch state.
Definition indiapi.h:148
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
Definition arduino_st4.h:34
Namespace to encapsulate INDI client, drivers, and mediator classes.