-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimu_handler.cpp
More file actions
232 lines (192 loc) · 4.36 KB
/
Copy pathimu_handler.cpp
File metadata and controls
232 lines (192 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <SPI.h>
#include "Wire.h"
#include <cmath>
#include "imu.h"
Imu::Imu()
: imu(), status(SensorStatus::UNINITIALIZED), consecutiveSuccesses(0), consecutiveFailures(0)
{
};
bool Imu::begin()
{
bool ok = imu.begin(LSM6DSO_IMU_ADDRESS, Wire);
status = ok ? SensorStatus::NOMINAL : SensorStatus::FAILED;
return ok;
};
SensorStatus Imu::checkHealth()
{
Wire.beginTransmission(LSM6DSO_IMU_ADDRESS);
bool ack = (Wire.endTransmission() == 0);
if(ack)
{
consecutiveFailures = 0;
consecutiveSuccesses++;
if (consecutiveSuccesses >= FULL_RECOVERY_THRESHOLD)
status = SensorStatus::NOMINAL;
else if (consecutiveSuccesses >= RECOVERY_THRESHOLD)
status = SensorStatus::DEGRADED;
}
else
{
consecutiveSuccesses = 0;
consecutiveFailures++;
if (status != SensorStatus::FAILED && consecutiveFailures >= DEGRADE_THRESHOLD)
status = SensorStatus::DEGRADED;
if (consecutiveFailures >= FAILURE_THRESHOLD)
status = SensorStatus::FAILED;
}
return status;
};
SensorStatus Imu::getStatus() const
{
return status;
}
std::int16_t Imu::getRawAccelX()
{
return imu.readRawAccelX();
}
std::int16_t Imu::getRawAccelY()
{
return imu.readRawAccelY();
}
std::int16_t Imu::getRawAccelZ()
{
return imu.readRawAccelZ();
}
std::int16_t Imu::getRawGyroX()
{
return imu.readRawGyroX();
}
std::int16_t Imu::getRawGyroY()
{
return imu.readRawGyroY();
}
std::int16_t Imu::getRawGyroZ()
{
return imu.readRawGyroZ();
}
std::int16_t Imu::getRawTemperature()
{
return imu.readRawTemp();
}
Imu::RawSample Imu::getRawSample()
{
return {
getRawAccelX(),
getRawAccelY(),
getRawAccelZ(),
getRawGyroX(),
getRawGyroY(),
getRawGyroZ(),
getRawTemperature()
};
}
float Imu::getAccelXG()
{
return imu.readFloatAccelX();
}
float Imu::getAccelYG()
{
return imu.readFloatAccelY();
}
float Imu::getAccelZG()
{
return imu.readFloatAccelZ();
}
Imu::Vector3 Imu::getAccelG()
{
return {
getAccelXG(),
getAccelYG(),
getAccelZG()
};
}
float Imu::getAccelMagnitudeG()
{
Vector3 accel = getAccelG();
return std::sqrt((accel.x * accel.x) + (accel.y * accel.y) + (accel.z * accel.z));
}
float Imu::getGyroXDps()
{
return imu.readFloatGyroX();
}
float Imu::getGyroYDps()
{
return imu.readFloatGyroY();
}
float Imu::getGyroZDps()
{
return imu.readFloatGyroZ();
}
Imu::Vector3 Imu::getGyroDps()
{
return {
getGyroXDps(),
getGyroYDps(),
getGyroZDps()
};
}
float Imu::getGyroMagnitudeDps()
{
Vector3 gyro = getGyroDps();
return std::sqrt((gyro.x * gyro.x) + (gyro.y * gyro.y) + (gyro.z * gyro.z));
}
float Imu::getTemperatureC()
{
return imu.readTempC();
}
float Imu::getTemperatureF()
{
return imu.readTempF();
}
Imu::MotionSample Imu::getMotionSample()
{
Vector3 accel = getAccelG();
Vector3 gyro = getGyroDps();
return {
accel,
gyro,
getTemperatureC(),
std::sqrt((accel.x * accel.x) + (accel.y * accel.y) + (accel.z * accel.z)),
std::sqrt((gyro.x * gyro.x) + (gyro.y * gyro.y) + (gyro.z * gyro.z))
};
}
std::uint8_t Imu::getDataReadyFlags()
{
return imu.listenDataReady();
}
bool Imu::isAccelDataReady()
{
return (getDataReadyFlags() & ACCEL_DATA_READY) != 0;
}
bool Imu::isGyroDataReady()
{
return (getDataReadyFlags() & GYRO_DATA_READY) != 0;
}
bool Imu::isTemperatureDataReady()
{
return (getDataReadyFlags() & TEMP_DATA_READY) != 0;
}
bool Imu::configureForFlight(std::uint8_t accelRangeG, std::uint16_t gyroRangeDps, std::uint16_t sampleRateHz)
{
bool ok = true;
ok = imu.setAccelRange(accelRangeG) && ok;
ok = imu.setGyroRange(gyroRangeDps) && ok;
ok = imu.setAccelDataRate(sampleRateHz) && ok;
ok = imu.setGyroDataRate(sampleRateHz) && ok;
ok = imu.setBlockDataUpdate(true) && ok;
ok = imu.setHighPerfAccel(true) && ok;
ok = imu.setHighPerfGyro(true) && ok;
if (!ok)
status = SensorStatus::DEGRADED;
return ok;
}
bool Imu::isAccelNearLimit(float marginG)
{
float limit = imu.getAccelRange();
return getAccelMagnitudeG() >= (limit - marginG);
}
bool Imu::isGyroNearLimit(float marginDps)
{
float limit = imu.getGyroRange();
return getGyroMagnitudeDps() >= (limit - marginDps);
}