2011-03-19 07:20:11 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2012-11-11 09:11:12 -04:00
|
|
|
// Function that will read the radio data, limit servos and trigger a failsafe
|
2011-02-25 01:33:39 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2012-08-04 13:39:20 -03:00
|
|
|
|
2011-10-27 15:34:00 -03:00
|
|
|
static void default_dead_zones()
|
|
|
|
{
|
2013-07-11 11:15:11 -03:00
|
|
|
g.rc_1.set_default_dead_zone(30);
|
|
|
|
g.rc_2.set_default_dead_zone(30);
|
2012-08-16 21:50:02 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2013-07-11 11:15:11 -03:00
|
|
|
g.rc_3.set_default_dead_zone(10);
|
|
|
|
g.rc_4.set_default_dead_zone(15);
|
2013-11-08 02:52:48 -04:00
|
|
|
g.rc_8.set_default_dead_zone(10);
|
2012-08-16 21:50:02 -03:00
|
|
|
#else
|
2013-07-11 11:15:11 -03:00
|
|
|
g.rc_3.set_default_dead_zone(30);
|
|
|
|
g.rc_4.set_default_dead_zone(40);
|
2012-08-16 21:50:02 -03:00
|
|
|
#endif
|
2013-07-11 11:15:11 -03:00
|
|
|
g.rc_6.set_default_dead_zone(0);
|
2011-10-27 15:34:00 -03:00
|
|
|
}
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void init_rc_in()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-08-16 21:50:02 -03:00
|
|
|
// set rc channel ranges
|
2013-08-11 00:51:08 -03:00
|
|
|
g.rc_1.set_angle(ROLL_PITCH_INPUT_MAX);
|
|
|
|
g.rc_2.set_angle(ROLL_PITCH_INPUT_MAX);
|
2012-08-16 21:50:02 -03:00
|
|
|
g.rc_3.set_range(g.throttle_min, g.throttle_max);
|
|
|
|
g.rc_4.set_angle(4500);
|
|
|
|
|
2012-12-09 03:44:42 -04:00
|
|
|
g.rc_1.set_type(RC_CHANNEL_TYPE_ANGLE_RAW);
|
|
|
|
g.rc_2.set_type(RC_CHANNEL_TYPE_ANGLE_RAW);
|
|
|
|
g.rc_4.set_type(RC_CHANNEL_TYPE_ANGLE_RAW);
|
2012-08-16 21:50:02 -03:00
|
|
|
|
2013-10-31 03:22:34 -03:00
|
|
|
//set auxiliary servo ranges
|
2012-08-16 21:50:02 -03:00
|
|
|
g.rc_5.set_range(0,1000);
|
|
|
|
g.rc_6.set_range(0,1000);
|
|
|
|
g.rc_7.set_range(0,1000);
|
|
|
|
g.rc_8.set_range(0,1000);
|
|
|
|
|
2013-07-11 11:15:11 -03:00
|
|
|
// set default dead zones
|
|
|
|
default_dead_zones();
|
2010-12-27 19:09:08 -04:00
|
|
|
}
|
|
|
|
|
2013-05-16 04:32:00 -03:00
|
|
|
// init_rc_out -- initialise motors and check if pilot wants to perform ESC calibration
|
2011-07-17 07:32:00 -03:00
|
|
|
static void init_rc_out()
|
2010-12-27 19:09:08 -04:00
|
|
|
{
|
2012-08-16 21:50:02 -03:00
|
|
|
motors.set_update_rate(g.rc_speed);
|
|
|
|
motors.set_frame_orientation(g.frame_orientation);
|
|
|
|
motors.Init(); // motor initialisation
|
|
|
|
motors.set_min_throttle(g.throttle_min);
|
|
|
|
|
2012-12-12 19:46:20 -04:00
|
|
|
for(uint8_t i = 0; i < 5; i++) {
|
2012-08-16 21:50:02 -03:00
|
|
|
delay(20);
|
|
|
|
read_radio();
|
|
|
|
}
|
2011-03-06 20:57:06 -04:00
|
|
|
|
2012-08-16 21:50:02 -03:00
|
|
|
// we want the input to be scaled correctly
|
|
|
|
g.rc_3.set_range_out(0,1000);
|
2012-05-31 18:59:03 -03:00
|
|
|
|
2013-05-16 04:32:00 -03:00
|
|
|
// full throttle means to enter ESC calibration
|
2013-10-13 08:41:11 -03:00
|
|
|
if(g.rc_3.control_in >= (g.throttle_max - 50)) {
|
2012-08-16 21:50:02 -03:00
|
|
|
if(g.esc_calibrate == 0) {
|
|
|
|
// we will enter esc_calibrate mode on next reboot
|
|
|
|
g.esc_calibrate.set_and_save(1);
|
2012-10-09 00:31:05 -03:00
|
|
|
// display message on console
|
2012-11-21 02:08:03 -04:00
|
|
|
cliSerial->printf_P(PSTR("Entering ESC Calibration: please restart APM.\n"));
|
2013-08-08 10:15:25 -03:00
|
|
|
// turn on esc calibration notification
|
2013-08-14 00:07:35 -03:00
|
|
|
AP_Notify::flags.esc_calibration = true;
|
2012-08-16 21:50:02 -03:00
|
|
|
// block until we restart
|
2013-08-29 00:52:39 -03:00
|
|
|
while(1) { delay(5); }
|
2012-08-16 21:50:02 -03:00
|
|
|
}else{
|
2012-11-21 02:08:03 -04:00
|
|
|
cliSerial->printf_P(PSTR("ESC Calibration active: passing throttle through to ESCs.\n"));
|
2012-08-16 21:50:02 -03:00
|
|
|
// clear esc flag
|
|
|
|
g.esc_calibrate.set_and_save(0);
|
2013-01-24 22:34:48 -04:00
|
|
|
// pass through user throttle to escs
|
2012-08-16 21:50:02 -03:00
|
|
|
init_esc();
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// did we abort the calibration?
|
|
|
|
if(g.esc_calibrate == 1)
|
|
|
|
g.esc_calibrate.set_and_save(0);
|
2013-05-16 04:32:00 -03:00
|
|
|
}
|
2012-08-16 21:50:02 -03:00
|
|
|
|
2013-05-16 04:32:00 -03:00
|
|
|
// enable output to motors
|
|
|
|
pre_arm_rc_checks();
|
|
|
|
if (ap.pre_arm_rc_check) {
|
2012-08-16 21:50:02 -03:00
|
|
|
output_min();
|
|
|
|
}
|
2011-03-26 18:03:20 -03:00
|
|
|
}
|
2011-03-06 20:57:06 -04:00
|
|
|
|
2013-05-16 04:32:00 -03:00
|
|
|
// output_min - enable and output lowest possible value to motors
|
2011-03-26 18:03:20 -03:00
|
|
|
void output_min()
|
|
|
|
{
|
2012-08-16 21:50:02 -03:00
|
|
|
// enable motors
|
|
|
|
motors.enable();
|
|
|
|
motors.output_min();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2012-11-13 05:18:21 -04:00
|
|
|
|
2013-03-16 05:14:21 -03:00
|
|
|
#define FAILSAFE_RADIO_TIMEOUT_MS 2000 // 2 seconds
|
2011-07-17 07:32:00 -03:00
|
|
|
static void read_radio()
|
2010-12-26 01:25:52 -04:00
|
|
|
{
|
2012-12-12 21:37:32 -04:00
|
|
|
static uint32_t last_update = 0;
|
2014-03-25 00:40:50 -03:00
|
|
|
if (hal.rcin->new_input()) {
|
2012-12-12 21:37:32 -04:00
|
|
|
last_update = millis();
|
2013-10-08 03:25:14 -03:00
|
|
|
ap.new_radio_frame = true;
|
2012-12-12 21:37:32 -04:00
|
|
|
uint16_t periods[8];
|
|
|
|
hal.rcin->read(periods,8);
|
2013-06-03 09:22:26 -03:00
|
|
|
g.rc_1.set_pwm(periods[rcmap.roll()-1]);
|
|
|
|
g.rc_2.set_pwm(periods[rcmap.pitch()-1]);
|
2012-11-11 09:11:12 -04:00
|
|
|
|
2013-06-03 09:22:26 -03:00
|
|
|
set_throttle_and_failsafe(periods[rcmap.throttle()-1]);
|
2012-11-11 09:11:12 -04:00
|
|
|
|
2013-06-03 09:22:26 -03:00
|
|
|
g.rc_4.set_pwm(periods[rcmap.yaw()-1]);
|
2012-12-12 21:37:32 -04:00
|
|
|
g.rc_5.set_pwm(periods[4]);
|
|
|
|
g.rc_6.set_pwm(periods[5]);
|
|
|
|
g.rc_7.set_pwm(periods[6]);
|
|
|
|
g.rc_8.set_pwm(periods[7]);
|
2013-10-21 10:39:25 -03:00
|
|
|
|
|
|
|
// flag we must have an rc receiver attached
|
|
|
|
if (!failsafe.rc_override_active) {
|
|
|
|
ap.rc_receiver_present = true;
|
|
|
|
}
|
2014-04-02 22:19:44 -03:00
|
|
|
|
|
|
|
// update output on any aux channels, for manual passthru
|
|
|
|
RC_Channel_aux::output_ch_all();
|
2012-11-13 05:18:21 -04:00
|
|
|
}else{
|
2012-12-12 21:37:32 -04:00
|
|
|
uint32_t elapsed = millis() - last_update;
|
2012-11-13 05:18:21 -04:00
|
|
|
// turn on throttle failsafe if no update from ppm encoder for 2 seconds
|
2013-03-16 05:14:21 -03:00
|
|
|
if ((elapsed >= FAILSAFE_RADIO_TIMEOUT_MS)
|
2013-09-26 05:54:33 -03:00
|
|
|
&& g.failsafe_throttle && motors.armed() && !failsafe.radio) {
|
2012-12-29 23:08:25 -04:00
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_RADIO, ERROR_CODE_RADIO_LATE_FRAME);
|
2013-03-16 05:14:21 -03:00
|
|
|
set_failsafe_radio(true);
|
2012-11-13 05:18:21 -04:00
|
|
|
}
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2012-11-11 09:11:12 -04:00
|
|
|
|
2013-09-26 05:54:33 -03:00
|
|
|
#define FS_COUNTER 3 // radio failsafe kicks in after 3 consecutive throttle values below failsafe_throttle_value
|
2012-11-11 09:11:12 -04:00
|
|
|
static void set_throttle_and_failsafe(uint16_t throttle_pwm)
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2012-11-11 09:11:12 -04:00
|
|
|
// if failsafe not enabled pass through throttle and exit
|
2012-12-10 10:38:43 -04:00
|
|
|
if(g.failsafe_throttle == FS_THR_DISABLED) {
|
2012-11-11 09:11:12 -04:00
|
|
|
g.rc_3.set_pwm(throttle_pwm);
|
2012-08-16 21:50:02 -03:00
|
|
|
return;
|
2012-11-11 09:11:12 -04:00
|
|
|
}
|
2012-08-16 21:50:02 -03:00
|
|
|
|
2012-11-11 09:11:12 -04:00
|
|
|
//check for low throttle value
|
2012-12-10 10:38:43 -04:00
|
|
|
if (throttle_pwm < (uint16_t)g.failsafe_throttle_value) {
|
2012-11-11 09:11:12 -04:00
|
|
|
|
|
|
|
// if we are already in failsafe or motors not armed pass through throttle and exit
|
2013-09-26 05:54:33 -03:00
|
|
|
if (failsafe.radio || !motors.armed()) {
|
2012-11-11 09:11:12 -04:00
|
|
|
g.rc_3.set_pwm(throttle_pwm);
|
|
|
|
return;
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
|
|
|
|
2012-11-11 09:11:12 -04:00
|
|
|
// check for 3 low throttle values
|
|
|
|
// Note: we do not pass through the low throttle until 3 low throttle values are recieved
|
2013-09-26 05:54:33 -03:00
|
|
|
failsafe.radio_counter++;
|
|
|
|
if( failsafe.radio_counter >= FS_COUNTER ) {
|
|
|
|
failsafe.radio_counter = FS_COUNTER; // check to ensure we don't overflow the counter
|
2013-03-16 05:14:21 -03:00
|
|
|
set_failsafe_radio(true);
|
2012-11-11 09:11:12 -04:00
|
|
|
g.rc_3.set_pwm(throttle_pwm); // pass through failsafe throttle
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2012-11-11 09:11:12 -04:00
|
|
|
}else{
|
|
|
|
// we have a good throttle so reduce failsafe counter
|
2013-09-26 05:54:33 -03:00
|
|
|
failsafe.radio_counter--;
|
|
|
|
if( failsafe.radio_counter <= 0 ) {
|
|
|
|
failsafe.radio_counter = 0; // check to ensure we don't underflow the counter
|
2012-11-11 09:11:12 -04:00
|
|
|
|
|
|
|
// disengage failsafe after three (nearly) consecutive valid throttle values
|
2013-09-26 05:54:33 -03:00
|
|
|
if (failsafe.radio) {
|
2013-03-16 05:14:21 -03:00
|
|
|
set_failsafe_radio(false);
|
2012-11-11 09:11:12 -04:00
|
|
|
}
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2012-11-11 09:11:12 -04:00
|
|
|
// pass through throttle
|
|
|
|
g.rc_3.set_pwm(throttle_pwm);
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void trim_radio()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2012-12-12 19:46:20 -04:00
|
|
|
for (uint8_t i = 0; i < 30; i++) {
|
2012-08-16 21:50:02 -03:00
|
|
|
read_radio();
|
|
|
|
}
|
2011-02-21 16:58:10 -04:00
|
|
|
|
2012-08-16 21:50:02 -03:00
|
|
|
g.rc_1.trim(); // roll
|
|
|
|
g.rc_2.trim(); // pitch
|
|
|
|
g.rc_4.trim(); // yaw
|
2011-02-21 16:58:10 -04:00
|
|
|
|
2012-08-16 21:50:02 -03:00
|
|
|
g.rc_1.save_eeprom();
|
|
|
|
g.rc_2.save_eeprom();
|
|
|
|
g.rc_4.save_eeprom();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|