2012-04-30 04:17:14 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
static void read_control_switch()
|
|
|
|
{
|
|
|
|
|
2012-12-18 07:44:12 -04:00
|
|
|
uint8_t switchPosition = readSwitch();
|
2012-04-30 04:17:14 -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;
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
if (oldSwitchPosition != switchPosition ||
|
|
|
|
(g.reset_switch_chan != 0 &&
|
2012-12-18 07:44:12 -04:00
|
|
|
hal.rcin->read(g.reset_switch_chan-1) > RESET_SWITCH_CHAN_PWM)) {
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2013-02-07 18:21:22 -04:00
|
|
|
set_mode((enum mode)modes[switchPosition].get());
|
2012-04-30 04:17:14 -03:00
|
|
|
|
|
|
|
oldSwitchPosition = switchPosition;
|
|
|
|
prev_WP = current_loc;
|
|
|
|
|
2013-06-16 20:50:53 -03:00
|
|
|
// reset speed integrator
|
2013-02-07 19:21:30 -04:00
|
|
|
g.pidSpeedThrottle.reset_I();
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:44:12 -04:00
|
|
|
static uint8_t readSwitch(void){
|
2013-02-07 18:21:22 -04:00
|
|
|
uint16_t pulsewidth = hal.rcin->read(g.mode_channel - 1);
|
2014-02-16 22:56:40 -04:00
|
|
|
if (pulsewidth <= 800 || pulsewidth >= 2200) return 255; // This is an error condition
|
2012-04-30 04:17:14 -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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_control_switch()
|
|
|
|
{
|
|
|
|
oldSwitchPosition = 0;
|
|
|
|
read_control_switch();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CH_7_PWM_TRIGGER 1800
|
|
|
|
|
|
|
|
// read at 10 hz
|
|
|
|
// set this to your trainer switch
|
|
|
|
static void read_trim_switch()
|
|
|
|
{
|
2013-02-07 18:21:22 -04:00
|
|
|
switch ((enum ch7_option)g.ch7_option.get()) {
|
|
|
|
case CH7_DO_NOTHING:
|
|
|
|
break;
|
|
|
|
case CH7_SAVE_WP:
|
2013-06-30 21:10:38 -03:00
|
|
|
if (channel_learn->radio_in > CH_7_PWM_TRIGGER) {
|
2013-02-07 18:21:22 -04:00
|
|
|
// switch is engaged
|
|
|
|
ch7_flag = true;
|
2012-11-28 07:44:03 -04:00
|
|
|
} else { // switch is disengaged
|
2013-02-07 18:21:22 -04:00
|
|
|
if (ch7_flag) {
|
|
|
|
ch7_flag = false;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
if (control_mode == MANUAL) {
|
2013-02-07 18:21:22 -04:00
|
|
|
hal.console->println_P(PSTR("Erasing waypoints"));
|
2012-11-28 07:44:03 -04:00
|
|
|
// if SW7 is ON in MANUAL = Erase the Flight Plan
|
2012-05-02 08:57:19 -03:00
|
|
|
g.command_total.set_and_save(CH7_wp_index);
|
2012-11-28 07:44:03 -04:00
|
|
|
g.command_total = 0;
|
|
|
|
g.command_index =0;
|
|
|
|
nav_command_index = 0;
|
2013-06-03 06:33:59 -03:00
|
|
|
if (channel_steer->control_in > 3000) {
|
2013-02-07 18:21:22 -04:00
|
|
|
// if roll is full right store the current location as home
|
|
|
|
init_home();
|
|
|
|
}
|
|
|
|
CH7_wp_index = 1;
|
2012-04-30 04:17:14 -03:00
|
|
|
return;
|
2013-03-01 07:32:57 -04:00
|
|
|
} else if (control_mode == LEARNING || control_mode == STEERING) {
|
2012-11-28 07:44:03 -04:00
|
|
|
// if SW7 is ON in LEARNING = record the Wp
|
|
|
|
// set the next_WP (home is stored at 0)
|
2013-02-07 18:21:22 -04:00
|
|
|
|
|
|
|
hal.console->printf_P(PSTR("Learning waypoint %u"), (unsigned)CH7_wp_index);
|
2012-11-28 07:44:03 -04:00
|
|
|
current_loc.id = MAV_CMD_NAV_WAYPOINT;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
// store the index
|
|
|
|
g.command_total.set_and_save(CH7_wp_index);
|
|
|
|
g.command_total = CH7_wp_index;
|
|
|
|
g.command_index = CH7_wp_index;
|
|
|
|
nav_command_index = 0;
|
2012-05-02 08:57:19 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
// save command
|
|
|
|
set_cmd_with_index(current_loc, CH7_wp_index);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
// increment index
|
|
|
|
CH7_wp_index++;
|
2013-02-07 18:21:22 -04:00
|
|
|
CH7_wp_index = constrain_int16(CH7_wp_index, 1, MAX_WAYPOINTS);
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2012-11-28 07:44:03 -04:00
|
|
|
} else if (control_mode == AUTO) {
|
|
|
|
// if SW7 is ON in AUTO = set to RTL
|
|
|
|
set_mode(RTL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 18:21:22 -04:00
|
|
|
break;
|
2012-11-28 07:44:03 -04:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|