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/ArduSub/surface_bottom_detector.cpp
Views: 1798
// Jacob Walser: [email protected]12#include "Sub.h"34// counter to verify contact with bottom5static uint32_t bottom_detector_count = 0;6static uint32_t surface_detector_count = 0;7static float current_depth = 0;89// checks if we have hit bottom or surface and updates the ap.at_bottom and ap.at_surface flags10// called at MAIN_LOOP_RATE11// ToDo: doesn't need to be called this fast12void Sub::update_surface_and_bottom_detector()13{14if (!motors.armed()) { // only update when armed15set_surfaced(false);16set_bottomed(false);17return;18}1920Vector3f velocity;21UNUSED_RESULT(ahrs.get_velocity_NED(velocity));2223// check that we are not moving up or down24bool vel_stationary = velocity.z > -0.05 && velocity.z < 0.05;2526if (ap.depth_sensor_present && sensor_health.depth) { // we can use the external pressure sensor for a very accurate and current measure of our z axis position27current_depth = barometer.get_altitude(); // cm282930if (ap.at_surface) {31set_surfaced(current_depth > g.surface_depth*0.01 - 0.05); // add a 5cm buffer so it doesn't trigger too often32} else {33set_surfaced(current_depth > g.surface_depth*0.01); // If we are above surface depth, we are surfaced34}353637if (motors.limit.throttle_lower && vel_stationary) {38// bottom criteria met - increment the counter and check if we've triggered39if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {40bottom_detector_count++;41} else {42set_bottomed(true);43}4445} else {46set_bottomed(false);47}4849// with no external baro, the only thing we have to go by is a vertical velocity estimate50} else if (vel_stationary) {51if (motors.limit.throttle_upper) {5253// surface criteria met, increment counter and see if we've triggered54if (surface_detector_count < ((float)SURFACE_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {55surface_detector_count++;56} else {57set_surfaced(true);58}5960} else if (motors.limit.throttle_lower) {61// bottom criteria met, increment counter and see if we've triggered62if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {63bottom_detector_count++;64} else {65set_bottomed(true);66}6768} else { // we're not at the limits of throttle, so reset both detectors69set_surfaced(false);70set_bottomed(false);71}7273} else { // we're moving up or down, so reset both detectors74set_surfaced(false);75set_bottomed(false);76}77}7879void Sub::set_surfaced(bool at_surface)80{818283if (ap.at_surface == at_surface) { // do nothing if state unchanged84return;85}8687ap.at_surface = at_surface;8889surface_detector_count = 0;9091if (ap.at_surface) {92LOGGER_WRITE_EVENT(LogEvent::SURFACED);93} else {94LOGGER_WRITE_EVENT(LogEvent::NOT_SURFACED);95}96}9798void Sub::set_bottomed(bool at_bottom)99{100101if (ap.at_bottom == at_bottom) { // do nothing if state unchanged102return;103}104105ap.at_bottom = at_bottom;106107bottom_detector_count = 0;108109if (ap.at_bottom) {110LOGGER_WRITE_EVENT(LogEvent::BOTTOMED);111} else {112LOGGER_WRITE_EVENT(LogEvent::NOT_BOTTOMED);113}114}115116117