2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2017-12-10 23:57:04 -04:00
|
|
|
#include "mode.h"
|
2014-01-24 23:00:07 -04:00
|
|
|
|
2018-03-14 17:14:49 -03:00
|
|
|
#if MODE_ACRO_ENABLED == ENABLED
|
|
|
|
|
2014-01-24 23:00:07 -04:00
|
|
|
/*
|
2016-07-25 15:45:29 -03:00
|
|
|
* Init and run calls for acro flight mode
|
2014-01-24 23:00:07 -04:00
|
|
|
*/
|
|
|
|
|
2017-12-10 23:51:13 -04:00
|
|
|
bool Copter::ModeAcro::init(bool ignore_checks)
|
2014-01-24 23:00:07 -04:00
|
|
|
{
|
2015-10-20 01:56:18 -03:00
|
|
|
// if landed and the mode we're switching from does not have manual throttle and the throttle stick is too high
|
2018-02-07 22:21:09 -04:00
|
|
|
if (motors->armed() && ap.land_complete && !copter.flightmode->has_manual_throttle() &&
|
|
|
|
(get_pilot_desired_throttle(channel_throttle->get_control_in(), copter.g2.acro_thr_mid) > copter.get_non_takeoff_throttle())) {
|
2015-10-20 01:56:18 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-18 08:47:07 -04:00
|
|
|
|
2015-10-20 01:56:18 -03:00
|
|
|
return true;
|
2014-01-24 23:00:07 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 23:51:13 -04:00
|
|
|
void Copter::ModeAcro::run()
|
2014-01-24 23:00:07 -04:00
|
|
|
{
|
2014-02-09 21:26:49 -04:00
|
|
|
float target_roll, target_pitch, target_yaw;
|
2016-01-18 01:36:44 -04:00
|
|
|
float pilot_throttle_scaled;
|
2014-01-24 23:00:07 -04:00
|
|
|
|
2016-01-18 01:36:44 -04:00
|
|
|
// if not armed set throttle to zero and exit immediately
|
2017-01-09 03:31:26 -04:00
|
|
|
if (!motors->armed() || ap.throttle_zero || !motors->get_interlock()) {
|
2017-12-28 22:36:18 -04:00
|
|
|
zero_throttle_and_relax_ac();
|
2014-01-30 08:44:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:36:39 -03:00
|
|
|
// clear landing flag
|
|
|
|
set_land_complete(false);
|
|
|
|
|
2017-01-09 03:31:26 -04:00
|
|
|
motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
|
2016-01-18 01:36:44 -04:00
|
|
|
|
2014-01-24 23:00:07 -04:00
|
|
|
// convert the input to the desired body frame rate
|
ArduCopter: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:46:59 -03: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);
|
2014-01-24 23:00:07 -04:00
|
|
|
|
2014-01-30 20:55:00 -04:00
|
|
|
// get pilot's desired throttle
|
2016-06-22 01:16:28 -03:00
|
|
|
pilot_throttle_scaled = get_pilot_desired_throttle(channel_throttle->get_control_in(), g2.acro_thr_mid);
|
2014-01-30 20:55:00 -04:00
|
|
|
|
|
|
|
// run attitude controller
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->input_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
|
2018-02-07 22:21:09 -04:00
|
|
|
attitude_control->set_throttle_out(pilot_throttle_scaled, false, copter.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
|
2017-12-10 23:51:13 -04:00
|
|
|
void Copter::ModeAcro::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
|
2016-04-16 06:58:46 -03:00
|
|
|
float total_in = norm(pitch_in, roll_in);
|
2014-11-10 20:07:08 -04:00
|
|
|
|
2016-10-23 03:59:09 -03:00
|
|
|
if (total_in > ROLL_PITCH_YAW_INPUT_MAX) {
|
|
|
|
float ratio = (float)ROLL_PITCH_YAW_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
|
2016-06-22 01:16:28 -03:00
|
|
|
if (g.acro_rp_expo <= 0) {
|
2014-08-12 23:25:59 -03:00
|
|
|
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
|
2016-06-22 01:16:28 -03:00
|
|
|
if (g.acro_rp_expo > 1.0f) {
|
|
|
|
g.acro_rp_expo = 1.0f;
|
2014-08-18 00:56:22 -03:00
|
|
|
}
|
|
|
|
|
2014-08-12 23:25:59 -03:00
|
|
|
// roll expo
|
2016-10-23 03:59:09 -03:00
|
|
|
rp_in = float(roll_in)/ROLL_PITCH_YAW_INPUT_MAX;
|
2014-08-12 23:25:59 -03:00
|
|
|
rp_in3 = rp_in*rp_in*rp_in;
|
2016-06-22 01:16:28 -03:00
|
|
|
rp_out = (g.acro_rp_expo * rp_in3) + ((1.0f - g.acro_rp_expo) * rp_in);
|
2016-10-23 03:59:09 -03:00
|
|
|
rate_bf_request.x = ROLL_PITCH_YAW_INPUT_MAX * rp_out * g.acro_rp_p;
|
2014-08-12 23:25:59 -03:00
|
|
|
|
|
|
|
// pitch expo
|
2016-10-23 03:59:09 -03:00
|
|
|
rp_in = float(pitch_in)/ROLL_PITCH_YAW_INPUT_MAX;
|
2014-08-12 23:25:59 -03:00
|
|
|
rp_in3 = rp_in*rp_in*rp_in;
|
2016-06-22 01:16:28 -03:00
|
|
|
rp_out = (g.acro_rp_expo * rp_in3) + ((1.0f - g.acro_rp_expo) * rp_in);
|
2016-10-23 03:59:09 -03:00
|
|
|
rate_bf_request.y = ROLL_PITCH_YAW_INPUT_MAX * rp_out * g.acro_rp_p;
|
2014-08-12 23:25:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate yaw rate request
|
2016-10-23 03:55:33 -03:00
|
|
|
rate_bf_request.z = get_pilot_desired_yaw_rate(yaw_in);
|
2014-01-24 23:00:07 -04:00
|
|
|
|
|
|
|
// 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) {
|
2017-05-23 02:02:33 -03:00
|
|
|
|
|
|
|
// get attitude targets
|
|
|
|
const Vector3f att_target = attitude_control->get_att_target_euler_cd();
|
|
|
|
|
2014-02-09 21:26:49 -04:00
|
|
|
// Calculate trainer mode earth frame rate command for roll
|
2017-05-23 02:02:33 -03:00
|
|
|
int32_t roll_angle = wrap_180_cd(att_target.x);
|
2014-02-09 21:26:49 -04:00
|
|
|
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
|
2017-05-23 02:02:33 -03:00
|
|
|
int32_t pitch_angle = wrap_180_cd(att_target.y);
|
2014-02-09 21:26:49 -04:00
|
|
|
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) {
|
2018-10-03 00:14:44 -03:00
|
|
|
const float angle_max = copter.aparm.angle_max;
|
|
|
|
if (roll_angle > angle_max){
|
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle-angle_max);
|
|
|
|
}else if (roll_angle < -angle_max) {
|
|
|
|
rate_ef_level.x -= g.acro_balance_roll*(roll_angle+angle_max);
|
2014-02-09 21:26:49 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 00:14:44 -03:00
|
|
|
if (pitch_angle > angle_max){
|
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle-angle_max);
|
|
|
|
}else if (pitch_angle < -angle_max) {
|
|
|
|
rate_ef_level.y -= g.acro_balance_pitch*(pitch_angle+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
|
2017-01-09 03:31:26 -04:00
|
|
|
attitude_control->euler_rate_to_ang_vel(attitude_control->get_att_target_euler_cd()*radians(0.01f), 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{
|
2019-03-11 13:15:25 -03:00
|
|
|
float 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();
|
2014-02-09 21:26:49 -04:00
|
|
|
|
|
|
|
// 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
|
2015-05-08 15:40:08 -03:00
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.x)-fabsf(rate_bf_level.x));
|
2014-02-09 21:26:49 -04:00
|
|
|
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
|
2015-05-15 12:53:29 -03:00
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.y)-fabsf(rate_bf_level.y));
|
2014-02-09 21:26:49 -04:00
|
|
|
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
|
2015-05-08 15:40:08 -03:00
|
|
|
rate_limit = fabsf(fabsf(rate_bf_request.z)-fabsf(rate_bf_level.z));
|
2014-02-09 21:26:49 -04:00
|
|
|
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;
|
|
|
|
}
|
2018-03-14 17:14:49 -03:00
|
|
|
#endif
|