2015-05-13 00:16:45 -03:00
|
|
|
#include "Rover.h"
|
|
|
|
|
2017-02-20 10:26:18 -04:00
|
|
|
static const int16_t CH_7_PWM_TRIGGER = 1800;
|
|
|
|
|
2017-07-18 23:19:08 -03:00
|
|
|
Mode *Rover::control_mode_from_num(const enum mode num)
|
|
|
|
{
|
|
|
|
Mode *ret = nullptr;
|
|
|
|
switch (num) {
|
|
|
|
case MANUAL:
|
|
|
|
ret = &mode_manual;
|
|
|
|
break;
|
|
|
|
case STEERING:
|
|
|
|
ret = &mode_steering;
|
|
|
|
break;
|
|
|
|
case HOLD:
|
|
|
|
ret = &mode_hold;
|
|
|
|
break;
|
|
|
|
case AUTO:
|
|
|
|
ret = &mode_auto;
|
|
|
|
break;
|
|
|
|
case RTL:
|
|
|
|
ret = &mode_rtl;
|
|
|
|
break;
|
|
|
|
case GUIDED:
|
|
|
|
ret = &mode_guided;
|
|
|
|
break;
|
|
|
|
case INITIALISING:
|
|
|
|
ret = &mode_initializing;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
2017-01-31 06:12:26 -04:00
|
|
|
const uint8_t switchPosition = readSwitch();
|
2016-12-20 09:32:57 -04: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;
|
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2015-11-19 23:04:16 -04:00
|
|
|
if (AP_HAL::millis() - failsafe.last_valid_rc_ms > 100) {
|
2014-03-08 02:12:06 -04:00
|
|
|
// 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).
|
2016-12-20 09:32:57 -04:00
|
|
|
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)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:19:08 -03:00
|
|
|
Mode *new_mode = control_mode_from_num((enum mode)modes[switchPosition].get());
|
|
|
|
if (new_mode != nullptr) {
|
2017-07-24 14:05:59 -03:00
|
|
|
set_mode(*new_mode, MODE_REASON_TX_COMMAND);
|
2017-07-18 23:19:08 -03:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
oldSwitchPosition = switchPosition;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
uint8_t Rover::readSwitch(void) {
|
2017-01-31 06:12:26 -04:00
|
|
|
const uint16_t pulsewidth = hal.rcin->read(g.mode_channel - 1);
|
2016-12-20 09:32:57 -04:00
|
|
|
if (pulsewidth <= 900 || pulsewidth >= 2200) {
|
|
|
|
return 255; // This is an error condition
|
|
|
|
}
|
|
|
|
if (pulsewidth <= 1230) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pulsewidth <= 1360) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (pulsewidth <= 1490) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (pulsewidth <= 1620) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (pulsewidth <= 1749) {
|
|
|
|
return 4; // Software Manual
|
|
|
|
}
|
|
|
|
return 5; // Hardware Manual
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
2015-05-12 02:03:23 -03:00
|
|
|
void Rover::reset_control_switch()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
2016-12-20 09:32:57 -04:00
|
|
|
oldSwitchPosition = 254;
|
|
|
|
read_control_switch();
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
2017-08-22 04:06:23 -03:00
|
|
|
// ready auxiliary switch's position
|
|
|
|
aux_switch_pos Rover::read_aux_switch_pos()
|
2012-04-30 04:17:14 -03:00
|
|
|
{
|
2017-08-24 08:32:58 -03:00
|
|
|
const uint16_t radio_in = channel_aux->get_radio_in();
|
2017-08-22 04:06:23 -03:00
|
|
|
if (radio_in < AUX_SWITCH_PWM_TRIGGER_LOW) return AUX_SWITCH_LOW;
|
|
|
|
if (radio_in > AUX_SWITCH_PWM_TRIGGER_HIGH) return AUX_SWITCH_HIGH;
|
|
|
|
return AUX_SWITCH_MIDDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise position of auxiliary switch
|
|
|
|
void Rover::init_aux_switch()
|
|
|
|
{
|
|
|
|
aux_ch7 = read_aux_switch_pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ch7 aux switch
|
|
|
|
void Rover::read_aux_switch()
|
|
|
|
{
|
|
|
|
// do not consume input during rc or throttle failsafe
|
2017-08-24 08:32:58 -03:00
|
|
|
if ((failsafe.bits & FAILSAFE_EVENT_THROTTLE) || (failsafe.bits & FAILSAFE_EVENT_RC)) {
|
2017-08-22 04:06:23 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ch7's current position
|
|
|
|
aux_switch_pos aux_ch7_pos = read_aux_switch_pos();
|
|
|
|
|
|
|
|
// return if no change to switch position
|
|
|
|
if (aux_ch7_pos == aux_ch7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aux_ch7 = aux_ch7_pos;
|
|
|
|
|
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:
|
2017-08-22 04:06:23 -03:00
|
|
|
if (aux_ch7 == AUX_SWITCH_HIGH) {
|
|
|
|
// do nothing if in AUTO mode
|
|
|
|
if (control_mode == &mode_auto) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if disarmed clear mission and set home to current location
|
|
|
|
if (!arming.is_armed()) {
|
|
|
|
mission.clear();
|
|
|
|
set_home_to_current_location(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// record the waypoint if in manual or steering modes
|
|
|
|
if (control_mode == &mode_manual || control_mode == &mode_steering) {
|
|
|
|
// 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)) {
|
|
|
|
hal.console->printf("Added waypoint %u", static_cast<uint32_t>(mission.num_commands()));
|
2012-11-28 07:44:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 18:21:22 -04:00
|
|
|
break;
|
2017-08-22 08:53:36 -03:00
|
|
|
|
|
|
|
// learn cruise speed and throttle
|
|
|
|
case CH7_LEARN_CRUISE:
|
|
|
|
if (aux_ch7 == AUX_SWITCH_HIGH) {
|
|
|
|
cruise_learn_start();
|
|
|
|
} else if (aux_ch7 == AUX_SWITCH_LOW) {
|
|
|
|
cruise_learn_complete();
|
|
|
|
}
|
|
|
|
break;
|
2012-11-28 07:44:03 -04:00
|
|
|
}
|
2012-04-30 04:17:14 -03:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:39:16 -04:00
|
|
|
// return true if motors are moving
|
2015-10-30 02:57:17 -03:00
|
|
|
bool Rover::motor_active()
|
|
|
|
{
|
2017-11-22 04:39:16 -04:00
|
|
|
// if soft disarmed, motors not active
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check throttle is active
|
|
|
|
if (!is_zero(g2.motors.get_throttle())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skid-steering vehicles active when steering
|
|
|
|
if (g2.motors.have_skid_steering() && !is_zero(g2.motors.get_steering())) {
|
|
|
|
return true;
|
2015-10-30 02:57:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|