Skip to content

Commit 87f6551

Browse files
committed
Replaced moprim_cmd_mutex_ with realtime_tools::LockFreeSPSCQueue<std::array<double, 25>, 1> → therefore changed command and state interfaces from std::vector<double> to std::array<double, 25>.
1 parent 5cae525 commit 87f6551

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

ur_robot_driver/include/ur_robot_driver/hardware_interface.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <string>
4949
#include <unordered_map>
5050
#include <vector>
51+
#include <array>
5152

5253
// ros2_control hardware_interface
5354
#include "hardware_interface/hardware_info.hpp"
@@ -67,10 +68,11 @@
6768
#include "rclcpp_lifecycle/state.hpp"
6869
#include "geometry_msgs/msg/transform_stamped.hpp"
6970
#include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
71+
#include "control_msgs/msg/motion_primitive.hpp"
72+
#include <realtime_tools/lock_free_queue.hpp>
7073

7174
// Motion primitives controller
7275
#include "motion_primitives_forward_controller/motion_primitives_forward_controller.hpp"
73-
#include "control_msgs/msg/motion_primitive.hpp"
7476

7577
using MoprimMotionType = control_msgs::msg::MotionPrimitive;
7678
using MoprimMotionHelperType = motion_primitives_forward_controller::MotionHelperType;
@@ -292,10 +294,7 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
292294
// Async thread handling
293295
std::shared_ptr<std::thread> async_moprim_cmd_thread_;
294296
std::atomic_bool async_moprim_thread_shutdown_;
295-
std::mutex moprim_cmd_mutex_;
296-
297-
// Command buffer for thread-safe communication
298-
std::vector<double> pending_moprim_cmd_;
297+
realtime_tools::LockFreeSPSCQueue<std::array<double, 25>, 1> moprim_cmd_queue_;
299298
std::atomic_bool new_moprim_cmd_available_;
300299

301300
// Status for communication with controller
@@ -305,8 +304,11 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
305304
std::atomic_bool ready_for_new_moprim_;
306305

307306
// Command and state interfaces for the motion primitives
308-
std::vector<double> hw_moprim_commands_;
309-
std::vector<double> hw_moprim_states_;
307+
// 25 Commands: motion_type + 6 joints + 2*7 positions (goal and via) + blend_radius + velocity + acceleration +
308+
// move_time
309+
std::array<double, 25> hw_moprim_commands_;
310+
// 2 States: execution_status, ready_for_new_primitive
311+
std::array<double, 2> hw_moprim_states_;
310312

