Skip to content

Commit 7214eb9

Browse files
committed
Refactor acceleration limits to handle forward and backward movement separately
Signed-off-by: RBT22 <[email protected]>
1 parent 964404a commit 7214eb9

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

nav2_behaviors/include/nav2_behaviors/plugins/drive_on_heading.hpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,22 @@ class DriveOnHeading : public TimedBehavior<ActionT>
136136
cmd_vel->twist.angular.z = 0.0;
137137

138138
double current_speed = last_vel_ == std::numeric_limits<double>::max() ? 0.0 : last_vel_;
139-
double min_feasible_speed = current_speed + deceleration_limit_ / this->cycle_frequency_;
140-
double max_feasible_speed = current_speed + acceleration_limit_ / this->cycle_frequency_;
139+
bool forward = command_speed_ > 0.0;
140+
double min_feasible_speed, max_feasible_speed;
141+
if (forward) {
142+
min_feasible_speed = current_speed + deceleration_limit_ / this->cycle_frequency_;
143+
max_feasible_speed = current_speed + acceleration_limit_ / this->cycle_frequency_;
144+
} else {
145+
min_feasible_speed = current_speed - acceleration_limit_ / this->cycle_frequency_;
146+
max_feasible_speed = current_speed - deceleration_limit_ / this->cycle_frequency_;
147+
}
141148
cmd_vel->twist.linear.x = std::clamp(command_speed_, min_feasible_speed, max_feasible_speed);
142149

143150
// Check if we need to slow down to avoid overshooting
144151
auto remaining_distance = std::fabs(command_x_) - distance;
145-
bool forward = command_speed_ > 0.0;
146-
if (forward) {
147-
double max_vel_to_stop = std::sqrt(-2.0 * deceleration_limit_ * remaining_distance);
148-
if (max_vel_to_stop < cmd_vel->twist.linear.x) {
149-
cmd_vel->twist.linear.x = max_vel_to_stop;
150-
}
151-
} else {
152-
double max_vel_to_stop = -std::sqrt(2.0 * acceleration_limit_ * remaining_distance);
153-
if (max_vel_to_stop > cmd_vel->twist.linear.x) {
154-
cmd_vel->twist.linear.x = max_vel_to_stop;
155-
}
152+
double max_vel_to_stop = std::sqrt(-2.0 * deceleration_limit_ * remaining_distance);
153+
if (max_vel_to_stop < std::abs(cmd_vel->twist.linear.x)) {
154+
cmd_vel->twist.linear.x = forward ? max_vel_to_stop : -max_vel_to_stop;
156155
}
157156

158157
// Ensure we don't go below minimum speed

0 commit comments

Comments
 (0)