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

callback function c++

asked 2016-09-14 07:32:06 -0500

updated 2016-09-21 06:09:27 -0500

I have been trying to implement a call back function with no success.

Here is the problem. I have a subscriber that is supposed to listen to a topic. Let's call it A. A is of type std_msgs/Float32

How can I implement a callback function?

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "std_msgs/Float32.h"
#include <sstream>

using namespace std;

ros::Publisher velocity_publisher;
ros::Subscriber pose_sub;
std_msgs::Float32 ball_pose;

void poseCallback(const std_msgs::Float32::ConstPtr & pose_message);
//void moveUp(std_msgs::Float32 distance_tolerance);

int main(int argc, char **argv)
    ros::init(argc, argv, "sphero_move");
    ros::NodeHandle n;

    velocity_publisher = n.advertise<geometry_msgs::twist>("/cmd_vel", 1000);
    pose_sub = n.subscribe("/ball_pose_x", 10, poseCallback);   
    ros::Rate loop_rate(0.5);



    return 0;

void poseCallback(const std_msgs::Float32::ConstPtr & pose_message)
    ball_pose = pose_message->data;

/**void moveUp(std_msgs::Float32 distance_tolerance)
    geometry_msgs::Twist vel_msg;
    ros::Rate loop_rate(10);

        vel_msg.linear.x = 25;
        vel_msg.linear.y = 0;
        vel_msg.linear.z = 0;



        }while((ball_pose-500)<distance_tolerance); }**="" <="" pre="">

I want to be able to update the position of the robot in every iteration to be able to act on the current position, since the robot is moving.

Here is the error I am receiving.

/home/sphero/catkin_ws/src/sphero_controller/src/sphero_move.cpp: In function ‘void poseCallback(const ConstPtr&)’: /home/sphero/catkin_ws/src/sphero_controller/src/sphero_move.cpp:34:12: error: no match for ‘operator=’ (operand types are ‘std_msgs::Float32 {aka std_msgs::Float32_<std::allocator<void> >}’ and ‘const _data_type {aka const float}’) ball_pose = pose_message->data; ^ In file included from /home/sphero/catkin_ws/src/sphero_controller/src/sphero_move.cpp:3:0: /opt/ros/kinetic/include/std_msgs/Float32.h:22:8: note: candidate: std_msgs::Float32_<std::allocator<void> >& std_msgs::Float32_<std::allocator<void> >::operator=(const std_msgs::Float32_<std::allocator<void> >&) struct Float32_ ^ /opt/ros/kinetic/include/std_msgs/Float32.h:22:8: note: no known conversion for argument 1 from ‘const _data_type {aka const float}’ to ‘const std_msgs::Float32_<std::allocator<void> >&’ sphero_controller/CMakeFiles/sphero_move_node.dir/build.make:62: recipe for target 'sphero_controller/CMakeFiles/sphero_move_node.dir/src/sphero_move.cpp.o' failed make[2]: * [sphero_controller/CMakeFiles/sphero_move_node.dir/src/sphero_move.cpp.o] Error 1 CMakeFiles/Makefile2:808: recipe for target 'sphero_controller/CMakeFiles/sphero_move_node.dir/all' failed make[1]: [sphero_controller/CMakeFiles/sphero_move_node.dir/all] Error 2 Makefile:138: recipe for target 'all' failed make: ** [all] Error 2

edit retag flag offensive close merge delete


Do you have more of the code you could post? Here you are showing that there is a Subscriber but you don't set it to anything. That is where you would tell your node to go to a callback function when messages are received on a topic. Where is your main function?

Thomas D gravatar image Thomas D  ( 2016-09-14 09:29:51 -0500 )edit

What error are you getting? Or how do you know that the code is not entering the callback function?

Thomas D gravatar image Thomas D  ( 2016-09-14 14:28:55 -0500 )edit

Do you still need me to add the errors? I think the problem is coming from the type of that

aldolereste gravatar image aldolereste  ( 2016-09-16 10:02:12 -0500 )edit

The example is now doing more than one thing: setting up a subscriber with a callback function and doing things in moveUp(). And moveUp() may not return. I would suggest getting just the callback working first. And try to say what you expect to see and what you actually get.

Thomas D gravatar image Thomas D  ( 2016-09-20 09:42:16 -0500 )edit

I've updated it with errors. thanks

aldolereste gravatar image aldolereste  ( 2016-09-21 06:10:10 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2016-09-14 07:41:50 -0500

Shay gravatar image

updated 2016-09-14 08:29:13 -0500

You can refer to Tutorials Writing the Subscriber Node.

pose_message->data.c_str(); converts a int32 number to String. For example, pose_message->data is 999, then sphero_pose is "999".

edit flag offensive delete link more


I just can't figure out the line inside the function definition

void poseCallback(const std_msgs::Int32ConstPtr& pose_message)

I have tried the code above, using the reference you suggested, but still no success.

aldolereste gravatar image aldolereste  ( 2016-09-14 08:22:25 -0500 )edit

You should provide more information regarding your errors. e.g. compilation errors, and some of the remaining code. is int, not std::string. int is no class, and has no method c_str

vmatos gravatar image vmatos  ( 2016-09-14 11:49:58 -0500 )edit

I've just updated the code.

aldolereste gravatar image aldolereste  ( 2016-09-14 14:05:40 -0500 )edit

So what's the result?

Shay gravatar image Shay  ( 2016-09-15 08:01:23 -0500 )edit

did not find it yet!

aldolereste gravatar image aldolereste  ( 2016-09-16 07:45:00 -0500 )edit

answered 2016-09-16 09:58:52 -0500

updated 2016-09-21 08:38:34 -0500

Was making a mistake, and finally found it.

void poseCallback(const std_msgs::Float32::ConstPtr & pose_message){ = pose_message->data;}


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-09-14 07:32:06 -0500

Seen: 4,959 times

Last updated: Sep 21 '16