311313
// flag to put all following primitives into a motion sequence instead of sending single primitives
312314
std::atomic_bool build_moprim_sequence_{ false };
@@ -315,10 +317,10 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
315317
void handleMoprimCommands();
316318
void resetMoprimCmdInterfaces();
317319
void asyncMoprimCmdThread();
318-
void processMoprimMotionCmd(const std::vector<double>& command);
319-
bool getMoprimTimeOrVelAndAcc(const std::vector<double>& command, double& velocity, double& acceleration,
320+
void processMoprimMotionCmd(const std::array<double, 25>& command);
321+
bool getMoprimTimeOrVelAndAcc(const std::array<double, 25>& command, double& velocity, double& acceleration,
320322
double& move_time);
321-
bool getMoprimVelAndAcc(const std::vector<double>& command, double& velocity, double& acceleration,
323+
bool getMoprimVelAndAcc(const std::array<double, 25>& command, double& velocity, double& acceleration,
322324
double& move_time);
323325
void quaternionToRotVec(double qx, double qy, double qz, double qw, double& rx, double& ry, double& rz);
324326

ur_robot_driver/src/hardware_interface.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ URPositionHardwareInterface::on_init(const hardware_interface::HardwareInfo& sys
151151
current_moprim_execution_status_ = MoprimExecutionState::IDLE;
152152
ready_for_new_moprim_ = false;
153153
motion_primitives_forward_controller_running_ = false;
154-
// 2 States: execution_status, ready_for_new_primitive
155-
hw_moprim_states_.resize(2, std::numeric_limits<double>::quiet_NaN());
156-
// 25 Commands: // motion_type + 6 joints + 2*7 positions (goal and via) + blend_radius + velocity + acceleration +
157-
// move_time
158-
hw_moprim_commands_.resize(25, std::numeric_limits<double>::quiet_NaN());
154+
hw_moprim_states_.fill(std::numeric_limits<double>::quiet_NaN());
155+
hw_moprim_commands_.fill(std::numeric_limits<double>::quiet_NaN());
159156

160157
for (const hardware_interface::ComponentInfo& joint : info_.joints) {
161158
if (joint.command_interfaces.size() != 2) {
@@ -1617,9 +1614,9 @@ void URPositionHardwareInterface::handleMoprimCommands()
16171614
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received Motion Primitives STOP command");
16181615
resetMoprimCmdInterfaces();
16191616
build_moprim_sequence_ = false;
1620-
moprim_sequence_.clear(); // delete motion sequence
1617+
moprim_sequence_.clear(); // delete motion sequence
16211618
ur_driver_->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_CANCEL, -1,
1622-
urcl::RobotReceiveTimeout::millisec(2000));
1619+
urcl::RobotReceiveTimeout::millisec(2000));
16231620
current_moprim_execution_status_ = MoprimExecutionState::STOPPING;
16241621
ready_for_new_moprim_ = false;
16251622
break;
@@ -1634,10 +1631,12 @@ void URPositionHardwareInterface::handleMoprimCommands()
16341631
}
16351632
default:
16361633
{
1637-
std::lock_guard<std::mutex> guard(moprim_cmd_mutex_);
16381634
if (!new_moprim_cmd_available_) {
1639-
// Copy command to thread-safe buffer
1640-
pending_moprim_cmd_ = hw_moprim_commands_;
1635+
// Push command to thread-safe queue
1636+
if (!moprim_cmd_queue_.push(hw_moprim_commands_)) {
1637+
RCLCPP_ERROR(rclcpp::get_logger("URPositionHardwareInterface"), "Failed to push command to "
1638+
"moprim_cmd_queue_");
1639+
}
16411640
new_moprim_cmd_available_ = true;
16421641
resetMoprimCmdInterfaces();
16431642
}
@@ -1661,13 +1660,12 @@ void URPositionHardwareInterface::asyncMoprimCmdThread()
16611660
while (!async_moprim_thread_shutdown_) {
16621661
// Check for new commands
16631662
if (new_moprim_cmd_available_) {
1664-
// RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "[asyncMoprimCmdThread] New command available");
1665-
std::vector<double> current_command;
1666-
{
1667-
std::lock_guard<std::mutex> guard(moprim_cmd_mutex_);
1668-
current_command = pending_moprim_cmd_;
1669-
new_moprim_cmd_available_ = false;
1663+
std::array<double, 25> current_command;
1664+
if (!moprim_cmd_queue_.pop(current_command)) {
1665+
RCLCPP_ERROR(rclcpp::get_logger("URPositionHardwareInterface"), "Failed to pop the command from "
1666+
"moprim_cmd_queue_.");
16701667
}
1668+
new_moprim_cmd_available_ = false;
16711669

16721670
// Process the command
16731671
processMoprimMotionCmd(current_command);
@@ -1679,9 +1677,9 @@ void URPositionHardwareInterface::asyncMoprimCmdThread()
16791677
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "[asyncMoprimCmdThread] Exiting");
16801678
}
16811679

1682-
void URPositionHardwareInterface::processMoprimMotionCmd(const std::vector<double>& command)
1680+
void URPositionHardwareInterface::processMoprimMotionCmd(const std::array<double, 25>& command)
16831681
{
1684-
if (command.empty() || std::isnan(command[0])) {
1682+
if (std::isnan(command[0])) {
16851683
return;
16861684
}
16871685
double velocity, acceleration, move_time;
@@ -1906,7 +1904,7 @@ void URPositionHardwareInterface::quaternionToRotVec(double qx, double qy, doubl
19061904
rz = axis.z() * angle; // rz
19071905
}
19081906

1909-
bool URPositionHardwareInterface::getMoprimTimeOrVelAndAcc(const std::vector<double>& command, double& velocity,
1907+
bool URPositionHardwareInterface::getMoprimTimeOrVelAndAcc(const std::array<double, 25>& command, double& velocity,
19101908
double& acceleration, double& move_time)
19111909
{
19121910
// Check if move_time is valid
@@ -1931,7 +1929,7 @@ bool URPositionHardwareInterface::getMoprimTimeOrVelAndAcc(const std::vector<dou
19311929
}
19321930
}
19331931

1934-
bool URPositionHardwareInterface::getMoprimVelAndAcc(const std::vector<double>& command, double& velocity,
1932+
bool URPositionHardwareInterface::getMoprimVelAndAcc(const std::array<double, 25>& command, double& velocity,
19351933
double& acceleration, double& move_time)
19361934
{
19371935
// Check if velocity and acceleration are valid

0 commit comments

Comments
 (0)