Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Use rclcpp::Time for stamping transforms (#20)
Browse files Browse the repository at this point in the history
* Use rclcpp::Time for stamping transforms

The motivation is that right now, `robot_state_publisher` doesn't respect
`use_sim_time` at all.

* Use std::chrono constructor to represent 0.5 sec
  • Loading branch information
cottsay authored and clalancette committed Jun 12, 2019
1 parent 8eb71ec commit 09cd998
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/robot_state_publisher/robot_state_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class RobotStatePublisher
std_msgs::msg::String model_xml_;
bool description_published_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr description_pub_;
rclcpp::Clock::SharedPtr clock_;
};

} // namespace robot_state_publisher
Expand Down
11 changes: 5 additions & 6 deletions src/robot_state_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ RobotStatePublisher::RobotStatePublisher(
: model_(model),
tf_broadcaster_(node_handle),
static_tf_broadcaster_(node_handle),
description_published_(false)
description_published_(false),
clock_(node_handle->get_clock())
{
// walk the tree and add segments to segments_
addChildren(tree.getRootSegment());
Expand Down Expand Up @@ -160,13 +161,11 @@ void RobotStatePublisher::publishFixedTransforms(const std::string & tf_prefix,
// loop over all fixed segments
for (auto seg = segments_fixed_.begin(); seg != segments_fixed_.end(); seg++) {
geometry_msgs::msg::TransformStamped tf_transform = kdlToTransform(seg->second.segment.pose(0));
std::chrono::nanoseconds now = std::chrono::high_resolution_clock::now().time_since_epoch();
rclcpp::Time now = clock_->now();
if (!use_tf_static) {
now += std::chrono::milliseconds(500); // 0.5sec in NS
now = now + rclcpp::Duration(std::chrono::milliseconds(500)); // 0.5sec in NS
}
tf_transform.header.stamp.sec =
static_cast<builtin_interfaces::msg::Time::_sec_type>(now.count() / 1000000000);
tf_transform.header.stamp.nanosec = now.count() % 1000000000;
tf_transform.header.stamp = now;

tf_transform.header.frame_id = tf_prefix + "/" + seg->second.root;
tf_transform.child_frame_id = tf_prefix + "/" + seg->second.tip;
Expand Down

0 comments on commit 09cd998

Please sign in to comment.