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 LEARNING:
|
|
|
|
ret = &mode_learning;
|
|
|
|
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;
|
|
|
|
prev_WP = current_loc;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
// reset speed integrator
|
2013-02-07 19:21:30 -04:00
|
|
|
g.pidSpeedThrottle.reset_I();
|
2016-12-20 09:32:57 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2016-12-20 09:32:57 -04:00
|
|
|
if (channel_learn->get_radio_in() > CH_7_PWM_TRIGGER) {
|
2013-02-07 18:21:22 -04:00
|
|
|
// switch is engaged
|
2016-12-20 09:32:57 -04:00
|
|
|
ch7_flag = true;
|
|
|
|
} else { // switch is disengaged
|
|
|
|
if (ch7_flag) {
|
|
|
|
ch7_flag = false;
|
2012-04-30 04:17:14 -03:00
|
|
|
|
2017-07-18 23:19:08 -03:00
|
|
|
if (control_mode == &mode_manual) {
|
2017-01-21 00:33:27 -04:00
|
|
|
hal.console->printf("Erasing waypoints\n");
|
2012-11-28 07:44:03 -04:00
|
|
|
// if SW7 is ON in MANUAL = Erase the Flight Plan
|
2016-12-20 09:32:57 -04:00
|
|
|
mission.clear();
|
APMRover2: Fix up after refactoring RC_Channel class
Further to refactor of RC_Channel class which included
adding get_xx set_xx methods, change reads and writes to the public members
to calls to get and set functionsss
old public member(int16_t) get function -> int16_t set function (int16_t)
(expression where c is an object of type RC_Channel)
c.radio_in c.get_radio_in() c.set_radio_in(v)
c.control_in c.get_control_in() c.set_control_in(v)
c.servo_out c.get_servo_out() c.set_servo_out(v)
c.pwm_out c.get_pwm_out() // use existing
c.radio_out c.get_radio_out() c.set_radio_out(v)
c.radio_max c.get_radio_max() c.set_radio_max(v)
c.radio_min c.get_radio_min() c.set_radio_min(v)
c.radio_trim c.get_radio_trim() c.set_radio_trim(v);
c.min_max_configured() // return true if min and max are configured
Because data members of RC_Channels are now private and so cannot be written directly
some overloads are provided in the Plane classes to provide the old functionality
new overload Plane::stick_mix_channel(RC_Channel *channel)
which forwards to the previously existing
void stick_mix_channel(RC_Channel *channel, int16_t &servo_out);
new overload Plane::channel_output_mixer(Rc_Channel* , RC_Channel*)const
which forwards to
(uint8_t mixing_type, int16_t & chan1, int16_t & chan2)const;
Rename functions
RC_Channel_aux::set_radio_trim(Aux_servo_function_t function)
to RC_Channel_aux::set_trim_to_radio_in_for(Aux_servo_function_t function)
RC_Channel_aux::set_servo_out(Aux_servo_function_t function, int16_t value)
to RC_Channel_aux::set_servo_out_for(Aux_servo_function_t function, int16_t value)
Rationale:
RC_Channel is a complicated class, which combines
several functionalities dealing with stick inputs
in pwm and logical units, logical and actual actuator
outputs, unit conversion etc, etc
The intent of this PR is to clarify existing use of
the class. At the basic level it should now be possible
to grep all places where private variable is set by
searching for the set_xx function.
(The wider purpose is to provide a more generic and
logically simpler method of output mixing. This is a small step)
2016-05-08 05:49:39 -03:00
|
|
|
if (channel_steer->get_control_in() > 3000) {
|
2016-12-20 09:32:57 -04:00
|
|
|
// if roll is full right store the current location as home
|
2017-06-05 04:55:24 -03:00
|
|
|
set_home_to_current_location(false);
|
2013-02-07 18:21:22 -04:00
|
|
|
}
|
2017-07-18 23:19:08 -03:00
|
|
|
} else if (control_mode == &mode_learning || control_mode == &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
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
// create new mission command
|
|
|
|
AP_Mission::Mission_Command cmd = {};
|
2014-03-10 05:46:06 -03:00
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
// set new waypoint to current location
|
|
|
|
cmd.content.location = current_loc;
|
2014-03-10 05:46:06 -03:00
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
// make the new command to a waypoint
|
|
|
|
cmd.id = MAV_CMD_NAV_WAYPOINT;
|
2016-12-23 05:49:39 -04:00
|
|
|
|
2016-12-20 09:32:57 -04:00
|
|
|
// save command
|
|
|
|
if (mission.add_cmd(cmd)) {
|
2017-03-30 11:07:24 -03:00
|
|
|
hal.console->printf("Learning waypoint %u", static_cast<uint32_t>(mission.num_commands()));
|
2016-12-20 09:32:57 -04:00
|
|
|
}
|
2017-07-18 23:19:08 -03:00
|
|
|
} else if (control_mode == &mode_auto) {
|
2016-12-20 09:32:57 -04:00
|
|
|
// if SW7 is ON in AUTO = set to RTL
|
2017-07-24 14:05:59 -03:00
|
|
|
set_mode(mode_rtl, MODE_REASON_TX_COMMAND);
|
2016-12-20 09:32:57 -04:00
|
|
|
break;
|
2012-11-28 07:44:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-10-30 02:57:17 -03:00
|
|
|
bool Rover::motor_active()
|
|
|
|
{
|
2017-03-14 21:56:33 -03:00
|
|
|
// Check if armed and output throttle servo is not neutral
|
2015-10-30 02:57:17 -03:00
|
|
|
if (hal.util->get_soft_armed()) {
|
2017-07-06 00:06:20 -03:00
|
|
|
if (!is_zero(g2.motors.get_throttle())) {
|
2015-10-30 02:57:17 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|