我想反序列化 Bag 文件中的消息,并使用 ROS 发布其部分内容以进行进一步处理。
我用 ROS C++ API 读取了一个 bagfile。我在编译时确切地知道消息的内容(主题名称和类型),因此我想将其反序列化为常见的 ROS 定义的类(例如此处定义的类https://docs.ros.org/en/noetic) /api/sensor_msgs/html/index-msg.html)在运行时。 这里有一些代码:
#include <rosbag/bag.h>
#include <rosbag/view.h>
//other content
rosbag::Bag bag;
bag.open("foo.bag", rosbag::bagmode::Read);
std::vector<std::string> topics{"camera1/camera_info"};
rosbag::View view(bag, rosbag::TopicQuery(topics));
for(auto const m : view)
{
const std::string& topic_name = m.getTopic();
std::cout << topic_name << std::endl;
std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>();
if (s != NULL)
std::cout << s->data << std::endl;
// I want the data in s as a string to be serialized into an instance
// of the type CameraInfo located in
// path/ros/distro/sensor_msgs/CameraInfo.h shipping with ROS.
CameraInfo ci = is_there_any_os_solution(s); //?
}
bag.close();
我试图在这个方向上找到一些东西,但我只是发现在编译时反序列化未知内容的库。
有人可以给一些提示吗?
所以基本上答案是使用
ìnstantiate
(我以某种方式认为它仅适用于原始类型)。最后它看起来像:
MoreComplexType::ConstPtr s = m.instantiate<MoreComplexType>();
我认为它会破坏更复杂的类型,并且我不太确定实例化方法在序列化它们时有多安全(例如,当创建包文件的 ROS 版本与反序列化时使用的版本不同时),但至少例如Sensor_msgs/CameraInfo.h 确实有效。