Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/ArduSub/mode_althold.cpp
Views: 1798
#include "Sub.h"123bool ModeAlthold::init(bool ignore_checks) {4if(!sub.control_check_barometer()) {5return false;6}78// initialize vertical maximum speeds and acceleration9// sets the maximum speed up and down returned by position controller10position_control->set_max_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);11position_control->set_correction_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);1213// initialise position and desired velocity14position_control->init_z_controller();1516sub.last_pilot_heading = ahrs.yaw_sensor;1718return true;19}2021// althold_run - runs the althold controller22// should be called at 100hz or more23void ModeAlthold::run()24{25run_pre();26control_depth();27run_post();28}2930void ModeAlthold::run_pre()31{32uint32_t tnow = AP_HAL::millis();3334// initialize vertical speeds and acceleration35position_control->set_max_speed_accel_z(-sub.get_pilot_speed_dn(), g.pilot_speed_up, g.pilot_accel_z);3637if (!motors.armed()) {38motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);39// Sub vehicles do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)40attitude_control->set_throttle_out(0.5,true,g.throttle_filt);41attitude_control->relax_attitude_controllers();42position_control->relax_z_controller(motors.get_throttle_hover());43sub.last_pilot_heading = ahrs.yaw_sensor;44return;45}4647motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);4849// get pilot desired lean angles50float target_roll, target_pitch;5152// Check if set_attitude_target_no_gps is valid53if (tnow - sub.set_attitude_target_no_gps.last_message_ms < 5000) {54float target_yaw;55Quaternion(56sub.set_attitude_target_no_gps.packet.q57).to_euler(58target_roll,59target_pitch,60target_yaw61);62target_roll = degrees(target_roll);63target_pitch = degrees(target_pitch);64target_yaw = degrees(target_yaw);6566attitude_control->input_euler_angle_roll_pitch_yaw(target_roll * 1e2f, target_pitch * 1e2f, target_yaw * 1e2f, true);67return;68}6970sub.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());7172// get pilot's desired yaw rate73float yaw_input = channel_yaw->pwm_to_angle_dz_trim(channel_yaw->get_dead_zone() * sub.gain, channel_yaw->get_radio_trim());74float target_yaw_rate = sub.get_pilot_desired_yaw_rate(yaw_input);7576// call attitude controller77if (!is_zero(target_yaw_rate)) { // call attitude controller with rate yaw determined by pilot input78attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);79sub.last_pilot_heading = ahrs.yaw_sensor;80sub.last_pilot_yaw_input_ms = tnow; // time when pilot last changed heading8182} else { // hold current heading8384// this check is required to prevent bounce back after very fast yaw maneuvers85// the inertia of the vehicle causes the heading to move slightly past the point when pilot input actually stopped86if (tnow < sub.last_pilot_yaw_input_ms + 250) { // give 250ms to slow down, then set target heading87target_yaw_rate = 0; // Stop rotation on yaw axis8889// call attitude controller with target yaw rate = 0 to decelerate on yaw axis90attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);91sub.last_pilot_heading = ahrs.yaw_sensor; // update heading to hold9293} else { // call attitude controller holding absolute bearing94attitude_control->input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, sub.last_pilot_heading, true);95}96}97}9899void ModeAlthold::run_post()100{101motors.set_forward(channel_forward->norm_input());102motors.set_lateral(channel_lateral->norm_input());103}104105void ModeAlthold::control_depth() {106float target_climb_rate_cm_s = sub.get_pilot_desired_climb_rate(channel_throttle->get_control_in());107target_climb_rate_cm_s = constrain_float(target_climb_rate_cm_s, -sub.get_pilot_speed_dn(), g.pilot_speed_up);108109// desired_climb_rate returns 0 when within the deadzone.110//we allow full control to the pilot, but as soon as there's no input, we handle being at surface/bottom111if (fabsf(target_climb_rate_cm_s) < 0.05f) {112if (sub.ap.at_surface) {113position_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 level114} else if (sub.ap.at_bottom) {115position_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 bottom116}117}118119position_control->set_pos_target_z_from_climb_rate_cm(target_climb_rate_cm_s);120position_control->update_z_controller();121}122123124