Skip to content

Commit 6869b56

Browse files
committed
Added async thread to log outside of update()
1 parent 4b834c0 commit 6869b56

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

ur_controllers/include/ur_controllers/freedrive_mode_controller.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@
3838

3939
#pragma once
4040

41-
#include <realtime_tools/realtime_buffer.h>
42-
#include <realtime_tools/realtime_server_goal_handle.h>
43-
4441
#include <memory>
4542
#include <string>
4643
#include <vector>
44+
#include <thread>
45+
#include <mutex>
4746

4847
#include <controller_interface/controller_interface.hpp>
4948
#include <rclcpp/rclcpp.hpp>
@@ -109,6 +108,15 @@ class FreedriveModeController : public controller_interface::ControllerInterface
109108
void start_timer();
110109
void timeout_callback();
111110

111+
std::thread logging_thread_;
112+
std::atomic<bool> logging_thread_running_;
113+
std::atomic<bool> logging_requested_;
114+
std::condition_variable logging_condition_;
115+
std::mutex log_mutex_;
116+
void log_task();
117+
void start_logging_thread();
118+
void stop_logging_thread();
119+
112120
static constexpr double ASYNC_WAITING = 2.0;
113121
/**
114122
* @brief wait until a command interface isn't in state ASYNC_WAITING anymore or until the parameter maximum_retries

ur_controllers/src/freedrive_mode_controller.cpp

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
162167
ur_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+
271313
bool FreedriveModeController::waitForAsyncCommand(std::function<double(void)> get_value)
272314
{
273315
const auto maximum_retries = freedrive_params_.check_io_successful_retries;

0 commit comments

Comments
 (0)