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
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2011-11-07 18:24:32 -04:00
|
|
|
get_stabilize_roll(int32_t target_angle)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-12-03 21:54:38 -04:00
|
|
|
// angle error
|
2012-03-11 05:36:12 -03:00
|
|
|
target_angle = wrap_180(target_angle - ahrs.roll_sensor);
|
2011-01-25 01:53:36 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
// limit the error we're feeding to the PID
|
2012-01-29 02:00:05 -04:00
|
|
|
target_angle = constrain(target_angle, -4500, 4500);
|
2011-12-11 03:25:52 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
// convert to desired Rate:
|
2012-01-29 02:00:05 -04:00
|
|
|
target_angle = g.pi_stabilize_roll.get_pi(target_angle, G_Dt);
|
2011-12-11 03:25:52 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
// output control:
|
2012-01-29 02:00:05 -04:00
|
|
|
return constrain(target_angle, -4500, 4500);
|
2011-12-08 08:30:47 -04:00
|
|
|
#else
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2012-02-17 02:07:35 -04:00
|
|
|
// convert to desired Rate:
|
2012-01-29 02:00:05 -04:00
|
|
|
int32_t target_rate = g.pi_stabilize_roll.get_p(target_angle);
|
2011-12-11 03:25:52 -04:00
|
|
|
|
2012-06-05 20:41:31 -03:00
|
|
|
int16_t i_stab;
|
2012-08-16 05:51:09 -03:00
|
|
|
if(labs(ahrs.roll_sensor) < 500){
|
2012-06-05 20:41:31 -03:00
|
|
|
target_angle = constrain(target_angle, -500, 500);
|
|
|
|
i_stab = g.pi_stabilize_roll.get_i(target_angle, G_Dt);
|
|
|
|
}else{
|
|
|
|
i_stab = g.pi_stabilize_roll.get_integrator();
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_rate_roll(target_rate) + i_stab;
|
2011-12-08 08:30:47 -04:00
|
|
|
#endif
|
2011-04-25 02:12:59 -03:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2011-11-07 18:24:32 -04:00
|
|
|
get_stabilize_pitch(int32_t target_angle)
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
2011-12-03 21:54:38 -04:00
|
|
|
// angle error
|
2012-03-11 05:36:12 -03:00
|
|
|
target_angle = wrap_180(target_angle - ahrs.pitch_sensor);
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2010-12-19 12:40:33 -04:00
|
|
|
// limit the error we're feeding to the PID
|
2012-01-29 02:00:05 -04:00
|
|
|
target_angle = constrain(target_angle, -4500, 4500);
|
2011-12-11 03:25:52 -04:00
|
|
|
|
2011-12-08 08:30:47 -04:00
|
|
|
// convert to desired Rate:
|
2012-01-29 02:00:05 -04:00
|
|
|
target_angle = g.pi_stabilize_pitch.get_pi(target_angle, G_Dt);
|
2011-12-08 08:30:47 -04:00
|
|
|
|
|
|
|
// output control:
|
2012-01-29 02:00:05 -04:00
|
|
|
return constrain(target_angle, -4500, 4500);
|
2011-12-08 08:30:47 -04:00
|
|
|
#else
|
2011-04-10 17:31:33 -03:00
|
|
|
|
2012-06-05 20:41:31 -03:00
|
|
|
// convert to desired Rate:
|
2012-01-29 02:00:05 -04:00
|
|
|
int32_t target_rate = g.pi_stabilize_pitch.get_p(target_angle);
|
2011-12-03 21:54:38 -04:00
|
|
|
|
2012-06-05 20:41:31 -03:00
|
|
|
int16_t i_stab;
|
2012-08-16 05:51:09 -03:00
|
|
|
if(labs(ahrs.pitch_sensor) < 500){
|
2012-06-05 20:41:31 -03:00
|
|
|
target_angle = constrain(target_angle, -500, 500);
|
|
|
|
i_stab = g.pi_stabilize_pitch.get_i(target_angle, G_Dt);
|
|
|
|
}else{
|
|
|
|
i_stab = g.pi_stabilize_pitch.get_integrator();
|
|
|
|
}
|
|
|
|
return get_rate_pitch(target_rate) + i_stab;
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
#endif
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2012-01-29 02:00:05 -04:00
|
|
|
get_stabilize_yaw(int32_t target_angle)
|
|
|
|
{
|
2012-03-25 03:55:49 -03:00
|
|
|
int32_t target_rate,i_term;
|
|
|
|
int32_t angle_error;
|
|
|
|
int32_t output;
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
// angle error
|
2012-03-25 03:55:49 -03:00
|
|
|
angle_error = wrap_180(target_angle - ahrs.yaw_sensor);
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2012-02-24 07:18:40 -04:00
|
|
|
// limit the error we're feeding to the PID
|
2012-03-25 03:55:49 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
angle_error = constrain(angle_error, -4500, 4500);
|
2012-02-24 07:18:40 -04:00
|
|
|
#else
|
2012-06-26 03:12:19 -03:00
|
|
|
angle_error = constrain(angle_error, -4000, 4000);
|
2012-02-24 07:18:40 -04:00
|
|
|
#endif
|
2011-12-29 14:23:18 -04:00
|
|
|
|
2012-03-25 03:55:49 -03:00
|
|
|
// convert angle error to desired Rate:
|
|
|
|
target_rate = g.pi_stabilize_yaw.get_p(angle_error);
|
|
|
|
i_term = g.pi_stabilize_yaw.get_i(angle_error, G_Dt);
|
2011-12-29 14:23:18 -04:00
|
|
|
|
2012-03-25 03:55:49 -03:00
|
|
|
// do not use rate controllers for helicotpers with external gyros
|
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2012-04-04 10:51:39 -03:00
|
|
|
if(!motors.ext_gyro_enabled){
|
2012-03-27 01:36:58 -03:00
|
|
|
output = get_rate_yaw(target_rate) + i_term;
|
2012-01-29 02:00:05 -04:00
|
|
|
}else{
|
2012-03-25 03:55:49 -03:00
|
|
|
output = constrain((target_rate + i_term), -4500, 4500);
|
2012-01-29 02:00:05 -04:00
|
|
|
}
|
|
|
|
#else
|
2012-03-25 03:55:49 -03:00
|
|
|
output = get_rate_yaw(target_rate) + i_term;
|
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-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-03-25 03:55:49 -03:00
|
|
|
// log output if PID logging is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_YAW_KP || g.radio_tuning == CH6_YAW_RATE_KP) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
2012-04-07 22:19:20 -03:00
|
|
|
Log_Write_PID(CH6_YAW_KP, angle_error, target_rate, i_term, 0, output, tuning_value);
|
2012-03-25 03:55:49 -03:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:33:21 -03:00
|
|
|
#endif
|
2012-03-25 03:55:49 -03:00
|
|
|
|
|
|
|
// ensure output does not go beyond barries of what an int16_t can hold
|
|
|
|
return constrain(output,-32000,32000);
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2012-02-09 01:09:24 -04:00
|
|
|
get_acro_roll(int32_t target_rate)
|
|
|
|
{
|
2012-02-15 15:29:25 -04:00
|
|
|
target_rate = target_rate * g.acro_p;
|
2012-02-09 01:09:24 -04:00
|
|
|
return get_rate_roll(target_rate);
|
|
|
|
}
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2012-02-09 01:09:24 -04:00
|
|
|
get_acro_pitch(int32_t target_rate)
|
|
|
|
{
|
2012-02-15 15:29:25 -04:00
|
|
|
target_rate = target_rate * g.acro_p;
|
2012-02-09 01:09:24 -04:00
|
|
|
return get_rate_pitch(target_rate);
|
|
|
|
}
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2012-02-11 02:22:19 -04:00
|
|
|
get_acro_yaw(int32_t target_rate)
|
2012-07-14 16:26:13 -03:00
|
|
|
{
|
|
|
|
target_rate = g.pi_stabilize_yaw.get_p(target_rate);
|
|
|
|
return get_rate_yaw(target_rate);
|
|
|
|
}
|
|
|
|
|
2012-08-18 04:03:49 -03:00
|
|
|
/*
|
2012-07-14 16:26:13 -03:00
|
|
|
static int16_t
|
|
|
|
get_acro_yaw2(int32_t target_rate)
|
2012-02-11 02:22:19 -04:00
|
|
|
{
|
2012-07-14 01:24:22 -03:00
|
|
|
int32_t p,i,d; // used to capture pid values for logging
|
|
|
|
int32_t rate_error; // current yaw rate error
|
|
|
|
int32_t current_rate; // current real yaw rate
|
|
|
|
int32_t decel_boost; // gain scheduling if we are overshooting
|
|
|
|
int32_t output; // output to rate controller
|
|
|
|
|
2012-02-11 02:22:19 -04:00
|
|
|
target_rate = g.pi_stabilize_yaw.get_p(target_rate);
|
2012-07-14 01:24:22 -03:00
|
|
|
current_rate = omega.z * DEGX100;
|
|
|
|
rate_error = target_rate - current_rate;
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
//Gain Scheduling:
|
|
|
|
//If the yaw input is to the right, but stick is moving to the middle
|
2012-07-14 16:26:13 -03:00
|
|
|
//and actual rate is greater than the target rate then we are
|
2012-07-14 01:24:22 -03:00
|
|
|
//going to overshoot the yaw target to the left side, so we should
|
2012-07-14 16:26:13 -03:00
|
|
|
//strengthen the yaw output to slow down the yaw!
|
|
|
|
|
|
|
|
#if (FRAME_CONFIG == HELI_FRAME || FRAME_CONFIG == TRI_FRAME)
|
2012-07-19 02:40:31 -03:00
|
|
|
static int32_t last_target_rate = 0; // last iteration's target rate
|
2012-07-14 01:24:22 -03:00
|
|
|
if ( target_rate > 0 && last_target_rate > target_rate && rate_error < 0 ){
|
|
|
|
decel_boost = 1;
|
|
|
|
} else if (target_rate < 0 && last_target_rate < target_rate && rate_error > 0 ){
|
|
|
|
decel_boost = 1;
|
2012-08-16 05:51:09 -03:00
|
|
|
} else if (target_rate == 0 && labs(current_rate) > 1000){
|
2012-07-14 16:26:13 -03:00
|
|
|
decel_boost = 1;
|
2012-07-14 01:24:22 -03:00
|
|
|
} else {
|
|
|
|
decel_boost = 0;
|
|
|
|
}
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
last_target_rate = target_rate;
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
#else
|
|
|
|
|
|
|
|
decel_boost = 0;
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
#endif
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
// separately calculate p, i, d values for logging
|
|
|
|
// we will use d=0, and hold i at it's last value
|
|
|
|
// since manual inputs are never steady state
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
p = g.pid_rate_yaw.get_p(rate_error);
|
|
|
|
i = g.pid_rate_yaw.get_integrator();
|
|
|
|
d = 0;
|
|
|
|
|
|
|
|
if (decel_boost){
|
|
|
|
p *= 2;
|
|
|
|
}
|
2012-07-14 16:26:13 -03:00
|
|
|
|
2012-07-14 01:24:22 -03:00
|
|
|
output = p+i+d;
|
|
|
|
|
2012-07-14 16:26:13 -03:00
|
|
|
// output control:
|
2012-07-14 01:24:22 -03:00
|
|
|
// constrain output
|
|
|
|
output = constrain(output, -4500, 4500);
|
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
|
|
|
// log output if PID loggins is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_YAW_KP || g.radio_tuning == CH6_YAW_RATE_KP) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
|
|
|
Log_Write_PID(CH6_YAW_RATE_KP, rate_error, p, i, d, output, tuning_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return output;
|
2012-02-11 02:22:19 -04:00
|
|
|
}
|
2012-08-18 04:03:49 -03:00
|
|
|
*/
|
2012-02-11 02:22:19 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t
|
2012-01-29 02:00:05 -04:00
|
|
|
get_rate_roll(int32_t target_rate)
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
2012-02-28 08:26:37 -04:00
|
|
|
static int32_t last_rate = 0; // previous iterations rate
|
2012-04-07 22:19:20 -03:00
|
|
|
int32_t p,i,d; // used to capture pid values for logging
|
2012-02-28 08:26:37 -04:00
|
|
|
int32_t current_rate; // this iteration's rate
|
2012-04-07 22:19:20 -03:00
|
|
|
int32_t rate_error; // simply target_rate - current_rate
|
2012-02-28 08:26:37 -04:00
|
|
|
int32_t rate_d; // roll's acceleration
|
|
|
|
int32_t output; // output from pid controller
|
|
|
|
int32_t rate_d_dampener; // value to dampen output based on acceleration
|
2012-02-17 02:38:23 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// get current rate
|
|
|
|
current_rate = (omega.x * DEGX100);
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// calculate and filter the acceleration
|
2012-02-28 09:56:26 -04:00
|
|
|
rate_d = roll_rate_d_filter.apply(current_rate - last_rate);
|
2012-02-28 08:26:37 -04:00
|
|
|
|
|
|
|
// store rate for next iteration
|
2012-02-17 02:38:23 -04:00
|
|
|
last_rate = current_rate;
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// call pid controller
|
2012-04-07 22:19:20 -03:00
|
|
|
rate_error = target_rate - current_rate;
|
|
|
|
p = g.pid_rate_roll.get_p(rate_error);
|
|
|
|
i = g.pid_rate_roll.get_i(rate_error, G_Dt);
|
|
|
|
d = g.pid_rate_roll.get_d(rate_error, G_Dt);
|
|
|
|
output = p + i + d;
|
2011-12-07 01:03:56 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// Dampening output with D term
|
2012-02-29 00:15:03 -04:00
|
|
|
rate_d_dampener = rate_d * roll_scale_d;
|
2012-02-28 08:26:37 -04:00
|
|
|
rate_d_dampener = constrain(rate_d_dampener, -400, 400);
|
|
|
|
output -= rate_d_dampener;
|
|
|
|
|
2012-04-07 22:19:20 -03:00
|
|
|
// constrain output
|
2012-06-05 20:41:31 -03:00
|
|
|
output = constrain(output, -5000, 5000);
|
2012-04-07 22:19:20 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-04-07 22:19:20 -03:00
|
|
|
// 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_RATE_KP || g.radio_tuning == CH6_RATE_KI || g.radio_tuning == CH6_RATE_KD) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
|
|
|
Log_Write_PID(CH6_RATE_KP, rate_error, p, i, d-rate_d_dampener, output, tuning_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// output control
|
2012-04-07 22:19:20 -03:00
|
|
|
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_pitch(int32_t target_rate)
|
|
|
|
{
|
2012-02-28 08:26:37 -04:00
|
|
|
static int32_t last_rate = 0; // previous iterations rate
|
2012-04-07 22:19:20 -03:00
|
|
|
int32_t p,i,d; // used to capture pid values for logging
|
2012-02-28 08:26:37 -04:00
|
|
|
int32_t current_rate; // this iteration's rate
|
2012-04-07 22:19:20 -03:00
|
|
|
int32_t rate_error; // simply target_rate - current_rate
|
2012-02-28 08:26:37 -04:00
|
|
|
int32_t rate_d; // roll's acceleration
|
|
|
|
int32_t output; // output from pid controller
|
|
|
|
int32_t rate_d_dampener; // value to dampen output based on acceleration
|
2012-02-17 02:38:23 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// get current rate
|
|
|
|
current_rate = (omega.y * DEGX100);
|
2011-12-08 09:23:50 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// calculate and filter the acceleration
|
2012-02-28 09:56:26 -04:00
|
|
|
rate_d = pitch_rate_d_filter.apply(current_rate - last_rate);
|
2012-02-17 02:38:23 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// store rate for next iteration
|
|
|
|
last_rate = current_rate;
|
2012-01-29 02:00:05 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// call pid controller
|
2012-04-07 22:19:20 -03:00
|
|
|
rate_error = target_rate - current_rate;
|
|
|
|
p = g.pid_rate_pitch.get_p(rate_error);
|
|
|
|
i = g.pid_rate_pitch.get_i(rate_error, G_Dt);
|
|
|
|
d = g.pid_rate_pitch.get_d(rate_error, G_Dt);
|
|
|
|
output = p + i + d;
|
2012-02-17 02:38:23 -04:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// Dampening output with D term
|
2012-02-29 00:15:03 -04:00
|
|
|
rate_d_dampener = rate_d * pitch_scale_d;
|
2012-02-28 08:26:37 -04:00
|
|
|
rate_d_dampener = constrain(rate_d_dampener, -400, 400);
|
|
|
|
output -= rate_d_dampener;
|
2011-12-11 03:25:52 -04:00
|
|
|
|
2012-04-07 22:19:20 -03:00
|
|
|
// constrain output
|
2012-06-05 20:41:31 -03:00
|
|
|
output = constrain(output, -5000, 5000);
|
2012-04-07 22:19:20 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-04-07 22:19:20 -03:00
|
|
|
// 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_RATE_KP || g.radio_tuning == CH6_RATE_KI || g.radio_tuning == CH6_RATE_KD) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
|
|
|
Log_Write_PID(CH6_RATE_KP+100, rate_error, p, i, d-rate_d_dampener, output, tuning_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
// output control
|
2012-04-07 22:19:20 -03:00
|
|
|
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-04-07 22:19:20 -03:00
|
|
|
int32_t p,i,d; // used to capture pid values for logging
|
2012-03-25 03:55:49 -03:00
|
|
|
int32_t rate_error;
|
|
|
|
int32_t output;
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
// rate control
|
2012-03-25 03:55:49 -03:00
|
|
|
rate_error = target_rate - (omega.z * DEGX100);
|
|
|
|
|
|
|
|
// separately calculate p, i, d values for logging
|
|
|
|
p = g.pid_rate_yaw.get_p(rate_error);
|
|
|
|
i = g.pid_rate_yaw.get_i(rate_error, G_Dt);
|
|
|
|
d = g.pid_rate_yaw.get_d(rate_error, G_Dt);
|
|
|
|
|
|
|
|
output = p+i+d;
|
2012-06-22 11:06:01 -03:00
|
|
|
output = constrain(output, -4500, 4500);
|
2012-03-25 03:55:49 -03:00
|
|
|
|
2012-03-27 01:33:21 -03:00
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-03-25 03:55:49 -03:00
|
|
|
// log output if PID loggins is on and we are tuning the yaw
|
|
|
|
if( g.log_bitmask & MASK_LOG_PID && (g.radio_tuning == CH6_YAW_KP || g.radio_tuning == CH6_YAW_RATE_KP) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 10 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
2012-04-07 22:19:20 -03:00
|
|
|
Log_Write_PID(CH6_YAW_RATE_KP, rate_error, p, i, d, output, tuning_value);
|
2012-03-25 03:55:49 -03:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:33:21 -03:00
|
|
|
#endif
|
2012-03-25 03:55:49 -03:00
|
|
|
|
2012-08-09 20:45:47 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME || FRAME_CONFIG == TRI_FRAME
|
|
|
|
// constrain output
|
|
|
|
return output;
|
|
|
|
#else
|
|
|
|
// output control:
|
2012-08-16 19:38:46 -03:00
|
|
|
int16_t yaw_limit = 2200 + abs(g.rc_4.control_in);
|
2012-08-09 20:45:47 -03:00
|
|
|
// smoother Yaw control:
|
|
|
|
return constrain(output, -yaw_limit, yaw_limit);
|
|
|
|
#endif
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2012-07-19 02:40:31 -03:00
|
|
|
static int16_t
|
|
|
|
get_throttle_rate(int16_t z_target_speed)
|
|
|
|
{
|
|
|
|
int16_t z_rate_error, output;
|
|
|
|
|
2011-12-05 00:51:34 -04:00
|
|
|
// calculate rate error
|
2012-06-14 02:27:03 -03:00
|
|
|
#if INERTIAL_NAV == ENABLED
|
|
|
|
z_rate_error = z_target_speed - accels_velocity.z; // calc the speed error
|
|
|
|
#else
|
|
|
|
z_rate_error = z_target_speed - climb_rate; // calc the speed error
|
|
|
|
#endif
|
2012-03-07 02:22:14 -04:00
|
|
|
|
2012-07-19 02:40:31 -03:00
|
|
|
int32_t tmp = (z_target_speed * z_target_speed * (int32_t)g.throttle_cruise) / 200000;
|
|
|
|
|
|
|
|
if(z_target_speed < 0) tmp = -tmp;
|
|
|
|
|
|
|
|
output = constrain(tmp, -3200, 3200);
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
// limit the rate
|
2012-07-19 02:40:31 -03:00
|
|
|
output += constrain(g.pid_throttle.get_pid(z_rate_error, .02), -80, 120);
|
2011-12-05 00:51:34 -04:00
|
|
|
|
2012-07-19 02:40:31 -03:00
|
|
|
return output;
|
2011-09-04 21:15:36 -03:00
|
|
|
}
|
|
|
|
|
2012-01-03 14:32:16 -04:00
|
|
|
// Keeps old data out of our calculation / logs
|
2012-01-20 14:07:38 -04:00
|
|
|
static void reset_nav_params(void)
|
2011-09-04 22:30:31 -03:00
|
|
|
{
|
2012-01-03 14:32:16 -04:00
|
|
|
nav_throttle = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
|
|
|
// always start Circle mode at same angle
|
2011-11-13 01:39:00 -04:00
|
|
|
circle_angle = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
|
|
|
// We must be heading to a new WP, so XTrack must be 0
|
2011-11-13 01:39:00 -04:00
|
|
|
crosstrack_error = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
|
|
|
// Will be set by new command
|
2011-11-13 01:39:00 -04:00
|
|
|
target_bearing = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
|
|
|
// Will be set by new command
|
2011-11-13 01:39:00 -04:00
|
|
|
wp_distance = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
|
|
|
// Will be set by new command, used by loiter
|
2011-11-13 01:39:00 -04:00
|
|
|
long_error = 0;
|
|
|
|
lat_error = 0;
|
2012-01-09 20:57:57 -04:00
|
|
|
|
2012-02-19 17:15:40 -04:00
|
|
|
// We want to by default pass WPs
|
|
|
|
slow_wp = false;
|
2012-07-20 02:59:03 -03:00
|
|
|
|
|
|
|
// make sure we stick to Nav yaw on takeoff
|
|
|
|
auto_yaw = nav_yaw;
|
2012-08-10 14:02:32 -03:00
|
|
|
|
|
|
|
// revert to smaller radius set in params
|
|
|
|
waypoint_radius = g.waypoint_radius;
|
2011-09-04 22:30:31 -03:00
|
|
|
}
|
|
|
|
|
2012-01-20 14:07:38 -04:00
|
|
|
/*
|
|
|
|
reset all I integrators
|
|
|
|
*/
|
|
|
|
static void reset_I_all(void)
|
2012-01-03 14:32:16 -04:00
|
|
|
{
|
2012-01-20 14:07:38 -04:00
|
|
|
reset_rate_I();
|
|
|
|
reset_stability_I();
|
|
|
|
reset_wind_I();
|
|
|
|
reset_throttle_I();
|
|
|
|
reset_optflow_I();
|
|
|
|
|
|
|
|
// This is the only place we reset Yaw
|
|
|
|
g.pi_stabilize_yaw.reset_I();
|
|
|
|
}
|
2012-01-03 14:32:16 -04:00
|
|
|
|
2012-01-20 14:07:38 -04:00
|
|
|
static void reset_rate_I()
|
|
|
|
{
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_rate_roll.reset_I();
|
|
|
|
g.pid_rate_pitch.reset_I();
|
|
|
|
g.pid_rate_yaw.reset_I();
|
2012-01-20 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_optflow_I(void)
|
|
|
|
{
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_optflow_roll.reset_I();
|
|
|
|
g.pid_optflow_pitch.reset_I();
|
2012-01-25 08:55:14 -04:00
|
|
|
of_roll = 0;
|
|
|
|
of_pitch = 0;
|
2012-01-03 14:32:16 -04:00
|
|
|
}
|
|
|
|
|
2012-01-20 14:07:38 -04:00
|
|
|
static void reset_wind_I(void)
|
|
|
|
{
|
|
|
|
// Wind Compensation
|
2012-03-18 18:47:49 -03:00
|
|
|
// this i is not currently being used, but we reset it anyway
|
|
|
|
// because someone may modify it and not realize it, causing a bug
|
2012-01-20 14:07:38 -04:00
|
|
|
g.pi_loiter_lat.reset_I();
|
|
|
|
g.pi_loiter_lon.reset_I();
|
|
|
|
|
2012-03-18 18:47:49 -03:00
|
|
|
g.pid_loiter_rate_lat.reset_I();
|
|
|
|
g.pid_loiter_rate_lon.reset_I();
|
|
|
|
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_nav_lat.reset_I();
|
|
|
|
g.pid_nav_lon.reset_I();
|
2012-01-20 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_throttle_I(void)
|
|
|
|
{
|
|
|
|
// For Altitude Hold
|
|
|
|
g.pi_alt_hold.reset_I();
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_throttle.reset_I();
|
2012-01-20 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_stability_I(void)
|
|
|
|
{
|
|
|
|
// Used to balance a quad
|
|
|
|
// This only needs to be reset during Auto-leveling in flight
|
|
|
|
g.pi_stabilize_roll.reset_I();
|
|
|
|
g.pi_stabilize_pitch.reset_I();
|
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
/*************************************************************
|
|
|
|
throttle control
|
|
|
|
****************************************************************/
|
|
|
|
|
2012-07-19 21:41:52 -03:00
|
|
|
/* Depricated
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static long
|
2012-07-19 21:41:52 -03:00
|
|
|
//get_nav_yaw_offset(int yaw_input, int reset)
|
2011-02-19 22:03:01 -04:00
|
|
|
{
|
2011-11-07 18:24:32 -04:00
|
|
|
int32_t _yaw;
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-07-16 19:12:52 -03:00
|
|
|
if(reset == 0){
|
2011-07-10 21:47:08 -03:00
|
|
|
// we are on the ground
|
2012-03-11 05:36:12 -03:00
|
|
|
return ahrs.yaw_sensor;
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2012-06-13 09:50:16 -03:00
|
|
|
// re-define nav_yaw if we have stick input
|
|
|
|
if(yaw_input != 0){
|
|
|
|
// set nav_yaw + or - the current location
|
|
|
|
_yaw = yaw_input + ahrs.yaw_sensor;
|
|
|
|
// we need to wrap our value so we can be 0 to 360 (*100)
|
|
|
|
return wrap_360(_yaw);
|
|
|
|
}else{
|
|
|
|
// no stick input, lets not change nav_yaw
|
|
|
|
return nav_yaw;
|
2011-07-10 21:47:08 -03:00
|
|
|
}
|
2012-06-13 09:50:16 -03:00
|
|
|
}
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
2012-07-19 21:41:52 -03:00
|
|
|
*/
|
2011-09-04 21:15:36 -03:00
|
|
|
|
2012-02-28 08:26:37 -04:00
|
|
|
static int16_t get_angle_boost(int16_t value)
|
2011-07-10 21:47:08 -03:00
|
|
|
{
|
2012-06-05 20:41:31 -03:00
|
|
|
float temp = cos_pitch_x * cos_roll_x;
|
2012-08-18 03:03:59 -03:00
|
|
|
temp = constrain(temp, .75, 1.0);
|
2012-06-20 00:01:05 -03:00
|
|
|
return ((float)(g.throttle_cruise + 80) / temp) - (g.throttle_cruise + 80);
|
2011-11-02 01:18:47 -03:00
|
|
|
}
|
2011-10-02 15:36:23 -03:00
|
|
|
|
2012-04-04 10:51:39 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
// heli_angle_boost - adds a boost depending on roll/pitch values
|
|
|
|
// equivalent of quad's angle_boost function
|
|
|
|
// throttle value should be 0 ~ 1000
|
|
|
|
static int16_t heli_get_angle_boost(int16_t throttle)
|
|
|
|
{
|
|
|
|
float angle_boost_factor = cos_pitch_x * cos_roll_x;
|
|
|
|
angle_boost_factor = 1.0 - constrain(angle_boost_factor, .5, 1.0);
|
2012-08-16 08:04:46 -03:00
|
|
|
int16_t throttle_above_mid = max(throttle - motors.throttle_mid,0);
|
2012-04-04 10:51:39 -03:00
|
|
|
return throttle + throttle_above_mid*angle_boost_factor;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // HELI_FRAME
|
|
|
|
|
2011-12-23 18:22:11 -04:00
|
|
|
#define NUM_G_SAMPLES 40
|
|
|
|
|
|
|
|
#if ACCEL_ALT_HOLD == 2
|
|
|
|
// z -14.4306 = going up
|
|
|
|
// z -6.4306 = going down
|
2012-08-16 08:04:46 -03:00
|
|
|
static int16_t get_z_damping()
|
2011-12-23 18:22:11 -04:00
|
|
|
{
|
2012-08-16 08:04:46 -03:00
|
|
|
int16_t output;
|
2011-12-23 18:22:11 -04:00
|
|
|
|
|
|
|
Z_integrator += get_world_Z_accel() - Z_offset;
|
|
|
|
output = Z_integrator * 3;
|
|
|
|
Z_integrator = Z_integrator * .8;
|
|
|
|
output = constrain(output, -100, 100);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float get_world_Z_accel()
|
|
|
|
{
|
2012-03-11 05:36:12 -03:00
|
|
|
accels_rot = ahrs.get_dcm_matrix() * imu.get_accel();
|
2011-12-23 18:22:11 -04:00
|
|
|
//Serial.printf("z %1.4f\n", accels_rot.z);
|
|
|
|
return accels_rot.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_z_damper()
|
|
|
|
{
|
|
|
|
Z_offset = 0;
|
2012-08-16 08:04:46 -03:00
|
|
|
for (int16_t i = 0; i < NUM_G_SAMPLES; i++){
|
2011-12-23 18:22:11 -04:00
|
|
|
delay(5);
|
|
|
|
read_AHRS();
|
|
|
|
Z_offset += get_world_Z_accel();
|
|
|
|
}
|
|
|
|
Z_offset /= (float)NUM_G_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
// Accelerometer Z dampening by Aurelio R. Ramos
|
|
|
|
// ---------------------------------------------
|
2011-12-23 18:22:11 -04:00
|
|
|
#elif ACCEL_ALT_HOLD == 1
|
2011-10-02 15:36:23 -03:00
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
// contains G and any other DC offset
|
|
|
|
static float estimatedAccelOffset = 0;
|
2011-10-02 15:36:23 -03:00
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
// state
|
|
|
|
static float synVelo = 0;
|
|
|
|
static float synPos = 0;
|
|
|
|
static float synPosFiltered = 0;
|
|
|
|
static float posError = 0;
|
|
|
|
static float prevSensedPos = 0;
|
2011-10-02 15:36:23 -03:00
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
// tuning for dead reckoning
|
2011-11-07 02:45:07 -04:00
|
|
|
static const float dt_50hz = 0.02;
|
|
|
|
static float synPosP = 10 * dt_50hz;
|
|
|
|
static float synPosI = 15 * dt_50hz;
|
|
|
|
static float synVeloP = 1.5 * dt_50hz;
|
|
|
|
static float maxVeloCorrection = 5 * dt_50hz;
|
2011-11-02 01:18:47 -03:00
|
|
|
static float maxSensedVelo = 1;
|
2011-11-07 02:45:07 -04:00
|
|
|
static float synPosFilter = 0.5;
|
2011-11-02 01:18:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
// Z damping term.
|
2011-11-07 02:45:07 -04:00
|
|
|
static float fullDampP = 0.100;
|
2011-11-02 01:18:47 -03:00
|
|
|
|
|
|
|
float get_world_Z_accel()
|
|
|
|
{
|
2012-03-11 05:36:12 -03:00
|
|
|
accels_rot = ahrs.get_dcm_matrix() * imu.get_accel();
|
2011-11-02 01:18:47 -03:00
|
|
|
return accels_rot.z;
|
2011-09-04 21:15:36 -03:00
|
|
|
}
|
2011-07-10 21:47:08 -03:00
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
static void init_z_damper()
|
2011-02-19 22:03:01 -04:00
|
|
|
{
|
2011-11-02 01:18:47 -03:00
|
|
|
estimatedAccelOffset = 0;
|
2012-08-16 08:04:46 -03:00
|
|
|
for (int16_t i = 0; i < NUM_G_SAMPLES; i++){
|
2011-12-23 18:22:11 -04:00
|
|
|
delay(5);
|
|
|
|
read_AHRS();
|
2011-11-02 01:18:47 -03:00
|
|
|
estimatedAccelOffset += get_world_Z_accel();
|
|
|
|
}
|
|
|
|
estimatedAccelOffset /= (float)NUM_G_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
float dead_reckon_Z(float sensedPos, float sensedAccel)
|
|
|
|
{
|
|
|
|
// the following algorithm synthesizes position and velocity from
|
|
|
|
// a noisy altitude and accelerometer data.
|
|
|
|
|
|
|
|
// synthesize uncorrected velocity by integrating acceleration
|
2011-11-07 02:45:07 -04:00
|
|
|
synVelo += (sensedAccel - estimatedAccelOffset) * dt_50hz;
|
2011-11-02 01:18:47 -03:00
|
|
|
|
|
|
|
// synthesize uncorrected position by integrating uncorrected velocity
|
2011-11-07 02:45:07 -04:00
|
|
|
synPos += synVelo * dt_50hz;
|
2011-11-02 01:18:47 -03:00
|
|
|
|
|
|
|
// filter synPos, the better this filter matches the filtering and dead time
|
|
|
|
// of the sensed position, the less the position estimate will lag.
|
|
|
|
synPosFiltered = synPosFiltered * (1 - synPosFilter) + synPos * synPosFilter;
|
|
|
|
|
|
|
|
// calculate error against sensor position
|
|
|
|
posError = sensedPos - synPosFiltered;
|
|
|
|
|
|
|
|
// correct altitude
|
|
|
|
synPos += synPosP * posError;
|
|
|
|
|
|
|
|
// correct integrated velocity by posError
|
|
|
|
synVelo = synVelo + constrain(posError, -maxVeloCorrection, maxVeloCorrection) * synPosI;
|
|
|
|
|
|
|
|
// correct integrated velocity by the sensed position delta in a small proportion
|
|
|
|
// (i.e., the low frequency of the delta)
|
2011-11-07 02:45:07 -04:00
|
|
|
float sensedVelo = (sensedPos - prevSensedPos) / dt_50hz;
|
2011-11-02 01:18:47 -03:00
|
|
|
synVelo += constrain(sensedVelo - synVelo, -maxSensedVelo, maxSensedVelo) * synVeloP;
|
|
|
|
|
|
|
|
prevSensedPos = sensedPos;
|
|
|
|
return synVelo;
|
|
|
|
}
|
|
|
|
|
2012-08-16 08:04:46 -03:00
|
|
|
static int16_t get_z_damping()
|
2011-11-02 01:18:47 -03:00
|
|
|
{
|
|
|
|
float sensedAccel = get_world_Z_accel();
|
|
|
|
float sensedPos = current_loc.alt / 100.0;
|
|
|
|
|
|
|
|
float synVelo = dead_reckon_Z(sensedPos, sensedAccel);
|
|
|
|
return constrain(fullDampP * synVelo * (-1), -300, 300);
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
|
|
|
|
2011-11-02 01:18:47 -03:00
|
|
|
#else
|
|
|
|
|
2012-08-16 08:04:46 -03:00
|
|
|
static int16_t get_z_damping()
|
2011-11-02 01:18:47 -03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-23 18:22:11 -04:00
|
|
|
static void init_z_damper()
|
|
|
|
{
|
|
|
|
}
|
2012-01-09 00:53:54 -04:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
|
|
|
#ifdef OPTFLOW_ENABLED
|
2012-01-25 08:55:14 -04: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-01-25 08:55:14 -04:00
|
|
|
int32_t new_roll = 0;
|
2012-04-21 08:17:12 -03:00
|
|
|
int32_t p,i,d;
|
2012-01-09 00:53:54 -04:00
|
|
|
|
|
|
|
// check if new optflow data available
|
|
|
|
if( optflow.last_update != last_of_roll_update) {
|
|
|
|
last_of_roll_update = optflow.last_update;
|
|
|
|
|
2012-01-25 08:55:14 -04:00
|
|
|
// add new distance moved
|
|
|
|
tot_x_cm += optflow.x_cm;
|
2012-01-09 00:53:54 -04:00
|
|
|
|
|
|
|
// only stop roll if caller isn't modifying roll
|
2012-08-17 23:48:12 -03:00
|
|
|
if( input_roll == 0 && current_loc.alt < 1500) {
|
2012-04-21 08:17:12 -03:00
|
|
|
p = g.pid_optflow_roll.get_p(-tot_x_cm);
|
|
|
|
i = g.pid_optflow_roll.get_i(-tot_x_cm,1.0); // we could use the last update time to calculate the time change
|
|
|
|
d = g.pid_optflow_roll.get_d(-tot_x_cm,1.0);
|
|
|
|
new_roll = p+i+d;
|
2012-01-09 00:53:54 -04:00
|
|
|
}else{
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_optflow_roll.reset_I();
|
2012-01-25 08:55:14 -04:00
|
|
|
tot_x_cm = 0;
|
2012-04-21 08:17:12 -03:00
|
|
|
p = 0; // for logging
|
|
|
|
i = 0;
|
|
|
|
d = 0;
|
2012-01-09 00:53:54 -04:00
|
|
|
}
|
2012-01-25 08:55:14 -04:00
|
|
|
// limit amount of change and maximum angle
|
|
|
|
of_roll = constrain(new_roll, (of_roll-20), (of_roll+20));
|
2012-04-21 08:17:12 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-04-21 08:17:12 -03:00
|
|
|
// 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) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 5 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
|
|
|
Log_Write_PID(CH6_OPTFLOW_KP, tot_x_cm, p, i, d, of_roll, tuning_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // LOGGING_ENABLED == ENABLED
|
2012-01-09 00:53:54 -04:00
|
|
|
}
|
|
|
|
|
2012-01-25 08:55:14 -04:00
|
|
|
// limit max angle
|
|
|
|
of_roll = constrain(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-01-09 00:53:54 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
#ifdef 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-01-25 08:55:14 -04:00
|
|
|
int32_t new_pitch = 0;
|
2012-04-21 08:17:12 -03:00
|
|
|
int32_t p,i,d;
|
2012-01-09 00:53:54 -04:00
|
|
|
|
|
|
|
// check if new optflow data available
|
|
|
|
if( optflow.last_update != last_of_pitch_update ) {
|
|
|
|
last_of_pitch_update = optflow.last_update;
|
|
|
|
|
2012-01-25 08:55:14 -04:00
|
|
|
// add new distance moved
|
|
|
|
tot_y_cm += optflow.y_cm;
|
2012-01-09 00:53:54 -04:00
|
|
|
|
2012-01-25 08:55:14 -04:00
|
|
|
// only stop roll if caller isn't modifying pitch
|
2012-08-17 23:48:12 -03:00
|
|
|
if( input_pitch == 0 && current_loc.alt < 1500 ) {
|
2012-04-21 08:17:12 -03:00
|
|
|
p = g.pid_optflow_pitch.get_p(tot_y_cm);
|
|
|
|
i = g.pid_optflow_pitch.get_i(tot_y_cm, 1.0); // we could use the last update time to calculate the time change
|
|
|
|
d = g.pid_optflow_pitch.get_d(tot_y_cm, 1.0);
|
|
|
|
new_pitch = p + i + d;
|
2012-01-09 00:53:54 -04:00
|
|
|
}else{
|
2012-01-25 08:55:14 -04:00
|
|
|
tot_y_cm = 0;
|
2012-01-29 02:00:05 -04:00
|
|
|
g.pid_optflow_pitch.reset_I();
|
2012-04-21 08:17:12 -03:00
|
|
|
p = 0; // for logging
|
|
|
|
i = 0;
|
|
|
|
d = 0;
|
2012-01-09 00:53:54 -04:00
|
|
|
}
|
2012-01-25 08:55:14 -04:00
|
|
|
|
|
|
|
// limit amount of change
|
|
|
|
of_pitch = constrain(new_pitch, (of_pitch-20), (of_pitch+20));
|
2012-04-21 08:17:12 -03:00
|
|
|
|
|
|
|
#if LOGGING_ENABLED == ENABLED
|
2012-06-02 12:25:16 -03:00
|
|
|
static int8_t log_counter = 0; // used to slow down logging of PID values to dataflash
|
2012-04-21 08:17:12 -03:00
|
|
|
// 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) ) {
|
|
|
|
log_counter++;
|
|
|
|
if( log_counter >= 5 ) { // (update rate / desired output rate) = (100hz / 10hz) = 10
|
|
|
|
log_counter = 0;
|
|
|
|
Log_Write_PID(CH6_OPTFLOW_KP+100, tot_y_cm, p, i, d, of_pitch, tuning_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // LOGGING_ENABLED == ENABLED
|
2012-01-09 00:53:54 -04:00
|
|
|
}
|
2012-01-25 08:55:14 -04:00
|
|
|
|
|
|
|
// limit max angle
|
2012-01-09 00:53:54 -04:00
|
|
|
of_pitch = constrain(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-01-09 00:53:54 -04:00
|
|
|
#endif
|
|
|
|
}
|