Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Rover/crash_check.cpp
Views: 1798
#include "Rover.h"12// Code to detect a crash or block3static const uint16_t CRASH_CHECK_TRIGGER_SEC = 2; // 2 seconds blocked indicates a crash4static const float CRASH_CHECK_THROTTLE_MIN = 5.0f; // vehicle must have a throttle greater that 5% to be considered crashed5static const float CRASH_CHECK_VEL_MIN = 0.08f; // vehicle must have a velocity under 0.08 m/s or rad/s to be considered crashed67// crash_check - disarms motors if a crash or block has been detected8// crashes are detected by the vehicle being static (no speed) for more than CRASH_CHECK_TRIGGER_SEC and motor are running9// called at 10Hz10void Rover::crash_check()11{12static uint16_t crash_counter; // number of iterations vehicle may have been crashed13bool crashed = false; //stores crash state1415// return immediately if disarmed, crash checking is disabled or vehicle is Hold, Manual or Acro mode(if it is not a balance bot)16if (!arming.is_armed() || g.fs_crash_check == FS_CRASH_DISABLE || ((!control_mode->is_autopilot_mode()) && (!is_balancebot()))) {17crash_counter = 0;18return;19}2021// Crashed if pitch/roll > crash_angle22if ((g2.crash_angle != 0) && ((fabsf(ahrs.get_pitch()) > radians(g2.crash_angle)) || (fabsf(ahrs.get_roll()) > radians(g2.crash_angle)))) {23crashed = true;24}2526// TODO : Check if min vel can be calculated27// min_vel = ( CRASH_CHECK_THROTTLE_MIN * g.speed_cruise) / g.throttle_cruise;2829if (!is_balancebot()) {30if (!crashed && ((ahrs.groundspeed() >= CRASH_CHECK_VEL_MIN) || // Check velocity31(fabsf(ahrs.get_gyro().z) >= CRASH_CHECK_VEL_MIN) || // Check turn speed32(fabsf(g2.motors.get_throttle()) < CRASH_CHECK_THROTTLE_MIN))) {33crash_counter = 0;34return;35}3637// we may be crashing38crash_counter++;3940// check if crashing for 2 seconds41if (crash_counter >= (CRASH_CHECK_TRIGGER_SEC * 10)) {42crashed = true;43}44}4546if (crashed) {47LOGGER_WRITE_ERROR(LogErrorSubsystem::CRASH_CHECK,48LogErrorCode::CRASH_CHECK_CRASH);4950if (is_balancebot()) {51// send message to gcs52gcs().send_text(MAV_SEVERITY_EMERGENCY, "Crash: Disarming");53arming.disarm(AP_Arming::Method::CRASH);54} else {55// send message to gcs56gcs().send_text(MAV_SEVERITY_EMERGENCY, "Crash: Going to HOLD");57// change mode to hold and disarm58set_mode(mode_hold, ModeReason::CRASH_FAILSAFE);59if (g.fs_crash_check == FS_CRASH_HOLD_AND_DISARM) {60arming.disarm(AP_Arming::Method::CRASH);61}62}63}64}656667