2011-09-08 22:29:39 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
//****************************************************************
|
|
|
|
// Function that controls aileron/rudder, elevator, rudder (if 4 channel control) and throttle to produce desired attitude and airspeed.
|
|
|
|
//****************************************************************
|
|
|
|
|
|
|
|
|
2012-09-11 00:01:36 -03:00
|
|
|
/*
|
|
|
|
get a speed scaling number for control surfaces. This is applied to
|
|
|
|
PIDs to change the scaling of the PID with speed. At high speed we
|
|
|
|
move the surfaces less, and at low speeds we move them more.
|
|
|
|
*/
|
|
|
|
static float get_speed_scaler(void)
|
|
|
|
{
|
|
|
|
float aspeed, speed_scaler;
|
2012-08-24 09:03:03 -03:00
|
|
|
if (ahrs.airspeed_estimate(&aspeed)) {
|
2012-08-21 23:19:50 -03:00
|
|
|
if (aspeed > 0) {
|
|
|
|
speed_scaler = g.scaling_speed / aspeed;
|
2012-07-15 22:21:50 -03:00
|
|
|
} else {
|
2012-08-21 23:19:50 -03:00
|
|
|
speed_scaler = 2.0;
|
2012-07-15 22:21:50 -03:00
|
|
|
}
|
2013-05-01 21:27:10 -03:00
|
|
|
speed_scaler = constrain_float(speed_scaler, 0.5, 2.0);
|
2012-08-21 23:19:50 -03:00
|
|
|
} else {
|
2013-06-03 02:32:08 -03:00
|
|
|
if (channel_throttle->servo_out > 0) {
|
|
|
|
speed_scaler = 0.5 + ((float)THROTTLE_CRUISE / channel_throttle->servo_out / 2.0); // First order taylor expansion of square root
|
2012-08-21 23:19:50 -03:00
|
|
|
// Should maybe be to the 2/7 power, but we aren't goint to implement that...
|
|
|
|
}else{
|
|
|
|
speed_scaler = 1.67;
|
|
|
|
}
|
2012-08-24 09:03:03 -03:00
|
|
|
// This case is constrained tighter as we don't have real speed info
|
2013-05-01 21:27:10 -03:00
|
|
|
speed_scaler = constrain_float(speed_scaler, 0.6, 1.67);
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2012-09-11 00:01:36 -03:00
|
|
|
return speed_scaler;
|
|
|
|
}
|
|
|
|
|
2012-09-22 20:33:17 -03:00
|
|
|
/*
|
|
|
|
return true if the current settings and mode should allow for stick mixing
|
|
|
|
*/
|
|
|
|
static bool stick_mixing_enabled(void)
|
|
|
|
{
|
2013-07-05 01:55:22 -03:00
|
|
|
if (auto_throttle_mode) {
|
2012-09-23 18:13:57 -03:00
|
|
|
// we're in an auto mode. Check the stick mixing flag
|
2013-04-01 18:52:56 -03:00
|
|
|
if (g.stick_mixing != STICK_MIXING_DISABLED &&
|
2012-09-23 18:13:57 -03:00
|
|
|
geofence_stickmixing() &&
|
2013-04-01 20:42:56 -03:00
|
|
|
failsafe == FAILSAFE_NONE &&
|
|
|
|
(g.throttle_fs_enabled == 0 ||
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->radio_in >= g.throttle_fs_value)) {
|
2012-09-23 18:13:57 -03:00
|
|
|
// we're in an auto mode, and haven't triggered failsafe
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-22 20:33:17 -03:00
|
|
|
}
|
2012-09-23 18:13:57 -03:00
|
|
|
// non-auto mode. Always do stick mixing
|
|
|
|
return true;
|
2012-09-22 20:33:17 -03:00
|
|
|
}
|
|
|
|
|
2012-09-11 00:01:36 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
/*
|
|
|
|
this is the main roll stabilization function. It takes the
|
|
|
|
previously set nav_roll calculates roll servo_out to try to
|
|
|
|
stabilize the plane at the given roll
|
|
|
|
*/
|
|
|
|
static void stabilize_roll(float speed_scaler)
|
2012-09-11 00:01:36 -03:00
|
|
|
{
|
2011-09-09 11:18:38 -03:00
|
|
|
if (inverted_flight) {
|
|
|
|
// we want to fly upside down. We need to cope with wrap of
|
|
|
|
// the roll_sensor interfering with wrap of nav_roll, which
|
|
|
|
// would really confuse the PID code. The easiest way to
|
|
|
|
// handle this is to ensure both go in the same direction from
|
|
|
|
// zero
|
2012-08-07 03:05:51 -03:00
|
|
|
nav_roll_cd += 18000;
|
|
|
|
if (ahrs.roll_sensor < 0) nav_roll_cd -= 36000;
|
2011-09-09 11:18:38 -03:00
|
|
|
}
|
|
|
|
|
2013-07-13 07:28:07 -03:00
|
|
|
channel_roll->servo_out = g.rollController.get_servo_out(nav_roll_cd - ahrs.roll_sensor,
|
|
|
|
speed_scaler,
|
2013-06-26 07:48:45 -03:00
|
|
|
control_mode == STABILIZE,
|
|
|
|
aparm.flybywire_airspeed_min);
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
this is the main pitch stabilization function. It takes the
|
|
|
|
previously set nav_pitch and calculates servo_out values to try to
|
|
|
|
stabilize the plane at the given attitude.
|
|
|
|
*/
|
|
|
|
static void stabilize_pitch(float speed_scaler)
|
|
|
|
{
|
2013-06-03 02:32:08 -03:00
|
|
|
int32_t demanded_pitch = nav_pitch_cd + g.pitch_trim_cd + channel_throttle->servo_out * g.kff_throttle_to_pitch;
|
2013-07-13 07:28:07 -03:00
|
|
|
channel_pitch->servo_out = g.pitchController.get_servo_out(demanded_pitch - ahrs.pitch_sensor,
|
2013-06-26 07:48:45 -03:00
|
|
|
speed_scaler,
|
|
|
|
control_mode == STABILIZE,
|
|
|
|
aparm.flybywire_airspeed_min,
|
|
|
|
aparm.flybywire_airspeed_max);
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
/*
|
|
|
|
this gives the user control of the aircraft in stabilization modes
|
|
|
|
*/
|
2013-04-01 18:52:56 -03:00
|
|
|
static void stabilize_stick_mixing_direct()
|
2012-12-04 02:32:37 -04:00
|
|
|
{
|
|
|
|
if (!stick_mixing_enabled() ||
|
2013-07-10 10:25:38 -03:00
|
|
|
control_mode == ACRO ||
|
2012-12-04 02:32:37 -04:00
|
|
|
control_mode == FLY_BY_WIRE_A ||
|
|
|
|
control_mode == FLY_BY_WIRE_B ||
|
2013-07-13 07:05:53 -03:00
|
|
|
control_mode == CRUISE ||
|
2012-12-04 02:32:37 -04:00
|
|
|
control_mode == TRAINING) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 18:52:56 -03:00
|
|
|
// do direct stick mixing on aileron/elevator
|
2012-12-04 02:32:37 -04:00
|
|
|
float ch1_inf;
|
|
|
|
float ch2_inf;
|
|
|
|
|
2013-06-03 02:32:08 -03:00
|
|
|
ch1_inf = (float)channel_roll->radio_in - (float)channel_roll->radio_trim;
|
2013-01-10 14:42:24 -04:00
|
|
|
ch1_inf = fabsf(ch1_inf);
|
2012-12-04 02:32:37 -04:00
|
|
|
ch1_inf = min(ch1_inf, 400.0);
|
|
|
|
ch1_inf = ((400.0 - ch1_inf) /400.0);
|
|
|
|
|
2013-06-03 02:32:08 -03:00
|
|
|
ch2_inf = (float)channel_pitch->radio_in - channel_pitch->radio_trim;
|
2013-01-10 14:42:24 -04:00
|
|
|
ch2_inf = fabsf(ch2_inf);
|
2012-12-04 02:32:37 -04:00
|
|
|
ch2_inf = min(ch2_inf, 400.0);
|
|
|
|
ch2_inf = ((400.0 - ch2_inf) /400.0);
|
|
|
|
|
|
|
|
// scale the sensor input based on the stick input
|
|
|
|
// -----------------------------------------------
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->servo_out *= ch1_inf;
|
|
|
|
channel_pitch->servo_out *= ch2_inf;
|
2013-04-01 18:52:56 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
// Mix in stick inputs
|
|
|
|
// -------------------
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->servo_out += channel_roll->pwm_to_angle();
|
|
|
|
channel_pitch->servo_out += channel_pitch->pwm_to_angle();
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
|
|
|
|
2013-04-01 18:52:56 -03:00
|
|
|
/*
|
|
|
|
this gives the user control of the aircraft in stabilization modes
|
|
|
|
using FBW style controls
|
|
|
|
*/
|
|
|
|
static void stabilize_stick_mixing_fbw()
|
|
|
|
{
|
|
|
|
if (!stick_mixing_enabled() ||
|
2013-07-10 10:25:38 -03:00
|
|
|
control_mode == ACRO ||
|
2013-04-01 18:52:56 -03:00
|
|
|
control_mode == FLY_BY_WIRE_A ||
|
|
|
|
control_mode == FLY_BY_WIRE_B ||
|
2013-07-13 07:05:53 -03:00
|
|
|
control_mode == CRUISE ||
|
2013-04-01 18:52:56 -03:00
|
|
|
control_mode == TRAINING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// do FBW style stick mixing. We don't treat it linearly
|
|
|
|
// however. For inputs up to half the maximum, we use linear
|
|
|
|
// addition to the nav_roll and nav_pitch. Above that it goes
|
|
|
|
// non-linear and ends up as 2x the maximum, to ensure that
|
|
|
|
// the user can direct the plane in any direction with stick
|
|
|
|
// mixing.
|
2013-06-03 02:32:08 -03:00
|
|
|
float roll_input = channel_roll->norm_input();
|
2013-05-26 19:24:35 -03:00
|
|
|
if (roll_input > 0.5f) {
|
2013-04-01 18:52:56 -03:00
|
|
|
roll_input = (3*roll_input - 1);
|
2013-05-26 19:24:35 -03:00
|
|
|
} else if (roll_input < -0.5f) {
|
|
|
|
roll_input = (3*roll_input + 1);
|
2013-04-01 18:52:56 -03:00
|
|
|
}
|
|
|
|
nav_roll_cd += roll_input * g.roll_limit_cd;
|
|
|
|
nav_roll_cd = constrain_int32(nav_roll_cd, -g.roll_limit_cd.get(), g.roll_limit_cd.get());
|
|
|
|
|
2013-06-03 02:32:08 -03:00
|
|
|
float pitch_input = channel_pitch->norm_input();
|
2013-04-01 18:52:56 -03:00
|
|
|
if (fabsf(pitch_input) > 0.5f) {
|
|
|
|
pitch_input = (3*pitch_input - 1);
|
|
|
|
}
|
|
|
|
if (inverted_flight) {
|
|
|
|
pitch_input = -pitch_input;
|
|
|
|
}
|
|
|
|
if (pitch_input > 0) {
|
2013-06-26 07:48:45 -03:00
|
|
|
nav_pitch_cd += pitch_input * aparm.pitch_limit_max_cd;
|
2013-04-01 18:52:56 -03:00
|
|
|
} else {
|
2013-06-26 07:48:45 -03:00
|
|
|
nav_pitch_cd += -(pitch_input * aparm.pitch_limit_min_cd);
|
2013-04-01 18:52:56 -03:00
|
|
|
}
|
2013-06-26 07:48:45 -03:00
|
|
|
nav_pitch_cd = constrain_int32(nav_pitch_cd, aparm.pitch_limit_min_cd.get(), aparm.pitch_limit_max_cd.get());
|
2013-04-01 18:52:56 -03:00
|
|
|
}
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
/*
|
|
|
|
stabilize the yaw axis
|
|
|
|
*/
|
|
|
|
static void stabilize_yaw(float speed_scaler)
|
|
|
|
{
|
|
|
|
float ch4_inf = 1.0;
|
|
|
|
|
|
|
|
if (stick_mixing_enabled()) {
|
2012-09-23 18:13:57 -03:00
|
|
|
// stick mixing performed for rudder for all cases including FBW
|
|
|
|
// important for steering on the ground during landing
|
|
|
|
// -----------------------------------------------
|
2013-06-03 02:32:08 -03:00
|
|
|
ch4_inf = (float)channel_rudder->radio_in - (float)channel_rudder->radio_trim;
|
2013-01-10 14:42:24 -04:00
|
|
|
ch4_inf = fabsf(ch4_inf);
|
2012-09-23 18:13:57 -03:00
|
|
|
ch4_inf = min(ch4_inf, 400.0);
|
|
|
|
ch4_inf = ((400.0 - ch4_inf) /400.0);
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
// Apply output to Rudder
|
|
|
|
calc_nav_yaw(speed_scaler, ch4_inf);
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->servo_out *= ch4_inf;
|
|
|
|
channel_rudder->servo_out += channel_rudder->pwm_to_angle();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
a special stabilization function for training mode
|
|
|
|
*/
|
|
|
|
static void stabilize_training(float speed_scaler)
|
|
|
|
{
|
|
|
|
if (training_manual_roll) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->servo_out = channel_roll->control_in;
|
2012-12-04 02:32:37 -04:00
|
|
|
} else {
|
|
|
|
// calculate what is needed to hold
|
|
|
|
stabilize_roll(speed_scaler);
|
2013-06-03 02:32:08 -03:00
|
|
|
if ((nav_roll_cd > 0 && channel_roll->control_in < channel_roll->servo_out) ||
|
|
|
|
(nav_roll_cd < 0 && channel_roll->control_in > channel_roll->servo_out)) {
|
2012-12-04 02:32:37 -04:00
|
|
|
// allow user to get out of the roll
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->servo_out = channel_roll->control_in;
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (training_manual_pitch) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_pitch->servo_out = channel_pitch->control_in;
|
2012-12-04 02:32:37 -04:00
|
|
|
} else {
|
|
|
|
stabilize_pitch(speed_scaler);
|
2013-06-03 02:32:08 -03:00
|
|
|
if ((nav_pitch_cd > 0 && channel_pitch->control_in < channel_pitch->servo_out) ||
|
|
|
|
(nav_pitch_cd < 0 && channel_pitch->control_in > channel_pitch->servo_out)) {
|
2012-12-04 02:32:37 -04:00
|
|
|
// allow user to get back to level
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_pitch->servo_out = channel_pitch->control_in;
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stabilize_yaw(speed_scaler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-10 10:25:38 -03:00
|
|
|
/*
|
|
|
|
this is the ACRO mode stabilization function. It does rate
|
|
|
|
stabilization on roll and pitch axes
|
|
|
|
*/
|
|
|
|
static void stabilize_acro(float speed_scaler)
|
|
|
|
{
|
2013-07-12 04:53:53 -03:00
|
|
|
float roll_rate = (channel_roll->control_in/4500.0f) * g.acro_roll_rate;
|
|
|
|
float pitch_rate = (channel_pitch->control_in/4500.0f) * g.acro_pitch_rate;
|
2013-07-10 10:25:38 -03:00
|
|
|
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
check for special roll handling near the pitch poles
|
|
|
|
*/
|
2013-07-13 08:06:45 -03:00
|
|
|
if (roll_rate == 0) {
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
we have no roll stick input, so we will enter "roll locked"
|
|
|
|
mode, and hold the roll we had when the stick was released
|
|
|
|
*/
|
2013-07-10 10:25:38 -03:00
|
|
|
if (!acro_state.locked_roll) {
|
|
|
|
acro_state.locked_roll = true;
|
2013-07-13 08:06:45 -03:00
|
|
|
acro_state.locked_roll_err = 0;
|
|
|
|
} else {
|
|
|
|
acro_state.locked_roll_err += ahrs.get_gyro().x * 0.02f;
|
2013-07-10 10:25:38 -03:00
|
|
|
}
|
2013-07-13 08:06:45 -03:00
|
|
|
int32_t roll_error_cd = -ToDeg(acro_state.locked_roll_err)*100;
|
|
|
|
nav_roll_cd = ahrs.roll_sensor + roll_error_cd;
|
|
|
|
// try to reduce the integrated angular error to zero. We set
|
|
|
|
// 'stabilze' to true, which disables the roll integrator
|
|
|
|
channel_roll->servo_out = g.rollController.get_servo_out(roll_error_cd,
|
|
|
|
speed_scaler,
|
|
|
|
true,
|
|
|
|
aparm.flybywire_airspeed_min);
|
2013-07-10 10:25:38 -03:00
|
|
|
} else {
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
aileron stick is non-zero, use pure rate control until the
|
|
|
|
user releases the stick
|
|
|
|
*/
|
2013-07-10 10:25:38 -03:00
|
|
|
acro_state.locked_roll = false;
|
|
|
|
channel_roll->servo_out = g.rollController.get_rate_out(roll_rate, speed_scaler);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pitch_rate == 0) {
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
user has zero pitch stick input, so we lock pitch at the
|
|
|
|
point they release the stick
|
|
|
|
*/
|
2013-07-10 10:25:38 -03:00
|
|
|
if (!acro_state.locked_pitch) {
|
|
|
|
acro_state.locked_pitch = true;
|
|
|
|
acro_state.locked_pitch_cd = ahrs.pitch_sensor;
|
|
|
|
}
|
2013-07-13 08:06:45 -03:00
|
|
|
// try to hold the locked pitch. Note that we have the pitch
|
|
|
|
// integrator enabled, which helps with inverted flight
|
2013-07-10 10:25:38 -03:00
|
|
|
nav_pitch_cd = acro_state.locked_pitch_cd;
|
2013-07-13 08:06:45 -03:00
|
|
|
channel_pitch->servo_out = g.pitchController.get_servo_out(nav_pitch_cd - ahrs.pitch_sensor,
|
|
|
|
speed_scaler,
|
|
|
|
false,
|
|
|
|
aparm.flybywire_airspeed_min,
|
|
|
|
aparm.flybywire_airspeed_max);
|
2013-07-10 10:25:38 -03:00
|
|
|
} else {
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
user has non-zero pitch input, use a pure rate controller
|
|
|
|
*/
|
2013-07-10 10:25:38 -03:00
|
|
|
acro_state.locked_pitch = false;
|
|
|
|
channel_pitch->servo_out = g.pitchController.get_rate_out(pitch_rate, speed_scaler);
|
|
|
|
}
|
|
|
|
|
2013-07-10 19:05:25 -03:00
|
|
|
/*
|
|
|
|
call the normal yaw stabilize for now. This allows for manual
|
|
|
|
rudder input, plus automatic coordinated turn handling. For
|
|
|
|
knife-edge we'll need to do something quite different
|
|
|
|
*/
|
2013-07-10 10:25:38 -03:00
|
|
|
stabilize_yaw(speed_scaler);
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
/*
|
|
|
|
main stabilization function for all 3 axes
|
|
|
|
*/
|
|
|
|
static void stabilize()
|
|
|
|
{
|
2013-07-05 05:05:27 -03:00
|
|
|
if (control_mode == MANUAL) {
|
|
|
|
// nothing to do
|
|
|
|
return;
|
|
|
|
}
|
2012-12-04 02:32:37 -04:00
|
|
|
float speed_scaler = get_speed_scaler();
|
|
|
|
|
|
|
|
if (control_mode == TRAINING) {
|
|
|
|
stabilize_training(speed_scaler);
|
2013-07-10 10:25:38 -03:00
|
|
|
} else if (control_mode == ACRO) {
|
|
|
|
stabilize_acro(speed_scaler);
|
2012-12-04 02:32:37 -04:00
|
|
|
} else {
|
2013-04-01 18:52:56 -03:00
|
|
|
if (g.stick_mixing == STICK_MIXING_FBW && control_mode != STABILIZE) {
|
|
|
|
stabilize_stick_mixing_fbw();
|
|
|
|
}
|
2012-12-04 02:32:37 -04:00
|
|
|
stabilize_roll(speed_scaler);
|
|
|
|
stabilize_pitch(speed_scaler);
|
2013-04-01 18:52:56 -03:00
|
|
|
if (g.stick_mixing == STICK_MIXING_DIRECT || control_mode == STABILIZE) {
|
|
|
|
stabilize_stick_mixing_direct();
|
|
|
|
}
|
2012-12-04 02:32:37 -04:00
|
|
|
stabilize_yaw(speed_scaler);
|
|
|
|
}
|
2013-06-01 04:34:40 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
see if we should zero the attitude controller integrators.
|
|
|
|
*/
|
2013-06-03 02:32:08 -03:00
|
|
|
if (channel_throttle->control_in == 0 &&
|
2013-07-10 00:40:13 -03:00
|
|
|
relative_altitude_abs_cm() < 500 &&
|
2013-06-01 04:34:40 -03:00
|
|
|
fabs(barometer.get_climb_rate()) < 0.5f &&
|
2013-07-10 01:03:03 -03:00
|
|
|
g_gps->ground_speed_cm < 300) {
|
2013-06-01 04:34:40 -03:00
|
|
|
// we are low, with no climb rate, and zero throttle, and very
|
|
|
|
// low ground speed. Zero the attitude controller
|
|
|
|
// integrators. This prevents integrator buildup pre-takeoff.
|
|
|
|
g.rollController.reset_I();
|
|
|
|
g.pitchController.reset_I();
|
|
|
|
g.yawController.reset_I();
|
|
|
|
}
|
2012-12-04 02:32:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 11:18:38 -03:00
|
|
|
static void calc_throttle()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-06-26 07:48:45 -03:00
|
|
|
if (aparm.throttle_cruise <= 1) {
|
2013-03-07 23:59:19 -04:00
|
|
|
// user has asked for zero throttle - this may be done by a
|
|
|
|
// mission which wants to turn off the engine for a parachute
|
|
|
|
// landing
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out = 0;
|
2013-03-07 23:59:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-14 22:12:24 -03:00
|
|
|
if (g.alt_control_algorithm == ALT_CONTROL_TECS || g.alt_control_algorithm == ALT_CONTROL_DEFAULT) {
|
2013-06-26 05:36:53 -03:00
|
|
|
channel_throttle->servo_out = SpdHgt_Controller->get_throttle_demand();
|
|
|
|
} else if (!alt_control_airspeed()) {
|
2013-06-26 07:48:45 -03:00
|
|
|
int16_t throttle_target = aparm.throttle_cruise + throttle_nudge;
|
2011-12-09 19:40:56 -04:00
|
|
|
|
2012-07-15 22:21:50 -03:00
|
|
|
// TODO: think up an elegant way to bump throttle when
|
|
|
|
// groundspeed_undershoot > 0 in the no airspeed sensor case; PID
|
|
|
|
// control?
|
2011-12-09 19:40:56 -04:00
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// no airspeed sensor, we use nav pitch to determine the proper throttle output
|
|
|
|
// AUTO, RTL, etc
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
if (nav_pitch_cd >= 0) {
|
2013-06-26 07:48:45 -03:00
|
|
|
channel_throttle->servo_out = throttle_target + (aparm.throttle_max - throttle_target) * nav_pitch_cd / aparm.pitch_limit_max_cd;
|
2012-08-21 23:19:50 -03:00
|
|
|
} else {
|
2013-06-26 07:48:45 -03:00
|
|
|
channel_throttle->servo_out = throttle_target - (throttle_target - aparm.throttle_min) * nav_pitch_cd / aparm.pitch_limit_min_cd;
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
2013-06-26 07:48:45 -03:00
|
|
|
channel_throttle->servo_out = constrain_int16(channel_throttle->servo_out, aparm.throttle_min.get(), aparm.throttle_max.get());
|
2012-08-21 23:19:50 -03:00
|
|
|
} else {
|
|
|
|
// throttle control with airspeed compensation
|
|
|
|
// -------------------------------------------
|
|
|
|
energy_error = airspeed_energy_error + altitude_error_cm * 0.098f;
|
|
|
|
|
|
|
|
// positive energy errors make the throttle go higher
|
2013-06-26 07:48:45 -03:00
|
|
|
channel_throttle->servo_out = aparm.throttle_cruise + g.pidTeThrottle.get_pid(energy_error);
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out += (channel_pitch->servo_out * g.kff_pitch_to_throttle);
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out = constrain_int16(channel_throttle->servo_out,
|
2013-06-26 07:48:45 -03:00
|
|
|
aparm.throttle_min.get(), aparm.throttle_max.get());
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2013-06-26 05:36:53 -03:00
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************
|
2012-08-21 23:19:50 -03:00
|
|
|
* Calculate desired roll/pitch/yaw angles (in medium freq loop)
|
|
|
|
*****************************************/
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
// Yaw is separated into a function for heading hold on rolling take-off
|
|
|
|
// ----------------------------------------------------------------------
|
2012-08-21 23:34:40 -03:00
|
|
|
static void calc_nav_yaw(float speed_scaler, float ch4_inf)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-04-11 21:25:46 -03:00
|
|
|
if (hold_course_cd != -1) {
|
2012-08-22 04:34:01 -03:00
|
|
|
// steering on or close to ground
|
2013-04-11 21:25:46 -03:00
|
|
|
int32_t bearing_error_cd = nav_controller->bearing_error_cd();
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->servo_out = g.pidWheelSteer.get_pid_4500(bearing_error_cd, speed_scaler) +
|
|
|
|
g.kff_rudder_mix * channel_roll->servo_out;
|
|
|
|
channel_rudder->servo_out = constrain_int16(channel_rudder->servo_out, -4500, 4500);
|
2012-08-22 04:34:01 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->servo_out = g.yawController.get_servo_out(speed_scaler,
|
2013-06-26 07:48:45 -03:00
|
|
|
control_mode == STABILIZE,
|
|
|
|
aparm.flybywire_airspeed_min,
|
|
|
|
aparm.flybywire_airspeed_max);
|
2012-08-14 22:19:12 -03:00
|
|
|
|
2013-05-05 02:31:41 -03:00
|
|
|
// add in rudder mixing from roll
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->servo_out += channel_roll->servo_out * g.kff_rudder_mix;
|
|
|
|
channel_rudder->servo_out = constrain_int16(channel_rudder->servo_out, -4500, 4500);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 11:18:38 -03:00
|
|
|
static void calc_nav_pitch()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
// Calculate the Pitch of the plane
|
|
|
|
// --------------------------------
|
2013-07-14 22:12:24 -03:00
|
|
|
if (g.alt_control_algorithm == ALT_CONTROL_TECS || g.alt_control_algorithm == ALT_CONTROL_DEFAULT) {
|
2013-06-26 05:36:53 -03:00
|
|
|
nav_pitch_cd = SpdHgt_Controller->get_pitch_demand();
|
|
|
|
} else if (alt_control_airspeed()) {
|
2012-08-21 23:19:50 -03:00
|
|
|
nav_pitch_cd = -g.pidNavPitchAirspeed.get_pid(airspeed_error_cm);
|
|
|
|
} else {
|
|
|
|
nav_pitch_cd = g.pidNavPitchAltitude.get_pid(altitude_error_cm);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
2013-06-26 07:48:45 -03:00
|
|
|
nav_pitch_cd = constrain_int32(nav_pitch_cd, aparm.pitch_limit_min_cd.get(), aparm.pitch_limit_max_cd.get());
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 11:18:38 -03:00
|
|
|
static void calc_nav_roll()
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-04-11 21:25:46 -03:00
|
|
|
nav_roll_cd = nav_controller->nav_roll_cd();
|
2012-12-18 22:36:00 -04:00
|
|
|
nav_roll_cd = constrain_int32(nav_roll_cd, -g.roll_limit_cd.get(), g.roll_limit_cd.get());
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************
|
2012-08-21 23:19:50 -03:00
|
|
|
* Roll servo slew limit
|
|
|
|
*****************************************/
|
2011-09-08 22:29:39 -03:00
|
|
|
/*
|
2012-08-21 23:19:50 -03:00
|
|
|
* float roll_slew_limit(float servo)
|
|
|
|
* {
|
|
|
|
* static float last;
|
2013-05-01 21:27:10 -03:00
|
|
|
* float temp = constrain_float(servo, last-ROLL_SLEW_LIMIT * delta_ms_fast_loop/1000.f, last + ROLL_SLEW_LIMIT * delta_ms_fast_loop/1000.f);
|
2012-08-21 23:19:50 -03:00
|
|
|
* last = servo;
|
|
|
|
* return temp;
|
|
|
|
* }*/
|
2011-09-08 22:29:39 -03:00
|
|
|
|
|
|
|
/*****************************************
|
2012-08-21 23:19:50 -03:00
|
|
|
* Throttle slew limit
|
|
|
|
*****************************************/
|
2012-11-27 21:13:09 -04:00
|
|
|
static void throttle_slew_limit(int16_t last_throttle)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2012-11-27 20:42:05 -04:00
|
|
|
// if slew limit rate is set to zero then do not slew limit
|
2013-06-26 07:48:45 -03:00
|
|
|
if (aparm.throttle_slewrate) {
|
2012-11-27 20:42:05 -04:00
|
|
|
// limit throttle change by the given percentage per second
|
2013-06-26 07:48:45 -03:00
|
|
|
float temp = aparm.throttle_slewrate * G_Dt * 0.01 * fabsf(channel_throttle->radio_max - channel_throttle->radio_min);
|
2012-11-27 21:13:09 -04:00
|
|
|
// allow a minimum change of 1 PWM per cycle
|
|
|
|
if (temp < 1) {
|
|
|
|
temp = 1;
|
|
|
|
}
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->radio_out = constrain_int16(channel_throttle->radio_out, last_throttle - temp, last_throttle + temp);
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
2011-09-09 11:18:38 -03:00
|
|
|
|
2013-06-26 05:36:53 -03:00
|
|
|
|
2013-03-08 23:41:04 -04:00
|
|
|
/*
|
|
|
|
check for automatic takeoff conditions being met
|
|
|
|
*/
|
|
|
|
static bool auto_takeoff_check(void)
|
|
|
|
{
|
2013-06-26 05:36:53 -03:00
|
|
|
#if 1
|
2013-03-25 04:25:24 -03:00
|
|
|
if (g_gps == NULL || g_gps->status() != GPS::GPS_OK_FIX_3D) {
|
2013-03-08 23:41:04 -04:00
|
|
|
// no auto takeoff without GPS lock
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-10 01:03:03 -03:00
|
|
|
if (g_gps->ground_speed_cm < g.takeoff_throttle_min_speed*100.0f) {
|
2013-03-08 23:41:04 -04:00
|
|
|
// we haven't reached the minimum ground speed
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-21 21:53:45 -03:00
|
|
|
|
|
|
|
if (g.takeoff_throttle_min_accel > 0.0f) {
|
|
|
|
float xaccel = ins.get_accel().x;
|
|
|
|
if (ahrs.pitch_sensor > -3000 &&
|
|
|
|
ahrs.pitch_sensor < 4500 &&
|
|
|
|
abs(ahrs.roll_sensor) < 3000 &&
|
|
|
|
xaccel >= g.takeoff_throttle_min_accel) {
|
|
|
|
// trigger with minimum acceleration when flat
|
|
|
|
// Thanks to Chris Miser for this suggestion
|
|
|
|
gcs_send_text_fmt(PSTR("Triggered AUTO xaccel=%.1f"), xaccel);
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-08 23:41:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-21 21:53:45 -03:00
|
|
|
|
2013-03-08 23:41:04 -04:00
|
|
|
// we're good for takeoff
|
|
|
|
return true;
|
2013-06-26 05:36:53 -03:00
|
|
|
|
|
|
|
#else
|
|
|
|
// this is a more advanced check that relies on TECS
|
|
|
|
uint32_t now = hal.scheduler->micros();
|
2013-06-26 07:48:45 -03:00
|
|
|
static bool launchCountStarted;
|
|
|
|
static uint32_t last_tkoff_arm_time;
|
2013-06-26 05:36:53 -03:00
|
|
|
|
|
|
|
if (g_gps == NULL || g_gps->status() != GPS::GPS_OK_FIX_3D)
|
|
|
|
{
|
|
|
|
// no auto takeoff without GPS lock
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (SpdHgt_Controller->get_VXdot() >= g.takeoff_throttle_min_accel || g.takeoff_throttle_min_accel == 0.0 || launchCountStarted)
|
|
|
|
{
|
|
|
|
if (!launchCountStarted)
|
|
|
|
{
|
|
|
|
launchCountStarted = true;
|
|
|
|
last_tkoff_arm_time = now;
|
|
|
|
gcs_send_text_fmt(PSTR("Armed AUTO, xaccel = %.1f m/s/s, waiting %.1f sec"), SpdHgt_Controller->get_VXdot(), 0.1f*float(min(g.takeoff_throttle_delay,15)));
|
|
|
|
}
|
|
|
|
if ((now - last_tkoff_arm_time) <= 2500000)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((g_gps->ground_speed > g.takeoff_throttle_min_speed*100.0f || g.takeoff_throttle_min_speed == 0.0) && ((now -last_tkoff_arm_time) >= min(uint32_t(g.takeoff_throttle_delay*100000),1500000)))
|
|
|
|
{
|
|
|
|
gcs_send_text_fmt(PSTR("Triggered AUTO, GPSspd = %.1f"), g_gps->ground_speed*100.0f);
|
|
|
|
launchCountStarted = false;
|
|
|
|
last_tkoff_arm_time = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
launchCountStarted = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gcs_send_text_fmt(PSTR("Timeout AUTO"));
|
|
|
|
launchCountStarted = false;
|
|
|
|
last_tkoff_arm_time = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
launchCountStarted = false;
|
|
|
|
last_tkoff_arm_time = 0;
|
|
|
|
return false;
|
|
|
|
#endif
|
2013-03-08 23:41:04 -04:00
|
|
|
}
|
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-27 03:26:53 -03:00
|
|
|
/* We want to supress the throttle if we think we are on the ground and in an autopilot controlled throttle mode.
|
|
|
|
|
|
|
|
Disable throttle if following conditions are met:
|
|
|
|
* 1 - We are in Circle mode (which we use for short term failsafe), or in FBW-B or higher
|
|
|
|
* AND
|
|
|
|
* 2 - Our reported altitude is within 10 meters of the home altitude.
|
|
|
|
* 3 - Our reported speed is under 5 meters per second.
|
2013-03-08 23:41:04 -04:00
|
|
|
* 4 - We are not performing a takeoff in Auto mode or takeoff speed/accel not yet reached
|
2012-08-27 03:26:53 -03:00
|
|
|
* OR
|
|
|
|
* 5 - Home location is not set
|
|
|
|
*/
|
|
|
|
static bool suppress_throttle(void)
|
|
|
|
{
|
|
|
|
if (!throttle_suppressed) {
|
|
|
|
// we've previously met a condition for unsupressing the throttle
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-05 01:55:22 -03:00
|
|
|
if (!auto_throttle_mode) {
|
2012-08-27 03:26:53 -03:00
|
|
|
// the user controls the throttle
|
|
|
|
throttle_suppressed = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 23:41:04 -04:00
|
|
|
if (control_mode==AUTO && takeoff_complete == false && auto_takeoff_check()) {
|
2012-08-27 03:26:53 -03:00
|
|
|
// we're in auto takeoff
|
|
|
|
throttle_suppressed = false;
|
2013-07-01 04:05:13 -03:00
|
|
|
if (hold_course_cd != -1) {
|
|
|
|
// update takeoff course hold, if already initialised
|
|
|
|
hold_course_cd = ahrs.yaw_sensor;
|
|
|
|
gcs_send_text_fmt(PSTR("Holding course %ld"), hold_course_cd);
|
|
|
|
}
|
2012-08-27 03:26:53 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-10 00:40:13 -03:00
|
|
|
if (relative_altitude_abs_cm() >= 1000) {
|
2012-08-27 03:26:53 -03:00
|
|
|
// we're more than 10m from the home altitude
|
|
|
|
throttle_suppressed = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_gps != NULL &&
|
2013-03-25 04:25:24 -03:00
|
|
|
g_gps->status() >= GPS::GPS_OK_FIX_2D &&
|
2013-07-10 01:03:03 -03:00
|
|
|
g_gps->ground_speed_cm >= 500) {
|
2013-04-25 07:01:59 -03:00
|
|
|
// if we have an airspeed sensor, then check it too, and
|
|
|
|
// require 5m/s. This prevents throttle up due to spiky GPS
|
|
|
|
// groundspeed with bad GPS reception
|
|
|
|
if (!airspeed.use() || airspeed.get_airspeed() >= 5) {
|
|
|
|
// we're moving at more than 5 m/s
|
|
|
|
throttle_suppressed = false;
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-27 03:26:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// throttle remains suppressed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-05 01:22:00 -03:00
|
|
|
/*
|
2013-04-28 07:49:53 -03:00
|
|
|
implement a software VTail or elevon mixer. There are 4 different mixing modes
|
2013-04-05 01:22:00 -03:00
|
|
|
*/
|
2013-04-28 07:49:53 -03:00
|
|
|
static void channel_output_mixer(uint8_t mixing_type, int16_t &chan1_out, int16_t &chan2_out)
|
2013-04-05 01:22:00 -03:00
|
|
|
{
|
2013-04-28 07:49:53 -03:00
|
|
|
int16_t c1, c2;
|
2013-04-05 01:47:35 -03:00
|
|
|
int16_t v1, v2;
|
2013-04-05 01:22:00 -03:00
|
|
|
|
2013-04-05 01:47:35 -03:00
|
|
|
// first get desired elevator and rudder as -500..500 values
|
2013-04-28 07:49:53 -03:00
|
|
|
c1 = chan1_out - 1500;
|
|
|
|
c2 = chan2_out - 1500;
|
2013-04-05 01:22:00 -03:00
|
|
|
|
2013-05-25 05:27:57 -03:00
|
|
|
v1 = (c1 - c2) * g.mixing_gain;
|
|
|
|
v2 = (c1 + c2) * g.mixing_gain;
|
2013-04-05 01:22:00 -03:00
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
// now map to mixed output
|
|
|
|
switch (mixing_type) {
|
|
|
|
case MIXING_DISABLED:
|
2013-04-05 01:22:00 -03:00
|
|
|
return;
|
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
case MIXING_UPUP:
|
2013-04-05 01:22:00 -03:00
|
|
|
break;
|
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
case MIXING_UPDN:
|
2013-04-05 01:22:00 -03:00
|
|
|
v2 = -v2;
|
|
|
|
break;
|
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
case MIXING_DNUP:
|
2013-04-05 01:22:00 -03:00
|
|
|
v1 = -v1;
|
|
|
|
break;
|
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
case MIXING_DNDN:
|
2013-04-05 01:22:00 -03:00
|
|
|
v1 = -v1;
|
|
|
|
v2 = -v2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-25 05:27:57 -03:00
|
|
|
// scale for a 1500 center and 900..2100 range, symmetric
|
|
|
|
v1 = constrain_int16(v1, -600, 600);
|
|
|
|
v2 = constrain_int16(v2, -600, 600);
|
2013-04-05 01:22:00 -03:00
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
chan1_out = 1500 + v1;
|
|
|
|
chan2_out = 1500 + v2;
|
2013-04-05 01:22:00 -03:00
|
|
|
}
|
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
/*****************************************
|
|
|
|
* Set the flight control servos based on the current calculated values
|
|
|
|
*****************************************/
|
2011-09-09 11:18:38 -03:00
|
|
|
static void set_servos(void)
|
2011-09-08 22:29:39 -03:00
|
|
|
{
|
2013-06-03 02:32:08 -03:00
|
|
|
int16_t last_throttle = channel_throttle->radio_out;
|
2012-08-21 23:19:50 -03:00
|
|
|
|
2012-12-04 02:32:37 -04:00
|
|
|
if (control_mode == MANUAL) {
|
2012-08-21 23:19:50 -03:00
|
|
|
// do a direct pass through of radio values
|
2013-04-28 07:49:53 -03:00
|
|
|
if (g.mix_mode == 0 || g.elevon_output != MIXING_DISABLED) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->radio_out = channel_roll->radio_in;
|
|
|
|
channel_pitch->radio_out = channel_pitch->radio_in;
|
2012-08-21 23:19:50 -03:00
|
|
|
} else {
|
2013-06-03 03:12:41 -03:00
|
|
|
channel_roll->radio_out = channel_roll->read();
|
|
|
|
channel_pitch->radio_out = channel_pitch->read();
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->radio_out = channel_throttle->radio_in;
|
|
|
|
channel_rudder->radio_out = channel_rudder->radio_in;
|
2012-10-30 22:39:03 -03:00
|
|
|
|
2013-06-28 21:14:57 -03:00
|
|
|
// setup extra channels. We want this to come from the
|
|
|
|
// main input channel, but using the 2nd channels dead
|
2012-11-05 08:37:58 -04:00
|
|
|
// zone, reverse and min/max settings. We need to use
|
|
|
|
// pwm_to_angle_dz() to ensure we don't trim the value for the
|
|
|
|
// deadzone of the main aileron channel, otherwise the 2nd
|
|
|
|
// aileron won't quite follow the first one
|
2013-06-28 21:14:57 -03:00
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_aileron, channel_roll->pwm_to_angle_dz(0));
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_elevator, channel_pitch->pwm_to_angle_dz(0));
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_rudder, channel_rudder->pwm_to_angle_dz(0));
|
2012-11-05 08:37:58 -04:00
|
|
|
|
2013-06-28 21:14:57 -03:00
|
|
|
// this variant assumes you have the corresponding
|
2012-11-20 20:48:46 -04:00
|
|
|
// input channel setup in your transmitter for manual control
|
|
|
|
// of the 2nd aileron
|
|
|
|
RC_Channel_aux::copy_radio_in_out(RC_Channel_aux::k_aileron_with_input);
|
2013-02-04 17:57:58 -04:00
|
|
|
RC_Channel_aux::copy_radio_in_out(RC_Channel_aux::k_elevator_with_input);
|
2012-09-08 02:13:22 -03:00
|
|
|
RC_Channel_aux::copy_radio_in_out(RC_Channel_aux::k_flap_auto);
|
2012-10-30 22:39:45 -03:00
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
if (g.mix_mode == 0 && g.elevon_output == MIXING_DISABLED) {
|
2012-10-30 22:39:45 -03:00
|
|
|
// set any differential spoilers to follow the elevons in
|
|
|
|
// manual mode.
|
2013-06-03 02:32:08 -03:00
|
|
|
RC_Channel_aux::set_radio(RC_Channel_aux::k_dspoiler1, channel_roll->radio_out);
|
|
|
|
RC_Channel_aux::set_radio(RC_Channel_aux::k_dspoiler2, channel_pitch->radio_out);
|
2012-10-30 22:39:45 -03:00
|
|
|
}
|
2012-08-21 23:19:50 -03:00
|
|
|
} else {
|
|
|
|
if (g.mix_mode == 0) {
|
2012-11-20 20:48:46 -04:00
|
|
|
// both types of secondary aileron are slaved to the roll servo out
|
2013-06-03 02:32:08 -03:00
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_aileron, channel_roll->servo_out);
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_aileron_with_input, channel_roll->servo_out);
|
2013-02-04 17:57:58 -04:00
|
|
|
|
|
|
|
// both types of secondary elevator are slaved to the pitch servo out
|
2013-06-03 02:32:08 -03:00
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_elevator, channel_pitch->servo_out);
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_elevator_with_input, channel_pitch->servo_out);
|
2013-06-28 21:14:57 -03:00
|
|
|
|
|
|
|
// setup secondary rudder
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_rudder, channel_rudder->servo_out);
|
2012-08-21 23:19:50 -03:00
|
|
|
}else{
|
|
|
|
/*Elevon mode*/
|
|
|
|
float ch1;
|
|
|
|
float ch2;
|
2013-06-03 02:32:08 -03:00
|
|
|
ch1 = channel_pitch->servo_out - (BOOL_TO_SIGN(g.reverse_elevons) * channel_roll->servo_out);
|
|
|
|
ch2 = channel_pitch->servo_out + (BOOL_TO_SIGN(g.reverse_elevons) * channel_roll->servo_out);
|
2012-10-30 22:39:45 -03:00
|
|
|
|
|
|
|
/* Differential Spoilers
|
|
|
|
If differential spoilers are setup, then we translate
|
|
|
|
rudder control into splitting of the two ailerons on
|
|
|
|
the side of the aircraft where we want to induce
|
|
|
|
additional drag.
|
|
|
|
*/
|
|
|
|
if (RC_Channel_aux::function_assigned(RC_Channel_aux::k_dspoiler1) && RC_Channel_aux::function_assigned(RC_Channel_aux::k_dspoiler2)) {
|
2012-10-02 23:40:45 -03:00
|
|
|
float ch3 = ch1;
|
|
|
|
float ch4 = ch2;
|
2013-06-03 02:32:08 -03:00
|
|
|
if ( BOOL_TO_SIGN(g.reverse_elevons) * channel_rudder->servo_out < 0) {
|
|
|
|
ch1 += abs(channel_rudder->servo_out);
|
|
|
|
ch3 -= abs(channel_rudder->servo_out);
|
2012-10-02 23:40:45 -03:00
|
|
|
} else {
|
2013-06-03 02:32:08 -03:00
|
|
|
ch2 += abs(channel_rudder->servo_out);
|
|
|
|
ch4 -= abs(channel_rudder->servo_out);
|
2012-10-02 23:40:45 -03:00
|
|
|
}
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_dspoiler1, ch3);
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_dspoiler2, ch4);
|
2012-10-30 22:39:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// directly set the radio_out values for elevon mode
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->radio_out = elevon.trim1 + (BOOL_TO_SIGN(g.reverse_ch1_elevon) * (ch1 * 500.0/ SERVO_MAX));
|
|
|
|
channel_pitch->radio_out = elevon.trim2 + (BOOL_TO_SIGN(g.reverse_ch2_elevon) * (ch2 * 500.0/ SERVO_MAX));
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
2012-08-28 02:51:32 -03:00
|
|
|
#if OBC_FAILSAFE == ENABLED
|
|
|
|
// this is to allow the failsafe module to deliberately crash
|
|
|
|
// the plane. Only used in extreme circumstances to meet the
|
|
|
|
// OBC rules
|
|
|
|
if (obc.crash_plane()) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->servo_out = -4500;
|
|
|
|
channel_pitch->servo_out = -4500;
|
|
|
|
channel_rudder->servo_out = -4500;
|
|
|
|
channel_throttle->servo_out = 0;
|
2012-08-28 02:51:32 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// push out the PWM values
|
2012-09-12 23:01:40 -03:00
|
|
|
if (g.mix_mode == 0) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_roll->calc_pwm();
|
|
|
|
channel_pitch->calc_pwm();
|
2012-09-12 23:01:40 -03:00
|
|
|
}
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->calc_pwm();
|
2012-09-22 03:17:38 -03:00
|
|
|
|
|
|
|
#if THROTTLE_OUT == 0
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out = 0;
|
2012-09-22 03:17:38 -03:00
|
|
|
#else
|
|
|
|
// convert 0 to 100% into PWM
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out = constrain_int16(channel_throttle->servo_out,
|
2013-06-26 07:48:45 -03:00
|
|
|
aparm.throttle_min.get(),
|
|
|
|
aparm.throttle_max.get());
|
2012-09-22 03:17:38 -03:00
|
|
|
|
|
|
|
if (suppress_throttle()) {
|
2012-11-26 08:29:00 -04:00
|
|
|
// throttle is suppressed in auto mode
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->servo_out = 0;
|
2012-09-22 03:17:38 -03:00
|
|
|
if (g.throttle_suppress_manual) {
|
|
|
|
// manual pass through of throttle while throttle is suppressed
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->radio_out = channel_throttle->radio_in;
|
2012-09-22 03:17:38 -03:00
|
|
|
} else {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->calc_pwm();
|
2012-09-22 03:17:38 -03:00
|
|
|
}
|
2012-11-26 08:29:00 -04:00
|
|
|
} else if (g.throttle_passthru_stabilize &&
|
2012-12-04 02:32:37 -04:00
|
|
|
(control_mode == STABILIZE ||
|
|
|
|
control_mode == TRAINING ||
|
2013-07-10 10:25:38 -03:00
|
|
|
control_mode == ACRO ||
|
2012-12-04 02:32:37 -04:00
|
|
|
control_mode == FLY_BY_WIRE_A)) {
|
2012-11-26 08:29:00 -04:00
|
|
|
// manual pass through of throttle while in FBWA or
|
|
|
|
// STABILIZE mode with THR_PASS_STAB set
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->radio_out = channel_throttle->radio_in;
|
2012-09-22 03:17:38 -03:00
|
|
|
} else {
|
2012-11-26 08:29:00 -04:00
|
|
|
// normal throttle calculation based on servo_out
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_throttle->calc_pwm();
|
2012-09-22 03:17:38 -03:00
|
|
|
}
|
|
|
|
#endif
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auto flap deployment
|
2012-09-08 02:13:22 -03:00
|
|
|
if(control_mode < FLY_BY_WIRE_B) {
|
|
|
|
RC_Channel_aux::copy_radio_in_out(RC_Channel_aux::k_flap_auto);
|
|
|
|
} else if (control_mode >= FLY_BY_WIRE_B) {
|
2012-12-04 02:32:37 -04:00
|
|
|
int16_t flapSpeedSource = 0;
|
|
|
|
|
2012-09-08 02:13:22 -03:00
|
|
|
// FIXME: use target_airspeed in both FBW_B and g.airspeed_enabled cases - Doug?
|
|
|
|
if (control_mode == FLY_BY_WIRE_B) {
|
|
|
|
flapSpeedSource = target_airspeed_cm * 0.01;
|
|
|
|
} else if (airspeed.use()) {
|
|
|
|
flapSpeedSource = g.airspeed_cruise_cm * 0.01;
|
|
|
|
} else {
|
2013-06-26 07:48:45 -03:00
|
|
|
flapSpeedSource = aparm.throttle_cruise;
|
2012-09-08 02:13:22 -03:00
|
|
|
}
|
|
|
|
if ( g.flap_1_speed != 0 && flapSpeedSource > g.flap_1_speed) {
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_flap_auto, 0);
|
|
|
|
} else if (g.flap_2_speed != 0 && flapSpeedSource > g.flap_2_speed) {
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_flap_auto, g.flap_1_percent);
|
|
|
|
} else {
|
|
|
|
RC_Channel_aux::set_servo_out(RC_Channel_aux::k_flap_auto, g.flap_2_percent);
|
2012-08-21 23:19:50 -03:00
|
|
|
}
|
|
|
|
}
|
2011-10-09 10:25:20 -03:00
|
|
|
|
2012-11-27 21:13:09 -04:00
|
|
|
if (control_mode >= FLY_BY_WIRE_B) {
|
|
|
|
/* only do throttle slew limiting in modes where throttle
|
|
|
|
* control is automatic */
|
|
|
|
throttle_slew_limit(last_throttle);
|
|
|
|
}
|
|
|
|
|
2013-04-05 05:38:43 -03:00
|
|
|
if (control_mode == TRAINING) {
|
|
|
|
// copy rudder in training mode
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_rudder->radio_out = channel_rudder->radio_in;
|
2013-04-05 05:38:43 -03:00
|
|
|
}
|
|
|
|
|
2013-03-30 00:38:43 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_DISABLED
|
|
|
|
if (!g.hil_servos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-28 07:49:53 -03:00
|
|
|
if (g.vtail_output != MIXING_DISABLED) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_output_mixer(g.vtail_output, channel_pitch->radio_out, channel_rudder->radio_out);
|
2013-04-28 07:49:53 -03:00
|
|
|
} else if (g.elevon_output != MIXING_DISABLED) {
|
2013-06-03 02:32:08 -03:00
|
|
|
channel_output_mixer(g.elevon_output, channel_pitch->radio_out, channel_roll->radio_out);
|
2013-04-05 01:22:00 -03:00
|
|
|
}
|
|
|
|
|
2012-08-21 23:19:50 -03:00
|
|
|
// send values to the PWM timers for output
|
|
|
|
// ----------------------------------------
|
2013-06-03 03:12:41 -03:00
|
|
|
channel_roll->output();
|
|
|
|
channel_pitch->output();
|
|
|
|
channel_throttle->output();
|
|
|
|
channel_rudder->output();
|
2012-08-21 23:19:50 -03:00
|
|
|
// Route configurable aux. functions to their respective servos
|
|
|
|
g.rc_5.output_ch(CH_5);
|
|
|
|
g.rc_6.output_ch(CH_6);
|
|
|
|
g.rc_7.output_ch(CH_7);
|
|
|
|
g.rc_8.output_ch(CH_8);
|
2013-06-23 20:04:36 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
2012-08-21 23:19:50 -03:00
|
|
|
g.rc_9.output_ch(CH_9);
|
2013-06-23 20:04:36 -03:00
|
|
|
#endif
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2 || CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
2012-08-21 23:19:50 -03:00
|
|
|
g.rc_10.output_ch(CH_10);
|
|
|
|
g.rc_11.output_ch(CH_11);
|
2013-06-23 20:04:36 -03:00
|
|
|
#endif
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
2013-04-25 07:10:40 -03:00
|
|
|
g.rc_12.output_ch(CH_12);
|
|
|
|
# endif
|
2011-10-09 10:25:20 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-11-20 22:19:32 -04:00
|
|
|
static bool demoing_servos;
|
|
|
|
|
2013-07-13 07:05:53 -03:00
|
|
|
static void demo_servos(uint8_t i)
|
|
|
|
{
|
2012-08-21 23:19:50 -03:00
|
|
|
while(i > 0) {
|
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("Demo Servos!"));
|
2012-11-20 22:19:32 -04:00
|
|
|
demoing_servos = true;
|
2013-03-30 00:38:43 -03:00
|
|
|
servo_write(1, 1400);
|
2012-08-21 23:19:50 -03:00
|
|
|
mavlink_delay(400);
|
2013-03-30 00:38:43 -03:00
|
|
|
servo_write(1, 1600);
|
2012-08-21 23:19:50 -03:00
|
|
|
mavlink_delay(200);
|
2013-03-30 00:38:43 -03:00
|
|
|
servo_write(1, 1500);
|
2012-11-20 22:19:32 -04:00
|
|
|
demoing_servos = false;
|
2012-08-21 23:19:50 -03:00
|
|
|
mavlink_delay(400);
|
|
|
|
i--;
|
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
2012-08-28 00:15:04 -03:00
|
|
|
|
|
|
|
// return true if we should use airspeed for altitude/throttle control
|
|
|
|
static bool alt_control_airspeed(void)
|
|
|
|
{
|
2013-07-14 22:12:24 -03:00
|
|
|
return airspeed.use() && g.alt_control_algorithm == ALT_CONTROL_AIRSPEED;
|
2012-08-28 00:15:04 -03:00
|
|
|
}
|