Fixposition SDK 0.0.0-heads/main-0-g97f6014
Collection of c++ libraries and apps for use with Fixposition products on Linux
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/fpl.hpp>
33#include <fpsdk_common/time.hpp>
34
35/* PACKAGE */
36
37namespace fpsdk {
38namespace ros1 {
39/**
40 * @brief ROS1 bag writer
41 */
42namespace bagwriter {
43/* ****************************************************************************************************************** */
44
45/**
46 * @brief ROS1 bag writer helper
47 */
48class BagWriter
49{
50 public:
51 BagWriter();
52 ~BagWriter();
53
54 /**
55 * @brief Open bag for writing
56 *
57 * @param[in] path Path/filename of the bag file
58 * @param[in] compress Compress bag, 0 = no compression, 1 = LZ4, 2+ = BZ2
59 *
60 * @returns true if bag was sucessfully opened
61 */
62 bool Open(const std::string& path, const int compress = 0);
63
64 /**
65 * @brief Close bag
66 */
67 void Close();
68
69 /**
70 * @brief Write a message to the bag
71 *
72 * @tparam T ROS message type
73 * @param[in] msg The message
74 * @param[in] topic Topic name
75 * @param[in] time Bag record time
76 *
77 * @returns true if message was added, false otherwise (message definition missing)
78 */
79 template <typename T>
80 bool WriteMessage(const T& msg, const std::string& topic, const ros::Time& time)
81 {
82 bool ok = false;
83 if (bag_) {
84 try {
85 bag_->write(topic, time, msg);
86 ok = true;
87 } catch (const rosbag::BagException& ex) {
88 WARNING("BagWriter: write fail: %s", ex.what());
89 }
90 }
91 return ok;
92 }
93
94 /**
95 * @brief Write a message to the bag
96 *
97 * @tparam T ROS message type
98 * @param[in] msg The message
99 * @param[in] topic Topic name
100 * @param[in] time Bag record time
101 *
102 * @returns true if message was added, false otherwise (message definition missing)
103 */
104 template <typename T>
105 bool WriteMessage(const T& msg, const std::string& topic, const common::time::RosTime& time = {})
106 {
107 return WriteMessage<T>(msg, topic, ros::Time(time.sec_, time.nsec_));
108 }
109
110 /**
111 * @brief Add ROS message definition from .fpl
112 *
113 * @note No checks on the provided data are done!
114 *
115 * @param[in] rosmsgdef The message definition
116 */
117 void AddMsgDef(const common::fpl::RosMsgDef& rosmsgdef);
118
119 /**
120 * @brief Write message from .fpl
121 *
122 * @note No checks on the provided data are done!
123 *
124 * @param[in] rosmsgbin The recorded message
125 *
126 * @returns true if message was added, false otherwise (message definition missing)
127 */
128 bool WriteMessage(const common::fpl::RosMsgBin& rosmsgbin);
129
130 private:
131 std::unique_ptr<rosbag::Bag> bag_; //!< Bag file handle
132 std::map<std::string, boost::shared_ptr<ros::M_string>> msg_defs_; //!< Message definitions (connection headers)
133};
134
135/* ****************************************************************************************************************** */
136} // namespace bagwriter
137} // namespace ros1
138} // namespace fpsdk
139#endif // __FPSDK_ROS1_BAGWRITER_HPP__
bool WriteMessage(const T &msg, const std::string &topic, const ros::Time &time)
Write a message to the bag.
Definition bagwriter.hpp:80
void AddMsgDef(const common::fpl::RosMsgDef &rosmsgdef)
Add ROS message definition from .fpl.
bool Open(const std::string &path, const int compress=0)
Open bag for writing.
bool WriteMessage(const common::fpl::RosMsgBin &rosmsgbin)
Write message from .fpl.
bool WriteMessage(const T &msg, const std::string &topic, const common::time::RosTime &time={})
Write a message to the bag.
Fixposition SDK: .fpl utilities.
#define WARNING(...)
Print a warning message.
Definition logging.hpp:81
ROS1 bag writer.
Definition bagwriter.hpp:42
Fixposition SDK: ROS1 library.
Definition doc.hpp:21
Fixposition SDK.
Helper for extracting a serialised ROS message.
Definition fpl.hpp:359
Helper for extracting ROS message definition (the relevant fields from the "connection header")
Definition fpl.hpp:336
Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
Definition time.hpp:156
Fixposition SDK: Time utilities.