0
$\begingroup$

Rosanswers logo

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?


Originally posted by mr19b020 on ROS Answers with karma: 3 on 2021-03-16

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by Tristan9497 with karma: 220 on 2021-03-16

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by mr19b020 on 2021-03-17:
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".

Comment by Tristan9497 on 2021-03-17:
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?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.