ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

tf2::toMsg throws Error even if I use the same functions as in tf2_geometry_msgs.h

asked 2022-09-20 16:45:46 -0600

oroshimaru1 gravatar image

Hey guys I have a Point A and a Pose B. I want the coordinates of A relative to B, I am working with ros2 galactic. Thats my try:

inline geometry_msgs::msg::Point rel_pos(
  geometry_msgs::msg::Point A, geometry_msgs::msg::Pose B)
{
    tf2::Transform t;
    tf2::fromMsg(B, t);
    tf2::Vector3 v_in;
    tf2::fromMsg(A, v_in);
    tf2::Vector3 v_out = t.inverse() * v_in;
    geometry_msgs::msg::Point t_out;
    tf2::toMsg(v_out, t_out);
    return t_out;
}

An this is the code of

tf2::doTransform

from https://github.com/ros/geometry2/blob...

inline
  void doTransform(const geometry_msgs::Point& t_in, geometry_msgs::Point& t_out, const geometry_msgs::TransformStamped& transform)
  {
    tf2::Transform t;
    fromMsg(transform.transform, t);
    tf2::Vector3 v_in;
    fromMsg(t_in, v_in);
    tf2::Vector3 v_out = t * v_in;
    toMsg(v_out, t_out);
  }

Im getting the following Error:

error: no matching function for call to ‘toMsg(tf2::Vector3&, geometry_msgs::msg::Point&)’ 34 | toMsg(v_out, t_out);

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2022-09-20 23:59:41 -0600

ravijoshi gravatar image

The following error you reported seems correct:

error: no matching function for call to ‘toMsg(tf2::Vector3&, geometry_msgs::msg::Point&)’ 34 | toMsg(v_out, t_out);

The tf2_geometry_msgs.h page you are looking belongs to ROS Noetic. The tf2_geometry_msgs.h in ROS Galactic does not use tf2::toMsg function.

Anyway, lets go back to the question. Using tf2::toMsg(v_out, t_out), you are essentially converting tf2::Vector3 into geometry_msgs::msg::Point. Please see below the structure of geometry_msgs/msg/Point message:

# This contains the position of a point in free space

float64 x
float64 y
float64 z

The above definition has been take from here.

We can easily convert tf2::Vector3 into geometry_msgs::msg::Point using the following code:

tf2::Vector3 v_out(1.0, 2.0, 3.0);

geometry_msgs::msg::Point t_out;
t_out.x = v_out.getX();
t_out.y = v_out.getY();
t_out.z = v_out.getZ();
edit flag offensive delete link more

Comments

Hey @ravijoshi thank you! Now, I am getting the following error strangely:

undefined reference to `void tf2::fromMsg<geometry_msgs::msg::Point_<std::allocator<void> >, tf2::Vector3>(geometry_msgs::msg::Point_<std::allocator<void> > const&, tf2::Vector3&)

How can there be a linking error for fromMsg if the rest of tf2 is correctly linked. Is there maybe some template and i need to linking the library which has the functions for the right types?

oroshimaru1 gravatar image oroshimaru1  ( 2022-09-21 01:53:23 -0600 )edit
  1. You should check the tf2_geometry_msgs.h for ROS Galactic. Please see answer for the link.
  2. The undefined reference error is coming most probably from your CMakeLists.txt file. Please share it in your question.
  3. If the problem persist, please mention clearly what conversion is failing. For example, in this answer, I explained converting tf2::Vector3 to geometry_msgs::msg::Point
ravijoshi gravatar image ravijoshi  ( 2022-09-21 03:23:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-09-20 16:42:44 -0600

Seen: 785 times

Last updated: Sep 22 '22