Search code examples
pythonbashubuntuubuntu-16.04ros

Rostopic pub equivalent in Python


I am working with a simulated bebop2 These are the commands I am using to run the simulation.

sphinx /opt/parrot-sphinx/usr/share/sphinx/drones/bebop2.drone

roslaunch bebop_driver bebop_node.launch ip:=10.202.0.1

In this case bebop_driver is the subscriber and bebop_commander the publisher( see code below)

I've been using:

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}}'

in order to publish to cmd_vel topic successfully .I need to publish the same message to the same topic using a Python script, but so far I haven't been able.

This is the Python script I am trying to use :

#!/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()

Solution

  • Few mistakes/suggestions in your code:

    • You are not checking if the user is actually entering all the arguments at the start, namely filename, speed and time. Here try using below code:

      if len(sys.argv)>2:
         speed = float(sys.argv[1])
         time = float(sys.argv[2]) 
      else:
         print("one or more arguments missing!!")
      
    • There is no need of speed != "" and time != "" once you checked len(sys.argv)>2 condition.
    • you are passing an unknown variable movement_msg inside movement_publisher.publish(). Kindly check below line:

      movement_publisher.publish(movement_msg)
      

      It should be movement_cmd.

    Modified code(Tested):

    Filename: 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!!')
    

    Note: Don't forget to copy the file test_publisher.py to scripts directory and make it executable via chmod +x test_publisher.py

    Output:

    (Terminal 1): Run roscore command. You must have a roscore running in order for ROS nodes to communicate.

    enter image description here

    (Terminal 2): Run python publisher file with arguments.

    enter image description here

    (Terminal 3): checking rostopic information

    enter image description here