ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
You instantiate a new publisher everytime your callback posCallback is called. There is a time gap between instantiating a publisher till a publish call to it actually publishes, as things are threaded behind the scenes. So what happens everytime your posCallback is called:
What you can do is make the Publisher a global variable and use it. Minimal example:
#include <ros/ros.h>
[...]
ros::Publisher pub_twist;
[...]
void posCallback(const geometry_msgs::Point& msg)
{
[...]
pub_twist.publish(twist_msg);
}
int main(int argc, char **argv)
{
[...]
pub_twist= nh.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
ros::Subscriber sub = nh.subscribe("color_position", 100, posCallback);
ros::spin();
}
2 | No.2 Revision |
You instantiate a new publisher everytime your callback posCallback is called. There is a time gap between instantiating a publisher till a publish call to it actually publishes, as things are threaded behind the scenes. So what happens everytime your posCallback is called:
What you can do is make the Publisher a global variable and use it. Minimal example:
#include <ros/ros.h>
[...]
ros::Publisher pub_twist;
[...]
void posCallback(const geometry_msgs::Point& msg)
{
[...]
pub_twist.publish(twist_msg);
}
int main(int argc, char **argv)
{
[...]
pub_twist= nh.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
ros::Subscriber sub = nh.subscribe("color_position", 100, posCallback);
ros::spin();
}