5 #include "build/debug.h"
7 #include "common/axis.h"
8 #include "common/maths.h"
9 #include "common/time.h"
11 #include "config/feature.h"
13 #include "fc/config.h"
14 #include "fc/runtime_config.h"
18 #include "sensors/sensors.h"
19 #include "sensors/diagnostics.h"
20 #include "sensors/gyro.h"
21 #include "sensors/compass.h"
22 #include "sensors/acceleration.h"
23 #include "sensors/barometer.h"
24 #include "sensors/rangefinder.h"
25 #include "sensors/pitotmeter.h"
26 #include "sensors/opflow.h"
28 extern uint8_t requestedSensors
[SENSOR_INDEX_COUNT
];
29 extern uint8_t detectedSensors
[SENSOR_INDEX_COUNT
];
31 hardwareSensorStatus_e
getHwGyroStatus(void)
33 // Gyro is assumed to be always healthy, but it must be present
34 if (detectedSensors
[SENSOR_INDEX_GYRO
] == GYRO_NONE
) {
35 return HW_SENSOR_UNAVAILABLE
;
41 hardwareSensorStatus_e
getHwAccelerometerStatus(void)
43 if (detectedSensors
[SENSOR_INDEX_ACC
] != ACC_NONE
) {
47 return HW_SENSOR_UNHEALTHY
;
50 if (requestedSensors
[SENSOR_INDEX_ACC
] != ACC_NONE
) {
51 // Selected but not detected
52 return HW_SENSOR_UNAVAILABLE
;
55 // Not selected and not detected
56 return HW_SENSOR_NONE
;
61 hardwareSensorStatus_e
getHwCompassStatus(void)
65 if ((ARMING_FLAG(SIMULATOR_MODE_HITL
) || ARMING_FLAG(SIMULATOR_MODE_SITL
)) && sensors(SENSOR_MAG
)) {
66 if (compassIsHealthy()) {
69 return HW_SENSOR_UNHEALTHY
;
73 if (detectedSensors
[SENSOR_INDEX_MAG
] != MAG_NONE
) {
74 if (compassIsHealthy()) {
77 return HW_SENSOR_UNHEALTHY
;
80 if (requestedSensors
[SENSOR_INDEX_MAG
] != MAG_NONE
) {
81 // Selected but not detected
82 return HW_SENSOR_UNAVAILABLE
;
84 // Not selected and not detected
85 return HW_SENSOR_NONE
;
89 return HW_SENSOR_NONE
;
93 hardwareSensorStatus_e
getHwBarometerStatus(void)
97 if (ARMING_FLAG(SIMULATOR_MODE_HITL
) || ARMING_FLAG(SIMULATOR_MODE_SITL
)) {
98 if (requestedSensors
[SENSOR_INDEX_BARO
] == BARO_NONE
) {
99 return HW_SENSOR_NONE
;
100 } else if (baroIsHealthy()) {
103 return HW_SENSOR_UNHEALTHY
;
107 if (detectedSensors
[SENSOR_INDEX_BARO
] != BARO_NONE
) {
108 if (baroIsHealthy()) {
112 return HW_SENSOR_UNHEALTHY
;
116 if (requestedSensors
[SENSOR_INDEX_BARO
] != BARO_NONE
) {
117 // Selected but not detected
118 return HW_SENSOR_UNAVAILABLE
;
121 // Not selected and not detected
122 return HW_SENSOR_NONE
;
126 return HW_SENSOR_NONE
;
130 hardwareSensorStatus_e
getHwRangefinderStatus(void)
132 #if defined(USE_RANGEFINDER)
133 if (detectedSensors
[SENSOR_INDEX_RANGEFINDER
] != RANGEFINDER_NONE
) {
134 if (rangefinderIsHealthy()) {
137 return HW_SENSOR_UNHEALTHY
;
141 if (requestedSensors
[SENSOR_INDEX_RANGEFINDER
] != RANGEFINDER_NONE
) {
142 // Selected but not detected
143 return HW_SENSOR_UNAVAILABLE
;
145 // Not selected and not detected
146 return HW_SENSOR_NONE
;
150 return HW_SENSOR_NONE
;
154 hardwareSensorStatus_e
getHwPitotmeterStatus(void)
156 #if defined(USE_PITOT)
157 if (detectedSensors
[SENSOR_INDEX_PITOT
] != PITOT_NONE
) {
158 if (pitotIsHealthy()) {
161 return HW_SENSOR_UNHEALTHY
;
165 if (requestedSensors
[SENSOR_INDEX_PITOT
] != PITOT_NONE
) {
166 // Selected but not detected
167 return HW_SENSOR_UNAVAILABLE
;
169 // Not selected and not detected
170 return HW_SENSOR_NONE
;
174 return HW_SENSOR_NONE
;
178 hardwareSensorStatus_e
getHwGPSStatus(void)
181 if (sensors(SENSOR_GPS
)) {
182 if (isGPSHealthy()) {
185 return HW_SENSOR_UNHEALTHY
;
189 if (feature(FEATURE_GPS
) && gpsStats
.timeouts
> 4) {
190 // Selected but not detected
191 return HW_SENSOR_UNAVAILABLE
;
193 // Not selected and not detected
194 return HW_SENSOR_NONE
;
198 return HW_SENSOR_NONE
;
202 hardwareSensorStatus_e
getHwOpticalFlowStatus(void)
204 #if defined(USE_OPFLOW)
205 if (detectedSensors
[SENSOR_INDEX_OPFLOW
] != OPFLOW_NONE
) {
206 if (opflowIsHealthy()) {
209 return HW_SENSOR_UNHEALTHY
;
213 if (requestedSensors
[SENSOR_INDEX_OPFLOW
] != OPFLOW_NONE
) {
214 // Selected but not detected
215 return HW_SENSOR_UNAVAILABLE
;
217 // Not selected and not detected
218 return HW_SENSOR_NONE
;
222 return HW_SENSOR_NONE
;
226 bool isHardwareHealthy(void)
228 const hardwareSensorStatus_e gyroStatus
= getHwGyroStatus();
229 const hardwareSensorStatus_e accStatus
= getHwAccelerometerStatus();
230 const hardwareSensorStatus_e baroStatus
= getHwBarometerStatus();
231 const hardwareSensorStatus_e magStatus
= getHwCompassStatus();
232 const hardwareSensorStatus_e rangefinderStatus
= getHwRangefinderStatus();
233 const hardwareSensorStatus_e pitotStatus
= getHwPitotmeterStatus();
234 const hardwareSensorStatus_e gpsStatus
= getHwGPSStatus();
235 const hardwareSensorStatus_e opflowStatus
= getHwOpticalFlowStatus();
237 // Sensor is considered failing if it's either unavailable (selected but not detected) or unhealthy (returning invalid readings)
238 if (gyroStatus
== HW_SENSOR_UNAVAILABLE
|| gyroStatus
== HW_SENSOR_UNHEALTHY
)
241 if (accStatus
== HW_SENSOR_UNAVAILABLE
|| accStatus
== HW_SENSOR_UNHEALTHY
)
244 if (baroStatus
== HW_SENSOR_UNAVAILABLE
|| baroStatus
== HW_SENSOR_UNHEALTHY
)
247 if (magStatus
== HW_SENSOR_UNAVAILABLE
|| magStatus
== HW_SENSOR_UNHEALTHY
)
250 if (rangefinderStatus
== HW_SENSOR_UNAVAILABLE
|| rangefinderStatus
== HW_SENSOR_UNHEALTHY
)
253 if (pitotStatus
== HW_SENSOR_UNAVAILABLE
|| pitotStatus
== HW_SENSOR_UNHEALTHY
)
256 if (gpsStatus
== HW_SENSOR_UNAVAILABLE
|| gpsStatus
== HW_SENSOR_UNHEALTHY
)
259 if (opflowStatus
== HW_SENSOR_UNAVAILABLE
|| opflowStatus
== HW_SENSOR_UNHEALTHY
)