ROS and Node-RED Part 3: Managing ROS launch configurations

Once you start using ROS you will have to write launch files that combine a set of ROS nodes to a meaningful robot runtime configuration. I personally found it always tedious to edit these files, combine functionalities, adapt parameters and restart the whole launch file from command line. In this third tutorial, I present my Node-RED approach of handing launch configurations and parameters.

Roslaunch from Node-RED

It is mandatory to have configured a ROS command line environment when you start the Node-RED server. This can be achieved easily when you have sourced the ROS setup in your .bashrc. Btw. we are still on Ubuntu 14.04 as you might have guessed from the first tutorial:

source /opt/ros/indigo/setup.bash
source ~/catkin_ws/devel/setup.bash

Be careful when using Node-RED as a system service in Ubuntu using upstart. I was not able to execute the above two lines in an upstart script. My solution when Node-RED should be started at system startup is to auto login a normal user (see this link how to configure autologin, in German, sorry –> leave me a comment for any similar English explanation) and add the ROS setup and Node-RED startup to this users .bashrc. However, this solution may be a security risk, if a keyboard is plugged to the PC.

My roslaunch flow is then performed in three steps in Node-RED when the flow is triggered, e.g., using an inject node:

  1. Create a roslaunch configuration in Node-RED using a template node.
  2. Write this configuration to a temporary file using a file node. (wait a second before the file is written using delay node)
  3. Execute a roslaunch command on the temporary file using an exec node.

A flow implementing the above three steps is given below.

