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

Revision history [back]

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.