Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Rover/mode_smart_rtl.cpp
Views: 1798
#include "Rover.h"12bool ModeSmartRTL::_enter()3{4// SmartRTL requires EKF (not DCM)5Location ekf_origin;6if (!ahrs.get_origin(ekf_origin)) {7return false;8}910// refuse to enter SmartRTL if smart RTL's home has not been set11if (!g2.smart_rtl.is_active()) {12return false;13}1415// initialise waypoint navigation library16g2.wp_nav.init(MAX(0, g2.rtl_speed));1718// set desired location to reasonable stopping point19if (!g2.wp_nav.set_desired_location_to_stopping_location()) {20return false;21}2223// init state24smart_rtl_state = SmartRTLState::WaitForPathCleanup;25_loitering = false;2627return true;28}2930void ModeSmartRTL::update()31{32switch (smart_rtl_state) {33case SmartRTLState::WaitForPathCleanup:34// check if return path is computed and if yes, begin journey home35if (g2.smart_rtl.request_thorough_cleanup()) {36smart_rtl_state = SmartRTLState::PathFollow;37_load_point = true;38}39// Note: this may lead to an unnecessary 20ms slow down of the vehicle (but it is unlikely)40stop_vehicle();41break;4243case SmartRTLState::PathFollow:44// load point if required45if (_load_point) {46Vector3f dest_NED;47if (!g2.smart_rtl.pop_point(dest_NED)) {48// if not more points, we have reached home49gcs().send_text(MAV_SEVERITY_INFO, "Reached destination");50smart_rtl_state = SmartRTLState::StopAtHome;51break;52} else {53// peek at the next point. this can fail if the IO task currently has the path semaphore54Vector3f next_dest_NED;55if (g2.smart_rtl.peek_point(next_dest_NED)) {56if (!g2.wp_nav.set_desired_location_NED(dest_NED, next_dest_NED)) {57// this should never happen because the EKF origin should already be set58gcs().send_text(MAV_SEVERITY_INFO, "SmartRTL: failed to set destination");59smart_rtl_state = SmartRTLState::Failure;60INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);61}62} else {63// no next point so add only immediate point64if (!g2.wp_nav.set_desired_location_NED(dest_NED)) {65// this should never happen because the EKF origin should already be set66gcs().send_text(MAV_SEVERITY_INFO, "SmartRTL: failed to set destination");67smart_rtl_state = SmartRTLState::Failure;68INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);69}70}71}72_load_point = false;73}74// update navigation controller75navigate_to_waypoint();7677// check if we've reached the next point78if (g2.wp_nav.reached_destination()) {79_load_point = true;80}81break;8283case SmartRTLState::StopAtHome:84case SmartRTLState::Failure:85_reached_destination = true;86// we have reached the destination87// boats loiters, rovers stop88if (!rover.is_boat()) {89stop_vehicle();90} else {91// if not loitering yet, start loitering92if (!_loitering) {93_loitering = rover.mode_loiter.enter();94}95if (_loitering) {96rover.mode_loiter.update();97} else {98stop_vehicle();99}100}101break;102}103}104105// get desired location106bool ModeSmartRTL::get_desired_location(Location& destination) const107{108switch (smart_rtl_state) {109case SmartRTLState::WaitForPathCleanup:110return false;111case SmartRTLState::PathFollow:112if (g2.wp_nav.is_destination_valid()) {113destination = g2.wp_nav.get_destination();114return true;115}116return false;117case SmartRTLState::StopAtHome:118case SmartRTLState::Failure:119return false;120}121// should never reach here but just in case122return false;123}124125// set desired speed in m/s126bool ModeSmartRTL::set_desired_speed(float speed)127{128return g2.wp_nav.set_speed_max(speed);129}130131// save current position for use by the smart_rtl flight mode132void ModeSmartRTL::save_position()133{134const bool save_pos = (rover.control_mode != &rover.mode_smartrtl);135g2.smart_rtl.update(true, save_pos);136}137138139