Ask Your Question
0

Making a simple program which uses cmd_vel [closed]

asked 2021-03-16 14:39:19 -0600

mr19b020 gravatar image

Hello, I am trying to make a simple program which sends a cmd_vel message. My program is looking like this right now. I work on an virtual machine with Ubuntu (ROS noetic) if that matters.

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

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

  ros::init(argc, argv,"talker");
  ros::NodeHandle nh;

  ros::Publisher chatter;
  chatter = nh.advertise<geometry_msgs::Twist>("/rosbot/cmd_vel", 100);
  ros::Rate loop_rate(10);
    geometry_msgs::Twist msg;
    msg.linear.x = 1;
    msg.linear.y = 0;
    msg.linear.z = 0;
    msg.angular.x = 0;
    msg.angular.y = 0;
    msg.angular.z = 0;
    ROS_INFO_STREAM("Sent message is: "<<msg.linear.x);
    chatter.publish(msg);
    ros::spin();

  return 0;
}

I can enter this in the command line and the robot moves without a problem:

rostopic pub /rosbot/cmd_vel geometry_msgs/Twist "linear:
  x: 0.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0

Where is the problem in my code?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by mr19b020
close date 2021-03-17 12:05:00.440064

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-03-16 18:50:41 -0600

Hi,

your problem probably is, that ros::spin is made to contiously request data via subscribers. It does not affect publishers like described in 257361.

I guess your publisher publishes the velocity only once. So we introduce a while loop that repeats at a fixed rate as long as the rosmaster node is active.

please try the following:

ros::Rate loop_rate(10);
geometry_msgs::Twist msg;
while(ros::ok())
{
   msg.linear.x = 1;
   msg.linear.y = 0;
   msg.linear.z = 0;
   msg.angular.x = 0;
   msg.angular.y = 0;
   msg.angular.z = 0;
   chatter.publish(msg);
   loop_rate.sleep(); //if you want to have somewhat fixed rate publishing 
   ros::spinOnce();           
}

furthermore try removing the first "/" at your frame_id in the advertis statement so you have the following: I am unsure if that makes a difference but this here definately works

chatter = nh.advertise<geometry_msgs::Twist>("rosbot/cmd_vel", 100);

Btw, I dont think you need a that big queue in the publisher probably 5 does the job allready, but this shouldn't make a difference for your problem.

Let me know if this helped you

edit flag offensive delete link more

Comments

Hi, thanks for the answer and the good explanation. The program now works even with the "/" in the advertise statement. I thought that I don't need a loop, because after entering the "rostopic pub" in the comand line the robot was driving even after I entered "strg+c".

mr19b020 gravatar image mr19b020  ( 2021-03-17 12:04:37 -0600 )edit

I am happy that i was able to help you. Yea i thought that both should work but wasn't entirely sure about it.

Can you accept my answer, if it solved the question by clicking the check mark next to my answer?

Tristan9497 gravatar image Tristan9497  ( 2021-03-17 12:53:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-03-16 14:39:19 -0600

Seen: 67 times

Last updated: Mar 17