2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
throttle control
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
// user input:
|
|
|
|
// -----------
|
|
|
|
void output_manual_throttle()
|
|
|
|
{
|
2011-01-31 13:10:07 -04:00
|
|
|
rc_3.servo_out = (float)rc_3.control_in * angle_boost();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Autopilot
|
|
|
|
// ---------
|
|
|
|
void output_auto_throttle()
|
|
|
|
{
|
2010-12-26 01:18:25 -04:00
|
|
|
rc_3.servo_out = (float)nav_throttle * angle_boost();
|
2011-01-31 13:10:07 -04:00
|
|
|
// make sure we never send a 0 throttle that will cut the motors
|
2010-12-26 01:18:25 -04:00
|
|
|
rc_3.servo_out = max(rc_3.servo_out, 1);
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void calc_nav_throttle()
|
2011-02-06 03:04:26 -04:00
|
|
|
{
|
|
|
|
// limit error
|
2011-02-13 18:32:34 -04:00
|
|
|
long error = constrain(altitude_error, -400, 400);
|
2011-02-06 03:04:26 -04:00
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
if(altitude_sensor == BARO) {
|
2011-02-10 03:25:56 -04:00
|
|
|
float t = pid_baro_throttle.kP();
|
|
|
|
|
2011-02-13 18:32:34 -04:00
|
|
|
if(error > 0){ // go up
|
2011-02-13 01:21:32 -04:00
|
|
|
pid_baro_throttle.kP(t);
|
2011-02-13 18:32:34 -04:00
|
|
|
}else{ // go down
|
2011-02-10 03:25:56 -04:00
|
|
|
pid_baro_throttle.kP(t/4.0);
|
2011-02-07 02:19:01 -04:00
|
|
|
}
|
2011-02-13 01:21:32 -04:00
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
// limit output of throttle control
|
2011-02-11 18:14:06 -04:00
|
|
|
nav_throttle = pid_baro_throttle.get_pid(error, delta_ms_fast_loop, 1.0);
|
2011-02-13 01:21:32 -04:00
|
|
|
nav_throttle = throttle_cruise + constrain(nav_throttle, -30, 80);
|
|
|
|
|
2011-02-10 03:25:56 -04:00
|
|
|
pid_baro_throttle.kP(t);
|
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
} else {
|
|
|
|
// SONAR
|
2011-02-06 03:04:26 -04:00
|
|
|
nav_throttle = pid_sonar_throttle.get_pid(error, delta_ms_fast_loop, 1.0);
|
2011-01-09 22:24:26 -04:00
|
|
|
|
|
|
|
// limit output of throttle control
|
2011-01-31 13:10:07 -04:00
|
|
|
nav_throttle = throttle_cruise + constrain(nav_throttle, -60, 100);
|
2011-01-09 22:24:26 -04:00
|
|
|
}
|
2011-02-13 01:21:32 -04:00
|
|
|
|
|
|
|
nav_throttle = (nav_throttle + nav_throttle_old) >> 1;
|
|
|
|
nav_throttle_old = nav_throttle;
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2010-12-26 01:18:25 -04:00
|
|
|
float angle_boost()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2011-02-13 18:32:34 -04:00
|
|
|
float temp = cos_pitch_x * cos_roll_x;
|
|
|
|
temp = 2.0 - constrain(temp, .7, 1.0);
|
2010-12-19 12:40:33 -04:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************
|
|
|
|
yaw control
|
|
|
|
****************************************************************/
|
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
void output_manual_yaw()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
|
|
|
if(rc_3.control_in == 0){
|
2011-01-02 16:34:42 -04:00
|
|
|
clear_yaw_control();
|
2011-01-25 01:53:36 -04:00
|
|
|
} else {
|
2011-01-02 16:34:42 -04:00
|
|
|
// Yaw control
|
|
|
|
if(rc_4.control_in == 0){
|
|
|
|
//clear_yaw_control();
|
|
|
|
output_yaw_with_hold(true); // hold yaw
|
|
|
|
}else{
|
|
|
|
output_yaw_with_hold(false); // rate control yaw
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
void auto_yaw()
|
2010-12-26 01:18:25 -04:00
|
|
|
{
|
2011-01-02 16:34:42 -04:00
|
|
|
output_yaw_with_hold(true); // hold yaw
|
2010-12-26 01:18:25 -04:00
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
/*************************************************************
|
|
|
|
picth and roll control
|
|
|
|
****************************************************************/
|
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
|
2011-01-11 17:12:29 -04:00
|
|
|
/*// how hard to tilt towards the target
|
2010-12-19 12:40:33 -04:00
|
|
|
// -----------------------------------
|
|
|
|
void calc_nav_pid()
|
|
|
|
{
|
2011-01-09 22:24:26 -04:00
|
|
|
// how hard to pitch to target
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
nav_angle = pid_nav.get_pid(wp_distance * 100, dTnav, 1.0);
|
|
|
|
nav_angle = constrain(nav_angle, -pitch_max, pitch_max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// distribute the pitch angle based on our orientation
|
|
|
|
// ---------------------------------------------------
|
|
|
|
void calc_nav_pitch()
|
|
|
|
{
|
2011-01-09 22:24:26 -04:00
|
|
|
// how hard to pitch to target
|
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
long angle = wrap_360(nav_bearing - dcm.yaw_sensor);
|
2011-01-09 22:24:26 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
bool rev = false;
|
|
|
|
float roll_out;
|
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
if(angle > 18000){
|
|
|
|
angle -= 18000;
|
2010-12-19 12:40:33 -04:00
|
|
|
rev = true;
|
|
|
|
}
|
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
roll_out = abs(angle - 18000);
|
2010-12-19 12:40:33 -04:00
|
|
|
roll_out = (9000.0 - roll_out) / 9000.0;
|
|
|
|
roll_out = (rev) ? roll_out : -roll_out;
|
|
|
|
|
|
|
|
nav_pitch = (float)nav_angle * roll_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// distribute the roll angle based on our orientation
|
|
|
|
// --------------------------------------------------
|
|
|
|
void calc_nav_roll()
|
|
|
|
{
|
2011-01-25 01:53:36 -04:00
|
|
|
long angle = wrap_360(nav_bearing - dcm.yaw_sensor);
|
2011-01-09 22:24:26 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
bool rev = false;
|
|
|
|
float roll_out;
|
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
if(angle > 18000){
|
|
|
|
angle -= 18000;
|
2010-12-19 12:40:33 -04:00
|
|
|
rev = true;
|
|
|
|
}
|
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
roll_out = abs(angle - 9000);
|
2010-12-19 12:40:33 -04:00
|
|
|
roll_out = (9000.0 - roll_out) / 9000.0;
|
|
|
|
roll_out = (rev) ? -roll_out : roll_out;
|
|
|
|
|
|
|
|
nav_roll = (float)nav_angle * roll_out;
|
|
|
|
}
|
2011-01-11 17:12:29 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-01-09 22:24:26 -04:00
|
|
|
|