Python中的Rostopic pub等效项

时间:2019-06-25 22:47:18

标签: python bash ubuntu ubuntu-16.04 ros

我正在使用模拟的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()

1 个答案:

答案 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节点进行通信。

enter image description here

(端子2):使用参数运行python publisher文件。

enter image description here

(3号航站楼):检查rostopic信息

enter image description here