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/ArduCopter/Attitude.cpp
Views: 1798
#include "Copter.h"12/*************************************************************3* Attitude Rate controllers and timing4****************************************************************/56/*7update rate controller when run from main thread (normal operation)8*/9void Copter::run_rate_controller_main()10{11// set attitude and position controller loop time12const float last_loop_time_s = AP::scheduler().get_last_loop_time_s();13pos_control->set_dt(last_loop_time_s);14attitude_control->set_dt(last_loop_time_s);1516if (!using_rate_thread) {17motors->set_dt(last_loop_time_s);18// only run the rate controller if we are not using the rate thread19attitude_control->rate_controller_run();20}21// reset sysid and other temporary inputs22attitude_control->rate_controller_target_reset();23}2425/*************************************************************26* throttle control27****************************************************************/2829// update estimated throttle required to hover (if necessary)30// called at 100hz31void Copter::update_throttle_hover()32{33// if not armed or landed or on standby then exit34if (!motors->armed() || ap.land_complete || standby_active) {35return;36}3738// do not update in manual throttle modes or Drift39if (flightmode->has_manual_throttle() || (copter.flightmode->mode_number() == Mode::Number::DRIFT)) {40return;41}4243// do not update while climbing or descending44if (!is_zero(pos_control->get_vel_desired_cms().z)) {45return;46}4748// get throttle output49float throttle = motors->get_throttle();5051// calc average throttle if we are in a level hover. accounts for heli hover roll trim52if (throttle > 0.0f && fabsf(inertial_nav.get_velocity_z_up_cms()) < 60 &&53fabsf(ahrs.roll_sensor-attitude_control->get_roll_trim_cd()) < 500 && labs(ahrs.pitch_sensor) < 500) {54// Can we set the time constant automatically55motors->update_throttle_hover(0.01f);56#if HAL_GYROFFT_ENABLED57gyro_fft.update_freq_hover(0.01f, motors->get_throttle_out());58#endif59}60}6162// get_pilot_desired_climb_rate - transform pilot's throttle input to climb rate in cm/s63// without any deadzone at the bottom64float Copter::get_pilot_desired_climb_rate(float throttle_control)65{66// throttle failsafe check67if (failsafe.radio || !rc().has_ever_seen_rc_input()) {68return 0.0f;69}7071#if TOY_MODE_ENABLED72if (g2.toy_mode.enabled()) {73// allow throttle to be reduced after throttle arming and for74// slower descent close to the ground75g2.toy_mode.throttle_adjust(throttle_control);76}77#endif7879// ensure a reasonable throttle value80throttle_control = constrain_float(throttle_control,0.0f,1000.0f);8182// ensure a reasonable deadzone83g.throttle_deadzone.set(constrain_int16(g.throttle_deadzone, 0, 400));8485float desired_rate = 0.0f;86const float mid_stick = get_throttle_mid();87const float deadband_top = mid_stick + g.throttle_deadzone;88const float deadband_bottom = mid_stick - g.throttle_deadzone;8990// check throttle is above, below or in the deadband91if (throttle_control < deadband_bottom) {92// below the deadband93desired_rate = get_pilot_speed_dn() * (throttle_control-deadband_bottom) / deadband_bottom;94} else if (throttle_control > deadband_top) {95// above the deadband96desired_rate = g.pilot_speed_up * (throttle_control-deadband_top) / (1000.0f-deadband_top);97} else {98// must be in the deadband99desired_rate = 0.0f;100}101102return desired_rate;103}104105// get_non_takeoff_throttle - a throttle somewhere between min and mid throttle which should not lead to a takeoff106float Copter::get_non_takeoff_throttle()107{108return MAX(0,motors->get_throttle_hover()/2.0f);109}110111// set_accel_throttle_I_from_pilot_throttle - smoothes transition from pilot controlled throttle to autopilot throttle112void Copter::set_accel_throttle_I_from_pilot_throttle()113{114// get last throttle input sent to attitude controller115float pilot_throttle = constrain_float(attitude_control->get_throttle_in(), 0.0f, 1.0f);116// shift difference between pilot's throttle and hover throttle into accelerometer I117pos_control->get_accel_z_pid().set_integrator((pilot_throttle-motors->get_throttle_hover()) * 1000.0f);118}119120// rotate vector from vehicle's perspective to North-East frame121void Copter::rotate_body_frame_to_NE(float &x, float &y)122{123float ne_x = x*ahrs.cos_yaw() - y*ahrs.sin_yaw();124float ne_y = x*ahrs.sin_yaw() + y*ahrs.cos_yaw();125x = ne_x;126y = ne_y;127}128129// It will return the PILOT_SPEED_DN value if non zero, otherwise if zero it returns the PILOT_SPEED_UP value.130uint16_t Copter::get_pilot_speed_dn() const131{132if (g2.pilot_speed_dn == 0) {133return abs(g.pilot_speed_up);134} else {135return abs(g2.pilot_speed_dn);136}137}138139140