Main Content

Update Header Field of a ROS 2 Message in Simulink

This example illustrates how to update the header field of a ROS 2 message using Simulink®.

Some ROS 2 messages contain a specific header field which maps to std_msgs/Header message type. The header field contains timestamp and coordinate frame information of the ROS 2 message. This example model shows how to use the ROS 2 Header Assignment block to update that information for a ROS 2 message, in Simulink.

open_system('ros2HeaderAssignmentExampleModel.slx')

The Blank Message block creates an empty ROS 2 message of type, sensor_msgs/LaserScan. Any other message type that contains a header field of std_msgs/Header can be used here, instead. The output of the Blank Message block is then fed to the ROS 2 Header Assignment block, which updates the header field of this message. For display, the frame_id and stamp values of the updated ROS 2 message header are selected from the list of bus elements uisng Bus Selector blocks.

Update Coordinate Frame ID and Timestamp Values in Header

Open the ROS 2 Header Assignment block to display its block parameters. The Set Frame ID option is selected and the name of the coordinate frame that is associated with the message is specified in the text box as lidar_link. This will be set as the frame_id value for the Header. The Set Timestamp option is also selected which sets the stamp value of the Header to the current ROS 2 System time, by default. The Header field name option is set to Use the default Header field name because, the name of the Header field in a blank message is its default value, header. If you are using a ROS 2 message with a custom name for the Header field, you can select the Specify your own option from the dropdown and specify the name of the Header field in the text box.

Run the model. Observe the updated values for the frame_id and stamp fields of ROS 2 message in their repective displays.

sim('ros2HeaderAssignmentExampleModel');