Ask Your Question
0

Problem publishing at cmd_vel

asked 2016-08-01 09:49:55 -0500

patrchri gravatar image

Hello,

I have written the following code so I can send commands to my robots motors, which are listening to the /labrob/cmd_vel topic:

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

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

  ros::init(argc, argv,"vel");
  ros::NodeHandle vel;
  ros::Publisher vel_pub = vel.advertise<geometry_msgs::Twist>("/labrob/cmd_vel", 100);
  ros::Rate loop_rate(10);
    geometry_msgs::Twist msg;
    msg.linear.x = 0.1;
    msg.linear.y = 0;
    msg.linear.z = 0;
    msg.angular.x = 0;
    msg.angular.y = 0;
    msg.angular.z = 0;
    vel_pub.publish(msg);
    ros::spin();

  return 0;
}

With the rostopic pub command via the terminal, I can move the robot fine, but with the code I posted, the robot does not respond. What I am doing wrong ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-08-01 10:19:42 -0500

patrchri gravatar image

updated 2016-08-01 10:22:17 -0500

I don't know what went wrong, but I modified the code to this:

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

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

  ros::init(argc, argv,"vel");
  ros::NodeHandle vel;
  ros::Publisher vel_pub = vel.advertise<geometry_msgs::Twist>("/labrob/cmd_vel", 100);
    while(ros::ok){
    geometry_msgs::Twist msg;
    msg.linear.x = 0.1;
    msg.linear.y = 0;
    msg.linear.z = 0;
    msg.angular.x = 0;
    msg.angular.y = 0;
    msg.angular.z = 0;
    vel_pub.publish(msg);
    ros::spinOnce();
    }
  return 0;
}

And it worked.

edit flag offensive delete link more

Comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-08-01 09:49:55 -0500

Seen: 1,176 times

Last updated: Aug 01 '16