[
    {
        "id": "cc9a367a.dd0918",
        "type": "tab",
        "label": "Test1"
    },
    {
        "id": "b73657b6.347178",
        "type": "template",
        "z": "cc9a367a.dd0918",
        "name": "minimal.launch",
        "field": "payload",
        "fieldType": "msg",
        "format": "html",
        "syntax": "plain",
        "template": "<launch>\n  <!-- Turtlebot -->\n  <arg name=\"base\"              default=\"$(env TURTLEBOT_BASE)\"         doc=\"mobile base type [create, roomba]\"/>\n  <arg name=\"battery\"           default=\"$(env TURTLEBOT_BATTERY)\"      doc=\"kernel provided locatio for battery info, use /proc/acpi/battery/BAT0 in 2.6 or earlier kernels.\" />\n  <arg name=\"stacks\"            default=\"$(env TURTLEBOT_STACKS)\"       doc=\"stack type displayed in visualisation/simulation [circles, hexagons]\"/>\n  <arg name=\"3d_sensor\"         default=\"$(env TURTLEBOT_3D_SENSOR)\"    doc=\"3d sensor types [kinect, asux_xtion_pro]\"/>\n  <arg name=\"simulation\"        default=\"$(env TURTLEBOT_SIMULATION)\"   doc=\"set flags to indicate this turtle is run in simulation mode.\"/>\n  <arg name=\"serialport\"        default=\"$(env TURTLEBOT_SERIAL_PORT)\"  doc=\"used by create to configure the port it is connected on [/dev/ttyUSB0, /dev/ttyS0]\"/>\n  <arg name=\"robot_name\"        default=\"$(env TURTLEBOT_NAME)\"         doc=\"used as a unique identifier and occasionally to preconfigure root namespaces, gateway/zeroconf ids etc.\"/>\n  <arg name=\"robot_type\"        default=\"$(env TURTLEBOT_TYPE)\"         doc=\"just in case you are considering a 'variant' and want to make use of this.\"/>\n\n  <param name=\"/use_sim_time\" value=\"$(arg simulation)\"/>\n\n  <include file=\"$(find turtlebot_bringup)/launch/includes/robot.launch.xml\">\n    <arg name=\"base\" value=\"$(arg base)\" />\n    <arg name=\"stacks\" value=\"$(arg stacks)\" />\n    <arg name=\"3d_sensor\" value=\"$(arg 3d_sensor)\" />\n  </include>\n  <include file=\"$(find turtlebot_bringup)/launch/includes/mobile_base.launch.xml\">\n    <arg name=\"base\" value=\"$(arg base)\" />\n    <arg name=\"serialport\" value=\"$(arg serialport)\" />\n  </include>\n  <!-- Rosbridge -->\n  <arg name=\"port\" default=\"9090\" />\n  <arg name=\"address\" default=\"\" />\n  <arg name=\"ssl\" default=\"false\" />\n  <arg name=\"certfile\" default=\"\"/>\n  <arg name=\"keyfile\" default=\"\" />\n\n  <arg name=\"retry_startup_delay\" default=\"5\" />\n\n  <arg name=\"fragment_timeout\" default=\"600\" />\n  <arg name=\"delay_between_messages\" default=\"0\" />\n  <arg name=\"max_message_size\" default=\"None\" />\n\n  <arg name=\"authenticate\" default=\"false\" />\n\n  <group if=\"$(arg ssl)\">\n    <node name=\"rosbridge_websocket\" pkg=\"rosbridge_server\" type=\"rosbridge_websocket\" output=\"screen\">\n      <param name=\"certfile\" value=\"$(arg certfile)\" />\n      <param name=\"keyfile\" value=\"$(arg keyfile)\" />\n      <param name=\"authenticate\" value=\"$(arg authenticate)\" />\n      <param name=\"port\" value=\"$(arg port)\"/>\n      <param name=\"address\" value=\"$(arg address)\"/>\n      <param name=\"retry_startup_delay\" value=\"$(arg retry_startup_delay)\"/>\n      <param name=\"fragment_timeout\" value=\"$(arg fragment_timeout)\"/>\n      <param name=\"delay_between_messages\" value=\"$(arg delay_between_messages)\"/>\n      <param name=\"max_message_size\" value=\"$(arg max_message_size)\"/>\n    </node>\n  </group>\n  <group unless=\"$(arg ssl)\">\n    <node name=\"rosbridge_websocket\" pkg=\"rosbridge_server\" type=\"rosbridge_websocket\" output=\"screen\">\n      <param name=\"authenticate\" value=\"$(arg authenticate)\" />\n      <param name=\"port\" value=\"$(arg port)\"/>\n      <param name=\"address\" value=\"$(arg address)\"/>\n      <param name=\"retry_startup_delay\" value=\"$(arg retry_startup_delay)\"/>\n      <param name=\"fragment_timeout\" value=\"$(arg fragment_timeout)\"/>\n      <param name=\"delay_between_messages\" value=\"$(arg delay_between_messages)\"/>\n      <param name=\"max_message_size\" value=\"$(arg max_message_size)\"/>\n    </node>\n  </group>\n\n  <node name=\"rosapi\" pkg=\"rosapi\" type=\"rosapi_node\" />\n</launch>\n",
        "x": 260,
        "y": 280,
        "wires": [
            [
                "e397f13b.7f1ea"
            ]
        ]
    },
    {
        "id": "3e55c524.39868a",
        "type": "inject",
        "z": "cc9a367a.dd0918",
        "name": "Start",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 110,
        "y": 280,
        "wires": [
            [
                "b73657b6.347178"
            ]
        ]
    },
    {
        "id": "4062327e.354b8c",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "content",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 920,
        "y": 200,
        "wires": []
    },
    {
        "id": "6e3567b.42d3098",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "filename",
        "active": true,
        "console": "false",
        "complete": "filename",
        "x": 920,
        "y": 140,
        "wires": []
    },
    {
        "id": "e397f13b.7f1ea",
        "type": "function",
        "z": "cc9a367a.dd0918",
        "name": "Convert Filename",
        "func": "msg.filename = \"/tmp/\" + parseInt( new Date().getTime()) + \".launch\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 450,
        "y": 280,
        "wires": [
            [
                "29bdc1.1329624",
                "343cb1cc.66fa6e",
                "6e3567b.42d3098",
                "4062327e.354b8c"
            ]
        ]
    },
    {
        "id": "29bdc1.1329624",
        "type": "file",
        "z": "cc9a367a.dd0918",
        "name": "write launchfile",
        "filename": "",
        "appendNewline": true,
        "createDir": false,
        "overwriteFile": "false",
        "x": 720,
        "y": 280,
        "wires": []
    },
    {
        "id": "343cb1cc.66fa6e",
        "type": "delay",
        "z": "cc9a367a.dd0918",
        "name": "Wait 1s",
        "pauseType": "delay",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 240,
        "y": 400,
        "wires": [
            [
                "569f5f6e.3d6b4"
            ]
        ]
    },
    {
        "id": "3f9b0113.e9a65e",
        "type": "exec",
        "z": "cc9a367a.dd0918",
        "command": "roslaunch ",
        "addpay": true,
        "append": "",
        "useSpawn": "",
        "timer": "",
        "name": "",
        "x": 560,
        "y": 400,
        "wires": [
            [
                "81ca8c0d.05b86"
            ],
            [
                "380f8a22.6be1e6"
            ],
            [
                "c640c322.33cef"
            ]
        ]
    },
    {
        "id": "81ca8c0d.05b86",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "stdout",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 910,
        "y": 340,
        "wires": []
    },
    {
        "id": "380f8a22.6be1e6",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "stderr",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 910,
        "y": 380,
        "wires": []
    },
    {
        "id": "c640c322.33cef",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "return code",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 930,
        "y": 420,
        "wires": []
    },
    {
        "id": "569f5f6e.3d6b4",
        "type": "change",
        "z": "cc9a367a.dd0918",
        "name": "Set Launchfile",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filename",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 400,
        "wires": [
            [
                "3f9b0113.e9a65e"
            ]
        ]
    }
]

