Search code examples
pythonubuntu-16.04ros

Launch a ROS Node with an specific IP


I've been publishing messages to a simulated Bebop2 launching the node with this commandroslaunch bebop_driver bebop_node.launch ip:=10.202.0.1 . I am trying to write a python script in order to do the same using this rospy.init_node("bebop_driver" ) , but I cant find the way to specify the ip.

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

Once the drone is successfully simulated I use this command for takeoff

rostopic pub --once bebop/takeoff std_msgs/Empty

Here is my bebop_node.launch

<?xml version="1.0"?>
<launch>
   <arg name="namespace" default="bebop" />
   <arg name="ip" default="10.202.0.1" />
   <arg name="drone_type" default="bebop2" /> <!-- available drone types: bebop1, bebop2 -->
   <arg name="config_file" default="$(find bebop_driver)/config/defaults.yaml" />
   <arg name="camera_info_url" default="package://bebop_driver/data/$(arg drone_type)_camera_calib.yaml" />
       <node pkg="bebop_driver" name="bebop_driver" type="bebop_driver_node" output="screen">
           <param name="camera_info_url" value="$(arg camera_info_url)" />
           <param name="bebop_ip" value="$(arg ip)" />
           <rosparam command="load" file="$(arg config_file)" />
       </node>
       <include file="$(find bebop_description)/launch/description.launch" />

</launch>

This is the python I am trying to use in order to move the drone by publishing messages to the cmd_vel topic.


#!/usr/bin/env python

import roslib;roslib.load_manifest('bebop_driver')

import rospy

from geometry_msgs.msg import Twist

import sys

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 : 

  print ("Publishing")

  pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)

  rospy.init_node("bebop_commander" )


  twist = Twist()
  twist.linear.x = speed; twist.linear.y = 0; twist.linear.z = 0;                      
  twist.angular.x = 0;  twist.angular.y = 0;  twist.angular.z = 0; 

  pub.publish(twist)


rospy.spin()


Solution

  • By adding ip:=10.202.0.1 to your command you are setting a launch file argument. Checking the called launch file shows

    <param name="bebop_ip" value="$(arg ip)" />
    

    which means the argument is used to set the parameter bebop_ip. This can be also achieved in python like:

    rospy.set_param('bebop_ip', '10.202.0.1')
    

    You can find the documentation at the ROS wiki. Note that setting parameters also can be done in C++ or by command-line.

    Update:

    Since you are trying to command a running driver, you should change the name of your commander because two nodes of the same name are not possible:

    rospy.init_node("bebop_commander")
    

    Also you need to add the driver name to params and topics like

    rospy.set_param('/bebop_driver/bebop_ip', '10.202.0.1')
    

    and

    pub = rospy.Publisher('/bebop_driver/cmd_vel', Twist, queue_size=10)
    

    to address the running driver correctly.