2015-10-24 18:52:39 -03:00
|
|
|
#include "Plane.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
is_flying and crash detection logic
|
|
|
|
*/
|
|
|
|
|
2016-03-28 18:52:30 -03:00
|
|
|
#define CRASH_DETECTION_DELAY_MS 500
|
2015-11-17 20:11:21 -04:00
|
|
|
#define IS_FLYING_IMPACT_TIMER_MS 3000
|
2016-08-22 23:49:25 -03:00
|
|
|
#define GPS_IS_FLYING_SPEED_CMS 150
|
2015-11-17 20:11:21 -04:00
|
|
|
|
2015-10-24 18:52:39 -03:00
|
|
|
/*
|
|
|
|
Do we think we are flying?
|
|
|
|
Probabilistic method where a bool is low-passed and considered a probability.
|
|
|
|
*/
|
|
|
|
void Plane::update_is_flying_5Hz(void)
|
|
|
|
{
|
|
|
|
float aspeed;
|
|
|
|
bool is_flying_bool;
|
2015-11-19 23:04:52 -04:00
|
|
|
uint32_t now_ms = AP_HAL::millis();
|
2015-10-24 18:52:39 -03:00
|
|
|
|
2016-11-17 21:07:10 -04:00
|
|
|
uint32_t ground_speed_thresh_cm = (aparm.min_gndspeed_cm > 0) ? ((uint32_t)(aparm.min_gndspeed_cm*0.9f)) : GPS_IS_FLYING_SPEED_CMS;
|
2015-10-24 18:52:39 -03:00
|
|
|
bool gps_confirmed_movement = (gps.status() >= AP_GPS::GPS_OK_FIX_3D) &&
|
|
|
|
(gps.ground_speed_cm() >= ground_speed_thresh_cm);
|
|
|
|
|
|
|
|
// airspeed at least 75% of stall speed?
|
2017-11-26 19:37:00 -04:00
|
|
|
bool airspeed_movement = ahrs.airspeed_estimate(&aspeed) && (aspeed >= (MAX(aparm.airspeed_min,2)*0.75f));
|
2015-10-24 18:52:39 -03:00
|
|
|
|
|
|
|
|
2016-05-27 20:25:44 -03:00
|
|
|
if (quadplane.is_flying()) {
|
|
|
|
is_flying_bool = true;
|
|
|
|
|
|
|
|
} else if(arming.is_armed()) {
|
|
|
|
// when armed assuming flying and we need overwhelming evidence that we ARE NOT flying
|
2015-10-24 18:52:39 -03:00
|
|
|
// short drop-outs of GPS are common during flight due to banking which points the antenna in different directions
|
|
|
|
bool gps_lost_recently = (gps.last_fix_time_ms() > 0) && // we have locked to GPS before
|
|
|
|
(gps.status() < AP_GPS::GPS_OK_FIX_2D) && // and it's lost now
|
|
|
|
(now_ms - gps.last_fix_time_ms() < 5000); // but it wasn't that long ago (<5s)
|
|
|
|
|
|
|
|
if ((auto_state.last_flying_ms > 0) && gps_lost_recently) {
|
|
|
|
// we've flown before, remove GPS constraints temporarily and only use airspeed
|
|
|
|
is_flying_bool = airspeed_movement; // moving through the air
|
|
|
|
} else {
|
|
|
|
// we've never flown yet, require good GPS movement
|
|
|
|
is_flying_bool = airspeed_movement || // moving through the air
|
|
|
|
gps_confirmed_movement; // locked and we're moving
|
|
|
|
}
|
|
|
|
|
|
|
|
if (control_mode == AUTO) {
|
|
|
|
/*
|
|
|
|
make is_flying() more accurate during various auto modes
|
|
|
|
*/
|
|
|
|
|
2015-11-17 20:11:21 -04:00
|
|
|
// Detect X-axis deceleration for probable ground impacts.
|
|
|
|
// Limit the max probability so it can decay faster. This
|
|
|
|
// will not change the is_flying state, anything above 0.1
|
|
|
|
// is "true", it just allows it to decay faster once we decide we
|
|
|
|
// aren't flying using the normal schemes
|
2015-12-30 20:15:57 -04:00
|
|
|
if (g.crash_accel_threshold == 0) {
|
|
|
|
crash_state.impact_detected = false;
|
2015-12-30 23:23:18 -04:00
|
|
|
} else if (ins.get_accel_peak_hold_neg_x() < -(g.crash_accel_threshold)) {
|
2015-11-17 20:11:21 -04:00
|
|
|
// large deceleration detected, lets lower confidence VERY quickly
|
2015-12-30 20:15:57 -04:00
|
|
|
crash_state.impact_detected = true;
|
|
|
|
crash_state.impact_timer_ms = now_ms;
|
|
|
|
if (isFlyingProbability > 0.2f) {
|
|
|
|
isFlyingProbability = 0.2f;
|
2015-11-17 20:11:21 -04:00
|
|
|
}
|
2015-12-30 20:15:57 -04:00
|
|
|
} else if (crash_state.impact_detected &&
|
2015-11-17 20:11:21 -04:00
|
|
|
(now_ms - crash_state.impact_timer_ms > IS_FLYING_IMPACT_TIMER_MS)) {
|
2015-12-30 20:15:57 -04:00
|
|
|
// no impacts seen in a while, clear the flag so we stop clipping isFlyingProbability
|
2015-11-17 20:11:21 -04:00
|
|
|
crash_state.impact_detected = false;
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:52:39 -03:00
|
|
|
switch (flight_stage)
|
|
|
|
{
|
2016-12-13 22:20:57 -04:00
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_TAKEOFF:
|
2015-10-24 18:52:39 -03:00
|
|
|
break;
|
|
|
|
|
2016-12-13 22:20:57 -04:00
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_NORMAL:
|
2015-11-17 20:11:21 -04:00
|
|
|
if (in_preLaunch_flight_stage()) {
|
|
|
|
// while on the ground, an uncalibrated airspeed sensor can drift to 7m/s so
|
|
|
|
// ensure we aren't showing a false positive.
|
|
|
|
is_flying_bool = false;
|
|
|
|
crash_state.is_crashed = false;
|
|
|
|
auto_state.started_flying_in_auto_ms = 0;
|
|
|
|
}
|
2015-10-24 18:52:39 -03:00
|
|
|
break;
|
|
|
|
|
2016-12-13 22:20:57 -04:00
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_VTOL:
|
2016-01-01 02:36:41 -04:00
|
|
|
// TODO: detect ground impacts
|
|
|
|
break;
|
|
|
|
|
2017-11-02 19:12:07 -03:00
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_LAND:
|
2017-11-07 02:21:37 -04:00
|
|
|
if (landing.is_on_approach() && auto_state.sink_rate > 0.2f) {
|
2017-11-02 19:12:07 -03:00
|
|
|
is_flying_bool = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-12-13 23:18:48 -04:00
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_ABORT_LAND:
|
2015-11-17 20:11:21 -04:00
|
|
|
if (auto_state.sink_rate < -0.5f) {
|
|
|
|
// steep climb
|
|
|
|
is_flying_bool = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-10-24 18:52:39 -03:00
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// when disarmed assume not flying and need overwhelming evidence that we ARE flying
|
|
|
|
is_flying_bool = airspeed_movement && gps_confirmed_movement;
|
|
|
|
|
2017-01-10 03:42:57 -04:00
|
|
|
if ((flight_stage == AP_Vehicle::FixedWing::FLIGHT_TAKEOFF) || landing.is_flaring()) {
|
2015-10-24 18:52:39 -03:00
|
|
|
is_flying_bool = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 20:11:21 -04:00
|
|
|
if (!crash_state.impact_detected || !is_flying_bool) {
|
2015-12-30 20:15:57 -04:00
|
|
|
// when impact is detected, enforce a clip. Only allow isFlyingProbability to go down, not up.
|
2015-11-17 20:11:21 -04:00
|
|
|
// low-pass the result.
|
|
|
|
// coef=0.15f @ 5Hz takes 3.0s to go from 100% down to 10% (or 0% up to 90%)
|
|
|
|
isFlyingProbability = (0.85f * isFlyingProbability) + (0.15f * (float)is_flying_bool);
|
|
|
|
}
|
2015-10-24 18:52:39 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
update last_flying_ms so we always know how long we have not
|
|
|
|
been flying for. This helps for crash detection and auto-disarm
|
|
|
|
*/
|
|
|
|
bool new_is_flying = is_flying();
|
|
|
|
|
|
|
|
// we are flying, note the time
|
|
|
|
if (new_is_flying) {
|
|
|
|
|
|
|
|
auto_state.last_flying_ms = now_ms;
|
|
|
|
|
|
|
|
if (!previous_is_flying) {
|
|
|
|
// just started flying in any mode
|
|
|
|
started_flying_ms = now_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((control_mode == AUTO) &&
|
|
|
|
((auto_state.started_flying_in_auto_ms == 0) || !previous_is_flying) ) {
|
|
|
|
|
|
|
|
// We just started flying, note that time also
|
|
|
|
auto_state.started_flying_in_auto_ms = now_ms;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previous_is_flying = new_is_flying;
|
2016-07-10 21:23:08 -03:00
|
|
|
adsb.set_is_flying(new_is_flying);
|
2016-09-01 15:11:17 -03:00
|
|
|
#if FRSKY_TELEM_ENABLED == ENABLED
|
2016-09-21 19:38:58 -03:00
|
|
|
frsky_telemetry.set_is_flying(new_is_flying);
|
2016-09-01 15:11:17 -03:00
|
|
|
#endif
|
2016-10-25 22:24:41 -03:00
|
|
|
g2.stats.set_flying(new_is_flying);
|
2015-10-24 18:52:39 -03:00
|
|
|
|
2016-02-07 12:39:41 -04:00
|
|
|
crash_detection_update();
|
|
|
|
|
2017-10-23 21:39:45 -03:00
|
|
|
Log_Write_Status();
|
2017-06-19 00:16:43 -03:00
|
|
|
|
|
|
|
// tell AHRS flying state
|
|
|
|
ahrs.set_likely_flying(new_is_flying);
|
2015-10-24 18:52:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if we think we are flying. This is a probabilistic
|
|
|
|
estimate, and needs to be used very carefully. Each use case needs
|
|
|
|
to be thought about individually.
|
|
|
|
*/
|
|
|
|
bool Plane::is_flying(void)
|
|
|
|
{
|
|
|
|
if (hal.util->get_soft_armed()) {
|
2016-04-22 07:20:06 -03:00
|
|
|
if (quadplane.is_flying_vtol()) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-24 18:52:39 -03:00
|
|
|
// when armed, assume we're flying unless we probably aren't
|
|
|
|
return (isFlyingProbability >= 0.1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// when disarmed, assume we're not flying unless we probably are
|
|
|
|
return (isFlyingProbability >= 0.9f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if we have crashed
|
|
|
|
*/
|
|
|
|
void Plane::crash_detection_update(void)
|
|
|
|
{
|
2016-11-17 21:07:10 -04:00
|
|
|
if (control_mode != AUTO || !aparm.crash_detection_enable)
|
2015-10-24 18:52:39 -03:00
|
|
|
{
|
|
|
|
// crash detection is only available in AUTO mode
|
|
|
|
crash_state.debounce_timer_ms = 0;
|
2015-11-17 20:11:21 -04:00
|
|
|
crash_state.is_crashed = false;
|
2015-10-24 18:52:39 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-19 23:04:52 -04:00
|
|
|
uint32_t now_ms = AP_HAL::millis();
|
2015-10-24 18:52:39 -03:00
|
|
|
bool crashed_near_land_waypoint = false;
|
|
|
|
bool crashed = false;
|
|
|
|
bool been_auto_flying = (auto_state.started_flying_in_auto_ms > 0) &&
|
|
|
|
(now_ms - auto_state.started_flying_in_auto_ms >= 2500);
|
|
|
|
|
2016-03-28 18:52:30 -03:00
|
|
|
if (!is_flying() && arming.is_armed())
|
2015-10-24 18:52:39 -03:00
|
|
|
{
|
2017-01-10 03:42:57 -04:00
|
|
|
if (landing.is_expecting_impact()) {
|
2015-10-24 18:52:39 -03:00
|
|
|
// We should be nice and level-ish in this flight stage. If not, we most
|
|
|
|
// likely had a crazy landing. Throttle is inhibited already at the flare
|
|
|
|
// but go ahead and notify GCS and perform any additional post-crash actions.
|
|
|
|
// Declare a crash if we are oriented more that 60deg in pitch or roll
|
2015-11-17 20:11:21 -04:00
|
|
|
if (!crash_state.checkedHardLanding && // only check once
|
2016-03-28 18:52:30 -03:00
|
|
|
been_auto_flying &&
|
|
|
|
(labs(ahrs.roll_sensor) > 6000 || labs(ahrs.pitch_sensor) > 6000)) {
|
2015-10-24 18:52:39 -03:00
|
|
|
crashed = true;
|
2016-03-28 18:52:30 -03:00
|
|
|
crash_state.debounce_time_total_ms = CRASH_DETECTION_DELAY_MS;
|
2015-10-24 18:52:39 -03:00
|
|
|
|
|
|
|
// did we "crash" within 75m of the landing location? Probably just a hard landing
|
|
|
|
crashed_near_land_waypoint =
|
|
|
|
get_distance(current_loc, mission.get_current_nav_cmd().content.location) < 75;
|
|
|
|
|
|
|
|
// trigger hard landing event right away, or never again. This inhibits a false hard landing
|
|
|
|
// event when, for example, a minute after a good landing you pick the plane up and
|
|
|
|
// this logic is still running and detects the plane is on its side as you carry it.
|
2015-11-17 20:11:21 -04:00
|
|
|
crash_state.debounce_timer_ms = now_ms + CRASH_DETECTION_DELAY_MS;
|
2015-10-24 18:52:39 -03:00
|
|
|
}
|
2015-11-17 20:11:21 -04:00
|
|
|
|
|
|
|
crash_state.checkedHardLanding = true;
|
|
|
|
|
2017-01-10 03:42:57 -04:00
|
|
|
} else if (landing.is_on_approach()) {
|
|
|
|
// when altitude gets low, we automatically flare so ground crashes
|
|
|
|
// most likely can not be triggered from here. However,
|
|
|
|
// a crash into a tree would be caught here.
|
|
|
|
if (been_auto_flying) {
|
|
|
|
crashed = true;
|
|
|
|
crash_state.debounce_time_total_ms = CRASH_DETECTION_DELAY_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch (flight_stage)
|
|
|
|
{
|
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_TAKEOFF:
|
|
|
|
if (g.takeoff_throttle_min_accel > 0 &&
|
|
|
|
!throttle_suppressed) {
|
|
|
|
// if you have an acceleration holding back throttle, but you met the
|
|
|
|
// accel threshold but still not fying, then you either shook/hit the
|
|
|
|
// plane or it was a failed launch.
|
|
|
|
crashed = true;
|
|
|
|
crash_state.debounce_time_total_ms = CRASH_DETECTION_DELAY_MS;
|
|
|
|
}
|
|
|
|
// TODO: handle auto missions without NAV_TAKEOFF mission cmd
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_NORMAL:
|
|
|
|
if (!in_preLaunch_flight_stage() && been_auto_flying) {
|
|
|
|
crashed = true;
|
|
|
|
crash_state.debounce_time_total_ms = CRASH_DETECTION_DELAY_MS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AP_Vehicle::FixedWing::FLIGHT_VTOL:
|
|
|
|
// we need a totally new method for this
|
|
|
|
crashed = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
}
|
2015-10-24 18:52:39 -03:00
|
|
|
} else {
|
2015-11-17 20:11:21 -04:00
|
|
|
crash_state.checkedHardLanding = false;
|
2015-10-24 18:52:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!crashed) {
|
|
|
|
// reset timer
|
|
|
|
crash_state.debounce_timer_ms = 0;
|
|
|
|
|
|
|
|
} else if (crash_state.debounce_timer_ms == 0) {
|
|
|
|
// start timer
|
|
|
|
crash_state.debounce_timer_ms = now_ms;
|
|
|
|
|
2016-03-28 18:52:30 -03:00
|
|
|
} else if ((now_ms - crash_state.debounce_timer_ms >= crash_state.debounce_time_total_ms) && !crash_state.is_crashed) {
|
2015-10-24 18:52:39 -03:00
|
|
|
crash_state.is_crashed = true;
|
|
|
|
|
2016-11-17 21:07:10 -04:00
|
|
|
if (aparm.crash_detection_enable == CRASH_DETECT_ACTION_BITMASK_DISABLED) {
|
2015-10-24 18:52:39 -03:00
|
|
|
if (crashed_near_land_waypoint) {
|
2017-07-08 22:15:58 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "Hard landing detected. No action taken");
|
2015-10-24 18:52:39 -03:00
|
|
|
} else {
|
2017-07-08 22:15:58 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_EMERGENCY, "Crash detected. No action taken");
|
2015-10-24 18:52:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-11-17 21:07:10 -04:00
|
|
|
if (aparm.crash_detection_enable & CRASH_DETECT_ACTION_BITMASK_DISARM) {
|
2015-10-24 18:52:39 -03:00
|
|
|
disarm_motors();
|
|
|
|
}
|
|
|
|
if (crashed_near_land_waypoint) {
|
2017-07-08 22:15:58 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "Hard landing detected");
|
2015-10-24 18:52:39 -03:00
|
|
|
} else {
|
2017-07-08 22:15:58 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_EMERGENCY, "Crash detected");
|
2015-10-24 18:52:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 20:11:21 -04:00
|
|
|
/*
|
|
|
|
* return true if we are in a pre-launch phase of an auto-launch, typically used in bungee launches
|
|
|
|
*/
|
|
|
|
bool Plane::in_preLaunch_flight_stage(void) {
|
|
|
|
return (control_mode == AUTO &&
|
|
|
|
throttle_suppressed &&
|
2016-12-13 22:20:57 -04:00
|
|
|
flight_stage == AP_Vehicle::FixedWing::FLIGHT_NORMAL &&
|
2017-10-29 03:31:09 -03:00
|
|
|
mission.get_current_nav_cmd().id == MAV_CMD_NAV_TAKEOFF &&
|
|
|
|
!quadplane.is_vtol_takeoff(mission.get_current_nav_cmd().id));
|
2015-11-17 20:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|