control_acro.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "Sub.h"
  2. /*
  3. * control_acro.pde - init and run calls for acro flight mode
  4. */
  5. // acro_init - initialise acro controller
  6. bool Sub::acro_init()
  7. {
  8. // set target altitude to zero for reporting
  9. pos_control.set_alt_target(0);
  10. // attitude hold inputs become thrust inputs in acro mode
  11. // set to neutral to prevent chaotic behavior (esp. roll/pitch)
  12. set_neutral_controls();
  13. return true;
  14. }
  15. // acro_run - runs the acro controller
  16. // should be called at 100hz or more
  17. void Sub::acro_run()
  18. {
  19. float target_roll, target_pitch, target_yaw;
  20. // if not armed set throttle to zero and exit immediately
  21. if (!motors.armed()) {
  22. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  23. attitude_control.set_throttle_out(0,true,g.throttle_filt);
  24. attitude_control.relax_attitude_controllers();
  25. return;
  26. }
  27. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
  28. // convert the input to the desired body frame rate
  29. get_pilot_desired_angle_rates(channel_roll->get_control_in(), channel_pitch->get_control_in(), channel_yaw->get_control_in(), target_roll, target_pitch, target_yaw);
  30. // run attitude controller
  31. attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
  32. // output pilot's throttle without angle boost
  33. attitude_control.set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);
  34. //control_in is range 0-1000
  35. //radio_in is raw pwm value
  36. motors.set_forward(channel_forward->norm_input());
  37. motors.set_lateral(channel_lateral->norm_input());
  38. }
  39. // get_pilot_desired_angle_rates - transform pilot's roll pitch and yaw input into a desired lean angle rates
  40. // returns desired angle rates in centi-degrees-per-second
  41. void Sub::get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out)
  42. {
  43. float rate_limit;
  44. Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
  45. // apply circular limit to pitch and roll inputs
  46. float total_in = norm(pitch_in, roll_in);
  47. if (total_in > ROLL_PITCH_INPUT_MAX) {
  48. float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
  49. roll_in *= ratio;
  50. pitch_in *= ratio;
  51. }
  52. // calculate roll, pitch rate requests
  53. if (g.acro_expo <= 0) {
  54. rate_bf_request.x = roll_in * g.acro_rp_p;
  55. rate_bf_request.y = pitch_in * g.acro_rp_p;
  56. } else {
  57. // expo variables
  58. float rp_in, rp_in3, rp_out;
  59. // range check expo
  60. if (g.acro_expo > 1.0f) {
  61. g.acro_expo = 1.0f;
  62. }
  63. // roll expo
  64. rp_in = float(roll_in)/ROLL_PITCH_INPUT_MAX;
  65. rp_in3 = rp_in*rp_in*rp_in;
  66. rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
  67. rate_bf_request.x = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
  68. // pitch expo
  69. rp_in = float(pitch_in)/ROLL_PITCH_INPUT_MAX;
  70. rp_in3 = rp_in*rp_in*rp_in;
  71. rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
  72. rate_bf_request.y = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
  73. }
  74. // calculate yaw rate request
  75. rate_bf_request.z = yaw_in * g.acro_yaw_p;
  76. // calculate earth frame rate corrections to pull the vehicle back to level while in ACRO mode
  77. if (g.acro_trainer != ACRO_TRAINER_DISABLED) {
  78. // Calculate trainer mode earth frame rate command for roll
  79. int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
  80. rate_ef_level.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;
  81. // Calculate trainer mode earth frame rate command for pitch
  82. int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
  83. rate_ef_level.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;
  84. // Calculate trainer mode earth frame rate command for yaw
  85. rate_ef_level.z = 0;
  86. // Calculate angle limiting earth frame rate commands
  87. if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
  88. if (roll_angle > aparm.angle_max) {
  89. rate_ef_level.x -= g.acro_balance_roll*(roll_angle-aparm.angle_max);
  90. } else if (roll_angle < -aparm.angle_max) {
  91. rate_ef_level.x -= g.acro_balance_roll*(roll_angle+aparm.angle_max);
  92. }
  93. if (pitch_angle > aparm.angle_max) {
  94. rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-aparm.angle_max);
  95. } else if (pitch_angle < -aparm.angle_max) {
  96. rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+aparm.angle_max);
  97. }
  98. }
  99. // convert earth-frame level rates to body-frame level rates
  100. attitude_control.euler_rate_to_ang_vel(attitude_control.get_att_target_euler_cd()*radians(0.01f), rate_ef_level, rate_bf_level);
  101. // combine earth frame rate corrections with rate requests
  102. if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
  103. rate_bf_request.x += rate_bf_level.x;
  104. rate_bf_request.y += rate_bf_level.y;
  105. rate_bf_request.z += rate_bf_level.z;
  106. } else {
  107. float acro_level_mix = constrain_float(1-MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0, 0, 1)*ahrs.cos_pitch();
  108. // Scale leveling rates by stick input
  109. rate_bf_level = rate_bf_level*acro_level_mix;
  110. // Calculate rate limit to prevent change of rate through inverted
  111. rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
  112. rate_bf_request.x += rate_bf_level.x;
  113. rate_bf_request.x = constrain_float(rate_bf_request.x, -rate_limit, rate_limit);
  114. // Calculate rate limit to prevent change of rate through inverted
  115. rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
  116. rate_bf_request.y += rate_bf_level.y;
  117. rate_bf_request.y = constrain_float(rate_bf_request.y, -rate_limit, rate_limit);
  118. // Calculate rate limit to prevent change of rate through inverted
  119. rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
  120. rate_bf_request.z += rate_bf_level.z;
  121. rate_bf_request.z = constrain_float(rate_bf_request.z, -rate_limit, rate_limit);
  122. }
  123. }
  124. // hand back rate request
  125. roll_out = rate_bf_request.x;
  126. pitch_out = rate_bf_request.y;
  127. yaw_out = rate_bf_request.z;
  128. }