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_loiter.cpp
Views: 1798
#include "Rover.h"12bool ModeLoiter::_enter()3{4// set _destination to reasonable stopping point5if (!g2.wp_nav.get_stopping_location(_destination)) {6return false;7}89// initialise desired speed to current speed10if (!attitude_control.get_forward_speed(_desired_speed)) {11_desired_speed = 0.0f;12}1314// initialise heading to current heading15_desired_yaw_cd = ahrs.yaw_sensor;1617return true;18}1920void ModeLoiter::update()21{22// get distance (in meters) to destination23_distance_to_destination = rover.current_loc.get_distance(_destination);2425const float loiter_radius = g2.sailboat.tack_enabled() ? g2.sailboat.get_loiter_radius() : g2.loit_radius;2627// if within loiter radius slew desired speed towards zero and use existing desired heading28if (_distance_to_destination <= loiter_radius) {29// sailboats should not stop unless motoring30const float desired_speed_within_radius = g2.sailboat.tack_enabled() ? 0.1f : 0.0f;31_desired_speed = attitude_control.get_desired_speed_accel_limited(desired_speed_within_radius, rover.G_Dt);3233// if we have a sail but not trying to use it then point into the wind34if (!g2.sailboat.tack_enabled() && g2.sailboat.sail_enabled()) {35_desired_yaw_cd = degrees(g2.windvane.get_true_wind_direction_rad()) * 100.0f;36}37} else {38// P controller with hard-coded gain to convert distance to desired speed39_desired_speed = MIN((_distance_to_destination - loiter_radius) * g2.loiter_speed_gain, g2.wp_nav.get_default_speed());4041// calculate bearing to destination42_desired_yaw_cd = rover.current_loc.get_bearing_to(_destination);43float yaw_error_cd = wrap_180_cd(_desired_yaw_cd - ahrs.yaw_sensor);44// if destination is behind vehicle, reverse towards it45if ((fabsf(yaw_error_cd) > 9000 && g2.loit_type == 0) || g2.loit_type == 2) {46_desired_yaw_cd = wrap_180_cd(_desired_yaw_cd + 18000);47yaw_error_cd = wrap_180_cd(_desired_yaw_cd - ahrs.yaw_sensor);48_desired_speed = -_desired_speed;49}5051// reduce desired speed if yaw_error is large52// 45deg of error reduces speed to 75%, 90deg of error reduces speed to 50%53float yaw_error_ratio = 1.0f - constrain_float(fabsf(yaw_error_cd / 9000.0f), 0.0f, 1.0f) * 0.5f;54_desired_speed *= yaw_error_ratio;55}5657// 0 turn rate is no limit58float turn_rate = 0.0;5960// make sure sailboats don't try and sail directly into the wind61if (g2.sailboat.use_indirect_route(_desired_yaw_cd)) {62_desired_yaw_cd = g2.sailboat.calc_heading(_desired_yaw_cd);63if (g2.sailboat.tacking()) {64// use pivot turn rate for tacks65turn_rate = g2.wp_nav.get_pivot_rate();66}67}6869// run steering and throttle controllers70calc_steering_to_heading(_desired_yaw_cd, turn_rate);71calc_throttle(_desired_speed, true);72}7374// get desired location75bool ModeLoiter::get_desired_location(Location& destination) const76{77destination = _destination;78return true;79}808182