Obstacle Avoidance and DWB Controller
Last updated
Last updated
In this article, we will explore the implementation of the DWB controller in Navigation2, a critic-based and highly configurable variant of the Dynamic Window Approach (DWA) Algorithm. We will start with a brief overview of the DWA algorithm, followed by a detailed examination of the DWB controller's key components, including the local planner, trajectory generator, and base obstacle critic.
DWA simplifies robot movement calculation by assuming constant translation and rotational velocity during short time intervals. Under this assumption, the robot's path is reduced to either a straight line or an arc during these periods, making collision detection straightforward. The algorithm searches a 2D velocity space to determine the next command. Velocity pairs leading to potential collisions form inaccessible areas, while the robot's current velocity and acceleration limits confine the reachable space to a rectangular region centered around the current velocity of the robot. An objective function determines the best velocity command. For example, the function used in the DWA paper is as follows:
In the previous section, we outlined three essential components of a dynamic-window-based algorithm:
The dynamic window. This is determined by the robot's current velocity and the maximum accelerations.
The inaccessible regions. These regions depend on the robot's current location and the locations of the obstacles, informed by sensor data and map information.
The objective function. The objective function evaluates various velocity commands to identify the optimal one.
In the Nav2 codebase, the standard practice for initializing an instance is through the configure
method. For instance, the configure
method in the DWBLocalPlanner
class requires three arguments: LifecycleNode
, tf2
, and Costmap2DROS
. These objects provide the context and inputs for the DWBLocalPlanner
class. Parameters are another type of input. They are adjustable settings used to fine-tune the planner's behavior. We will cover this topic in a separate article.
The outputs of the DWBLocalPlanner
are handled by the DWBPublisher
class, which is responsible for publishing computation results to specific topics. For example:
The DWBLocalPlanner
has the following public methods:
The setPlan
method suggests that the planner receives a global plan, which it may then publish or modify for further processing. The role of the setSpeedLimit
method is self-explanatory: it sets the linear speed limit of the robot. Specifically, it sets the speed limit of the trajectory generator, the component responsible for simulating the robot's trajectory. The next two methods are related. scoreTrajectory
is used to evaluate proposed trajectories when the planner computes the velocity commands. In the context of the DWA algorithm, an objective function is required to choose the optimal velocity command, and scoreTrajectory
implements such a function.
Let's take a look at the method computeVelocityCommands
. Setting aside variable initialization and error handling, here is a simplified overview of its implementation:
It first prepares a global plan based on the current pose and the goal pose. Then, it locks the cost map so that the map cannot be modified during the calculation of the trajectory score. Next, it invokes the scoring algorithm and selects the best one. Finally, it unlocks the cost map and publishes the local plan and the cost map.
Move on to the coreScoringAlgorithm
. Setting aside variable initialization and the exception handling, the method has the following structure:
It iterates through the candidate velocity command and generates the correspondent trajectory. Then it calculates the score for each trajectory and returns the best one. The key step in this function is the invocation of the method scoreTrajectory
.
The method scoreTrajector
is straightforward. It iterates through the critics and each of them produces a score. The total score is used as the score of the trajectory. Recall in the paper, the objective function is defined as follows:
So far, we've examined the part in the DWBLocalPlanner
that computes the velocity commands. We've learned the scoring algorithm, which implements the objective function in the DWA pager, is based on critics and this is the reason why nav2 DWB controller is called critic-based controller. The remaining question is where is the code that determines the dynamic window and accessible regions? This is answered in the next section.
Two trajectory generators provided in navigation 2:
It's worth noting that the LimitedAccelGenerator
is a derived class of the StandardTrajectoryGenerator
. One of the key methods in the trajectory generator class is the method generateTrajectory
:
This method takes the start pose, start velocity, and the velocity command as inputs and generate a Trajectory2D
object, whose definition is given as follows:
The implementation of the generateTrajectory
method is straightforward. It essentially simulates a path:
Regarding the dynamic window in the DWA algorithm, it's implemented in the computeNewVelocity
method, which is overridden by the LimitedAccelGenerator
which takes the acceleration into account.
You may still wonder how obstacle avoidance is handled by the code. Remember, the DWA algorithm introduces the concept of inaccessible regions in the velocity space, representing velocity commands that could lead to collisions.
In the nav2 implementation, given the current pose of the robot and the velocity command, the planner calculates a trajectory and submits it to the base obstacle critic for evaluation. If this trajectory intersects with any obstacle in the map, it is considered invalid and excluded from the selection of the optimal trajectory.
Recall that when the local planner computes the velocity, it locks the cost map object. Now we can see why it needs to be locked: we want to ensure that all critics use the same cost map for their evaluation.
This article discusses the implementation of the DWB controller in navigation 2. It begins with an overview of the DWA algorithm and introduces key concepts such as velocity space, inaccessible regions, and the dynamic window. It then focuses on the DWBLocalPlanner
, a key component in the DWB controller, and discusses the trajectory generator and the base obstacle critic. The article also highlights how these components are related to the steps of the DWA algorithm.
If you enjoy reading this article and wish to support my work, you can make a contribution through Stripe or Buy Me a Coffee. Your support means a lot to me and helps me to continue producing content that you like and find useful.
The core component in the DWB controller is the DWBLocalPlanner
class, instantiated in . Rather than diving into the interactions between DWBLocalPlanner
and other components in the navigation stack, this article focuses on the code that implements the DWA algorithm.
In the formula, heading, dist, and velocity are critics and each produces a score. For example, the score produced by the heading
critic is . The coefficient corresponds to the critic scale in the implementation. For a complete reference of critics, you can check out the .
Standard trajectory generator ()
Limited acceleration trajectory generator ()
If you search for keywords such as "obstacle" or "avoidance" in the directory, you won't find any results. This suggests that the obstacle avoidance and the related accessible region determination are handled by other parts of the code.
The base obstacle critic can obtain obstacle information through the cost map. Special cost values are defined in the to indicate the nature of a cell in the map:
Here, we only list the key part of the BaseObstacleCritic
implementation. For more details, please refer to the:
(っ◔◡◔)っ give support