-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from lbr-stack/dev-rolling-worker
Adds a worker class for re-use in asynchronous FT estimation
- Loading branch information
Showing
5 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef LBR_FRI_ROS2__WORKER_HPP_ | ||
#define LBR_FRI_ROS2__WORKER_HPP_ | ||
|
||
#include <atomic> | ||
#include <thread> | ||
|
||
#include "rclcpp/logger.hpp" | ||
#include "rclcpp/logging.hpp" | ||
#include "realtime_tools/thread_priority.hpp" | ||
|
||
#include "lbr_fri_ros2/formatting.hpp" | ||
|
||
namespace lbr_fri_ros2 { | ||
class Worker { | ||
protected: | ||
static constexpr char LOGGER_NAME[] = "lbr_fri_ros2::Worker"; | ||
|
||
public: | ||
Worker(); | ||
~Worker(); | ||
|
||
virtual void run_async(int rt_prio = 80); | ||
void request_stop(); | ||
|
||
protected: | ||
virtual void perform_work_() = 0; | ||
|
||
std::atomic_bool should_stop_, running_; | ||
std::thread run_thread_; | ||
}; | ||
} // namespace lbr_fri_ros2 | ||
#endif // LBR_FRI_ROS2__WORKER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "lbr_fri_ros2/worker.hpp" | ||
|
||
namespace lbr_fri_ros2 { | ||
Worker::Worker() : should_stop_(true), running_(false) {} | ||
|
||
Worker::~Worker() { | ||
this->request_stop(); | ||
if (run_thread_.joinable()) { | ||
run_thread_.join(); | ||
} | ||
} | ||
|
||
void Worker::run_async(int rt_prio) { | ||
if (running_) { | ||
RCLCPP_WARN_STREAM(rclcpp::get_logger(LOGGER_NAME), | ||
ColorScheme::WARNING << "Worker already running" << ColorScheme::ENDC); | ||
return; | ||
} | ||
run_thread_ = std::thread([this, rt_prio]() { | ||
if (!realtime_tools::configure_sched_fifo(rt_prio)) { | ||
RCLCPP_WARN_STREAM(rclcpp::get_logger(LOGGER_NAME), | ||
ColorScheme::WARNING | ||
<< "Failed to set FIFO realtime scheduling policy. Refer to " | ||
"[https://control.ros.org/master/doc/ros2_control/" | ||
"controller_manager/doc/userdoc.html]." | ||
<< ColorScheme::ENDC); | ||
} else { | ||
RCLCPP_INFO_STREAM(rclcpp::get_logger(LOGGER_NAME), | ||
ColorScheme::OKGREEN | ||
<< "Realtime scheduling policy set to FIFO with priority '" << rt_prio | ||
<< "'" << ColorScheme::ENDC); | ||
} | ||
|
||
RCLCPP_INFO(rclcpp::get_logger(LOGGER_NAME), "Starting run thread"); | ||
should_stop_ = false; | ||
|
||
// perform work in child-class | ||
this->perform_work_(); | ||
// perform work end | ||
|
||
running_ = false; | ||
RCLCPP_INFO(rclcpp::get_logger(LOGGER_NAME), "Exiting run thread"); | ||
}); | ||
} | ||
|
||
void Worker::request_stop() { | ||
if (!running_) { | ||
return; | ||
} | ||
RCLCPP_INFO(rclcpp::get_logger(LOGGER_NAME), "Requesting run thread stop"); | ||
should_stop_ = true; | ||
} | ||
} // namespace lbr_fri_ros2 |