control_surface.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "Sub.h"
  2. bool Sub::surface_init()
  3. {
  4. if(!control_check_barometer()) {
  5. return false;
  6. }
  7. // initialize vertical speeds and leash lengths
  8. pos_control.set_max_speed_z(wp_nav.get_default_speed_down(), wp_nav.get_default_speed_up());
  9. pos_control.set_max_accel_z(wp_nav.get_accel_z());
  10. // initialise position and desired velocity
  11. pos_control.set_alt_target(inertial_nav.get_altitude());
  12. pos_control.set_desired_velocity_z(inertial_nav.get_velocity_z());
  13. return true;
  14. }
  15. void Sub::surface_run()
  16. {
  17. float target_roll, target_pitch;
  18. float target_yaw_rate;
  19. // if not armed set throttle to zero and exit immediately
  20. if (!motors.armed()) {
  21. motors.output_min();
  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. // Already at surface, hold depth at surface
  28. if (ap.at_surface) {
  29. set_mode(ALT_HOLD, MODE_REASON_SURFACE_COMPLETE);
  30. }
  31. // convert pilot input to lean angles
  32. // To-Do: convert get_pilot_desired_lean_angles to return angles as floats
  33. get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, aparm.angle_max);
  34. // get pilot's desired yaw rate
  35. target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  36. // call attitude controller
  37. attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
  38. // set target climb rate
  39. float cmb_rate = constrain_float(abs(wp_nav.get_default_speed_up()), 1, pos_control.get_max_speed_up());
  40. // record desired climb rate for logging
  41. desired_climb_rate = cmb_rate;
  42. // update altitude target and call position controller
  43. pos_control.set_alt_target_from_climb_rate_ff(cmb_rate, G_Dt, true);
  44. pos_control.update_z_controller();
  45. // pilot has control for repositioning
  46. motors.set_forward(channel_forward->norm_input());
  47. motors.set_lateral(channel_lateral->norm_input());
  48. }