Monday, March 16, 2020

How to run a shell script from a ROS launch file

I wanted to execute a bash shell script from the ROS launch file but the ROS Wiki were not very clear. After some trial and error, I figured out how to do it. The following steps illustrate the procedure I used:

Create a shell script
  1. In the ROS workspace package, e.g. /path/to/workspace/package/script/ folder, create a shell script e.g. run_script.sh.
  2. Type in the script commands, e.g. see the code listing below.

    Note 1: ensure the shebang statement is at the top i.e. #!/bin/bash and a exit status code (0 for success or other values) is returned from the script.

    Note 2: Use the chmod command to make the script executable, e.g. $ chmod a+x run_script.sh

#!/bin/bash

# just print this out
echo "Hello ROS world"

# exit gracefully by returning a status 
exit 0


Create a launch file
  1. In the ROS workspace package launch folder, create a launch file e.g. /path/to/workspace/package/launch/hello_script.launch.
  2. Using a text editor, type in the following:

    Note: fill in the package name, e.g. beginner_tutorials, and the type, which should be the shell script name; name is any label you want to associate with the script node.


<launch>
        <node pkg="beginner_tutorials"
                type="run_script.sh" name="run_script"
                output="screen"
        />
</launch>

Run the launch file
  1. In a terminal, type in the ros launch command:

    $ roslaunch beginner_tutorials hello_script.launch

    Note: change beginner_tutorials to your package name and hello_script.launch to the launch file created previously.

    The script is executed as shown in the print out of "Hello ROS world" below.

No comments:

Related Posts with Thumbnails