Robotics StackExchange | Archived questions

Can i call a callback funcion on my main funcion?

Hi,

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 subcam = nh.subscribe(robotname+"/camerafront/imageraw", 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

Asked by JonPortugal on 2014-06-07 16:37:19 UTC

Comments

Answers

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()

Asked by billtecteacher on 2014-06-07 16:54:18 UTC

Comments

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!

Asked by JonPortugal on 2014-06-07 17:23:35 UTC

Comments

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.

Asked by billtecteacher on 2014-06-07 17:46:02 UTC

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.

Asked by ahendrix on 2014-06-08 02:05:55 UTC

Comments

@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?

Asked by surabhi96 on 2018-09-16 18:19:14 UTC

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] = Euler.at<std_msgs::Float64>(i); callback(my_arr);

Asked by surabhi96 on 2018-09-16 18:21:51 UTC

@surabhi96: that sounds like a new question, and it looks like you've asked one: https://answers.ros.org/question/303480/calling-a-callback-function-from-another-callback-function/ . I'll answer there.

Asked by ahendrix on 2018-09-16 20:13:32 UTC