Search code examples
pythonbashparsingros

How to pass arguments to ros launch file from bash script and how to create ros launch file running python script with those parser arguments


I have a python script which runs as follows:

rosrun camera_calibrator cameracalibrator.py --size 12x8 --square 0.05 image:=/topic_name

I have launch file are follows:

<launch>
  <node name="image_pub_sub_node" pkg="image_pub" type="image_pub_sub" output="screen"/>
  <node name="cameracalibrator_node" pkg="camera_calibration" type="cameracalibrator.py" output="screen"/>
</launch>

Bash script as:

#!/bin/sh
foldername=camera_calibration_$(date +"%m-%d-%Y")
roslaunch image_pub cam_calibrator.launch $foldername

I want to pass this foldername in bash to roslaunch file (cam_calibrator.launch) as above, then get that folder-name as argument and send that to my python script "cameracalibrator.py" just like --size, --square and image:=/topic_name arguments as well to the image_pub_sub c++ script.

Also how to send those size and square arguments to python script in launch file?

Can anyone let me now please?


Solution

  • The roslaunch command line accept arguments, so yes it is possible to do this.

    In your launch file, you must declare the arguments like this :

    <launch>
        <arg name="foldername"      default="whatever_you_want" />
    </launch>
    

    Then, you want to check the list of parameters supported by the nodes that you are using. Typically, I see that you are using the node cameracalibrator.py. If this node is well written, it defines to ROS the parameters that it uses, like image (which I am assuming is a ROS topic name). Once you know which parameter your node needs, you can provide it to ROS in your launch file like this:

    (in this case, I am using the image parameter since it is the only one from your question where I am sure it is a normal ROS parameter)

    <launch>
        <!-- list of arguments that can be given as inputs of the launch file -->
        <arg name="foldername"      default="whatever_you_want" />
        <arg name="image_name"      default="whatever_you_want" />
    
        <node name="cameracalibrator_node" pkg="camera_calibration" type="cameracalibrator.py" output="screen">
    
            <!-- list of all parameters to pass to the node -->
            <param name="image"   type="string" value="$(arg image_name)" />
    
        </node>
    
    </launch>
    

    Once you have such a ros launch file, you can call it with:

    #!/usr/bin/env bash
    roslaunch image_pub cam_calibrator.launch foldername :=foldername image_name:=$YOUR_IMAGE_NAME
    

    PS: it is recommended to use #!/usr/bin/env bash (or sh) instead of #!/bin/sh