FP Perception 0.1.2
Loading...
Searching...
No Matches
driver_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <chrono>
5#include <functional>
6#include <mutex>
7#include <memory>
8#include <filesystem>
9#include <atomic>
10
11#include <diagnostic_updater/diagnostic_updater.hpp>
12#include <rclcpp/rclcpp.hpp>
14
15namespace fp_perception
16{
17
19{
20public:
21 virtual ~DriverBase() = default;
22
30 virtual void initialize(const rclcpp::Node::SharedPtr& node)
31 {
32 initialize_base(node);
33 }
34
39 virtual void deinitialize() = 0;
40
46 virtual std::string getName() const
47 {
48 return name_;
49 }
50
55 virtual void test()
56 {
57 RCLCPP_INFO(node_->get_logger(), "Driver test function called for driver: %s", name_.c_str());
58 }
59
68 void check_directory(std::string folder_name)
69 {
70 // Create "test" directory if it doesn't exist
71 const std::filesystem::path dir(folder_name);
72 if (!std::filesystem::exists(dir))
73 {
74 std::filesystem::create_directory(dir);
75 }
76 }
77
78 const std::filesystem::path check_file(const std::string& file_name)
79 {
80 // Check if the file exists
81 const std::filesystem::path file_path(file_name);
82 if (!std::filesystem::exists(file_path))
83 {
84 throw fp_perception_exception("File does not exist: " + file_name);
85 }
86 return file_path;
87 }
88
89protected:
95 void initialize_base(const rclcpp::Node::SharedPtr& node)
96 {
97 node_ = node;
98
99 if (!node_->has_parameter("use_diagnostics"))
100 node_->declare_parameter("use_diagnostics", false);
101
102 diagnostics_enabled_ = node_->get_parameter("use_diagnostics").as_bool();
103 }
104
106 {
108 }
109
110 void enable_diagnostics(const std::string& hardware_id, const std::string& task_name,
111 std::function<void(diagnostic_updater::DiagnosticStatusWrapper&)> task,
112 std::chrono::milliseconds period = std::chrono::seconds(1))
113 {
115 return;
116
117 diagnostics_timer_.reset();
118 diagnostics_updater_.reset();
119
120 diagnostics_updater_ = std::make_unique<diagnostic_updater::Updater>(node_);
121 diagnostics_updater_->setHardwareID(hardware_id.empty() ? name_ : hardware_id);
122 diagnostics_updater_->add(task_name, std::move(task));
123 diagnostics_timer_ = node_->create_wall_timer(period, [this]() {
125 diagnostics_updater_->force_update();
126 });
127 }
128
130 {
131 diagnostics_timer_.reset();
132 diagnostics_updater_.reset();
133 }
134
138 rclcpp::Node::SharedPtr node_;
139
143 std::string name_;
144
149 std::mutex buffer_mutex_;
150
155 std::condition_variable buffer_cv_;
156
161 std::atomic<bool> is_running_{ false };
162
166 bool diagnostics_enabled_{ false };
167
168 std::unique_ptr<diagnostic_updater::Updater> diagnostics_updater_;
169 rclcpp::TimerBase::SharedPtr diagnostics_timer_;
170};
171} // namespace fp_perception
Definition driver_base.hpp:19
virtual void initialize(const rclcpp::Node::SharedPtr &node)
Initialize the driver.
Definition driver_base.hpp:30
virtual ~DriverBase()=default
void check_directory(std::string folder_name)
Check if the test directory exists, create it if not.
Definition driver_base.hpp:68
virtual std::string getName() const
Get the name of the driver.
Definition driver_base.hpp:46
virtual void test()
Test the driver.
Definition driver_base.hpp:55
std::mutex buffer_mutex_
Mutex to protect access to the driver data.
Definition driver_base.hpp:149
std::atomic< bool > is_running_
Flag to indicate if the driver thread is running.
Definition driver_base.hpp:161
std::string name_
Name of the driver.
Definition driver_base.hpp:143
std::condition_variable buffer_cv_
Condition variable to notify when new data is available.
Definition driver_base.hpp:155
rclcpp::Node::SharedPtr node_
ROS node for the driver.
Definition driver_base.hpp:138
bool diagnostics_enabled_
Flag to enable standard ROS diagnostics publishing.
Definition driver_base.hpp:166
void initialize_base(const rclcpp::Node::SharedPtr &node)
Initializer base driver in place of constructor due to plugin semantics.
Definition driver_base.hpp:95
rclcpp::TimerBase::SharedPtr diagnostics_timer_
Definition driver_base.hpp:169
const std::filesystem::path check_file(const std::string &file_name)
Definition driver_base.hpp:78
std::unique_ptr< diagnostic_updater::Updater > diagnostics_updater_
Definition driver_base.hpp:168
void disable_diagnostics()
Definition driver_base.hpp:129
virtual void deinitialize()=0
Deinitialize the driver.
bool diagnostics_enabled() const
Definition driver_base.hpp:105
void enable_diagnostics(const std::string &hardware_id, const std::string &task_name, std::function< void(diagnostic_updater::DiagnosticStatusWrapper &)> task, std::chrono::milliseconds period=std::chrono::seconds(1))
Definition driver_base.hpp:110
Definition audio_buffer.hpp:16
Base class for driver exceptions.
Definition exceptions.hpp:14