Skip to content

Commit 3d906dd

Browse files
committed
First version with timer
1 parent 22c1a05 commit 3d906dd

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

ur_controllers/include/ur_controllers/freedrive_mode_controller.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class FreedriveModeController : public controller_interface::ControllerInterface
9696

9797
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::Bool>> enable_freedrive_mode_sub_;
9898

99-
rclcpp::TimerBase::SharedPtr goal_handle_timer_; ///< Timer to frequently check on the running goal
100-
rclcpp::Duration action_monitor_period_ = rclcpp::Duration(50ms);
99+
rclcpp::TimerBase::SharedPtr freedrive_sub_timer_; ///< Timer to check on the topic
100+
std::chrono::seconds timeout_interval_ = std::chrono::seconds(2);
101101
void readFreedriveModeCmd(const std_msgs::msg::Bool::SharedPtr msg);
102102

103103
std::shared_ptr<freedrive_mode_controller::ParamListener> freedrive_param_listener_;
@@ -107,6 +107,9 @@ class FreedriveModeController : public controller_interface::ControllerInterface
107107
std::atomic<bool> change_requested_;
108108
std::atomic<double> async_state_;
109109
std::atomic<double> first_log_;
110+
std::atomic<double> timer_started_;
111+
112+
void timeout_callback();
110113

111114
static constexpr double ASYNC_WAITING = 2.0;
112115
/**

ur_controllers/src/freedrive_mode_controller.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ ur_controllers::FreedriveModeController::on_configure(const rclcpp_lifecycle::St
8888
"~/freedrive_mode_active", 10,
8989
std::bind(&FreedriveModeController::readFreedriveModeCmd, this, std::placeholders::_1));
9090

91+
timer_started_ = false;
92+
9193
const auto logger = get_node()->get_logger();
9294

9395
if (!freedrive_param_listener_) {
@@ -218,6 +220,17 @@ controller_interface::return_type ur_controllers::FreedriveModeController::updat
218220

219221
void FreedriveModeController::readFreedriveModeCmd(const std_msgs::msg::Bool::SharedPtr msg)
220222
{
223+
if (!timer_started_)
224+
{
225+
// Start the timer only after the first message is received
226+
freedrive_sub_timer_ = get_node()->create_wall_timer(
227+
timeout_interval_,
228+
std::bind(&FreedriveModeController::timeout_callback, this));
229+
timer_started_ = true;
230+
231+
RCLCPP_INFO(get_node()->get_logger(), "Timer started after receiving first command.");
232+
}
233+
221234
// Process the freedrive_mode command.
222235
if(msg->data)
223236
{
@@ -231,6 +244,24 @@ void FreedriveModeController::readFreedriveModeCmd(const std_msgs::msg::Bool::Sh
231244
change_requested_ = true;
232245
}
233246
}
247+
248+
if (freedrive_sub_timer_)
249+
{
250+
freedrive_sub_timer_->reset();
251+
}
252+
}
253+
254+
void FreedriveModeController::timeout_callback()
255+
{
256+
257+
if(timer_started_){
258+
RCLCPP_INFO(get_node()->get_logger(), "Freedrive mode will be deactivated since client is not reachable.");
259+
260+
freedrive_active_ = false;
261+
change_requested_ = true;
262+
}
263+
264+
timer_started_ = false;
234265
}
235266

236267
// Timeout handling for the topic

0 commit comments

Comments
 (0)