Main Content

rosdevice

Connect to remote ROS device

Description

The rosdevice object is used to create a connection with a ROS device. The ROS device can be the local device or a remote device. The object contains the necessary login information and other parameters of the ROS distribution. Once a connection is made using rosdevice, you can run and stop a ROS core or ROS nodes and check the status of the ROS network. Before running ROS nodes, you must connect MATLAB® to the ROS network using the rosinit function.

You can deploy ROS nodes to a ROS device using Simulink® models. For an example, see Generate a Standalone ROS Node from Simulink.

You can also deploy ROS nodes generated from MATLAB code.

Note

To connect to a remote ROS device, an SSH server must be installed on the device. To connect to the local host, an SSH server installation on the local device is not required if you specify the deviceAddress as 'localhost'. Alternatively, if you specify the deviceAddress as '127.0.0.1' or as the host name referring to the local device, then an SSH server must be installed on the local device.

Creation

Description

device = rosdevice(deviceAddress,username,password) creates a rosdevice object connected to the ROS device at the specified address and with the specified user name and password.

example

device = rosdevice creates a rosdevice object connected to a ROS device using the saved values for deviceAddress, username, and password.

device = rosdevice('localhost') creates a rosdevice object connected to the local device.

Properties

expand all

This property is read-only.

Host name or IP address of the ROS device, specified as a character vector.

Example: '192.168.1.10'

Example: 'samplehost.foo.com'

This property is read-only.

User name used to connect to the ROS device, specified as a character vector.

Example: 'user'

Location of ROS installation, specified as a character vector. If a folder is not specified, MATLAB tries to determine the correct folder for you. When you deploy a ROS node, set this value from Simulink in the Configuration Parameters dialog box, under Hardware Implementation.

Example: '/opt/ros/hydro'

Catkin folder where models are deployed on device, specified as a character vector. When you deploy a ROS node, set this value from Simulink in the Configuration Parameters dialog box, under Hardware Implementation.

Example: '~/catkin_ws_test'

This property is read-only.

Nodes available to run on a ROS device, returned as a cell array of character vectors. Nodes are only listed if they are part of the CatkinWorkspace and have been deployed to the device using Simulink.

Example: {'robotcontroller','publishernode'}

Object Functions

runNodeStart ROS or ROS 2 node
stopNodeStop ROS or ROS 2 node
isNodeRunningDetermine if ROS or ROS 2 node is running
runCoreStart ROS core
stopCoreStop ROS core
isCoreRunningDetermine if ROS core is running
systemExecute system command on device
putFileCopy file to device
getFileGet file from device
deleteFileDelete file from device
dirList folder contents on device
openShellOpen interactive command shell to device

Examples

collapse all

Connect to a remote ROS device and start a ROS core. The ROS core is needed to run ROS nodes to communicate via a ROS network. You can run and stop a ROS core or node and check their status using a rosdevice object.

Create a connection to a ROS device. Specify the address, user name, and password of your specific ROS device. The device contains information about the ROS device, including the available ROS nodes that can be run using runNode.

ipaddress = '192.168.203.131';
d = rosdevice(ipaddress,'user','password')
d = 
  rosdevice with properties:

      DeviceAddress: '192.168.203.131'
           Username: 'user'
          ROSFolder: '/opt/ros/indigo'
    CatkinWorkspace: '~/catkin_ws'
     AvailableNodes: {'voxel_grid_filter_sl'}

Run a ROS core and check if it is running.

runCore(d)
Another roscore / ROS master is already running on the ROS device. Use the 'stopCore' function to stop it.
running = isCoreRunning(d)
running = logical
   1

Stop the ROS core and confirm that it is no longer running.

stopCore(d)
pause(2)
running = isCoreRunning(d)
running = logical
   0

Connect to a remote ROS device and start a ROS node. Run a ROS core so that ROS nodes can communicate via a ROS network. You can run and stop a ROS core or node and check their status using a rosdevice object.

Create a connection to a ROS device. Specify the address, user name, and password of your specific ROS device. The device already contains the available ROS nodes that can be run using runNode.

ipaddress = '192.168.203.129';
d = rosdevice(ipaddress,'user','password');
d.ROSFolder = '/opt/ros/indigo';
d.CatkinWorkspace = '~/catkin_ws_test'
d = 
  rosdevice with properties:

      DeviceAddress: '192.168.203.129'
           Username: 'user'
          ROSFolder: '/opt/ros/indigo'
    CatkinWorkspace: '~/catkin_ws_test'
     AvailableNodes: {'robotcontroller'  'robotcontroller2'}

