Configure MATLAB Coder for ROS 2 Node Generation
To generate C++ code for ROS 2 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 2 Node generation.
By default the node uses the 'rmw_fastrtps_cpp'
ROS middleware (RMW)
implementation unless otherwise specified by the RMW_IMPLEMENTATION
environment variable. For more information on RMW implementations see Switching Between ROS Middleware Implementations.
setenv('RMW_IMPLEMENTATION','rmw_cyclonedds_cpp')
Note
When the
coder.Hardware.BuildAction
property is specified as'Build and Run'
, theRMW_IMPLEMENTATION
environment variable must be set before code generation.When the
coder.Hardware.BuildAction
property is specified as'None'
or'Build and Load'
, theRMW_IMPLEMENTATION
environment variable must be set after code generation.
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
cfg.Hardware = coder.hardware('Robot Operating System 2 (ROS 2)');
Specify the Hardware
property of the object as a 'Robot
Operating System 2 (ROS 2)'
hardware configuration object, using the
coder.hardware
(MATLAB Coder) function.
Then, you can specify these coder.Hardware
properties specific to
ROS 2 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'
.
Property | Values | Description |
---|---|---|
cfg.Hardware.DeployTo |
| Deployment site for the code, which can be a remote device or the local host device. |
cfg.Hardware.BuildAction |
| Build action for code generation. |
cfg.Hardware.RemoteDeviceAddress |
character vector | Remote IP address or host name of the remote device. |
cfg.Hardware.RemoteDeviceUsername |
character vector | User name for the remote device. |
cfg.Hardware.RemoteDevicePassword |
character vector | Password for the remote device. |
cfg.Hardware.ROS2Workspace |
character vector | Path to the ROS 2 workspace in the remote device. For Windows
devices, the default value replaces '~' with the
user path. |
cfg.Hardware.ROS2Folder |
character vector | Path to the ROS installation folder. Leave it blank to use the MATLAB ROS distribution. |
cfg.Hardware.PackageMaintainerName |
character vector | ROS package maintainer name, which is used for
package.xml generation. |
cfg.Hardware.PackageMaintainerEmail |
character vector | ROS package maintainer e-mail ID, used for
package.xml generation. |
cfg.Hardware.PackageLicense |
character vector | ROS license information, used for package.xml
generation. |
cfg.Hardware.PackageVersion |
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 and build behavior changes based on
GenCodeOnly
and other hardware properties.
cfg.GenCodeOnly | cfg.Hardware.DeployTo | cfg.Hardware.BuildAction | Code 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:
| |
'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:
| |
'Build and Load' | Generates these artifacts on local host device and builds the executable on remote device:
| ||
'Build and Run' | Generates these artifacts on local host device, builds and runs the executable 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';
Specify Custom Source and Include Files, Additional Hardware Properties and ROS 2 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} = '"ROS2_PKG1","ROS2_PKG2"'; % Add ROS 2 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