forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.c
256 lines (231 loc) · 6.45 KB
/
diagnostics.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
#include "build/debug.h"
#include "common/axis.h"
#include "common/maths.h"
#include "common/time.h"
#include "config/feature.h"
#include "fc/config.h"
#include "fc/runtime_config.h"
#include "io/gps.h"
#include "sensors/sensors.h"
#include "sensors/diagnostics.h"
#include "sensors/gyro.h"
#include "sensors/compass.h"
#include "sensors/acceleration.h"
#include "sensors/barometer.h"
#include "sensors/rangefinder.h"
#include "sensors/pitotmeter.h"
#include "sensors/opflow.h"
extern uint8_t requestedSensors[SENSOR_INDEX_COUNT];
extern uint8_t detectedSensors[SENSOR_INDEX_COUNT];
hardwareSensorStatus_e getHwGyroStatus(void)
{
// Gyro is assumed to be always healthy
return HW_SENSOR_OK;
}
hardwareSensorStatus_e getHwAccelerometerStatus(void)
{
#if defined(USE_ACC)
if (detectedSensors[SENSOR_INDEX_ACC] != ACC_NONE) {
if (accIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_ACC] != ACC_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwCompassStatus(void)
{
#if defined(USE_MAG)
if (detectedSensors[SENSOR_INDEX_MAG] != MAG_NONE) {
if (compassIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_MAG] != MAG_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwBarometerStatus(void)
{
#if defined(USE_BARO)
if (detectedSensors[SENSOR_INDEX_BARO] != BARO_NONE) {
if (baroIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_BARO] != BARO_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwRangefinderStatus(void)
{
#if defined(USE_RANGEFINDER)
if (detectedSensors[SENSOR_INDEX_RANGEFINDER] != RANGEFINDER_NONE) {
if (rangefinderIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_RANGEFINDER] != RANGEFINDER_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwPitotmeterStatus(void)
{
#if defined(USE_PITOT)
if (detectedSensors[SENSOR_INDEX_PITOT] != PITOT_NONE) {
if (pitotIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_PITOT] != PITOT_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwGPSStatus(void)
{
#if defined(USE_GPS)
if (sensors(SENSOR_GPS)) {
if (isGPSHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (feature(FEATURE_GPS) && gpsStats.timeouts > 3) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
hardwareSensorStatus_e getHwOpticalFlowStatus(void)
{
#if defined(USE_OPTICAL_FLOW)
if (detectedSensors[SENSOR_INDEX_OPFLOW] != OPFLOW_NONE) {
if (opflowIsHealthy()) {
return HW_SENSOR_OK;
}
else {
return HW_SENSOR_UNHEALTHY;
}
}
else {
if (requestedSensors[SENSOR_INDEX_OPFLOW] != OPFLOW_NONE) {
// Selected but not detected
return HW_SENSOR_UNAVAILABLE;
}
else {
// Not selected and not detected
return HW_SENSOR_NONE;
}
}
#else
return HW_SENSOR_NONE;
#endif
}
bool isHardwareHealthy(void)
{
const hardwareSensorStatus_e gyroStatus = getHwGyroStatus();
const hardwareSensorStatus_e accStatus = getHwAccelerometerStatus();
const hardwareSensorStatus_e baroStatus = getHwBarometerStatus();
const hardwareSensorStatus_e magStatus = getHwCompassStatus();
const hardwareSensorStatus_e rangefinderStatus = getHwRangefinderStatus();
const hardwareSensorStatus_e pitotStatus = getHwPitotmeterStatus();
const hardwareSensorStatus_e gpsStatus = getHwGPSStatus();
const hardwareSensorStatus_e opflowStatus = getHwOpticalFlowStatus();
// Sensor is considered failing if it's either unavailable (selected but not detected) or unhealthy (returning invalid readings)
if (gyroStatus == HW_SENSOR_UNAVAILABLE || gyroStatus == HW_SENSOR_UNHEALTHY)
return false;
if (accStatus == HW_SENSOR_UNAVAILABLE || accStatus == HW_SENSOR_UNHEALTHY)
return false;
if (baroStatus == HW_SENSOR_UNAVAILABLE || baroStatus == HW_SENSOR_UNHEALTHY)
return false;
if (magStatus == HW_SENSOR_UNAVAILABLE || magStatus == HW_SENSOR_UNHEALTHY)
return false;
if (rangefinderStatus == HW_SENSOR_UNAVAILABLE || rangefinderStatus == HW_SENSOR_UNHEALTHY)
return false;
if (pitotStatus == HW_SENSOR_UNAVAILABLE || pitotStatus == HW_SENSOR_UNHEALTHY)
return false;
if (gpsStatus == HW_SENSOR_UNAVAILABLE || gpsStatus == HW_SENSOR_UNHEALTHY)
return false;
if (opflowStatus == HW_SENSOR_UNAVAILABLE || opflowStatus == HW_SENSOR_UNHEALTHY)
return false;
return true;
}