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/mode_acro.cpp
Views: 1798
#include "Copter.h"12#include "mode.h"34#if MODE_ACRO_ENABLED56/*7* Init and run calls for acro flight mode8*/9void ModeAcro::run()10{11// convert the input to the desired body frame rate12float target_roll, target_pitch, target_yaw;13get_pilot_desired_angle_rates(channel_roll->norm_input_dz(), channel_pitch->norm_input_dz(), channel_yaw->norm_input_dz(), target_roll, target_pitch, target_yaw);1415if (!motors->armed()) {16// Motors should be Stopped17motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::SHUT_DOWN);18} else if (copter.ap.throttle_zero19|| (copter.air_mode == AirMode::AIRMODE_ENABLED && motors->get_spool_state() == AP_Motors::SpoolState::SHUT_DOWN)) {20// throttle_zero is never true in air mode, but the motors should be allowed to go through ground idle21// in order to facilitate the spoolup block2223// Attempting to Land or motors not yet spinning24// if airmode is enabled only an actual landing will spool down the motors25motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);26} else {27motors->set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);28}2930float pilot_desired_throttle = get_pilot_desired_throttle();3132switch (motors->get_spool_state()) {33case AP_Motors::SpoolState::SHUT_DOWN:34// Motors Stopped35attitude_control->reset_target_and_rate(true);36attitude_control->reset_rate_controller_I_terms();37pilot_desired_throttle = 0.0f;38break;3940case AP_Motors::SpoolState::GROUND_IDLE:41// Landed42attitude_control->reset_target_and_rate();43attitude_control->reset_rate_controller_I_terms_smoothly();44pilot_desired_throttle = 0.0f;45break;4647case AP_Motors::SpoolState::THROTTLE_UNLIMITED:48// clear landing flag above zero throttle49if (!motors->limit.throttle_lower) {50set_land_complete(false);51}52break;5354case AP_Motors::SpoolState::SPOOLING_UP:55case AP_Motors::SpoolState::SPOOLING_DOWN:56// do nothing57break;58}5960// run attitude controller61if (g2.acro_options.get() & uint8_t(AcroOptions::RATE_LOOP_ONLY)) {62attitude_control->input_rate_bf_roll_pitch_yaw_2(target_roll, target_pitch, target_yaw);63} else {64attitude_control->input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);65}6667// output pilot's throttle without angle boost68attitude_control->set_throttle_out(pilot_desired_throttle, false, copter.g.throttle_filt);69}7071bool ModeAcro::init(bool ignore_checks)72{73if (g2.acro_options.get() & uint8_t(AcroOptions::AIR_MODE)) {74disable_air_mode_reset = false;75copter.air_mode = AirMode::AIRMODE_ENABLED;76}7778return true;79}8081void ModeAcro::exit()82{83if (!disable_air_mode_reset && (g2.acro_options.get() & uint8_t(AcroOptions::AIR_MODE))) {84copter.air_mode = AirMode::AIRMODE_DISABLED;85}86disable_air_mode_reset = false;87}8889void ModeAcro::air_mode_aux_changed()90{91disable_air_mode_reset = true;92}9394float ModeAcro::throttle_hover() const95{96if (g2.acro_thr_mid > 0) {97return g2.acro_thr_mid;98}99return Mode::throttle_hover();100}101102// get_pilot_desired_angle_rates - transform pilot's normalised roll pitch and yaw input into a desired lean angle rates103// inputs are -1 to 1 and the function returns desired angle rates in centi-degrees-per-second104void ModeAcro::get_pilot_desired_angle_rates(float roll_in, float pitch_in, float yaw_in, float &roll_out, float &pitch_out, float &yaw_out)105{106float rate_limit;107Vector3f rate_ef_level_cd, rate_bf_level_cd, rate_bf_request_cd;108109// apply circular limit to pitch and roll inputs110float total_in = norm(pitch_in, roll_in);111112if (total_in > 1.0) {113float ratio = 1.0 / total_in;114roll_in *= ratio;115pitch_in *= ratio;116}117118// calculate roll, pitch rate requests119120// roll expo121rate_bf_request_cd.x = g2.command_model_acro_rp.get_rate() * 100.0 * input_expo(roll_in, g2.command_model_acro_rp.get_expo());122123// pitch expo124rate_bf_request_cd.y = g2.command_model_acro_rp.get_rate() * 100.0 * input_expo(pitch_in, g2.command_model_acro_rp.get_expo());125126// yaw expo127rate_bf_request_cd.z = g2.command_model_acro_y.get_rate() * 100.0 * input_expo(yaw_in, g2.command_model_acro_y.get_expo());128129// calculate earth frame rate corrections to pull the copter back to level while in ACRO mode130131if (g.acro_trainer != (uint8_t)Trainer::OFF) {132133// get attitude targets134const Vector3f att_target = attitude_control->get_att_target_euler_cd();135136// Calculate trainer mode earth frame rate command for roll137int32_t roll_angle = wrap_180_cd(att_target.x);138rate_ef_level_cd.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;139140// Calculate trainer mode earth frame rate command for pitch141int32_t pitch_angle = wrap_180_cd(att_target.y);142rate_ef_level_cd.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;143144// Calculate trainer mode earth frame rate command for yaw145rate_ef_level_cd.z = 0;146147// Calculate angle limiting earth frame rate commands148if (g.acro_trainer == (uint8_t)Trainer::LIMITED) {149const float angle_max = copter.aparm.angle_max;150if (roll_angle > angle_max){151rate_ef_level_cd.x += sqrt_controller(angle_max - roll_angle, g2.command_model_acro_rp.get_rate() * 100.0 / ACRO_LEVEL_MAX_OVERSHOOT, attitude_control->get_accel_roll_max_cdss(), G_Dt);152}else if (roll_angle < -angle_max) {153rate_ef_level_cd.x += sqrt_controller(-angle_max - roll_angle, g2.command_model_acro_rp.get_rate() * 100.0 / ACRO_LEVEL_MAX_OVERSHOOT, attitude_control->get_accel_roll_max_cdss(), G_Dt);154}155156if (pitch_angle > angle_max){157rate_ef_level_cd.y += sqrt_controller(angle_max - pitch_angle, g2.command_model_acro_rp.get_rate() * 100.0 / ACRO_LEVEL_MAX_OVERSHOOT, attitude_control->get_accel_pitch_max_cdss(), G_Dt);158}else if (pitch_angle < -angle_max) {159rate_ef_level_cd.y += sqrt_controller(-angle_max - pitch_angle, g2.command_model_acro_rp.get_rate() * 100.0 / ACRO_LEVEL_MAX_OVERSHOOT, attitude_control->get_accel_pitch_max_cdss(), G_Dt);160}161}162163// convert earth-frame level rates to body-frame level rates164attitude_control->euler_rate_to_ang_vel(attitude_control->get_attitude_target_quat(), rate_ef_level_cd, rate_bf_level_cd);165166// combine earth frame rate corrections with rate requests167if (g.acro_trainer == (uint8_t)Trainer::LIMITED) {168rate_bf_request_cd.x += rate_bf_level_cd.x;169rate_bf_request_cd.y += rate_bf_level_cd.y;170rate_bf_request_cd.z += rate_bf_level_cd.z;171}else{172float acro_level_mix = constrain_float(1-float(MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0), 0, 1)*ahrs.cos_pitch();173174// Scale levelling rates by stick input175rate_bf_level_cd = rate_bf_level_cd * acro_level_mix;176177// Calculate rate limit to prevent change of rate through inverted178rate_limit = fabsf(fabsf(rate_bf_request_cd.x)-fabsf(rate_bf_level_cd.x));179rate_bf_request_cd.x += rate_bf_level_cd.x;180rate_bf_request_cd.x = constrain_float(rate_bf_request_cd.x, -rate_limit, rate_limit);181182// Calculate rate limit to prevent change of rate through inverted183rate_limit = fabsf(fabsf(rate_bf_request_cd.y)-fabsf(rate_bf_level_cd.y));184rate_bf_request_cd.y += rate_bf_level_cd.y;185rate_bf_request_cd.y = constrain_float(rate_bf_request_cd.y, -rate_limit, rate_limit);186187// Calculate rate limit to prevent change of rate through inverted188rate_limit = fabsf(fabsf(rate_bf_request_cd.z)-fabsf(rate_bf_level_cd.z));189rate_bf_request_cd.z += rate_bf_level_cd.z;190rate_bf_request_cd.z = constrain_float(rate_bf_request_cd.z, -rate_limit, rate_limit);191}192}193194// hand back rate request195roll_out = rate_bf_request_cd.x;196pitch_out = rate_bf_request_cd.y;197yaw_out = rate_bf_request_cd.z;198}199#endif200201202