FP Perception 0.1.2
Loading...
Searching...
No Matches
opencv_vision_driver.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <opencv2/opencv.hpp>
5#include <cv_bridge/cv_bridge.hpp>
8
9namespace fp_perception
10{
19{
20public:
22 {
23 }
24 ~OpenCVDriver() override
25 {
27 }
28
36 void initialize(const rclcpp::Node::SharedPtr& node) override
37 {
38 // Configure parameters for the node
39 node->declare_parameter("driver.vision.OpenCVDriver.name", "OpenCVDriver");
40 node->declare_parameter("driver.vision.OpenCVDriver.device_id", 0);
41
42 // Load parameters from the node
43 name_ = node->get_parameter("driver.vision.OpenCVDriver.name").as_string();
44 device_id = node->get_parameter("driver.vision.OpenCVDriver.device_id").as_int();
45
46 // Initialize the base driver
47 initialize_base(node);
48
49 // Publish about the assigned driver parameters
50 RCLCPP_INFO(node_->get_logger(), "Assigned driver name: %s", name_.c_str());
51 RCLCPP_INFO(node_->get_logger(), "Assigned driver device_id: %d", device_id);
52
53 RCLCPP_INFO(node_->get_logger(), "OpenCVDriver starting on video device %d", device_id);
54
55 // open the camera device
57
58 // check if the camera opened successfully
59 if (!capture_device.isOpened())
60 {
61 RCLCPP_ERROR(node_->get_logger(), "OpenCVDriver failed to open video device ID: %d", device_id);
62 throw fp_perception_exception("OpenCVDriver failed to open video device ID: " + std::to_string(device_id));
63 }
64
66 {
67 enable_diagnostics("opencv-camera-" + std::to_string(device_id), name_ + " status",
68 [this](diagnostic_updater::DiagnosticStatusWrapper& status) { produce_diagnostics(status); });
69 }
70
71 RCLCPP_INFO(node_->get_logger(), "OpenCVDriver started on video device %d", device_id);
72 }
73
78 void deinitialize() override
79 {
81 if (capture_device.isOpened())
82 {
83 capture_device.release();
84 RCLCPP_INFO(node_->get_logger(), "OpenCVDriver stopped.");
85 }
86 }
87
95 {
96 if (!capture_device.isOpened())
97 throw fp_perception_exception("Camera device is not opened");
98
99 cv::Mat frame;
100
101 std::lock_guard<std::mutex> lock(buffer_mutex_);
102 capture_device >> frame;
103
104 if (frame.empty())
105 {
107 throw fp_perception_exception("Captured empty frame from camera");
108 }
109
111
112 vision_frame vision;
113 vision.image = frame;
114 vision.frame_id = name_;
115 vision.stamp = node_->now();
116
117 return vision;
118 }
123 void test() override
124 {
125 RCLCPP_INFO(node_->get_logger(), "OpenCVDriver test function called");
126
127 // Create the "test" directory if it doesn't exist
129
130 // Save image to the "test" folder
131 cv::imwrite("test/opencv_vision_image.jpg", captureFrame().image);
132
133 RCLCPP_INFO(node_->get_logger(), "OpenCVDriver test image saved to 'test/opencv_vision_image.jpg'. Test "
134 "completed.");
135 }
136
137protected:
138 void produce_diagnostics(diagnostic_updater::DiagnosticStatusWrapper& status)
139 {
140 if (!capture_device.isOpened())
141 status.summary(diagnostic_msgs::msg::DiagnosticStatus::ERROR, "Camera device is not open");
142 else if (capture_failure_count_.load() > 0)
143 status.summary(diagnostic_msgs::msg::DiagnosticStatus::WARN, "Camera capture has recent failures");
144 else
145 status.summary(diagnostic_msgs::msg::DiagnosticStatus::OK, "Camera capture healthy");
146
147 status.add("device_id", device_id);
148 status.add("device_open", capture_device.isOpened() ? "true" : "false");
149 status.add("successful_capture_count", successful_capture_count_.load());
150 status.add("capture_failure_count", capture_failure_count_.load());
151 }
152
154 cv::VideoCapture capture_device;
155 std::atomic<uint64_t> successful_capture_count_{ 0 };
156 std::atomic<uint64_t> capture_failure_count_{ 0 };
157};
158
159} // namespace fp_perception
void check_directory(std::string folder_name)
Check if the test directory exists, create it if not.
Definition driver_base.hpp:68
std::mutex buffer_mutex_
Mutex to protect access to the driver data.
Definition driver_base.hpp:149
std::string name_
Name of the driver.
Definition driver_base.hpp:143
rclcpp::Node::SharedPtr node_
ROS node for the driver.
Definition driver_base.hpp:138
void initialize_base(const rclcpp::Node::SharedPtr &node)
Initializer base driver in place of constructor due to plugin semantics.
Definition driver_base.hpp:95
void disable_diagnostics()
Definition driver_base.hpp:129
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
OpenCVDriver class for handling video input from a camera using OpenCV.
Definition opencv_vision_driver.hpp:19
std::atomic< uint64_t > capture_failure_count_
Definition opencv_vision_driver.hpp:156
std::atomic< uint64_t > successful_capture_count_
Definition opencv_vision_driver.hpp:155
void initialize(const rclcpp::Node::SharedPtr &node) override
Initialize the driver.
Definition opencv_vision_driver.hpp:36
cv::VideoCapture capture_device
Definition opencv_vision_driver.hpp:154
~OpenCVDriver() override
Definition opencv_vision_driver.hpp:24
int device_id
Definition opencv_vision_driver.hpp:153
void test() override
Test function to check the driver functionality by writing the image to a file This function creates ...
Definition opencv_vision_driver.hpp:123
void deinitialize() override
Stop driver streaming.
Definition opencv_vision_driver.hpp:78
void produce_diagnostics(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition opencv_vision_driver.hpp:138
OpenCVDriver()
Definition opencv_vision_driver.hpp:21
vision_frame captureFrame() override
Get the latest image data from the driver.
Definition opencv_vision_driver.hpp:94
Definition vision_source_driver.hpp:10
Definition audio_buffer.hpp:16
Base class for driver exceptions.
Definition exceptions.hpp:14
Definition structs.hpp:12
cv::Mat image
Definition structs.hpp:13
rclcpp::Time stamp
Definition structs.hpp:15
std::string frame_id
Definition structs.hpp:14