VOXL OpenVINS Server 0.7.0
Visual Inertial Odometry Server for VOXL Platform
Loading...
Searching...
No Matches
CameraBase.h
Go to the documentation of this file.
1/**
2 * @file CameraBase.h
3 * @brief Base class for camera implementations in VOXL OpenVINS
4 * @author Joao Leonardo Silva Cotta (@zauberflote1)
5 * @date 2025
6 * @version 1.0
7 *
8 * This header defines the abstract base class for all camera implementations
9 * in the VOXL OpenVINS system. It provides the interface for camera handling,
10 * pipe communication, image processing, and VIO integration.
11 */
12
13#ifndef CAMERABASE_H
14#define CAMERABASE_H
15
16#pragma once
17
18// Standard includes
19#include <memory>
20#include <iostream>
21#include <pthread.h>
22#include <string>
23#include <mutex>
24#include <vector>
25#include <functional>
26#include <optional>
27
28// Third-party includes
29#include <opencv2/opencv.hpp>
30#include <modal_pipe.h>
31#include <boost/lockfree/spsc_queue.hpp>
32#include <core/VioManager.h>
33#include <core/VioManagerOptions.h>
34
35// Local includes
36#include "VoxlVars.h"
37
38namespace voxl
39{
40
41 /**
42 * @class CameraBase
43 * @brief Base class for all camera implementations
44 *
45 * This abstract class defines the interface for camera handlers in the system.
46 * It provides the foundation for different camera types (mono, stereo, etc.)
47 * and handles pipe communication, image processing, and VIO integration.
48 *
49 * The class manages:
50 * - Pipe client connections for image data
51 * - Image processing and feature extraction
52 * - Integration with the VIO manager
53 * - Thread-safe operations
54 * - OpenCL context for GPU acceleration
55 */
57 {
58 public:
59 /**
60 * @brief Constructor
61 * @param camera_info Camera configuration information
62 */
63 explicit CameraBase(const cam_info &camera_info);
64
65 /**
66 * @brief Virtual destructor
67 */
68 virtual ~CameraBase() = default;
69
70 /**
71 * @brief Initialize the camera pipe connection
72 *
73 * This method establishes the connection to the camera pipe service
74 * and sets up the necessary callbacks for image data reception.
75 *
76 * @return True if successful, false otherwise
77 */
78 virtual bool connect();
79
80 /**
81 * @brief Disconnect and clean up resources
82 *
83 * This method properly closes the pipe connection and cleans up
84 * any allocated resources.
85 */
86 virtual void disconnect();
87
88 /**
89 * @brief Get the camera information
90 * @return Camera information structure
91 */
92 const cam_info &get_camera_info() const { return camera_info_; }
93
94 /**
95 * @brief Pop camera data from the queue
96 *
97 * Retrieves the next available camera data from the internal queue.
98 * This method is used to get processed image data for VIO processing.
99 *
100 * The method uses a lock-free SPSC (Single Producer, Single Consumer)
101 * queue for efficient thread-safe data transfer between the camera
102 * callback thread and the VIO processing thread.
103 *
104 * @param out Reference to store the popped data
105 * @return true if data was popped, false if queue is empty
106 */
107
108 /**
109 * @brief Get the camera pipe channel
110 * @return Pipe channel number
111 */
112 int get_channel() const { return channel_; }
113
114 /**
115 * @brief Get the camera identifier
116 * @return Camera identifier
117 */
118 size_t get_id() const { return camera_info_.cam_id; }
119
120 protected:
121 /**
122 * @brief Process incoming image data
123 *
124 * This method is called by the pipe callback when new image data arrives.
125 * Derived classes must implement this method to handle their specific
126 * image processing requirements.
127 *
128 * @param meta Image metadata containing timestamp and other information
129 * @param frame Pointer to image data buffer
130 */
131 virtual void process_image(const camera_image_metadata_t &meta, voxl::ImageType img_type, void *frame) = 0;
132
133 /**
134 * @brief Common callback function for pipe client
135 *
136 * This function receives raw image data from the pipe and dispatches
137 * it to the appropriate processing method. It serves as the entry
138 * point for all camera data processing.
139 *
140 * @param ch Channel number
141 * @param meta Image metadata
142 * @param frame Pointer to image data
143 * @param context Context pointer (the CameraBase instance)
144 */
145 static void camera_callback(int ch, camera_image_metadata_t meta, char *frame, void *context);
146
147 static void camera_device_buffer_callback(int ch, mpa_ion_buf_t *data, void *context);
148
149 // ============================================================================
150 // MEMBER VARIABLES
151 // ============================================================================
152
153 /** @brief Camera configuration information */
155
156 /** @brief Channel used for pipe communication */
157 int channel_{-1};
158
159 /** @brief Indicates if camera is connected to pipe */
160 bool is_connected_{false};
161
162 /** @brief Mutex for thread-safe operations */
163 std::mutex mutex_;
164
165 /** @brief OpenCL context used if GPU is enabled */
166 cl_context ctx_{nullptr};
167
168 /** @brief OpenCL command queue used if GPU is enabled */
169 cl_command_queue q_{nullptr};
170
171 /**
172 * @brief Lock-free SPSC queue for camera data
173 *
174 * Used for efficient communication between camera thread and VIO thread.
175 * Capacity of 64 provides approximately 2.4 seconds of buffering at 30fps.
176 */
177
178 /** @brief Instance-local buffer for image processing */
180
181 /** @brief Per-instance reusable mask for feature tracking */
182 cv::Mat use_mask_;
183
184 /** @brief Indicates if frames should be dropped */
185 bool drop_frames{false};
186
187 /** @brief Indicates if jerk detection should be skipped */
189
190 };
191
192} // namespace voxl
193
194#endif // CAMERABASE_H
Global variable declarations and constants for VOXL OpenVINS server.
Base class for all camera implementations.
Definition CameraBase.h:57
bool drop_frames
Indicates if frames should be dropped.
Definition CameraBase.h:185
cv::Mat use_mask_
Per-instance reusable mask for feature tracking.
Definition CameraBase.h:182
std::mutex mutex_
Mutex for thread-safe operations.
Definition CameraBase.h:163
int get_channel() const
Pop camera data from the queue.
Definition CameraBase.h:112
cl_context ctx_
OpenCL context used if GPU is enabled.
Definition CameraBase.h:166
const cam_info & get_camera_info() const
Get the camera information.
Definition CameraBase.h:92
cam_info camera_info_
Camera configuration information.
Definition CameraBase.h:154
virtual bool connect()
Initialize the camera pipe connection.
bool skip_jerk_detection
Indicates if jerk detection should be skipped.
Definition CameraBase.h:188
static void camera_callback(int ch, camera_image_metadata_t meta, char *frame, void *context)
Common callback function for pipe client.
virtual void disconnect()
Disconnect and clean up resources.
cl_command_queue q_
OpenCL command queue used if GPU is enabled.
Definition CameraBase.h:169
int channel_
Channel used for pipe communication.
Definition CameraBase.h:157
size_t get_id() const
Get the camera identifier.
Definition CameraBase.h:118
img_ringbuf_packet curr_message_
Lock-free SPSC queue for camera data.
Definition CameraBase.h:179
bool is_connected_
Indicates if camera is connected to pipe.
Definition CameraBase.h:160
virtual void process_image(const camera_image_metadata_t &meta, voxl::ImageType img_type, void *frame)=0
Process incoming image data.
virtual ~CameraBase()=default
Virtual destructor.
Main namespace for VOXL OpenVINS server components.
Camera information and calibration data.
Definition VoxlCommon.h:146
size_t cam_id
Unique camera identifier.
Definition VoxlCommon.h:151
Structure to hold image data packet for the ring buffer.
Definition VoxlVars.h:61