feat(tracker): add tracker and RViz packages

This commit is contained in:
2026-10-06 15:55:26 +07:00
commit 482ad5ba27
41 changed files with 3254 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.10)
project(robot_dynamic_obstacle_tracker_rviz)
find_package(catkin REQUIRED COMPONENTS
geometry_msgs roscpp robot_dynamic_obstacle_tracker robot_sensor_msgs
robot_sensor_ports robot_time sensor_msgs tf2_ros visualization_msgs)
find_package(Eigen3 REQUIRED)
find_path(TF3_INCLUDE_DIR tf3/buffer_core.h REQUIRED)
find_library(TF3_LIBRARY NAMES tf3 REQUIRED)
catkin_package(CATKIN_DEPENDS
geometry_msgs roscpp robot_dynamic_obstacle_tracker robot_sensor_msgs
robot_sensor_ports robot_time sensor_msgs tf2_ros visualization_msgs)
include_directories(${catkin_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIRS} ${TF3_INCLUDE_DIR})
add_executable(tracker_rviz_sim src/tracker_rviz_sim.cpp)
target_compile_features(tracker_rviz_sim PRIVATE cxx_std_17)
target_compile_options(tracker_rviz_sim PRIVATE -Wall -Wextra -Wpedantic)
target_link_libraries(tracker_rviz_sim ${catkin_LIBRARIES} ${TF3_LIBRARY})
add_dependencies(tracker_rviz_sim ${catkin_EXPORTED_TARGETS})
install(TARGETS tracker_rviz_sim RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
install(DIRECTORY launch rviz DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION})

View File

@@ -0,0 +1,41 @@
# Dynamic obstacle tracker RViz review
This standalone ROS1 demo ray-casts two simulated LiDAR scans at 10 Hz, feeds
those same scans through `SensorStore` -> `tf3` -> `TrackerService`, and opens
RViz. The scene contains static clutter and several obstacles moving on
bounded paths while the robot moves within a small area and turns slowly, so
the scene stays visible during review. The default layout
has 9 stationary and 3 moving circles; counts can be adjusted with the private
ROS parameters `static_circle_count` (1-9) and `moving_circle_count` (1-3).
Build from the workspace root and launch:
```bash
cd ~/T800_ws
catkin_make --pkg robot_dynamic_obstacle_tracker_rviz -j2
source devel/setup.bash
export ROS_MASTER_URI=http://127.0.0.1:11321
export ROS_IP=127.0.0.1
roslaunch robot_dynamic_obstacle_tracker_rviz tracker_rviz_sim.launch
```
For a denser scene, set the counts on the launch command, for example:
```bash
roslaunch robot_dynamic_obstacle_tracker_rviz tracker_rviz_sim.launch \
robot_dynamic_obstacle_tracker_rviz_sim/static_circle_count:=9 \
robot_dynamic_obstacle_tracker_rviz_sim/moving_circle_count:=3
```
The dedicated ROS master port keeps these demo topics and TF frames separate
from any already-running Gazebo or robot navigation graph.
RViz shows front/rear simulated LiDAR in blue/orange, tracked stationary
objects in green, moving objects in red, unknown motion in yellow, velocity
arrows, and a 2 second constant-velocity prediction. Yellow and purple outlines
with `GT STATIC`/`GT MOVING` labels show simulated ground truth, so missed or
occluded detections remain visible for comparison. The node also publishes
`/tracker/status`. Close the launch terminal with Ctrl-C when done.
This is a visualization harness for the tracker pipeline, not a Gazebo run and
not yet an integration with `move_base2` or the real robot's scan topics.

View File

