2011-09-08 22:29:39 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2012-08-01 00:59:20 -03:00
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
static void read_control_switch()
|
|
|
|
{
|
2012-08-16 21:50:15 -03:00
|
|
|
static bool switch_debouncer;
|
2012-12-04 18:22:21 -04:00
|
|
|
uint8_t switchPosition = readSwitch();
|
2012-08-16 21:50:15 -03:00
|
|
|
|
|
|
|
// If switchPosition = 255 this indicates that the mode control channel input was out of range
|
|
|
|
// If we get this value we do not want to change modes.
|
|
|
|
if(switchPosition == 255) return;
|
2011-12-18 18:42:32 -04:00
|
|
|
|
2013-07-19 01:12:25 -03:00
|
|
|
if (failsafe.ch3_failsafe || failsafe.ch3_counter > 0) {
|
2013-02-20 20:21:39 -04:00
|
|
|
// when we are in ch3_failsafe mode then RC input is not
|
|
|
|
// working, and we need to ignore the mode switch channel
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 01:49:57 -04:00
|
|
|
if (hal.scheduler->millis() - failsafe.last_valid_rc_ms > 100) {
|
|
|
|
// only use signals that are less than 0.1s old.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-18 18:42:32 -04:00
|
|
|
// we look for changes in the switch position. If the
|
|
|
|
// RST_SWITCH_CH parameter is set, then it is a switch that can be
|
|
|
|
// used to force re-reading of the control switch. This is useful
|
|
|
|
// when returning to the previous mode after a failsafe or fence
|
|
|
|
// breach. This channel is best used on a momentary switch (such
|
|
|
|
// as a spring loaded trainer switch).
|
2012-08-16 21:50:15 -03:00
|
|
|
if (oldSwitchPosition != switchPosition ||
|
|
|
|
(g.reset_switch_chan != 0 &&
|
2012-12-04 18:22:21 -04:00
|
|
|
hal.rcin->read(g.reset_switch_chan-1) > RESET_SWITCH_CHAN_PWM)) {
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-01 00:59:20 -03:00
|
|
|
if (switch_debouncer == false) {
|
|
|
|
// this ensures that mode switches only happen if the
|
|
|
|
// switch changes for 2 reads. This prevents momentary
|
|
|
|
// spikes in the mode control channel from causing a mode
|
|
|
|
// switch
|
|
|
|
switch_debouncer = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-30 17:15:48 -04:00
|
|
|
set_mode((enum FlightMode)(flight_modes[switchPosition].get()));
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-16 21:50:15 -03:00
|
|
|
oldSwitchPosition = switchPosition;
|
2014-03-16 01:53:10 -03:00
|
|
|
prev_WP_loc = current_loc;
|
2012-08-16 21:50:15 -03:00
|
|
|
}
|
2011-09-08 22:29:39 -03:00
|
|
|
|
2012-08-16 21:50:15 -03:00
|
|
|
if (g.reset_mission_chan != 0 &&
|
2012-12-04 18:22:21 -04:00
|
|
|
hal.rcin->read(g.reset_mission_chan-1) > RESET_SWITCH_CHAN_PWM) {
|
2014-03-13 23:06:40 -03:00
|
|
|
mission.start();
|
2014-03-16 01:53:10 -03:00
|
|
|
prev_WP_loc = current_loc;
|
2012-08-08 21:33:16 -03:00
|
|
|
}
|
|
|
|
|
2012-08-01 00:59:20 -03:00
|
|
|
switch_debouncer = false;
|
|
|
|
|
2011-09-08 22:29:39 -03:00
|
|
|
if (g.inverted_flight_ch != 0) {
|
|
|
|
// if the user has configured an inverted flight channel, then
|
|
|
|
// fly upside down when that channel goes above INVERTED_FLIGHT_PWM
|
2012-12-04 18:22:21 -04:00
|
|
|
inverted_flight = (control_mode != MANUAL && hal.rcin->read(g.inverted_flight_ch-1) > INVERTED_FLIGHT_PWM);
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 01:49:57 -04:00
|
|
|
static uint8_t readSwitch(void)
|
|
|
|
{
|
2012-12-04 18:22:21 -04:00
|
|
|
uint16_t pulsewidth = hal.rcin->read(g.flight_mode_channel - 1);
|
2014-03-08 01:49:57 -04:00
|
|
|
if (pulsewidth <= 900 || pulsewidth >= 2200) return 255; // This is an error condition
|
2012-08-16 21:50:15 -03:00
|
|
|
if (pulsewidth > 1230 && pulsewidth <= 1360) return 1;
|
|
|
|
if (pulsewidth > 1360 && pulsewidth <= 1490) return 2;
|
|
|
|
if (pulsewidth > 1490 && pulsewidth <= 1620) return 3;
|
|
|
|
if (pulsewidth > 1620 && pulsewidth <= 1749) return 4; // Software Manual
|
|
|
|
if (pulsewidth >= 1750) return 5; // Hardware Manual
|
|
|
|
return 0;
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_control_switch()
|
|
|
|
{
|
2013-12-16 23:13:32 -04:00
|
|
|
oldSwitchPosition = 254;
|
2012-08-16 21:50:15 -03:00
|
|
|
read_control_switch();
|
2011-09-08 22:29:39 -03:00
|
|
|
}
|
|
|
|
|