Adding flexibility

So far, we cannot combine different launch file sections on demand, since we only have one template node to produce the content. We have to look a bit deeper into the template node that is used to produce the content of the launch file. First, we split the node into many in order to combine various launchfile sections at runtime. As an example, we split the minimal.launch that is used to startup a basic Turtlebot 2 configuration into a header, footer and content part of the launch file. Furthermore, we add a content part containing the launch configuration for an USB Camera 3D Sensor. The image below is showing the result.

3D sensor left out from launchfile

We are now able to omit the 3D sensor in each roslaunch simply by drawing a wire directly from minimal.launch to </launch>. As a result, launch files can be composed from text modules on demand. No launch file editing is needed anymore. In a next step, we add even more flexibility by making launch file parameters explicitly adaptable as Node-RED variables.

The resulting flow can be copy & pasted from below.

[
    {
        "id": "cc9a367a.dd0918",
        "type": "tab",
        "label": "Test1"
    },
    {
        "id": "b73657b6.347178",
        "type": "template",
        "z": "cc9a367a.dd0918",
        "name": "minimal.launch",
        "field": "payload",
        "fieldType": "msg",
        "format": "html",
        "syntax": "mustache",
        "template": "{{&payload}}  \n  <!-- Turtlebot -->\n  <arg name=\"base\"              default=\"$(env TURTLEBOT_BASE)\"         doc=\"mobile base type [create, roomba]\"/>\n  <arg name=\"battery\"           default=\"$(env TURTLEBOT_BATTERY)\"      doc=\"kernel provided locatio for battery info, use /proc/acpi/battery/BAT0 in 2.6 or earlier kernels.\" />\n  <arg name=\"stacks\"            default=\"$(env TURTLEBOT_STACKS)\"       doc=\"stack type displayed in visualisation/simulation [circles, hexagons]\"/>\n  <arg name=\"3d_sensor\"         default=\"$(env TURTLEBOT_3D_SENSOR)\"    doc=\"3d sensor types [kinect, asux_xtion_pro]\"/>\n  <arg name=\"simulation\"        default=\"$(env TURTLEBOT_SIMULATION)\"   doc=\"set flags to indicate this turtle is run in simulation mode.\"/>\n  <arg name=\"serialport\"        default=\"$(env TURTLEBOT_SERIAL_PORT)\"  doc=\"used by create to configure the port it is connected on [/dev/ttyUSB0, /dev/ttyS0]\"/>\n  <arg name=\"robot_name\"        default=\"$(env TURTLEBOT_NAME)\"         doc=\"used as a unique identifier and occasionally to preconfigure root namespaces, gateway/zeroconf ids etc.\"/>\n  <arg name=\"robot_type\"        default=\"$(env TURTLEBOT_TYPE)\"         doc=\"just in case you are considering a 'variant' and want to make use of this.\"/>\n\n  <param name=\"/use_sim_time\" value=\"$(arg simulation)\"/>\n\n  <include file=\"$(find turtlebot_bringup)/launch/includes/robot.launch.xml\">\n    <arg name=\"base\" value=\"$(arg base)\" />\n    <arg name=\"stacks\" value=\"$(arg stacks)\" />\n    <arg name=\"3d_sensor\" value=\"$(arg 3d_sensor)\" />\n  </include>\n  <include file=\"$(find turtlebot_bringup)/launch/includes/mobile_base.launch.xml\">\n    <arg name=\"base\" value=\"$(arg base)\" />\n    <arg name=\"serialport\" value=\"$(arg serialport)\" />\n  </include>\n  <!-- Rosbridge -->\n  <arg name=\"port\" default=\"9090\" />\n  <arg name=\"address\" default=\"\" />\n  <arg name=\"ssl\" default=\"false\" />\n  <arg name=\"certfile\" default=\"\"/>\n  <arg name=\"keyfile\" default=\"\" />\n\n  <arg name=\"retry_startup_delay\" default=\"5\" />\n\n  <arg name=\"fragment_timeout\" default=\"600\" />\n  <arg name=\"delay_between_messages\" default=\"0\" />\n  <arg name=\"max_message_size\" default=\"None\" />\n\n  <arg name=\"authenticate\" default=\"false\" />\n\n  <group if=\"$(arg ssl)\">\n    <node name=\"rosbridge_websocket\" pkg=\"rosbridge_server\" type=\"rosbridge_websocket\" output=\"screen\">\n      <param name=\"certfile\" value=\"$(arg certfile)\" />\n      <param name=\"keyfile\" value=\"$(arg keyfile)\" />\n      <param name=\"authenticate\" value=\"$(arg authenticate)\" />\n      <param name=\"port\" value=\"$(arg port)\"/>\n      <param name=\"address\" value=\"$(arg address)\"/>\n      <param name=\"retry_startup_delay\" value=\"$(arg retry_startup_delay)\"/>\n      <param name=\"fragment_timeout\" value=\"$(arg fragment_timeout)\"/>\n      <param name=\"delay_between_messages\" value=\"$(arg delay_between_messages)\"/>\n      <param name=\"max_message_size\" value=\"$(arg max_message_size)\"/>\n    </node>\n  </group>\n  <group unless=\"$(arg ssl)\">\n    <node name=\"rosbridge_websocket\" pkg=\"rosbridge_server\" type=\"rosbridge_websocket\" output=\"screen\">\n      <param name=\"authenticate\" value=\"$(arg authenticate)\" />\n      <param name=\"port\" value=\"$(arg port)\"/>\n      <param name=\"address\" value=\"$(arg address)\"/>\n      <param name=\"retry_startup_delay\" value=\"$(arg retry_startup_delay)\"/>\n      <param name=\"fragment_timeout\" value=\"$(arg fragment_timeout)\"/>\n      <param name=\"delay_between_messages\" value=\"$(arg delay_between_messages)\"/>\n      <param name=\"max_message_size\" value=\"$(arg max_message_size)\"/>\n    </node>\n  </group>\n\n  <node name=\"rosapi\" pkg=\"rosapi\" type=\"rosapi_node\" />\n",
        "x": 220,
        "y": 160,
        "wires": [
            [
                "6c6e4866.b1c548"
            ]
        ]
    },
    {
        "id": "3e55c524.39868a",
        "type": "inject",
        "z": "cc9a367a.dd0918",
        "name": "Start",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 110,
        "y": 280,
        "wires": [
            [
                "403438bd.4561e8"
            ]
        ]
    },
    {
        "id": "4062327e.354b8c",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "content",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 920,
        "y": 200,
        "wires": []
    },
    {
        "id": "6e3567b.42d3098",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "filename",
        "active": true,
        "console": "false",
        "complete": "filename",
        "x": 920,
        "y": 140,
        "wires": []
    },
    {
        "id": "e397f13b.7f1ea",
        "type": "function",
        "z": "cc9a367a.dd0918",
        "name": "Convert Filename",
        "func": "msg.filename = \"/tmp/\" + parseInt( new Date().getTime()) + \".launch\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 450,
        "y": 280,
        "wires": [
            [
                "29bdc1.1329624",
                "6e3567b.42d3098",
                "4062327e.354b8c",
                "343cb1cc.66fa6e"
            ]
        ]
    },
    {
        "id": "29bdc1.1329624",
        "type": "file",
        "z": "cc9a367a.dd0918",
        "name": "write launchfile",
        "filename": "",
        "appendNewline": true,
        "createDir": false,
        "overwriteFile": "false",
        "x": 720,
        "y": 280,
        "wires": []
    },
    {
        "id": "343cb1cc.66fa6e",
        "type": "delay",
        "z": "cc9a367a.dd0918",
        "name": "Wait 1s",
        "pauseType": "delay",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 240,
        "y": 400,
        "wires": [
            [
                "569f5f6e.3d6b4"
            ]
        ]
    },
    {
        "id": "3f9b0113.e9a65e",
        "type": "exec",
        "z": "cc9a367a.dd0918",
        "command": "roslaunch ",
        "addpay": true,
        "append": "",
        "useSpawn": "",
        "timer": "",
        "name": "",
        "x": 560,
        "y": 400,
        "wires": [
            [
                "81ca8c0d.05b86"
            ],
            [
                "380f8a22.6be1e6"
            ],
            [
                "c640c322.33cef"
            ]
        ]
    },
    {
        "id": "81ca8c0d.05b86",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "stdout",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 910,
        "y": 340,
        "wires": []
    },
    {
        "id": "380f8a22.6be1e6",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "stderr",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 910,
        "y": 380,
        "wires": []
    },
    {
        "id": "c640c322.33cef",
        "type": "debug",
        "z": "cc9a367a.dd0918",
        "name": "return code",
        "active": true,
        "console": "false",
        "complete": "payload",
        "x": 930,
        "y": 420,
        "wires": []
    },
    {
        "id": "569f5f6e.3d6b4",
        "type": "change",
        "z": "cc9a367a.dd0918",
        "name": "Set Launchfile",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filename",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 400,
        "wires": [
            [
                "3f9b0113.e9a65e"
            ]
        ]
    },
    {
        "id": "403438bd.4561e8",
        "type": "template",
        "z": "cc9a367a.dd0918",
        "name": "<launch>",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<launch>",
        "x": 160,
        "y": 120,
        "wires": [
            [
                "b73657b6.347178"
            ]
        ]
    },
    {
        "id": "6dcca7f7.5305c8",
        "type": "template",
        "z": "cc9a367a.dd0918",
        "name": "</launch>",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{&payload}}</launch>",
        "x": 320,
        "y": 240,
        "wires": [
            [
                "e397f13b.7f1ea"
            ]
        ]
    },
    {
        "id": "6c6e4866.b1c548",
        "type": "template",
        "z": "cc9a367a.dd0918",
        "name": "3Dsensor.launch",
        "field": "payload",
        "fieldType": "msg",
        "format": "html",
        "syntax": "mustache",
        "template": "{{&payload}}  \n  <!-- 3D sensor -->\n  <include file=\"$(find turtlebot_bringup)/launch/3dsensor.launch\">\n    <arg name=\"rgb_processing\" value=\"true\" />\n    <arg name=\"depth_registration\" value=\"false\" />\n    <arg name=\"depth_processing\" value=\"false\" />\n    \n    <!-- We must specify an absolute topic name because if not it will be prefixed by \"$(arg camera)\".\n         Probably is a bug in the nodelet manager: https://github.com/ros/nodelet_core/issues/7 --> \n    <arg name=\"scan_topic\" value=\"/scan\" />\n  </include>",
        "x": 290,
        "y": 200,
        "wires": [
            [
                "6dcca7f7.5305c8"
            ]
        ]
    }
]

