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.

Create MATLAB Coder Configuration Object

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');

Configure Hardware Properties

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'.

PropertyValuesDescription
cfg.Hardware.DeployTo

'Localhost' (default)

'Remote Device'

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

'None' (default)

'Build and Load'

'Build and Run'

Build action for code generation.
cfg.Hardware.RemoteDeviceAddress

'192.168.128.130' (default)

character vector

Remote IP address or host name of the remote device.
cfg.Hardware.RemoteDeviceUsername

'user' (default)

character vector

User name for the remote device.
cfg.Hardware.RemoteDevicePassword

'password' (default)

character vector

Password for the remote device.
cfg.Hardware.CatkinWorkspace

'~/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.
cfg.Hardware.ROSFolder

'/opt/ros/noetic' (default)

character vector

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

'ROS User' (default)

character vector

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

'rosuser@test.com' (default)

character vector

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

'BSD' (default)

character vector

ROS license information, used for package.xml generation.
cfg.Hardware.PackageVersion

'1.0.0' (default)

character vector

Version number of the ROS package.

Configure Build Options

You can also specify whether you only need the code to be generated using the GenCodeOnly property of the coder configuration object. This table highlights how the code generation behavior changes based on GenCodeOnly and other hardware properties.

cfg.GenCodeOnlycfg.Hardware.DeployTocfg.Hardware.BuildActionCode Generation and Build Behavior
true'Localhost''None'Generates ROS Package source code folder src on local host device
'Build and Load'
'Build and Run'
'Remote Device''None'

Generates these artifacts on local host device:

  • ROS package source code folder src

  • TAR archive of the source code in a .tgz file

  • Shell script to extract and set up the package on remote device

'Build and Load'
'Build and Run'
false'Localhost''None'Generates ROS Package source code folder src on local host device
'Build and Load'Generates ROS Package source code folder src and builds the executable on local host device
'Build and Run'Generates ROS Package source code folder src, builds and runs the executable on local host device
'Remote Device''None'

Generates these artifacts on local host device:

  • ROS package source code folder src

  • TAR archive of the source code in a .tgz file

  • Shell script to extract and set up the package on remote device

'Build and Load'

Generates these artifacts on local host device and builds the executable on remote device:

  • ROS package source code folder src

  • TAR archive of the source code in a .tgz file

  • Shell script to extract and set up the package on remote device

'Build and Run'

Generates these artifacts on local host device, builds and runs the executable on remote device:

  • ROS package source code folder src

  • TAR archive of the source code in a .tgz file

  • Shell script to extract and set up the package on remote device

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 = '192.168.243.144';
cfg.Hardware.RemoteDeviceUsername = 'user';
cfg.Hardware.RemoteDevicePassword = 'password';

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

Specify Custom Source and Include Files, Additional Hardware Properties and ROS Dependencies

To specify custom source files to compile and link with the generated code, use the CustomSource (MATLAB Coder) property. To specify include folders to add to the include path when compiling the generated code, use the CustomInclude (MATLAB Coder) property.

cfg.CustomInclude = ["C:\MyProject","C:\ROS Files"];
cfg.CustomSource = ["adder.cpp","multiplier.cpp"];

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

Related Topics