2017-07-18 23:17:45 -03:00
|
|
|
#include "mode.h"
|
|
|
|
#include "Rover.h"
|
|
|
|
|
2017-11-22 08:38:57 -04:00
|
|
|
// constructor
|
|
|
|
ModeAuto::ModeAuto(ModeRTL& mode_rtl) :
|
|
|
|
_mode_rtl(mode_rtl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:17:45 -03:00
|
|
|
bool ModeAuto::_enter()
|
|
|
|
{
|
2017-07-11 06:58:47 -03:00
|
|
|
// fail to enter auto if no mission commands
|
|
|
|
if (mission.num_commands() == 0) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE, "No Mission. Can't set AUTO.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:41:28 -04:00
|
|
|
// initialise waypoint speed
|
|
|
|
set_desired_speed_to_default();
|
|
|
|
|
2017-08-08 21:24:30 -03:00
|
|
|
// init location target
|
2017-08-09 22:30:35 -03:00
|
|
|
set_desired_location(rover.current_loc);
|
2017-08-03 05:11:45 -03:00
|
|
|
|
|
|
|
// other initialisation
|
2017-07-18 23:17:45 -03:00
|
|
|
auto_triggered = false;
|
2017-08-03 05:11:45 -03:00
|
|
|
|
2017-08-03 03:19:57 -03:00
|
|
|
// initialise reversed to be false
|
|
|
|
set_reversed(false);
|
|
|
|
|
2017-08-03 05:11:45 -03:00
|
|
|
// restart mission processing
|
|
|
|
mission.start_or_resume();
|
2017-07-18 23:17:45 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeAuto::_exit()
|
|
|
|
{
|
2017-08-03 03:19:57 -03:00
|
|
|
// stop running the mission
|
2017-07-18 23:17:45 -03:00
|
|
|
if (mission.state() == AP_Mission::MISSION_RUNNING) {
|
|
|
|
mission.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeAuto::update()
|
|
|
|
{
|
2017-08-03 05:11:45 -03:00
|
|
|
switch (_submode) {
|
|
|
|
case Auto_WP:
|
|
|
|
{
|
|
|
|
_distance_to_destination = get_distance(rover.current_loc, _destination);
|
2018-02-05 02:39:48 -04:00
|
|
|
const bool near_wp = _distance_to_destination <= rover.g.waypoint_radius;
|
2017-08-03 05:11:45 -03:00
|
|
|
// check if we've reached the destination
|
2018-02-05 02:39:48 -04:00
|
|
|
if (!_reached_destination && (near_wp || location_passed_point(rover.current_loc, _origin, _destination))) {
|
|
|
|
// trigger reached
|
|
|
|
_reached_destination = true;
|
2017-08-03 05:11:45 -03:00
|
|
|
}
|
2018-02-05 02:39:48 -04:00
|
|
|
// determine if we should keep navigating
|
|
|
|
if (!_reached_destination || (rover.is_boat() && !near_wp)) {
|
2017-08-03 05:11:45 -03:00
|
|
|
// continue driving towards destination
|
2017-12-06 22:42:34 -04:00
|
|
|
calc_steering_to_waypoint(_reached_destination ? rover.current_loc : _origin, _destination, _reversed);
|
2018-04-21 03:45:12 -03:00
|
|
|
calc_throttle(calc_reduced_speed_for_turn_or_distance(_reversed ? -_desired_speed : _desired_speed), true, false);
|
2017-08-03 05:11:45 -03:00
|
|
|
} else {
|
|
|
|
// we have reached the destination so stop
|
2017-08-10 00:06:43 -03:00
|
|
|
stop_vehicle();
|
2017-08-03 05:11:45 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Auto_HeadingAndSpeed:
|
|
|
|
{
|
|
|
|
if (!_reached_heading) {
|
|
|
|
// run steering and throttle controllers
|
2017-12-23 01:54:35 -04:00
|
|
|
calc_steering_to_heading(_desired_yaw_cd, _desired_speed < 0);
|
2018-04-21 03:45:12 -03:00
|
|
|
calc_throttle(_desired_speed, true, false);
|
2017-12-23 01:54:35 -04:00
|
|
|
// check if we have reached within 5 degrees of target
|
|
|
|
_reached_heading = (fabsf(_desired_yaw_cd - ahrs.yaw_sensor) < 500);
|
2017-08-03 05:11:45 -03:00
|
|
|
} else {
|
2017-08-10 00:06:43 -03:00
|
|
|
stop_vehicle();
|
2017-08-03 05:11:45 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-11-22 08:38:57 -04:00
|
|
|
|
|
|
|
case Auto_RTL:
|
|
|
|
_mode_rtl.update();
|
|
|
|
break;
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:32:53 -04:00
|
|
|
// return distance (in meters) to destination
|
|
|
|
float ModeAuto::get_distance_to_destination() const
|
|
|
|
{
|
|
|
|
if (_submode == Auto_RTL) {
|
|
|
|
return _mode_rtl.get_distance_to_destination();
|
|
|
|
}
|
|
|
|
return _distance_to_destination;
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:11:45 -03:00
|
|
|
// set desired location to drive to
|
2017-12-06 22:42:34 -04:00
|
|
|
void ModeAuto::set_desired_location(const struct Location& destination, float next_leg_bearing_cd)
|
2017-08-03 05:11:45 -03:00
|
|
|
{
|
|
|
|
// call parent
|
2017-08-09 22:30:35 -03:00
|
|
|
Mode::set_desired_location(destination, next_leg_bearing_cd);
|
2017-08-03 05:11:45 -03:00
|
|
|
|
|
|
|
_submode = Auto_WP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if vehicle has reached or even passed destination
|
|
|
|
bool ModeAuto::reached_destination()
|
2017-07-18 23:17:45 -03:00
|
|
|
{
|
2017-08-03 05:11:45 -03:00
|
|
|
if (_submode == Auto_WP) {
|
|
|
|
return _reached_destination;
|
|
|
|
}
|
2017-11-22 08:38:57 -04:00
|
|
|
if (_submode == Auto_RTL) {
|
|
|
|
return _mode_rtl.reached_destination();
|
|
|
|
}
|
2017-08-03 05:11:45 -03:00
|
|
|
// we should never reach here but just in case, return true to allow missions to continue
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set desired heading in centidegrees (vehicle will turn to this heading)
|
|
|
|
void ModeAuto::set_desired_heading_and_speed(float yaw_angle_cd, float target_speed)
|
|
|
|
{
|
|
|
|
// call parent
|
|
|
|
Mode::set_desired_heading_and_speed(yaw_angle_cd, target_speed);
|
|
|
|
|
|
|
|
_submode = Auto_HeadingAndSpeed;
|
|
|
|
_reached_heading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if vehicle has reached desired heading
|
|
|
|
bool ModeAuto::reached_heading()
|
|
|
|
{
|
|
|
|
if (_submode == Auto_HeadingAndSpeed) {
|
|
|
|
return _reached_heading;
|
|
|
|
}
|
|
|
|
// we should never reach here but just in case, return true to allow missions to continue
|
|
|
|
return true;
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|
|
|
|
|
2017-11-22 08:38:57 -04:00
|
|
|
// start RTL (within auto)
|
|
|
|
void ModeAuto::start_RTL()
|
|
|
|
{
|
|
|
|
if (_mode_rtl.enter()) {
|
|
|
|
_submode = Auto_RTL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 03:19:57 -03:00
|
|
|
// execute the mission in reverse (i.e. backing up)
|
|
|
|
void ModeAuto::set_reversed(bool value)
|
|
|
|
{
|
|
|
|
if (_reversed != value) {
|
|
|
|
_reversed = value;
|
|
|
|
rover.set_reverse(_reversed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:17:45 -03:00
|
|
|
/*
|
|
|
|
check for triggering of start of auto mode
|
|
|
|
*/
|
|
|
|
bool ModeAuto::check_trigger(void)
|
|
|
|
{
|
|
|
|
// check for user pressing the auto trigger to off
|
|
|
|
if (auto_triggered && g.auto_trigger_pin != -1 && rover.check_digital_pin(g.auto_trigger_pin) == 1) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "AUTO triggered off");
|
|
|
|
auto_triggered = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if already triggered, then return true, so you don't
|
|
|
|
// need to hold the switch down
|
|
|
|
if (auto_triggered) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if auto trigger and kickstart are disabled
|
|
|
|
if (g.auto_trigger_pin == -1 && is_zero(g.auto_kickstart)) {
|
|
|
|
// no trigger configured - let's go!
|
|
|
|
auto_triggered = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if trigger pin has been pushed
|
|
|
|
if (g.auto_trigger_pin != -1 && rover.check_digital_pin(g.auto_trigger_pin) == 0) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "Triggered AUTO with pin");
|
|
|
|
auto_triggered = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if mission is started by giving vehicle a kick with acceleration > AUTO_KICKSTART
|
|
|
|
if (!is_zero(g.auto_kickstart)) {
|
|
|
|
const float xaccel = rover.ins.get_accel().x;
|
|
|
|
if (xaccel >= g.auto_kickstart) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "Triggered AUTO xaccel=%.1f", static_cast<double>(xaccel));
|
|
|
|
auto_triggered = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-21 03:45:12 -03:00
|
|
|
void ModeAuto::calc_throttle(float target_speed, bool nudge_allowed, bool avoidance_enabled)
|
2017-07-18 23:17:45 -03:00
|
|
|
{
|
2017-08-03 03:19:57 -03:00
|
|
|
// If not autostarting set the throttle to minimum
|
2017-08-03 05:11:45 -03:00
|
|
|
if (!check_trigger()) {
|
2017-08-10 00:06:43 -03:00
|
|
|
stop_vehicle();
|
2017-07-18 23:17:45 -03:00
|
|
|
return;
|
|
|
|
}
|
2018-04-21 03:45:12 -03:00
|
|
|
Mode::calc_throttle(target_speed, nudge_allowed, avoidance_enabled);
|
2017-07-18 23:17:45 -03:00
|
|
|
}
|