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_surface.cpp
Views: 1798
1
#include "Sub.h"
2
3
4
bool ModeSurface::init(bool ignore_checks)
5
{
6
if(!sub.control_check_barometer()) {
7
return false;
8
}
9
10
// initialize vertical speeds and acceleration
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
return true;
18
19
}
20
21
void ModeSurface::run()
22
{
23
float target_roll, target_pitch;
24
25
// if not armed set throttle to zero and exit immediately
26
if (!motors.armed()) {
27
motors.output_min();
28
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
29
attitude_control->set_throttle_out(0,true,g.throttle_filt);
30
attitude_control->relax_attitude_controllers();
31
position_control->init_z_controller();
32
return;
33
}
34
35
// Already at surface, hold depth at surface
36
if (sub.ap.at_surface) {
37
set_mode(Mode::Number::ALT_HOLD, ModeReason::SURFACE_COMPLETE);
38
}
39
40
// convert pilot input to lean angles
41
// To-Do: convert sub.get_pilot_desired_lean_angles to return angles as floats
42
sub.get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, sub.aparm.angle_max);
43
44
// get pilot's desired yaw rate
45
float target_yaw_rate = sub.get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
46
47
// call attitude controller
48
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);
49
50
// set target climb rate
51
float cmb_rate = constrain_float(fabsf(sub.wp_nav.get_default_speed_up()), 1, position_control->get_max_speed_up_cms());
52
53
// record desired climb rate for logging
54
sub.desired_climb_rate = cmb_rate;
55
56
// update altitude target and call position controller
57
position_control->set_pos_target_z_from_climb_rate_cm(cmb_rate);
58
position_control->update_z_controller();
59
60
// pilot has control for repositioning
61
motors.set_forward(channel_forward->norm_input());
62
motors.set_lateral(channel_lateral->norm_input());
63
}
64
65