@@ -0,0 +1,8 @@
<launch>
<param name="/use_sim_time" value="false" />
<node pkg="robot_dynamic_obstacle_tracker_rviz" type="tracker_rviz_sim"
name="robot_dynamic_obstacle_tracker_rviz_sim" output="screen" />
<node pkg="rviz" type="rviz" name="tracker_review_rviz"
args="-d $(find robot_dynamic_obstacle_tracker_rviz)/rviz/tracker_review.rviz"
output="screen" />
</launch>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package format="2">
<name>robot_dynamic_obstacle_tracker_rviz</name>
<version>0.1.0</version>
<description>RViz review harness with moving and stationary simulated LiDAR obstacles.</description>
<maintainer email="tdd@gmail.com">DuongTD</maintainer>
<license>BSD-3-Clause</license>
<buildtool_depend>catkin</buildtool_depend>
<depend>robot_dynamic_obstacle_tracker</depend>
<depend>robot_sensor_ports</depend>
<depend>robot_sensor_msgs</depend>
<depend>robot_time</depend>
<depend>tf3</depend>
<depend>roscpp</depend>
<depend>sensor_msgs</depend>
<depend>geometry_msgs</depend>
<depend>visualization_msgs</depend>
<depend>tf2_ros</depend>
<exec_depend>rviz</exec_depend>
</package>

View File

@@ -0,0 +1,83 @@
Panels:
- Class: rviz/Displays
Name: Displays
- Class: rviz/Views
Name: Views
Visualization Manager:
Class: ""
Global Options:
Background Color: 35; 38; 42
Fixed Frame: odom
Frame Rate: 30
Displays:
- Class: rviz/Grid
Enabled: true
Name: Ground grid
Plane: XY
Plane Cell Count: 20
Cell Size: 1
Color: 105; 112; 120
Alpha: 0.5
Reference Frame: <Fixed Frame>
- Class: rviz/TF
Enabled: true
Name: TF
Frame Timeout: 15
Show Arrows: true
Show Axes: true
Show Names: true
Marker Scale: 0.45
- Class: rviz/LaserScan
Enabled: true
Name: Front simulated LiDAR
Topic: /f_scan
Queue Size: 5
Style: Points
Size (Pixels): 4
Color Transformer: FlatColor
Color: 40; 190; 255
Use Fixed Frame: true
- Class: rviz/LaserScan
Enabled: true
Name: Rear simulated LiDAR
Topic: /b_scan
Queue Size: 5
Style: Points
Size (Pixels): 4
Color Transformer: FlatColor
Color: 255; 190; 40
Use Fixed Frame: true
- Class: rviz/MarkerArray
Enabled: true
Name: Tracked obstacles and velocity
Marker Topic: /tracker/tracks
- Class: rviz/MarkerArray
Enabled: true
Name: 2 second constant velocity prediction
Marker Topic: /tracker/predictions
- Class: rviz/MarkerArray
Enabled: true
Name: Simulated obstacle ground truth
Marker Topic: /tracker/simulated_obstacles
Tools:
- Class: rviz/Interact
- Class: rviz/MoveCamera
- Class: rviz/Select
- Class: rviz/FocusCamera
- Class: rviz/Measure
- Class: rviz/SetInitialPose
- Class: rviz/SetGoal
Views:
Current:
Class: rviz/Orbit
Distance: 11
Focal Point:
X: 1
Y: 0
Z: 0
Pitch: 1.15
Yaw: 0.75
Near Clip Distance: 0.01
Window Geometry:
Width: 1400
Height: 900

View File

