主要内容

ROS 2 Custom Message Support

Custom messages are user-defined messages that you can use to extend the set of message types currently supported in ROS 2. If you are sending and receiving supported message types, you do not need to use custom messages. To see a list of supported message types, callros2 msg listin the MATLAB® Command Window. For more information about supported ROS 2 messages, seeWork with Basic ROS 2 Messages.

If this if your first time working with ROS 2 custom messages, check theROS System Requirements.

Custom messages Contents

ROS 2 custom messages are specified in ROS 2 package folders that contain amsgdirectory. Themsgfolder contains all your custom message type definitions. For example, the packageexample_b_msgs.,内在customfolder, has the below folder and file structure.

The package contains one custom message type,独立.msg.. MATLAB uses these files to generate the necessary files for using the custom messages contained in the package. For more information on message naming conventions, seeROS 2接口定义.

在此示例中,您可以通过在Matlab®中创建ROS 2自定义消息的过程。您必须拥有包含所需的ROS 2包msg文件。

确保自定义消息包正确后,请注意文件夹路径位置,然后调用ros2genmsgwith the specified path. The following example provided three messagesexample_package_a,example_package_b, 和example_package_cthat have dependencies. This example also illustrates that you can use a folder containing multiple messages and generate them all at the same time.

To set up custom messages in MATLAB, open MATLAB in a new session. Place your custom message folder in a location and note the folder path. In this example, the custom message interface folder is present in the current directory. If you are creating custom message packages in a separate location, provide the appropriate path to the folder that contains the custom message packages.

folderPath = fullfile(pwd,"custom");copyfile("example_*_msgs",folderPath);

指定自定义消息文件和呼叫的文件夹路径ros2genmsgto create custom messages for MATLAB.

ros2genmsg(folderPath)
识别消息文件在文件夹/ U: /文档MATLAB/Examples/ros-ex44405863/custom'.Done. Validating message files in folder 'U:/Documents/MATLAB/Examples/ros-ex44405863/custom'.Done. [3/3] Generating MATLAB interfaces for custom message packages... Done. Running colcon build in folder 'U:/Documents/MATLAB/Examples/ros-ex44405863/custom/matlab_msg_gen/win64'. Build in progress. This may take several minutes... Build succeeded.build log

Callros2 msg listto verify creation of new custom messages.

You can now use the above created custom message as the standard messages. For more information on sending and receiving messages, seeExchange Data with ROS 2 Publishers and Subscribers.

Create a publisher to useexample_package_b/Standalonemessage.

node = ros2node(“/ node_1”);pub = ros2publisher(节点,"/example_topic","example_b_msgs/Standalone");

Create a subscriber on the same topic.

sub = ros2subscriber(node,"/example_topic");

Create a message and send the message.

custom_msg = ros2message("example_b_msgs/Standalone");custom_msg.int_property = uint32(12); custom_msg.string_property='This is ROS 2 custom message example'; send(pub,custom_msg); pause(3)%允许几秒钟才能到达

UseLatestMessagefield to know the recent message received by the subscriber.

sub.latestmessage.
ans =struct with fields:INT_PROPERTY:12 String_Property:'这是ROS 2自定义消息示例'

删除创建的ROS对象。

清除nodepubsub