2 ******************************************************************************
3 * @addtogroup OpenPilot System OpenPilot System
5 * @addtogroup OpenPilot Libraries OpenPilot System Libraries
8 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
9 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
10 * @brief Utilities to validate a flight configuration
11 * @see The GNU Public License (GPL) Version 3
13 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <openpilot.h>
31 #include <pios_board_info.h>
34 #include "inc/sanitycheck.h"
37 #include <manualcontrolsettings.h>
38 #include <flightmodesettings.h>
39 #include <systemsettings.h>
40 #include <stabilizationsettings.h>
41 #include <systemalarms.h>
42 #include <revosettings.h>
43 #include <positionstate.h>
45 // a number of useful macros
46 #define ADDSEVERITY(check) severity = (severity != SYSTEMALARMS_ALARM_OK ? severity : ((check) ? SYSTEMALARMS_ALARM_OK : SYSTEMALARMS_ALARM_CRITICAL))
47 #define ADDEXTENDEDALARMSTATUS(error_code, error_substatus) if ((severity != SYSTEMALARMS_ALARM_OK) && (alarmstatus == SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE)) { alarmstatus = (error_code); alarmsubstatus = (error_substatus); }
50 typedef struct SANITYCHECK_CustomHookInstance
{
51 SANITYCHECK_CustomHook_function
*hook
;
52 struct SANITYCHECK_CustomHookInstance
*next
;
54 } SANITYCHECK_CustomHookInstance
;
56 // ! Check a stabilization mode switch position for safety
57 static bool check_stabilization_settings(int index
, bool multirotor
, bool coptercontrol
, bool gpsassisted
);
59 SANITYCHECK_CustomHookInstance
*hooks
= 0;
62 * Run a preflight check over the hardware configuration
63 * and currently active modules
65 int32_t configuration_check()
67 int32_t severity
= SYSTEMALARMS_ALARM_OK
;
68 SystemAlarmsExtendedAlarmStatusOptions alarmstatus
= SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE
;
69 uint8_t alarmsubstatus
= 0;
71 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
72 bool coptercontrol
= bdinfo
->board_type
== 0x04;
74 // Classify navigation capability
76 RevoSettingsFusionAlgorithmOptions revoFusion
;
77 RevoSettingsFusionAlgorithmGet(&revoFusion
);
78 bool navCapableFusion
;
80 case REVOSETTINGS_FUSIONALGORITHM_COMPLEMENTARYMAGGPSOUTDOOR
:
81 case REVOSETTINGS_FUSIONALGORITHM_GPSNAVIGATIONINS13
:
82 case REVOSETTINGS_FUSIONALGORITHM_GPSNAVIGATIONINS13CF
:
83 navCapableFusion
= true;
86 navCapableFusion
= false;
87 // check for hitl. hitl allows to feed position and velocity state via
88 // telemetry, this makes nav possible even with an unsuited algorithm
89 if (PositionStateHandle()) {
90 if (PositionStateReadOnly()) {
91 navCapableFusion
= true;
95 #else /* ifdef REVOLUTION */
96 const bool navCapableFusion
= false;
97 #endif /* ifdef REVOLUTION */
100 // Classify airframe type
101 bool multirotor
= (GetCurrentFrameType() == FRAME_TYPE_MULTIROTOR
);
104 // For each available flight mode position sanity check the available
107 FlightModeSettingsFlightModePositionOptions modes
[FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_NUMELEM
];
108 StabilizationSettingsFlightModeAssistMapOptions FlightModeAssistMap
[STABILIZATIONSETTINGS_FLIGHTMODEASSISTMAP_NUMELEM
];
109 ManualControlSettingsFlightModeNumberGet(&num_modes
);
110 StabilizationSettingsFlightModeAssistMapGet(FlightModeAssistMap
);
111 FlightModeSettingsFlightModePositionGet(modes
);
113 for (uint32_t i
= 0; i
< num_modes
; i
++) {
114 uint8_t gps_assisted
= FlightModeAssistMap
[i
];
116 ADDSEVERITY(!coptercontrol
);
117 ADDSEVERITY(multirotor
);
118 ADDSEVERITY(navCapableFusion
);
121 switch ((FlightModeSettingsFlightModePositionOptions
)modes
[i
]) {
122 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_MANUAL
:
123 ADDSEVERITY(!gps_assisted
);
124 ADDSEVERITY(!multirotor
);
126 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED1
:
127 ADDSEVERITY(check_stabilization_settings(1, multirotor
, coptercontrol
, gps_assisted
));
129 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED2
:
130 ADDSEVERITY(check_stabilization_settings(2, multirotor
, coptercontrol
, gps_assisted
));
132 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED3
:
133 ADDSEVERITY(check_stabilization_settings(3, multirotor
, coptercontrol
, gps_assisted
));
135 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED4
:
136 ADDSEVERITY(check_stabilization_settings(4, multirotor
, coptercontrol
, gps_assisted
));
138 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED5
:
139 ADDSEVERITY(check_stabilization_settings(5, multirotor
, coptercontrol
, gps_assisted
));
141 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_STABILIZED6
:
142 ADDSEVERITY(check_stabilization_settings(6, multirotor
, coptercontrol
, gps_assisted
));
144 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_PATHPLANNER
:
146 ADDSEVERITY(!gps_assisted
);
148 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_POSITIONHOLD
:
149 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_VELOCITYROAM
:
150 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_LAND
:
151 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_AUTOTAKEOFF
:
152 ADDSEVERITY(!coptercontrol
);
153 ADDSEVERITY(navCapableFusion
);
156 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_COURSELOCK
:
157 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_HOMELEASH
:
158 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_ABSOLUTEPOSITION
:
159 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_POI
:
160 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_RETURNTOBASE
:
161 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_AUTOCRUISE
:
162 ADDSEVERITY(!gps_assisted
);
163 ADDSEVERITY(!coptercontrol
);
164 ADDSEVERITY(navCapableFusion
);
166 #if !defined(PIOS_EXCLUDE_ADVANCED_FEATURES)
167 case FLIGHTMODESETTINGS_FLIGHTMODEPOSITION_AUTOTUNE
:
168 ADDSEVERITY(!gps_assisted
);
169 // it would be fun to try autotune on a fixed wing
170 // but that should only be attempted by devs at first
171 ADDSEVERITY(multirotor
);
173 #endif /* !defined(PIOS_EXCLUDE_ADVANCED_FEATURES) */
175 // Uncovered modes are automatically an error
178 // mark the first encountered erroneous setting in status and substatus
179 if ((severity
!= SYSTEMALARMS_ALARM_OK
) && (alarmstatus
== SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE
)) {
180 alarmstatus
= SYSTEMALARMS_EXTENDEDALARMSTATUS_FLIGHTMODE
;
186 // Check throttle/collective channel range for valid configuration of input for critical control
187 SystemSettingsThrustControlOptions thrustType
;
188 SystemSettingsThrustControlGet(&thrustType
);
189 ManualControlSettingsChannelMinData channelMin
;
190 ManualControlSettingsChannelMaxData channelMax
;
191 ManualControlSettingsChannelMinGet(&channelMin
);
192 ManualControlSettingsChannelMaxGet(&channelMax
);
193 switch (thrustType
) {
194 case SYSTEMSETTINGS_THRUSTCONTROL_THROTTLE
:
195 ADDSEVERITY(fabsf(channelMax
.Throttle
- channelMin
.Throttle
) > 300.0f
);
196 ADDEXTENDEDALARMSTATUS(SYSTEMALARMS_EXTENDEDALARMSTATUS_BADTHROTTLEORCOLLECTIVEINPUTRANGE
, 0);
198 case SYSTEMSETTINGS_THRUSTCONTROL_COLLECTIVE
:
199 ADDSEVERITY(fabsf(channelMax
.Collective
- channelMin
.Collective
) > 300.0f
);
200 ADDEXTENDEDALARMSTATUS(SYSTEMALARMS_EXTENDEDALARMSTATUS_BADTHROTTLEORCOLLECTIVEINPUTRANGE
, 0);
206 // query sanity check hooks
207 if (severity
< SYSTEMALARMS_ALARM_CRITICAL
) {
208 SANITYCHECK_CustomHookInstance
*instance
= NULL
;
209 LL_FOREACH(hooks
, instance
) {
210 if (instance
->enabled
) {
211 alarmstatus
= instance
->hook();
212 if (alarmstatus
!= SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE
) {
213 severity
= SYSTEMALARMS_ALARM_CRITICAL
;
220 FlightModeSettingsDisableSanityChecksOptions checks_disabled
;
221 FlightModeSettingsDisableSanityChecksGet(&checks_disabled
);
222 if (checks_disabled
== FLIGHTMODESETTINGS_DISABLESANITYCHECKS_TRUE
) {
223 severity
= SYSTEMALARMS_ALARM_WARNING
;
226 if (severity
!= SYSTEMALARMS_ALARM_OK
) {
227 ExtendedAlarmsSet(SYSTEMALARMS_ALARM_SYSTEMCONFIGURATION
, severity
, alarmstatus
, alarmsubstatus
);
229 AlarmsClear(SYSTEMALARMS_ALARM_SYSTEMCONFIGURATION
);
236 * Checks the stabilization settings for a particular mode and makes
237 * sure it is appropriate for the airframe
238 * @param[in] index Which stabilization mode to check
239 * @returns true or false
241 static bool check_stabilization_settings(int index
, bool multirotor
, bool coptercontrol
, bool gpsassisted
)
243 uint8_t modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_NUMELEM
];
245 // Get the different axis modes for this switch position
248 FlightModeSettingsStabilization1SettingsArrayGet((FlightModeSettingsStabilization1SettingsOptions
*)modes
);
251 FlightModeSettingsStabilization2SettingsArrayGet((FlightModeSettingsStabilization2SettingsOptions
*)modes
);
254 FlightModeSettingsStabilization3SettingsArrayGet((FlightModeSettingsStabilization3SettingsOptions
*)modes
);
257 FlightModeSettingsStabilization4SettingsArrayGet((FlightModeSettingsStabilization4SettingsOptions
*)modes
);
260 FlightModeSettingsStabilization5SettingsArrayGet((FlightModeSettingsStabilization5SettingsOptions
*)modes
);
263 FlightModeSettingsStabilization6SettingsArrayGet((FlightModeSettingsStabilization6SettingsOptions
*)modes
);
269 // For multirotors verify that roll/pitch/yaw are not set to "none"
270 // (why not? might be fun to test ones reactions ;) if you dare, set your frame to "custom"!
272 for (uint32_t i
= 0; i
< FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
; i
++) {
273 if (modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_MANUAL
) {
280 // For multirotors verify that roll/pitch are either attitude or rattitude
281 for (uint32_t i
= 0; i
< FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_YAW
; i
++) {
282 if (!(modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ATTITUDE
||
283 modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_RATTITUDE
)) {
290 // coptercontrol cannot do altitude holding
292 if (modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEHOLD
293 || modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEVARIO
299 // check that thrust modes are only set to thrust axis
300 for (uint32_t i
= 0; i
< FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
; i
++) {
301 if (modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEHOLD
302 || modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEVARIO
307 if (!(modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_MANUAL
308 || modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEHOLD
309 || modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ALTITUDEVARIO
310 || modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_CRUISECONTROL
315 // if cruise control, ensure Acro+ is not set
316 if (modes
[FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_THRUST
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_CRUISECONTROL
) {
317 for (uint32_t i
= 0; i
< FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_YAW
; i
++) {
318 // Do not allow Acro+, attitude estimation is not safe.
319 if (modes
[i
] == FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_ACRO
) {
325 // Warning: This assumes that certain conditions in the XML file are met. That
326 // FLIGHTMODESETTINGS_STABILIZATION1SETTINGS_MANUAL has the same numeric value for each channel
327 // and is the same for STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL
328 // (this is checked at compile time by static constraint manualcontrol.h)
334 FrameType_t
GetCurrentFrameType()
336 SystemSettingsAirframeTypeOptions airframe_type
;
338 SystemSettingsAirframeTypeGet(&airframe_type
);
339 switch ((SystemSettingsAirframeTypeOptions
)airframe_type
) {
340 case SYSTEMSETTINGS_AIRFRAMETYPE_QUADX
:
341 case SYSTEMSETTINGS_AIRFRAMETYPE_QUADP
:
342 case SYSTEMSETTINGS_AIRFRAMETYPE_HEXA
:
343 case SYSTEMSETTINGS_AIRFRAMETYPE_OCTO
:
344 case SYSTEMSETTINGS_AIRFRAMETYPE_OCTOX
:
345 case SYSTEMSETTINGS_AIRFRAMETYPE_HEXAX
:
346 case SYSTEMSETTINGS_AIRFRAMETYPE_HEXAH
:
347 case SYSTEMSETTINGS_AIRFRAMETYPE_OCTOV
:
348 case SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXP
:
349 case SYSTEMSETTINGS_AIRFRAMETYPE_HEXACOAX
:
350 case SYSTEMSETTINGS_AIRFRAMETYPE_TRI
:
351 case SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXX
:
352 return FRAME_TYPE_MULTIROTOR
;
354 case SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWING
:
355 case SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWINGELEVON
:
356 case SYSTEMSETTINGS_AIRFRAMETYPE_FIXEDWINGVTAIL
:
357 return FRAME_TYPE_FIXED_WING
;
359 case SYSTEMSETTINGS_AIRFRAMETYPE_HELICP
:
360 return FRAME_TYPE_HELI
;
362 case SYSTEMSETTINGS_AIRFRAMETYPE_GROUNDVEHICLECAR
:
363 case SYSTEMSETTINGS_AIRFRAMETYPE_GROUNDVEHICLEDIFFERENTIAL
:
364 case SYSTEMSETTINGS_AIRFRAMETYPE_GROUNDVEHICLEMOTORCYCLE
:
365 case SYSTEMSETTINGS_AIRFRAMETYPE_GROUNDVEHICLEBOAT
:
366 case SYSTEMSETTINGS_AIRFRAMETYPE_GROUNDVEHICLEDIFFERENTIALBOAT
:
367 return FRAME_TYPE_GROUND
;
369 case SYSTEMSETTINGS_AIRFRAMETYPE_VTOL
:
370 case SYSTEMSETTINGS_AIRFRAMETYPE_CUSTOM
:
371 return FRAME_TYPE_CUSTOM
;
373 // anyway it should not reach here
374 return FRAME_TYPE_CUSTOM
;
377 void SANITYCHECK_AttachHook(SANITYCHECK_CustomHook_function
*hook
)
380 SANITYCHECK_CustomHookInstance
*instance
= NULL
;
382 // Check whether there is an existing instance and enable it
383 LL_FOREACH(hooks
, instance
) {
384 if (instance
->hook
== hook
) {
385 instance
->enabled
= true;
390 // No existing instance found, attach this new one
391 instance
= (SANITYCHECK_CustomHookInstance
*)pios_malloc(sizeof(SANITYCHECK_CustomHookInstance
));
392 PIOS_Assert(instance
);
393 instance
->hook
= hook
;
394 instance
->next
= NULL
;
395 instance
->enabled
= true;
396 LL_APPEND(hooks
, instance
);
399 void SANITYCHECK_DetachHook(SANITYCHECK_CustomHook_function
*hook
)
404 SANITYCHECK_CustomHookInstance
*instance
= NULL
;
405 LL_FOREACH(hooks
, instance
) {
406 if (instance
->hook
== hook
) {
407 instance
->enabled
= false;