(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Converting Datatypes

Description: This is a quick description of the changes in syntax for converting datatypes.

Tutorial Level: INTERMEDIATE


  1. C++
    1. tf
    2. tf2


In tf you would do the following:


Hard coded methods for bullet to message only.

   1 #include "tf/transform_datatypes.h"
   2 btVector3 btv = btVector3(xvalues[i], yvalues[i], zvalues[i]);
   3 geometry_msgs::Vector3 msgv;
   4 vector3TFToMsg(btv, msgv);


With tf2 there is a templated convert method. This allows a modular design which can convert any datatype which implements the overloaded template methods.

Same result using bullet and message bindings:

   1 #include "tf2_bullet/tf2_bullet.h"
   2 #include "tf2_geometry_msgs/tf2_geometry_msgs.h"
   3 tf2::Stamped<btVector3> b(btVector3(1,2,3), ros::Time(), "my_frame");
   4 geometry_msgs::Vector3Stamped m;
   5 tf2::convert(b, m);

Much more extensible to convert bullet to kdl:

   1 #include "tf2_kdl/tf2_kdl.h"
   2 #include "tf2_bullet/tf2_bullet.h"
   3 tf2::Stamped<btVector3> b(btVector3(1,2,3), ros::Time(), "my_frame");
   4 tf2::Stamped<KDL::Vector> k;
   5 tf2::convert(b, k);

These headers are packaged in their own packages and allows packages to only depend on the converters they actually need.

Current implementations include tf2_bullet tf2_kdl tf2_geometry_msgs

You can also create your own datatype conversions. There are tutorials for C++ and Python

Wiki: tf2/Tutorials/Migration/DataConversions (last edited 2017-03-15 16:42:52 by TullyFoote)