LibMultiSense
LibMultiSense Documentation
Loading...
Searching...
No Matches
MultiSenseMultiChannel.hh
Go to the documentation of this file.
1
37#pragma once
38
39#include <condition_variable>
40#include <deque>
41#include <mutex>
42
43#include "MultiSenseChannel.hh"
44
45namespace multisense {
46
55MULTISENSE_API bool frames_synchronized(const std::vector<ImageFrame> &frames, const std::chrono::nanoseconds &tolerance);
56
61public:
62
70 explicit MultiChannelSynchronizer(std::vector<std::unique_ptr<Channel>> channels,
71 const std::chrono::nanoseconds &tolerance,
72 size_t max_queue_size = 0):
73 m_owned_channels(std::move(channels)),
74 m_tolerance(tolerance),
75 m_max_queue_size(max_queue_size)
76 {
77 for (auto &channel : m_owned_channels)
78 {
79 const auto config = channel->get_config();
80
81 if (!config.time_config || !config.time_config->ptp_enabled)
82 {
83 throw std::runtime_error("Creating a MultiChannelSynchronizer with PTP disabled");
84 }
85
86 m_channels.push_back(channel.get());
87 }
88
89 m_active_frames.resize(m_channels.size());
90 add_user_callbacks();
91 }
92
100 explicit MultiChannelSynchronizer(std::vector<Channel*> channels,
101 const std::chrono::nanoseconds &tolerance,
102 size_t max_queue_size = 0):
103 m_channels(std::move(channels)),
104 m_tolerance(tolerance),
105 m_max_queue_size(max_queue_size)
106 {
107 m_active_frames.resize(m_channels.size());
108 add_user_callbacks();
109 }
110
112
116 Channel& channel(size_t index)
117 {
118 if (index >= m_channels.size())
119 {
120 throw std::runtime_error("Invalid multi-channel access");
121 }
122
123 return *m_channels.at(index);
124 }
125
131 std::optional<std::vector<ImageFrame>> get_synchronized_frame()
132 {
133 return get_synchronized_frame(std::nullopt);
134 }
135
143 std::optional<std::vector<ImageFrame>> get_synchronized_frame(const std::optional<std::chrono::nanoseconds> &timeout);
144
145private:
146
151
155 std::vector<Channel*> m_channels{};
156
160 std::vector<std::unique_ptr<Channel>> m_owned_channels{};
161
165 std::vector<ImageFrame> m_active_frames{};
166
170 std::chrono::nanoseconds m_tolerance{};
171
175 size_t m_max_queue_size = 0;
176
180 std::mutex m_frame_mutex;
181
185 std::condition_variable m_frame_cv;
186
190 std::deque<std::vector<ImageFrame>> m_ready_frames{};
191};
192
193}
Copyright 2013-2025 Carnegie Robotics, LLC 4501 Hatfield Street, Pittsburgh, PA 15201 http://www....
#define MULTISENSE_API
Helper class which provides a interface to synchronize data across multiple channels.
std::condition_variable m_frame_cv
Condition variable to notify the user a collection of synchronized frames is ready.
std::mutex m_frame_mutex
Mutex to notify the user a collection of synchronized frames is ready.
MultiChannelSynchronizer(std::vector< std::unique_ptr< Channel > > channels, const std::chrono::nanoseconds &tolerance, size_t max_queue_size=0)
Construct a synchronizer owning the underlying channels.
void add_user_callbacks()
Helper to add user callbacks to the input channels.
std::optional< std::vector< ImageFrame > > get_synchronized_frame()
Get a collection synchronized frames from the input channels with no timeout on waiting.
std::optional< std::vector< ImageFrame > > get_synchronized_frame(const std::optional< std::chrono::nanoseconds > &timeout)
Get a collection synchronized frames from the input channels and return if we have not recieved a col...
Channel & channel(size_t index)
Access a channel by index.
MultiChannelSynchronizer(std::vector< Channel * > channels, const std::chrono::nanoseconds &tolerance, size_t max_queue_size=0)
Construct a synchronizer without owning the underlying channels.
MULTISENSE_API bool frames_synchronized(const std::vector< ImageFrame > &frames, const std::chrono::nanoseconds &tolerance)
Free function which determines if a collection of frames are synchronized within a given tolerance.