Skip to content

Commit db0754f

Browse files
committed
removed stop thread
1 parent 0cc909f commit db0754f

File tree

2 files changed

+19
-79
lines changed

2 files changed

+19
-79
lines changed

ur_robot_driver/include/ur_robot_driver/hardware_interface.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,12 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
291291

292292
// Async thread handling
293293
std::shared_ptr<std::thread> async_moprim_cmd_thread_;
294-
std::shared_ptr<std::thread> async_moprim_stop_thread_;
295294
std::atomic_bool async_moprim_thread_shutdown_;
296295
std::mutex moprim_cmd_mutex_;
297-
std::mutex moprim_stop_mutex_;
298296

299297
// Command buffer for thread-safe communication
300298
std::vector<double> pending_moprim_cmd_;
301299
std::atomic_bool new_moprim_cmd_available_;
302-
std::atomic_bool new_moprim_stop_available_;
303-
std::atomic_bool new_moprim_reset_available_;
304300

305301
// Status for communication with controller
306302
bool motion_primitives_forward_controller_running_;
@@ -319,10 +315,7 @@ class URPositionHardwareInterface : public hardware_interface::SystemInterface
319315
void handleMoprimCommands();
320316
void resetMoprimCmdInterfaces();
321317
void asyncMoprimCmdThread();
322-
void asyncMoprimStopThread();
323318
void processMoprimMotionCmd(const std::vector<double>& command);
324-
void processMoprimStopCmd();
325-
void processMoprimResetCmd();
326319
bool getMoprimTimeOrVelAndAcc(const std::vector<double>& command, double& velocity, double& acceleration,
327320
double& move_time);
328321
bool getMoprimVelAndAcc(const std::vector<double>& command, double& velocity, double& acceleration,

ur_robot_driver/src/hardware_interface.cpp

Lines changed: 19 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ URPositionHardwareInterface::on_init(const hardware_interface::HardwareInfo& sys
148148
// Motion primitives stuff
149149
async_moprim_thread_shutdown_ = false;
150150
new_moprim_cmd_available_ = false;
151-
new_moprim_stop_available_ = false;
152-
new_moprim_reset_available_ = false;
153151
current_moprim_execution_status_ = MoprimExecutionState::IDLE;
154152
ready_for_new_moprim_ = false;
155153
motion_primitives_forward_controller_running_ = false;
@@ -722,7 +720,6 @@ URPositionHardwareInterface::on_configure(const rclcpp_lifecycle::State& previou
722720

723721
// Start async thread for sending motion primitives
724722
async_moprim_cmd_thread_ = std::make_shared<std::thread>(&URPositionHardwareInterface::asyncMoprimCmdThread, this);
725-
async_moprim_stop_thread_ = std::make_shared<std::thread>(&URPositionHardwareInterface::asyncMoprimStopThread, this);
726723

727724
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "System successfully started!");
728725

@@ -778,11 +775,6 @@ hardware_interface::CallbackReturn URPositionHardwareInterface::stop()
778775
async_moprim_cmd_thread_->join();
779776
async_moprim_cmd_thread_.reset();
780777
}
781-
if (async_moprim_stop_thread_) {
782-
async_moprim_thread_shutdown_ = true;
783-
async_moprim_stop_thread_->join();
784-
async_moprim_stop_thread_.reset();
785-
}
786778

787779
ur_driver_.reset();
788780

@@ -1590,12 +1582,18 @@ void URPositionHardwareInterface::check_passthrough_trajectory_controller()
15901582

15911583
void URPositionHardwareInterface::trajectory_done_callback(urcl::control::TrajectoryResult result)
15921584
{
1585+
RCLCPP_INFO(get_logger(), "Trajectory done callback called with result: %d", static_cast<int>(result));
15931586
if (result == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE) {
15941587
passthrough_trajectory_abort_ = 1.0;
15951588
} else {
15961589
passthrough_trajectory_abort_ = 0.0;
15971590
}
15981591
passthrough_trajectory_transfer_state_ = 5.0;
1592+
1593+
if (result == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_CANCELED) {
1594+
RCLCPP_INFO(get_logger(), "Robot stopped, TRAJECTORY_RESULT_CANCELED");
1595+
current_moprim_execution_status_ = MoprimExecutionState::STOPPED;
1596+
}
15991597
return;
16001598
}
16011599

@@ -1616,20 +1614,22 @@ void URPositionHardwareInterface::handleMoprimCommands()
16161614
switch (static_cast<uint8_t>(hw_moprim_commands_[0])) {
16171615
case static_cast<uint8_t>(MoprimMotionHelperType::STOP_MOTION):
16181616
{
1619-
std::lock_guard<std::mutex> guard(moprim_stop_mutex_);
1620-
if (!new_moprim_stop_available_) {
1621-
new_moprim_stop_available_ = true;
1622-
resetMoprimCmdInterfaces();
1623-
}
1617+
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received Motion Primitives STOP command");
1618+
resetMoprimCmdInterfaces();
1619+
build_moprim_sequence_ = false;
1620+
moprim_sequence_.clear(); // delete motion sequence
1621+
ur_driver_->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_CANCEL, -1,
1622+
urcl::RobotReceiveTimeout::millisec(2000));
1623+
current_moprim_execution_status_ = MoprimExecutionState::STOPPING;
1624+
ready_for_new_moprim_ = false;
16241625
break;
16251626
}
16261627
case static_cast<uint8_t>(MoprimMotionHelperType::RESET_STOP):
16271628
{
1628-
std::lock_guard<std::mutex> guard(moprim_stop_mutex_);
1629-
if (!new_moprim_reset_available_) {
1630-
new_moprim_reset_available_ = true;
1631-
resetMoprimCmdInterfaces();
1632-
}
1629+
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received RESET_STOP command");
1630+
resetMoprimCmdInterfaces();
1631+
current_moprim_execution_status_ = MoprimExecutionState::IDLE;
1632+
ready_for_new_moprim_ = true; // set to true to allow sending new commands
16331633
break;
16341634
}
16351635
default:
@@ -1656,58 +1656,6 @@ void URPositionHardwareInterface::resetMoprimCmdInterfaces()
16561656
std::fill(hw_moprim_commands_.begin(), hw_moprim_commands_.end(), std::numeric_limits<double>::quiet_NaN());
16571657
}
16581658

