2011-03-19 07:20:11 -03:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2011-12-03 21:54:38 -04:00
2013-08-11 00:51:08 -03:00
// get_pilot_desired_angle - transform pilot's roll or pitch input into a desired lean angle
// returns desired angle in centi-degrees
static void get_pilot_desired_lean_angles(int16_t roll_in, int16_t pitch_in, int16_t &roll_out, int16_t &pitch_out)
{
static float _scaler = 1.0;
static int16_t _angle_max = 0;
2013-11-13 01:21:21 -04:00
// range check the input
roll_in = constrain_int16(roll_in, -ROLL_PITCH_INPUT_MAX, ROLL_PITCH_INPUT_MAX);
pitch_in = constrain_int16(pitch_in, -ROLL_PITCH_INPUT_MAX, ROLL_PITCH_INPUT_MAX);
2013-08-11 00:51:08 -03:00
// return immediately if no scaling required
if (g.angle_max == ROLL_PITCH_INPUT_MAX) {
roll_out = roll_in;
pitch_out = pitch_in;
return;
}
// check if angle_max has been updated and redo scaler
if (g.angle_max != _angle_max) {
_angle_max = g.angle_max;
_scaler = (float)g.angle_max/(float)ROLL_PITCH_INPUT_MAX;
}
// convert pilot input to lean angle
roll_out = roll_in * _scaler;
pitch_out = pitch_in * _scaler;
}
2012-10-01 02:02:49 -03:00
static void
2011-11-07 18:24:32 -04:00
get_stabilize_roll(int32_t target_angle)
2010-12-19 12:40:33 -04:00
{
2012-08-21 23:19:50 -03:00
// angle error
2013-07-22 22:37:10 -03:00
target_angle = wrap_180_cd(target_angle - ahrs.roll_sensor);
2011-01-25 01:53:36 -04:00
2013-04-22 06:47:56 -03:00
// convert to desired rate
2013-07-22 22:37:10 -03:00
int32_t target_rate = g.pi_stabilize_roll.kP() * target_angle;
2012-06-05 20:41:31 -03:00
2013-10-12 23:56:01 -03:00
// constrain the target rate
if (!ap.disable_stab_rate_limit) {
2013-10-27 10:34:42 -03:00
target_rate = constrain_int32(target_rate, -g.angle_rate_max, g.angle_rate_max);
2013-10-12 23:56:01 -03:00
}
2012-10-01 02:02:49 -03:00
// set targets for rate controller
2013-04-22 06:47:56 -03:00
set_roll_rate_target(target_rate, EARTH_FRAME);
2011-04-25 02:12:59 -03:00
}
2012-10-01 02:02:49 -03:00
static void
2011-11-07 18:24:32 -04:00
get_stabilize_pitch(int32_t target_angle)
2011-01-25 01:53:36 -04:00
{
2012-08-21 23:19:50 -03:00
// angle error
2013-03-28 23:14:31 -03:00
target_angle = wrap_180_cd(target_angle - ahrs.pitch_sensor);
2011-02-17 05:36:33 -04:00
2013-04-22 06:47:56 -03:00
// convert to desired rate
2013-07-22 22:37:10 -03:00
int32_t target_rate = g.pi_stabilize_pitch.kP() * target_angle;
2012-10-01 02:02:49 -03:00
2013-10-12 23:56:01 -03:00
// constrain the target rate
if (!ap.disable_stab_rate_limit) {
2013-10-27 10:34:42 -03:00
target_rate = constrain_int32(target_rate, -g.angle_rate_max, g.angle_rate_max);
2013-10-12 23:56:01 -03:00
}
2012-10-01 02:02:49 -03:00
// set targets for rate controller
2013-04-22 06:47:56 -03:00
set_pitch_rate_target(target_rate, EARTH_FRAME);
2012-01-29 02:00:05 -04:00
}
2010-12-19 12:40:33 -04:00
2012-10-01 02:02:49 -03:00
static void
2012-01-29 02:00:05 -04:00
get_stabilize_yaw(int32_t target_angle)
{
2013-07-22 22:37:10 -03:00
int32_t target_rate;
2012-08-21 23:19:50 -03:00
int32_t angle_error;
2012-10-01 02:02:49 -03:00
int32_t output = 0;
2012-03-25 03:55:49 -03:00
2012-08-21 23:19:50 -03:00
// angle error
2013-07-22 22:37:10 -03:00
angle_error = wrap_180_cd(target_angle - ahrs.yaw_sensor);
2011-07-10 21:47:08 -03:00
2012-08-21 23:19:50 -03:00
// limit the error we're feeding to the PID
2013-07-22 22:37:10 -03:00
angle_error = constrain_int32(angle_error, -4500, 4500);
2011-12-29 14:23:18 -04:00
2012-08-21 23:19:50 -03:00
// convert angle error to desired Rate:
2013-07-22 22:37:10 -03:00
target_rate = g.pi_stabilize_yaw.kP() * angle_error;
2011-12-29 14:23:18 -04:00
2012-08-21 23:19:50 -03:00
// do not use rate controllers for helicotpers with external gyros
2012-03-25 03:55:49 -03:00
#if FRAME_CONFIG == HELI_FRAME
2013-07-22 16:23:16 -03:00
if(motors.tail_type() == AP_MOTORS_HELI_TAILTYPE_SERVO_EXTGYRO) {
2013-07-22 22:37:10 -03:00
g.rc_4.servo_out = constrain_int32(target_rate, -4500, 4500);
2012-08-21 23:19:50 -03:00
}
2011-12-08 08:30:47 -04:00
#endif
2012-03-25 03:55:49 -03:00
2012-03-27 01:33:21 -03:00
#if LOGGING_ENABLED == ENABLED
2012-08-21 23:19:50 -03:00
// log output if PID logging is on and we are tuning the yaw
2013-06-30 06:51:43 -03:00
if( g.log_bitmask & MASK_LOG_PID && g.radio_tuning == CH6_STABILIZE_YAW_KP ) {
2013-01-08 01:45:12 -04:00
pid_log_counter++;
if( pid_log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
pid_log_counter = 0;
2013-07-22 22:37:10 -03:00
Log_Write_PID(CH6_STABILIZE_YAW_KP, angle_error, target_rate, 0, 0, output, tuning_value);
2012-08-21 23:19:50 -03:00
}
}
2012-03-27 01:33:21 -03:00
#endif
2012-03-25 03:55:49 -03:00
2012-10-01 02:02:49 -03:00
// set targets for rate controller
2013-07-22 22:37:10 -03:00
set_yaw_rate_target(target_rate, EARTH_FRAME);
2012-10-14 05:47:46 -03:00
}
2013-08-04 05:46:04 -03:00
// get_acro_level_rates - calculate earth frame rate corrections to pull the copter back to level while in ACRO mode
2013-07-06 09:59:57 -03:00
static void
2013-07-31 05:53:14 -03:00
get_acro_level_rates()
2013-07-06 09:59:57 -03:00
{
2013-08-04 06:14:07 -03:00
// zero earth frame leveling if trainer is disabled
if (g.acro_trainer == ACRO_TRAINER_DISABLED) {
set_roll_rate_target(0, BODY_EARTH_FRAME);
set_pitch_rate_target(0, BODY_EARTH_FRAME);
set_yaw_rate_target(0, BODY_EARTH_FRAME);
return;
}
2013-07-31 05:53:14 -03:00
// Calculate trainer mode earth frame rate command for roll
2013-07-06 09:59:57 -03:00
int32_t roll_angle = wrap_180_cd(ahrs.roll_sensor);
int32_t target_rate = 0;
2013-08-04 06:14:07 -03:00
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
2013-08-11 00:51:08 -03:00
if (roll_angle > g.angle_max){
target_rate = g.pi_stabilize_roll.get_p(g.angle_max-roll_angle);
}else if (roll_angle < -g.angle_max) {
target_rate = g.pi_stabilize_roll.get_p(-g.angle_max-roll_angle);
2013-07-06 09:59:57 -03:00
}
}
2013-07-31 05:53:14 -03:00
roll_angle = constrain_int32(roll_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE);
2013-08-04 06:14:07 -03:00
target_rate -= roll_angle * g.acro_balance_roll;
2013-07-06 09:59:57 -03:00
2013-07-31 05:53:14 -03:00
// add earth frame targets for roll rate controller
2013-07-06 09:59:57 -03:00
set_roll_rate_target(target_rate, BODY_EARTH_FRAME);
2013-07-31 05:53:14 -03:00
// Calculate trainer mode earth frame rate command for pitch
int32_t pitch_angle = wrap_180_cd(ahrs.pitch_sensor);
target_rate = 0;
2013-08-04 06:14:07 -03:00
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
2013-08-11 00:51:08 -03:00
if (pitch_angle > g.angle_max){
target_rate = g.pi_stabilize_pitch.get_p(g.angle_max-pitch_angle);
}else if (pitch_angle < -g.angle_max) {
target_rate = g.pi_stabilize_pitch.get_p(-g.angle_max-pitch_angle);
2013-07-31 05:53:14 -03:00
}
}
2013-08-04 05:46:04 -03:00
pitch_angle = constrain_int32(pitch_angle, -ACRO_LEVEL_MAX_ANGLE, ACRO_LEVEL_MAX_ANGLE);
2013-08-04 06:14:07 -03:00
target_rate -= pitch_angle * g.acro_balance_pitch;
2013-07-31 05:53:14 -03:00
// add earth frame targets for pitch rate controller
set_pitch_rate_target(target_rate, BODY_EARTH_FRAME);
// add earth frame targets for yaw rate controller
set_yaw_rate_target(0, BODY_EARTH_FRAME);
2013-07-06 09:59:57 -03:00
}
2013-07-31 05:53:14 -03:00
// Roll with rate input and stabilized in the body frame
2013-07-06 09:59:57 -03:00
static void
2013-07-31 05:53:14 -03:00
get_roll_rate_stabilized_bf(int32_t stick_angle)
2013-07-06 09:59:57 -03:00
{
static float angle_error = 0;
2013-07-31 05:53:14 -03:00
// convert the input to the desired body frame roll rate
2013-08-04 08:22:12 -03:00
int32_t rate_request = stick_angle * g.acro_rp_p;
2013-07-06 09:59:57 -03:00
2013-08-04 06:14:07 -03:00
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
rate_request += acro_roll_rate;
}else{
2013-07-31 05:53:14 -03:00
// Scale pitch leveling by stick input
2013-08-04 05:46:04 -03:00
acro_roll_rate = (float)acro_roll_rate*acro_level_mix;
2013-07-31 05:53:14 -03:00
// Calculate rate limit to prevent change of rate through inverted
2013-08-04 05:46:04 -03:00
int32_t rate_limit = labs(labs(rate_request)-labs(acro_roll_rate));
rate_request += acro_roll_rate;
2013-07-31 05:53:14 -03:00
rate_request = constrain_int32(rate_request, -rate_limit, rate_limit);
2013-07-06 09:59:57 -03:00
}
// add automatic correction
2013-07-31 05:53:14 -03:00
int32_t rate_correction = g.pi_stabilize_roll.get_p(angle_error);
// set body frame targets for rate controller
set_roll_rate_target(rate_request+rate_correction, BODY_FRAME);
2013-07-06 09:59:57 -03:00
// Calculate integrated body frame rate error
2013-07-31 05:53:14 -03:00
angle_error += (rate_request - (omega.x * DEGX100)) * G_Dt;
2013-07-06 09:59:57 -03:00
// don't let angle error grow too large
2013-08-04 05:46:04 -03:00
angle_error = constrain_float(angle_error, -MAX_ROLL_OVERSHOOT, MAX_ROLL_OVERSHOOT);
2013-07-06 09:59:57 -03:00
2013-07-11 10:33:55 -03:00
if (!motors.armed() || g.rc_3.servo_out == 0) {
angle_error = 0;
}
2013-07-31 05:53:14 -03:00
}
// Pitch with rate input and stabilized in the body frame
static void
get_pitch_rate_stabilized_bf(int32_t stick_angle)
{
static float angle_error = 0;
// convert the input to the desired body frame pitch rate
2013-08-04 08:22:12 -03:00
int32_t rate_request = stick_angle * g.acro_rp_p;
2013-07-31 05:53:14 -03:00
2013-08-04 06:14:07 -03:00
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
rate_request += acro_pitch_rate;
}else{
2013-07-31 05:53:14 -03:00
// Scale pitch leveling by stick input
2013-08-04 05:46:04 -03:00
acro_pitch_rate = (float)acro_pitch_rate*acro_level_mix;
2013-07-31 05:53:14 -03:00
// Calculate rate limit to prevent change of rate through inverted
2013-08-04 05:46:04 -03:00
int32_t rate_limit = labs(labs(rate_request)-labs(acro_pitch_rate));
2013-07-31 05:53:14 -03:00
2013-08-04 05:46:04 -03:00
rate_request += acro_pitch_rate;
2013-07-31 05:53:14 -03:00
rate_request = constrain_int32(rate_request, -rate_limit, rate_limit);
}
// add automatic correction
int32_t rate_correction = g.pi_stabilize_pitch.get_p(angle_error);
2013-07-06 09:59:57 -03:00
// set body frame targets for rate controller
2013-07-31 05:53:14 -03:00
set_pitch_rate_target(rate_request+rate_correction, BODY_FRAME);
2013-07-06 09:59:57 -03:00
2013-07-31 05:53:14 -03:00
// Calculate integrated body frame rate error
angle_error += (rate_request - (omega.y * DEGX100)) * G_Dt;
2013-07-06 09:59:57 -03:00
2013-07-31 05:53:14 -03:00
// don't let angle error grow too large
2013-08-04 05:46:04 -03:00
angle_error = constrain_float(angle_error, -MAX_PITCH_OVERSHOOT, MAX_PITCH_OVERSHOOT);
2013-07-06 09:59:57 -03:00
2013-07-31 05:53:14 -03:00
if (!motors.armed() || g.rc_3.servo_out == 0) {
angle_error = 0;
2013-07-06 09:59:57 -03:00
}
}
// Yaw with rate input and stabilized in the body frame
static void
get_yaw_rate_stabilized_bf(int32_t stick_angle)
{
static float angle_error = 0;
2013-07-31 05:53:14 -03:00
// convert the input to the desired body frame yaw rate
2013-08-04 08:22:12 -03:00
int32_t rate_request = stick_angle * g.acro_yaw_p;
2013-07-06 09:59:57 -03:00
2013-08-04 06:14:07 -03:00
if (g.acro_trainer == ACRO_TRAINER_LIMITED) {
rate_request += acro_yaw_rate;
}else{
2013-07-31 05:53:14 -03:00
// Scale pitch leveling by stick input
2013-08-04 05:46:04 -03:00
acro_yaw_rate = (float)acro_yaw_rate*acro_level_mix;
2013-07-31 05:53:14 -03:00
// Calculate rate limit to prevent change of rate through inverted
2013-08-04 05:46:04 -03:00
int32_t rate_limit = labs(labs(rate_request)-labs(acro_yaw_rate));
rate_request += acro_yaw_rate;
2013-07-31 05:53:14 -03:00
rate_request = constrain_int32(rate_request, -rate_limit, rate_limit);
2013-07-06 09:59:57 -03:00
}
// add automatic correction
2013-07-31 05:53:14 -03:00
int32_t rate_correction = g.pi_stabilize_yaw.get_p(angle_error);
// set body frame targets for rate controller
set_yaw_rate_target(rate_request+rate_correction, BODY_FRAME);
2013-07-06 09:59:57 -03:00
// Calculate integrated body frame rate error
2013-07-31 05:53:14 -03:00
angle_error += (rate_request - (omega.z * DEGX100)) * G_Dt;
2013-07-06 09:59:57 -03:00
// don't let angle error grow too large
2013-08-04 05:46:04 -03:00
angle_error = constrain_float(angle_error, -MAX_YAW_OVERSHOOT, MAX_YAW_OVERSHOOT);
2013-07-06 09:59:57 -03:00
2013-07-11 10:33:55 -03:00
if (!motors.armed() || g.rc_3.servo_out == 0) {
angle_error = 0;
}
2013-07-06 09:59:57 -03:00
}
2013-08-04 03:56:35 -03:00
// Roll with rate input and stabilized in the earth frame
static void
get_roll_rate_stabilized_ef(int32_t stick_angle)
{
int32_t angle_error = 0;
// convert the input to the desired roll rate
2013-08-04 08:22:12 -03:00
int32_t target_rate = stick_angle * g.acro_rp_p - (acro_roll * g.acro_balance_roll);
2013-08-04 03:56:35 -03:00
// convert the input to the desired roll rate
2013-08-04 05:46:04 -03:00
acro_roll += target_rate * G_Dt;
acro_roll = wrap_180_cd(acro_roll);
2013-08-04 03:56:35 -03:00
// ensure that we don't reach gimbal lock
2013-08-11 10:15:27 -03:00
if (labs(acro_roll) > g.angle_max) {
2013-08-11 02:54:50 -03:00
acro_roll = constrain_int32(acro_roll, -g.angle_max, g.angle_max);
2013-08-04 05:46:04 -03:00
angle_error = wrap_180_cd(acro_roll - ahrs.roll_sensor);
2013-08-04 03:56:35 -03:00
} else {
// angle error with maximum of +- max_angle_overshoot
2013-08-04 05:46:04 -03:00
angle_error = wrap_180_cd(acro_roll - ahrs.roll_sensor);
2013-08-04 03:56:35 -03:00
angle_error = constrain_int32(angle_error, -MAX_ROLL_OVERSHOOT, MAX_ROLL_OVERSHOOT);
}
#if FRAME_CONFIG == HELI_FRAME
2013-11-04 07:54:47 -04:00
if (!motors.motor_runup_complete()) {
2013-08-04 03:56:35 -03:00
angle_error = 0;
}
#else
// reset target angle to current angle if motors not spinning
if (!motors.armed() || g.rc_3.servo_out == 0) {
angle_error = 0;
}
#endif // HELI_FRAME
2013-08-04 05:46:04 -03:00
// update acro_roll to be within max_angle_overshoot of our current heading
acro_roll = wrap_180_cd(angle_error + ahrs.roll_sensor);
2013-08-04 03:56:35 -03:00
// set earth frame targets for rate controller
set_roll_rate_target(g.pi_stabilize_roll.get_p(angle_error) + target_rate, EARTH_FRAME);
}
// Pitch with rate input and stabilized in the earth frame
static void
get_pitch_rate_stabilized_ef(int32_t stick_angle)
{
int32_t angle_error = 0;
// convert the input to the desired pitch rate
2013-08-04 08:22:12 -03:00
int32_t target_rate = stick_angle * g.acro_rp_p - (acro_pitch * g.acro_balance_pitch);
2013-08-04 03:56:35 -03:00
// convert the input to the desired pitch rate
2013-08-04 05:46:04 -03:00
acro_pitch += target_rate * G_Dt;
acro_pitch = wrap_180_cd(acro_pitch);
2013-08-04 03:56:35 -03:00
// ensure that we don't reach gimbal lock
2013-08-11 02:54:50 -03:00
if (labs(acro_pitch) > g.angle_max) {
acro_pitch = constrain_int32(acro_pitch, -g.angle_max, g.angle_max);
2013-08-04 05:46:04 -03:00
angle_error = wrap_180_cd(acro_pitch - ahrs.pitch_sensor);
2013-08-04 03:56:35 -03:00
} else {
// angle error with maximum of +- max_angle_overshoot
2013-08-04 05:46:04 -03:00
angle_error = wrap_180_cd(acro_pitch - ahrs.pitch_sensor);
2013-08-04 03:56:35 -03:00
angle_error = constrain_int32(angle_error, -MAX_PITCH_OVERSHOOT, MAX_PITCH_OVERSHOOT);
}
#if FRAME_CONFIG == HELI_FRAME
2013-11-04 07:54:47 -04:00
if (!motors.motor_runup_complete()) {
2013-08-04 03:56:35 -03:00
angle_error = 0;
}
#else
// reset target angle to current angle if motors not spinning
if (!motors.armed() || g.rc_3.servo_out == 0) {
angle_error = 0;
}
#endif // HELI_FRAME
2013-08-04 05:46:04 -03:00
// update acro_pitch to be within max_angle_overshoot of our current heading
acro_pitch = wrap_180_cd(angle_error + ahrs.pitch_sensor);
2013-08-04 03:56:35 -03:00
// set earth frame targets for rate controller
2013-08-04 05:46:04 -03:00
set_pitch_rate_target(g.pi_stabilize_pitch.get_p(angle_error) + target_rate, EARTH_FRAME);
2013-08-04 03:56:35 -03:00
}
2012-10-14 09:30:16 -03:00
// Yaw with rate input and stabilized in the earth frame
static void
get_yaw_rate_stabilized_ef(int32_t stick_angle)
{
int32_t angle_error = 0;
// convert the input to the desired yaw rate
2013-08-04 08:22:12 -03:00
int32_t target_rate = stick_angle * g.acro_yaw_p;
2012-10-14 09:30:16 -03:00
2012-10-18 11:24:34 -03:00
// convert the input to the desired yaw rate
2012-10-14 09:30:16 -03:00
nav_yaw += target_rate * G_Dt;
2013-03-28 23:14:31 -03:00
nav_yaw = wrap_360_cd(nav_yaw);
2012-10-14 09:30:16 -03:00
2012-10-16 22:18:24 -03:00
// calculate difference between desired heading and current heading
2013-03-28 23:14:31 -03:00
angle_error = wrap_180_cd(nav_yaw - ahrs.yaw_sensor);
2012-10-14 09:30:16 -03:00
2012-10-16 22:18:24 -03:00
// limit the maximum overshoot
2013-04-21 09:52:30 -03:00
angle_error = constrain_int32(angle_error, -MAX_YAW_OVERSHOOT, MAX_YAW_OVERSHOOT);
2012-10-14 09:30:16 -03:00
2013-07-05 15:56:03 -03:00
#if FRAME_CONFIG == HELI_FRAME
2013-11-04 07:54:47 -04:00
if (!motors.motor_runup_complete()) {
2013-07-05 15:56:03 -03:00
angle_error = 0;
}
#else
2013-06-03 05:50:45 -03:00
// reset target angle to current heading if motors not spinning
if (!motors.armed() || g.rc_3.servo_out == 0) {
2012-10-14 09:30:16 -03:00
angle_error = 0;
}
2013-07-05 15:56:03 -03:00
#endif // HELI_FRAME
2012-10-14 09:30:16 -03:00
2012-10-18 11:24:34 -03:00
// update nav_yaw to be within max_angle_overshoot of our current heading
2013-03-28 23:14:31 -03:00
nav_yaw = wrap_360_cd(angle_error + ahrs.yaw_sensor);
2012-10-14 09:30:16 -03:00
// set earth frame targets for rate controller
set_yaw_rate_target(g.pi_stabilize_yaw.get_p(angle_error)+target_rate, EARTH_FRAME);
}
2012-10-14 05:47:46 -03:00
// set_roll_rate_target - to be called by upper controllers to set roll rate targets in the earth frame
void set_roll_rate_target( int32_t desired_rate, uint8_t earth_or_body_frame ) {
rate_targets_frame = earth_or_body_frame;
if( earth_or_body_frame == BODY_FRAME ) {
roll_rate_target_bf = desired_rate;
}else{
roll_rate_target_ef = desired_rate;
}
}
// set_pitch_rate_target - to be called by upper controllers to set pitch rate targets in the earth frame
void set_pitch_rate_target( int32_t desired_rate, uint8_t earth_or_body_frame ) {
rate_targets_frame = earth_or_body_frame;
if( earth_or_body_frame == BODY_FRAME ) {
pitch_rate_target_bf = desired_rate;
}else{
pitch_rate_target_ef = desired_rate;
}
}
// set_yaw_rate_target - to be called by upper controllers to set yaw rate targets in the earth frame
void set_yaw_rate_target( int32_t desired_rate, uint8_t earth_or_body_frame ) {
rate_targets_frame = earth_or_body_frame;
if( earth_or_body_frame == BODY_FRAME ) {
yaw_rate_target_bf = desired_rate;
}else{
yaw_rate_target_ef = desired_rate;
}
}
// update_rate_contoller_targets - converts earth frame rates to body frame rates for rate controllers
void
update_rate_contoller_targets()
{
if( rate_targets_frame == EARTH_FRAME ) {
// convert earth frame rates to body frame rates
2013-07-06 09:59:57 -03:00
roll_rate_target_bf = roll_rate_target_ef - sin_pitch * yaw_rate_target_ef;
pitch_rate_target_bf = cos_roll_x * pitch_rate_target_ef + sin_roll * cos_pitch_x * yaw_rate_target_ef;
yaw_rate_target_bf = cos_pitch_x * cos_roll_x * yaw_rate_target_ef - sin_roll * pitch_rate_target_ef;
}else if( rate_targets_frame == BODY_EARTH_FRAME ) {
// add converted earth frame rates to body frame rates
2013-08-04 05:46:04 -03:00
acro_roll_rate = roll_rate_target_ef - sin_pitch * yaw_rate_target_ef;
acro_pitch_rate = cos_roll_x * pitch_rate_target_ef + sin_roll * cos_pitch_x * yaw_rate_target_ef;
acro_yaw_rate = cos_pitch_x * cos_roll_x * yaw_rate_target_ef - sin_roll * pitch_rate_target_ef;
2012-10-14 05:47:46 -03:00
}
}
2012-10-14 09:30:16 -03:00
2012-10-01 02:02:49 -03:00
// run roll, pitch and yaw rate controllers and send output to motors
// targets for these controllers comes from stabilize controllers
void
run_rate_controllers()
{
2013-11-06 01:47:06 -04:00
#if FRAME_CONFIG == HELI_FRAME
// convert desired roll and pitch rate to roll and pitch swash angles
heli_integrated_swash_controller(roll_rate_target_bf, pitch_rate_target_bf);
// helicopters only use rate controllers for yaw and only when not using an external gyro
2013-07-22 16:23:16 -03:00
if(motors.tail_type() != AP_MOTORS_HELI_TAILTYPE_SERVO_EXTGYRO) {
2012-11-26 22:02:41 -04:00
g.rc_4.servo_out = get_heli_rate_yaw(yaw_rate_target_bf);
2013-11-06 01:47:06 -04:00
}else{
// do not use rate controllers for helicotpers with external gyros
g.rc_4.servo_out = constrain_int32(yaw_rate_target_bf, -4500, 4500);
2012-10-06 01:46:19 -03:00
}
#else
2012-10-01 02:02:49 -03:00
// call rate controllers
2012-10-14 05:47:46 -03:00
g.rc_1.servo_out = get_rate_roll(roll_rate_target_bf);
g.rc_2.servo_out = get_rate_pitch(pitch_rate_target_bf);
g.rc_4.servo_out = get_rate_yaw(yaw_rate_target_bf);
2012-10-06 01:46:19 -03:00
#endif
2012-11-23 02:57:49 -04:00
2012-12-03 10:05:14 -04:00
// run throttle controller if accel based throttle controller is enabled and active (active means it has been given a target)
2013-07-12 10:57:30 -03:00
if( throttle_accel_controller_active ) {
2012-11-24 00:41:17 -04:00
set_throttle_out(get_throttle_accel(throttle_accel_target_ef), true);
2012-11-23 02:57:49 -04:00
}
2012-10-01 02:02:49 -03:00
}
2012-11-26 22:02:41 -04:00
#if FRAME_CONFIG != HELI_FRAME
2012-07-14 16:26:13 -03:00
static int16_t
2012-08-21 23:19:50 -03:00
get_rate_roll(int32_t target_rate)
2012-02-11 02:22:19 -04:00
{
2012-10-28 16:12:04 -03:00
int32_t p,i,d; // used to capture pid values for logging
int32_t current_rate; // this iteration's rate
int32_t rate_error; // simply target_rate - current_rate
int32_t output; // output from pid controller
2012-07-14 16:26:13 -03:00
2012-08-21 23:19:50 -03:00
// get current rate
current_rate = (omega.x * DEGX100);
2012-07-14 16:26:13 -03:00
2012-08-21 23:19:50 -03:00
// call pid controller
2012-10-28 16:12:04 -03:00
rate_error = target_rate - current_rate;
p = g.pid_rate_roll.get_p(rate_error);
2013-07-10 03:12:04 -03:00
// get i term
i = g.pid_rate_roll.get_integrator();
// update i term as long as we haven't breached the limits or the I term will certainly reduce
2013-07-21 01:58:24 -03:00
if (!motors.limit.roll_pitch || ((i>0&&rate_error<0)||(i<0&&rate_error>0))) {
2013-07-10 03:12:04 -03:00
i = g.pid_rate_roll.get_i(rate_error, G_Dt);
2012-10-10 03:54:46 -03:00
}
2012-10-28 16:12:04 -03:00
d = g.pid_rate_roll.get_d(rate_error, G_Dt);
output = p + i + d;
2012-07-14 16:26:13 -03:00
2012-08-21 23:19:50 -03:00
// constrain output
2013-04-21 09:52:30 -03:00
output = constrain_int32(output, -5000, 5000);
2012-07-14 01:24:22 -03:00
#if LOGGING_ENABLED == ENABLED
2012-08-21 23:19:50 -03:00
// log output if PID logging is on and we are tuning the rate P, I or D gains
2013-06-30 06:51:43 -03:00
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_RATE_ROLL_PITCH_KP || g.radio_tuning == CH6_RATE_ROLL_PITCH_KI || g.radio_tuning == CH6_RATE_ROLL_PITCH_KD) ) {
2013-01-08 01:45:12 -04:00
pid_log_counter++; // Note: get_rate_pitch pid logging relies on this function to update pid_log_counter so if you change the line above you must change the equivalent line in get_rate_pitch
if( pid_log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
pid_log_counter = 0;
2013-06-30 06:51:43 -03:00
Log_Write_PID(CH6_RATE_ROLL_PITCH_KP, rate_error, p, i, d, output, tuning_value);
2012-08-21 23:19:50 -03:00
}
}
2012-07-14 01:24:22 -03:00
#endif
2012-08-21 23:19:50 -03:00
// output control
return output;
2012-02-11 02:22:19 -04:00
}
2012-02-28 08:26:37 -04:00
static int16_t
2012-08-21 23:19:50 -03:00
get_rate_pitch(int32_t target_rate)
2011-01-25 01:53:36 -04:00
{
2012-08-21 23:19:50 -03:00
int32_t p,i,d; // used to capture pid values for logging
int32_t current_rate; // this iteration's rate
int32_t rate_error; // simply target_rate - current_rate
int32_t output; // output from pid controller
2012-04-07 22:19:20 -03:00
2012-08-21 23:19:50 -03:00
// get current rate
current_rate = (omega.y * DEGX100);
2012-04-07 22:19:20 -03:00
2012-08-21 23:19:50 -03:00
// call pid controller
rate_error = target_rate - current_rate;
2012-10-28 16:12:04 -03:00
p = g.pid_rate_pitch.get_p(rate_error);
2013-07-10 03:12:04 -03:00
// get i term
i = g.pid_rate_pitch.get_integrator();
// update i term as long as we haven't breached the limits or the I term will certainly reduce
2013-07-21 01:58:24 -03:00
if (!motors.limit.roll_pitch || ((i>0&&rate_error<0)||(i<0&&rate_error>0))) {
2012-10-28 16:12:04 -03:00
i = g.pid_rate_pitch.get_i(rate_error, G_Dt);
2012-10-10 03:54:46 -03:00
}
2013-07-10 03:12:04 -03:00
2012-10-28 16:12:04 -03:00
d = g.pid_rate_pitch.get_d(rate_error, G_Dt);
output = p + i + d;
2012-08-21 23:19:50 -03:00
// constrain output
2013-04-21 09:52:30 -03:00
output = constrain_int32(output, -5000, 5000);
2012-04-07 22:19:20 -03:00
#if LOGGING_ENABLED == ENABLED
2012-08-21 23:19:50 -03:00
// log output if PID logging is on and we are tuning the rate P, I or D gains
2013-06-30 06:51:43 -03:00
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_RATE_ROLL_PITCH_KP || g.radio_tuning == CH6_RATE_ROLL_PITCH_KI || g.radio_tuning == CH6_RATE_ROLL_PITCH_KD) ) {
2013-01-08 01:45:12 -04:00
if( pid_log_counter == 0 ) { // relies on get_rate_roll having updated pid_log_counter
2013-06-30 06:51:43 -03:00
Log_Write_PID(CH6_RATE_ROLL_PITCH_KP+100, rate_error, p, i, d, output, tuning_value);
2012-08-21 23:19:50 -03:00
}
}
2012-04-07 22:19:20 -03:00
#endif
2012-08-21 23:19:50 -03:00
// output control
return output;
2012-01-29 02:00:05 -04:00
}
2012-02-28 08:26:37 -04:00
static int16_t
2012-01-29 02:00:05 -04:00
get_rate_yaw(int32_t target_rate)
{
2012-08-21 23:19:50 -03:00
int32_t p,i,d; // used to capture pid values for logging
int32_t rate_error;
int32_t output;
2012-03-25 03:55:49 -03:00
2012-08-21 23:19:50 -03:00
// rate control
rate_error = target_rate - (omega.z * DEGX100);
2012-03-25 03:55:49 -03:00
2012-08-21 23:19:50 -03:00
// separately calculate p, i, d values for logging
p = g.pid_rate_yaw.get_p(rate_error);
2013-07-05 20:49:45 -03:00
// get i term
i = g.pid_rate_yaw.get_integrator();
// update i term as long as we haven't breached the limits or the I term will certainly reduce
2013-07-21 01:58:24 -03:00
if (!motors.limit.yaw || ((i>0&&rate_error<0)||(i<0&&rate_error>0))) {
2012-10-10 03:54:46 -03:00
i = g.pid_rate_yaw.get_i(rate_error, G_Dt);
}
2013-07-05 20:49:45 -03:00
// get d value
2012-08-21 23:19:50 -03:00
d = g.pid_rate_yaw.get_d(rate_error, G_Dt);
2012-03-25 03:55:49 -03:00
2012-08-21 23:19:50 -03:00
output = p+i+d;
2013-04-21 09:52:30 -03:00
output = constrain_int32(output, -4500, 4500);
2012-03-25 03:55:49 -03:00
2012-03-27 01:33:21 -03:00
#if LOGGING_ENABLED == ENABLED
2012-08-21 23:19:50 -03:00
// log output if PID loggins is on and we are tuning the yaw
2013-01-08 01:45:12 -04:00
if( g.log_bitmask & MASK_LOG_PID && g.radio_tuning == CH6_YAW_RATE_KP ) {
pid_log_counter++;
if( pid_log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
pid_log_counter = 0;
2012-08-21 23:19:50 -03:00
Log_Write_PID(CH6_YAW_RATE_KP, rate_error, p, i, d, output, tuning_value);
}
}
2012-03-27 01:33:21 -03:00
#endif
2012-03-25 03:55:49 -03:00
2012-08-21 23:19:50 -03:00
// constrain output
return output;
2010-12-19 12:40:33 -04:00
}
2012-11-26 22:02:41 -04:00
#endif // !HELI_FRAME
2010-12-19 12:40:33 -04:00
2012-01-25 08:55:14 -04:00
// calculate modified roll/pitch depending upon optical flow calculated position
2012-01-09 00:53:54 -04:00
static int32_t
2012-08-17 23:48:12 -03:00
get_of_roll(int32_t input_roll)
2012-01-09 00:53:54 -04:00
{
2012-12-10 11:09:45 -04:00
#if OPTFLOW == ENABLED
2012-08-21 23:19:50 -03:00
static float tot_x_cm = 0; // total distance from target
2012-02-28 08:26:37 -04:00
static uint32_t last_of_roll_update = 0;
2012-08-21 23:19:50 -03:00
int32_t new_roll = 0;
int32_t p,i,d;
// check if new optflow data available
if( optflow.last_update != last_of_roll_update) {
last_of_roll_update = optflow.last_update;
// add new distance moved
tot_x_cm += optflow.x_cm;
// only stop roll if caller isn't modifying roll
if( input_roll == 0 && current_loc.alt < 1500) {
p = g.pid_optflow_roll.get_p(-tot_x_cm);
2013-01-23 01:50:44 -04:00
i = g.pid_optflow_roll.get_i(-tot_x_cm,1.0f); // we could use the last update time to calculate the time change
d = g.pid_optflow_roll.get_d(-tot_x_cm,1.0f);
2012-08-21 23:19:50 -03:00
new_roll = p+i+d;
}else{
g.pid_optflow_roll.reset_I();
tot_x_cm = 0;
p = 0; // for logging
i = 0;
d = 0;
}
// limit amount of change and maximum angle
2013-04-21 09:52:30 -03:00
of_roll = constrain_int32(new_roll, (of_roll-20), (of_roll+20));
2012-08-21 23:19:50 -03:00
#if LOGGING_ENABLED == ENABLED
// log output if PID logging is on and we are tuning the rate P, I or D gains
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_OPTFLOW_KP || g.radio_tuning == CH6_OPTFLOW_KI || g.radio_tuning == CH6_OPTFLOW_KD) ) {
2013-01-08 01:45:12 -04:00
pid_log_counter++; // Note: get_of_pitch pid logging relies on this function updating pid_log_counter so if you change the line above you must change the equivalent line in get_of_pitch
if( pid_log_counter >= 5 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
pid_log_counter = 0;
2012-08-21 23:19:50 -03:00
Log_Write_PID(CH6_OPTFLOW_KP, tot_x_cm, p, i, d, of_roll, tuning_value);
}
}
#endif // LOGGING_ENABLED == ENABLED
}
// limit max angle
2013-04-21 09:52:30 -03:00
of_roll = constrain_int32(of_roll, -1000, 1000);
2012-04-21 08:17:12 -03:00
2012-08-17 23:48:12 -03:00
return input_roll+of_roll;
2012-01-09 00:53:54 -04:00
#else
2012-08-17 23:48:12 -03:00
return input_roll;
2012-11-23 02:57:49 -04:00
#endif
2012-01-09 00:53:54 -04:00
}
static int32_t
2012-08-17 23:48:12 -03:00
get_of_pitch(int32_t input_pitch)
2012-01-09 00:53:54 -04:00
{
2012-12-10 11:09:45 -04:00
#if OPTFLOW == ENABLED
2012-01-25 08:55:14 -04:00
static float tot_y_cm = 0; // total distance from target
2012-02-28 08:26:37 -04:00
static uint32_t last_of_pitch_update = 0;
2012-08-21 23:19:50 -03:00
int32_t new_pitch = 0;
int32_t p,i,d;
// check if new optflow data available
if( optflow.last_update != last_of_pitch_update ) {
last_of_pitch_update = optflow.last_update;
// add new distance moved
tot_y_cm += optflow.y_cm;
// only stop roll if caller isn't modifying pitch
if( input_pitch == 0 && current_loc.alt < 1500 ) {
p = g.pid_optflow_pitch.get_p(tot_y_cm);
2013-01-23 01:50:44 -04:00
i = g.pid_optflow_pitch.get_i(tot_y_cm, 1.0f); // we could use the last update time to calculate the time change
d = g.pid_optflow_pitch.get_d(tot_y_cm, 1.0f);
2012-08-21 23:19:50 -03:00
new_pitch = p + i + d;
}else{
tot_y_cm = 0;
g.pid_optflow_pitch.reset_I();
p = 0; // for logging
i = 0;
d = 0;
}
// limit amount of change
2013-04-21 09:52:30 -03:00
of_pitch = constrain_int32(new_pitch, (of_pitch-20), (of_pitch+20));
2012-08-21 23:19:50 -03:00
#if LOGGING_ENABLED == ENABLED
// log output if PID logging is on and we are tuning the rate P, I or D gains
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_OPTFLOW_KP || g.radio_tuning == CH6_OPTFLOW_KI || g.radio_tuning == CH6_OPTFLOW_KD) ) {
2013-01-08 01:45:12 -04:00
if( pid_log_counter == 0 ) { // relies on get_of_roll having updated the pid_log_counter
2012-08-21 23:19:50 -03:00
Log_Write_PID(CH6_OPTFLOW_KP+100, tot_y_cm, p, i, d, of_pitch, tuning_value);
}
}
#endif // LOGGING_ENABLED == ENABLED
}
// limit max angle
2013-04-21 09:52:30 -03:00
of_pitch = constrain_int32(of_pitch, -1000, 1000);
2012-04-21 08:17:12 -03:00
2012-08-17 23:48:12 -03:00
return input_pitch+of_pitch;
2012-01-09 00:53:54 -04:00
#else
2012-08-17 23:48:12 -03:00
return input_pitch;
2012-11-23 02:57:49 -04:00
#endif
}
2012-12-08 01:23:32 -04:00
/*************************************************************
* yaw controllers
*************************************************************/
2013-03-22 05:38:07 -03:00
// get_look_at_yaw - updates bearing to look at center of circle or do a panorama
// should be called at 100hz
static void get_circle_yaw()
{
static uint8_t look_at_yaw_counter = 0; // used to reduce update rate to 10hz
// if circle radius is zero do panorama
if( g.circle_radius == 0 ) {
// slew yaw towards circle angle
nav_yaw = get_yaw_slew(nav_yaw, ToDeg(circle_angle)*100, AUTO_YAW_SLEW_RATE);
}else{
look_at_yaw_counter++;
if( look_at_yaw_counter >= 10 ) {
look_at_yaw_counter = 0;
yaw_look_at_WP_bearing = pv_get_bearing_cd(inertial_nav.get_position(), yaw_look_at_WP);
}
// slew yaw
nav_yaw = get_yaw_slew(nav_yaw, yaw_look_at_WP_bearing, AUTO_YAW_SLEW_RATE);
}
// call stabilize yaw controller
get_stabilize_yaw(nav_yaw);
}
// get_look_at_yaw - updates bearing to location held in look_at_yaw_WP and calls stabilize yaw controller
// should be called at 100hz
static void get_look_at_yaw()
{
static uint8_t look_at_yaw_counter = 0; // used to reduce update rate to 10hz
look_at_yaw_counter++;
if( look_at_yaw_counter >= 10 ) {
look_at_yaw_counter = 0;
yaw_look_at_WP_bearing = pv_get_bearing_cd(inertial_nav.get_position(), yaw_look_at_WP);
}
// slew yaw and call stabilize controller
nav_yaw = get_yaw_slew(nav_yaw, yaw_look_at_WP_bearing, AUTO_YAW_SLEW_RATE);
get_stabilize_yaw(nav_yaw);
}
2012-12-08 01:23:32 -04:00
static void get_look_ahead_yaw(int16_t pilot_yaw)
{
// Commanded Yaw to automatically look ahead.
2013-08-01 02:12:55 -03:00
if (g_gps->fix && g_gps->ground_speed_cm > YAW_LOOK_AHEAD_MIN_SPEED) {
2013-07-10 01:02:48 -03:00
nav_yaw = get_yaw_slew(nav_yaw, g_gps->ground_course_cd, AUTO_YAW_SLEW_RATE);
2013-03-28 23:14:31 -03:00
get_stabilize_yaw(wrap_360_cd(nav_yaw + pilot_yaw)); // Allow pilot to "skid" around corners up to 45 degrees
2012-12-08 01:23:32 -04:00
}else{
2013-08-04 08:22:12 -03:00
nav_yaw += pilot_yaw * g.acro_yaw_p * G_Dt;
2013-03-28 23:14:31 -03:00
nav_yaw = wrap_360_cd(nav_yaw);
2012-12-08 01:23:32 -04:00
get_stabilize_yaw(nav_yaw);
}
}
2012-11-23 02:57:49 -04:00
/*************************************************************
* throttle control
****************************************************************/
// update_throttle_cruise - update throttle cruise if necessary
static void update_throttle_cruise(int16_t throttle)
{
// ensure throttle_avg has been initialised
if( throttle_avg == 0 ) {
throttle_avg = g.throttle_cruise;
}
2012-11-24 09:50:09 -04:00
// calc average throttle if we are in a level hover
if (throttle > g.throttle_min && abs(climb_rate) < 60 && labs(ahrs.roll_sensor) < 500 && labs(ahrs.pitch_sensor) < 500) {
2013-01-10 14:42:24 -04:00
throttle_avg = throttle_avg * 0.99f + (float)throttle * 0.01f;
2012-11-23 02:57:49 -04:00
g.throttle_cruise = throttle_avg;
}
}
#if FRAME_CONFIG == HELI_FRAME
2012-11-24 00:41:17 -04:00
// get_angle_boost - returns a throttle including compensation for roll/pitch angle
2012-11-23 02:57:49 -04:00
// throttle value should be 0 ~ 1000
2012-11-24 00:41:17 -04:00
// for traditional helicopters
static int16_t get_angle_boost(int16_t throttle)
2012-11-23 02:57:49 -04:00
{
float angle_boost_factor = cos_pitch_x * cos_roll_x;
2013-05-01 21:26:49 -03:00
angle_boost_factor = 1.0f - constrain_float(angle_boost_factor, .5f, 1.0f);
2013-11-04 07:54:47 -04:00
int16_t throttle_above_mid = max(throttle - motors.get_collective_mid(),0);
2012-11-23 02:57:49 -04:00
2012-11-24 00:41:17 -04:00
// to allow logging of angle boost
angle_boost = throttle_above_mid*angle_boost_factor;
return throttle + angle_boost;
2012-11-23 02:57:49 -04:00
}
2012-11-24 00:41:17 -04:00
#else // all multicopters
// get_angle_boost - returns a throttle including compensation for roll/pitch angle
// throttle value should be 0 ~ 1000
static int16_t get_angle_boost(int16_t throttle)
{
float temp = cos_pitch_x * cos_roll_x;
int16_t throttle_out;
2013-05-01 21:26:49 -03:00
temp = constrain_float(temp, 0.5f, 1.0f);
2013-03-01 21:28:44 -04:00
// reduce throttle if we go inverted
2013-05-01 21:26:49 -03:00
temp = constrain_float(9000-max(labs(ahrs.roll_sensor),labs(ahrs.pitch_sensor)), 0, 3000) / (3000 * temp);
2013-03-01 21:28:44 -04:00
// apply scale and constrain throttle
2013-05-01 21:26:49 -03:00
throttle_out = constrain_float((float)(throttle-g.throttle_min) * temp + g.throttle_min, g.throttle_min, 1000);
2012-11-24 00:41:17 -04:00
// to allow logging of angle boost
angle_boost = throttle_out - throttle;
return throttle_out;
}
#endif // FRAME_CONFIG == HELI_FRAME
2012-11-23 02:57:49 -04:00
2012-12-03 10:05:14 -04:00
// set_throttle_out - to be called by upper throttle controllers when they wish to provide throttle output directly to motors
2012-11-23 02:57:49 -04:00
// provide 0 to cut motors
2012-11-24 00:41:17 -04:00
void set_throttle_out( int16_t throttle_out, bool apply_angle_boost )
2012-11-23 02:57:49 -04:00
{
2012-11-24 00:41:17 -04:00
if( apply_angle_boost ) {
g.rc_3.servo_out = get_angle_boost(throttle_out);
}else{
g.rc_3.servo_out = throttle_out;
// clear angle_boost for logging purposes
angle_boost = 0;
}
2013-02-27 03:57:04 -04:00
// update compass with throttle value
compass.set_throttle((float)g.rc_3.servo_out/1000.0f);
2012-11-23 02:57:49 -04:00
}
// set_throttle_accel_target - to be called by upper throttle controllers to set desired vertical acceleration in earth frame
void set_throttle_accel_target( int16_t desired_acceleration )
{
2013-07-12 10:57:30 -03:00
throttle_accel_target_ef = desired_acceleration;
throttle_accel_controller_active = true;
2012-12-03 10:05:14 -04:00
}
// disable_throttle_accel - disables the accel based throttle controller
// it will be re-enasbled on the next set_throttle_accel_target
// required when we wish to set motors to zero when pilot inputs zero throttle
void throttle_accel_deactivate()
{
throttle_accel_controller_active = false;
2012-11-23 02:57:49 -04:00
}
2013-07-26 09:43:09 -03:00
// set_throttle_takeoff - allows parents to tell throttle controller we are taking off so I terms can be cleared
static void
set_throttle_takeoff()
{
2013-07-27 22:47:19 -03:00
// set alt target
2013-11-11 09:29:09 -04:00
controller_desired_alt = current_loc.alt + ALT_HOLD_TAKEOFF_JUMP;
2013-07-27 22:47:19 -03:00
// clear i term from acceleration controller
if (g.pid_throttle_accel.get_integrator() < 0) {
2013-07-26 09:43:09 -03:00
g.pid_throttle_accel.reset_I();
2013-07-27 22:47:19 -03:00
}
2013-09-12 10:29:53 -03:00
// tell motors to do a slow start
motors.slow_start(true);
2013-07-26 09:43:09 -03:00
}
2012-11-23 02:57:49 -04:00
// get_throttle_accel - accelerometer based throttle controller
// returns an actual throttle output (0 ~ 1000) to be sent to the motors
static int16_t
get_throttle_accel(int16_t z_target_accel)
{
2012-12-17 01:36:05 -04:00
static float z_accel_error = 0; // The acceleration error in cm.
2013-01-08 03:41:07 -04:00
static uint32_t last_call_ms = 0; // the last time this controller was called
2012-12-17 01:36:05 -04:00
int32_t p,i,d; // used to capture pid values for logging
int16_t output;
float z_accel_meas;
2013-01-08 03:41:07 -04:00
uint32_t now = millis();
2012-11-23 02:57:49 -04:00
// Calculate Earth Frame Z acceleration
2013-01-01 22:52:53 -04:00
z_accel_meas = -(ahrs.get_accel_ef().z + GRAVITY_MSS) * 100;
2012-11-23 02:57:49 -04:00
2013-01-08 03:41:07 -04:00
// reset target altitude if this controller has just been engaged
if( now - last_call_ms > 100 ) {
// Reset Filter
z_accel_error = 0;
} else {
// calculate accel error and Filter with fc = 2 Hz
2013-05-01 21:26:49 -03:00
z_accel_error = z_accel_error + 0.11164f * (constrain_float(z_target_accel - z_accel_meas, -32000, 32000) - z_accel_error);
2013-01-08 03:41:07 -04:00
}
last_call_ms = now;
2012-11-23 02:57:49 -04:00
// separately calculate p, i, d values for logging
p = g.pid_throttle_accel.get_p(z_accel_error);
2013-07-25 12:45:59 -03:00
// get i term
i = g.pid_throttle_accel.get_integrator();
// update i term as long as we haven't breached the limits or the I term will certainly reduce
2013-07-26 02:15:27 -03:00
if ((!motors.limit.throttle_lower && !motors.limit.throttle_upper) || (i>0&&z_accel_error<0) || (i<0&&z_accel_error>0)) {
2013-01-23 01:50:44 -04:00
i = g.pid_throttle_accel.get_i(z_accel_error, .01f);
2012-11-23 02:57:49 -04:00
}
2013-07-25 12:45:59 -03:00
2013-01-23 01:50:44 -04:00
d = g.pid_throttle_accel.get_d(z_accel_error, .01f);
2012-11-23 02:57:49 -04:00
2013-07-18 17:02:54 -03:00
#if FRAME_CONFIG == HELI_FRAME
if (ap.takeoff_complete == true){
output = constrain_float(p+i+d+g.throttle_cruise, g.throttle_min, g.throttle_max);
} else {
// Avoid harshing the mechanics pushing into the ground
// stab_col_min should gently push down on the ground
2013-11-04 07:54:47 -04:00
output = constrain_float(p+i+d+g.throttle_cruise, motors.get_manual_collective_min(), motors.get_manual_collective_max());
2013-07-18 17:02:54 -03:00
}
#else
2013-05-01 21:26:49 -03:00
output = constrain_float(p+i+d+g.throttle_cruise, g.throttle_min, g.throttle_max);
2013-07-18 17:02:54 -03:00
#endif // HELI_FRAME
2012-11-23 02:57:49 -04:00
#if LOGGING_ENABLED == ENABLED
// log output if PID loggins is on and we are tuning the yaw
2013-06-30 06:51:43 -03:00
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_THROTTLE_ACCEL_KP || g.radio_tuning == CH6_THROTTLE_ACCEL_KI || g.radio_tuning == CH6_THROTTLE_ACCEL_KD) ) {
2013-01-08 01:45:12 -04:00
pid_log_counter++;
if( pid_log_counter >= 10 ) { // (update rate / desired output rate) = (50hz / 10hz) = 5hz
pid_log_counter = 0;
2013-06-30 06:51:43 -03:00
Log_Write_PID(CH6_THROTTLE_ACCEL_KP, z_accel_error, p, i, d, output, tuning_value);
2012-11-23 02:57:49 -04:00
}
}
#endif
return output;
}
2013-01-30 11:25:41 -04:00
// get_pilot_desired_throttle - transform pilot's throttle input to make cruise throttle mid stick
// used only for manual throttle modes
// returns throttle output 0 to 1000
#define THROTTLE_IN_MIDDLE 500 // the throttle mid point
static int16_t get_pilot_desired_throttle(int16_t throttle_control)
{
int16_t throttle_out;
// exit immediately in the simple cases
if( throttle_control == 0 || g.throttle_mid == 500) {
return throttle_control;
}
// ensure reasonable throttle values
2013-04-21 09:52:30 -03:00
throttle_control = constrain_int16(throttle_control,0,1000);
g.throttle_mid = constrain_int16(g.throttle_mid,300,700);
2013-01-30 11:25:41 -04:00
// check throttle is above, below or in the deadband
if (throttle_control < THROTTLE_IN_MIDDLE) {
// below the deadband
2013-01-31 10:21:18 -04:00
throttle_out = g.throttle_min + ((float)(throttle_control-g.throttle_min))*((float)(g.throttle_mid - g.throttle_min))/((float)(500-g.throttle_min));
2013-01-30 11:25:41 -04:00
}else if(throttle_control > THROTTLE_IN_MIDDLE) {
// above the deadband
throttle_out = g.throttle_mid + ((float)(throttle_control-500))*(float)(1000-g.throttle_mid)/500.0f;
}else{
// must be in the deadband
throttle_out = g.throttle_mid;
}
return throttle_out;
}
2012-11-23 02:57:49 -04:00
// get_pilot_desired_climb_rate - transform pilot's throttle input to
// climb rate in cm/s. we use radio_in instead of control_in to get the full range
// without any deadzone at the bottom
#define THROTTLE_IN_DEADBAND_TOP (THROTTLE_IN_MIDDLE+THROTTLE_IN_DEADBAND) // top of the deadband
#define THROTTLE_IN_DEADBAND_BOTTOM (THROTTLE_IN_MIDDLE-THROTTLE_IN_DEADBAND) // bottom of the deadband
static int16_t get_pilot_desired_climb_rate(int16_t throttle_control)
{
int16_t desired_rate = 0;
// throttle failsafe check
2013-09-26 05:54:33 -03:00
if( failsafe.radio ) {
2012-11-23 02:57:49 -04:00
return 0;
}
// ensure a reasonable throttle value
2013-04-21 09:52:30 -03:00
throttle_control = constrain_int16(throttle_control,0,1000);
2012-11-23 02:57:49 -04:00
// check throttle is above, below or in the deadband
if (throttle_control < THROTTLE_IN_DEADBAND_BOTTOM) {
// below the deadband
2012-12-21 23:52:49 -04:00
desired_rate = (int32_t)g.pilot_velocity_z_max * (throttle_control-THROTTLE_IN_DEADBAND_BOTTOM) / (THROTTLE_IN_MIDDLE - THROTTLE_IN_DEADBAND);
2012-11-23 02:57:49 -04:00
}else if (throttle_control > THROTTLE_IN_DEADBAND_TOP) {
// above the deadband
2012-12-21 23:52:49 -04:00
desired_rate = (int32_t)g.pilot_velocity_z_max * (throttle_control-THROTTLE_IN_DEADBAND_TOP) / (THROTTLE_IN_MIDDLE - THROTTLE_IN_DEADBAND);
2012-11-23 02:57:49 -04:00
}else{
// must be in the deadband
desired_rate = 0;
}
2012-11-24 03:45:28 -04:00
// desired climb rate for logging
desired_climb_rate = desired_rate;
2012-11-23 02:57:49 -04:00
return desired_rate;
}
2013-03-17 01:24:49 -03:00
// get_initial_alt_hold - get new target altitude based on current altitude and climb rate
static int32_t
get_initial_alt_hold( int32_t alt_cm, int16_t climb_rate_cms)
{
int32_t target_alt;
int32_t linear_distance; // half the distace we swap between linear and sqrt and the distace we offset sqrt.
int32_t linear_velocity; // the velocity we swap between linear and sqrt.
linear_velocity = ALT_HOLD_ACCEL_MAX/g.pi_alt_hold.kP();
if (abs(climb_rate_cms) < linear_velocity) {
target_alt = alt_cm + climb_rate_cms/g.pi_alt_hold.kP();
} else {
linear_distance = ALT_HOLD_ACCEL_MAX/(2*g.pi_alt_hold.kP()*g.pi_alt_hold.kP());
if (climb_rate_cms > 0){
target_alt = alt_cm + linear_distance + (int32_t)climb_rate_cms*(int32_t)climb_rate_cms/(2*ALT_HOLD_ACCEL_MAX);
} else {
target_alt = alt_cm - ( linear_distance + (int32_t)climb_rate_cms*(int32_t)climb_rate_cms/(2*ALT_HOLD_ACCEL_MAX) );
}
}
2013-04-21 09:52:30 -03:00
return constrain_int32(target_alt, alt_cm - ALT_HOLD_INIT_MAX_OVERSHOOT, alt_cm + ALT_HOLD_INIT_MAX_OVERSHOOT);
2013-03-17 01:24:49 -03:00
}
2012-11-23 02:57:49 -04:00
// get_throttle_rate - calculates desired accel required to achieve desired z_target_speed
// sets accel based throttle controller target
static void
2013-04-02 09:10:34 -03:00
get_throttle_rate(float z_target_speed)
2012-11-23 02:57:49 -04:00
{
2013-01-08 03:41:07 -04:00
static uint32_t last_call_ms = 0;
2012-12-17 01:36:05 -04:00
static float z_rate_error = 0; // The velocity error in cm.
2013-06-01 09:13:47 -03:00
static float z_target_speed_filt = 0; // The filtered requested speed
float z_target_speed_delta; // The change in requested speed
2013-07-22 23:02:04 -03:00
int32_t p; // used to capture pid values for logging
2013-04-02 09:10:34 -03:00
int32_t output; // the target acceleration if the accel based throttle is enabled, otherwise the output to be sent to the motors
2013-01-08 03:41:07 -04:00
uint32_t now = millis();
2012-11-23 02:57:49 -04:00
2013-01-08 03:41:07 -04:00
// reset target altitude if this controller has just been engaged
if( now - last_call_ms > 100 ) {
// Reset Filter
2013-06-01 09:13:47 -03:00
z_rate_error = 0;
z_target_speed_filt = z_target_speed;
2013-04-01 04:10:44 -03:00
output = 0;
2013-01-08 03:41:07 -04:00
} else {
// calculate rate error and filter with cut off frequency of 2 Hz
2013-01-10 14:42:24 -04:00
z_rate_error = z_rate_error + 0.20085f * ((z_target_speed - climb_rate) - z_rate_error);
2013-06-01 09:13:47 -03:00
// feed forward acceleration based on change in the filtered desired speed.
z_target_speed_delta = 0.20085f * (z_target_speed - z_target_speed_filt);
z_target_speed_filt = z_target_speed_filt + z_target_speed_delta;
output = z_target_speed_delta * 50.0f; // To-Do: replace 50 with dt
2013-01-08 03:41:07 -04:00
}
last_call_ms = now;
2012-11-23 02:57:49 -04:00
2013-07-22 23:02:04 -03:00
// calculate p
p = g.pid_throttle_rate.kP() * z_rate_error;
2012-11-23 02:57:49 -04:00
2013-04-02 09:10:34 -03:00
// consolidate and constrain target acceleration
2013-07-22 23:02:04 -03:00
output += p;
2013-04-02 09:10:34 -03:00
output = constrain_int32(output, -32000, 32000);
2012-11-23 02:57:49 -04:00
#if LOGGING_ENABLED == ENABLED
// log output if PID loggins is on and we are tuning the yaw
2013-06-30 06:51:43 -03:00
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_THROTTLE_RATE_KP || g.radio_tuning == CH6_THROTTLE_RATE_KD) ) {
2013-01-08 01:45:12 -04:00
pid_log_counter++;
if( pid_log_counter >= 10 ) { // (update rate / desired output rate) = (50hz / 10hz) = 5hz
pid_log_counter = 0;
2013-07-22 23:02:04 -03:00
Log_Write_PID(CH6_THROTTLE_RATE_KP, z_rate_error, p, 0, 0, output, tuning_value);
2012-11-23 02:57:49 -04:00
}
}
#endif
2013-07-12 10:57:30 -03:00
// set target for accel based throttle controller
set_throttle_accel_target(output);
2012-11-24 09:50:09 -04:00
// update throttle cruise
2012-12-05 21:27:38 -04:00
// TO-DO: this may not be correct because g.rc_3.servo_out has not been updated for this iteration
2012-11-24 09:50:09 -04:00
if( z_target_speed == 0 ) {
update_throttle_cruise(g.rc_3.servo_out);
}
2012-11-23 02:57:49 -04:00
}
// get_throttle_althold - hold at the desired altitude in cm
// updates accel based throttle controller targets
// Note: max_climb_rate is an optional parameter to allow reuse of this function by landing controller
static void
2012-12-21 23:52:49 -04:00
get_throttle_althold(int32_t target_alt, int16_t min_climb_rate, int16_t max_climb_rate)
2012-11-23 02:57:49 -04:00
{
2013-01-08 01:45:12 -04:00
int32_t alt_error;
2013-03-17 01:24:49 -03:00
float desired_rate;
int32_t linear_distance; // half the distace we swap between linear and sqrt and the distace we offset sqrt.
2012-12-17 01:36:05 -04:00
// calculate altitude error
2013-01-08 01:45:12 -04:00
alt_error = target_alt - current_loc.alt;
2012-12-17 01:36:05 -04:00
2012-12-26 08:18:16 -04:00
// check kP to avoid division by zero
if( g.pi_alt_hold.kP() != 0 ) {
2013-03-17 01:24:49 -03:00
linear_distance = ALT_HOLD_ACCEL_MAX/(2*g.pi_alt_hold.kP()*g.pi_alt_hold.kP());
2013-01-08 01:45:12 -04:00
if( alt_error > 2*linear_distance ) {
2013-03-17 01:24:49 -03:00
desired_rate = safe_sqrt(2*ALT_HOLD_ACCEL_MAX*(alt_error-linear_distance));
2013-01-08 01:45:12 -04:00
}else if( alt_error < -2*linear_distance ) {
2013-03-17 01:24:49 -03:00
desired_rate = -safe_sqrt(2*ALT_HOLD_ACCEL_MAX*(-alt_error-linear_distance));
2012-12-26 08:18:16 -04:00
}else{
2013-01-08 01:45:12 -04:00
desired_rate = g.pi_alt_hold.get_p(alt_error);
2012-12-26 08:18:16 -04:00
}
2012-12-22 22:17:26 -04:00
}else{
2012-12-26 08:18:16 -04:00
desired_rate = 0;
2012-12-17 01:36:05 -04:00
}
2012-11-23 02:57:49 -04:00
2013-05-01 21:26:49 -03:00
desired_rate = constrain_float(desired_rate, min_climb_rate, max_climb_rate);
2012-11-23 02:57:49 -04:00
// call rate based throttle controller which will update accel based throttle controller targets
get_throttle_rate(desired_rate);
2013-01-08 01:45:12 -04:00
// update altitude error reported to GCS
altitude_error = alt_error;
2012-11-23 02:57:49 -04:00
// TO-DO: enabled PID logging for this controller
}
2013-01-08 03:41:07 -04:00
// get_throttle_althold_with_slew - altitude controller with slew to avoid step changes in altitude target
// calls normal althold controller which updates accel based throttle controller targets
static void
2013-03-16 23:14:35 -03:00
get_throttle_althold_with_slew(int32_t target_alt, int16_t min_climb_rate, int16_t max_climb_rate)
2013-01-08 03:41:07 -04:00
{
2013-07-25 12:45:59 -03:00
float alt_change = target_alt-controller_desired_alt;
// adjust desired alt if motors have not hit their limits
if ((alt_change<0 && !motors.limit.throttle_lower) || (alt_change>0 && !motors.limit.throttle_upper)) {
controller_desired_alt += constrain_float(alt_change, min_climb_rate*0.02f, max_climb_rate*0.02f);
}
2013-01-08 03:41:07 -04:00
// do not let target altitude get too far from current altitude
2013-05-01 21:26:49 -03:00
controller_desired_alt = constrain_float(controller_desired_alt,current_loc.alt-750,current_loc.alt+750);
2013-01-08 03:41:07 -04:00
get_throttle_althold(controller_desired_alt, min_climb_rate-250, max_climb_rate+250); // 250 is added to give head room to alt hold controller
}
2012-12-21 23:52:49 -04:00
// get_throttle_rate_stabilized - rate controller with additional 'stabilizer'
// 'stabilizer' ensure desired rate is being met
// calls normal throttle rate controller which updates accel based throttle controller targets
static void
get_throttle_rate_stabilized(int16_t target_rate)
{
2013-07-25 12:45:59 -03:00
// adjust desired alt if motors have not hit their limits
if ((target_rate<0 && !motors.limit.throttle_lower) || (target_rate>0 && !motors.limit.throttle_upper)) {
controller_desired_alt += target_rate * 0.02f;
}
2012-12-21 23:52:49 -04:00
// do not let target altitude get too far from current altitude
2013-05-01 21:26:49 -03:00
controller_desired_alt = constrain_float(controller_desired_alt,current_loc.alt-750,current_loc.alt+750);
2012-12-21 23:52:49 -04:00
2013-08-15 04:06:27 -03:00
#if AC_FENCE == ENABLED
// do not let target altitude be too close to the fence
// To-Do: add this to other altitude controllers
if((fence.get_enabled_fences() & AC_FENCE_TYPE_ALT_MAX) != 0) {
float alt_limit = fence.get_safe_alt() * 100.0f;
if (controller_desired_alt > alt_limit) {
controller_desired_alt = alt_limit;
}
}
#endif
2013-04-08 23:58:01 -03:00
// update target altitude for reporting purposes
set_target_alt_for_reporting(controller_desired_alt);
2012-12-21 23:52:49 -04:00
2013-01-08 03:41:07 -04:00
get_throttle_althold(controller_desired_alt, -g.pilot_velocity_z_max-250, g.pilot_velocity_z_max+250); // 250 is added to give head room to alt hold controller
2012-12-21 23:52:49 -04:00
}
2012-11-23 02:57:49 -04:00
// get_throttle_land - high level landing logic
// sends the desired acceleration in the accel based throttle controller
// called at 50hz
static void
get_throttle_land()
{
2012-12-29 00:51:14 -04:00
// if we are above 10m and the sonar does not sense anything perform regular alt hold descent
2013-01-08 03:41:07 -04:00
if (current_loc.alt >= LAND_START_ALT && !(g.sonar_enabled && sonar_alt_health >= SONAR_ALT_HEALTH_MAX)) {
2013-04-18 02:52:21 -03:00
get_throttle_althold_with_slew(LAND_START_ALT, -wp_nav.get_descent_velocity(), -abs(g.land_speed));
2012-11-23 02:57:49 -04:00
}else{
2012-11-24 09:50:09 -04:00
get_throttle_rate_stabilized(-abs(g.land_speed));
2013-07-26 02:15:27 -03:00
// disarm when the landing detector says we've landed and throttle is at min (or we're in failsafe so we have no pilot thorottle input)
2013-09-26 05:54:33 -03:00
if( ap.land_complete && (g.rc_3.control_in == 0 || failsafe.radio) ) {
2013-07-26 02:15:27 -03:00
init_disarm_motors();
}
}
}
// reset_land_detector - initialises land detector
static void reset_land_detector()
{
set_land_complete(false);
land_detector = 0;
}
// update_land_detector - checks if we have landed and updates the ap.land_complete flag
// returns true if we have landed
static bool update_land_detector()
{
// detect whether we have landed by watching for low climb rate and minimum throttle
if (abs(climb_rate) < 20 && motors.limit.throttle_lower) {
2013-07-27 22:47:19 -03:00
if (!ap.land_complete) {
// run throttle controller if accel based throttle controller is enabled and active (active means it has been given a target)
if( land_detector < LAND_DETECTOR_TRIGGER) {
land_detector++;
}else{
2013-07-26 02:15:27 -03:00
set_land_complete(true);
2013-07-27 22:47:19 -03:00
land_detector = 0;
2012-11-24 09:50:09 -04:00
}
}
2013-07-26 02:15:27 -03:00
}else{
// we've sensed movement up or down so reset land_detector
land_detector = 0;
if(ap.land_complete) {
set_land_complete(false);
}
2012-11-23 02:57:49 -04:00
}
2013-07-26 02:15:27 -03:00
// return current state of landing
return ap.land_complete;
2012-11-23 02:57:49 -04:00
}
2012-12-29 00:51:14 -04:00
// get_throttle_surface_tracking - hold copter at the desired distance above the ground
// updates accel based throttle controller targets
static void
get_throttle_surface_tracking(int16_t target_rate)
{
static uint32_t last_call_ms = 0;
2013-01-08 03:41:07 -04:00
float distance_error;
2013-08-18 21:52:59 -03:00
float velocity_correction;
2012-12-29 00:51:14 -04:00
uint32_t now = millis();
// reset target altitude if this controller has just been engaged
2013-01-08 03:41:07 -04:00
if( now - last_call_ms > 200 ) {
target_sonar_alt = sonar_alt + controller_desired_alt - current_loc.alt;
2012-12-29 00:51:14 -04:00
}
2013-01-08 03:41:07 -04:00
last_call_ms = now;
2012-12-29 00:51:14 -04:00
2013-08-18 21:52:59 -03:00
// adjust sonar target alt if motors have not hit their limits
2013-07-25 12:45:59 -03:00
if ((target_rate<0 && !motors.limit.throttle_lower) || (target_rate>0 && !motors.limit.throttle_upper)) {
target_sonar_alt += target_rate * 0.02f;
}
2012-12-29 00:51:14 -04:00
// do not let target altitude get too far from current altitude above ground
// Note: the 750cm limit is perhaps too wide but is consistent with the regular althold limits and helps ensure a smooth transition
2013-05-01 21:26:49 -03:00
target_sonar_alt = constrain_float(target_sonar_alt,sonar_alt-750,sonar_alt+750);
2013-04-08 23:58:01 -03:00
2013-08-18 21:52:59 -03:00
// calc desired velocity correction from target sonar alt vs actual sonar alt
distance_error = target_sonar_alt-sonar_alt;
velocity_correction = distance_error * g.sonar_gain;
velocity_correction = constrain_float(velocity_correction, -THR_SURFACE_TRACKING_VELZ_MAX, THR_SURFACE_TRACKING_VELZ_MAX);
2012-12-29 00:51:14 -04:00
2013-08-18 21:52:59 -03:00
// call regular rate stabilize alt hold controller
get_throttle_rate_stabilized(target_rate + velocity_correction);
2012-12-29 00:51:14 -04:00
}
2012-11-23 02:57:49 -04:00
/*
* reset all I integrators
*/
static void reset_I_all(void)
{
reset_rate_I();
reset_throttle_I();
reset_optflow_I();
}
static void reset_rate_I()
{
g.pid_rate_roll.reset_I();
g.pid_rate_pitch.reset_I();
g.pid_rate_yaw.reset_I();
}
static void reset_optflow_I(void)
{
g.pid_optflow_roll.reset_I();
g.pid_optflow_pitch.reset_I();
of_roll = 0;
of_pitch = 0;
}
static void reset_throttle_I(void)
{
// For Altitude Hold
g.pi_alt_hold.reset_I();
2012-12-17 01:36:05 -04:00
g.pid_throttle_accel.reset_I();
2012-11-23 02:57:49 -04:00
}
2013-01-31 03:30:03 -04:00
static void set_accel_throttle_I_from_pilot_throttle(int16_t pilot_throttle)
2013-01-11 23:20:37 -04:00
{
// shift difference between pilot's throttle and hover throttle into accelerometer I
2013-01-31 03:30:03 -04:00
g.pid_throttle_accel.set_integrator(pilot_throttle-g.throttle_cruise);
2013-01-11 23:20:37 -04:00
}