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_follow.cpp
Views: 1798
#include "Rover.h"12#if MODE_FOLLOW_ENABLED3// initialize follow mode4bool ModeFollow::_enter()5{6if (!g2.follow.enabled()) {7return false;8}910// initialise speed to waypoint speed11_desired_speed = g2.wp_nav.get_default_speed();1213return true;14}1516// exit handling17void ModeFollow::_exit()18{19g2.follow.clear_offsets_if_required();20}2122void ModeFollow::update()23{24// stop vehicle if no speed estimate25float speed;26if (!attitude_control.get_forward_speed(speed)) {27// no valid speed so stop28g2.motors.set_throttle(0.0f);29g2.motors.set_steering(0.0f);30return;31}3233Vector3f dist_vec; // vector to lead vehicle34Vector3f dist_vec_offs; // vector to lead vehicle + offset35Vector3f vel_of_target; // velocity of lead vehicle3637// if no target simply stop the vehicle38if (!g2.follow.get_target_dist_and_vel_ned(dist_vec, dist_vec_offs, vel_of_target)) {39_reached_destination = true;40stop_vehicle();41return;42}4344// calculate desired velocity vector45Vector2f desired_velocity_ne;46const float kp = g2.follow.get_pos_p().kP();47desired_velocity_ne.x = vel_of_target.x + (dist_vec_offs.x * kp);48desired_velocity_ne.y = vel_of_target.y + (dist_vec_offs.y * kp);4950// if desired velocity is zero stop vehicle51if (is_zero(desired_velocity_ne.x) && is_zero(desired_velocity_ne.y)) {52_reached_destination = true;53stop_vehicle();54return;55}5657// we have not reached the target58_reached_destination = false;5960// scale desired velocity to stay within horizontal speed limit61float desired_speed = safe_sqrt(sq(desired_velocity_ne.x) + sq(desired_velocity_ne.y));62if (!is_zero(desired_speed) && (desired_speed > _desired_speed)) {63const float scalar_xy = _desired_speed / desired_speed;64desired_velocity_ne *= scalar_xy;65desired_speed = _desired_speed;66}6768// calculate vehicle heading69const float desired_yaw_cd = wrap_180_cd(atan2f(desired_velocity_ne.y, desired_velocity_ne.x) * DEGX100);7071// run steering and throttle controllers72calc_steering_to_heading(desired_yaw_cd);73calc_throttle(desired_speed, true);74}7576// return desired heading (in degrees) for reporting to ground station (NAV_CONTROLLER_OUTPUT message)77float ModeFollow::wp_bearing() const78{79return g2.follow.get_bearing_to_target();80}8182// return distance (in meters) to destination83float ModeFollow::get_distance_to_destination() const84{85return g2.follow.get_distance_to_target();86}8788// set desired speed in m/s89bool ModeFollow::set_desired_speed(float speed)90{91if (is_negative(speed)) {92return false;93}94_desired_speed = speed;95return true;96}9798#endif // MODE_FOLLOW_ENABLED99100101