control_circle.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "Sub.h"
  2. /*
  3. * control_circle.pde - init and run calls for circle flight mode
  4. */
  5. // circle_init - initialise circle controller flight mode
  6. bool Sub::circle_init()
  7. {
  8. if (!position_ok()) {
  9. return false;
  10. }
  11. circle_pilot_yaw_override = false;
  12. // initialize speeds and accelerations
  13. pos_control.set_max_speed_xy(wp_nav.get_default_speed_xy());
  14. pos_control.set_max_accel_xy(wp_nav.get_wp_acceleration());
  15. pos_control.set_max_speed_z(-get_pilot_speed_dn(), g.pilot_speed_up);
  16. pos_control.set_max_accel_z(g.pilot_accel_z);
  17. // initialise circle controller including setting the circle center based on vehicle speed
  18. circle_nav.init();
  19. return true;
  20. }
  21. // circle_run - runs the circle flight mode
  22. // should be called at 100hz or more
  23. void Sub::circle_run()
  24. {
  25. float target_yaw_rate = 0;
  26. float target_climb_rate = 0;
  27. // update parameters, to allow changing at runtime
  28. pos_control.set_max_speed_xy(wp_nav.get_default_speed_xy());
  29. pos_control.set_max_accel_xy(wp_nav.get_wp_acceleration());
  30. pos_control.set_max_speed_z(-get_pilot_speed_dn(), g.pilot_speed_up);
  31. pos_control.set_max_accel_z(g.pilot_accel_z);
  32. // if not armed set throttle to zero and exit immediately
  33. if (!motors.armed()) {
  34. // To-Do: add some initialisation of position controllers
  35. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  36. // Sub vehicles do not stabilize roll/pitch/yaw when disarmed
  37. attitude_control.set_throttle_out(0,true,g.throttle_filt);
  38. attitude_control.relax_attitude_controllers();
  39. pos_control.set_alt_target_to_current_alt();
  40. return;
  41. }
  42. // process pilot inputs
  43. // get pilot's desired yaw rate
  44. target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  45. if (!is_zero(target_yaw_rate)) {
  46. circle_pilot_yaw_override = true;
  47. }
  48. // get pilot desired climb rate
  49. target_climb_rate = get_pilot_desired_climb_rate(channel_throttle->get_control_in());
  50. // set motors to full range
  51. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
  52. // run circle controller
  53. circle_nav.update();
  54. ///////////////////////
  55. // update xy outputs //
  56. float lateral_out, forward_out;
  57. translate_circle_nav_rp(lateral_out, forward_out);
  58. // Send to forward/lateral outputs
  59. motors.set_lateral(lateral_out);
  60. motors.set_forward(forward_out);
  61. // call attitude controller
  62. if (circle_pilot_yaw_override) {
  63. attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_yaw_rate);
  64. } else {
  65. attitude_control.input_euler_angle_roll_pitch_yaw(channel_roll->get_control_in(), channel_pitch->get_control_in(), circle_nav.get_yaw(), true);
  66. }
  67. // update altitude target and call position controller
  68. pos_control.set_alt_target_from_climb_rate(target_climb_rate, G_Dt, false);
  69. pos_control.update_z_controller();
  70. }