@@ -0,0 +1,362 @@
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <Eigen/Geometry>
#include <geometry_msgs/Point.h>
#include <geometry_msgs/TransformStamped.h>
#include <robot/time.h>
#include <robot_dynamic_obstacle_tracker/sensor_ports_input.h>
#include <robot_dynamic_obstacle_tracker/tracker_service.h>
#include <robot_sensor_msgs/LaserScan.h>
#include <robot_sensor_ports/sensor_store.h>
#include <robot_sensor_ports/sensor_types.h>
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
#include <std_msgs/ColorRGBA.h>
#include <std_msgs/String.h>
#include <tf2_ros/transform_broadcaster.h>
#include <tf3/buffer_core.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/MarkerArray.h>
namespace
{
using namespace robot_dynamic_obstacle_tracker;
constexpr double kPi = 3.14159265358979323846;
struct Circle { Eigen::Vector2d center; double radius; };
Stamp toStamp(const ros::Time& stamp)
{
return static_cast<Stamp>(stamp.sec) * 1000000000LL + stamp.nsec;
}
class TrackerRvizSim
{
public:
TrackerRvizSim() : nh_(), private_nh_("~"), tf_(std::make_shared<tf3::BufferCore>())
{
robot_sensor_ports::SensorStoreConfig store_config;
robot_sensor_ports::SensorEntryConfig source;
source.data_type = "robot_sensor_msgs/LaserScan";
source.max_age = 0.3;
source.topic = "/f_scan";
store_config.sensors.push_back(source);
source.topic = "/b_scan";
store_config.sensors.push_back(source);
if (!store_.configure(store_config, error_)) throw std::runtime_error(error_);
input_ = makeSensorPortsInput(store_, tf_, [this] { return latest_stamp_; });
TrackerConfig config;
config.tracking_frame = "odom";
config.sources = {{"/f_scan", true, 0.3}, {"/b_scan", true, 0.3}};
if (!service_.configure(config, {input_, [] { return std::chrono::steady_clock::now(); }}, error_))
throw std::runtime_error(error_);
front_pub_ = nh_.advertise<sensor_msgs::LaserScan>("/f_scan", 2);
rear_pub_ = nh_.advertise<sensor_msgs::LaserScan>("/b_scan", 2);
tracks_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("/tracker/tracks", 2);
prediction_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("/tracker/predictions", 2);
ground_truth_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("/tracker/simulated_obstacles", 2);
status_pub_ = nh_.advertise<std_msgs::String>("/tracker/status", 2);
private_nh_.param("static_circle_count", static_circle_count_, 9);
private_nh_.param("moving_circle_count", moving_circle_count_, 3);
if (static_circle_count_ < 1 || static_circle_count_ > 9)
{
ROS_WARN("~static_circle_count must be in [1, 9]; clamping");
static_circle_count_ = std::clamp(static_circle_count_, 1, 9);
}
if (moving_circle_count_ < 1 || moving_circle_count_ > 3)
{
ROS_WARN("~moving_circle_count must be in [1, 3]; clamping");
moving_circle_count_ = std::clamp(moving_circle_count_, 1, 3);
}
start_wall_ = ros::WallTime::now();
timer_ = nh_.createTimer(ros::Duration(0.1), &TrackerRvizSim::onTimer, this);
ROS_INFO("Tracker RViz simulator started: %d static + %d moving circles; topics /f_scan, /b_scan, /tracker/tracks",
static_circle_count_, moving_circle_count_);
}
private:
void publishTf(const ros::Time& stamp, double elapsed)
{
const double yaw = 0.08 * std::sin(0.11 * elapsed);
const Eigen::Rotation2D<double> rotation(yaw);
const Eigen::Vector2d base(0.35 * std::sin(0.08 * elapsed),
0.20 * std::sin(0.07 * elapsed));
std::vector<geometry_msgs::TransformStamped> ros_transforms;
const auto append = [&](const std::string& child, const Eigen::Vector2d& xy, double angle,
const std::string& parent) {
tf3::TransformStampedMsg native;
native.header.stamp = tf3::Time(stamp.sec, stamp.nsec);
native.header.frame_id = parent;
native.child_frame_id = child;
native.transform.translation.x = xy.x();
native.transform.translation.y = xy.y();
native.transform.rotation.z = std::sin(angle * 0.5);
native.transform.rotation.w = std::cos(angle * 0.5);
if (!tf_->setTransform(native, "tracker_rviz_sim", false))
throw std::runtime_error("tf3 rejected simulated transform for " + child);
geometry_msgs::TransformStamped ros_tf;
ros_tf.header.stamp = stamp;
ros_tf.header.frame_id = parent;
ros_tf.child_frame_id = child;
ros_tf.transform.translation.x = xy.x();
ros_tf.transform.translation.y = xy.y();
ros_tf.transform.rotation.z = std::sin(angle * 0.5);
ros_tf.transform.rotation.w = std::cos(angle * 0.5);
ros_transforms.push_back(ros_tf);
};
append("base_link", base, yaw, "odom");
append("sim_front_laser", base + rotation * Eigen::Vector2d(0.32, 0.0), yaw, "odom");
append("sim_rear_laser", base + rotation * Eigen::Vector2d(-0.32, 0.0), yaw + kPi, "odom");
broadcaster_.sendTransform(ros_transforms);
}
sensor_msgs::LaserScan makeScan(const std::string& frame, const Eigen::Vector2d& sensor_xy,
double sensor_yaw, const std::vector<Circle>& circles,
const ros::Time& stamp) const
{
sensor_msgs::LaserScan scan;
scan.header.stamp = stamp;
scan.header.frame_id = frame;
scan.angle_min = -0.75f;
scan.angle_increment = static_cast<float>(1.5 / 240.0);
scan.angle_max = scan.angle_min + 240.0f * scan.angle_increment;
scan.range_min = 0.05f;
scan.range_max = 12.0f;
scan.scan_time = 0.1f;
scan.time_increment = scan.scan_time / 240.0f;
scan.ranges.assign(241, std::numeric_limits<float>::infinity());
for (std::size_t beam = 0; beam < scan.ranges.size(); ++beam)
{
const double angle = sensor_yaw + scan.angle_min + beam * scan.angle_increment;
const Eigen::Vector2d direction(std::cos(angle), std::sin(angle));
double nearest = scan.range_max;
for (const auto& circle : circles)
{
const Eigen::Vector2d relative = circle.center - sensor_xy;
const double projection = relative.dot(direction);
const double discriminant = projection * projection -
(relative.squaredNorm() - circle.radius * circle.radius);
if (projection <= 0.0 || discriminant < 0.0) continue;
const double distance = projection - std::sqrt(discriminant);
if (distance >= scan.range_min && distance < nearest) nearest = distance;
}
if (nearest < scan.range_max) scan.ranges[beam] = static_cast<float>(nearest);
}
return scan;
}
std::vector<Circle> makeObstacles(double elapsed) const
{
const std::vector<Circle> fixed = {
{{3.6, 1.6}, 0.20}, {{3.8, -1.8}, 0.18}, {{5.2, 2.5}, 0.22},
{{5.0, -1.3}, 0.19}, {{7.0, 0.5}, 0.20}, {{6.4, -2.5}, 0.22},
{{-2.0, 0.7}, 0.18}, {{-3.2, -1.3}, 0.21}, {{-4.5, 1.8}, 0.19}};
std::vector<Circle> obstacles(fixed.begin(), fixed.begin() + static_circle_count_);
const std::vector<Circle> moving = {
{{2.5, -0.6 + std::sin(0.25 * elapsed)}, 0.18},
{{4.5, 0.2 + std::sin(0.35 * elapsed)}, 0.18},
{{6.6, -0.3 + 1.4 * std::sin(0.22 * elapsed + 1.2)}, 0.20}};
obstacles.insert(obstacles.end(), moving.begin(), moving.begin() + moving_circle_count_);
return obstacles;
}
void storeAndPublish(const std::string& topic, const sensor_msgs::LaserScan& scan,
ros::Publisher& publisher)
{
robot_sensor_msgs::LaserScan native;
native.header.stamp = robot::Time(scan.header.stamp.sec, scan.header.stamp.nsec);
native.header.frame_id = scan.header.frame_id;
native.angle_min = scan.angle_min;
native.angle_max = scan.angle_max;
native.angle_increment = scan.angle_increment;
native.time_increment = scan.time_increment;
native.scan_time = scan.scan_time;
native.range_min = scan.range_min;
native.range_max = scan.range_max;
native.ranges = scan.ranges;
native.intensities = scan.intensities;
if (!robot_sensor_ports::put(store_, topic, native, native.header.stamp))
throw std::runtime_error("SensorStore rejected simulated scan on " + topic);
publisher.publish(scan);
}
std_msgs::ColorRGBA color(float r, float g, float b, float a = 1.0f) const
{
std_msgs::ColorRGBA out; out.r = r; out.g = g; out.b = b; out.a = a; return out;
}
void publishSnapshot(const ros::Time& stamp)
{
const auto snapshot = service_.latestSnapshot();
if (!snapshot) return;
visualization_msgs::MarkerArray tracks, predictions;
visualization_msgs::Marker clear;
clear.action = visualization_msgs::Marker::DELETEALL;
tracks.markers.push_back(clear);
predictions.markers.push_back(clear);
int id = 0;
for (const auto& track : snapshot->tracks)
{
const bool moving = track.motion == MotionClass::kMoving;
const auto c = moving ? color(1.0f, 0.16f, 0.10f) :
track.motion == MotionClass::kStationary ? color(0.15f, 0.85f, 0.25f) :
color(1.0f, 0.75f, 0.10f);
visualization_msgs::Marker body;
body.header.frame_id = snapshot->frame; body.header.stamp = stamp;
body.ns = "tracked_obstacles"; body.id = id++;
body.type = visualization_msgs::Marker::CYLINDER;
body.action = visualization_msgs::Marker::ADD;
body.pose.position.x = track.state.x(); body.pose.position.y = track.state.y();
body.pose.position.z = 0.08; body.pose.orientation.w = 1.0;
body.scale.x = body.scale.y = std::max(0.12, 2.0 * track.radius);
body.scale.z = 0.16; body.color = c;
tracks.markers.push_back(body);
visualization_msgs::Marker label;
label.header = body.header; label.ns = "track_labels"; label.id = id++;
label.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
label.action = visualization_msgs::Marker::ADD;
label.pose.position.x = track.state.x(); label.pose.position.y = track.state.y();
label.pose.position.z = 0.38; label.pose.orientation.w = 1.0;
label.scale.z = 0.22; label.color = color(1, 1, 1);
label.text = std::string(track.motion == MotionClass::kMoving ? "MOVING" :
track.motion == MotionClass::kStationary ? "STATIC" : "UNKNOWN") +
" #" + std::to_string(track.track_id) + " v=" +
std::to_string(track.state.tail<2>().norm()).substr(0, 4) + " m/s";
tracks.markers.push_back(label);
visualization_msgs::Marker arrow;
arrow.header = body.header; arrow.ns = "track_velocity"; arrow.id = id++;
arrow.type = visualization_msgs::Marker::ARROW;
arrow.action = visualization_msgs::Marker::ADD;
geometry_msgs::Point start, end;
start.x = track.state.x(); start.y = track.state.y(); start.z = 0.22;
end.x = start.x + track.state.z(); end.y = start.y + track.state.w(); end.z = 0.22;
arrow.points = {start, end}; arrow.scale.x = 0.025; arrow.scale.y = 0.07;
arrow.scale.z = 0.08; arrow.color = c;
tracks.markers.push_back(arrow);
visualization_msgs::Marker prediction;
prediction.header = body.header; prediction.ns = "constant_velocity_prediction";
prediction.id = id++; prediction.type = visualization_msgs::Marker::LINE_STRIP;
prediction.action = visualization_msgs::Marker::ADD;
prediction.scale.x = 0.035; prediction.color = c;
for (int step = 0; step <= 20; ++step)
{
const double t = 0.1 * step;
geometry_msgs::Point point;
point.x = track.state.x() + t * track.state.z();
point.y = track.state.y() + t * track.state.w(); point.z = 0.12;
prediction.points.push_back(point);
}
predictions.markers.push_back(prediction);
}
tracks_pub_.publish(tracks);
prediction_pub_.publish(predictions);
std_msgs::String status;
status.data = std::string("TrackerService: ") + healthName(snapshot->status) +
" | tracks=" + std::to_string(snapshot->tracks.size()) +
" | frame=" + snapshot->frame + " | " + snapshot->reason;
status_pub_.publish(status);
ROS_INFO_THROTTLE(2.0, "%s", status.data.c_str());
}
void publishGroundTruth(const ros::Time& stamp, const std::vector<Circle>& obstacles)
{
visualization_msgs::MarkerArray markers;
visualization_msgs::Marker clear;
clear.action = visualization_msgs::Marker::DELETEALL;
markers.markers.push_back(clear);
int id = 0;
for (std::size_t index = 0; index < obstacles.size(); ++index)
{
const bool moving = index >= static_cast<std::size_t>(static_circle_count_);
const auto c = moving ? color(0.95f, 0.25f, 1.0f, 0.95f) : color(0.95f, 0.9f, 0.25f, 0.9f);
visualization_msgs::Marker ring;
ring.header.frame_id = "odom"; ring.header.stamp = stamp;
ring.ns = moving ? "ground_truth_moving" : "ground_truth_static";
ring.id = id++; ring.type = visualization_msgs::Marker::LINE_STRIP;
ring.action = visualization_msgs::Marker::ADD;
ring.scale.x = 0.035; ring.color = c;
for (int step = 0; step <= 36; ++step)
{
const double angle = 2.0 * kPi * step / 36.0;
geometry_msgs::Point point;
point.x = obstacles[index].center.x() + obstacles[index].radius * std::cos(angle);
point.y = obstacles[index].center.y() + obstacles[index].radius * std::sin(angle);
point.z = 0.035;
ring.points.push_back(point);
}
markers.markers.push_back(ring);
visualization_msgs::Marker label;
label.header = ring.header; label.ns = "ground_truth_labels"; label.id = id++;
label.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
label.action = visualization_msgs::Marker::ADD;
label.pose.position.x = obstacles[index].center.x();
label.pose.position.y = obstacles[index].center.y();
label.pose.position.z = 0.32; label.pose.orientation.w = 1.0;
label.scale.z = 0.14; label.color = c;
label.text = std::string(moving ? "GT MOVING " : "GT STATIC ") + std::to_string(index);
markers.markers.push_back(label);
}
ground_truth_pub_.publish(markers);
}
void onTimer(const ros::TimerEvent&)
{
try
{
const ros::Time stamp = ros::Time::now();
if (stamp.isZero()) { ROS_WARN_THROTTLE(2.0, "Waiting for a nonzero ROS time"); return; }
latest_stamp_ = toStamp(stamp);
const double elapsed = (ros::WallTime::now() - start_wall_).toSec();
const double yaw = 0.08 * std::sin(0.11 * elapsed);
const Eigen::Rotation2D<double> rotation(yaw);
const Eigen::Vector2d base(0.35 * std::sin(0.08 * elapsed),
0.20 * std::sin(0.07 * elapsed));
const std::vector<Circle> obstacles = makeObstacles(elapsed);
publishTf(stamp, elapsed);
const auto front_xy = base + rotation * Eigen::Vector2d(0.32, 0.0);
const auto rear_xy = base + rotation * Eigen::Vector2d(-0.32, 0.0);
auto front = makeScan("sim_front_laser", front_xy, yaw, obstacles, stamp);
auto rear = makeScan("sim_rear_laser", rear_xy, yaw + kPi, obstacles, stamp);
storeAndPublish("/f_scan", front, front_pub_);
storeAndPublish("/b_scan", rear, rear_pub_);
publishGroundTruth(stamp, obstacles);
if (!service_.pollOnce(error_)) throw std::runtime_error("TrackerService pollOnce: " + error_);
publishSnapshot(stamp);
}
catch (const std::exception& ex) { ROS_ERROR_THROTTLE(1.0, "RViz simulator: %s", ex.what()); }
}
ros::NodeHandle nh_, private_nh_;
robot_sensor_ports::SensorStore store_;
std::shared_ptr<tf3::BufferCore> tf_;
std::shared_ptr<TrackerInput> input_;
TrackerService service_;
tf2_ros::TransformBroadcaster broadcaster_;
ros::Publisher front_pub_, rear_pub_, tracks_pub_, prediction_pub_, ground_truth_pub_, status_pub_;
ros::Timer timer_;
ros::WallTime start_wall_;
Stamp latest_stamp_ = 0;
std::string error_;
int static_circle_count_ = 9, moving_circle_count_ = 3;
};
} // namespace
int main(int argc, char** argv)
{
ros::init(argc, argv, "robot_dynamic_obstacle_tracker_rviz_sim");
try { TrackerRvizSim node; ros::spin(); }
catch (const std::exception& ex) { ROS_FATAL("Cannot start tracker RViz simulator: %s", ex.what()); return 1; }
return 0;
}