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

Basing on the fact that when you run rostopic echo /cmd_vel you are able to get the information, I created a basic version of your code and it seems to be working for me:

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

void cmd_vel_callback(const geometry_msgs::Twist& vel_cmd)
{ 
    ROS_INFO("I heard: [%f]", vel_cmd.linear.y);
    std::cout << "Twist Received " << std::endl;
}


int main( int argc, char* argv[] )
{
ros::init(argc, argv, "toeminator_publisher" );

ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/cmd_vel", 1000, cmd_vel_callback);

while( n.ok() ) 
{
    ros::spin();
}

return 1;
}

I changed ROS_INFO("I heard: [%s]", vel_cmd.linear.y); to ROS_INFO("I heard: [%f]", vel_cmd.linear.y); because it was giving me an Segmentation fault error message. Make sure you have added the geometry_msgs to your dependencies. I hope this helps. Good luck!

Basing Based on the fact that when you run rostopic echo /cmd_vel you are able to get the information, I created a basic version of your code and it seems to be working for me:

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

void cmd_vel_callback(const geometry_msgs::Twist& vel_cmd)
{ 
    ROS_INFO("I heard: [%f]", vel_cmd.linear.y);
    std::cout << "Twist Received " << std::endl;
}


int main( int argc, char* argv[] )
{
ros::init(argc, argv, "toeminator_publisher" );

ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/cmd_vel", 1000, cmd_vel_callback);

while( n.ok() ) 
{
    ros::spin();
}

return 1;
}

I changed ROS_INFO("I heard: [%s]", vel_cmd.linear.y); to ROS_INFO("I heard: [%f]", vel_cmd.linear.y); because it was giving me an Segmentation fault error message. Make sure you have added the geometry_msgs to your dependencies. I hope this helps. Good luck!