Robotics StackExchange | Archived questions

callback function c++

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 

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("/cmd_vel", 1000);
    pose_sub = n.subscribe("/ball_pose_x", 10, poseCallback);   
    ros::Rate loop_rate(0.5);

    //moveUp(30.00);
    loop_rate.sleep();

    ros::spin();

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

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

        velocity_publisher.publish(vel_msg);
        
        ros::spinOnce();
        loop_rate.sleep();

        }while((ball_pose-500)

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/catkinws/src/spherocontroller/src/spheromove.cpp: In function ‘void poseCallback(const ConstPtr&)’: /home/sphero/catkinws/src/spherocontroller/src/spheromove.cpp:34:12: error: no match for ‘operator=’ (operand types are ‘stdmsgs::Float32 {aka stdmsgs::Float32std::allocator<void >}’ and ‘const _datatype {aka const float}’) ballpose = posemessage->data; ^ In file included from /home/sphero/catkinws/src/spherocontroller/src/spheromove.cpp:3:0: /opt/ros/kinetic/include/stdmsgs/Float32.h:22:8: note: candidate: stdmsgs::Float32std::allocator<void >& stdmsgs::Float32std::allocator<void >::operator=(const stdmsgs::Float32std::allocator<void >&) struct Float32_ ^ /opt/ros/kinetic/include/stdmsgs/Float32.h:22:8: note: no known conversion for argument 1 from ‘const _datatype {aka const float}’ to ‘const stdmsgs::Float32std::allocator<void >&’ spherocontroller/CMakeFiles/spheromovenode.dir/build.make:62: recipe for target 'spherocontroller/CMakeFiles/spheromovenode.dir/src/spheromove.cpp.o' failed make[2]: *** [spherocontroller/CMakeFiles/spheromovenode.dir/src/spheromove.cpp.o] Error 1 CMakeFiles/Makefile2:808: recipe for target 'spherocontroller/CMakeFiles/spheromovenode.dir/all' failed make[1]: *** [spherocontroller/CMakeFiles/spheromove_node.dir/all] Error 2 Makefile:138: recipe for target 'all' failed make: *** [all] Error 2

Asked by aldolereste on 2016-09-14 07:32:06 UTC

Comments

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?

Asked by Thomas D on 2016-09-14 09:29:51 UTC

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

Asked by Thomas D on 2016-09-14 14:28:55 UTC

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

ball_pose

Asked by aldolereste on 2016-09-16 10:02:12 UTC

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.

Asked by Thomas D on 2016-09-20 09:42:16 UTC

I've updated it with errors. thanks

Asked by aldolereste on 2016-09-21 06:10:10 UTC

Answers

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".

Asked by Shay on 2016-09-14 07:41:50 UTC

Comments

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

void poseCallback(const std_msgs::Int32ConstPtr& pose_message)
{
    sphero_pose=pose_message->data.c_str();
}

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

Asked by aldolereste on 2016-09-14 08:22:25 UTC

You should provide more information regarding your errors. e.g. compilation errors, and some of the remaining code.

pose_message.data is int, not std::string. int is no class, and has no method c_str

Asked by vmatos on 2016-09-14 11:49:58 UTC

I've just updated the code.

Asked by aldolereste on 2016-09-14 14:05:40 UTC

So what's the result?

Asked by Shay on 2016-09-15 08:01:23 UTC

did not find it yet!

Asked by aldolereste on 2016-09-16 07:45:00 UTC

Was making a mistake, and finally found it.

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

thanks

Asked by aldolereste on 2016-09-16 09:58:52 UTC

Comments