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

How to transform Laser Data stream from laser_frame to base_link?

asked 2021-01-03 13:12:41 -0500

Mateen gravatar image

Hi, I am trying to transform my laser scan data in frame 'base_link' from frame 'base_laser' using the following ROS Wiki tutorial:

http://wiki.ros.org/navigation/Tutori...

In the tutorial, they create an arbitrary point (x,y,z):

//just an arbitrary point in space

14 laser_point.point.x = 1.0; 15 laser_point.point.y = 0.2; 16 laser_point.point.z = 0.0;

And transform it from baser_laser to base_link. But I want my each point of my Laser's real data to be transformed to base_link. How can I do that? How can I subscribe the topic '/scan' (on which laser data is being published) and then publish the transformed data on topic '/tf_scan'?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-01-03 23:47:11 -0500

Where are you getting your /scan from? You will need to first figure out what the frame id in your scan msg is. You can do this by rostopic echo /scan/header in your terminal. Then you need to make sure that a link exists in your tf from laser_link -> base_link. You can use tf_echo or tf_monitor for this. If a transform exists, you can subscribe to your /scan topic and in the callback have this to transform your entire scan to base_link: (I am using PCL here)

tf2_ros::TransformListener* tf_listener_1;
tf2_ros::Buffer tf_buffer_1;
geometry_msgs::TransformStamped transform_stamped;

// Point Cloud callback
void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
  // initialize pcl objects
  pcl::PointCloud<pcl::PointXYZI>::Ptr input_cloud(new pcl::PointCloud<pcl::PointXYZI>);
  pcl::fromROSMsg(*input, *input_cloud);

  // Transform point cloud to base_link
  try
  {
    transform_stamped = tf_buffer_1.lookupTransform("base_link",
                                                    input->header.frame_id,
                                                    ros::Time(0),
                                                    ros::Duration(2));
  }
  catch (tf2::TransformException &ex)
  {
    ROS_ERROR("%s",ex.what());
    return;
  }
  // transform current point cloud
  Eigen::Affine3d pc_to_base_link = tf2::transformToEigen(transform_stamped);
  pcl::transformPointCloud(*input_cloud, *input_cloud, pc_to_base_link);

// input_cloud is now in the base_link
...
}
edit flag offensive delete link more

Comments

Thank you so much Akhil. My frame ID is 'laser'. Where do I need to use the code you provided? Can you tell me the exact code lines, I would need to replace with your code?. Do I also need to publish the transformed data on a specific topic?

Mateen gravatar image Mateen  ( 2021-01-04 06:16:24 -0500 )edit

I highly urge you to go through the tf tutorials from ROS. In this case, you should have a main with a subscriber to your /scan topic. The callback from scan is what I have shown here. The first step in your callback (as shown here) is to find a transform from laser -> base_link and apply the transform to your pointcloud.

Typically you do not need to publish it back, but it depends on what you are trying to achieve in your application. If you are using rviz to visualize your scan, if there exists a tf from laser -> base_link, rviz will take care of the transforms for you.

Akhil Kurup gravatar image Akhil Kurup  ( 2021-01-04 11:04:57 -0500 )edit

If there isn't any transform between these two links, Do I need to create a static transform as well before substituting your code in the main code? How can I do that?

Mateen gravatar image Mateen  ( 2021-01-11 22:55:41 -0500 )edit

Yes you will have to! You can add this line to your launch file

<node pkg="tf" type="static_transform_publisher" name="static_tf_broadcaster" args="1 0 0 0 0 0 1 base_link laser_link 10" />
Akhil Kurup gravatar image Akhil Kurup  ( 2021-01-12 10:29:56 -0500 )edit

Do I need to convert laser data from polar form [angle,theta] to Cartesian form [x,y,z]? If yes, How can I do that?

Mateen gravatar image Mateen  ( 2021-01-13 05:33:28 -0500 )edit

If you are trying to just transform your data then you dont have to do any conversions

Akhil Kurup gravatar image Akhil Kurup  ( 2021-01-13 11:24:22 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-01-03 13:11:48 -0500

Seen: 1,096 times

Last updated: Jan 03 '21