2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* control_althold.pde - init and run calls for althold, flight mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
// althold_init - initialise althold controller
|
2017-04-14 16:10:29 -03:00
|
|
|
bool Sub::althold_init()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2018-04-14 00:16:57 -03:00
|
|
|
if(!control_check_barometer()) {
|
2017-02-03 17:33:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-24 18:50:20 -03:00
|
|
|
|
2021-04-27 03:50:06 -03:00
|
|
|
// initialize vertical maximum speeds and acceleration
|
2016-01-20 23:29:51 -04:00
|
|
|
// sets the maximum speed up and down returned by position controller
|
2022-09-27 18:09:00 -03:00
|
|
|
attitude_control.set_throttle_out(0.75, true, 100.0);
|
|
|
|
pos_control.init_z_controller();
|
2021-04-27 03:50:06 -03:00
|
|
|
pos_control.set_max_speed_accel_z(-get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
|
2021-07-08 01:16:08 -03:00
|
|
|
pos_control.set_correction_speed_accel_z(-get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
|
2022-09-27 18:09:00 -03:00
|
|
|
attitude_control.relax_attitude_controllers();
|
2015-12-30 18:57:56 -04:00
|
|
|
// initialise position and desired velocity
|
2022-09-27 18:09:00 -03:00
|
|
|
float pos = stopping_distance();
|
|
|
|
float zero = 0;
|
|
|
|
pos_control.input_pos_vel_accel_z(pos, zero, zero);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
2022-04-15 13:13:12 -03:00
|
|
|
if(prev_control_mode != control_mode_t::STABILIZE) {
|
|
|
|
last_roll = 0;
|
|
|
|
last_pitch = 0;
|
|
|
|
}
|
2017-02-03 00:18:27 -04:00
|
|
|
last_pilot_heading = ahrs.yaw_sensor;
|
2022-04-13 22:10:59 -03:00
|
|
|
last_input_ms = AP_HAL::millis();
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:10:59 -03:00
|
|
|
|
2022-09-27 18:09:00 -03:00
|
|
|
float Sub::stopping_distance() {
|
|
|
|
const float curr_pos_z = inertial_nav.get_position().z;
|
|
|
|
float curr_vel_z = inertial_nav.get_velocity().z;
|
|
|
|
float distance = - (curr_vel_z * curr_vel_z) / (2 * g.pilot_accel_z);
|
|
|
|
return curr_pos_z + distance;
|
|
|
|
}
|
|
|
|
|
2023-07-04 12:03:34 -03:00
|
|
|
void Sub::handle_mavlink_attitude_target(){
|
2017-02-03 17:33:27 -04:00
|
|
|
uint32_t tnow = AP_HAL::millis();
|
2022-04-13 22:10:59 -03:00
|
|
|
float target_roll, target_pitch, target_yaw;
|
2018-04-14 00:15:15 -03:00
|
|
|
// Check if set_attitude_target_no_gps is valid
|
|
|
|
if (tnow - sub.set_attitude_target_no_gps.last_message_ms < 5000) {
|
|
|
|
Quaternion(
|
|
|
|
set_attitude_target_no_gps.packet.q
|
|
|
|
).to_euler(
|
|
|
|
target_roll,
|
|
|
|
target_pitch,
|
|
|
|
target_yaw
|
|
|
|
);
|
2022-04-13 22:10:59 -03:00
|
|
|
target_roll = 100 * degrees(target_roll);
|
|
|
|
target_pitch = 100 * degrees(target_pitch);
|
|
|
|
target_yaw = 100 * degrees(target_yaw);
|
2022-04-28 16:27:55 -03:00
|
|
|
last_roll = target_roll;
|
|
|
|
last_pitch = target_pitch;
|
|
|
|
last_pilot_heading = target_yaw;
|
2022-04-13 22:10:59 -03:00
|
|
|
attitude_control.input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, target_yaw, true);
|
2023-02-14 12:54:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-07-04 12:03:34 -03:00
|
|
|
}
|
2023-02-14 12:54:44 -04:00
|
|
|
|
2022-04-13 22:10:59 -03:00
|
|
|
// althold_run - runs the althold controller
|
|
|
|
// should be called at 100hz or more
|
|
|
|
void Sub::althold_run()
|
|
|
|
{
|
|
|
|
// When unarmed, disable motors and stabilization
|
|
|
|
if (!motors.armed()) {
|
2023-07-04 12:03:34 -03:00
|
|
|
althold_init();
|
2022-04-13 22:10:59 -03:00
|
|
|
return;
|
2017-02-03 17:33:27 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:10:59 -03:00
|
|
|
handle_attitude();
|
2020-08-03 00:31:50 -03:00
|
|
|
|
2022-04-13 22:10:59 -03:00
|
|
|
control_depth();
|
2020-08-03 00:31:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sub::control_depth() {
|
2022-04-13 22:10:59 -03:00
|
|
|
// We rotate the RC inputs to the earth frame to check if the user is giving an input that would change the depth.
|
|
|
|
// Output the Z controller + pilot input to all motors.
|
2022-04-28 15:26:18 -03:00
|
|
|
Vector3f earth_frame_rc_inputs = ahrs.get_rotation_body_to_ned() * Vector3f(-channel_forward->norm_input(), -channel_lateral->norm_input(), (2.0f*(-0.5f+channel_throttle->norm_input())));
|
2022-04-13 22:10:59 -03:00
|
|
|
float target_climb_rate_cm_s = get_pilot_desired_climb_rate(500 + g.pilot_speed_up * earth_frame_rc_inputs.z);
|
2022-09-27 12:33:49 -03:00
|
|
|
|
|
|
|
bool surfacing = ap.at_surface || pos_control.get_pos_target_z_cm() > g.surface_depth;
|
|
|
|
float upper_speed_limit = surfacing ? 0 : g.pilot_speed_up;
|
|
|
|
float lower_speed_limit = ap.at_bottom ? 0 : -get_pilot_speed_dn();
|
|
|
|
target_climb_rate_cm_s = constrain_float(target_climb_rate_cm_s, lower_speed_limit, upper_speed_limit);
|
2022-04-13 22:10:59 -03:00
|
|
|
pos_control.set_pos_target_z_from_climb_rate_cm(target_climb_rate_cm_s);
|
2022-09-05 19:21:25 -03:00
|
|
|
|
2022-09-27 12:33:49 -03:00
|
|
|
if (surfacing) {
|
|
|
|
pos_control.set_alt_target_with_slew(MIN(pos_control.get_pos_target_z_cm(), g.surface_depth - 5.0f)); // set target to 5 cm below surface level
|
2022-09-05 19:21:25 -03:00
|
|
|
} else if (ap.at_bottom) {
|
|
|
|
pos_control.set_alt_target_with_slew(MAX(inertial_nav.get_altitude() + 10.0f, pos_control.get_pos_target_z_cm())); // set target to 10 cm above bottom
|
2020-09-04 00:31:33 -03:00
|
|
|
}
|
|
|
|
pos_control.update_z_controller();
|
2022-04-13 22:10:59 -03:00
|
|
|
// Read the output of the z controller and rotate it so it always points up
|
|
|
|
Vector3f throttle_vehicle_frame = ahrs.get_rotation_body_to_ned().transposed() * Vector3f(0, 0, motors.get_throttle_in_bidirectional());
|
|
|
|
//TODO: scale throttle with the ammount of thrusters in the given direction
|
2022-09-27 12:33:49 -03:00
|
|
|
float raw_throttle_factor = (ahrs.get_rotation_body_to_ned() * Vector3f(0, 0, 1.0)).xy().length();
|
|
|
|
motors.set_throttle(throttle_vehicle_frame.z + raw_throttle_factor * channel_throttle->norm_input());
|
2022-04-13 22:10:59 -03:00
|
|
|
motors.set_forward(-throttle_vehicle_frame.x + channel_forward->norm_input());
|
|
|
|
motors.set_lateral(-throttle_vehicle_frame.y + channel_lateral->norm_input());
|
|
|
|
}
|