That’s it! Happy hacking 😉

6 Comments

  1. Hello, I am a beginner in this world of Robotics and I am also starting to use the node-red tool. I would like to ask you what your experience has been after this time. Do you think it is a good option or can you recommend any other to develop the graphical interface more easily?
    Thank you !!!
    Julio

    • Hi Julio,
      to be honest, node-red is just one tool to orchestrate robotic software. The ROS universe offers a lot of stuff, but not everything, e.g., monitoring and basic hardware configuration is often just assumed to work properly. I personally prefer node-red over the Python-based ROS GUIs. I would invite you to try node-red. However, if you are new to ROS, it could be helpful to start at the command line using ROS commands. Programming ROS nodes is not supported by node-red. Node-red offers some glue to configure things more efficiently.
      Happy coding
      Marvin

  2. Hi,
    Some great work Here 🙂
    I’m having trouble with running a simple launch file from Node Red. The file works from a terminal but I get ‘roslaunch not found’ when I try to run the command with an exec node. Any help would be great.

    • Hi,
      the exec node needs to access the roslaunch command. You can achieve this when you source the ROS setup.bash in a terminal BEFORE you run node-red from the same terminal. Please refer to the ROS installation guide. A simple solution is to add the “source /opt/ros/ROS_VERSION/setup.bash” to your .bashrc. This applies to ROS Indigo and Kinetic. In ROS Melodic roslaunch should be in the PATH anyway. Hope it helps.
      Marvin

      • Hi,
        Thanks for the quick reply. I have tried both your suggested solutions but with no improvement. I still get ‘/bin/sh: 1: roslaunch: not found’ in node-red when trying to run my roslaunch command form node red.

        • Hi again,
          Seems like you are using sh (/bin/sh) instead of bash (/bin/bash). Hope this did not happen by accident 😉 Do you run your scripts as root? But, there is a solution for sh, too. You need to “source” in a different way. Just type “. /opt/ros/ROS_VERSION/setup.sh” before you run roslaunch and before you start node-red. To do this automatically at login, you can add this line to the .shrc file in your HOME (.shrc is not available for root). Hope it works.
          Marvin

Leave a Reply

Your email address will not be published.


*