我试图写一个订阅者和发布者节点;但是当我尝试使用rosrun
运行发布者时,它给我一个错误:@!/usr/bin/env: No such file or directory
这是error和相应的代码(我做了chmod +x
):
发布者节点:
@!/usr/bin/env python
import rospy
from std_msgs.msg import String
def publish_it():
pub = rospy.Publisher("first_msgs",String , queue_size = 10)
rospy.init_node("publisher_node",anonymous = True)
x = 1
z = 10
rate = rospy.Rate(z) #1/z sec delay
while not rospy.is_shutdown():
x += 1
if x == 100:
x = 0
msg = "Hi this is our first message . times:" + str(x)
rospy.loginfo(msg)
pub.publish(msg)
rate.sleep()
if __name__ == "__main__":
try:
publish_it()
except rospy.ROSInterruptException:
pass
订户节点:
@!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(data.data)
def iSeeIt():
rospy.init_node("subscriber_node",anonymous = True)
rospy.Subscriber("first_msgs",String,callback)
rospy.spin()
if __name__ == "__main__":
iSeeIt()
答案 0 :(得分:0)
您正在滥用shebang构造。不必使用@!
,而必须编写#!
,然后编写应应用于代码的解释器。此外,该行(即#!/usr/bin/env python
)必须位于脚本的第一行中。因此,您的订户节点的头应如下所示:
#!/usr/bin/env python
# subscriber node
import rospy
from std_msgs.msg import String
...
答案 1 :(得分:0)
rospy
的初始化必须放置在发布者之前,并且您的代码shebang
应该如下:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def publish_it():
rospy.init_node("publisher_node", anonymous=True)
pub = rospy.Publisher("first_msgs", String, queue_size=10)
x = 1
z = 10
rate = rospy.Rate(z)
....
[注意]:
chmod +x mypythonscript.py
)#!/usr/bin/python3
或#!/usr/bin/env python3
)。答案 2 :(得分:0)
谢谢大家,我通过使用#!/ usr / bin / python2.7更改@!/ usr / bin / env python来解决了这个问题。我正在使用python3,但我不太了解它是如何工作的,但我可以