2012-04-30 04:17:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
static void init_rc_in()
|
|
|
|
{
|
|
|
|
// set rc channel ranges
|
2013-02-07 18:21:22 -04:00
|
|
|
g.channel_steer.set_angle(SERVO_MAX);
|
2012-12-18 00:42:11 -04:00
|
|
|
g.channel_throttle.set_angle(100);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
// set rc dead zones
|
2013-02-07 18:21:22 -04:00
|
|
|
g.channel_steer.set_dead_zone(60);
|
2012-04-30 04:17:14 -03:00
|
|
|
g.channel_throttle.set_dead_zone(6);
|
|
|
|
|
|
|
|
//set auxiliary ranges
|
2013-03-18 04:06:34 -03:00
|
|
|
update_aux_servo_function(&g.rc_2, &g.rc_4, &g.rc_5, &g.rc_6, &g.rc_7, &g.rc_8);
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_rc_out()
|
|
|
|
{
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.rcout->enable_ch(CH_1);
|
|
|
|
hal.rcout->enable_ch(CH_2);
|
|
|
|
hal.rcout->enable_ch(CH_3);
|
|
|
|
hal.rcout->enable_ch(CH_4);
|
|
|
|
hal.rcout->enable_ch(CH_5);
|
|
|
|
hal.rcout->enable_ch(CH_6);
|
|
|
|
hal.rcout->enable_ch(CH_7);
|
|
|
|
hal.rcout->enable_ch(CH_8);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2013-02-07 18:21:22 -04:00
|
|
|
hal.rcout->write(CH_1, g.channel_steer.radio_trim); // Initialization of servo outputs
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.rcout->write(CH_3, g.channel_throttle.radio_trim);
|
|
|
|
|
2013-03-18 04:06:34 -03:00
|
|
|
hal.rcout->write(CH_2, g.rc_2.radio_trim);
|
|
|
|
hal.rcout->write(CH_4, g.rc_4.radio_trim);
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.rcout->write(CH_5, g.rc_5.radio_trim);
|
|
|
|
hal.rcout->write(CH_6, g.rc_6.radio_trim);
|
|
|
|
hal.rcout->write(CH_7, g.rc_7.radio_trim);
|
|
|
|
hal.rcout->write(CH_8, g.rc_8.radio_trim);
|
2012-04-30 04:17:14 -03:00
|
|
|
#else
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.rcout->write(CH_1, 1500); // Initialization of servo outputs
|
|
|
|
hal.rcout->write(CH_2, 1500);
|
|
|
|
hal.rcout->write(CH_3, 1000);
|
|
|
|
hal.rcout->write(CH_4, 1500);
|
|
|
|
|
|
|
|
hal.rcout->write(CH_5, 1500);
|
|
|
|
hal.rcout->write(CH_6, 1500);
|
|
|
|
hal.rcout->write(CH_7, 1500);
|
|
|
|
hal.rcout->write(CH_8, 2000);
|
2012-04-30 04:17:14 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_radio()
|
|
|
|
{
|
2013-03-14 21:04:33 -03:00
|
|
|
g.channel_steer.set_pwm(hal.rcin->read(CH_STEER));
|
2012-12-18 07:44:12 -04:00
|
|
|
|
2012-12-18 17:00:32 -04:00
|
|
|
g.channel_throttle.set_pwm(hal.rcin->read(CH_3));
|
2013-03-18 04:06:34 -03:00
|
|
|
|
|
|
|
g.rc_2.set_pwm(hal.rcin->read(CH_2));
|
|
|
|
g.rc_4.set_pwm(hal.rcin->read(CH_4));
|
2012-12-18 17:00:32 -04:00
|
|
|
g.rc_5.set_pwm(hal.rcin->read(CH_5));
|
|
|
|
g.rc_6.set_pwm(hal.rcin->read(CH_6));
|
|
|
|
g.rc_7.set_pwm(hal.rcin->read(CH_7));
|
|
|
|
g.rc_8.set_pwm(hal.rcin->read(CH_8));
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
control_failsafe(g.channel_throttle.radio_in);
|
|
|
|
|
|
|
|
g.channel_throttle.servo_out = g.channel_throttle.control_in;
|
|
|
|
|
|
|
|
if (g.channel_throttle.servo_out > 50) {
|
2012-11-17 02:45:20 -04:00
|
|
|
throttle_nudge = (g.throttle_max - g.throttle_cruise) * ((g.channel_throttle.norm_input()-0.5) / 0.5);
|
2012-04-30 04:17:14 -03:00
|
|
|
} else {
|
|
|
|
throttle_nudge = 0;
|
|
|
|
}
|
|
|
|
|
2013-03-14 21:04:33 -03:00
|
|
|
if (g.skid_steer_in) {
|
|
|
|
// convert the two radio_in values from skid steering values
|
|
|
|
/*
|
|
|
|
mixing rule:
|
|
|
|
steering = motor1 - motor2
|
|
|
|
throttle = 0.5*(motor1 + motor2)
|
|
|
|
motor1 = throttle + 0.5*steering
|
|
|
|
motor2 = throttle - 0.5*steering
|
|
|
|
*/
|
|
|
|
|
|
|
|
float motor1 = g.channel_steer.norm_input();
|
|
|
|
float motor2 = g.channel_throttle.norm_input();
|
2013-03-29 05:43:28 -03:00
|
|
|
float steering_scaled = motor1 - motor2;
|
2013-03-14 21:04:33 -03:00
|
|
|
float throttle_scaled = 0.5f*(motor1 + motor2);
|
|
|
|
int16_t steer = g.channel_steer.radio_trim;
|
|
|
|
int16_t thr = g.channel_throttle.radio_trim;
|
|
|
|
if (steering_scaled > 0.0f) {
|
|
|
|
steer += steering_scaled*(g.channel_steer.radio_max-g.channel_steer.radio_trim);
|
|
|
|
} else {
|
|
|
|
steer += steering_scaled*(g.channel_steer.radio_trim-g.channel_steer.radio_min);
|
|
|
|
}
|
|
|
|
if (throttle_scaled > 0.0f) {
|
|
|
|
thr += throttle_scaled*(g.channel_throttle.radio_max-g.channel_throttle.radio_trim);
|
|
|
|
} else {
|
|
|
|
thr += throttle_scaled*(g.channel_throttle.radio_trim-g.channel_throttle.radio_min);
|
|
|
|
}
|
|
|
|
g.channel_steer.set_pwm(steer);
|
|
|
|
g.channel_throttle.set_pwm(thr);
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void control_failsafe(uint16_t pwm)
|
|
|
|
{
|
2013-02-08 06:17:54 -04:00
|
|
|
if (!g.fs_throttle_enabled) {
|
|
|
|
// no throttle failsafe
|
2012-04-30 04:17:14 -03:00
|
|
|
return;
|
2013-02-08 06:17:54 -04:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
// Check for failsafe condition based on loss of GCS control
|
|
|
|
if (rc_override_active) {
|
2013-03-28 20:25:53 -03:00
|
|
|
failsafe_trigger(FAILSAFE_EVENT_RC, (millis() - failsafe.rc_override_timer) > 1500);
|
2013-02-08 06:17:54 -04:00
|
|
|
} else if (g.fs_throttle_enabled) {
|
2013-03-28 20:25:53 -03:00
|
|
|
failsafe_trigger(FAILSAFE_EVENT_THROTTLE, pwm < (uint16_t)g.fs_throttle_value);
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trim_control_surfaces()
|
|
|
|
{
|
|
|
|
read_radio();
|
|
|
|
// Store control surface trim values
|
|
|
|
// ---------------------------------
|
2013-02-07 18:21:22 -04:00
|
|
|
if (g.channel_steer.radio_in > 1400) {
|
|
|
|
g.channel_steer.radio_trim = g.channel_steer.radio_in;
|
|
|
|
// save to eeprom
|
|
|
|
g.channel_steer.save_eeprom();
|
2012-11-27 17:58:11 -04:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trim_radio()
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 30; y++) {
|
|
|
|
read_radio();
|
|
|
|
}
|
2012-11-27 17:58:11 -04:00
|
|
|
trim_control_surfaces();
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|