Skip to content

Commit 52c972b

Browse files
committed
fix: reprodocuer route bug
Signed-off-by: xtk8532704 <1041084556@qq.com>
1 parent 2c870ab commit 52c972b

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

planning/planning_debug_tools/src/perception_replayer/perception_replayer_common.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ void PerceptionReplayerCommon::publish_topics_at_timestamp(
357357
}
358358

359359
if (param_.replay_route) {
360-
publish_route_at_timestamp(bag_timestamp);
360+
publish_route_at_timestamp(bag_timestamp, current_timestamp);
361361
}
362362
}
363363

@@ -410,7 +410,8 @@ void PerceptionReplayerCommon::publish_reference_images_at_timestamp(
410410
}
411411
}
412412

413-
void PerceptionReplayerCommon::publish_route_at_timestamp(const rclcpp::Time & bag_timestamp)
413+
void PerceptionReplayerCommon::publish_route_at_timestamp(
414+
const rclcpp::Time & bag_timestamp, const rclcpp::Time & current_timestamp)
414415
{
415416
// Helper: find the index of the last message at or before bag_timestamp.
416417
// Returns nullopt if there is no such message.
@@ -436,17 +437,33 @@ void PerceptionReplayerCommon::publish_route_at_timestamp(const rclcpp::Time & b
436437
if (!rosbag_route_data_.empty()) {
437438
const auto idx = find_last_before(rosbag_route_data_, bag_timestamp);
438439
if (idx.has_value() && last_published_route_idx_ != idx.value()) {
439-
route_pub_->publish(rosbag_route_data_[idx.value()].second);
440+
auto route_msg = rosbag_route_data_[idx.value()].second;
441+
route_msg.header.stamp = current_timestamp;
442+
route_pub_->publish(route_msg);
440443
last_published_route_idx_ = idx.value();
441444
}
442445
}
443446

444-
// route state
447+
// route state: publish when bag index changes, or re-publish every 5s (replay time)
445448
if (!rosbag_route_state_data_.empty()) {
446449
const auto idx = find_last_before(rosbag_route_state_data_, bag_timestamp);
447-
if (idx.has_value() && last_published_route_state_idx_ != idx.value()) {
448-
route_state_pub_->publish(rosbag_route_state_data_[idx.value()].second);
449-
last_published_route_state_idx_ = idx.value();
450+
if (idx.has_value()) {
451+
const bool index_changed =
452+
!last_published_route_state_idx_.has_value() ||
453+
last_published_route_state_idx_.value() != idx.value();
454+
constexpr double k_route_state_republish_period_sec = 10.0;
455+
const bool period_elapsed =
456+
!last_route_state_publish_replay_time_.has_value() ||
457+
(current_timestamp - last_route_state_publish_replay_time_.value()).seconds() >=
458+
k_route_state_republish_period_sec;
459+
460+
if (index_changed || period_elapsed) {
461+
auto route_state_msg = rosbag_route_state_data_[idx.value()].second;
462+
route_state_msg.stamp = current_timestamp;
463+
route_state_pub_->publish(route_state_msg);
464+
last_published_route_state_idx_ = idx.value();
465+
last_route_state_publish_replay_time_ = current_timestamp;
466+
}
450467
}
451468
}
452469
}

planning/planning_debug_tools/src/perception_replayer/perception_replayer_common.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ class PerceptionReplayerCommon : public rclcpp::Node
122122
/**
123123
* @brief Publish route and route state (transient_local) at the given timestamp.
124124
* Only publishes when the message to publish differs from the last published one.
125-
* @param bag_timestamp
125+
* @param bag_timestamp Used to select messages from the rosbag.
126+
* @param current_timestamp Stamp written on published messages (replay clock).
126127
*/
127-
void publish_route_at_timestamp(const rclcpp::Time & bag_timestamp);
128+
void publish_route_at_timestamp(
129+
const rclcpp::Time & bag_timestamp, const rclcpp::Time & current_timestamp);
128130

129131
protected:
130132
const PerceptionReplayerCommonParam param_;
@@ -231,6 +233,8 @@ class PerceptionReplayerCommon : public rclcpp::Node
231233
// track last published index to avoid redundant publishes for transient_local topics
232234
std::optional<size_t> last_published_route_idx_;
233235
std::optional<size_t> last_published_route_state_idx_;
236+
/// Last time route_state was published (replay clock); used for periodic re-publish.
237+
std::optional<rclcpp::Time> last_route_state_publish_replay_time_;
234238

235239
rclcpp::Publisher<PoseWithCovarianceStamped>::SharedPtr recorded_ego_as_initialpose_pub_;
236240
rclcpp::Publisher<PoseStamped>::SharedPtr goal_as_mission_planning_goal_pub_;

0 commit comments

Comments
 (0)