2013-10-29 10:11:48 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
// Code to detect a crash main ArduCopter code
|
|
|
|
#ifndef CRASH_CHECK_ITERATIONS_MAX
|
2013-10-30 01:27:06 -03:00
|
|
|
# define CRASH_CHECK_ITERATIONS_MAX 20 // 2 second (ie. 10 iterations at 10hz) inverted indicates a crash
|
2013-10-29 10:11:48 -03:00
|
|
|
#endif
|
|
|
|
#ifndef CRASH_CHECK_ANGLE_DEVIATION_CD
|
2013-10-30 01:27:06 -03:00
|
|
|
# define CRASH_CHECK_ANGLE_DEVIATION_CD 2000 // 20 degrees beyond angle max is signal we are inverted
|
|
|
|
#endif
|
|
|
|
#ifndef CRASH_CHECK_ALT_CHANGE_LIMIT_CM
|
|
|
|
# define CRASH_CHECK_ALT_CHANGE_LIMIT_CM 50 // baro altitude must not change by more than 50cm
|
2013-10-29 10:11:48 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// should be called at 10hz
|
|
|
|
void crash_check()
|
|
|
|
{
|
|
|
|
static uint8_t inverted_count; // number of iterations we have been inverted
|
2013-10-30 01:27:06 -03:00
|
|
|
static int32_t baro_alt_prev;
|
2013-10-29 10:11:48 -03:00
|
|
|
|
2013-10-30 01:27:06 -03:00
|
|
|
// return immediately if motors are not armed or pilot's throttle is above zero
|
|
|
|
if (!motors.armed() || (g.rc_3.control_in != 0 && !failsafe.radio)) {
|
2013-10-29 10:11:48 -03:00
|
|
|
inverted_count = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return immediately if we are not in an angle stabilize flight mode or we are flipping
|
2014-02-03 03:25:11 -04:00
|
|
|
if (control_mode == ACRO || control_mode == FLIP) {
|
2013-10-29 10:11:48 -03:00
|
|
|
inverted_count = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check angles
|
2013-10-18 05:03:31 -03:00
|
|
|
int32_t lean_max = aparm.angle_max + CRASH_CHECK_ANGLE_DEVIATION_CD;
|
2013-10-29 10:11:48 -03:00
|
|
|
if (labs(ahrs.roll_sensor) > lean_max || labs(ahrs.pitch_sensor) > lean_max) {
|
|
|
|
inverted_count++;
|
2013-10-30 01:27:06 -03:00
|
|
|
|
|
|
|
// if we have just become inverted record the baro altitude
|
|
|
|
if (inverted_count == 1) {
|
|
|
|
baro_alt_prev = baro_alt;
|
|
|
|
|
|
|
|
// exit if baro altitude change indicates we are moving (probably falling)
|
|
|
|
}else if (labs(baro_alt - baro_alt_prev) > CRASH_CHECK_ALT_CHANGE_LIMIT_CM) {
|
|
|
|
inverted_count = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
// check if inverted for 2 seconds
|
|
|
|
}else if (inverted_count >= CRASH_CHECK_ITERATIONS_MAX) {
|
2013-10-29 10:11:48 -03:00
|
|
|
// log an error in the dataflash
|
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_CRASH_CHECK, ERROR_CODE_CRASH_CHECK_CRASH);
|
|
|
|
// send message to gcs
|
2013-10-30 01:27:06 -03:00
|
|
|
gcs_send_text_P(SEVERITY_HIGH,PSTR("Crash: Disarming"));
|
2013-10-29 10:11:48 -03:00
|
|
|
// disarm motors
|
|
|
|
init_disarm_motors();
|
|
|
|
}
|
2013-10-30 01:27:06 -03:00
|
|
|
}else{
|
|
|
|
// we are not inverted so reset counter
|
|
|
|
inverted_count = 0;
|
2013-10-29 10:11:48 -03:00
|
|
|
}
|
|
|
|
}
|