surface_bottom_detector.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Jacob Walser: jacob@bluerobotics.com
  2. #include "Sub.h"
  3. uint8_t maxthree(float x1,float x2,float x3);
  4. // counter to verify contact with bottom
  5. static uint32_t bottom_detector_count = 0;
  6. static uint32_t surface_detector_count = 0;
  7. static float current_depth = 0;
  8. bool bottomdetect = FALSE;
  9. bool setZ = TRUE;
  10. // checks if we have have hit bottom or surface and updates the ap.at_bottom and ap.at_surface flags
  11. // called at MAIN_LOOP_RATE
  12. // ToDo: doesn't need to be called this fast
  13. uint8_t maxthree(float x1,float x2,float x3){
  14. if(fabsf(x1)>fabsf(x2) || fabsf(fabsf(x1)-fabsf(x2))<0.0000001){//x1>=x3
  15. if(fabsf(x1)>fabsf(x3) || fabsf(fabsf(x1)-fabsf(x3))<0.0000001){//x1>=x3
  16. return 1;
  17. }else{//x3>x1
  18. return 3;
  19. }
  20. }
  21. if(fabsf(x2)>fabsf(x1) || fabsf(fabsf(x2)-fabsf(x1))<0.0000001){//x2>=x1
  22. if(fabsf(x2)>fabsf(x3) || fabsf(fabsf(x2)-fabsf(x3))<0.0000001){//x2>=x3
  23. return 2;
  24. }else{//x3>=x2
  25. return 3;
  26. }
  27. }
  28. return 0;
  29. }
  30. void Sub::bottom_detectorgain(void){
  31. float velocity_z = pos_control.alt_rate.get()/100;// m/s
  32. bool vel_stationary = velocity_z > -0.05 && velocity_z < 0.05;
  33. Matrix3f mat_body = ahrs.get_rotation_body_to_ned();
  34. Vector3f Xbx = mat_body*Vector3f(1,0,0);
  35. Vector3f Xby = mat_body*Vector3f(0,1,0);
  36. Vector3f Xbz = mat_body*Vector3f(0,0,1);
  37. float dot1=Xbx*Vector3f(0,0,1);
  38. float dot2=Xby*Vector3f(0,0,1);
  39. float dot3=Xbz*Vector3f(0,0,1);
  40. const AP_Motors6DOF &motors6dof = AP::motors6dof();//6自由度电机计算出来的PWM
  41. static uint16_t countx=0;
  42. static uint16_t county=0;
  43. static uint16_t countz=0;
  44. Vector3f velocity;
  45. velocity.z = pos_control.alt_rate.get()/100;//12.20 cm
  46. // check that we are not moving up or down
  47. if (vel_stationary)//速度很小
  48. {
  49. if(maxthree(dot1,dot2,dot3)==1) {//x轴
  50. float x1 = (float)(motors6dof.motor_to_detect[0] - NETRULPWM)/(NETRULPWM-1000) ;
  51. float x2 = (float)(motors6dof.motor_to_detect[1] - NETRULPWM)/(NETRULPWM-1000) ;
  52. if (fabsf(x1)>0.95 && fabsf(x2)>0.95)
  53. {
  54. countz=0;
  55. county=0;
  56. if (countx < 2*MAIN_LOOP_RATE) {
  57. countx++;
  58. } else{
  59. bottom_set = TRUE;
  60. }
  61. }else{
  62. bottom_set = FALSE;
  63. countx = 0;
  64. }
  65. }
  66. else if (maxthree(dot1,dot2,dot3)==2 ){//y轴
  67. float l = (float)(motors6dof.motor_to_detect[5] - NETRULPWM)/(NETRULPWM-1000) ;
  68. countx=0;
  69. countz=0;
  70. if (fabsf(l)>0.9 )
  71. {
  72. if (county < 2*MAIN_LOOP_RATE) {
  73. county++;
  74. } else{
  75. bottom_set = TRUE;
  76. }
  77. }else{
  78. bottom_set = FALSE;
  79. county = 0;
  80. }
  81. }
  82. else if (maxthree(dot1,dot2,dot3)==3 ){//z轴
  83. countx=0;
  84. county=0;
  85. float z1 = (float)(motors6dof.motor_to_detect[2] - NETRULPWM)/(NETRULPWM-1000) ;
  86. float z2 = (float)(motors6dof.motor_to_detect[3] - NETRULPWM)/(NETRULPWM-1000) ;
  87. //float z3 = (float)(motors6dof.motor_to_detect[4] - NETRULPWM)/(NETRULPWM-1000) ;
  88. if(fabsf(z1)>0.95 && fabsf(z2)>0.95){
  89. if (countz < 2*MAIN_LOOP_RATE) {
  90. countz++;
  91. } else{
  92. bottom_set = TRUE;
  93. }
  94. }else{
  95. bottom_set = FALSE;
  96. countz = 0;
  97. }
  98. }else{
  99. countx=0;
  100. county=0;
  101. countz=0;
  102. bottom_set = FALSE; //这里忽略了三个或者2个相等的情况
  103. }
  104. }
  105. else{
  106. countx=0;
  107. county=0;
  108. countz=0;
  109. bottom_set = FALSE; //速度比较大
  110. }
  111. }
  112. float Sub::surface_depth_use(void){
  113. Matrix3f mat_body = ahrs.get_rotation_body_to_ned();
  114. float surface_depth_use;
  115. if (abs(mat_body.c.x) <0.2)
  116. {
  117. surface_depth_use = g.surface_depth/100.0;
  118. }else if(fabsf(mat_body.c.x) <0.45){
  119. surface_depth_use = -0.2;//m
  120. }else if(fabsf(mat_body.c.x) <0.7){
  121. surface_depth_use = -0.3;//m
  122. }else{
  123. surface_depth_use = -0.45;//m
  124. }
  125. return surface_depth_use;
  126. }
  127. void Sub::update_surface_and_bottom_detector()
  128. {
  129. if (!motors.armed()) { // only update when armed
  130. set_surfaced(false);
  131. set_bottomed(false);
  132. return;
  133. }
  134. Vector3f velocity;
  135. //velocity.z = pos_control.alt_rate.get()/100;//12.20 cm
  136. velocity.z = velocity_z_filer/100;//03.24 cm
  137. // check that we are not moving up or down
  138. bool vel_stationary = velocity.z > -0.05 && velocity.z < 0.05;
  139. 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
  140. current_depth = barometer.get_altitude(); // cm
  141. if (ap.at_surface) {
  142. set_surfaced(current_depth > surface_depth_use() - 0.05); // add a 5cm buffer so it doesn't trigger too often
  143. } else {
  144. set_surfaced(current_depth > surface_depth_use()); // If we are above surface depth, we are surfaced
  145. }
  146. if (motors.limit.throttle_lower && vel_stationary){
  147. // bottom criteria met - increment the counter and check if we've triggered
  148. if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  149. bottom_detector_count++;
  150. } else {
  151. set_bottomed(true);//03.02
  152. }
  153. } else {
  154. set_bottomed(false);
  155. }
  156. // with no external baro, the only thing we have to go by is a vertical velocity estimate
  157. } else if (vel_stationary) {
  158. if (motors.limit.throttle_upper) {
  159. // surface criteria met, increment counter and see if we've triggered
  160. if (surface_detector_count < ((float)SURFACE_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  161. surface_detector_count++;
  162. } else {
  163. set_surfaced(true);
  164. }
  165. } else if (motors.limit.throttle_lower) {
  166. // bottom criteria met, increment counter and see if we've triggered
  167. if (bottom_detector_count < ((float)BOTTOM_DETECTOR_TRIGGER_SEC)*MAIN_LOOP_RATE) {
  168. bottom_detector_count++;
  169. } else {
  170. set_bottomed(true);//03.02
  171. }
  172. } else { // we're not at the limits of throttle, so reset both detectors
  173. set_surfaced(false);
  174. set_bottomed(false);
  175. }
  176. } else { // we're moving up or down, so reset both detectors
  177. set_surfaced(false);
  178. set_bottomed(false);
  179. }
  180. }
  181. void Sub::set_surfaced(bool at_surface)
  182. {
  183. if (ap.at_surface == at_surface) { // do nothing if state unchanged
  184. return;
  185. }
  186. ap.at_surface = at_surface;
  187. surface_detector_count = 0;
  188. if (ap.at_surface) {
  189. Log_Write_Event(DATA_SURFACED);
  190. } else {
  191. Log_Write_Event(DATA_NOT_SURFACED);
  192. }
  193. }
  194. void Sub::set_bottomed(bool at_bottom)
  195. {
  196. if (ap.at_bottom == at_bottom) { // do nothing if state unchanged
  197. return;
  198. }
  199. ap.at_bottom = at_bottom;
  200. bottom_detector_count = 0;
  201. if (ap.at_bottom) {
  202. Log_Write_Event(DATA_BOTTOMED);
  203. } else {
  204. Log_Write_Event(DATA_NOT_BOTTOMED);
  205. }
  206. }