control_sport_.bak 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "Sub.h"
  2. extern mavlink_rov_state_monitoring_t rov_message;
  3. // stabilize_init - initialise stabilize controller
  4. bool Sub::sport_init()
  5. {
  6. // set target altitude to zero for reporting
  7. pos_control.set_alt_target(0);
  8. if (prev_control_mode == ALT_HOLD) {
  9. last_roll = ahrs.roll_sensor;
  10. last_pitch = ahrs.pitch_sensor;
  11. } else {
  12. last_roll = 0;
  13. last_pitch = 0;
  14. }
  15. last_yaw = ahrs.yaw_sensor;
  16. pitch_input_inc=0;
  17. yaw_press = (int16_t)(ahrs.yaw_sensor/100);//记住方位
  18. last_input_ms = AP_HAL::millis();
  19. return true;
  20. }
  21. void Sub::handle_attitude_sport(){
  22. uint32_t tnow = AP_HAL::millis();
  23. // get pilot desired lean angles
  24. float target_roll, target_pitch, target_yaw;
  25. // Check if set_attitude_target_no_gps is valid
  26. if (tnow - sub.set_attitude_target_no_gps.last_message_ms < 5000) {
  27. Quaternion(
  28. set_attitude_target_no_gps.packet.q
  29. ).to_euler(
  30. target_roll,
  31. target_pitch,
  32. target_yaw
  33. );
  34. target_roll = 100 * degrees(target_roll);
  35. target_pitch = 100 * degrees(target_pitch);
  36. target_yaw = 100 * degrees(target_yaw);
  37. last_roll = target_roll;
  38. last_pitch = target_pitch;
  39. last_yaw = target_yaw;
  40. attitude_control.input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, target_yaw, true);
  41. } else {
  42. // If we don't have a mavlink attitude target, we use the pilot's input instead
  43. //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());
  44. //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());
  45. 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());
  46. target_yaw = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  47. if (abs(target_roll) > 300 || abs(target_pitch) > 300) {
  48. last_roll = ahrs.roll_sensor;
  49. last_pitch = ahrs.pitch_sensor;
  50. last_yaw = ahrs.yaw_sensor;
  51. last_input_ms = tnow;
  52. attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
  53. } else if (abs(target_yaw) > 300) {
  54. // if only yaw is being controlled, don't update pitch and roll
  55. attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, target_yaw);
  56. last_yaw = ahrs.yaw_sensor;
  57. last_input_ms = tnow;
  58. } else if (tnow < last_input_ms + 250) {
  59. // just brake for a few mooments so we don't bounce
  60. last_yaw = ahrs.yaw_sensor;
  61. attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, 0);
  62. } else {
  63. // Lock attitude
  64. attitude_control.input_euler_angle_roll_pitch_yaw(last_roll, last_pitch, last_yaw, true);
  65. }
  66. }
  67. }
  68. // stabilize_run - runs the main stabilize controller
  69. // should be called at 100hz or more
  70. void Sub::sport_run()
  71. {
  72. // if not armed set throttle to zero and exit immediately
  73. if (!motors.armed()) {
  74. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  75. attitude_control.set_throttle_out(0,true,g.throttle_filt);
  76. attitude_control.relax_attitude_controllers();
  77. last_roll = 0;
  78. last_pitch = 0;
  79. last_yaw = ahrs.yaw_sensor;
  80. yaw_press = (int16_t)(ahrs.yaw_sensor/100);//记住方位
  81. return;
  82. }
  83. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
  84. handle_attitude_sport();
  85. // output pilot's throttle
  86. //attitude_control.set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);
  87. attitude_control.set_throttle_out(1.0-(float)PressLevel_f*0.1, false, g.throttle_filt);//压力分级
  88. rov_message.pressure_level = int(PressLevel);
  89. //control_in is range -1000-1000
  90. //radio_in is raw pwm value
  91. motors.set_forward(channel_forward->norm_input());
  92. motors.set_lateral(channel_lateral->norm_input());
  93. //motors.set_lateral(0);
  94. static int j = 0;
  95. j++;
  96. if(j>800)
  97. {
  98. gcs().send_text(MAV_SEVERITY_INFO, " roll p y %d %d %d\n",(int16_t)(channel_lateral->norm_input()*5700),(int16_t)((0.5-channel_throttle->norm_input())*5700*2),channel_yaw->get_control_in());
  99. //gcs().send_text(MAV_SEVERITY_INFO, " throttle forward lateral %f %f %f \n",(float)PressLevel_f*0.1,channel_forward->norm_input());
  100. //gcs().send_text(MAV_SEVERITY_INFO, " channel_roll p y %d %d %d\n",channel_roll->get_control_in(),channel_pitch->get_control_in(),channel_yaw->get_control_in());
  101. //gcs().send_text(MAV_SEVERITY_INFO, " throttle forward lateral %f %f %f \n",channel_throttle->norm_input(),channel_forward->norm_input(),channel_lateral->norm_input());
  102. j=0;
  103. }
  104. }