control_sport.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "Sub.h"
  2. //#include "/AP_Math/AP_Math.h"
  3. extern mavlink_rov_state_monitoring_t rov_message;
  4. Quaternion attitude_desired_quat_;
  5. /*
  6. * control_althold.pde - init and run calls for althold, flight mode
  7. */
  8. // althold_init - initialise althold controller
  9. bool Sub::sport_init()
  10. {
  11. attitude_control.set_throttle_out(0.5 ,true, g.throttle_filt);
  12. //holding_depth = false;
  13. //检查是否有深度计存在
  14. if(!control_check_barometer()) {
  15. return false;
  16. }
  17. // initialize vertical speeds and leash lengths
  18. // sets the maximum speed up and down returned by position controller
  19. //get_pilot_speed_dn QGC PILOT 设置的值
  20. pos_control.set_max_speed_z(-get_pilot_speed_dn(), g.pilot_speed_up);//(-500,500)设置最大最小速度 和刹车长度
  21. pos_control.set_max_accel_z(g.pilot_accel_z);//设置最大加速度值 和刹车长度
  22. pos_control.relax_alt_hold_controllers();//目标高度为当前高度,目标速度和上次速度 当前速度
  23. //
  24. //pos_control.set_target_to_stopping_point_z();//刹车长度 根据当前速度估计目标深度
  25. pos_control.calc_leash_length_z();//刹车长度 12.19
  26. pos_control.set_alt_target(barometer.get_altitude()*100);//当前深度12.19
  27. holding_depth = true;
  28. ahrs.get_quat_body_to_ned(attitude_desired_quat_);//12.19
  29. last_roll = 0;
  30. last_pitch = 0;
  31. last_yaw = ahrs.yaw_sensor;
  32. last_input_ms = AP_HAL::millis();
  33. last_input_ms_stable = AP_HAL::millis();
  34. gcs().send_text(MAV_SEVERITY_INFO, " init Z target %f %f",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  35. return true;
  36. }
  37. void Sub::handle_attitude_sport(){
  38. uint32_t tnow = AP_HAL::millis();
  39. // get pilot desired lean angles
  40. float target_roll, target_pitch, target_yaw;
  41. // Check if set_attitude_target_no_gps is valid
  42. if (tnow - sub.set_attitude_target_no_gps.last_message_ms < 5000) {
  43. Quaternion(
  44. set_attitude_target_no_gps.packet.q
  45. ).to_euler(
  46. target_roll,
  47. target_pitch,
  48. target_yaw
  49. );
  50. target_roll = 100 * degrees(target_roll);
  51. target_pitch = 100 * degrees(target_pitch);
  52. target_yaw = 100 * degrees(target_yaw);
  53. last_roll = target_roll;
  54. last_pitch = target_pitch;
  55. last_yaw = target_yaw;
  56. attitude_control.input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, target_yaw, true);
  57. } else {
  58. // If we don't have a mavlink attitude target, we use the pilot's input instead
  59. //get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, attitude_control.get_althold_lean_angle_max());
  60. //get_pilot_desired_lean_angles((int16_t)(channel_lateral->norm_input()*5700), (int16_t)((0.5-channel_throttle->norm_input())*5700*2), target_roll, target_pitch, attitude_control.get_althold_lean_angle_max());
  61. get_pilot_desired_lean_angles(0, (int16_t)((0.5-channel_throttle->norm_input())*5700*2), target_roll, target_pitch, attitude_control.get_althold_lean_angle_max());
  62. target_yaw = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  63. if (abs(target_roll) > 300 || abs(target_pitch) > 300) {
  64. //last_roll = ahrs.roll_sensor;
  65. //last_pitch = ahrs.pitch_sensor;
  66. //last_yaw = ahrs.yaw_sensor;
  67. ahrs.get_quat_body_to_ned(attitude_desired_quat_);
  68. last_input_ms = tnow;
  69. attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
  70. } else if (abs(target_yaw) > 300) {
  71. // if only yaw is being controlled, don't update pitch and roll
  72. attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, target_yaw);
  73. //last_yaw = ahrs.yaw_sensor;
  74. ahrs.get_quat_body_to_ned(attitude_desired_quat_);
  75. last_input_ms = tnow;
  76. } else if (tnow < last_input_ms + 250) {
  77. // just brake for a few mooments so we don't bounce
  78. last_yaw = ahrs.yaw_sensor;
  79. attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, 0);
  80. } else {
  81. // Lock attitude
  82. //attitude_control.input_euler_angle_roll_pitch_yaw(last_roll, last_pitch, last_yaw, true);
  83. attitude_control.input_quaternion(attitude_desired_quat_);
  84. }
  85. }
  86. }
  87. // althold_run - runs the althold controller
  88. // should be called at 100hz or more
  89. void Sub::sport_run()
  90. {
  91. static int f2 = 201;
  92. static int f3 = 201;
  93. // When unarmed, disable motors and stabilization
  94. if (!motors.armed()) {
  95. f2 = 201;
  96. f3 = 201;
  97. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  98. // Sub vehicles do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)
  99. attitude_control.set_throttle_out(0.5 ,true, g.throttle_filt);
  100. attitude_control.relax_attitude_controllers();
  101. pos_control.relax_alt_hold_controllers();
  102. //pos_control.set_alt_target(barometer.get_altitude()*100);//当前深度
  103. last_roll = 0;
  104. last_pitch = 0;
  105. last_yaw = ahrs.yaw_sensor;
  106. holding_depth = false;
  107. ahrs.get_quat_body_to_ned(attitude_desired_quat_);//12.19
  108. static int f = 0;
  109. f++;
  110. if(f>800)
  111. {
  112. gcs().send_text(MAV_SEVERITY_INFO, " disarm Z target %f %f",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  113. f=0;
  114. }
  115. return;
  116. }
  117. // Vehicle is armed, motors are free to run
  118. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
  119. handle_attitude_sport();
  120. //handle_attitude();
  121. pos_control.update_z_controller();
  122. // Read the output of the z controller and rotate it so it always points up
  123. Vector3f throttle_vehicle_frame = ahrs.get_rotation_body_to_ned().transposed() * Vector3f(0, 0, motors.get_throttle_in_bidirectional());
  124. // Output the Z controller + pilot input to all motors.
  125. //TODO: scale throttle with the ammount of thrusters in the given direction
  126. // motors.set_throttle(0.5+throttle_vehicle_frame.z + channel_throttle->norm_input()-0.5);
  127. motors.set_throttle(throttle_vehicle_frame.z + 1.0-(float)PressLevel_f*0.1);
  128. rov_message.pressure_level = int(PressLevel);
  129. motors.set_forward(-throttle_vehicle_frame.x + channel_forward->norm_input());
  130. motors.set_lateral(-throttle_vehicle_frame.y + channel_lateral->norm_input());
  131. // We rotate the RC inputs to the earth frame to check if the user is giving an input that would change the depth.
  132. //Vector3f earth_frame_rc_inputs = ahrs.get_rotation_body_to_ned() * Vector3f(channel_forward->norm_input(), channel_lateral->norm_input(), (2.0f*(-0.5f+channel_throttle->norm_input())));
  133. Vector3f earth_frame_rc_inputs = ahrs.get_rotation_body_to_ned() * Vector3f(channel_forward->norm_input(), channel_lateral->norm_input(), (2.0*(0.5-PressLevel_f*0.1)));
  134. if (fabsf(earth_frame_rc_inputs.z) > 0.05f) { // Throttle input above 5%
  135. // reset z targets to current values
  136. holding_depth = false;
  137. pos_control.relax_alt_hold_controllers();
  138. //pos_control.set_alt_target(barometer.get_altitude()*100);//当前深度
  139. static int f = 0;
  140. f2 = 201;
  141. f3 = 201;
  142. f++;
  143. if(f>400)
  144. {
  145. gcs().send_text(MAV_SEVERITY_INFO, " Z1 target %f %f ",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  146. gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
  147. f=0;
  148. }
  149. } else { // hold z
  150. if (ap.at_surface) { //最大油门不能向上动
  151. pos_control.set_alt_target(g.surface_depth - 10.0f); // set target to 5cm below surface level
  152. holding_depth = true;
  153. f2++;
  154. if(f2>200)
  155. {
  156. gcs().send_text(MAV_SEVERITY_INFO, " Zsurface target %f %f ",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  157. gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
  158. f2=0;
  159. }
  160. } else if (ap.at_bottom) {//最大油门不能向下动
  161. //pos_control.set_alt_target(inertial_nav.get_altitude() + 20.0f); // set target to 10 cm above bottom
  162. pos_control.set_alt_target(barometer.get_altitude()*100 +20);//cm
  163. holding_depth = true;
  164. f3++;
  165. if(f3>200)
  166. {
  167. gcs().send_text(MAV_SEVERITY_INFO, " Zbottom target %f %f ",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  168. gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
  169. f3=0;
  170. }
  171. } else if (!holding_depth) {
  172. //pos_control.set_target_to_stopping_point_z();//有对位置的付值
  173. pos_control.calc_leash_length_z();//刹车长度 12.19
  174. pos_control.set_alt_target(barometer.get_altitude()*100);//cm 当前深度
  175. f2 = 201;
  176. f3 = 201;
  177. holding_depth = true;
  178. gcs().send_text(MAV_SEVERITY_INFO, " Z4 target %f %f ",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  179. gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
  180. }
  181. }
  182. static int f5 = 0;
  183. f5++;
  184. if(f5>500)
  185. {
  186. gcs().send_text(MAV_SEVERITY_INFO, " Z00 target %f %f ",(float)pos_control.get_pos_target().z,barometer.get_altitude()*100);
  187. gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
  188. f5=0;
  189. }
  190. }