2012-04-30 04:17:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
//****************************************************************
|
|
|
|
// Function that controls aileron/rudder, elevator, rudder (if 4 channel control) and throttle to produce desired attitude and airspeed.
|
|
|
|
//****************************************************************
|
|
|
|
|
2012-05-14 16:21:29 -03:00
|
|
|
static void learning()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
|
|
|
// Calculate desired servo output for the turn // Wheels Direction
|
|
|
|
// ---------------------------------------------
|
2012-05-09 02:12:26 -03:00
|
|
|
|
|
|
|
g.channel_roll.servo_out = nav_roll;
|
2012-04-30 04:17:14 -03:00
|
|
|
g.channel_roll.servo_out = g.channel_roll.servo_out * g.turn_gain;
|
|
|
|
g.channel_rudder.servo_out = g.channel_roll.servo_out;
|
|
|
|
}
|
|
|
|
|
2012-11-27 21:13:39 -04:00
|
|
|
|
2012-11-27 20:42:22 -04:00
|
|
|
/*****************************************
|
|
|
|
* Throttle slew limit
|
|
|
|
*****************************************/
|
2012-11-27 21:13:39 -04:00
|
|
|
static void throttle_slew_limit(int16_t last_throttle)
|
2012-11-27 20:42:22 -04:00
|
|
|
{
|
|
|
|
// if slew limit rate is set to zero then do not slew limit
|
|
|
|
if (g.throttle_slewrate) {
|
|
|
|
// limit throttle change by the given percentage per second
|
|
|
|
float temp = g.throttle_slewrate * G_Dt * 0.01 * fabs(g.channel_throttle.radio_max - g.channel_throttle.radio_min);
|
2012-11-27 21:13:39 -04:00
|
|
|
// allow a minimum change of 1 PWM per cycle
|
|
|
|
if (temp < 1) {
|
|
|
|
temp = 1;
|
|
|
|
}
|
|
|
|
g.channel_throttle.radio_out = constrain(g.channel_throttle.radio_out, last_throttle - temp, last_throttle + temp);
|
2012-11-27 20:42:22 -04:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
static void calc_throttle()
|
2012-05-17 13:42:16 -03:00
|
|
|
{ int rov_speed;
|
|
|
|
|
|
|
|
int throttle_target = g.throttle_cruise + throttle_nudge + 50;
|
|
|
|
|
2012-11-17 02:45:20 -04:00
|
|
|
rov_speed = g.airspeed_cruise;
|
2012-05-17 13:42:16 -03:00
|
|
|
|
2012-11-17 02:45:20 -04:00
|
|
|
if (speed_boost)
|
|
|
|
rov_speed *= g.booster;
|
2012-05-17 13:42:16 -03:00
|
|
|
|
2012-11-17 02:45:20 -04:00
|
|
|
groundspeed_error = rov_speed - (float)ground_speed;
|
2012-05-17 13:42:16 -03:00
|
|
|
|
2012-11-27 20:42:22 -04:00
|
|
|
throttle = (throttle_target + g.pidTeThrottle.get_pid(groundspeed_error)) * 10;
|
2012-11-17 02:45:20 -04:00
|
|
|
g.channel_throttle.servo_out = constrain(((float)throttle / 10.0f), 0, g.throttle_max.get());
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************
|
|
|
|
* Calculate desired turn angles (in medium freq loop)
|
|
|
|
*****************************************/
|
|
|
|
|
|
|
|
static void calc_nav_roll()
|
|
|
|
{
|
|
|
|
// Adjust gain based on ground speed
|
|
|
|
nav_gain_scaler = (float)ground_speed / (g.airspeed_cruise * 100.0);
|
|
|
|
nav_gain_scaler = constrain(nav_gain_scaler, 0.2, 1.4);
|
|
|
|
|
|
|
|
// Calculate the required turn of the wheels rover
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2012-11-29 03:09:05 -04:00
|
|
|
// negative error = left turn
|
2012-04-30 04:17:14 -03:00
|
|
|
// positive error = right turn
|
|
|
|
|
2012-07-06 06:57:11 -03:00
|
|
|
nav_roll = g.pidNavRoll.get_pid(bearing_error, nav_gain_scaler); //returns desired bank angle in degrees*100
|
2012-06-13 15:43:35 -03:00
|
|
|
|
2012-11-29 03:09:05 -04:00
|
|
|
if(obstacle) { // obstacle avoidance
|
2012-06-13 15:43:35 -03:00
|
|
|
nav_roll += 9000; // if obstacle in front turn 90° right
|
2012-11-29 03:09:05 -04:00
|
|
|
speed_boost = false;
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
nav_roll = constrain(nav_roll, -g.roll_limit.get(), g.roll_limit.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zeros out navigation Integrators if we are changing mode, have passed a waypoint, etc.
|
|
|
|
// Keeps outdated data out of our calculations
|
|
|
|
static void reset_I(void)
|
|
|
|
{
|
|
|
|
g.pidNavRoll.reset_I();
|
|
|
|
g.pidTeThrottle.reset_I();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************
|
|
|
|
* Set the flight control servos based on the current calculated values
|
|
|
|
*****************************************/
|
|
|
|
static void set_servos(void)
|
|
|
|
{
|
2012-11-27 21:13:39 -04:00
|
|
|
int16_t last_throttle = g.channel_throttle.radio_out;
|
|
|
|
|
2012-05-14 16:21:29 -03:00
|
|
|
if((control_mode == MANUAL) || (control_mode == LEARNING)){
|
2012-04-30 04:17:14 -03:00
|
|
|
// do a direct pass through of radio values
|
|
|
|
g.channel_roll.radio_out = g.channel_roll.radio_in;
|
2012-06-13 15:43:35 -03:00
|
|
|
|
|
|
|
if(obstacle) // obstacle in front, turn right in Stabilize mode
|
|
|
|
g.channel_roll.radio_out -= 500;
|
|
|
|
|
2012-04-30 04:17:14 -03:00
|
|
|
g.channel_pitch.radio_out = g.channel_pitch.radio_in;
|
|
|
|
|
|
|
|
g.channel_throttle.radio_out = g.channel_throttle.radio_in;
|
|
|
|
g.channel_rudder.radio_out = g.channel_roll.radio_in;
|
|
|
|
} else {
|
2012-11-27 06:47:30 -04:00
|
|
|
g.channel_roll.calc_pwm();
|
2012-04-30 04:17:14 -03:00
|
|
|
g.channel_pitch.calc_pwm();
|
|
|
|
g.channel_rudder.calc_pwm();
|
|
|
|
|
|
|
|
g.channel_throttle.radio_out = g.channel_throttle.radio_in;
|
|
|
|
g.channel_throttle.servo_out = constrain(g.channel_throttle.servo_out, g.throttle_min.get(), g.throttle_max.get());
|
2012-11-27 06:47:30 -04:00
|
|
|
}
|
2012-05-17 13:42:16 -03:00
|
|
|
|
2012-11-27 06:47:30 -04:00
|
|
|
if (control_mode >= AUTO) {
|
|
|
|
// convert 0 to 100% into PWM
|
|
|
|
g.channel_throttle.calc_pwm();
|
2012-11-27 21:13:39 -04:00
|
|
|
|
|
|
|
// limit throttle movement speed
|
|
|
|
throttle_slew_limit(last_throttle);
|
2012-11-27 06:47:30 -04:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
|
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED || HIL_SERVOS
|
|
|
|
// send values to the PWM timers for output
|
|
|
|
// ----------------------------------------
|
|
|
|
APM_RC.OutputCh(CH_1, g.channel_roll.radio_out); // send to Servos
|
|
|
|
APM_RC.OutputCh(CH_2, g.channel_pitch.radio_out); // send to Servos
|
|
|
|
APM_RC.OutputCh(CH_3, g.channel_throttle.radio_out); // send to Servos
|
|
|
|
APM_RC.OutputCh(CH_4, g.channel_rudder.radio_out); // send to Servos
|
|
|
|
// Route configurable aux. functions to their respective servos
|
|
|
|
|
|
|
|
g.rc_5.output_ch(CH_5);
|
|
|
|
g.rc_6.output_ch(CH_6);
|
|
|
|
g.rc_7.output_ch(CH_7);
|
|
|
|
g.rc_8.output_ch(CH_8);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void demo_servos(byte i) {
|
|
|
|
|
|
|
|
while(i > 0){
|
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("Demo Servos!"));
|
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED || HIL_SERVOS
|
|
|
|
APM_RC.OutputCh(1, 1400);
|
|
|
|
mavlink_delay(400);
|
|
|
|
APM_RC.OutputCh(1, 1600);
|
|
|
|
mavlink_delay(200);
|
|
|
|
APM_RC.OutputCh(1, 1500);
|
|
|
|
#endif
|
|
|
|
mavlink_delay(400);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|