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_stabilize.cpp
Views: 1798
#include "Sub.h"123bool ModeStabilize::init(bool ignore_checks) {4// set target altitude to zero for reporting5position_control->set_pos_desired_z_cm(0);6sub.last_pilot_heading = ahrs.yaw_sensor;78return true;9return true;10}1112void ModeStabilize::run()13{14uint32_t tnow = AP_HAL::millis();15float target_roll, target_pitch;1617// if not armed set throttle to zero and exit immediately18if (!motors.armed()) {19motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);20attitude_control->set_throttle_out(0,true,g.throttle_filt);21attitude_control->relax_attitude_controllers();22sub.last_pilot_heading = ahrs.yaw_sensor;23return;24}2526motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);2728// convert pilot input to lean angles29// To-Do: convert sub.get_pilot_desired_lean_angles to return angles as floats30// TODO2: move into mode.h31sub.get_pilot_desired_lean_angles(channel_roll->get_control_in(), channel_pitch->get_control_in(), target_roll, target_pitch, sub.aparm.angle_max);3233// get pilot's desired yaw rate34float yaw_input = channel_yaw->pwm_to_angle_dz_trim(channel_yaw->get_dead_zone() * sub.gain, channel_yaw->get_radio_trim());35float target_yaw_rate = sub.get_pilot_desired_yaw_rate(yaw_input);3637// call attitude controller38// update attitude controller targets3940if (!is_zero(target_yaw_rate)) { // call attitude controller with rate yaw determined by pilot input41attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);42sub.last_pilot_heading = ahrs.yaw_sensor;43sub.last_pilot_yaw_input_ms = tnow; // time when pilot last changed heading4445} else { // hold current heading4647// this check is required to prevent bounce back after very fast yaw maneuvers48// the inertia of the vehicle causes the heading to move slightly past the point when pilot input actually stopped49if (tnow < sub.last_pilot_yaw_input_ms + 250) { // give 250ms to slow down, then set target heading50target_yaw_rate = 0; // Stop rotation on yaw axis5152// call attitude controller with target yaw rate = 0 to decelerate on yaw axis53attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(target_roll, target_pitch, target_yaw_rate);54sub.last_pilot_heading = ahrs.yaw_sensor; // update heading to hold5556} else { // call attitude controller holding absolute absolute bearing57attitude_control->input_euler_angle_roll_pitch_yaw(target_roll, target_pitch, sub.last_pilot_heading, true);58}59}6061// output pilot's throttle62attitude_control->set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);6364//control_in is range -1000-100065//radio_in is raw pwm value66motors.set_forward(channel_forward->norm_input());67motors.set_lateral(channel_lateral->norm_input());68}697071