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

Problems with publishing a cmd_vel message

asked 2019-08-21 01:20:07 -0600

Tima1995 gravatar image

Hey,

I am just trying to send my robot a msg that he starts moving forward. When it comes to receiving a msg, I can use rospy.wait_for_message to be sure that one has received that one. When it comes to moving the bot around, normally that worked well with the following code:

def moving(self): 
   self.pub_cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=5) 
   vel_cmd = Twist() 
   vel_cmd.linear.x = 0.1 
   self.pub_cmd_vel.publish(vel_cmd)

But the robot wasn´t moving. That´s why I implemented this code, which was working again:

def moving(self): 
       self.pub_cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=5) 
       self.finish = True 
       while self.finish: 
          vel_cmd = Twist() 
          vel_cmd.linear.x = 0.1 
          self.pub_cmd_vel.publish(vel_cmd)

My question is now, why is the one option working but the other don´t.

Thanks for your help!

edit retag flag offensive close merge delete

Comments

Can you confirm your loop is running with a print or something?

PapaG gravatar image PapaG  ( 2019-08-21 01:25:08 -0600 )edit

Yes it is running. When i have a print the terminal is full of it. I am thinking about the while not rospy.is_shutdown, that might be missing

Tima1995 gravatar image Tima1995  ( 2019-08-21 01:32:03 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-08-21 02:01:43 -0600

Your first code snippet is not working because ROS Publishers take a small amount of time to connect to other nodes so that messages can be delivered. Any messages published on them before then will get lost, which is the case here.

Your second code snippet works because even though the first few messages are lost, the while loop keeps publishing them so after a short while they will reach the destination node.

If you want to send a single message you can wait until at least one other node has connected to the publisher before publishing the message using the code below:

while self.pub_cmd_vel.get_num_connections() < 1:
    pass

Hope this helps.

edit flag offensive delete link more

Comments

Working :) Thanks for your answer!

Tima1995 gravatar image Tima1995  ( 2019-08-21 07:15:40 -0600 )edit

Hello again, i have one additional question. I tried to implement that solution into a bigger code. There, depending on a laser scan, I calculate the next action using a NN. This loop is performed several hundred times. But when I try your solution, I get stuck with self.pub_cmd_vel.get_num_connections() because it is constant 1 after execution. Is there another solution for the problem of sending till it is received so I can immediately continue afterwards? I was thinking about a dirty solution like haveing a while loop that stops the pubishing after a certain amount of time (i.e. 0.05 sec) that ensure delivering the message. But there might be another better suited solution. Kind regards and thanks in advance!

Tima1995 gravatar image Tima1995  ( 2019-10-16 11:37:46 -0600 )edit

If you need to guarantee delivery of a piece of information from one node to another, a topic is not the tool to use. You want to use a service instead, these have receipt acknowledgment, and are designed for this type of use.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-10-27 05:34:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-08-21 01:20:07 -0600

Seen: 1,601 times

Last updated: Aug 21 '19