我正在使用模拟的bebop2 这些是我用来运行模拟的命令。
sphinx /opt/parrot-sphinx/usr/share/sphinx/drones/bebop2.drone
roslaunch bebop_driver bebop_node.launch ip:=10.202.0.1
在这种情况下, bebop_driver 是订阅者,而 bebop_commander 是发布者(请参见下面的代码)
我一直在使用:
rostopic pub -r 10 cmd_vel geometry_msgs/Twist '{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}'
为了成功地发布到 cmd_vel 主题。我需要使用Python脚本将同一条消息发布到同一主题,但到目前为止,我还没有能够。
这是我要使用的Python脚本:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import sys
rospy.init_node("bebop_commander")
movement_publisher= rospy.Publisher('cmd_vel', Twist , queue_size=10)
movement_cmd = Twist()
speed = float(sys.argv[1])
time = float(sys.argv[2])
print ("Adelante")
if speed != "" and speed > 0 :
print ("Velocidad =" , speed , "m/s")
else:
print ("Falta parametro de velocidad o el valor es incorrecto")
if time != "" and time > 0 :
print ("Tiempo = ",time, "s")
else:
print ("Falta parametro de tiempo o el valor es incorrecto")
if time != "" and time > 0 :
movement_cmd.linear.x = 0
movement_cmd.linear.y = 0
movement_cmd.linear.z = 0
movement_cmd.angular.x = 0
movement_cmd.angular.y = 0
movement_cmd.angular.z = 0
movement_publisher.publish(movement_msg)
print ("Publishing")
rospy.spin()
答案 0 :(得分:1)
代码中很少有错误/建议:
您没有检查用户是否实际上在开始时输入了所有参数,即文件名,速度和时间。这里尝试使用以下代码:
if len(sys.argv)>2:
speed = float(sys.argv[1])
time = float(sys.argv[2])
else:
print("one or more arguments missing!!")
speed != ""
条件后,就不需要time != ""
和len(sys.argv)>2
。您正在movement_msg
内传递未知变量movement_publisher.publish()
。请检查以下行:
movement_publisher.publish(movement_msg)
应为movement_cmd
。
修改后的代码(已测试):
文件名:test_publisher.py
import rospy
from geometry_msgs.msg import Twist
import sys
rospy.init_node("bebop_commander")
movement_publisher= rospy.Publisher('cmd_vel', Twist , queue_size=10)
movement_cmd = Twist()
if len(sys.argv)>2:
speed = float(sys.argv[1])
time = float(sys.argv[2])
print("Adelante")
if speed > 0.0:
print("Velocidad =" , speed , "m/s")
else:
print("Falta parametro de velocidad o el valor es incorrecto")
if time > 0.0:
print ("Tiempo = ",time, "s")
movement_cmd.linear.x = 0
movement_cmd.linear.y = 0
movement_cmd.linear.z = 0
movement_cmd.angular.x = 0
movement_cmd.angular.y = 0
movement_cmd.angular.z = 0
movement_publisher.publish(movement_cmd)
print ("Publishing")
rospy.spin()
else:
print ("Falta parametro de tiempo o el valor es incorrecto")
else:
print('one or more argument is missing!!')
注意:请不要忘记将文件test_publisher.py
复制到scripts
目录并通过chmod +x test_publisher.py
使其可执行。
输出:
(端子1):运行roscore
命令。您必须运行roscore
才能使ROS
节点进行通信。
(端子2):使用参数运行python publisher
文件。
(3号航站楼):检查rostopic
信息