event_detector 1.0.0
Loading...
Searching...
No Matches
BufferManager.hpp
1
25#pragma once
26
27#include <condition_variable>
28#include <memory>
29#include <mutex>
30#include <shared_mutex>
31#include <string>
32#include <vector>
33
34#include <tf2_ros/buffer.h>
35#include <tf2_ros/transform_listener.h>
36#include <rclcpp/rclcpp.hpp>
37
38#include "event_detector/DifferenceBasedBuffer.hpp"
39#include "event_detector/common.hpp"
40
41namespace event_detector {
42
43template <typename T>
45
55 public:
65 template <typename T>
67 public:
72
79
89 void update(const std::vector<ClientConnection>& clients);
90
103 void insert(const typename std::shared_ptr<const T>& sample, const rclcpp::Time& stamp,
104 const std::string& client_id, const std::string& topic,
105 const bool clear_if_time_is_running_backwards = true);
106
112 std::vector<std::string> getClients();
113
121 std::vector<std::string> getFrames(const std::string& client_id);
122
137 std::vector<Stamped<T>> get(const std::string& client_id = "", const std::string& frame_id = "",
138 const std::string& topic = "");
139
157 std::vector<Stamped<T>> get(const int k, const std::string& client_id = "", const std::string& frame_id = "",
158 const std::string& topic = "");
159
178 std::vector<Stamped<T>> get(const rclcpp::Time& start_time, const rclcpp::Time& end_time,
179 const std::string& client_id = "", const std::string& frame_id = "",
180 const std::string& topic = "");
181
202 std::vector<Stamped<T>> get(const int k, const rclcpp::Time& start_time, const rclcpp::Time& end_time,
203 const std::string& client_id = "", const std::string& frame_id = "",
204 const std::string& topic = "");
205
217 string_map<std::vector<Stamped<T>>> getWithTopic(const int k, const rclcpp::Time& start_time,
218 const rclcpp::Time& end_time, const std::string& client_id = "",
219 const std::string& frame_id = "", const std::string& topic = "");
220
226 std::string getInfo() const;
227
234 void clearBuffer(const std::string& client_id, const std::string& topic);
235
245 bool timeIsRunningBackwards(const std::string& client_id, const std::string& topic, const rclcpp::Time& stamp);
246
247 protected:
252
256 string_map_2d<Buffer<std::shared_ptr<const T>>> buffer_by_client_topic_;
257
261 string_map_2d<std::string> frame_by_client_topic_;
262
266 string_map_2d<rclcpp::Time> last_stamp_by_client_topic_;
267
271 string_map_2d<std::shared_ptr<std::shared_mutex>> mutex_by_client_topic_;
272
276 mutable std::shared_mutex reconfigure_mutex_;
277 };
278
279 public:
284
296 void lockBuffers(const bool is_write);
297
306 void unlockBuffers(const bool is_write);
307
313 std::string getInfo() const;
314
315 public:
319 mutable std::mutex buffer_lock_mutex_;
320
324 std::condition_variable buffer_lock_condition_variable_;
325
330
331#define DATATYPE(TYPE, VAR) \
332 \
333 DataBuffers<TYPE> VAR;
334#include "event_detector/datatypes.macro"
335#undef DATATYPE
336};
337
338} // namespace event_detector
339
340#include "event_detector/BufferManager.tpp"
Ring-buffer-like container for keeping only values within a fixed range of a secondary index paramete...
Template class designed to hold all buffers for a specific data type.
string_map_2d< rclcpp::Time > last_stamp_by_client_topic_
container to store the last time stamp for each client and topic
std::string getInfo() const
Returns a string representation of the buffers that are currently present.
string_map< std::vector< Stamped< T > > > getWithTopic(const int k, const rclcpp::Time &start_time, const rclcpp::Time &end_time, const std::string &client_id="", const std::string &frame_id="", const std::string &topic="")
Same as corresponding get, but returns samples grouped by topic.
void setBufferManager(BufferManager *manager)
Assigns the managing BufferManager.
string_map_2d< Buffer< std::shared_ptr< const T > > > buffer_by_client_topic_
container for data buffers, stored by client ID and topic
std::vector< std::string > getFrames(const std::string &client_id)
Gets the frame IDs for a specific client that has buffered data.
std::vector< Stamped< T > > get(const int k, const rclcpp::Time &start_time, const rclcpp::Time &end_time, const std::string &client_id="", const std::string &frame_id="", const std::string &topic="")
Fetches most recent k buffer samples within specified timeframe.
void insert(const typename std::shared_ptr< const T > &sample, const rclcpp::Time &stamp, const std::string &client_id, const std::string &topic, const bool clear_if_time_is_running_backwards=true)
Inserts a new ROS message into the buffer.
std::vector< Stamped< T > > get(const int k, const std::string &client_id="", const std::string &frame_id="", const std::string &topic="")
Fetches most recent k buffer samples.
void clearBuffer(const std::string &client_id, const std::string &topic)
Clears buffer for a specific client and topic.
string_map_2d< std::string > frame_by_client_topic_
container to store frame ID by client ID and topic
bool timeIsRunningBackwards(const std::string &client_id, const std::string &topic, const rclcpp::Time &stamp)
Checks whether time is running backwards for a specific client and topic.
std::vector< Stamped< T > > get(const std::string &client_id="", const std::string &frame_id="", const std::string &topic="")
Fetches all buffer samples.
std::vector< Stamped< T > > get(const rclcpp::Time &start_time, const rclcpp::Time &end_time, const std::string &client_id="", const std::string &frame_id="", const std::string &topic="")
Fetches all buffer samples within specified timeframe.
std::vector< std::string > getClients()
Gets the clients IDs that have buffered data.
string_map_2d< std::shared_ptr< std::shared_mutex > > mutex_by_client_topic_
container to store mutexes for locking buffer access
DataBuffers()
Creates a new DataBuffers instance.
BufferManager * manager_
managing BufferManager
void update(const std::vector< ClientConnection > &clients)
Creates buffers and mutexes for topics mentioned in clients. If a buffer and mutex already exists for...
Component of the Event Detector responsible for buffering live data.
std::mutex buffer_lock_mutex_
mutex for locking all buffer access
BufferManager()
Creates a new BufferManager.
void lockBuffers(const bool is_write)
Locks all buffers for either write or read mode.
std::string getInfo() const
Returns a string representation of all buffers that are currently present.
std::condition_variable buffer_lock_condition_variable_
condition variable to lock write/read buffer access
void unlockBuffers(const bool is_write)
Unlocks all buffers for either write or read mode.
int buffer_lock_n_threads_
helper variable to count threads in write/read buffer access mode