Main Content

ros2device

Connect to remote ROS 2 device

Since R2021a

Description

The ros2device object creates a connection with a ROS 2 device. The ROS 2 device can be the local device or a remote device. The object contains the necessary login information and other parameters of the ROS 2 distribution. Once you have made a connection using ros2device, you can run and stop ROS 2 nodes.

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

You can also deploy ROS 2 nodes generated from MATLAB® code.

.

Note

To connect to a ROS 2 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 = ros2device(deviceAddress,username,password) creates a ros2device object connected to the ROS 2 device at the specified address and with the specified user name and password.

example

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

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

Properties

expand all

This property is read-only.

Host name or IP address of ROS 2 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 ROS 2 device, specified as a character vector.

Example: 'user'

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

Example: '/opt/ros/foxy'

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

Example: '~/ros2_ws_test'

This property is read-only.

Nodes available to run on a ROS 2 device, returned as a cell array of character vectors. Nodes are only listed if they are part of the ROS2Workspace 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
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 device and start a ROS 2 node using a ros2device object. Create a ros2device object by specifying the address, user name, and password of the remote device.

ipaddress = '192.168.203.131';
device = ros2device(ipaddress,'user','password');
device.ROS2Folder = '/opt/ros/foxy';
device.ROS2Workspace = '~/ros2_ws_test';

The ros2device object also contains information about the available ROS nodes. Check the available ROS 2 nodes on the connected device.

device.AvailableNodes

To execute the node on 'rmw_cyclonedds_cpp' ROS middleware set the RMW_IMPLEMENTATION environment variable using setenv.

setenv("RMW_IMPLEMENTATION","rmw_cyclonedds_cpp")

Use the runNode object function to run a ROS 2 node on the remote device, and then check that if the node is running.

runNode(device,'ros2FeedbackControlExample')
isNodeRunning(device,'ros2FeedbackControlExample')

Stop the ROS 2 node.

stopNode(device,'ros2FeedbackControlExample')

Limitations

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

Version History

Introduced in R2021a