Using ROS with MATLAB

You can use Robot Operating System (ROS) within MATLAB to acquire sensor data from your target.

Note: There's no need to install the ROS desktop software when you're using ROS within MATLAB. The functionality that you require of the ROS desktop software is already provided by MATLAB.

For more information on getting started with ROS within MATLAB, see the following sites provided by MathWorks:

Generally, the steps to set up one node as a master node, and a second node on a target (e.g., from your reference image) so that you can see the data that's passed from a target to host over ROS are:

  1. Set up your host (master node).

    The host machine refers to where MATLAB resides.

  2. Set up the target.
  3. Share data between the target and host (master node)

Set up the host (master node)

From your host machine, set up your master node:
  1. Run the rosinit command that's included with your MATLAB installation:
    $ rosinit
                        

    See rosinit from MATLAB for more information on connecting to a ROS network with MATLAB.

  2. After ROS is running, run these commands on your host:
    • rosnode to see the master node. For example:
      $rosnode list
      /matlab_global_node_11170
                               
    • rostopic to see the topics (or data) For example:
      $ rostopic list
      /rosout
                                  

      If you haven't yet started the ROS sensor publisher node on your target, you won't see any topics that you can subscribe to at the moment.

Set up the target

  1. On your target image, source the setup scripts to set up the necessary environment variables to use ROS.
    # . /opt/ros/kinetic/setup.sh
    # . /opt/ros/sensor_publisher/setup.sh --extend
                        
  2. Set the ROS_MASTER_URI variable to the URL, including port 11311 to your host machine where your master node resides. For example:
    # export ROS_MASTER_URI=http://10.123.123.10:11311
                        

    The URL should indicate where your roscore process is running.

  3. Set the publish_cameras parameter of the ROS sensor publisher node.

    In the configuration file on your target of the ROS sensor publisher node (/opt/ros/sensor_publisher/share/sensor_publisher/config.yaml), set the publish_cameras parameter to true. Do so such that the ROS sensor publisher node can publish camera raw video data.

    ROS supports the transfer of raw video data using messages. However, the support is limited to a single YUV format, and the high bandwidth associated to transferring raw video data can result in performance degradation.

Share data between the target and host machine (master node)

It's important to note that if you are sharing data from a camera, only the CbYCrY video format (SENSOR_FORMAT_VIDEO_CYBRY) can be published to ROS.

For sensors (lidar, radar, GPS, IMU), the following formats are supported:

  1. On the target (with the Sensor service already running), start the ROS sensor publisher node. For example:
    # roslaunch /opt/ros/sensor_publisher/share/sensor_publisher/launch/sensor_publisher_standalone_nodelet.launch
                        

    If you have built a customized sensor publisher node, you can use the appropriate launch file that's associated with your custom application.

  2. On the host machine where your master node resides, run the rosnode and rostopic commands. You should now see data appear from the target. The list of topics that you'll see may vary according to your sensor configuration and sensors that are operating on your target. For example, you may see the following list of topics if the Sensor service was started with the adas_example_capture.conf configuration file:
    $ rosnode list
    /QNX/sensor_publisher
    /rosout
    
    $ rostopic list
    /QNX/sensor_publisher/esr2_5
    /QNX/sensor_publisher/novatel_oem6_gps
    /QNX/sensor_publisher/novatel_oem6_imu
    /QNX/sensor_publisher/vlp_16
    /QNX/sensor_publisher/vu8
    /QNX/sensor_publisher/xsens_mti_g_710_gps
    /QNX/sensor_publisher/xsens_mti_g_710_imu
    /rosout
    /rosout_agg
                        
  3. On the host machine, subscribe to messages on a topic by calling rossubscriber. For example:
    $ sub = rossubscriber('/QNX/sensor_publisher/vlp_16')
                        

    This command creates a subscriber object to the topic that's specified. The name of the topic that you specify must be in the list of topics that was the output of the rostopic list command.

    See rossubscriber from MATLAB for more information on subscribing to messages on a topic.

  4. On the host machine, wait for new ROS topic messages by calling receive. For example:
    $ img_data = receive(sub)
                        

    This command returns the data in the format of the topic. For example, the topic may be an image. In this case you'll receive an Image message.

    Usually, an application that processes sensor data calls the receive in a loop to ensure that it's continuously handling new data from a device.

    See receive from MATLAB for more information on waiting for new ROS topic messages.