2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// Code to detect a crash main ArduCopter code
|
|
|
|
#define CRASH_CHECK_TRIGGER_SEC 2 // 2 seconds inverted indicates a crash
|
2017-02-03 00:18:27 -04:00
|
|
|
#define CRASH_CHECK_ANGLE_DEVIATION_DEG 30.0f // 30 degrees beyond angle max is signal we are inverted
|
2015-12-30 18:57:56 -04:00
|
|
|
#define CRASH_CHECK_ACCEL_MAX 3.0f // vehicle must be accelerating less than 3m/s/s to be considered crashed
|
|
|
|
|
|
|
|
// crash_check - disarms motors if a crash has been detected
|
|
|
|
// crashes are detected by the vehicle being more than 20 degrees beyond it's angle limits continuously for more than 1 second
|
|
|
|
// called at MAIN_LOOP_RATE
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::crash_check()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
static uint16_t crash_counter; // number of iterations vehicle may have been crashed
|
|
|
|
|
|
|
|
// return immediately if disarmed, or crash checking disabled
|
2016-02-20 23:39:48 -04:00
|
|
|
if (!motors.armed() || g.fs_crash_check == 0) {
|
2015-12-30 18:57:56 -04:00
|
|
|
crash_counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:30:42 -04:00
|
|
|
// return immediately if we are not in an angle stabilize flight mode
|
|
|
|
if (control_mode == ACRO) {
|
2015-12-30 18:57:56 -04:00
|
|
|
crash_counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vehicle not crashed if 1hz filtered acceleration is more than 3m/s (1G on Z-axis has been subtracted)
|
|
|
|
if (land_accel_ef_filter.get().length() >= CRASH_CHECK_ACCEL_MAX) {
|
|
|
|
crash_counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for angle error over 30 degrees
|
2017-02-03 00:18:27 -04:00
|
|
|
const float angle_error = attitude_control.get_att_error_angle_deg();
|
|
|
|
if (angle_error <= CRASH_CHECK_ANGLE_DEVIATION_DEG) {
|
2015-12-30 18:57:56 -04:00
|
|
|
crash_counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we may be crashing
|
|
|
|
crash_counter++;
|
|
|
|
|
|
|
|
// check if crashing for 2 seconds
|
2017-02-03 00:18:27 -04:00
|
|
|
if (crash_counter >= (CRASH_CHECK_TRIGGER_SEC * scheduler.get_loop_rate_hz())) {
|
2015-12-30 18:57:56 -04:00
|
|
|
// log an error in the dataflash
|
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_CRASH_CHECK, ERROR_CODE_CRASH_CHECK_CRASH);
|
|
|
|
// send message to gcs
|
|
|
|
gcs_send_text(MAV_SEVERITY_EMERGENCY,"Crash: Disarming");
|
|
|
|
// disarm motors
|
|
|
|
init_disarm_motors();
|
|
|
|
}
|
|
|
|
}
|