VOXL OpenVINS Server 1.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 Zauberflote
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 bool popCameraData(ov_core::CameraData &out) { return camera_queue.pop(out); }
108
109 /**
110 * @brief Get the camera pipe channel
111 * @return Pipe channel number
112 */
113 int get_channel() const { return channel_; }
114
115 /**
116 * @brief Get the camera identifier
117 * @return Camera identifier
118 */
119 size_t get_id() const { return camera_info_.cam_id; }
120
121 protected:
122 /**
123 * @brief Process incoming image data
124 *
125 * This method is called by the pipe callback when new image data arrives.
126 * Derived classes must implement this method to handle their specific
127 * image processing requirements.
128 *
129 * @param meta Image metadata containing timestamp and other information
130 * @param frame Pointer to image data buffer
131 */
132 virtual void process_image(const camera_image_metadata_t &meta, voxl::ImageType img_type, void *frame) = 0;
133
134 /**
135 * @brief Common callback function for pipe client
136 *
137 * This function receives raw image data from the pipe and dispatches
138 * it to the appropriate processing method. It serves as the entry
139 * point for all camera data processing.
140 *
141 * @param ch Channel number
142 * @param meta Image metadata
143 * @param frame Pointer to image data
144 * @param context Context pointer (the CameraBase instance)
145 */
146 static void camera_callback(int ch, camera_image_metadata_t meta, char *frame, void *context);
147
148 static void camera_device_buffer_callback(int ch, mpa_ion_buf_t *data, void *context);
149
150 // ============================================================================
151 // MEMBER VARIABLES
152 // ============================================================================
153
154 /** @brief Camera configuration information */
156
157 /** @brief Channel used for pipe communication */
158 int channel_{-1};
159
160 /** @brief Indicates if camera is connected to pipe */
161 bool is_connected_{false};
162
163 /** @brief Mutex for thread-safe operations */
164 std::mutex mutex_;
165
166 /** @brief OpenCL context used if GPU is enabled */
167 cl_context ctx_{nullptr};
168
169 /** @brief OpenCL command queue used if GPU is enabled */
170 cl_command_queue q_{nullptr};
171
172 /**
173 * @brief Lock-free SPSC queue for camera data
174 *
175 * Used for efficient communication between camera thread and VIO thread.
176 * Capacity of 64 provides approximately 2.4 seconds of buffering at 30fps.
177 */
178 boost::lockfree::spsc_queue<ov_core::CameraData, boost::lockfree::capacity<64>> camera_queue;
179
180 /** @brief Instance-local buffer for image processing */
182
183 /** @brief Per-instance reusable mask for feature tracking */
184 cv::Mat use_mask_;
185
186 /** @brief Indicates if frames should be dropped */
187 bool drop_frames{false};
188
189 /** @brief Indicates if jerk detection should be skipped */
191
192 };
193
194} // namespace voxl
195
196#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:187
cv::Mat use_mask_
Per-instance reusable mask for feature tracking.
Definition CameraBase.h:184
std::mutex mutex_
Mutex for thread-safe operations.
Definition CameraBase.h:164
int get_channel() const
Get the camera pipe channel.
Definition CameraBase.h:113
cl_context ctx_
OpenCL context used if GPU is enabled.
Definition CameraBase.h:167
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:155
virtual bool connect()
Initialize the camera pipe connection.
bool skip_jerk_detection
Indicates if jerk detection should be skipped.
Definition CameraBase.h:190
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:170
int channel_
Channel used for pipe communication.
Definition CameraBase.h:158
bool popCameraData(ov_core::CameraData &out)
Pop camera data from the queue.
Definition CameraBase.h:107
size_t get_id() const
Get the camera identifier.
Definition CameraBase.h:119
img_ringbuf_packet curr_message_
Instance-local buffer for image processing.
Definition CameraBase.h:181
boost::lockfree::spsc_queue< ov_core::CameraData, boost::lockfree::capacity< 64 > > camera_queue
Lock-free SPSC queue for camera data.
Definition CameraBase.h:178
bool is_connected_
Indicates if camera is connected to pipe.
Definition CameraBase.h:161
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:198
size_t cam_id
Unique camera identifier.
Definition VoxlCommon.h:209
Structure to hold image data packet for the ring buffer.
Definition VoxlVars.h:61