2017-02-03 00:18:27 -04:00
|
|
|
// ArduSub position hold flight mode
|
|
|
|
// GPS required
|
|
|
|
// Jacob Walser August 2016
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
#if POSHOLD_ENABLED == ENABLED
|
|
|
|
|
|
|
|
// poshold_init - initialise PosHold controller
|
2023-04-03 09:11:21 -03:00
|
|
|
bool ModePoshold::init(bool ignore_checks)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2017-02-03 17:33:27 -04:00
|
|
|
// fail to initialise PosHold mode if no GPS lock
|
2023-04-03 09:11:21 -03:00
|
|
|
if (!sub.position_ok()) {
|
2015-12-30 18:57:56 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-03 00:18:27 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialize vertical speeds and acceleration
|
2023-04-03 09:11:21 -03:00
|
|
|
position_control->set_max_speed_accel_xy(sub.wp_nav.get_default_speed_xy(), sub.wp_nav.get_wp_acceleration());
|
|
|
|
position_control->set_correction_speed_accel_xy(sub.wp_nav.get_default_speed_xy(), sub.wp_nav.get_wp_acceleration());
|
|
|
|
position_control->set_max_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
|
|
|
|
position_control->set_correction_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// initialise position and desired velocity
|
2023-04-03 09:11:21 -03:00
|
|
|
position_control->init_xy_controller_stopping_point();
|
|
|
|
position_control->init_z_controller();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2020-08-03 00:32:30 -03:00
|
|
|
// Stop all thrusters
|
2023-04-03 09:11:21 -03:00
|
|
|
attitude_control->set_throttle_out(0.5f ,true, g.throttle_filt);
|
|
|
|
attitude_control->relax_attitude_controllers();
|
|
|
|
position_control->relax_z_controller(0.5f);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2023-04-03 09:11:21 -03:00
|
|
|
sub.last_pilot_heading = ahrs.yaw_sensor;
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// poshold_run - runs the PosHold controller
|
|
|
|
// should be called at 100hz or more
|
2023-04-03 09:11:21 -03:00
|
|
|
void ModePoshold::run()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2017-02-03 17:33:27 -04:00
|
|
|
uint32_t tnow = AP_HAL::millis();
|
2020-08-03 00:32:30 -03:00
|
|
|
// When unarmed, disable motors and stabilization
|
2017-04-13 16:34:58 -03:00
|
|
|
if (!motors.armed()) {
|
2019-04-09 20:09:55 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
2020-08-03 00:32:30 -03:00
|
|
|
// Sub vehicles do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)
|
2023-04-03 09:11:21 -03:00
|
|
|
attitude_control->set_throttle_out(0.5f ,true, g.throttle_filt);
|
|
|
|
attitude_control->relax_attitude_controllers();
|
|
|
|
position_control->init_xy_controller_stopping_point();
|
|
|
|
position_control->relax_z_controller(0.5f);
|
|
|
|
sub.last_pilot_heading = ahrs.yaw_sensor;
|
2015-12-30 18:57:56 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
// set motors to full range
|
2019-04-09 20:09:55 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
// update xy outputs //
|
2017-03-22 17:02:00 -03:00
|
|
|
float pilot_lateral = channel_lateral->norm_input();
|
|
|
|
float pilot_forward = channel_forward->norm_input();
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
float lateral_out = 0;
|
|
|
|
float forward_out = 0;
|
|
|
|
|
2023-04-03 09:11:21 -03:00
|
|
|
if (sub.position_ok()) {
|
2020-08-03 00:32:30 -03:00
|
|
|
// Allow pilot to reposition the sub
|
|
|
|
if (fabsf(pilot_lateral) > 0.1 || fabsf(pilot_forward) > 0.1) {
|
2023-04-03 09:11:21 -03:00
|
|
|
position_control->init_xy_controller_stopping_point();
|
2020-08-03 00:32:30 -03:00
|
|
|
}
|
2023-04-03 09:11:21 -03:00
|
|
|
sub.translate_pos_control_rp(lateral_out, forward_out);
|
|
|
|
position_control->update_xy_controller();
|
2017-02-03 17:33:27 -04:00
|
|
|
} else {
|
2023-04-03 09:11:21 -03:00
|
|
|
position_control->init_xy_controller_stopping_point();
|
2017-02-03 17:33:27 -04:00
|
|
|
}
|
2020-08-03 00:32:30 -03:00
|
|
|
motors.set_forward(forward_out + pilot_forward);
|
|
|
|
motors.set_lateral(lateral_out + pilot_lateral);
|
2017-02-03 17:33:27 -04:00
|
|
|
/////////////////////
|
|
|
|
// Update attitude //
|
|
|
|
|
|
|
|
// get pilot's desired yaw rate
|
2023-10-31 16:49:06 -03:00
|
|
|
float yaw_input = channel_yaw->pwm_to_angle_dz_trim(channel_yaw->get_dead_zone() * sub.gain, channel_yaw->get_radio_trim());
|
|
|
|
float target_yaw_rate = sub.get_pilot_desired_yaw_rate(yaw_input);
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
// convert pilot input to lean angles
|
|
|
|
// To-Do: convert get_pilot_desired_lean_angles to return angles as floats
|
|
|
|
float target_roll, target_pitch;
|
2023-04-03 09:11:21 -03:00
|
|
|
sub.get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, sub.aparm.angle_max);
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
// update attitude controller targets
|
|
|
|
if (!is_zero(target_yaw_rate)) { // call attitude controller with rate yaw determined by pilot input
|
2023-04-03 09:11:21 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
|
|
|
|
sub.last_pilot_heading = ahrs.yaw_sensor;
|
|
|
|
sub.last_pilot_yaw_input_ms = tnow; // time when pilot last changed heading
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
} else { // hold current heading
|
|
|
|
|
2023-10-11 04:41:54 -03:00
|
|
|
// this check is required to prevent bounce back after very fast yaw manoeuvres
|
2017-02-03 17:33:27 -04:00
|
|
|
// the inertia of the vehicle causes the heading to move slightly past the point when pilot input actually stopped
|
2023-04-03 09:11:21 -03:00
|
|
|
if (tnow < sub.last_pilot_yaw_input_ms + 250) { // give 250ms to slow down, then set target heading
|
2017-02-03 17:33:27 -04:00
|
|
|
target_yaw_rate = 0; // Stop rotation on yaw axis
|
|
|
|
|
|
|
|
// call attitude controller with target yaw rate = 0 to decelerate on yaw axis
|
2023-04-03 09:11:21 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
|
|
|
|
sub.last_pilot_heading = ahrs.yaw_sensor; // update heading to hold
|
2017-02-03 17:33:27 -04:00
|
|
|
|
|
|
|
} else { // call attitude controller holding absolute absolute bearing
|
2023-04-03 09:11:21 -03:00
|
|
|
attitude_control->input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, sub.last_pilot_heading, true);
|
2017-02-03 17:33:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update z axis //
|
2020-08-03 00:32:30 -03:00
|
|
|
control_depth();
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
#endif // POSHOLD_ENABLED == ENABLED
|