From 7b1e34d219eaa559ef85c015a8f453cfa4f8a07d Mon Sep 17 00:00:00 2001 From: bnsgeyer Date: Mon, 11 Nov 2019 09:53:16 -0500 Subject: [PATCH] AC_WPNAV:make speed changes during missions obey WPNAV_ACCEL Includes commits by rmackay9 AC_WPNav: fixup max speed acceleration AC_WPNav: simplify the initialisation of poscontrol's max speed Changed at Leonard's request to keep things simpler --- libraries/AC_WPNav/AC_WPNav.cpp | 45 ++++++++++++++++++++++++++++++--- libraries/AC_WPNav/AC_WPNav.h | 4 +++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/libraries/AC_WPNav/AC_WPNav.cpp b/libraries/AC_WPNav/AC_WPNav.cpp index 8ff6bba6ae..664ce4354c 100644 --- a/libraries/AC_WPNav/AC_WPNav.cpp +++ b/libraries/AC_WPNav/AC_WPNav.cpp @@ -118,6 +118,9 @@ void AC_WPNav::wp_and_spline_init() // initialise feed forward velocity to zero _pos_control.set_desired_velocity_xy(0.0f, 0.0f); + // initialize the desired wp speed if not already done + _wp_desired_speed_xy_cms = _wp_speed_cms; + // initialise position controller speed and acceleration _pos_control.set_max_speed_xy(_wp_speed_cms); _pos_control.set_max_accel_xy(_wp_accel_cmss); @@ -133,11 +136,9 @@ void AC_WPNav::wp_and_spline_init() /// set_speed_xy - allows main code to pass target horizontal velocity for wp navigation void AC_WPNav::set_speed_xy(float speed_cms) { - // range check new target speed and update position controller + // range check target speed if (speed_cms >= WPNAV_WP_SPEED_MIN) { - _pos_control.set_max_speed_xy(speed_cms); - // flag that wp leash must be recalculated - _flags.recalc_wp_leash = true; + _wp_desired_speed_xy_cms = speed_cms; } } @@ -494,6 +495,9 @@ bool AC_WPNav::update_wpnav() _pos_control.set_max_accel_xy(_wp_accel_cmss); _pos_control.set_max_accel_z(_wp_accel_z_cmss); + // wp_speed_update - update _pos_control.set_max_speed_xy if speed change has been requested + wp_speed_update(dt); + // advance the target if necessary if (!advance_wp_target_along_track(dt)) { // To-Do: handle inability to advance along track (probably because of missing terrain data) @@ -783,6 +787,9 @@ bool AC_WPNav::update_spline() // get dt from pos controller float dt = _pos_control.get_dt(); + // wp_speed_update - update _pos_control.set_max_speed_xy if speed change has been requested + wp_speed_update(dt); + // advance the target if necessary if (!advance_spline_target_along_track(dt)) { // To-Do: handle failure to advance along track (due to missing terrain data) @@ -1031,3 +1038,33 @@ float AC_WPNav::get_slow_down_speed(float dist_from_dest_cm, float accel_cmss) return target_speed; } } + +/// wp_speed_update - calculates how to handle speed change requests +void AC_WPNav::wp_speed_update(float dt) +{ + // return if speed has not changed + float curr_max_speed_xy_cms = _pos_control.get_max_speed_xy(); + if (is_equal(_wp_desired_speed_xy_cms, curr_max_speed_xy_cms)) { + return; + } + // calculate speed change + if (_wp_desired_speed_xy_cms > curr_max_speed_xy_cms) { + // speed up is requested so increase speed within limit set by WPNAV_ACCEL + curr_max_speed_xy_cms += _wp_accel_cmss * dt; + if (curr_max_speed_xy_cms > _wp_desired_speed_xy_cms) { + curr_max_speed_xy_cms = _wp_desired_speed_xy_cms; + } + } else if (_wp_desired_speed_xy_cms < curr_max_speed_xy_cms) { + // slow down is requested so reduce speed within limit set by WPNAV_ACCEL + curr_max_speed_xy_cms -= _wp_accel_cmss * dt; + if (curr_max_speed_xy_cms < _wp_desired_speed_xy_cms) { + curr_max_speed_xy_cms = _wp_desired_speed_xy_cms; + } + } + + // update position controller speed + _pos_control.set_max_speed_xy(curr_max_speed_xy_cms); + + // flag that wp leash must be recalculated + _flags.recalc_wp_leash = true; +} diff --git a/libraries/AC_WPNav/AC_WPNav.h b/libraries/AC_WPNav/AC_WPNav.h index fc2ec9cf5c..301050c657 100644 --- a/libraries/AC_WPNav/AC_WPNav.h +++ b/libraries/AC_WPNav/AC_WPNav.h @@ -247,6 +247,9 @@ protected: /// get_slow_down_speed - returns target speed of target point based on distance from the destination (in cm) float get_slow_down_speed(float dist_from_dest_cm, float accel_cmss); + + /// wp_speed_update - calculates how to change speed when changes are requested + void wp_speed_update(float dt); /// spline protected functions @@ -288,6 +291,7 @@ protected: // waypoint controller internal variables uint32_t _wp_last_update; // time of last update_wpnav call + float _wp_desired_speed_xy_cms; // desired wp speed in cm/sec Vector3f _origin; // starting point of trip to next waypoint in cm from ekf origin Vector3f _destination; // target destination in cm from ekf origin Vector3f _pos_delta_unit; // each axis's percentage of the total track from origin to destination