I'm new in ROS and I'm developing a little project for an exam. I just finished the project, but I would change a little thing: instead of publish a message on a Topic using " rostopic pub /start std_msgs/String "start" " command in Ubuntu, I'd like to do it using a python code as shown:
#!/usr/bin/env python
from __future__ import print_function
from std_msgs.msg import String
import sys
import rospy
def start_stop(command):
pub = rospy.Publisher('start', String, queue_size=10)
command_str = String()
command_str.data = command
pub.publish(command_str)
rospy.loginfo("OK")
if __name__ == "__main__":
rospy.init_node('utility')
executed = False
if len(sys.argv) == 3:
if sys.argv[1].lower() == "command":
start_stop(sys.argv[2])
executed = True
elif ....
....
else:
executed = False
else:
executed = False
if executed:
print("Command sent")
else:
print("Command not sent.")
Now, if I use in ubuntu terminal " rosrun simulazione_traffico utility.py command start " command, I can see "OK" and "Sent" on the terminal. But, using " rostopic echo /start ", I can't see anything, so the String has not been sent to the Topic. Can you tell me why? I really don't know if I'm missing something important.
You are not "spinning" your ros node. You need to add something like this to your main function:
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
rate.sleep()
See here for details: https://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29