surface_bottom_detector.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Jacob Walser: jacob@bluerobotics.com
  2. #include "Sub.h"
  3. // counter to verify contact with bottom
  4. static uint32_t bottom_detector_count = 0;
  5. static uint32_t surface_detector_count = 0;
  6. static float current_depth = 0;
  7. // checks if we have have hit bottom or surface and updates the ap.at_bottom and ap.at_surface flags
  8. // called at MAIN_LOOP_RATE
  9. // ToDo: doesn't need to be called this fast
  10. void Sub::update_surface_and_bottom_detector()
  11. {
  12. static float last_alt = 0.0;//12.20
  13. if (!motors.armed()) { // only update when armed
  14. set_surfaced(false);
  15. set_bottomed(false);
  16. last_alt = barometer.get_altitude();
  17. return;
  18. }
  19. Vector3f velocity;
  20. //ahrs.get_velocity_NED(velocity);//12.20
  21. float cur_alt = barometer.get_altitude(); // m
  22. float vel = AC_AttitudeControl::sqrt_controller(cur_alt-last_alt, pos_control._p_pos_z.kP(), pos_control._accel_z_cms/100, pos_control._dt);
  23. velocity.z = vel;//12.20
  24. static int f = 0;
  25. f++;
  26. if(f>600)
  27. {
  28. gcs().send_text(MAV_SEVERITY_INFO, " velocity.z %f %f %f ",cur_alt,last_alt,velocity.z);
  29. f=0;
  30. }
  31. static uint16_t i=0;
  32. uint16_t timecnt = (uint16_t)(0.25/MAIN_LOOP_SECONDS);
  33. i++;
  34. if (i>timecnt)
  35. {
  36. i = 0;
  37. last_alt = cur_alt;//12.19
  38. }
  39. // check that we are not moving up or down
  40. bool vel_stationary = velocity.z > -0.05 && velocity.z < 0.05;
  41. if (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 position
  42. current_depth = barometer.get_altitude(); // cm
  43. if (ap.at_surface) {
  44. set_surfaced(current_depth > g.surface_depth/100.0 - 0.05); // add a 5cm buffer so it doesn't trigger too often
  45. } else {
  46. set_surfaced(current_depth > g.surface_depth/100.0); // If we are above surface depth, we are surfaced
  47. }
  48. if (motors.limit.throttle_lower && vel_stationary) {
  49. // bottom criteria met - increment the counter and check if we've triggered
  50. if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  51. bottom_detector_count++;
  52. } else {
  53. set_bottomed(true);
  54. }
  55. } else {
  56. set_bottomed(false);
  57. }
  58. // with no external baro, the only thing we have to go by is a vertical velocity estimate
  59. } else if (vel_stationary) {
  60. if (motors.limit.throttle_upper) {
  61. // surface criteria met, increment counter and see if we've triggered
  62. if (surface_detector_count < ((float)SURFACE_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  63. surface_detector_count++;
  64. } else {
  65. set_surfaced(true);
  66. }
  67. } else if (motors.limit.throttle_lower) {
  68. // bottom criteria met, increment counter and see if we've triggered
  69. if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  70. bottom_detector_count++;
  71. } else {
  72. set_bottomed(true);
  73. }
  74. } else { // we're not at the limits of throttle, so reset both detectors
  75. set_surfaced(false);
  76. set_bottomed(false);
  77. }
  78. } else { // we're moving up or down, so reset both detectors
  79. set_surfaced(false);
  80. set_bottomed(false);
  81. }
  82. }
  83. void Sub::set_surfaced(bool at_surface)
  84. {
  85. if (ap.at_surface == at_surface) { // do nothing if state unchanged
  86. return;
  87. }
  88. ap.at_surface = at_surface;
  89. surface_detector_count = 0;
  90. if (ap.at_surface) {
  91. Log_Write_Event(DATA_SURFACED);
  92. } else {
  93. Log_Write_Event(DATA_NOT_SURFACED);
  94. }
  95. }
  96. void Sub::set_bottomed(bool at_bottom)
  97. {
  98. if (ap.at_bottom == at_bottom) { // do nothing if state unchanged
  99. return;
  100. }
  101. ap.at_bottom = at_bottom;
  102. bottom_detector_count = 0;
  103. if (ap.at_bottom) {
  104. Log_Write_Event(DATA_BOTTOMED);
  105. } else {
  106. Log_Write_Event(DATA_NOT_BOTTOMED);
  107. }
  108. }