1659-
void URPositionHardwareInterface::asyncMoprimStopThread()
1660-
{
1661-
while (!async_moprim_thread_shutdown_) {
1662-
if (new_moprim_stop_available_) {
1663-
std::lock_guard<std::mutex> guard(moprim_stop_mutex_);
1664-
new_moprim_stop_available_ = false;
1665-
processMoprimStopCmd();
1666-
} else if (new_moprim_reset_available_) {
1667-
std::lock_guard<std::mutex> guard(moprim_stop_mutex_);
1668-
new_moprim_reset_available_ = false;
1669-
processMoprimResetCmd();
1670-
}
1671-
// Small sleep to prevent busy waiting
1672-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1673-
}
1674-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "[asyncMoprimStopThread] Exiting");
1675-
}
1676-
1677-
void URPositionHardwareInterface::processMoprimStopCmd()
1678-
{
1679-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received Motion Primitives STOP command");
1680-
// delete motion sequence
1681-
build_moprim_sequence_ = false;
1682-
moprim_sequence_.clear();
1683-
1684-
if (instruction_executor_->isTrajectoryRunning()) {
1685-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Stopping motion ...");
1686-
if (!instruction_executor_->cancelMotion()) {
1687-
RCLCPP_ERROR(rclcpp::get_logger("URPositionHardwareInterface"), "Failed to stop motion");
1688-
current_moprim_execution_status_ = MoprimExecutionState::ERROR;
1689-
} else {
1690-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Motion stopped successfully");
1691-
current_moprim_execution_status_ = MoprimExecutionState::STOPPED;
1692-
ready_for_new_moprim_ = false;
1693-
}
1694-
} else {
1695-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "No motion to stop");
1696-
current_moprim_execution_status_ = MoprimExecutionState::STOPPED;
1697-
ready_for_new_moprim_ = false;
1698-
}
1699-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"),
1700-
" [processMoprimStopCmd] After executing stop: current_moprim_execution_status_ = %d",
1701-
static_cast<uint8_t>(current_moprim_execution_status_));
1702-
}
1703-
1704-
void URPositionHardwareInterface::processMoprimResetCmd()
1705-
{
1706-
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received RESET_STOP command");
1707-
current_moprim_execution_status_ = MoprimExecutionState::IDLE;
1708-
ready_for_new_moprim_ = true; // set to true to allow sending new commands
1709-
}
1710-
17111659
void URPositionHardwareInterface::asyncMoprimCmdThread()
17121660
{
17131661
while (!async_moprim_thread_shutdown_) {
@@ -1745,8 +1693,7 @@ void URPositionHardwareInterface::processMoprimMotionCmd(const std::vector<doubl
17451693
case static_cast<uint8_t>(MoprimMotionHelperType::MOTION_SEQUENCE_START):
17461694
{
17471695
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Received MOTION_SEQUENCE_START: add all "
1748-
"following "
1749-
"commands to the motion sequence.");
1696+
"following commands to the motion sequence.");
17501697
build_moprim_sequence_ = true; // set flag to put all following commands into the motion sequence
17511698
moprim_sequence_.clear();
17521699
ready_for_new_moprim_ = true; // set to true to allow sending new commands

0 commit comments

Comments
 (0)