2012-04-30 04:17:14 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-05-13 00:16:45 -03:00
|
|
|
#include "Rover.h"
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::read_control_switch()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
2015-07-21 22:13:02 -03:00
|
|
|
static bool switch_debouncer;
|
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;
|
|
|
|
|
2014-03-08 02:12:06 -04:00
|
|
|
if (hal.scheduler->millis() - failsafe.last_valid_rc_ms > 100) {
|
|
|
|
// only use signals that are less than 0.1s old.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-30 04:17:14 -03: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).
|
|
|
|
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
|
|
|
|
2015-07-21 22:13:02 -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;
|
|
|
|
}
|
|
|
|
|
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;
|
2014-03-17 04:44:25 -03:00
|
|
|
prev_WP = current_loc;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-21 22:13:02 -03:00
|
|
|
switch_debouncer = false;
|
|
|
|
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
uint8_t Rover::readSwitch(void){
|
2013-02-07 18:21:22 -04:00
|
|
|
uint16_t pulsewidth = hal.rcin->read(g.mode_channel - 1);
|
2014-03-08 02:12:06 -04:00
|
|
|
if (pulsewidth <= 900 || 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;
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::reset_control_switch()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
2015-07-21 23:53:09 -03:00
|
|
|
oldSwitchPosition = 254;
|
2012-04-30 04:17:14 -03:00
|
|
|
read_control_switch();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CH_7_PWM_TRIGGER 1800
|
|
|
|
|
|
|
|
// read at 10 hz
|
|
|
|
// set this to your trainer switch
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::read_trim_switch()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
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) {
|
2015-10-25 16:50:51 -03:00
|
|
|
hal.console->println("Erasing waypoints");
|
2012-11-28 07:44:03 -04:00
|
|
|
// if SW7 is ON in MANUAL = Erase the Flight Plan
|
2014-03-10 05:46:06 -03:00
|
|
|
mission.clear();
|
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();
|
|
|
|
}
|
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
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2014-03-10 05:46:06 -03:00
|
|
|
// create new mission command
|
|
|
|
AP_Mission::Mission_Command cmd = {};
|
|
|
|
|
|
|
|
// set new waypoint to current location
|
|
|
|
cmd.content.location = current_loc;
|
|
|
|
|
|
|
|
// make the new command to a waypoint
|
|
|
|
cmd.id = MAV_CMD_NAV_WAYPOINT;
|
|
|
|
|
|
|
|
// save command
|
|
|
|
if(mission.add_cmd(cmd)) {
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("Learning waypoint %u", (unsigned)mission.num_commands());
|
2014-03-10 05:46:06 -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
|
|
|
}
|
|
|
|
|