2011-03-19 07:20:11 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-12-19 12:40:33 -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
|
2011-04-10 17:31:33 -03:00
|
|
|
simple_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-04-25 02:12:59 -03:00
|
|
|
void
|
|
|
|
limit_nav_pitch_roll(long pmax)
|
|
|
|
{
|
|
|
|
// limit the nav pitch and roll of the copter
|
|
|
|
//long pmax = g.pitch_max.get();
|
|
|
|
nav_roll = constrain(nav_roll, -pmax, pmax);
|
|
|
|
nav_pitch = constrain(nav_pitch, -pmax, pmax);
|
|
|
|
}
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
void
|
|
|
|
output_stabilize_roll()
|
2011-01-25 01:53:36 -04:00
|
|
|
{
|
2011-06-16 14:03:26 -03:00
|
|
|
float error;
|
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-04-10 17:31:33 -03: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-05-18 20:38:24 -03:00
|
|
|
g.rc_1.servo_out = g.pid_stabilize_roll.get_pi(error, delta_ms_fast_loop, 1.0); // 2500 * .7 = 1750
|
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-05-18 20:38:24 -03:00
|
|
|
g.rc_1.servo_out -= degrees(omega.x) * 100.0 * g.pid_stabilize_roll.kD();
|
2011-05-13 16:29:45 -03:00
|
|
|
g.rc_1.servo_out = min(g.rc_1.servo_out, 2500);
|
|
|
|
g.rc_1.servo_out = max(g.rc_1.servo_out, -2500);
|
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
|
|
|
{
|
2011-06-16 14:03:26 -03:00
|
|
|
float error;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-10 17:31:33 -03: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
|
2011-04-10 17:31:33 -03:00
|
|
|
error = constrain(error, -2500, 2500);
|
|
|
|
|
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-05-18 20:38:24 -03:00
|
|
|
g.rc_2.servo_out = g.pid_stabilize_pitch.get_pi(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
|
2011-05-18 20:38:24 -03:00
|
|
|
g.rc_2.servo_out -= degrees(omega.y) * 100.0 * g.pid_stabilize_pitch.kD();
|
2011-05-13 16:29:45 -03:00
|
|
|
g.rc_2.servo_out = min(g.rc_2.servo_out, 2500);
|
|
|
|
g.rc_2.servo_out = max(g.rc_2.servo_out, -2500);
|
2010-12-19 12:40:33 -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
|
2011-04-08 16:13:31 -03:00
|
|
|
long rate = degrees(omega.y) * 100; // 3rad = 17188 , 6rad = 34377
|
|
|
|
rate = constrain(rate, -36000, 36000); // limit to something fun!
|
|
|
|
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
|
2011-04-08 16:13:31 -03:00
|
|
|
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-04-25 02:12:59 -03:00
|
|
|
// I removed these, they don't seem to be needed.
|
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();
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_3.servo_out = max(g.rc_3.servo_out, 0);
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2011-06-16 14:03:26 -03:00
|
|
|
{
|
|
|
|
// limit error
|
|
|
|
nav_throttle = g.pid_baro_throttle.get_pid(altitude_error, delta_ms_medium_loop, 1.0);
|
|
|
|
nav_throttle = g.throttle_cruise + constrain(nav_throttle, -60, 60);
|
|
|
|
|
|
|
|
// simple filtering
|
|
|
|
if(nav_throttle_old == 0)
|
|
|
|
nav_throttle_old = nav_throttle;
|
|
|
|
|
|
|
|
nav_throttle = (nav_throttle + nav_throttle_old) >> 1;
|
|
|
|
nav_throttle_old = nav_throttle;
|
|
|
|
|
|
|
|
// clear the new data flag
|
|
|
|
invalid_throttle = false;
|
|
|
|
|
|
|
|
//Serial.printf("nav_thr %d, scaler %2.2f ", nav_throttle, scaler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void calc_nav_throttle2()
|
2011-02-19 22:03:01 -04:00
|
|
|
{
|
|
|
|
// 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){
|
2011-04-21 20:07:31 -03:00
|
|
|
// try and prevent rapid fall
|
2011-05-29 16:32:55 -03:00
|
|
|
scaler = (altitude_sensor == BARO) ? .8 : .8;
|
2011-02-25 01:33:39 -04:00
|
|
|
}
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
if(altitude_sensor == BARO){
|
2011-05-27 20:35:08 -03:00
|
|
|
nav_throttle = g.pid_baro_throttle.get_pid(error, delta_ms_medium_loop, scaler); // .2
|
2011-05-29 16:32:55 -03:00
|
|
|
nav_throttle = g.throttle_cruise + constrain(nav_throttle, -30, 80);
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-05-27 20:35:08 -03:00
|
|
|
nav_throttle = g.pid_sonar_throttle.get_pid(error, delta_ms_medium_loop, scaler); // .5
|
2011-05-29 16:32:55 -03:00
|
|
|
nav_throttle = g.throttle_cruise + constrain(nav_throttle, -40, 100);
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
2011-02-21 00:30:56 -04:00
|
|
|
|
2011-05-21 13:24:07 -03:00
|
|
|
// simple filtering
|
2011-05-31 02:29:06 -03:00
|
|
|
if(nav_throttle_old == 0)
|
|
|
|
nav_throttle_old = nav_throttle;
|
|
|
|
|
2011-05-21 13:24:07 -03:00
|
|
|
nav_throttle = (nav_throttle + nav_throttle_old) >> 1;
|
|
|
|
nav_throttle_old = nav_throttle;
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2011-05-21 13:24:07 -03:00
|
|
|
// clear the new data flag
|
2011-04-25 02:12:59 -03:00
|
|
|
invalid_throttle = false;
|
|
|
|
|
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;
|
2011-04-21 20:07:31 -03:00
|
|
|
temp = 2.0 - constrain(temp, .5, 1.0);
|
2011-02-19 22:03:01 -04:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
yaw control
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
void output_manual_yaw()
|
|
|
|
{
|
2011-05-31 02:29:06 -03:00
|
|
|
// Yaw control
|
|
|
|
if(g.rc_4.control_in == 0){
|
|
|
|
output_yaw_with_hold(true); // hold yaw
|
|
|
|
}else{
|
|
|
|
output_yaw_with_hold(false); // rate control yaw
|
2011-02-19 22:03:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void auto_yaw()
|
|
|
|
{
|
|
|
|
output_yaw_with_hold(true); // hold yaw
|
|
|
|
}
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
void
|
|
|
|
clear_yaw_control()
|
|
|
|
{
|
|
|
|
//Serial.print("Clear ");
|
|
|
|
rate_yaw_flag = false; // exit rate_yaw_flag
|
|
|
|
nav_yaw = dcm.yaw_sensor; // save our Yaw
|
|
|
|
g.rc_4.servo_out = 0; // reset our output. It can stick when we are at 0 throttle
|
|
|
|
yaw_error = 0;
|
|
|
|
yaw_debug = YAW_HOLD; //0
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:14:18 -03:00
|
|
|
#if YAW_OPTION == 0
|
2011-04-25 02:12:59 -03:00
|
|
|
void
|
|
|
|
output_yaw_with_hold(boolean hold)
|
|
|
|
{
|
|
|
|
// rate control
|
|
|
|
long rate = degrees(omega.z) * 100; // 3rad = 17188 , 6rad = 34377
|
|
|
|
rate = constrain(rate, -36000, 36000); // limit to something fun!
|
|
|
|
|
|
|
|
if(hold){
|
|
|
|
// look to see if we have exited rate control properly - ie stopped turning
|
|
|
|
if(rate_yaw_flag){
|
|
|
|
// we are still in motion from rate control
|
2011-06-16 14:03:26 -03:00
|
|
|
if((fabs(omega.z) < .25) || (brake_timer < 2)){
|
2011-04-25 02:12:59 -03:00
|
|
|
clear_yaw_control();
|
|
|
|
hold = true; // just to be explicit
|
|
|
|
}else{
|
|
|
|
hold = false; // return to rate control until we slow down.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// rate control
|
|
|
|
|
|
|
|
// 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
|
|
|
|
rate_yaw_flag = true;
|
|
|
|
yaw_error = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hold){
|
2011-06-16 14:03:26 -03:00
|
|
|
brake_timer = 0;
|
2011-04-25 02:12:59 -03:00
|
|
|
// try and hold the current nav_yaw setting
|
|
|
|
yaw_error = nav_yaw - dcm.yaw_sensor; // +- 60°
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
// we need to wrap our value so we can be -180 to 180 (*100)
|
2011-04-25 02:12:59 -03:00
|
|
|
yaw_error = wrap_180(yaw_error);
|
|
|
|
|
|
|
|
// limit the error we're feeding to the PID
|
2011-06-16 14:03:26 -03:00
|
|
|
yaw_error = constrain(yaw_error, -9000, 9000); // limit error to 40 degees
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// Apply PID and save the new angle back to RC_Channel
|
2011-05-26 02:58:17 -03:00
|
|
|
g.rc_4.servo_out = g.pid_yaw.get_pi(yaw_error, delta_ms_fast_loop, 1.0); // .4 * 4000 = 1600
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
// add in yaw dampener
|
2011-05-29 01:02:01 -03:00
|
|
|
g.rc_4.servo_out -= rate * g.pid_yaw.kD();
|
2011-05-28 03:52:56 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
yaw_debug = YAW_HOLD; //0
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
if(g.rc_4.control_in == 0){
|
2011-06-16 14:03:26 -03:00
|
|
|
brake_timer--;
|
2011-04-25 02:12:59 -03:00
|
|
|
// adaptive braking
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_4.servo_out = (int)(-1200.0 * omega.z);
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
yaw_debug = YAW_BRAKE; // 1
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// RATE control
|
2011-06-16 14:03:26 -03:00
|
|
|
brake_timer = 100;
|
2011-04-25 02:12:59 -03:00
|
|
|
yaw_debug = YAW_RATE; // 2
|
2011-06-16 14:03:26 -03:00
|
|
|
long error = ((long)g.rc_4.control_in * 6) - (degrees(omega.z) * 100); // control is += 4500 * 6 = 36000
|
|
|
|
g.rc_4.servo_out = g.pid_acro_rate_yaw.get_pid(error, delta_ms_fast_loop, 1.0); // kP .07 * 36000 = 2520
|
2011-04-25 02:12:59 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit Output
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_4.servo_out = constrain(g.rc_4.servo_out, -3200, 3200); // limit to 32°
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
//Serial.printf("%d\n",g.rc_4.servo_out);
|
|
|
|
}
|
2011-05-23 23:14:18 -03:00
|
|
|
#elif YAW_OPTION == 1
|
|
|
|
|
|
|
|
void
|
|
|
|
output_yaw_with_hold(boolean hold)
|
|
|
|
{
|
2011-05-23 23:18:30 -03:00
|
|
|
// re-define nav_yaw if we have stick input
|
|
|
|
if(g.rc_4.control_in != 0){
|
|
|
|
// set nav_yaw + or - the current location
|
2011-06-19 02:31:33 -03:00
|
|
|
nav_yaw = (long)g.rc_4.control_in + dcm.yaw_sensor;
|
|
|
|
//nav_yaw += (long)(g.rc_4.control_in / 90);
|
2011-05-23 23:18:30 -03:00
|
|
|
}
|
2011-05-23 23:14:18 -03:00
|
|
|
|
2011-05-26 02:58:17 -03:00
|
|
|
// we need to wrap our value so we can be 0 to 360 (*100)
|
|
|
|
nav_yaw = wrap_360(nav_yaw);
|
|
|
|
|
2011-05-23 23:14:18 -03:00
|
|
|
// how far off is nav_yaw from our current yaw?
|
|
|
|
yaw_error = nav_yaw - dcm.yaw_sensor;
|
|
|
|
|
|
|
|
// we need to wrap our value so we can be -180 to 180 (*100)
|
|
|
|
yaw_error = wrap_180(yaw_error);
|
|
|
|
|
|
|
|
// limit the error we're feeding to the PID
|
2011-05-26 02:58:17 -03:00
|
|
|
yaw_error = constrain(yaw_error, -3500, 3500); // limit error to 60 degees
|
2011-05-23 23:14:18 -03:00
|
|
|
|
|
|
|
// Apply PID and save the new angle back to RC_Channel
|
2011-05-26 02:58:17 -03:00
|
|
|
g.rc_4.servo_out = g.pid_yaw.get_pi(yaw_error, delta_ms_fast_loop, 1.0); // .4 * 4000 = 1600
|
2011-05-23 23:14:18 -03:00
|
|
|
|
|
|
|
// add in yaw dampener
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_4.servo_out -= (degrees(omega.z) * 100) * g.pid_yaw.kD();
|
2011-05-29 01:02:01 -03:00
|
|
|
g.rc_4.servo_out = constrain(g.rc_4.servo_out, -2500, 2500); // limit error to 60 degees
|
2011-05-23 23:14:18 -03:00
|
|
|
}
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
#elif YAW_OPTION == 2
|
|
|
|
|
|
|
|
void
|
|
|
|
output_yaw_with_hold(boolean hold)
|
|
|
|
{
|
|
|
|
if(hold){
|
|
|
|
// try and hold the current nav_yaw setting
|
|
|
|
yaw_error = nav_yaw - dcm.yaw_sensor; // +- 60°
|
|
|
|
|
|
|
|
// we need to wrap our value so we can be -180 to 180 (*100)
|
|
|
|
yaw_error = wrap_180(yaw_error);
|
|
|
|
|
|
|
|
// limit the error we're feeding to the PID
|
|
|
|
yaw_error = constrain(yaw_error, -3500, 3500); // limit error to 40 degees
|
|
|
|
|
|
|
|
// Apply PID and save the new angle back to RC_Channel
|
|
|
|
g.rc_4.servo_out = g.pid_yaw.get_pi(yaw_error, delta_ms_fast_loop, 1.0); // .4 * 4000 = 1600
|
|
|
|
|
|
|
|
// add in yaw dampener
|
|
|
|
g.rc_4.servo_out -= (degrees(omega.z) * 100) * g.pid_yaw.kD();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// RATE control
|
|
|
|
long error = ((long)g.rc_4.control_in * 6) - (degrees(omega.z) * 100); // control is += 4500 * 6 = 36000
|
|
|
|
g.rc_4.servo_out = g.pid_acro_rate_yaw.get_pid(error, delta_ms_fast_loop, 1.0); // kP .07 * 36000 = 2520
|
|
|
|
nav_yaw = dcm.yaw_sensor; // save our Yaw
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit Output
|
|
|
|
g.rc_4.servo_out = constrain(g.rc_4.servo_out, -2500, 2500); // limit to 24°
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:14:18 -03:00
|
|
|
#endif
|