Skip to content

Commit d35a32b

Browse files
committed
Add Wrench conversion
1 parent 33e31a6 commit d35a32b

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

test_tf2/test/test_convert.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <tf2_eigen/tf2_eigen.h>
4242
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
4343

44+
using Vector6d = Eigen::Matrix<double, 6, 1>;
45+
4446
TEST(tf2Convert, kdlToBullet)
4547
{
4648
double epsilon = 1e-9;
@@ -234,6 +236,34 @@ TEST(TfEigenKdl, TestFrameAffine3d)
234236
EXPECT_EQ(kdl_v, kdl_v1);
235237
}
236238

239+
TEST(TfEigenKdl, TestTwistMatrix)
240+
{
241+
const auto kdl_v = KDL::Twist(KDL::Vector(1, 2, 3), KDL::Vector(4, 5, 6));
242+
Vector6d eigen_v;
243+
tf2::convert(kdl_v, eigen_v);
244+
KDL::Twist kdl_v1;
245+
tf2::convert(eigen_v, kdl_v1);
246+
EXPECT_EQ(kdl_v, kdl_v1);
247+
}
248+
249+
250+
TEST(TfEigenKdl, TestMatrixWrench)
251+
{
252+
Vector6d eigen_v;
253+
eigen_v << 1, 2, 3, 3, 2, 1;
254+
KDL::Wrench kdl_v;
255+
tf2::convert(eigen_v, kdl_v);
256+
std::array<tf2::Vector3, 2> tf2_v;
257+
tf2::convert(kdl_v, tf2_v);
258+
Vector6d eigen_v1;
259+
tf2::convert(tf2_v, eigen_v1);
260+
std::array<tf2::Vector3, 2> tf2_v1;
261+
tf2::convert(eigen_v1, tf2_v1);
262+
Vector6d eigen_v2;
263+
tf2::convert(tf2_v1, eigen_v2);
264+
EXPECT_EQ(eigen_v, eigen_v2);
265+
}
266+
237267

238268
TEST(TfEigenKdl, TestVectorVector3d)
239269
{

tf2_eigen/include/tf2_eigen/tf2_eigen.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <geometry_msgs/msg/quaternion.hpp>
4040
#include <geometry_msgs/msg/quaternion_stamped.hpp>
4141
#include <geometry_msgs/msg/twist.hpp>
42+
#include <geometry_msgs/msg/wrench.hpp>
4243

4344
namespace tf2
4445
{
@@ -388,6 +389,35 @@ struct defaultMessage<Eigen::Matrix<double, 6, 1>>
388389
using type = geometry_msgs::msg::Twist;
389390
};
390391

392+
template <>
393+
struct ImplDetails<Eigen::Matrix<double, 6, 1>, geometry_msgs::msg::Wrench>
394+
{
395+
static void toMsg(const Eigen::Matrix<double, 6, 1> & in, geometry_msgs::msg::Wrench & msg)
396+
{
397+
msg.force.x = in[0];
398+
msg.force.y = in[1];
399+
msg.force.z = in[2];
400+
msg.torque.x = in[3];
401+
msg.torque.y = in[4];
402+
msg.torque.z = in[5];
403+
}
404+
405+
/** \brief Convert a Twist message transform type to a Eigen 6x1 Matrix.
406+
* This function is a specialization of the toMsg template defined in tf2/convert.h.
407+
* \param msg The Twist message to convert.
408+
* \param out The twist converted to a Eigen 6x1 Matrix.
409+
*/
410+
static void fromMsg(const geometry_msgs::msg::Wrench & msg, Eigen::Matrix<double, 6, 1> & out)
411+
{
412+
out[0] = msg.force.x;
413+
out[1] = msg.force.y;
414+
out[2] = msg.force.z;
415+
out[3] = msg.torque.x;
416+
out[4] = msg.torque.y;
417+
out[5] = msg.torque.z;
418+
}
419+
};
420+
391421
} // namespace impl
392422

393423
/** \brief Convert a Eigen::Vector3d type to a geometry_msgs::msg::Vector3.

tf2_geometry_msgs/include/tf2_geometry_msgs/tf2_geometry_msgs.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@
4848
#include <geometry_msgs/msg/wrench.hpp>
4949
#include <kdl/frames.hpp>
5050

51-
namespace tf2
51+
namespace Eigen
5252
{
53+
template <typename T, int, int, int, int, int>
54+
class Matrix;
55+
}
5356

57+
namespace tf2
58+
{
5459
/** \brief Convert a TransformStamped message to a KDL frame.
5560
* \param t TransformStamped message to convert.
5661
* \return The converted KDL Frame.
@@ -446,7 +451,21 @@ struct ImplDetails<std::array<tf2::Vector3, 2>, geometry_msgs::msg::Wrench>
446451
}
447452
};
448453

454+
template <>
455+
struct defaultMessage<std::array<tf2::Vector3, 2> >
456+
{
457+
using type = geometry_msgs::msg::Wrench;
458+
};
449459
} // namespace impl
460+
461+
template <int options, int row, int cols>
462+
void convert(
463+
const Eigen::Matrix<double, 6, 1, options, row, cols> & in, std::array<tf2::Vector3, 2> & out)
464+
{
465+
out[0] = tf2::Vector3{in(0), in(1), in(2)};
466+
out[1] = tf2::Vector3{in(3), in(4), in(5)};
467+
}
468+
450469
} // namespace tf2
451470

452471
#endif // TF2_GEOMETRY_MSGS_H

tf2_kdl/include/tf2_kdl/tf2_kdl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
#include <geometry_msgs/msg/wrench_stamped.hpp>
4444
#include <kdl/frames.hpp>
4545

46+
namespace Eigen
47+
{
48+
template <typename T, int, int, int, int, int>
49+
class Matrix;
50+
}
51+
4652
namespace tf2
4753
{
4854
/** \brief Convert a timestamped transform to the equivalent KDL data type.
@@ -245,6 +251,14 @@ struct defaultMessage<KDL::Wrench>
245251
};
246252
} // namespace impl
247253

254+
template <int options, int rows, int cols>
255+
void convert(Eigen::Matrix<double, 6, 1, options, rows, cols> const & in, KDL::Wrench & out)
256+
{
257+
const auto msg =
258+
toMsg<Eigen::Matrix<double, 6, 1, options, rows, cols>, geometry_msgs::msg::Wrench>(in);
259+
fromMsg<>(msg, out);
260+
}
261+
248262
// ---------------------
249263
// Frame
250264
// ---------------------

0 commit comments

Comments
 (0)