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
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::default_dead_zones()
|
2011-10-27 15:34:00 -03:00
|
|
|
{
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_roll->set_default_dead_zone(30);
|
|
|
|
channel_pitch->set_default_dead_zone(30);
|
2012-08-16 21:50:02 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->set_default_dead_zone(10);
|
|
|
|
channel_yaw->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
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->set_default_dead_zone(30);
|
|
|
|
channel_yaw->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
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::init_rc_in()
|
2010-12-19 12:40:33 -04:00
|
|
|
{
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_roll = RC_Channel::rc_channel(rcmap.roll()-1);
|
|
|
|
channel_pitch = RC_Channel::rc_channel(rcmap.pitch()-1);
|
|
|
|
channel_throttle = RC_Channel::rc_channel(rcmap.throttle()-1);
|
|
|
|
channel_yaw = RC_Channel::rc_channel(rcmap.yaw()-1);
|
|
|
|
|
2012-08-16 21:50:02 -03:00
|
|
|
// set rc channel ranges
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_roll->set_angle(ROLL_PITCH_INPUT_MAX);
|
|
|
|
channel_pitch->set_angle(ROLL_PITCH_INPUT_MAX);
|
|
|
|
channel_yaw->set_angle(4500);
|
2016-01-05 06:55:34 -04:00
|
|
|
channel_throttle->set_range(0, THR_MAX);
|
2012-08-16 21:50:02 -03:00
|
|
|
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_roll->set_type(RC_CHANNEL_TYPE_ANGLE_RAW);
|
|
|
|
channel_pitch->set_type(RC_CHANNEL_TYPE_ANGLE_RAW);
|
|
|
|
channel_yaw->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
|
2015-12-06 18:11:01 -04:00
|
|
|
g.rc_5.set_range_in(0,1000);
|
|
|
|
g.rc_6.set_range_in(0,1000);
|
|
|
|
g.rc_7.set_range_in(0,1000);
|
|
|
|
g.rc_8.set_range_in(0,1000);
|
2012-08-16 21:50:02 -03:00
|
|
|
|
2013-07-11 11:15:11 -03:00
|
|
|
// set default dead zones
|
|
|
|
default_dead_zones();
|
2014-10-08 09:52:16 -03:00
|
|
|
|
|
|
|
// initialise throttle_zero flag
|
|
|
|
ap.throttle_zero = true;
|
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
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::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
|
2015-07-15 02:35:20 -03:00
|
|
|
#if FRAME_CONFIG != HELI_FRAME
|
2015-05-14 22:04:41 -03:00
|
|
|
motors.set_throttle_range(g.throttle_min, channel_throttle->radio_min, channel_throttle->radio_max);
|
2015-07-15 03:03:14 -03:00
|
|
|
motors.set_hover_throttle(g.throttle_mid);
|
2015-07-15 02:35:20 -03:00
|
|
|
#endif
|
2012-08-16 21:50:02 -03:00
|
|
|
|
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
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->set_range_out(0,1000);
|
2012-05-31 18:59:03 -03:00
|
|
|
|
2014-09-19 10:16:31 -03:00
|
|
|
// check if we should enter esc calibration mode
|
|
|
|
esc_calibration_startup_check();
|
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) {
|
2015-04-22 15:59:34 -03:00
|
|
|
enable_motor_output();
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2014-11-20 03:32:13 -04:00
|
|
|
|
2015-06-10 03:00:18 -03:00
|
|
|
// refresh auxiliary channel to function map
|
|
|
|
RC_Channel_aux::update_aux_servo_function();
|
|
|
|
|
2014-11-20 03:32:13 -04:00
|
|
|
// setup correct scaling for ESCs like the UAVCAN PX4ESC which
|
2015-05-11 23:24:13 -03:00
|
|
|
// take a proportion of speed.
|
|
|
|
hal.rcout->set_esc_scaling(channel_throttle->radio_min, channel_throttle->radio_max);
|
2011-03-26 18:03:20 -03:00
|
|
|
}
|
2011-03-06 20:57:06 -04:00
|
|
|
|
2015-04-22 15:59:34 -03:00
|
|
|
// enable_motor_output() - enable and output lowest possible value to motors
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::enable_motor_output()
|
2011-03-26 18:03:20 -03:00
|
|
|
{
|
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
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::read_radio()
|
2010-12-26 01:25:52 -04:00
|
|
|
{
|
2014-10-07 11:01:15 -03:00
|
|
|
static uint32_t last_update_ms = 0;
|
|
|
|
uint32_t tnow_ms = millis();
|
|
|
|
|
2014-03-25 00:40:50 -03:00
|
|
|
if (hal.rcin->new_input()) {
|
2013-10-08 03:25:14 -03:00
|
|
|
ap.new_radio_frame = true;
|
2015-05-11 23:24:13 -03:00
|
|
|
RC_Channel::set_pwm_all();
|
|
|
|
|
|
|
|
set_throttle_and_failsafe(channel_throttle->radio_in);
|
|
|
|
set_throttle_zero_flag(channel_throttle->control_in);
|
2014-09-23 10:55:19 -03:00
|
|
|
|
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();
|
2016-01-06 17:39:36 -04:00
|
|
|
|
|
|
|
float dt = (tnow_ms - last_update_ms)*1.0e-3f;
|
|
|
|
rc_throttle_control_in_filter.apply(g.rc_3.control_in, dt);
|
|
|
|
last_update_ms = tnow_ms;
|
2012-11-13 05:18:21 -04:00
|
|
|
}else{
|
2014-10-07 11:01:15 -03:00
|
|
|
uint32_t elapsed = tnow_ms - last_update_ms;
|
2014-07-16 23:41:43 -03:00
|
|
|
// turn on throttle failsafe if no update from the RC Radio for 500ms or 2000ms if we are using RC_OVERRIDE
|
2014-07-21 07:13:38 -03:00
|
|
|
if (((!failsafe.rc_override_active && (elapsed >= FS_RADIO_TIMEOUT_MS)) || (failsafe.rc_override_active && (elapsed >= FS_RADIO_RC_OVERRIDE_TIMEOUT_MS))) &&
|
2014-10-24 23:15:06 -03:00
|
|
|
(g.failsafe_throttle && (ap.rc_receiver_present||motors.armed()) && !failsafe.radio)) {
|
2014-07-16 23:49:21 -03:00
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_RADIO, ERROR_CODE_RADIO_LATE_FRAME);
|
|
|
|
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
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::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) {
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->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
|
2014-10-24 23:15:06 -03:00
|
|
|
if (failsafe.radio || !(ap.rc_receiver_present || motors.armed())) {
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->set_pwm(throttle_pwm);
|
2012-11-11 09:11:12 -04:00
|
|
|
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);
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->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
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->set_pwm(throttle_pwm);
|
2012-08-16 21:50:02 -03:00
|
|
|
}
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 09:52:16 -03:00
|
|
|
#define THROTTLE_ZERO_DEBOUNCE_TIME_MS 400
|
2015-03-13 16:38:23 -03:00
|
|
|
// set_throttle_zero_flag - set throttle_zero flag from debounced throttle control
|
|
|
|
// throttle_zero is used to determine if the pilot intends to shut down the motors
|
|
|
|
// Basically, this signals when we are not flying. We are either on the ground
|
|
|
|
// or the pilot has shut down the copter in the air and it is free-falling
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::set_throttle_zero_flag(int16_t throttle_control)
|
2014-10-08 09:52:16 -03:00
|
|
|
{
|
|
|
|
static uint32_t last_nonzero_throttle_ms = 0;
|
|
|
|
uint32_t tnow_ms = millis();
|
|
|
|
|
2015-03-13 16:38:23 -03:00
|
|
|
// if not using throttle interlock and non-zero throttle and not E-stopped,
|
|
|
|
// or using motor interlock and it's enabled, then motors are running,
|
|
|
|
// and we are flying. Immediately set as non-zero
|
2015-04-22 14:56:05 -03:00
|
|
|
if ((!ap.using_interlock && (throttle_control > 0) && !ap.motor_emergency_stop) || (ap.using_interlock && motors.get_interlock())) {
|
2014-10-08 09:52:16 -03:00
|
|
|
last_nonzero_throttle_ms = tnow_ms;
|
|
|
|
ap.throttle_zero = false;
|
|
|
|
} else if (tnow_ms - last_nonzero_throttle_ms > THROTTLE_ZERO_DEBOUNCE_TIME_MS) {
|
|
|
|
ap.throttle_zero = true;
|
|
|
|
}
|
2015-03-16 01:35:57 -03:00
|
|
|
}
|