control_surface.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. gcs().send_text(MAV_SEVERITY_INFO, " surfacemode target %f %f %f",(float)pos_control.get_pos_target().z,pos_control.get_alt(),barometer.get_altitude());
  14. return true;
  15. }
  16. void Sub::surface_run()
  17. {
  18. float target_roll, target_pitch;
  19. float target_yaw_rate;
  20. // if not armed set throttle to zero and exit immediately
  21. if (!motors.armed()) {
  22. motors.output_min();
  23. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  24. attitude_control.set_throttle_out(0,true,g.throttle_filt);
  25. attitude_control.relax_attitude_controllers();
  26. return;
  27. }
  28. // Already at surface, hold depth at surface
  29. if (ap.at_surface) {
  30. set_mode(ALT_HOLD, MODE_REASON_SURFACE_COMPLETE);
  31. }
  32. // convert pilot input to lean angles
  33. // To-Do: convert get_pilot_desired_lean_angles to return angles as floats
  34. get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, aparm.angle_max);
  35. // get pilot's desired yaw rate
  36. target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
  37. // call attitude controller
  38. attitude_control.input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
  39. // set target climb rate
  40. float cmb_rate = constrain_float(abs(wp_nav.get_default_speed_up()), 1, pos_control.get_max_speed_up());
  41. // record desired climb rate for logging
  42. desired_climb_rate = cmb_rate;
  43. // update altitude target and call position controller
  44. pos_control.set_alt_target_from_climb_rate_ff(cmb_rate, G_Dt, true);
  45. pos_control.update_z_controller();
  46. // pilot has control for repositioning
  47. motors.set_forward(channel_forward->norm_input());
  48. motors.set_lateral(channel_lateral->norm_input());
  49. }