How to set the data field in PointCloud2
# -*- coding: utf-8 -*-
import struct
from time import time_ns
import numpy as np
from rosbags.rosbag1 import Writer
from rosbags.serde import cdr_to_ros1, serialize_cdr
from rosbags.typesys.types import sensor_msgs__msg__PointCloud2 as Pcl2
from rosbags.typesys.types import sensor_msgs__msg__PointField as Field
from rosbags.typesys.types import std_msgs__msg__Header as Hdr
from rosbags.typesys.types import builtin_interfaces__msg__Time as Ts
# create writer instance and open for writing
with Writer('rosbag_2022_06_28.bag') as writer:
# add new connection
topic = '/radar4d'
type_pcl2 = Pcl2.__msgtype__
connection = writer.add_connection(topic, type_pcl2, latching=True)
points = np.array([[225.0, -71.0, 819.8], [237.0, -24.0, 816.0]], np.float32)
points.tobytes()
uint8_points = np.array(
[
[struct.pack('<f', 225.0), struct.pack('<f', -71.0), struct.pack('<f', 819.8)],
[struct.pack('<f', 237.0), struct.pack('<f', -24.0), struct.pack('<f', 816.0)],
], np.uint8
)
uint8_points.astype(np.uint8)
# serialize and write message
msg = Pcl2(
header=Hdr(stamp=Ts(sec=1656293939, nanosec=823496000), frame_id='radar4d'),
height=1,
width=len(points),
fields=[
Field(name='x', offset=0, datatype=7, count=1),
Field(name='y', offset=4, datatype=7, count=1),
Field(name='z', offset=8, datatype=7, count=1)],
is_bigendian=False,
point_step=12,
row_step=len(points) * 12,
data=uint8_points,
is_dense=False
)
timestamp = time_ns()
writer.write(connection, timestamp, cdr_to_ros1(serialize_cdr(msg, type_pcl2), type_pcl2))
Just like the code above, how to set the msg.data?