TR-mbed 1.0
Loading...
Searching...
No Matches
ISM330.h
Go to the documentation of this file.
1
12#pragma once
13
14#ifndef ISM330_H_
15#define ISM330_H_
16#include "mbed.h"
18#include <cstdint>
19
24class ISM330 : public IMU {
25public:
26 // Structs
27 typedef struct {
28 float x;
29 float y;
30 float z;
32
33 typedef struct {
34 int16_t ax;
35 int16_t ay;
36 int16_t az;
37 int16_t gx;
38 int16_t gy;
39 int16_t gz;
41
42 typedef struct {
43 float ax;
44 float ay;
45 float az;
46 float gx;
47 float gy;
48 float gz;
50
51
52 ISM330(I2C &i2c, uint8_t address) noexcept;
53
63 bool begin(float prop_gain=0.1f, float int_gain=0.0f) noexcept;
64
73 uint8_t whoAmI() noexcept;
74
80 void reset() noexcept;
81
82 void calibrate() noexcept;
83
84 IMU::EulerAngles getImuAngles() override;
85
86 IMU::EulerAngles read() override { return getImuAngles(); }
87
88 //Full read functions
89 //Reads Accel and Gyro sequentially, reduces I2C transactions
90 ISM330_RAW_DATA_TypeDef readAGraw() noexcept; //used during calibration
91
92 ISM330_DATA_TypeDef readAG() noexcept;
93
94
95 // Temperature Reading
96 float readTemperature() noexcept; // Returns temp in C-25, so at 25C it should read 0
97 // If you're wondering why, it's just how the IMU is calibrated
98
99 float accelTempScale(float temp); // Returns the scaling of the accelerometer with respect to Temperature; At 25C = 0;
100
101 float accelTempOffset(float temp); // Returns the offset of the accelerometer with respect to Temperature; At 25C = 0; (Just adding a constant)
102
103 float gyroTempScale(float temp); // Returns the scaling of the gyro with respect to Temperature; At 25C = 0;
104
105 float gyroTempOffset(float temp); // Returns the offset of the gyro with respect to Temperature; At 25C = 0; (Just adding a constant)
106
107 // Update Structs
108 void getAGVectors(ISM330_VECTOR_TypeDef& accel, ISM330_VECTOR_TypeDef& gyro);
109
110 // IMU Sensor Fusion
111 void computeAnglesMah(IMU::EulerAngles& angles); // Mahony Compute Euler Angles
112
113 void computeAnglesMadgwick(IMU::EulerAngles& angles);
114
115 // Mahony Sensor Fusion
116 //Adafruit_Mahony(float prop_gain, float int_gain);
117 void mahonyStart(float prop_gain, float int_gain);
118
120
121 void mahonyUpdate(float mx, float my, float mz, float dt);
122
123 void mahonyUpdateIMU(float dt);
124
125 // Madgwick Sensor Fusion
126
127 void madgwickStart(float gain);
128
129 void madgwickUpdate(float mx, float my, float mz, float dt);
130
131 void madgwickUpdateIMU(float dt);
132
133 // Reading individual sensor values
134 ISM330_VECTOR_TypeDef readAccel() noexcept;
135
136 ISM330_VECTOR_TypeDef readGyro() noexcept;
137
138private:
139 I2C &i2c;
140 uint8_t _address;
141 uint8_t whoAmIReading;
142
143 // Raw offsets for accel and gyro
144 float axBias;
145 float ayBias;
146 float azBias;
147 float wxBias;
148 float wyBias;
149 float wzBias;
150
151 //Raw I2C readings to actual measurements
152 uint8_t agReadings[12]; //Accel and Gyro Readings buffer
153 uint8_t xReadings[6]; //Accel Readings buffer
154 uint8_t gReadings[6]; //Gyro Readings buffer
155 float temperature; //Temperature in C-25, so at 25C it should read 0
156
157 ISM330_VECTOR_TypeDef readingToAccel(const uint8_t *readings, float temp);
158
159 ISM330_VECTOR_TypeDef readingToGyro(const uint8_t *readings, float temp);
160
161 float invSqrt(float x); //Inverse square root, used for normalizing vectors
162
163 float beta; // algorithm gain
164 float q0;
165 float q1;
166 float q2;
167 float q3; // quaternion of sensor frame relative to auxiliary frame
168 float invSampleFreq;
169 float grav[3]; // gravity vector
170
171 //Mahony Quaternions
172 float mahq0;
173 float mahq1;
174 float mahq2;
175 float mahq3;
176
177 float twoKp; // 2 * proportional gain (Kp)
178 float twoKi; // 2 * integral gain (Ki)
179 float integralFBx, integralFBy, integralFBz; // integral error terms scaled by Ki
180
181 bool anglesComputed = false; // Flag for whether or not angles have been computed
182};
183
184#endif // ISM330_H_
Definition IMU.h:7
Interface class for ISM330DHCX IMU using SPI.
Definition ISM330.h:24
float gyroTempScale(float temp)
Definition ISM330.cpp:253
void getAGVectors(ISM330_VECTOR_TypeDef &accel, ISM330_VECTOR_TypeDef &gyro)
Definition ISM330.cpp:228
ISM330_RAW_DATA_TypeDef readAGraw() noexcept
Definition ISM330.cpp:190
void madgwickUpdate(float mx, float my, float mz, float dt)
Definition ISM330.cpp:566
float accelTempScale(float temp)
Definition ISM330.cpp:244
ISM330_VECTOR_TypeDef readGyro() noexcept
Definition ISM330.cpp:179
IMU::EulerAngles read() override
Definition ISM330.h:86
ISM330_VECTOR_TypeDef readAccel() noexcept
Definition ISM330.cpp:170
void initQuaternionFromAccel()
Definition ISM330.cpp:304
bool begin(float prop_gain=0.1f, float int_gain=0.0f) noexcept
Initialize communication with the device.
Definition ISM330.cpp:52
uint8_t whoAmI() noexcept
Read the WHO_AM_I register value.
Definition ISM330.cpp:113
void madgwickStart(float gain)
Definition ISM330.cpp:554
IMU::EulerAngles getImuAngles() override
Definition ISM330.cpp:282
float accelTempOffset(float temp)
Definition ISM330.cpp:249
void computeAnglesMadgwick(IMU::EulerAngles &angles)
Definition ISM330.cpp:811
void reset() noexcept
Issue a software reset to the device.
Definition ISM330.cpp:122
ISM330_DATA_TypeDef readAG() noexcept
Definition ISM330.cpp:208
void mahonyUpdate(float mx, float my, float mz, float dt)
Definition ISM330.cpp:351
void mahonyStart(float prop_gain, float int_gain)
Definition ISM330.cpp:290
void calibrate() noexcept
Definition ISM330.cpp:74
void computeAnglesMah(IMU::EulerAngles &angles)
Definition ISM330.cpp:543
float readTemperature() noexcept
Definition ISM330.cpp:239
void mahonyUpdateIMU(float dt)
Definition ISM330.cpp:467
void madgwickUpdateIMU(float dt)
Definition ISM330.cpp:704
float gyroTempOffset(float temp)
Definition ISM330.cpp:257
Definition IMU.h:10
Definition ISM330.h:42
float gz
Definition ISM330.h:48
float gx
Definition ISM330.h:46
float ax
Definition ISM330.h:43
float gy
Definition ISM330.h:47
float az
Definition ISM330.h:45
float ay
Definition ISM330.h:44
Definition ISM330.h:33
int16_t ax
Definition ISM330.h:34
int16_t ay
Definition ISM330.h:35
int16_t gy
Definition ISM330.h:38
int16_t gz
Definition ISM330.h:39
int16_t az
Definition ISM330.h:36
int16_t gx
Definition ISM330.h:37
Definition ISM330.h:27
float x
Definition ISM330.h:28
float z
Definition ISM330.h:30
float y
Definition ISM330.h:29