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: ROS2 bag writer
12 *
13 * @page FPSDK_ROS2_BAGWRITER ROS2 bag writer
14 *
15 * **API**: fpsdk_ros1/bagwriter.hpp and fpsdk::ros1::bagwriter
16 *
17 */
18#ifndef __FPSDK_ROS2_BAGWRITER_HPP__
19#define __FPSDK_ROS2_BAGWRITER_HPP__
20
21/* LIBC/STL */
22
23/* EXTERNAL */
24#include "fpsdk_ros2/ext/rosbag2_cpp_writer.hpp"
25
26/* Fixposition SDK */
27#include <fpsdk_common/fpl.hpp>
28#include <fpsdk_common/time.hpp>
29
30/* PACKAGE */
31
32namespace fpsdk {
33namespace ros2 {
34/**
35 * @brief ROS2 bag writer
36 */
37namespace bagwriter {
38/* ****************************************************************************************************************** */
39
40/**
41 * @brief ROS2 bag writer helper
42 */
43class BagWriter
44{
45 public:
46 BagWriter();
47 ~BagWriter();
48
49 /**
50 * @brief Open bag for writing
51 *
52 * @param[in] path Path of the bag directory
53 * @param[in] compress Compress bag, 0 = no compression, 1 = ...
54 * Note: not fully implemented
55 *
56 * @returns true if bag was sucessfully opened
57 */
58 bool Open(const std::string& path, const int compress = 0);
59
60 /**
61 * @brief Close bag
62 */
63 void Close();
64
65 /**
66 * @brief Write a message to the bag
67 *
68 * @tparam T ROS message type
69 * @param[in] msg The message
70 * @param[in] topic Topic name
71 * @param[in] time Bag record time
72 *
73 * @returns true if message was added, false otherwise (message definition missing)
74 */
75 template <typename T>
76 bool WriteMessage(const T& msg, const std::string& topic, const rclcpp::Time& time)
77 {
78 bool ok = false;
79 try {
80 if (bag_) {
81 bag_->write(msg, topic, time);
82 ok = true;
83 }
84 } catch (const std::exception& ex) {
85 WARNING("BagWriter: write fail: %s", ex.what());
86 }
87 return ok;
88 }
89
90 /**
91 * @brief Write a message to the bag
92 *
93 * @tparam T ROS message type
94 * @param[in] msg The message
95 * @param[in] topic Topic name
96 * @param[in] time Bag record time
97 */
98 template <typename T>
99 bool WriteMessage(const T& msg, const std::string& topic, const common::time::RosTime& time)
100 {
101 return WriteMessage(msg, topic, rclcpp::Time(time.sec_, time.nsec_, RCL_ROS_TIME));
102 }
103
104 /**
105 * @brief Add ROS message definition from .fpl
106 *
107 * @note No checks on the provided data are done!
108 *
109 * @param[in] rosmsgdef The message definition
110 */
111 void AddMsgDef(const common::fpl::RosMsgDef& rosmsgdef);
112
113 /**
114 * @brief Write message from .fpl
115 *
116 * @note No checks on the provided data are done!
117 *
118 * @param[in] rosmsgbin The recorded message
119 *
120 * @returns true if message was added, false otherwise (e.g. ROS1->ROS2 conversion not implemented)
121 */
122 bool WriteMessage(const common::fpl::RosMsgBin& rosmsgbin);
123
124 private:
125 std::unique_ptr<rosbag2_cpp::Writer> bag_; //!< Bag file handle
126 std::map<std::string, common::fpl::RosMsgDef> defs_; //!< Message definitions (connection headers)
127};
128
129/* ****************************************************************************************************************** */
130} // namespace bagwriter
131} // namespace ros2
132} // namespace fpsdk
133#endif // __FPSDK_ROS2_BAGWRITER_HPP__
bool WriteMessage(const T &msg, const std::string &topic, const common::time::RosTime &time)
Write a message to the bag.
Definition bagwriter.hpp:99
bool WriteMessage(const common::fpl::RosMsgBin &rosmsgbin)
Write message from .fpl.
void AddMsgDef(const common::fpl::RosMsgDef &rosmsgdef)
Add ROS message definition from .fpl.
bool WriteMessage(const T &msg, const std::string &topic, const rclcpp::Time &time)
Write a message to the bag.
Definition bagwriter.hpp:76
bool Open(const std::string &path, const int compress=0)
Open bag for writing.
Fixposition SDK: .fpl utilities.
#define WARNING(...)
Print a warning message.
Definition logging.hpp:81
ROS2 bag writer.
Definition bagwriter.hpp:37
Fixposition SDK: ROS2 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
uint32_t nsec_
Nanoseconds part of time (should be in range 0-999999999)
Definition time.hpp:188
uint32_t sec_
Seconds part of time.
Definition time.hpp:187
Fixposition SDK: Time utilities.