Ask Your Question

Can i call a callback funcion on my main funcion?

asked 2014-06-07 16:37:19 -0500

JonPortugal gravatar image


I'm a noob at ros but i'm trying to do some school work. I have a cameraCallback funcion and it is subscribed as "// Setup camera subscriber ros::Subscriber sub_cam = nh.subscribe(robot_name+"/camera_front/image_raw", 1, cameraCallback);"

Now, i need to call only the funtion cameraCallback, withouth calling ros::spinOnce. Can anyone tell me how to do it? cameraCallback function if defined like this: "void cameraCallback(const sensor_msgs::ImageConstPtr& msg){}" and i can't call it on my main, with "cameraCallback(&msg);"

Any answer will be helpfull. Thanks in advance

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2014-06-08 02:05:55 -0500

ahendrix gravatar image

Your callback is a function, so it's always possible to call it manually.

In your case, I suspect the reason you're having trouble calling it is because it takes a const sensor_msgs::ImageConstPtr & as the argument, which is actually a const boost::shared_ptr<sensor_msgs::Image> &, and you're passing it something else.

The simplest way to solve this is to construct the appropriate shared pointer type and pass it in.

Since you're working with images, you might also be able to use cv_bridge to create the appropriate shared pointer based on an existing OpenCV image.

edit flag offensive delete link more


@ahendrix In my case, the callback function expects a const pkg_name::Euler_val::ConstPtr& array which is a custom message defined as float64[3] Euler_angles. How do I call my callback function in this case?

surabhi96 gravatar image surabhi96  ( 2018-09-16 18:19:14 -0500 )edit

I have done the following: const boost::shared_ptr<std_msgs::Float64> my_arr(new std_msgs::Float64[6], std::default_delete<std_msgs::Float64[]>()); for(int i = 0; i<3; i++) my_arr.get()[i] =<std_msgs::Float64>(i); callback(my_arr);

surabhi96 gravatar image surabhi96  ( 2018-09-16 18:21:51 -0500 )edit

@surabhi96: that sounds like a new question, and it looks like you've asked one: . I'll answer there.

ahendrix gravatar image ahendrix  ( 2018-09-16 20:13:32 -0500 )edit

answered 2014-06-07 16:54:18 -0500

billtecteacher gravatar image

Not sure from what you said why you can't call spin? I believe you can also use instead of spinOnce you can use use equivalent of ros python r = rospy.Rate(rate in hz) and then in the loop r.sleep()

edit flag offensive delete link more

answered 2014-06-07 17:23:35 -0500

JonPortugal gravatar image

Because i wanted to call the function even if it had no new data... But i understood already that spinOnce is my only and best option!

edit flag offensive delete link more


Perhaps you could have your callback collect new data from the topic that would be passed to a lastest_data_off_queue variable to another function that runs at the rate you need for your system to work. If you also needed that second function to run if there is new data then it could be called by your call back as well.

billtecteacher gravatar image billtecteacher  ( 2014-06-07 17:46:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2014-06-07 16:37:19 -0500

Seen: 542 times

Last updated: Jun 08 '14