我是 ROS 新手,正在开发一个用于考试的小项目。 我刚刚完成了这个项目,但我想改变一点事情:我不想在 Ubuntu 中使用“ rostopic pub /start std_msgs/String "start" ”命令在主题上发布消息,而是使用 python 代码来完成它如图:
#!/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.")
现在,如果我在 ubuntu 终端中使用“rosrun simulazione_traffico utility.py command start”命令,我可以在终端上看到“确定”和“已发送”。 但是,使用“ rostopic echo /start ”,我看不到任何东西,因此字符串尚未发送到主题。你能告诉我为什么吗?我真的不知道我是否错过了一些重要的事情。
非常感谢。
您没有“旋转”您的 ros 节点。你需要在你的 main 函数中添加这样的东西:
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
rate.sleep()
请参阅此处了解详细信息:https://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29