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
|
|
|
|
|
|
|
//Function that will read the radio data, limit servos and trigger a failsafe
|
|
|
|
// ----------------------------------------------------------------------------
|
2012-01-11 03:38:34 -04:00
|
|
|
static int8_t failsafeCounter = 0; // we wait a second to take over the throttle and send the plane circling
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2012-08-04 13:39:20 -03:00
|
|
|
|
2012-08-04 14:44:41 -03:00
|
|
|
extern RC_Channel* rc_ch[NUM_CHANNELS];
|
2012-08-04 13:39:20 -03:00
|
|
|
|
2011-10-27 15:34:00 -03:00
|
|
|
static void default_dead_zones()
|
|
|
|
{
|
|
|
|
g.rc_1.set_dead_zone(60);
|
|
|
|
g.rc_2.set_dead_zone(60);
|
2011-11-06 05:37:59 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2011-11-12 10:23:07 -04:00
|
|
|
g.rc_3.set_dead_zone(20);
|
|
|
|
g.rc_4.set_dead_zone(30);
|
2011-11-06 05:37:59 -04:00
|
|
|
#else
|
|
|
|
g.rc_3.set_dead_zone(60);
|
2012-02-11 01:57:22 -04:00
|
|
|
g.rc_4.set_dead_zone(80);
|
2011-11-06 05:37:59 -04:00
|
|
|
#endif
|
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
|
|
|
{
|
2011-02-25 01:33:39 -04:00
|
|
|
// set rc channel ranges
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_1.set_angle(4500);
|
|
|
|
g.rc_2.set_angle(4500);
|
2012-02-19 11:24:10 -04:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
// we do not want to limit the movment of the heli's swash plate
|
|
|
|
g.rc_3.set_range(0, 1000);
|
|
|
|
#else
|
2012-06-20 19:00:53 -03:00
|
|
|
g.rc_3.set_range(g.throttle_min, g.throttle_max);
|
2012-02-19 11:24:10 -04:00
|
|
|
#endif
|
2011-04-21 02:15:45 -03:00
|
|
|
g.rc_4.set_angle(4500);
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-07-12 17:04:15 -03:00
|
|
|
// reverse: CW = left
|
|
|
|
// normal: CW = left???
|
|
|
|
|
2011-05-05 14:47:11 -03:00
|
|
|
g.rc_1.set_type(RC_CHANNEL_ANGLE_RAW);
|
|
|
|
g.rc_2.set_type(RC_CHANNEL_ANGLE_RAW);
|
|
|
|
g.rc_4.set_type(RC_CHANNEL_ANGLE_RAW);
|
|
|
|
|
2012-08-04 13:39:20 -03:00
|
|
|
rc_ch[CH_1] = &g.rc_1;
|
|
|
|
rc_ch[CH_2] = &g.rc_2;
|
|
|
|
rc_ch[CH_3] = &g.rc_3;
|
|
|
|
rc_ch[CH_4] = &g.rc_4;
|
|
|
|
rc_ch[CH_5] = &g.rc_5;
|
|
|
|
rc_ch[CH_6] = &g.rc_6;
|
|
|
|
rc_ch[CH_7] = &g.rc_7;
|
|
|
|
rc_ch[CH_8] = &g.rc_8;
|
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
//set auxiliary ranges
|
2012-07-15 04:37:40 -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);
|
2012-07-18 11:49:41 -03:00
|
|
|
|
|
|
|
#if MOUNT == ENABLED
|
2012-08-06 18:33:51 -03:00
|
|
|
update_aux_servo_function(&g.rc_9, &g.rc_10, &g.rc_11);
|
2012-07-18 11:49:41 -03:00
|
|
|
#endif
|
2010-12-27 19:09:08 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void init_rc_out()
|
2010-12-27 19:09:08 -04:00
|
|
|
{
|
2011-11-12 23:47:42 -04:00
|
|
|
APM_RC.Init( &isr_registry ); // APM Radio initialization
|
2012-08-04 13:39:20 -03:00
|
|
|
|
2012-04-04 11:00:56 -03:00
|
|
|
#if INSTANT_PWM == 1
|
|
|
|
motors.set_update_rate(AP_MOTORS_SPEED_INSTANT_PWM);
|
|
|
|
#else
|
|
|
|
motors.set_update_rate(g.rc_speed);
|
|
|
|
#endif
|
2012-08-09 20:56:29 -03:00
|
|
|
|
2012-04-04 11:00:56 -03:00
|
|
|
motors.set_frame_orientation(g.frame_orientation);
|
|
|
|
motors.Init(); // motor initialisation
|
2012-06-20 19:00:53 -03:00
|
|
|
motors.set_min_throttle(g.throttle_min);
|
|
|
|
motors.set_max_throttle(g.throttle_max);
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-26 18:03:20 -03:00
|
|
|
for(byte i = 0; i < 5; i++){
|
2011-03-06 20:57:06 -04:00
|
|
|
delay(20);
|
|
|
|
read_radio();
|
|
|
|
}
|
|
|
|
|
2012-05-31 18:59:03 -03:00
|
|
|
// we want the input to be scaled correctly
|
|
|
|
g.rc_3.set_range_out(0,1000);
|
|
|
|
|
2011-11-22 02:12:19 -04:00
|
|
|
// sanity check - prevent unconfigured radios from outputting
|
2011-03-26 18:03:20 -03:00
|
|
|
if(g.rc_3.radio_min >= 1300){
|
|
|
|
g.rc_3.radio_min = g.rc_3.radio_in;
|
|
|
|
}
|
2011-11-22 02:12:19 -04:00
|
|
|
|
|
|
|
// we are full throttle
|
2012-02-19 01:11:06 -04:00
|
|
|
if(g.rc_3.control_in >= (MAXIMUM_THROTTLE - 50)){
|
2011-11-22 02:12:19 -04:00
|
|
|
if(g.esc_calibrate == 0){
|
|
|
|
// we will enter esc_calibrate mode on next reboot
|
|
|
|
g.esc_calibrate.set_and_save(1);
|
|
|
|
// send miinimum throttle out to ESC
|
2012-04-04 11:00:56 -03:00
|
|
|
motors.output_min();
|
2011-11-22 02:12:19 -04:00
|
|
|
// block until we restart
|
|
|
|
while(1){
|
|
|
|
//Serial.println("esc");
|
|
|
|
delay(200);
|
|
|
|
dancing_light();
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//Serial.println("esc init");
|
|
|
|
// clear esc flag
|
|
|
|
g.esc_calibrate.set_and_save(0);
|
|
|
|
// block until we restart
|
|
|
|
init_esc();
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// did we abort the calibration?
|
|
|
|
if(g.esc_calibrate == 1)
|
|
|
|
g.esc_calibrate.set_and_save(0);
|
|
|
|
|
|
|
|
// send miinimum throttle out to ESC
|
|
|
|
output_min();
|
|
|
|
}
|
2012-08-05 18:08:31 -03:00
|
|
|
|
|
|
|
enable_aux_servos();
|
2012-08-09 20:56:29 -03:00
|
|
|
|
|
|
|
#if TOY_EDF == ENABLED
|
|
|
|
// add access to CH8 and CH6
|
|
|
|
APM_RC.enable_out(CH_8);
|
|
|
|
APM_RC.enable_out(CH_6);
|
|
|
|
#endif
|
2011-03-26 18:03:20 -03:00
|
|
|
}
|
2011-03-06 20:57:06 -04:00
|
|
|
|
2011-03-26 18:03:20 -03:00
|
|
|
void output_min()
|
|
|
|
{
|
2012-07-15 04:37:40 -03:00
|
|
|
// enable motors
|
2012-04-04 11:00:56 -03:00
|
|
|
motors.enable();
|
|
|
|
motors.output_min();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-07-17 07:32:00 -03:00
|
|
|
static void read_radio()
|
2010-12-26 01:25:52 -04:00
|
|
|
{
|
2011-06-16 14:03:26 -03:00
|
|
|
if (APM_RC.GetState() == 1){
|
2011-09-16 03:33:00 -03:00
|
|
|
new_radio_frame = true;
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_1.set_pwm(APM_RC.InputCh(CH_1));
|
|
|
|
g.rc_2.set_pwm(APM_RC.InputCh(CH_2));
|
|
|
|
g.rc_3.set_pwm(APM_RC.InputCh(CH_3));
|
|
|
|
g.rc_4.set_pwm(APM_RC.InputCh(CH_4));
|
|
|
|
g.rc_5.set_pwm(APM_RC.InputCh(CH_5));
|
|
|
|
g.rc_6.set_pwm(APM_RC.InputCh(CH_6));
|
|
|
|
g.rc_7.set_pwm(APM_RC.InputCh(CH_7));
|
|
|
|
g.rc_8.set_pwm(APM_RC.InputCh(CH_8));
|
|
|
|
|
|
|
|
#if FRAME_CONFIG != HELI_FRAME
|
|
|
|
// limit our input to 800 so we can still pitch and roll
|
2012-02-19 01:11:06 -04:00
|
|
|
g.rc_3.control_in = min(g.rc_3.control_in, MAXIMUM_THROTTLE);
|
2011-06-16 14:03:26 -03:00
|
|
|
#endif
|
|
|
|
|
2011-11-26 17:18:40 -04:00
|
|
|
throttle_failsafe(g.rc_3.radio_in);
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2012-02-10 02:20:20 -04:00
|
|
|
#define FS_COUNTER 3
|
2011-07-17 07:32:00 -03:00
|
|
|
static void throttle_failsafe(uint16_t pwm)
|
2011-02-24 01:56:59 -04:00
|
|
|
{
|
2012-01-06 14:21:23 -04:00
|
|
|
// Don't enter Failsafe if not enabled by user
|
2011-02-24 01:56:59 -04:00
|
|
|
if(g.throttle_fs_enabled == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//check for failsafe and debounce funky reads
|
|
|
|
// ------------------------------------------
|
2011-07-17 07:30:21 -03:00
|
|
|
if (pwm < (unsigned)g.throttle_fs_value){
|
2011-02-24 01:56:59 -04:00
|
|
|
// we detect a failsafe from radio
|
|
|
|
// throttle has dropped below the mark
|
|
|
|
failsafeCounter++;
|
2012-02-10 02:20:20 -04:00
|
|
|
if (failsafeCounter == FS_COUNTER-1){
|
2012-05-29 15:16:26 -03:00
|
|
|
// called right before trigger
|
|
|
|
// do nothing
|
2012-02-10 02:20:20 -04:00
|
|
|
}else if(failsafeCounter == FS_COUNTER) {
|
2012-01-06 14:21:23 -04:00
|
|
|
// Don't enter Failsafe if we are not armed
|
2012-01-09 20:55:51 -04:00
|
|
|
// home distance is in meters
|
|
|
|
// This is to prevent accidental RTL
|
2012-05-29 15:24:41 -03:00
|
|
|
if(motors.armed() && takeoff_complete){
|
2012-01-09 20:55:51 -04:00
|
|
|
SendDebug("MSG FS ON ");
|
|
|
|
SendDebugln(pwm, DEC);
|
2012-01-06 14:21:23 -04:00
|
|
|
set_failsafe(true);
|
2012-01-09 20:55:51 -04:00
|
|
|
}
|
2012-02-10 02:20:20 -04:00
|
|
|
}else if (failsafeCounter > FS_COUNTER){
|
|
|
|
failsafeCounter = FS_COUNTER+1;
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}else if(failsafeCounter > 0){
|
|
|
|
// we are no longer in failsafe condition
|
|
|
|
// but we need to recover quickly
|
|
|
|
failsafeCounter--;
|
|
|
|
if (failsafeCounter > 3){
|
|
|
|
failsafeCounter = 3;
|
|
|
|
}
|
|
|
|
if (failsafeCounter == 1){
|
|
|
|
SendDebug("MSG FS OFF ");
|
|
|
|
SendDebugln(pwm, DEC);
|
|
|
|
}else if(failsafeCounter == 0) {
|
2011-11-26 17:18:40 -04:00
|
|
|
set_failsafe(false);
|
2011-02-24 01:56:59 -04:00
|
|
|
}else if (failsafeCounter <0){
|
|
|
|
failsafeCounter = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 07:32:00 -03:00
|
|
|
static void trim_radio()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2010-12-27 21:45:43 -04:00
|
|
|
for (byte i = 0; i < 30; i++){
|
2010-12-26 01:25:52 -04:00
|
|
|
read_radio();
|
|
|
|
}
|
2011-02-21 16:58:10 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
g.rc_1.trim(); // roll
|
|
|
|
g.rc_2.trim(); // pitch
|
|
|
|
g.rc_4.trim(); // yaw
|
2011-02-21 16:58:10 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
g.rc_1.save_eeprom();
|
|
|
|
g.rc_2.save_eeprom();
|
|
|
|
g.rc_4.save_eeprom();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|