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/Blimp/failsafe.cpp
Views: 1798
#include "Blimp.h"12//3// failsafe support4// Andrew Tridgell, December 20115//6// our failsafe strategy is to detect main loop lockup and disarm the motors7//89static bool failsafe_enabled = false;10static uint16_t failsafe_last_ticks;11static uint32_t failsafe_last_timestamp;12static bool in_failsafe;1314//15// failsafe_enable - enable failsafe16//17void Blimp::failsafe_enable()18{19failsafe_enabled = true;20failsafe_last_timestamp = micros();21}2223//24// failsafe_disable - used when we know we are going to delay the mainloop significantly25//26void Blimp::failsafe_disable()27{28failsafe_enabled = false;29}3031//32// failsafe_check - this function is called from the core timer interrupt at 1kHz.33//34void Blimp::failsafe_check()35{36uint32_t tnow = AP_HAL::micros();3738const uint16_t ticks = scheduler.ticks();39if (ticks != failsafe_last_ticks) {40// the main loop is running, all is OK41failsafe_last_ticks = ticks;42failsafe_last_timestamp = tnow;43if (in_failsafe) {44in_failsafe = false;45LOGGER_WRITE_ERROR(LogErrorSubsystem::CPU, LogErrorCode::FAILSAFE_RESOLVED);46}47return;48}4950if (!in_failsafe && failsafe_enabled && tnow - failsafe_last_timestamp > 2000000) {51// motors are running but we have gone 2 second since the52// main loop ran. That means we're in trouble and should53// disarm the motors->54in_failsafe = true;55// reduce motors to minimum (we do not immediately disarm because we want to log the failure)56if (motors->armed()) {57motors->output_min();58//TODO: this may not work correctly.59}6061LOGGER_WRITE_ERROR(LogErrorSubsystem::CPU, LogErrorCode::FAILSAFE_OCCURRED);62}6364if (failsafe_enabled && in_failsafe && tnow - failsafe_last_timestamp > 1000000) {65// disarm motors every second66failsafe_last_timestamp = tnow;67if (motors->armed()) {68motors->armed(false);69motors->output();70}71}72}737475