123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "Sub.h"
- static uint32_t bottom_detector_count = 0;
- static uint32_t surface_detector_count = 0;
- static float current_depth = 0;
- void Sub::update_surface_and_bottom_detector()
- {
- if (!motors.armed()) {
- set_surfaced(false);
- set_bottomed(false);
- return;
- }
- Vector3f velocity;
- ahrs.get_velocity_NED(velocity);
-
- bool vel_stationary = velocity.z > -0.05 && velocity.z < 0.05;
- if (ap.depth_sensor_present && sensor_health.depth) {
- current_depth = barometer.get_altitude();
- if (ap.at_surface) {
- set_surfaced(current_depth > g.surface_depth/100.0 - 0.05);
- } else {
- set_surfaced(current_depth > g.surface_depth/100.0);
- }
- if (motors.limit.throttle_lower && vel_stationary) {
-
- if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
- bottom_detector_count++;
- } else {
- set_bottomed(true);
- }
- } else {
- set_bottomed(false);
- }
-
- } else if (vel_stationary) {
- if (motors.limit.throttle_upper) {
-
- if (surface_detector_count < ((float)SURFACE_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
- surface_detector_count++;
- } else {
- set_surfaced(true);
- }
- } else if (motors.limit.throttle_lower) {
-
- if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
- bottom_detector_count++;
- } else {
- set_bottomed(true);
- }
- } else {
- set_surfaced(false);
- set_bottomed(false);
- }
- } else {
- set_surfaced(false);
- set_bottomed(false);
- }
- }
- void Sub::set_surfaced(bool at_surface)
- {
- if (ap.at_surface == at_surface) {
- return;
- }
- ap.at_surface = at_surface;
- surface_detector_count = 0;
- if (ap.at_surface) {
- Log_Write_Event(DATA_SURFACED);
- } else {
- Log_Write_Event(DATA_NOT_SURFACED);
- }
- }
- void Sub::set_bottomed(bool at_bottom)
- {
- if (ap.at_bottom == at_bottom) {
- return;
- }
- ap.at_bottom = at_bottom;
- bottom_detector_count = 0;
- if (ap.at_bottom) {
- Log_Write_Event(DATA_BOTTOMED);
- } else {
- Log_Write_Event(DATA_NOT_BOTTOMED);
- }
- }
|