Fixposition SDK 0.0.0-heads/main-0-g7b59b93
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
bagwriter.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: ROS1 bag writer
12 *
13 * @page FPSDK_ROS1_BAGWRITER ROS1 bag writer
14 *
15 * **API**: fpsdk_ros1/bagwriter.hpp and fpsdk::ros1::bagwriter
16 *
17 */
18#ifndef __FPSDK_ROS1_BAGWRITER_HPP__
19#define __FPSDK_ROS1_BAGWRITER_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23#include <map>
24#include <memory>
25#include <vector>
26
27/* EXTERNAL */
28#include "fpsdk_ros1/ext/ros_time.hpp"
29#include "fpsdk_ros1/ext/rosbag_bag.hpp"
30
31/* Fixposition SDK */
32#include <fpsdk_common/time.hpp>
33
34/* PACKAGE */
35
36namespace fpsdk {
37namespace ros1 {
38/**
39 * @brief ROS1 bag writer
40 */
41namespace bagwriter {
42/* ****************************************************************************************************************** */
43
44/**
45 * @brief ROS1 bag writer helper
46 */
48{
49 public:
50 BagWriter();
51 ~BagWriter();
52
53 /**
54 * @brief Open bag for writing
55 *
56 * @param path Path/filename of the bag file
57 * @param compress Compress bag, 0 = no compression, 1 = LZ4, 2+ = BZ2
58 *
59 * @returns true if bag was sucessfully opened
60 */
61 bool Open(const std::string& path, const int compress);
62
63 /**
64 * @brief Close bag
65 */
66 void Close();
67
68 /**
69 * @brief Write a message to the bag, do nothing if no bag is open
70 *
71 * @param[in] msg The message
72 * @param[in] topic Optional topic name, if not given the original topic name is used
73 *
74 * The bag record time of the msg is used in the output bag. This time is stored and used for writing messages
75 * that have no time (templated method below).
76 */
77 void WriteMessage(const rosbag::MessageInstance& msg, const std::string& topic = "");
78
79 /**
80 * @brief Write a message to the bag, do nothing if no bag is open
81 *
82 * @tparam T ROS message type
83 * @param[in] msg The message
84 * @param[in] topic Topic name
85 * @param[in] time Optional bag record time, if ros::Time() is given the time from the last written message
86 * (see method above) is used
87 */
88 template <typename T>
89 void WriteMessage(const T& msg, const std::string& topic, const ros::Time& time = {})
90 {
91 if (bag_) {
92 if (!time.isZero()) {
93 time_ = time;
94 }
95 bag_->write(topic, time_, msg);
96 }
97 }
98
100
101 /**
102 * @brief Write a message to the bag, do nothing if no bag is open
103 *
104 * @tparam T ROS message type
105 * @param[in] msg The message
106 * @param[in] topic Topic name
107 * @param[in] time Optional bag record time, if ros::Time() is given the time from the last written message
108 * (see method above) is used
109 */
110 template <typename T>
111 void WriteMessage(const T& msg, const std::string& topic, const RosTime& time = {})
112 {
113 WriteMessage<T>(msg, topic, ros::Time(time.sec_, time.nsec_));
114 }
115
116 /**
117 * @brief Add ROS message definition
118 *
119 * @note No checks on the provided data are done!
120 *
121 * @param[in] topic The topic name
122 * @param[in] msg_name The message name (a.k.a. data type)
123 * @param[in] msg_md5 The message MD5 sum
124 * @param[in] msg_def The message definition
125 */
127 const std::string& topic, const std::string& msg_name, const std::string& msg_md5, const std::string& msg_def);
128
129 /**
130 * @brief Write raw binary (serialised) message
131 *
132 * @note No checks on the provided data are done!
133 *
134 * @param[in] data The binary message data
135 * @param[in] topic The topic name
136 * @param[in] time Optional bag record time, if not given the time from the last written message is used
137 *
138 * @returns true if message was added, false otherwise (message definition missing)
139 */
140 bool WriteMessage(const std::vector<uint8_t>& data, const std::string& topic, const ros::Time& time = {});
141
142 /**
143 * @brief Write raw binary (serialised) message
144 *
145 * @note No checks on the provided data are done!
146 *
147 * @param[in] data The binary message data
148 * @param[in] topic The topic name
149 * @param[in] time Optional bag record time, if not given the time from the last written message is used
150 *
151 * @returns true if message was added, false otherwise (message definition missing)
152 */
153 bool WriteMessage(const std::vector<uint8_t>& data, const std::string& topic, const RosTime& time = {});
154
155 private:
156 std::unique_ptr<rosbag::Bag> bag_; //!< Bag file handle
157 ros::Time time_; //!< Last known bag record time
158 std::map<std::string, boost::shared_ptr<ros::M_string>> msg_defs_; //!< Message definitions (connection headers)
159};
160
161/* ****************************************************************************************************************** */
162} // namespace bagwriter
163} // namespace ros1
164} // namespace fpsdk
165#endif // __FPSDK_ROS1_BAGWRITER_HPP__
ROS1 bag writer helper.
Definition bagwriter.hpp:48
bool WriteMessage(const std::vector< uint8_t > &data, const std::string &topic, const ros::Time &time={})
Write raw binary (serialised) message.
void WriteMessage(const T &msg, const std::string &topic, const RosTime &time={})
Write a message to the bag, do nothing if no bag is open.
bool Open(const std::string &path, const int compress)
Open bag for writing.
void WriteMessage(const rosbag::MessageInstance &msg, const std::string &topic="")
Write a message to the bag, do nothing if no bag is open.
bool WriteMessage(const std::vector< uint8_t > &data, const std::string &topic, const RosTime &time={})
Write raw binary (serialised) message.
void WriteMessage(const T &msg, const std::string &topic, const ros::Time &time={})
Write a message to the bag, do nothing if no bag is open.
Definition bagwriter.hpp:89
void AddMsgDef(const std::string &topic, const std::string &msg_name, const std::string &msg_md5, const std::string &msg_def)
Add ROS message definition.
Fixposition SDK.
Definition fpsdk_doc.hpp:20
Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
Definition time.hpp:67
Fixposition SDK: Time utilities.