CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduSub/mode_althold.cpp
Views: 1798
1
#include "Sub.h"
2
3
4
bool ModeAlthold::init(bool ignore_checks) {
5
if(!sub.control_check_barometer()) {
6
return false;
7
}
8
9
// initialize vertical maximum speeds and acceleration
10
// sets the maximum speed up and down returned by position controller
11
position_control->set_max_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
12
position_control->set_correction_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
13
14
// initialise position and desired velocity
15
position_control->init_z_controller();
16
17
sub.last_pilot_heading = ahrs.yaw_sensor;
18
19
return true;
20
}
21
22
// althold_run - runs the althold controller
23
// should be called at 100hz or more
24
void ModeAlthold::run()
25
{
26
run_pre();
27
control_depth();
28
run_post();
29
}
30
31
void ModeAlthold::run_pre()
32
{
33
uint32_t tnow = AP_HAL::millis();
34
35
// initialize vertical speeds and acceleration
36
position_control->set_max_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);
37
38
if (!motors.armed()) {
39
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
40
// Sub vehicles do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)
41
attitude_control->set_throttle_out(0.5,true,g.throttle_filt);
42
attitude_control->relax_attitude_controllers();
43
position_control->relax_z_controller(motors.get_throttle_hover());
44
sub.last_pilot_heading = ahrs.yaw_sensor;
45
return;
46
}
47
48
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
49
50
// get pilot desired lean angles
51
float target_roll, target_pitch;
52
53
// Check if set_attitude_target_no_gps is valid
54
if (tnow - sub.set_attitude_target_no_gps.last_message_ms < 5000) {
55
float target_yaw;
56
Quaternion(
57
sub.set_attitude_target_no_gps.packet.q
58
).to_euler(
59
target_roll,
60
target_pitch,
61
target_yaw
62
);
63
target_roll = degrees(target_roll);
64
target_pitch = degrees(target_pitch);
65
target_yaw = degrees(target_yaw);
66
67
attitude_control->input_euler_angle_roll_pitch_yaw(target_roll * 1e2f, target_pitch * 1e2f, target_yaw * 1e2f, true);
68
return;
69
}
70
71
sub.get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, attitude_control->get_althold_lean_angle_max_cd());
72
73
// get pilot's desired yaw rate
74
float yaw_input = channel_yaw->pwm_to_angle_dz_trim(channel_yaw->get_dead_zone() * sub.gain, channel_yaw->get_radio_trim());
75
float target_yaw_rate = sub.get_pilot_desired_yaw_rate(yaw_input);
76
77
// call attitude controller
78
if (!is_zero(target_yaw_rate)) { // call attitude controller with rate yaw determined by pilot input
79
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
80
sub.last_pilot_heading = ahrs.yaw_sensor;
81
sub.last_pilot_yaw_input_ms = tnow; // time when pilot last changed heading
82
83
} else { // hold current heading
84
85
// this check is required to prevent bounce back after very fast yaw maneuvers
86
// the inertia of the vehicle causes the heading to move slightly past the point when pilot input actually stopped
87
if (tnow < sub.last_pilot_yaw_input_ms + 250) { // give 250ms to slow down, then set target heading
88
target_yaw_rate = 0; // Stop rotation on yaw axis
89
90
// call attitude controller with target yaw rate = 0 to decelerate on yaw axis
91
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
92
sub.last_pilot_heading = ahrs.yaw_sensor; // update heading to hold
93
94
} else { // call attitude controller holding absolute bearing
95
attitude_control->input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, sub.last_pilot_heading, true);
96
}
97
}
98
}
99
100
void ModeAlthold::run_post()
101
{
102
motors.set_forward(channel_forward->norm_input());
103
motors.set_lateral(channel_lateral->norm_input());
104
}
105
106
void ModeAlthold::control_depth() {
107
float target_climb_rate_cm_s = sub.get_pilot_desired_climb_rate(channel_throttle->get_control_in());
108
target_climb_rate_cm_s = constrain_float(target_climb_rate_cm_s, -sub.get_pilot_speed_dn(), g.pilot_speed_up);
109
110
// desired_climb_rate returns 0 when within the deadzone.
111
//we allow full control to the pilot, but as soon as there's no input, we handle being at surface/bottom
112
if (fabsf(target_climb_rate_cm_s) < 0.05f) {
113
if (sub.ap.at_surface) {
114
position_control->set_pos_desired_z_cm(MIN(position_control->get_pos_desired_z_cm(), g.surface_depth - 5.0f)); // set target to 5 cm below surface level
115
} else if (sub.ap.at_bottom) {
116
position_control->set_pos_desired_z_cm(MAX(inertial_nav.get_position_z_up_cm() + 10.0f, position_control->get_pos_desired_z_cm())); // set target to 10 cm above bottom
117
}
118
}
119
120
position_control->set_pos_target_z_from_climb_rate_cm(target_climb_rate_cm_s);
121
position_control->update_z_controller();
122
}
123
124