Run a ROS core. Connect MATLAB® to the ROS master using rosinit. This core enables you to run ROS nodes on your ROS device.

runCore(d)
rosinit(d.DeviceAddress,11311)
Initializing global node /matlab_global_node_84497 with NodeURI http://192.168.203.1:56034/

Check the available ROS nodes on the connected ROS device. These nodes listed were generated from Simulink® models following the process in the Get Started with ROS in Simulink example.

d.AvailableNodes
ans = 1×2 cell
    {'robotcontroller'}    {'robotcontroller2'}

Run a ROS node and specify the node name. Check if the node is running.

runNode(d,'RobotController')
running = isNodeRunning(d,'RobotController')
running = logical
   1

Stop the ROS node. Disconnect from the ROS network. Stop the ROS core.

stopNode(d,'RobotController')
rosshutdown
Shutting down global node /matlab_global_node_84497 with NodeURI http://192.168.203.1:56034/
stopCore(d)

Run multiple ROS nodes on a connected ROS device. ROS nodes can be generated using Simulink® models to perform different tasks on the ROS network. These nodes are then deployed on a ROS device and can be run independently of Simulink®.

This example uses two different Simulink models that have been deployed as ROS nodes. See Generate a Standalone ROS Node from Simulink and follow the instructions to generate and deploy a ROS node. Do this twice and name them 'robotcontroller' and 'robotcontroller2'. The 'robotcontroller' node sends velocity commands to a robot to navigate it to a given point. The 'robotcontroller2' node uses the same model, but doubles the linear velocity to drive the robot faster.

Create a connection to a ROS device. Specify the address, user name, and password of your specific ROS device. The device contains information about the ROS device, including the available ROS nodes that can be run using runNode.

ipaddress = '192.168.203.129';
d = rosdevice(ipaddress,'user','password')
d = 
  rosdevice with properties:

      DeviceAddress: '192.168.203.129'
           Username: 'user'
          ROSFolder: '/opt/ros/indigo'
    CatkinWorkspace: '~/catkin_ws'
     AvailableNodes: {0×1 cell}

d.CatkinWorkspace = '~/catkin_ws_test'
d = 
  rosdevice with properties:

      DeviceAddress: '192.168.203.129'
           Username: 'user'
          ROSFolder: '/opt/ros/indigo'
    CatkinWorkspace: '~/catkin_ws_test'
     AvailableNodes: {'robotcontroller'  'robotcontroller2'}

Run a ROS core. The ROS Core is the master enables you to run ROS nodes on your ROS device. Connect MATLAB® to the ROS master using rosinit. For this example, the port is set to 11311. rosinit can automatically select a port for you without specifying this input.

runCore(d)
rosinit(d.DeviceAddress,11311)
Initializing global node /matlab_global_node_66434 with NodeURI http://192.168.203.1:59395/

Check the available ROS nodes on the connected ROS device. The nodes listed in this example were generated from Simulink® models following the process in the Generate a Standalone ROS Node from Simulink example. Two separate nodes are generated, 'robotcontroller' and 'robotcontroller2', which have the linear velocity set to 1 and 2 in the model respectively.

d.AvailableNodes
ans = 1×2 cell
    {'robotcontroller'}    {'robotcontroller2'}

Start up the Robot Simulator using ExampleHelperSimulinkRobotROS. This simulator automatically connects to the ROS master on the ROS device. You will use this simulator to run a ROS node and control the robot.

sim = ExampleHelperSimulinkRobotROS;

Run a ROS node, specifying the node name. The 'robotcontroller' node commands the robot to a specific location ([-10 10]). Wait to see the robot drive.

runNode(d,'robotcontroller')
pause(10)

Reset the Robot Simulator to reset the robot position. Alternatively, click Reset Simulation. Because the node is still running, the robot continues back to the specific location. To stop sending commands, stop the node.

resetSimulation(sim.Simulator)
pause(5)

stopNode(d,'robotcontroller')

Run the 'robotcontroller2' node. This model drives the robot with twice the linear velocity. Reset the robot position. Wait to see the robot drive. You should see a wider turn due to the increased velocity.

runNode(d,'robotcontroller2')
resetSimulation(sim.Simulator)
pause(10)

Close the simulator. Stop the ROS node. Disconnect from the ROS network and stop the ROS core.

close
stopNode(d,'robotcontroller2')
rosshutdown
Shutting down global node /matlab_global_node_66434 with NodeURI http://192.168.203.1:59395/
stopCore(d)

Limitations

  • You cannot change the ROSFolder property when connected to local host. For local host connections, it will always point to the ROS folder within MATLAB installation.

Version History

Introduced in R2019b