This example shows how to write data points in x, y, and z coordinates to a ROS or ROS 2 PointCloud2 message structure.
Create a random m-by-n-by-3 matrix with x, y, and z coordinate points.
Create a sensor_msgs/PointCloud2 message in ROS network.
rosMsg = struct with fields:
MessageType: 'sensor_msgs/PointCloud2'
Header: [1×1 struct]
Height: 0
Width: 0
Fields: [0×1 struct]
IsBigendian: 0
PointStep: 0
RowStep: 0
Data: [0×1 uint8]
IsDense: 0
Write the x, y, and z coordinate points to the ROS message. As x, y, and z are of data type uint8, the PointStep is 3 bytes.
rosMsg = struct with fields:
MessageType: 'sensor_msgs/PointCloud2'
Header: [1×1 struct]
Height: 128
Width: 128
Fields: [3×1 struct]
IsBigendian: 0
PointStep: 3
RowStep: 384
Data: [49152×1 uint8]
IsDense: 1
Now create a sensor_msgs/PointCloud2 message in ROS network to set the PointStep to the desired value.
Set the PointStep in sensor_msgs/PointCloud2 message to 32 to store remaining bytes with RGB or intensity data or both.
rosMsg = struct with fields:
MessageType: 'sensor_msgs/PointCloud2'
Header: [1×1 struct]
Height: 128
Width: 128
Fields: [3×1 struct]
IsBigendian: 0
PointStep: 32
RowStep: 4096
Data: [524288×1 uint8]
IsDense: 1
You can also create a sensor_msgs/PointCloud2 message in ROS 2 network.
ros2Msg = struct with fields:
MessageType: 'sensor_msgs/PointCloud2'
header: [1×1 struct]
height: 0
width: 0
fields: [1×1 struct]
is_bigendian: 0
point_step: 0
row_step: 0
data: 0
is_dense: 0
Write the x, y, and z coordinate points to the ROS 2 message. Set PointStep to 32.
ros2Msg = struct with fields:
MessageType: 'sensor_msgs/PointCloud2'
header: [1×1 struct]
height: 128
width: 128
fields: [3×1 struct]
is_bigendian: 0
point_step: 32
row_step: 4096
data: [524288×1 uint8]
is_dense: 1