问题描述
rosrun
命令未执行我的python文件。该命令只是被跳过。我已经使用命令使python脚本可执行
sudo chmod +x controller.py
。我无法运行任何python文件或rosrun命令。甚至python代码也没有错误。可能是什么问题?我是ROS的新手,所以请指导我。
controller.py
包含以下代码:
import rospy
from geometry_msgs.msg import Twist
#from sensor_msgs.msg import LaserScan
from nav_msgs.msg import odometry
from tf.transformations import euler_from_quaternion
import math
def odom_callback(data):
global x,y,pose,ebot_theta
x = data.pose.pose.orientation.x
y = data.pose.pose.orientation.y
z = data.pose.pose.orientation.z
w = data.pose.pose.orientation.w
pose = [data.pose.pose.position.x,data.pose.pose.position.y,euler_from_quaternion([x,z,w])[2]]
ebot_theta=euler_from_quaternion([x,w])[2]
#def laser_callback(msg):
#global regions
#regions = {
# 'bright':,# 'fright':,# 'front':,# 'fleft':,#'bleft':,#}
def Waypoints(t):
if t == 0:
h = 0.74
k = 0.488
elif t == 1:
h = 1.42
k = 1.289
elif t == 2:
h = 1.911
k = 1.54
elif t == 3:
h = 2.45
k = 1.2
elif t == 4:
h = 3.141
k = 0
elif t == 5:
h = 3.91
k = -1.289
elif t == 6:
h = 4.373
k = -1.54
elif t == 7:
h = 5.02
k = -1.125
elif t == 8:
h = 5.72
k = -0.297
elif t == 9:
h = 6.283
k = 0
else:
pass
return [h,k]
def control_loop():
rospy.init_node('ebot_controller',anonymous=True)
pub = rospy.Publisher('/cmd_vel',Twist,queue_size=10)
#rospy.Subscriber('/ebot/laser/scan',LaserScan,laser_callback)
rospy.Subscriber('/odom',odometry,odom_callback)
rate = rospy.Rate(10)
veLocity_msg = Twist()
veLocity_msg.linear.x = 0
veLocity_msg.angular.z = 0
pub.publish(veLocity_msg)
i=0
while not rospy.is_shutdown() & i<10:
[x1,y1]=[x,y]
[x2,y2]=Waypoints(i)
theta_goal= math.atan((y2-y1)/(x2-x1))
e_theta= ebot_theta-theta_goal
veLocity_msg.linear.x = 10
veLocity_msg.angular.z = (-1)*e_theta
pub.publish(veLocity_msg)
i=i+1
print("Controller message pushed at {}".format(rospy.get_time()))
rate.sleep()
if __name__ == '__control_loop__':
try:
control_loop()
except rospy.ROSInterruptException:
pass
解决方法
您的python脚本实际上没有运行任何东西。可以说它的主要功能是空的。
if __name__ == '__control_loop__':
需要成为
if __name__ == '__main__':
请参见https://docs.python.org/3/library/__main__.html。
另一件事是您可能会用&
时使用and
:
>>> False & 0<10
True
>>> False and 0<10
False
所以改变:
while not rospy.is_shutdown() & i<10:
到
while not rospy.is_shutdown() and i<10:
您还需要在循环中添加spin_once
。否则,将不会处理任何ROS通信。
#!/usr/bin/env python
添加此队友;)