2016-01-14 15:30:56 -04:00
|
|
|
#include "Sub.h"
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* control_acro.pde - init and run calls for acro flight mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
// acro_init - initialise acro controller
|
2016-01-14 15:30:56 -04:00
|
|
|
bool Sub::acro_init(bool ignore_checks)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
2016-04-05 00:17:39 -03:00
|
|
|
// set target altitude to zero for reporting
|
|
|
|
pos_control.set_alt_target(0);
|
|
|
|
|
|
|
|
return true;
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// acro_run - runs the acro controller
|
|
|
|
// should be called at 100hz or more
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::acro_run()
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
float target_roll, target_pitch, target_yaw;
|
|
|
|
|
2016-04-05 00:17:39 -03:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
|
|
|
if (!motors.armed() || !motors.get_interlock()) {
|
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_SPIN_WHEN_ARMED);
|
2015-12-30 18:57:56 -04:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-05 00:17:39 -03:00
|
|
|
motors.set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// convert the input to the desired body frame rate
|
2017-02-03 00:18:27 -04:00
|
|
|
get_pilot_desired_angle_rates(channel_roll->get_control_in(), channel_pitch->get_control_in(), channel_yaw->get_control_in(), target_roll, target_pitch, target_yaw);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
// run attitude controller
|
|
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
|
|
|
|
|
|
|
|
// output pilot's throttle without angle boost
|
2017-02-03 00:18:27 -04:00
|
|
|
attitude_control.set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);
|
2016-01-08 19:25:59 -04:00
|
|
|
|
|
|
|
//control_in is range 0-1000
|
|
|
|
//radio_in is raw pwm value
|
2017-02-03 00:18:27 -04:00
|
|
|
motors.set_forward(channel_forward->norm_input());
|
|
|
|
motors.set_lateral(channel_lateral->norm_input());
|
2015-12-30 18:57:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get_pilot_desired_angle_rates - transform pilot's roll pitch and yaw input into a desired lean angle rates
|
|
|
|
// returns desired angle rates in centi-degrees-per-second
|
2016-01-14 15:30:56 -04:00
|
|
|
void Sub::get_pilot_desired_angle_rates(int16_t roll_in, int16_t pitch_in, int16_t yaw_in, float &roll_out, float &pitch_out, float &yaw_out)
|
2015-12-30 18:57:56 -04:00
|
|
|
{
|
|
|
|
float rate_limit;
|
|
|
|
Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
|
|
|
|
|
|
|
|
// apply circular limit to pitch and roll inputs
|
2017-02-03 00:18:27 -04:00
|
|
|
float total_in = norm(pitch_in, roll_in);
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
if (total_in > ROLL_PITCH_INPUT_MAX) {
|
|
|
|
float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
|
|
|
|
roll_in *= ratio;
|
|
|
|
pitch_in *= ratio;
|
|
|
|
}
|
2017-02-03 17:33:27 -04:00
|
|
|
|
2015-12-30 18:57:56 -04:00
|
|
|
// calculate roll, pitch rate requests
|
|
|
|
if (g.acro_expo <= 0) {
|
|
|
|
rate_bf_request.x = roll_in * g.acro_rp_p;
|
|
|
|
rate_bf_request.y = pitch_in * g.acro_rp_p;
|
|
|
|
} else {
|
|
|
|
// expo variables
|
|
|
|
float rp_in, rp_in3, rp_out;
|
|
|
|
|
|
|
|
// range check expo
|
|
|
|
if (g.acro_expo > 1.0f) {
|
|
|
|
g.acro_expo = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// roll expo
|
|
|
|
rp_in = float(roll_in)/ROLL_PITCH_INPUT_MAX;
|
|
|
|
rp_in3 = rp_in*rp_in*rp_in;
|
|
|
|
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
|
|
|
|
rate_bf_request.x = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
|
|
|
|
|
|
|
|
// pitch expo
|
|
|
|
rp_in = float(pitch_in)/ROLL_PITCH_INPUT_MAX;
|
|
|
|
rp_in3 = rp_in*rp_in*rp_in;
|
|
|
|
rp_out = (g.acro_expo * rp_in3) + ((1 - g.acro_expo) * rp_in);
|
|
|
|
rate_bf_request.y = ROLL_PITCH_INPUT_MAX * rp_out * g.acro_rp_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate yaw rate request
|
|
|
|
rate_bf_request.z = yaw_in * g.acro_yaw_p;
|
|
|
|
|
2017-02-10 13:46:54 -04:00
|
|
|
// calculate earth frame rate corrections to pull the vehicle back to level while in ACRO mode
|
2015-12-30 18:57:56 -04:00
|
|
|
|
|
|
|
if (g.acro_trainer != ACRO_TRAINER_DISABLED) {
|
|
|
|
// Calculate trainer mode earth frame rate command for roll
|
|
|
|
int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
|
|
|
|
rate_ef_level.x = -constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_roll;
|
|
|
|
|
|
|
|
// Calculate trainer mode earth frame rate command for pitch
|
|
|
|
int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
|
|
|
|
rate_ef_level.y = -constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE) * g.acro_balance_pitch;
|
|
|
|
|
|
|
|
// Calculate trainer mode earth frame rate command for yaw
|
|
|
|
rate_ef_level.z = 0;
|
|
|
|
|
|
|
|
// Calculate angle limiting earth frame rate commands
|
|
|
|
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
|
2017-02-03 17:33:27 -04:00
|
|
|
if (roll_angle > aparm.angle_max) {
|
2015-12-30 18:57:56 -04:00
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle-aparm.angle_max);
|
2017-02-03 17:33:27 -04:00
|
|
|
} else if (roll_angle < -aparm.angle_max) {
|
2015-12-30 18:57:56 -04:00
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle+aparm.angle_max);
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:33:27 -04:00
|
|
|
if (pitch_angle > aparm.angle_max) {
|
2015-12-30 18:57:56 -04:00
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-aparm.angle_max);
|
2017-02-03 17:33:27 -04:00
|
|
|
} else if (pitch_angle < -aparm.angle_max) {
|
2015-12-30 18:57:56 -04:00
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+aparm.angle_max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert earth-frame level rates to body-frame level rates
|
|
|
|
attitude_control.euler_rate_to_ang_vel(attitude_control.get_att_target_euler_cd()*radians(0.01f), rate_ef_level, rate_bf_level);
|
|
|
|
|
|
|
|
// combine earth frame rate corrections with rate requests
|
|
|
|
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
|
|
|
|
rate_bf_request.x += rate_bf_level.x;
|
|
|
|
rate_bf_request.y += rate_bf_level.y;
|
|
|
|
rate_bf_request.z += rate_bf_level.z;
|
2017-02-03 17:33:27 -04:00
|
|
|
} else {
|
2015-12-30 18:57:56 -04:00
|
|
|
float acro_level_mix = constrain_float(1-MAX(MAX(abs(roll_in), abs(pitch_in)), abs(yaw_in))/4500.0, 0, 1)*ahrs.cos_pitch();
|
|
|
|
|
|
|
|
// Scale leveling rates by stick input
|
|
|
|
rate_bf_level = rate_bf_level*acro_level_mix;
|
|
|
|
|
|
|
|
// Calculate rate limit to prevent change of rate through inverted
|
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
|
|
|
|
rate_bf_request.x += rate_bf_level.x;
|
|
|
|
rate_bf_request.x = constrain_float(rate_bf_request.x, -rate_limit, rate_limit);
|
|
|
|
|
|
|
|
// Calculate rate limit to prevent change of rate through inverted
|
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
|
|
|
|
rate_bf_request.y += rate_bf_level.y;
|
|
|
|
rate_bf_request.y = constrain_float(rate_bf_request.y, -rate_limit, rate_limit);
|
|
|
|
|
|
|
|
// Calculate rate limit to prevent change of rate through inverted
|
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
|
|
|
|
rate_bf_request.z += rate_bf_level.z;
|
|
|
|
rate_bf_request.z = constrain_float(rate_bf_request.z, -rate_limit, rate_limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hand back rate request
|
|
|
|
roll_out = rate_bf_request.x;
|
|
|
|
pitch_out = rate_bf_request.y;
|
|
|
|
yaw_out = rate_bf_request.z;
|
|
|
|
}
|