2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
init_pids()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-01-02 16:34:42 -04:00
|
|
|
// create limits to how much dampening we'll allow
|
|
|
|
// this creates symmetry with the P gain value preventing oscillations
|
2011-02-17 05:36:33 -04:00
|
|
|
|
|
|
|
max_stabilize_dampener = g.pid_stabilize_roll.kP() * 2500; // = 0.6 * 2500 = 1500 or 15°
|
2011-02-17 03:09:13 -04:00
|
|
|
max_yaw_dampener = g.pid_yaw.kP() * 6000; // = .5 * 6000 = 3000
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
control_nav_mixer()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
// control +- 45° is mixed with the navigation request by the Autopilot
|
|
|
|
// output is in degrees = target pitch and roll of copter
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_1.servo_out = g.rc_1.control_mix(nav_roll);
|
|
|
|
g.rc_2.servo_out = g.rc_2.control_mix(nav_pitch);
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
fbw_nav_mixer()
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
|
|
|
// control +- 45° is mixed with the navigation request by the Autopilot
|
|
|
|
// output is in degrees = target pitch and roll of copter
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_1.servo_out = nav_roll;
|
|
|
|
g.rc_2.servo_out = nav_pitch;
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_stabilize_roll()
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
|
|
|
float error, rate;
|
|
|
|
int dampener;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
|
|
|
error = g.rc_1.servo_out - dcm.roll_sensor;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// limit the error we're feeding to the PID
|
2011-01-25 01:53:36 -04:00
|
|
|
error = constrain(error, -2500, 2500);
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// write out angles back to servo out - this will be converted to PWM by RC_Channel
|
2011-02-17 05:36:33 -04:00
|
|
|
g.rc_1.servo_out = g.pid_stabilize_roll.get_pid(error, delta_ms_fast_loop, 1.0);
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// We adjust the output by the rate of rotation:
|
|
|
|
// Rate control through bias corrected gyro rates
|
|
|
|
// omega is the raw gyro reading
|
2011-01-02 16:34:42 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Limit dampening to be equal to propotional term for symmetry
|
2011-02-17 03:09:13 -04:00
|
|
|
rate = degrees(omega.x) * 100.0; // 6rad = 34377
|
|
|
|
dampener = (rate * g.stabilize_dampener); // 34377 * .175 = 6000
|
|
|
|
g.rc_1.servo_out -= constrain(dampener, -max_stabilize_dampener, max_stabilize_dampener); // limit to 1500 based on kP
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_stabilize_pitch()
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
|
|
|
float error, rate;
|
|
|
|
int dampener;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
error = g.rc_2.servo_out - dcm.pitch_sensor;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// limit the error we're feeding to the PID
|
|
|
|
error = constrain(error, -2500, 2500);
|
2011-01-02 16:34:42 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// write out angles back to servo out - this will be converted to PWM by RC_Channel
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_2.servo_out = g.pid_stabilize_pitch.get_pid(error, delta_ms_fast_loop, 1.0);
|
2011-01-25 01:53:36 -04:00
|
|
|
|
|
|
|
// We adjust the output by the rate of rotation:
|
|
|
|
// Rate control through bias corrected gyro rates
|
|
|
|
// omega is the raw gyro reading
|
|
|
|
|
|
|
|
// Limit dampening to be equal to propotional term for symmetry
|
2011-02-17 03:09:13 -04:00
|
|
|
rate = degrees(omega.y) * 100.0; // 6rad = 34377
|
|
|
|
dampener = (rate * g.stabilize_dampener); // 34377 * .175 = 6000
|
|
|
|
g.rc_2.servo_out -= constrain(dampener, -max_stabilize_dampener, max_stabilize_dampener); // limit to 1500 based on kP
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
void
|
|
|
|
clear_yaw_control()
|
|
|
|
{
|
|
|
|
//Serial.print("Clear ");
|
2011-01-25 01:53:36 -04:00
|
|
|
rate_yaw_flag = false; // exit rate_yaw_flag
|
|
|
|
nav_yaw = dcm.yaw_sensor; // save our Yaw
|
2011-01-02 16:34:42 -04:00
|
|
|
yaw_error = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_yaw_with_hold(boolean hold)
|
2011-01-02 16:34:42 -04:00
|
|
|
{
|
|
|
|
if(hold){
|
2011-01-25 01:53:36 -04:00
|
|
|
// look to see if we have exited rate control properly - ie stopped turning
|
2011-01-02 16:34:42 -04:00
|
|
|
if(rate_yaw_flag){
|
|
|
|
// we are still in motion from rate control
|
2011-02-21 16:58:10 -04:00
|
|
|
if(fabs(omega.y) < .08){
|
2011-01-02 16:34:42 -04:00
|
|
|
clear_yaw_control();
|
|
|
|
hold = true; // just to be explicit
|
|
|
|
}else{
|
|
|
|
// return to rate control until we slow down.
|
|
|
|
hold = false;
|
|
|
|
}
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-04 02:06:26 -04:00
|
|
|
}else{
|
2011-01-02 16:34:42 -04:00
|
|
|
// rate control
|
2011-02-17 05:36:33 -04:00
|
|
|
|
|
|
|
// this indicates we are under rate control, when we enter Yaw Hold and
|
|
|
|
// return to 0° per second, we exit rate control and hold the current Yaw
|
2011-01-02 16:34:42 -04:00
|
|
|
rate_yaw_flag = true;
|
|
|
|
yaw_error = 0;
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
if(hold){
|
2011-01-09 22:28:59 -04:00
|
|
|
// try and hold the current nav_yaw setting
|
2011-01-25 01:53:36 -04:00
|
|
|
yaw_error = nav_yaw - dcm.yaw_sensor; // +- 60°
|
2011-01-02 16:34:42 -04:00
|
|
|
yaw_error = wrap_180(yaw_error);
|
|
|
|
|
|
|
|
// limit the error we're feeding to the PID
|
|
|
|
yaw_error = constrain(yaw_error, -6000, 6000); // limit error to 60 degees
|
2011-01-04 02:06:26 -04:00
|
|
|
|
|
|
|
// Apply PID and save the new angle back to RC_Channel
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_4.servo_out = g.pid_yaw.get_pid(yaw_error, delta_ms_fast_loop, 1.0); // .5 * 6000 = 3000
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
// We adjust the output by the rate of rotation
|
2011-01-09 22:28:59 -04:00
|
|
|
long rate = degrees(omega.z) * 100.0; // 3rad = 17188 , 6rad = 34377
|
2011-02-17 03:09:13 -04:00
|
|
|
int dampener = ((float)rate * g.hold_yaw_dampener); // 18000 * .17 = 3000
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
// Limit dampening to be equal to propotional term for symmetry
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_4.servo_out -= constrain(dampener, -max_yaw_dampener, max_yaw_dampener); // -3000
|
2011-02-17 05:36:33 -04:00
|
|
|
|
|
|
|
}else{
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2011-01-09 22:28:59 -04:00
|
|
|
// rate control
|
2011-02-21 16:58:10 -04:00
|
|
|
long rate = degrees(omega.z) * 100; // 3rad = 17188 , 6rad = 34377
|
|
|
|
rate = constrain(rate, -36000, 36000); // limit to something fun!
|
2011-02-25 01:33:39 -04:00
|
|
|
|
|
|
|
if(abs(rate) < 1000 ) //experiment to limit yaw reversing
|
|
|
|
rate = 0;
|
|
|
|
|
2011-02-21 16:58:10 -04:00
|
|
|
long error = ((long)g.rc_4.control_in * 6) - rate; // control is += 6000 * 6 = 36000
|
|
|
|
// -error = CCW, +error = CW
|
|
|
|
g.rc_4.servo_out = g.pid_acro_rate_yaw.get_pid(error, delta_ms_fast_loop, 1.0); // kP .07 * 36000 = 2520
|
|
|
|
g.rc_4.servo_out = constrain(g.rc_4.servo_out, -2400, 2400); // limit to 24°
|
2011-01-02 16:34:42 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-25 01:33:39 -04:00
|
|
|
// slight left rudder give right roll.
|
2011-01-02 16:34:42 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_rate_roll()
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
|
|
|
// rate control
|
2011-02-21 16:58:10 -04:00
|
|
|
long rate = degrees(omega.x) * 100; // 3rad = 17188 , 6rad = 34377
|
|
|
|
rate = constrain(rate, -36000, 36000); // limit to something fun!
|
|
|
|
long error = ((long)g.rc_1.control_in * 8) - rate; // control is += 4500 * 8 = 36000
|
2011-01-25 01:53:36 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_1.servo_out = g.pid_acro_rate_roll.get_pid(error, delta_ms_fast_loop, 1.0); // .075 * 36000 = 2700
|
2011-02-21 16:58:10 -04:00
|
|
|
g.rc_1.servo_out = constrain(g.rc_1.servo_out, -2400, 2400); // limit to 2400
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_rate_pitch()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-01-25 01:53:36 -04:00
|
|
|
// rate control
|
|
|
|
long rate = degrees(omega.y) * 100; // 3rad = 17188 , 6rad = 34377
|
|
|
|
rate = constrain(rate, -36000, 36000); // limit to something fun!
|
2011-02-17 03:09:13 -04:00
|
|
|
long error = ((long)g.rc_2.control_in * 8) - rate; // control is += 4500 * 8 = 36000
|
2011-01-25 01:53:36 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_2.servo_out = g.pid_acro_rate_pitch.get_pid(error, delta_ms_fast_loop, 1.0); // .075 * 36000 = 2700
|
|
|
|
g.rc_2.servo_out = constrain(g.rc_2.servo_out, -2400, 2400); // limit to 2400
|
2011-01-25 01:53:36 -04:00
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Zeros out navigation Integrators if we are changing mode, have passed a waypoint, etc.
|
|
|
|
// Keeps outdated data out of our calculations
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
reset_I(void)
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-02-17 03:09:13 -04:00
|
|
|
g.pid_nav_lat.reset_I();
|
|
|
|
g.pid_nav_lon.reset_I();
|
|
|
|
g.pid_baro_throttle.reset_I();
|
|
|
|
g.pid_sonar_throttle.reset_I();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
throttle control
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// user input:
|
|
|
|
// -----------
|
|
|
|
void output_manual_throttle()
|
|
|
|
{
|
|
|
|
g.rc_3.servo_out = (float)g.rc_3.control_in * angle_boost();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Autopilot
|
|
|
|
// ---------
|
|
|
|
void output_auto_throttle()
|
|
|
|
{
|
|
|
|
g.rc_3.servo_out = (float)nav_throttle * angle_boost();
|
|
|
|
// make sure we never send a 0 throttle that will cut the motors
|
|
|
|
g.rc_3.servo_out = max(g.rc_3.servo_out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void calc_nav_throttle()
|
|
|
|
{
|
|
|
|
// limit error
|
|
|
|
long error = constrain(altitude_error, -400, 400);
|
2011-02-25 01:33:39 -04:00
|
|
|
float scaler = 1.0;
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
if(error < 0){
|
|
|
|
scaler = (altitude_sensor == BARO) ? .5 : .9;
|
|
|
|
}
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
if(altitude_sensor == BARO){
|
|
|
|
nav_throttle = g.pid_baro_throttle.get_pid(error, delta_ms_fast_loop, scaler);
|
2011-02-19 22:03:01 -04:00
|
|
|
nav_throttle = g.throttle_cruise + constrain(nav_throttle, -30, 80);
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-25 01:33:39 -04:00
|
|
|
nav_throttle = g.pid_sonar_throttle.get_pid(error, delta_ms_fast_loop, scaler);
|
2011-02-19 22:03:01 -04:00
|
|
|
nav_throttle = g.throttle_cruise + constrain(nav_throttle, -60, 100);
|
|
|
|
}
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
nav_throttle = (nav_throttle + nav_throttle_old) >> 1;
|
|
|
|
nav_throttle_old = nav_throttle;
|
2011-02-25 01:33:39 -04:00
|
|
|
|
|
|
|
//Serial.printf("nav_thr %d, scaler %2.2f ", nav_throttle, scaler);
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float angle_boost()
|
|
|
|
{
|
|
|
|
float temp = cos_pitch_x * cos_roll_x;
|
|
|
|
temp = 2.0 - constrain(temp, .7, 1.0);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
yaw control
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
void output_manual_yaw()
|
|
|
|
{
|
|
|
|
if(g.rc_3.control_in == 0){
|
|
|
|
clear_yaw_control();
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-19 22:03:01 -04:00
|
|
|
// Yaw control
|
|
|
|
if(g.rc_4.control_in == 0){
|
|
|
|
//clear_yaw_control();
|
|
|
|
output_yaw_with_hold(true); // hold yaw
|
|
|
|
}else{
|
|
|
|
output_yaw_with_hold(false); // rate control yaw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void auto_yaw()
|
|
|
|
{
|
|
|
|
output_yaw_with_hold(true); // hold yaw
|
|
|
|
}
|