Skip to content

Commit 3114bc3

Browse files
fix: new api for realtime publishers in kilted
1 parent 8dce1b2 commit 3114bc3

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

include/crisp_controllers/utils/ros2_version.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@
3838
#else
3939
#define HAS_ROS2_CONTROL_INTROSPECTION 0
4040
#endif
41+
42+
#define REALTIME_TOOLS_NEW_API ROS2_VERSION_ABOVE_JAZZY

src/pose_broadcaster.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,24 @@ PoseBroadcaster::update(const rclcpp::Time & time, const rclcpp::Duration & peri
7070
bool should_publish =
7171
(publish_elapsed_ >= publish_interval_) || (publish_interval_.nanoseconds() == 0);
7272
if (should_publish && realtime_pose_publisher_) {
73+
geometry_msgs::msg::PoseStamped pose_msg;
74+
pose_msg.header.stamp = time;
75+
pose_msg.header.frame_id = params_.base_frame;
76+
pose_msg.pose.position.x = current_pose.translation()[0];
77+
pose_msg.pose.position.y = current_pose.translation()[1];
78+
pose_msg.pose.position.z = current_pose.translation()[2];
79+
pose_msg.pose.orientation.x = current_quaternion.x();
80+
pose_msg.pose.orientation.y = current_quaternion.y();
81+
pose_msg.pose.orientation.z = current_quaternion.z();
82+
pose_msg.pose.orientation.w = current_quaternion.w();
83+
84+
#if REALTIME_TOOLS_NEW_API
85+
if (realtime_pose_publisher_->try_publish(pose_msg)) {
86+
#else
7387
if (realtime_pose_publisher_->trylock()) {
74-
auto & pose_msg = realtime_pose_publisher_->msg_;
75-
pose_msg.header.stamp = time;
76-
pose_msg.header.frame_id = params_.base_frame;
77-
pose_msg.pose.position.x = current_pose.translation()[0];
78-
pose_msg.pose.position.y = current_pose.translation()[1];
79-
pose_msg.pose.position.z = current_pose.translation()[2];
80-
pose_msg.pose.orientation.x = current_quaternion.x();
81-
pose_msg.pose.orientation.y = current_quaternion.y();
82-
pose_msg.pose.orientation.z = current_quaternion.z();
83-
pose_msg.pose.orientation.w = current_quaternion.w();
88+
realtime_pose_publisher_->msg_ = pose_msg;
8489
realtime_pose_publisher_->unlockAndPublish();
85-
90+
#endif
8691
publish_elapsed_ = publish_elapsed_ - publish_interval_;
8792
// clamp to publish only 1 time even if missed multiple intervals
8893
publish_elapsed_ = std::min(publish_elapsed_, publish_interval_);

src/twist_broadcaster.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,23 @@ TwistBroadcaster::update(const rclcpp::Time & time, const rclcpp::Duration & per
7777
(publish_elapsed_ >= publish_interval_) || (publish_interval_.nanoseconds() == 0);
7878

7979
if (should_publish && realtime_twist_publisher_) {
80+
geometry_msgs::msg::TwistStamped twist_msg;
81+
twist_msg.header.stamp = time;
82+
twist_msg.header.frame_id = params_.end_effector_frame;
83+
twist_msg.twist.linear.x = current_velocity.linear()[0];
84+
twist_msg.twist.linear.y = current_velocity.linear()[1];
85+
twist_msg.twist.linear.z = current_velocity.linear()[2];
86+
twist_msg.twist.angular.x = current_velocity.angular()[0];
87+
twist_msg.twist.angular.y = current_velocity.angular()[1];
88+
twist_msg.twist.angular.z = current_velocity.angular()[2];
89+
90+
#if REALTIME_TOOLS_NEW_API
91+
if (realtime_twist_publisher_->try_publish(twist_msg)) {
92+
#else
8093
if (realtime_twist_publisher_->trylock()) {
81-
auto & twist_msg = realtime_twist_publisher_->msg_;
82-
twist_msg.header.stamp = time;
83-
twist_msg.header.frame_id = params_.end_effector_frame;
84-
twist_msg.twist.linear.x = current_velocity.linear()[0];
85-
twist_msg.twist.linear.y = current_velocity.linear()[1];
86-
twist_msg.twist.linear.z = current_velocity.linear()[2];
87-
twist_msg.twist.angular.x = current_velocity.angular()[0];
88-
twist_msg.twist.angular.y = current_velocity.angular()[1];
89-
twist_msg.twist.angular.z = current_velocity.angular()[2];
94+
realtime_twist_publisher_->msg_ = twist_msg;
9095
realtime_twist_publisher_->unlockAndPublish();
91-
96+
#endif
9297
publish_elapsed_ = publish_elapsed_ - publish_interval_;
9398
// clamp to publish only 1 time even if missed multiple intervals
9499
publish_elapsed_ = std::min(publish_elapsed_, publish_interval_);

0 commit comments

Comments
 (0)