2012-04-30 04:17:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2013-06-03 06:33:59 -03:00
|
|
|
/*
|
|
|
|
allow for runtime change of control channel ordering
|
|
|
|
*/
|
|
|
|
static void set_control_channels(void)
|
|
|
|
{
|
|
|
|
channel_steer = RC_Channel::rc_channel(rcmap.roll()-1);
|
|
|
|
channel_throttle = RC_Channel::rc_channel(rcmap.throttle()-1);
|
2013-06-30 21:10:38 -03:00
|
|
|
channel_learn = RC_Channel::rc_channel(g.learn_channel-1);
|
2013-06-03 06:33:59 -03:00
|
|
|
|
2012-04-30 04:17:14 -03:00
|
|
|
// set rc channel ranges
|
2013-06-03 06:33:59 -03:00
|
|
|
channel_steer->set_angle(SERVO_MAX);
|
|
|
|
channel_throttle->set_angle(100);
|
2013-06-03 09:22:47 -03:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2013-06-03 09:22:47 -03:00
|
|
|
static void init_rc_in()
|
|
|
|
{
|
2012-04-30 04:17:14 -03:00
|
|
|
// set rc dead zones
|
2013-07-11 11:13:18 -03:00
|
|
|
channel_steer->set_default_dead_zone(30);
|
2013-07-14 21:25:30 -03:00
|
|
|
channel_throttle->set_default_dead_zone(30);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
//set auxiliary ranges
|
2013-07-14 20:57:00 -03:00
|
|
|
update_aux();
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_rc_out()
|
|
|
|
{
|
2013-06-03 06:33:59 -03:00
|
|
|
for (uint8_t i=0; i<8; i++) {
|
|
|
|
RC_Channel::rc_channel(i)->enable_out();
|
|
|
|
RC_Channel::rc_channel(i)->output_trim();
|
|
|
|
}
|
2013-07-14 20:57:00 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
servo_write(CH_9, g.rc_9.radio_trim);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2 || CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
servo_write(CH_10, g.rc_10.radio_trim);
|
|
|
|
servo_write(CH_11, g.rc_11.radio_trim);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
servo_write(CH_12, g.rc_12.radio_trim);
|
|
|
|
#endif
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_radio()
|
|
|
|
{
|
2013-12-19 18:48:36 -04:00
|
|
|
if (!hal.rcin->valid_channels()) {
|
|
|
|
control_failsafe(channel_throttle->radio_in);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
failsafe.last_valid_rc_ms = hal.scheduler->millis();
|
|
|
|
|
2013-06-03 06:33:59 -03:00
|
|
|
for (uint8_t i=0; i<8; i++) {
|
|
|
|
RC_Channel::rc_channel(i)->set_pwm(RC_Channel::rc_channel(i)->read());
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2013-06-03 06:33:59 -03:00
|
|
|
control_failsafe(channel_throttle->radio_in);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2013-06-03 06:33:59 -03:00
|
|
|
channel_throttle->servo_out = channel_throttle->control_in;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2013-11-24 20:21:15 -04:00
|
|
|
if (abs(channel_throttle->servo_out) > 50) {
|
|
|
|
throttle_nudge = (g.throttle_max - g.throttle_cruise) * ((fabsf(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
|
|
|
|
*/
|
|
|
|
|
2013-06-03 06:33:59 -03:00
|
|
|
float motor1 = channel_steer->norm_input();
|
|
|
|
float motor2 = 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);
|
2013-06-03 06:33:59 -03:00
|
|
|
int16_t steer = channel_steer->radio_trim;
|
|
|
|
int16_t thr = channel_throttle->radio_trim;
|
2013-03-14 21:04:33 -03:00
|
|
|
if (steering_scaled > 0.0f) {
|
2013-06-03 06:33:59 -03:00
|
|
|
steer += steering_scaled*(channel_steer->radio_max-channel_steer->radio_trim);
|
2013-03-14 21:04:33 -03:00
|
|
|
} else {
|
2013-06-03 06:33:59 -03:00
|
|
|
steer += steering_scaled*(channel_steer->radio_trim-channel_steer->radio_min);
|
2013-03-14 21:04:33 -03:00
|
|
|
}
|
|
|
|
if (throttle_scaled > 0.0f) {
|
2013-06-03 06:33:59 -03:00
|
|
|
thr += throttle_scaled*(channel_throttle->radio_max-channel_throttle->radio_trim);
|
2013-03-14 21:04:33 -03:00
|
|
|
} else {
|
2013-06-03 06:33:59 -03:00
|
|
|
thr += throttle_scaled*(channel_throttle->radio_trim-channel_throttle->radio_min);
|
2013-03-14 21:04:33 -03:00
|
|
|
}
|
2013-06-03 06:33:59 -03:00
|
|
|
channel_steer->set_pwm(steer);
|
|
|
|
channel_throttle->set_pwm(thr);
|
2013-03-14 21:04:33 -03:00
|
|
|
}
|
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-12-19 18:48:36 -04:00
|
|
|
bool failed = pwm < (uint16_t)g.fs_throttle_value;
|
|
|
|
if (hal.scheduler->millis() - failsafe.last_valid_rc_ms > 2000) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
failsafe_trigger(FAILSAFE_EVENT_THROTTLE, failed);
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void trim_control_surfaces()
|
|
|
|
{
|
|
|
|
read_radio();
|
|
|
|
// Store control surface trim values
|
|
|
|
// ---------------------------------
|
2013-06-03 06:33:59 -03:00
|
|
|
if (channel_steer->radio_in > 1400) {
|
|
|
|
channel_steer->radio_trim = channel_steer->radio_in;
|
2013-02-07 18:21:22 -04:00
|
|
|
// save to eeprom
|
2013-06-03 06:33:59 -03:00
|
|
|
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
|
|
|
}
|