Main Content

Configure MATLAB Coder for ROS Node Generation

To generate C++ code for ROS Nodes from MATLAB functions, you must configure a MATLAB® Coder™ configuration object. This topic shows you how to configure the properties of the object to customize ROS Node generation.

To create a MATLAB Coder configuration object, use the coder.config (MATLAB Coder) object. ROS Toolbox only supports the executable build type for the MATLAB Coder configuration object.

cfg = coder.config('exe');

Specify the Hardware property of the object as a 'Robot Operating System (ROS)' hardware configuration object, using the coder.hardware (MATLAB Coder) function.

cfg.Hardware = coder.hardware('Robot Operating System (ROS)');

Then, you can specify these coder.Hardware properties specific to ROS node generation. For remote device deployment, the device parameters automatically save to MATLAB preferences, and are used the next time you set the deployment site to 'Remote Device'.


'Localhost' (default)

'Remote Device'

Deployment site for the code, which can be a remote device or the local host device.

'None' (default)

'Build and Load'

'Build and Run'

Build action for code generation.

'' (default)

character vector

Remote IP address or host name of the remote device.

'user' (default)

character vector

User name for the remote device.

'password' (default)

character vector

Password for the remote device.

'~/catkin_ws' (default)

character vector

Path to the Catkin workspace in the remote device. For Windows devices, the default value replaces '~' with the user path.

'/opt/ros/noetic' (default)

character vector

Path to the ROS installation folder. Leave it blank to use the MATLAB ROS distribution.

'ROS User' (default)

character vector

ROS package maintainer name, which is used for package.xml generation.

'' (default)

character vector

ROS package maintainer e-mail ID, used for package.xml generation.

'BSD' (default)

character vector

ROS license information, used for package.xml generation.

'1.0.0' (default)

character vector

Version number of the ROS package.

For example, the following code specifies that the generated code must be deployed to the remote device and run after build. It also specifies the remote device parameters.

cfg.Hardware.DeployTo = 'Remote Device';
cfg.Hardware.BuildAction = 'Build and run';
cfg.Hardware.RemoteDeviceAddress = '';
cfg.Hardware.RemoteDeviceUsername = 'user';
cfg.Hardware.RemoteDevicePassword = 'password';

To specify additional hardware-specific configuration parameters use coder.HardwareImplementation (MATLAB Coder) object. For example, this code specifies the manufacturer and type of the hardware as well as long long data type support for int64 and uint64 values.

cfg.HardwareImplementation.ProdHWDeviceType = 'Intel->x86-64 (Linux 64)';
cfg.HardwareImplementation.ProdLongLongMode = true;

To specify external ROS packages as dependencies for the generated ROS node, specify appropriate custom toolchain options in the coder configuration object.

cfg.BuildConfiguration = 'Specify';
cfg.CustomToolchainOptions{4} = '"ROS_PKG1","ROS_PKG2"'; % Add ROS Required Packages
cfg.CustomToolchainOptions{6} = '/usr/include/opencv';   % Add Include Directories
cfg.CustomToolchainOptions{8} = '"-lopencv_core","-lopencv_shape"'; % Add Link Libraries
cfg.CustomToolchainOptions{10} = '/usr/lib/opencv';    % Add Library Paths
cfg.CustomToolchainOptions{12} = '-DMYVAR=1';  % Add Defines

For more information on ROS Node generation, see MATLAB Programming for Code Generation.

Related Topics