@@ -104,6 +104,8 @@ ur_controllers::FreedriveModeController::on_configure(const rclcpp_lifecycle::St
104104 // Get parameters from the listener in case they were updated
105105 freedrive_params_ = freedrive_param_listener_->get_params ();
106106
107+ start_logging_thread ();
108+
107109 return ControllerInterface::on_configure (previous_state);
108110}
109111
@@ -112,9 +114,12 @@ ur_controllers::FreedriveModeController::on_activate(const rclcpp_lifecycle::Sta
112114{
113115 change_requested_ = false ;
114116 freedrive_active_ = false ;
115- first_log_ = false ;
116117 async_state_ = std::numeric_limits<double >::quiet_NaN ();
117118
119+ first_log_ = false ;
120+ logging_thread_running_ = false ;
121+ logging_requested_ = false ;
122+
118123 {
119124 const std::string interface_name = freedrive_params_.tf_prefix + " freedrive_mode/"
120125 " async_success" ;
@@ -162,12 +167,10 @@ controller_interface::CallbackReturn
162167ur_controllers::FreedriveModeController::on_deactivate (const rclcpp_lifecycle::State&)
163168{
164169 abort_command_interface_->get ().set_value (1.0 );
165-
166- // Set enable value to false, so in the update
167- // we can deactivate the freedrive mode
168- // Old comment?
169170 freedrive_active_ = false ;
170171
172+ stop_logging_thread ();
173+
171174 return CallbackReturn::SUCCESS;
172175}
173176
@@ -208,12 +211,12 @@ controller_interface::return_type ur_controllers::FreedriveModeController::updat
208211 }
209212
210213 if ((async_state_ == 1.0 ) && (first_log_)){
211- if (freedrive_active_){
212- RCLCPP_INFO (get_node ()->get_logger (), " Freedrive mode has been enabled successfully." );
213- } else {
214- RCLCPP_INFO (get_node ()->get_logger (), " Freedrive mode has been disabled successfully." );
215- }
216214 first_log_ = false ;
215+ logging_requested_= true ;
216+
217+ // Notify logging thread
218+ logging_condition_.notify_one ();
219+
217220 }
218221 return controller_interface::return_type::OK;
219222}
@@ -232,7 +235,6 @@ void FreedriveModeController::readFreedriveModeCmd(const std_msgs::msg::Bool::Sh
232235 if ((freedrive_active_) && (!change_requested_)){
233236 freedrive_active_ = false ;
234237 change_requested_ = true ;
235- // start_timer();
236238 }
237239 }
238240
@@ -268,6 +270,46 @@ void FreedriveModeController::timeout_callback()
268270 timer_started_ = false ;
269271}
270272
273+ void FreedriveModeController::start_logging_thread () {
274+ if (!logging_thread_running_) {
275+ logging_thread_running_ = true ;
276+ logging_thread_ = std::thread (&FreedriveModeController::log_task, this );
277+ }
278+ }
279+
280+ void FreedriveModeController::stop_logging_thread () {
281+ logging_thread_running_ = false ;
282+ if (logging_thread_.joinable ()) {
283+ logging_thread_.join ();
284+ }
285+ }
286+
287+ void FreedriveModeController::log_task () {
288+ while (logging_thread_running_) {
289+
290+ std::unique_lock<std::mutex> lock (log_mutex_);
291+
292+ auto condition = [this ] {
293+ return !logging_thread_running_ || logging_requested_;
294+ };
295+
296+ // Wait for the condition
297+ logging_condition_.wait (lock, condition);
298+
299+
300+ if (!logging_thread_running_) break ;
301+
302+ if (freedrive_active_){
303+ RCLCPP_INFO (get_node ()->get_logger (), " Freedrive mode has been enabled successfully." );
304+ } else {
305+ RCLCPP_INFO (get_node ()->get_logger (), " Freedrive mode has been disabled successfully." );
306+ }
307+
308+ // Reset to log only once
309+ logging_requested_ = false ;
310+ }
311+ }
312+
271313bool FreedriveModeController::waitForAsyncCommand (std::function<double (void )> get_value)
272314{
273315 const auto maximum_retries = freedrive_params_.check_io_successful_retries ;
0 commit comments