2014-01-24 23:00:07 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/*
|
2014-01-30 08:13:41 -04:00
|
|
|
* control_acro.pde - init and run calls for acro flight mode
|
2014-01-24 23:00:07 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// acro_init - initialise acro controller
|
|
|
|
static bool acro_init(bool ignore_checks)
|
|
|
|
{
|
2014-06-06 00:04:34 -03:00
|
|
|
// always successfully enter acro
|
2014-01-24 23:00:07 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// acro_run - runs the acro controller
|
|
|
|
// should be called at 100hz or more
|
|
|
|
static void acro_run()
|
|
|
|
{
|
2014-02-09 21:26:49 -04:00
|
|
|
float target_roll, target_pitch, target_yaw;
|
2014-01-30 20:55:00 -04:00
|
|
|
int16_t pilot_throttle_scaled;
|
2014-01-24 23:00:07 -04:00
|
|
|
|
2014-01-30 08:44:44 -04:00
|
|
|
// if motors not running reset angle targets
|
|
|
|
if(!motors.armed() || g.rc_3.control_in <= 0) {
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out_unstabilized(0,true,g.throttle_filt);
|
2014-01-30 08:44:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-24 23:00:07 -04:00
|
|
|
// convert the input to the desired body frame rate
|
|
|
|
get_pilot_desired_angle_rates(g.rc_1.control_in, g.rc_2.control_in, g.rc_4.control_in, target_roll, target_pitch, target_yaw);
|
|
|
|
|
2014-01-30 20:55:00 -04:00
|
|
|
// get pilot's desired throttle
|
|
|
|
pilot_throttle_scaled = get_pilot_desired_throttle(g.rc_3.control_in);
|
|
|
|
|
|
|
|
// run attitude controller
|
2014-02-13 22:56:46 -04:00
|
|
|
attitude_control.rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
|
2014-01-30 20:55:00 -04:00
|
|
|
|
|
|
|
// output pilot's throttle without angle boost
|
2015-04-16 01:54:29 -03:00
|
|
|
attitude_control.set_throttle_out(pilot_throttle_scaled, false, g.throttle_filt);
|
2014-01-24 23:00:07 -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
|
2014-02-09 21:26:49 -04:00
|
|
|
static void 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)
|
2014-01-24 23:00:07 -04:00
|
|
|
{
|
2014-01-27 00:55:02 -04:00
|
|
|
float rate_limit;
|
2014-01-24 23:00:07 -04:00
|
|
|
Vector3f rate_ef_level, rate_bf_level, rate_bf_request;
|
|
|
|
|
2014-11-10 20:07:08 -04:00
|
|
|
// apply circular limit to pitch and roll inputs
|
2014-11-10 21:22:22 -04:00
|
|
|
float total_in = pythagorous2((float)pitch_in, (float)roll_in);
|
2014-11-10 20:07:08 -04:00
|
|
|
|
2014-11-10 21:22:22 -04:00
|
|
|
if (total_in > ROLL_PITCH_INPUT_MAX) {
|
|
|
|
float ratio = (float)ROLL_PITCH_INPUT_MAX / total_in;
|
2014-11-10 20:07:08 -04:00
|
|
|
roll_in *= ratio;
|
|
|
|
pitch_in *= ratio;
|
|
|
|
}
|
2014-08-02 05:57:42 -03:00
|
|
|
|
2014-08-12 23:25:59 -03: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;
|
|
|
|
|
2014-08-18 00:56:22 -03:00
|
|
|
// range check expo
|
|
|
|
if (g.acro_expo > 1.0f) {
|
|
|
|
g.acro_expo = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-08-12 23:25:59 -03:00
|
|
|
// 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
|
2014-01-24 23:00:07 -04:00
|
|
|
rate_bf_request.z = yaw_in * g.acro_yaw_p;
|
|
|
|
|
|
|
|
// calculate earth frame rate corrections to pull the copter back to level while in ACRO mode
|
|
|
|
|
2014-02-09 21:26:49 -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) {
|
|
|
|
if (roll_angle > aparm.angle_max){
|
2014-07-12 05:49:20 -03:00
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle-aparm.angle_max);
|
2014-02-09 21:26:49 -04:00
|
|
|
}else if (roll_angle < -aparm.angle_max) {
|
2014-07-12 05:49:20 -03:00
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle+aparm.angle_max);
|
2014-02-09 21:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pitch_angle > aparm.angle_max){
|
2014-07-12 05:49:20 -03:00
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-aparm.angle_max);
|
2014-02-09 21:26:49 -04:00
|
|
|
}else if (pitch_angle < -aparm.angle_max) {
|
2014-07-12 05:49:20 -03:00
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+aparm.angle_max);
|
2014-02-09 21:26:49 -04:00
|
|
|
}
|
2014-01-24 23:00:07 -04:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:26:49 -04:00
|
|
|
// convert earth-frame level rates to body-frame level rates
|
2014-02-13 22:56:46 -04:00
|
|
|
attitude_control.frame_conversion_ef_to_bf(rate_ef_level, rate_bf_level);
|
2014-02-09 21:26:49 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}else{
|
|
|
|
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 = fabs(fabs(rate_bf_request.x)-fabs(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 = fabs(fabs(rate_bf_request.y)-fabs(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 = fabs(fabs(rate_bf_request.z)-fabs(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);
|
|
|
|
}
|
2014-01-24 23:00:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// hand back rate request
|
|
|
|
roll_out = rate_bf_request.x;
|
|
|
|
pitch_out = rate_bf_request.y;
|
|
|
|
yaw_out = rate_bf_request.z;
|
|
|
|
}
|