-
Notifications
You must be signed in to change notification settings - Fork 31
The Parser
In most cases we have access to the Ros Message Type Definition. Luckily for us this string contains all the information we need to know how to deserialize the ROS message. The goal of the parser is to extract the schema and made it available to the user and the deserializer.
This can be simply done calling the function:
ROSTypeList buildROSTypeMapFromDefinition( const std::string& type_name,
const std::string& msg_definition );
###Example 1
Further we try to parse the type(s) contained in geometry_msgs::Pose
#include <geometry_msgs/Pose.h>
// NOTE: in this trivial example we need to include geometry_msgs/Pose
// even if the main goal of this library is to avoid that.
///...
using namespace RosIntrospection;
ROSTypeList typelist = buildROSTypeMapFromDefinition(
ros::message_traits::DataType< geometry_msgs::Pose >::value(),
ros::message_traits::Definition< geometry_msgs::Pose >::value());
std::cout << typelist << std::endl;
Note that ROSTypeList
is a vector of ROSType
instances.ROSType
itself contains
one or more instances of ROSField
.
Take a look to the API.
The expected output is:
geometry_msgs/Pose :
position : geometry_msgs/Point
orientation : geometry_msgs/Quaternion
geometry_msgs/Point :
x : float64
y : float64
z : float64
geometry_msgs/Quaternion :
x : float64
y : float64
z : float64
w : float64
As expected each message type has a set of fields with a fieldname and a typename.
What is noteworthy is that also all of the non-built-in type in the hierarchy
are parsed as well, specifically geometry_msgs/Point
and geometry_msgs/Quaternion
.
###Example 2
In the next example we will parse all the types found in a single ROS bag.
We will not need to #include
any of those ROS Types.
To understand this chunk of code you must be familiar with the rosbag::Bag API
rosbag::Bag bag;
bag.open( file_name, rosbag::bagmode::Read );
rosbag::View bag_view ( bag, ros::TIME_MIN, ros::TIME_MAX, true );
auto first_time = bag_view.getBeginTime();
const auto& connections = bag_view.getConnections();
// create a list and a type map for each topic
std::map<std::string, RosIntrospection::ROSTypeList> type_map;
for(unsigned i=0; i<connections.size(); i++)
{
auto topic_map = buildROSTypeMapFromDefinition( connections[i]->datatype,
connections[i]->msg_def);
type_map.insert( std::make_pair(connections[i]->topic, topic_map));
}