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

how to use tf listener

asked 2012-04-25 23:43:57 -0600

ldima gravatar image

updated 2014-04-20 14:09:30 -0600

ngrennan gravatar image

Hello everyone. I didn't understand very well how to use tf,although I've read all the tutorials. I try to explain briefly: I use stage to simulate a robot that find some objects with a laser.Then I use a function findCoords to find the coords in the map of the objects found by the robot.Now i want to transform the point give by this function with tf,so what I made is to declare the listener as global variable,then do all the stuff and then in findCoords I do `listener.transformPoint("blabla",point,pointNew) then I compile and it gives no error but when i run it gives this error:

[FATAL] [1335431661.314896028]: You must call ros::init() before creating the first NodeHandle
[FATAL] [1335431661.314990035]: BREAKPOINT HIT
file = /tmp/buildd/ros-electric-ros-comm-1.6.6/debian/ros-electric-ros-comm/opt/ros/electric/stacks/ros_comm/clients/cpp/roscpp/src/libros/node_handle.cpp
line=151

how can I solve this?

edit retag flag offensive close merge delete

Comments

thanks markymark for answering,however i have called ros::init in the main..the structure is like this:tf::TransformListener listener as global variable,the function for coords is called from lasercallback and main

ldima gravatar image ldima  ( 2012-04-26 01:11:15 -0600 )edit

People need a lot more detail to help debug this. Can you demonstrate the problem in a small program?

joq gravatar image joq  ( 2012-04-26 02:29:51 -0600 )edit

I'm only guessing here - but is sounds like a scoping issue - but as joq says...code please.

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-04-26 02:35:08 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted
5

answered 2012-04-26 05:04:41 -0600

ldima gravatar image

it didn't fit in comment space,so i'm writing here.. I try to give an example,what I'm doing is:

#include <ros/ros.h>
#include <tf/tf.h>
#include <tf/transform_listener.h>
"others include..."

tf::TransformListener listener; //as global variable
ros::Publisher pubLaser;
"others global variables..."

void findCoords(const sensor_msgs::LaserScanPtr dataFromLaser){
   //in this function I have to transform,so i do the stuff and then..
   listener.transformPoint("base_link",point,newPoint);
   //where point,newPoint are StampedPoint
}

void laserCallback(const sensor_msgs::LaserScan::ConstPtr& fromLaser){
   //do stuff...
   findCoords(data);
}

int main(int argc,char** argv){
   ros::init(argc,argv,"name");
   ros::NodeHandle nh;
   ros::Subscriber laser=nh.subscribe("base_scan",10,laserCallback);
   pubLaser=nh.advertise<sensor_msgs::LaserScan>("from_Laser",100);
   ros::spin();
   return 0;
}

If i run it without the part of tf it works,but i didn't figure out how to make it run with tf,it gives me the error i said in the first post.

edit flag offensive delete link more

Comments

2

The tf::TransformListener listener; will be being instanciated before main() is called - I don't know for certian but I expect your listener member needs ros::init(argc,argv,"name") to have been called first. Move tf::TransformListener listener inside main and pass by reference to your methods

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-04-26 05:13:23 -0600 )edit

but findCoords is called from laserCallback,how can I pass listener to it?thanks

ldima gravatar image ldima  ( 2012-04-26 05:28:15 -0600 )edit
5

Simply implement tf::TransformListener listener as a pointer: tf::TransformListener* pListener = NULL; Then after ros::init(...) do pListener = new (tf::TransformListener); and in your callback have pListener->transformPoint("base_link",point,newPoint);

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-04-26 05:37:32 -0600 )edit

oh..yes thanks...i'm completely out today..anyway it works :) thanks markymark

ldima gravatar image ldima  ( 2012-04-26 06:15:16 -0600 )edit

please marks the answer as correct so that it will be easier for others to see the highlighted answer

Souvik Basak gravatar image Souvik Basak  ( 2022-01-01 08:26:55 -0600 )edit
2

answered 2012-04-25 23:55:01 -0600

MarkyMark2012 gravatar image

You've not called ros::init() in your listner. It's pretty much the first thing you need to do in any ROS app.

Mark

edit flag offensive delete link more

Comments

0

answered 2013-04-19 22:36:02 -0600

Marko gravatar image

Hi,

I am using the same way to create the tf::TransformListener as it is described above, but I also need to change the cache time of the listener to about 3 min and I am having trouble in succeeding that. I would be very grateful for any help.

Marko

edit flag offensive delete link more

Comments

1

solved.. listener = new tf::TransformListener(ros::Duration(180));

Marko gravatar image Marko  ( 2013-04-19 23:27:29 -0600 )edit

Question Tools

Stats

Asked: 2012-04-25 23:43:57 -0600

Seen: 8,352 times

Last updated: Apr 19 '13