Dynamic obstacle detection → tracking → prediction for Nav2 (ROS 2). This repo provides:
- Foreground mask layer for Costmap2D (separates dynamic foreground from inflated static background)
- Detection node: Obstacle detection and persistent tracking with Hungarian assignment
- Predictor node: Constant velocity prediction for short-horizon trajectory forecasts
- Custom messages for obstacle exchange (topic-based architecture)
- Visualization node for RViz markers (separate from core processing)
navigation2_dynamic/
├── foreground_mask_layer/ # costmap2d plugin (C++)
├── nav2_dynamic_interface/ # msgs (ObstacleArray, Obstacle)
├── obstacle_detection/ # detection + tracking node (C++)
│ ├── src/detection_node.cpp # main detection/tracking node
│ ├── src/obstacle_viz_node.cpp # visualization node
│ ├── src/cluster_engine.cpp # blob extraction
│ └── src/tracker_engine.cpp # Hungarian tracking
└── obstacle_predictor/ # predictor node (Python)
└── obstacle_predictor/
└── predictor_node_cv.py # CV prediction with history
- Foreground mask isolates dynamic cells from obstacle layer (fast) vs inflated static map (slow).
- ClusterEngine extracts blobs (centroid + convex hull).
- TrackerEngine keeps persistent IDs (Hungarian assignment, gating, history).
- Detection node publishes current detections on
obstacles_detectedtopic. - Predictor node subscribes to detections, maintains history per ID, computes predictions, and publishes full trajectories on
obstacles_array. - Detection node subscribes to
obstacles_arrayto update tracker with predicted positions for next-cycle matching. - Visualization node generates RViz markers for hulls, centroids, IDs, trajectories, covariance ellipses, and velocity arrows.
Costmap2D plugin (nav2_costmap_2d::Layer) that:
- Subscribes to a static map, inflates it by a configurable radius,
- Compares rolling obstacle costs vs inflated static background,
- Flags foreground (dynamic) cells (optionally writing them back to the master grid).
Key params (YAML)
foreground_mask_layer:
plugin: "foreground_mask::ForegroundMaskLayer"
map_topic: "/map" # static map
inflation_radius: 20 # buffer around static obstacles
observation_sources: scan # pass-through to ObstacleLayerInterfaces shared across nodes.
Messages
-
Obstacle.msgstd_msgs/Header headerunique_identifier_msgs/UUID uuidint64 id# Persistent track IDbuiltin_interfaces/Duration delta_time# Fixed timestep between predictionsgeometry_msgs/PoseWithCovariance[] position# position[0]=current, [1..N]=predictionsgeometry_msgs/TwistWithCovariance[] velocity# velocity[0..N] aligned with positionsgeometry_msgs/Polygon polygon# Convex hull boundary
-
ObstacleArray.msgstd_msgs/Header headerObstacle[] obstacles
Executable: detection_node
- Brings up a dedicated
Costmap2DROS - Clusters blobs (8-connectivity), centroids, convex hull (monotone chain)
- Tracks across ticks (Hungarian assignment, gating radius, missed counts)
- Publishes current detections and subscribes to predictions for tracker updates
Topics
- Pub:
obstacles_detected(nav2_dynamic_interface/ObstacleArray) - Current detections only (position[0]) - Sub:
obstacles_array(nav2_dynamic_interface/ObstacleArray) - Full predictions from predictor - Sub (via Costmap2DROS/ObstacleLayer): scans, TF, map, etc.
Detection node now:
- Publishes detections with only
position[0](current state) - Subscribes to predictor's output to update tracker's
predicted_next_positionfor improved Hungarian matching - Does NOT publish final obstacle array (predictor owns that responsibility)
Selected params
detection_node:
ros__parameters:
tracking:
gate_distance: 1.5 # Max distance (m) for valid track-detection match
max_missed: 3 # Max consecutive missed detections before track retirement
prediction:
delta_time: 0.2 # Time step between predictions (seconds, used for msg field)Executable: predictor_node_cv
Topic-based pub/sub architecture with constant velocity model only.
- Subscribes to
obstacles_detected(current detections) - Maintains per-ID history buffer (configurable window)
- Computes velocity from full history (first → last point)
- Publishes
obstacles_arraywith current + predicted positions
Topics
- Sub:
obstacles_detected- Current detections from detection_node - Pub:
obstacles_array- Full obstacle trajectories (current + predictions)
Selected params
predictor_node:
ros__parameters:
# History management
history_window: 20 # Max number of past poses to store per track
cleanup_threshold: 10 # Remove tracks not seen for N cycles
# Velocity estimation (uses full history: first → last point)
min_history_length: 3 # Min poses required before estimating non-zero velocity
# Prediction output
prediction_steps: 5 # Number of future steps to predict
prediction_dt: 0.2 # Time between prediction steps (seconds)Executable: obstacle_viz_node
- Subscribes to
obstacles_arrayand generates RViz-friendly visualization - Publishes marker arrays for centroids, trajectories, hulls, velocity arrows, covariance ellipses, track IDs
- Decouples visualization from core detection/tracking for better resource management
Topics
- Sub:
obstacles_array - Pub:
cluster_markers(visualization_msgs/MarkerArray)
Marker types:
- Centroid spheres (color-coded by track ID)
- Track ID labels
- Predicted trajectory line strips
- Convex hull boundaries
- Covariance ellipses (growing with prediction horizon)
- Velocity arrows (color-coded by speed: green→yellow→red)
# create a workspace if needed
mkdir -p ~/ws_nav2dyn/src && cd ~/ws_nav2dyn/src
# clone your repo
git clone -b dev https://github.com/LEM-RPTU/navigation2_dynamic.git navigation2_dynamic
# build
cd ..
colcon build --symlink-install \
--packages-select \
foreground_mask_layer nav2_dynamic_interface obstacle_detection obstacle_predictor
# source
source install/setup.bashDependencies
- ROS 2 Humble+
nav2_costmap_2d,rclcpp(_lifecycle),geometry_msgs,visualization_msgs,unique_identifier_msgs- Python node:
rclpy(no external dependencies like numpy)
A combined launch starts the detection node, predictor node, and visualization under a namespace:
ros2 launch obstacle_detection dynamic_obstacle.launch.py \
namespace:=fleet/robot1 \
params_file:=/path/to/obstacle_predictor.yamlOr launch nodes individually:
# Terminal 1: Detection node
ros2 run obstacle_detection detection_node --ros-args \
-r __ns:=/robot1 \
--params-file /path/to/obstacle_predictor.yaml
# Terminal 2: Predictor node
ros2 run obstacle_predictor predictor_node --ros-args \
-r __ns:=/robot1 \
--params-file /path/to/obstacle_predictor.yaml
# Terminal 3: Visualization node
ros2 run obstacle_detection obstacle_viz_node --ros-args \
-r __ns:=/robot1Expected outputs:
/<ns>/obstacles_detected(detections only)/<ns>/obstacles_array(full predictions)/<ns>/cluster_markers(visualization)
Frame IDs
- Costmap uses
mapasglobal_frame; ensure TFmap↔odomexists. - Messages carry the costmap's
global_frame_id.
- Foreground vs background: Pick a sufficiently large
inflation_radiusfor the static map to avoid mislabeling static obstacles as dynamic due to pose drift with larger COSTMAP. - Tracking gate:
tracking.gate_distancebounds associations; tune for your costmap resolution and dynamics. - History length:
history_windowcaps memory usage;min_history_lengthensures stable velocity estimates. - Prediction timestep: Both nodes should use the same
prediction_dtvalue (0.2s default). - Detection rate: Detection node runs at 200ms (5Hz); predictor processes asynchronously.
- Visualization: Run on a separate executor to avoid slowing detection/tracking.
Add these displays:
- MarkerArray on
/<ns>/obstacle_markers:- Namespace filters:
centroids,labels,hulls,trajectories,covariance,velocity
- Namespace filters:
- Costmap layers to confirm foreground masking
- Set fixed frame to
map
Marker color coding:
- Centroids: Hashed by track ID (persistent colors)
- Velocity arrows: Green (slow) → Yellow → Red (fast)
- Trajectories: Predicted path line strips
Topic: obstacles_detected (nav2_dynamic_interface/ObstacleArray)
Published by: detection_node
Consumed by: predictor_node
header.stamp, header.frame_id
obstacles[*]:
id, uuid
position[0] # Current centroid only
velocity # Empty (not computed by detector)
polygon # Convex hull
delta_time # Set to 0 (ignored)
Topic: obstacles_array (nav2_dynamic_interface/ObstacleArray)
Published by: predictor_node
Consumed by: detection_node (for tracker updates), obstacle_viz_node
header.stamp, header.frame_id
obstacles[*]:
id, uuid
delta_time # Fixed prediction timestep (e.g., 0.2s)
position[0] # Current position (copied from detection)
position[1..N] # N predicted future positions
velocity[0..N] # Estimated/predicted velocities (aligned with positions)
polygon # Convex hull
covariance fields # Pose/velocity uncertainty (grows with horizon)
-
C++ standards: Plugin uses C++14; detection node uses C++17.
-
Assignment: Hungarian (square cost matrix, distance on centroids), with gating.
-
Velocity estimation: Predictor uses full history (first → last point) for smoothing.
-
Threading:
Costmap2DROSspins in its own thread.- Detection node timer runs at 200ms (5Hz).
- Predictor uses
MultiThreadedExecutorwithReentrantCallbackGroupfor concurrent topic processing. - No worker queues or async service calls (topic-based is inherently async).
-
Topic flow:
detection_node → obstacles_detected → predictor_node ↑ ↓ obstacle_viz_node ← obstacles_array ──────┘
- Only constant velocity model — No Kalman Filter or VAR; covariances are heuristic (scatter-based).
- Integer IDs — UUID field exists but not fully utilized; track IDs are simple monotonic integers.
- No ego motion compensation — Moving robot doesn't filter its own motion from obstacle velocity estimates.
- Full UUID support - Replace integer IDs with proper UUID implementation for robust tracking across nodes
- Ego motion compensation - Implement proper ego-vehicle motion filtering to improve dynamic object identification
- Kalman Filter predictor - Implement proper CV/CTRV Kalman filter with tuned process/measurement noise
- VAR predictor - Implement Vector Auto-Regressive with proper configurations
- Unit/integration tests - Add CI with colcon test
- Top-level: Apache-2.0 (see
LICENSE). - Packages may declare BSD-3-Clause or Apache-2.0 in their
package.xml. Always check the package's ownpackage.xmlfor the definitive license.
Based on Nav2 and ROS 2 ecosystems. Thanks to the contributors and advisors involved in design discussions and review.
-
Maintainer: Riyan Cyriac Jose
-
Contribution: Eric Schoeneberg
-
Issues: GitHub Issues
# Dynamic obstacle costmap configuration
/**/dynamic_obstacle_costmap:
dynamic_obstacle_costmap:
ros__parameters:
update_frequency: 5.0
global_frame: map
robot_base_frame: base_footprint
rolling_window: true
width: 20
height: 20
resolution: 0.1
plugins: ["foreground_mask_layer", "denoise_layer", "inflation_layer"]
foreground_mask_layer:
plugin: "foreground_mask::ForegroundMaskLayer"
map_topic: "/map"
inflation_radius: 15
observation_sources: scan
scan:
topic: /scan
data_type: "LaserScan"
denoise_layer:
plugin: "nav2_costmap_2d::DenoiseLayer"
minimal_group_size: 2
inflation_layer:
plugin: "nav2_costmap_2d::InflationLayer"
inflation_radius: 0.3
# Detection node configuration
/**/detection_node:
ros__parameters:
tracking:
gate_distance: 1.5 # Max distance (m) for valid track-detection match
max_missed: 3 # Max consecutive missed detections before track retirement
prediction:
delta_time: 0.2 # Time step between predictions (seconds)
# Predictor node configuration
/**/predictor_node:
ros__parameters:
history_window: 20 # Max number of past poses to store per track
cleanup_threshold: 10 # Remove tracks not seen for N cycles
min_history_length: 3 # Min poses required for velocity estimation
prediction_steps: 5 # Number of future steps to predict
prediction_dt: 0.2 # Time between prediction steps (seconds)Q: Detection node publishes but predictor doesn't respond
- Check topic remapping:
ros2 topic list | grep obstacles - Verify predictor is receiving:
ros2 topic echo /obstacles_detected --once - Check logs for history buffer initialization
Q: Visualization markers not showing
- Verify RViz fixed frame matches
header.frame_id(usuallymap) - Check topic name:
ros2 topic echo /cluster_markers --once - Enable specific marker namespaces in RViz MarkerArray display
Q: High CPU usage
- Reduce costmap resolution or size
- Decrease detection timer frequency (increase period in code)
- Disable visualization node if not needed
- Reduce
prediction_stepsor increaseprediction_dt