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 <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); //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)<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
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 yourmain
function?What error are you getting? Or how do you know that the code is not entering the callback function?
Do you still need me to add the errors? I think the problem is coming from the type of that
The example is now doing more than one thing: setting up a subscriber with a callback function and doing things in
moveUp()
. AndmoveUp()
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.I've updated it with errors. thanks