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/failsafe.cpp
Views: 1798
/*1failsafe support2Andrew Tridgell, December 20113*/45#include "Rover.h"67#include <stdio.h>89/*10our failsafe strategy is to detect main loop lockup and disarm.11*/1213/*14this failsafe_check function is called from the core timer interrupt15at 1kHz.16*/17void Rover::failsafe_check()18{19static uint16_t last_ticks;20static uint32_t last_timestamp;21const uint32_t tnow = AP_HAL::micros();2223const uint16_t ticks = scheduler.ticks();24if (ticks != last_ticks) {25// the main loop is running, all is OK26last_ticks = ticks;27last_timestamp = tnow;28return;29}3031if (tnow - last_timestamp > 200000) {32// we have gone at least 0.2 seconds since the main loop33// ran. That means we're in trouble, or perhaps are in34// an initialisation routine or log erase. disarm the motors35// To-Do: log error36if (arming.is_armed()) {37// disarm motors38arming.disarm(AP_Arming::Method::CPUFAILSAFE);39}40}41}4243/*44called to set/unset a failsafe event.45*/46void Rover::failsafe_trigger(uint8_t failsafe_type, const char* type_str, bool on)47{48uint8_t old_bits = failsafe.bits;49if (on) {50failsafe.bits |= failsafe_type;51} else {52failsafe.bits &= ~failsafe_type;53}54if (old_bits == 0 && failsafe.bits != 0) {55// a failsafe event has started56failsafe.start_time = millis();57}58if (failsafe.triggered != 0 && failsafe.bits == 0) {59// a failsafe event has ended60gcs().send_text(MAV_SEVERITY_INFO, "%s Failsafe Cleared", type_str);61}6263failsafe.triggered &= failsafe.bits;6465if ((failsafe.triggered == 0) &&66(failsafe.bits != 0) &&67(millis() - failsafe.start_time > g.fs_timeout * 1000) &&68(control_mode != &mode_rtl) &&69((control_mode != &mode_hold || (g2.fs_options & (uint32_t)Failsafe_Options::Failsafe_Option_Active_In_Hold)))) {70failsafe.triggered = failsafe.bits;71gcs().send_text(MAV_SEVERITY_WARNING, "%s Failsafe", type_str);7273// clear rc overrides74RC_Channels::clear_overrides();7576if ((control_mode == &mode_auto) &&77((failsafe_type == FAILSAFE_EVENT_THROTTLE && g.fs_throttle_enabled == FS_THR_ENABLED_CONTINUE_MISSION) ||78(failsafe_type == FAILSAFE_EVENT_GCS && g.fs_gcs_enabled == FS_GCS_ENABLED_CONTINUE_MISSION))) {79// continue with mission in auto mode80gcs().send_text(MAV_SEVERITY_WARNING, "Failsafe - Continuing Auto Mode");81} else {82switch ((FailsafeAction)g.fs_action.get()) {83case FailsafeAction::None:84break;85case FailsafeAction::SmartRTL:86if (set_mode(mode_smartrtl, ModeReason::FAILSAFE)) {87break;88}89FALLTHROUGH;90case FailsafeAction::RTL:91if (set_mode(mode_rtl, ModeReason::FAILSAFE)) {92break;93}94FALLTHROUGH;95case FailsafeAction::Hold:96set_mode(mode_hold, ModeReason::FAILSAFE);97break;98case FailsafeAction::SmartRTL_Hold:99if (!set_mode(mode_smartrtl, ModeReason::FAILSAFE)) {100set_mode(mode_hold, ModeReason::FAILSAFE);101}102break;103case FailsafeAction::Terminate:104arming.disarm(AP_Arming::Method::FAILSAFE_ACTION_TERMINATE);105break;106}107}108}109}110111void Rover::handle_battery_failsafe(const char* type_str, const int8_t action)112{113switch ((FailsafeAction)action) {114case FailsafeAction::None:115break;116case FailsafeAction::SmartRTL:117if (set_mode(mode_smartrtl, ModeReason::BATTERY_FAILSAFE)) {118break;119}120FALLTHROUGH;121case FailsafeAction::RTL:122if (set_mode(mode_rtl, ModeReason::BATTERY_FAILSAFE)) {123break;124}125FALLTHROUGH;126case FailsafeAction::Hold:127set_mode(mode_hold, ModeReason::BATTERY_FAILSAFE);128break;129case FailsafeAction::SmartRTL_Hold:130if (!set_mode(mode_smartrtl, ModeReason::BATTERY_FAILSAFE)) {131set_mode(mode_hold, ModeReason::BATTERY_FAILSAFE);132}133break;134case FailsafeAction::Terminate:135#if AP_ROVER_ADVANCED_FAILSAFE_ENABLED136char battery_type_str[17];137snprintf(battery_type_str, 17, "%s battery", type_str);138g2.afs.gcs_terminate(true, battery_type_str);139#else140arming.disarm(AP_Arming::Method::BATTERYFAILSAFE);141#endif // AP_ROVER_ADVANCED_FAILSAFE_ENABLED142break;143}144}145146#if AP_ROVER_ADVANCED_FAILSAFE_ENABLED147/*148check for AFS failsafe check149*/150void Rover::afs_fs_check(void)151{152// perform AFS failsafe checks153g2.afs.check(failsafe.last_valid_rc_ms);154}155#endif156157158