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

How to make a boost::shared_ptr<T> for global var

asked 2013-03-12 10:17:36 -0500

MartinW gravatar image

updated 2013-03-12 10:20:24 -0500

Hello all,

I was wondering how to create a boost::shared_ptr<T> global variable for a tf::TransformListener object? I am encountering the same problem as in this question posted a while ago but I don't know enough about boost::shared_ptr<T>.

Can someone explain how I can declare and initialize boost::shared_ptr<T> with respect to tf::TransformListener? Many thanks in advance.

Kind Regards, Martin

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
2

answered 2013-03-12 10:31:15 -0500

Hi Martin,

boost::shared_ptr<tf::TransformListener> tf_ptr;

...

int main(int argc, const char* argv []) {

  ros::init(argc, argv, "my_node");
  tf_ptr.reset(new tf::TransformListener);

  ... do more stuff here. Use tf_ptr as a regular pointer for the most part ...

  return 0;
}

The comments in this thread also explain the same answer.

edit flag offensive delete link more

Comments

Thanks for the quick reply piyushk! I tried this but now I am back to getting the error: reference to ‘transform’ is ambiguous. candidates are: tf::StampedTransform transform. Do I need to make StampedTransform a shared_ptr too? the variable transform is what I need in my callback

MartinW gravatar image MartinW  ( 2013-03-12 10:47:15 -0500 )edit

Can you give the code sample producing that.

dornhege gravatar image dornhege  ( 2013-03-12 10:53:33 -0500 )edit

Hey dornhege, I put the code into another answer (forgetting that I could edit my own question haha)

MartinW gravatar image MartinW  ( 2013-03-12 11:05:27 -0500 )edit
0

answered 2013-03-12 11:02:54 -0500

MartinW gravatar image

updated 2013-03-12 11:09:25 -0500

Hey @dornhege and @piyushk,

Thanks for taking the time to reply to my questions! However, I am still getting some errors so I am just going to post a little bit of my code here and maybe you can help me fix the problems.

So my global variables for the transform are:

tf::StampedTransform transform;
boost::share_ptr<tf::TransformListener> transform_ptr;

Then I have my

int main (int argc, char** argv)
{

ros::init (argc, argv, "SitAssessManager");

transform_ptr.reset(new tf::TransformListener);
ros::NodeHandle n;

try{
transform_ptr->waitForTransform("/base_link","/camera_depth_optical_frame",ros::Time::now(),ros::Duration(3));
        transform_ptr->lookupTransform("/base_link", "/camera_depth_optical_frame",ros::Time::now(), transform);
        }
    catch (tf::TransformException ex){
        ROS_WARN("Base to camera transform unavailable %s", ex.what());
        }

while(n.ok())
{
ros::spinOnce();
loop_rate.sleep();
}

Where in my Kinect frames callback I use a vector3f point and use transform to go from the frame to frame just by: t_point = transform*point;

Here is the compiling error:

 In function ‘void callback(const PointCloud2ConstPtr&)’:
  /martin/ros_workspace/SituationAssessmentManager/src/H20.cpp:559:13: error: reference to ‘transform’ is ambiguous
  /martin/ros_workspace/SituationAssessmentManager/src/H20.cpp:80:22: error: candidates are: tf::StampedTransform transform
  /usr/include/c++/4.6/bits/stl_algo.h:4907:5: error:                 template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
  /usr/include/c++/4.6/bits/stl_algo.h:4871:5: error:                 template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
  /martin/ros_workspace/SituationAssessmentManager/src/H20.cpp:718:96: error: reference to ‘transform’ is ambiguous
  /martin/ros_workspace/SituationAssessmentManager/src/H20.cpp:80:22: error: candidates are: tf::StampedTransform transform

So the error shows up from both calls to 'transform', once in the callback and once in main

Thanks again!

Kind Regards, Martin

edit flag offensive delete link more

Comments

@martin: Can you post a link to the entire file? This is a pretty nasty error.

piyushk gravatar image piyushk  ( 2013-03-12 19:59:38 -0500 )edit

Do you have a using namespace std; anywhere? In any case: renaming transform to something else should fix that error.

dornhege gravatar image dornhege  ( 2013-03-13 01:00:49 -0500 )edit

Yea, I had using namespace std; before I declared it. After I changed the name, it fixed the error! Thanks for all the help!

MartinW gravatar image MartinW  ( 2013-03-14 07:57:05 -0500 )edit
0

answered 2013-03-12 10:26:31 -0500

dornhege gravatar image

You should only encounter the problem if you create the pointer before calling ros::init(). The shared pointer itself should not create any TransformListener on its own.

So, the new to be put in the shared_ptr needs to come after ros::init.

edit flag offensive delete link more

Comments

Well, I have it set as a global variable at the top of my program: <tf::TransformListener> listener and <tf::TransformListener> transform. Then I have a function called getTransform() where I run the transformation code listener.waitForTransform and listener.lookupTransform ... continued

MartinW gravatar image MartinW  ( 2013-03-12 10:31:36 -0500 )edit

Then for my main() I run init first just like every other program I've made and I make a call to getTransform() right before entering a while(n.ok()) loop where the callback uses the global transform variables. I'm not using any pointers now because I don't understand how to use the them

MartinW gravatar image MartinW  ( 2013-03-12 10:33:13 -0500 )edit

If it's static variables and not pointers, you'll get exactly the problems that you have. See the answer by @piyushk for a shared_ptr sample.

dornhege gravatar image dornhege  ( 2013-03-12 10:52:02 -0500 )edit

Question Tools

Stats

Asked: 2013-03-12 10:17:36 -0500

Seen: 3,434 times

Last updated: Mar 12 '13