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

Revision history [back]

click to hide/show revision 1
initial version

If you want to synchronize the data and use one callback for both of the topics, have a look at the message_filter tutorial.

If you want to synchronize the data and use one callback for both of the topics, have a look at the message_filter tutorial.

Edit:

The callback should look something like this (no guarantee that this works though, beware of typos):

void velCallback(geometry_msgs::Twist::ConstPtr& vel) {
 //Since vel is a const pointer you cannot edit the values inside but have to use the copy new_vel.
 geometry_msgs::Twist new_vel = *vel; 
 if(vel->linear.x > 1.8) {
    new_vel.linear.x = 1.8; 
    vel_pub_.publish(new_vel);
  }
}

Please have a look at this tutorial to see how subscribers and publishers work.

I would also suggest to have a queue size of at least 1: ros::Subscriber vel_sub = n.subscribe("cmd_vel", 1, velCallback);

If you want to synchronize the data and use one callback for both of the topics, have a look at the message_filter tutorial.

Edit:

The callback should look something like this (no guarantee that this works though, beware of typos):

void velCallback(geometry_msgs::Twist::ConstPtr& vel) {
 //Since vel is a const pointer you cannot edit the values inside but have to use the copy new_vel.
 geometry_msgs::Twist new_vel = *vel; 
 if(vel->linear.x > 1.8) {
    new_vel.linear.x = 1.8; 
    vel_pub_.publish(new_vel);
  }
}

Please have a look at this tutorial to see how subscribers and publishers work.

I would also suggest to have a queue size of at least 1: ros::Subscriber vel_sub = n.subscribe("cmd_vel", 1, velCallback);

Edit2:

For the case you mentioned in the comments something like this could work (not tested, beware of typos):

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

ros::Publisher pub;

void velCallback(geometry_msgs::Twist::ConstPtr& vel)
{
   geometry_msgs::Twist new_vel = *vel;
   if (vel->linear.x > 1.8) {
     new_vel.linear.x = 1.8;
   }
   pub.publish(new_vel);
}

int main(int argc, char **argv)
{

ros::init(argc, argv, "my_node");
ros::NodeHandle n;

pub = n.advertise<geometry_msgs::Twist>("cmd_vel", 10);
ros::Subscriber sub = n.subscribe("my_cmd_vel", 10, velCallback);

ros::spin();

return 0;
}

You only have to make sure that the node publishing the original cmd_vel values publishes to my_cmd_vel now which you can achieve via remapping the topic in the launch file. The node I posted above will take that value and pass it on to cmd_vel or change the linear speed if necessary and then pass it on to cmd_vel.

Since the callback is running in a different thread than your main function you cannot rely on always having a value for your linear_actual in the main function. So do whatever you need to do with the actual values in the callback. In this easy example this is good enough.