def test_lidar_packet(info: client.SensorInfo) -> None: pf = _client.PacketFormat.from_info(info) """Test reading and writing values from empty packets.""" p = client.LidarPacket(bytes(pf.lidar_packet_size), info) w = pf.columns_per_packet h = pf.pixels_per_column assert len( client.ChanField.__members__) == 4, "Don't forget to update tests!" assert np.array_equal(p.field(client.ChanField.RANGE), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.REFLECTIVITY), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.SIGNAL), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.NEAR_IR), np.zeros((h, w))) assert len( client.ColHeader.__members__) == 5, "Don't forget to update tests!" assert np.array_equal(p.header(client.ColHeader.TIMESTAMP), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.FRAME_ID), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.MEASUREMENT_ID), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.ENCODER_COUNT), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.STATUS), np.zeros(w)) # should not be able to modify a packet built from a read-only buffer with pytest.raises(ValueError): p.field(client.ChanField.SIGNAL)[0] = 1 with pytest.raises(ValueError): p.header(client.ColHeader.MEASUREMENT_ID)[0] = 1 # a writeable lidar packet q = client.LidarPacket(bytearray(pf.lidar_packet_size), info) q.field(client.ChanField.SIGNAL)[:] = np.ones((h, w)) assert np.array_equal(q.field(client.ChanField.SIGNAL), np.ones((h, w))) # TODO: masking prevents writing RANGE. Need separate get/set for fields # q.view(client.ChanField.RANGE)[:] = np.ones((h, w)) # assert np.array_equal(q.view(client.ChanField.RANGE), np.ones((h, w))) with pytest.raises(ValueError): q.field(client.ChanField.SIGNAL)[:] = np.ones((w, h)) with pytest.raises(ValueError): q.field(client.ChanField.SIGNAL)[:] = np.ones((h - 1, w)) with pytest.raises(ValueError): q.field(client.ChanField.SIGNAL)[:] = np.ones((h, w + 1)) q.header(client.ColHeader.MEASUREMENT_ID)[:] = np.ones(w) assert np.array_equal(q.header(client.ColHeader.MEASUREMENT_ID), np.ones(w))
def test_make_packets(meta: client.SensorInfo) -> None: pf = _client.PacketFormat.from_info(meta) client.ImuPacket(bytes(pf.imu_packet_size), meta) client.ImuPacket(bytearray(pf.imu_packet_size), meta) with pytest.raises(ValueError): client.ImuPacket(bytes(), meta) with pytest.raises(ValueError): client.ImuPacket(bytes(pf.imu_packet_size - 1), meta) client.LidarPacket(bytes(pf.lidar_packet_size), meta) client.LidarPacket(bytearray(pf.lidar_packet_size), meta) with pytest.raises(ValueError): client.LidarPacket(bytes(), meta) with pytest.raises(ValueError): client.LidarPacket(bytes(pf.lidar_packet_size - 1), meta)
def random_lidar_packets(metadata, random_time=NO_RANDOM_TIME ) -> Iterator[client.LidarPacket]: global current_timestamp pf = _client.PacketFormat.from_info(metadata) timestamp = None current_timestamp += random() * 4.0 if random_time == RANDOM_FLOAT: timestamp = current_timestamp while True: buf = bytearray(getrandbits(8) for _ in range(pf.lidar_packet_size)) yield client.LidarPacket(buf, metadata, timestamp)
def test_lidar_packet(meta: client.SensorInfo) -> None: """Test reading and writing values from empty packets.""" pf = _client.PacketFormat.from_info(meta) p = client.LidarPacket(bytes(pf.lidar_packet_size), meta) w = pf.columns_per_packet h = pf.pixels_per_column assert len( client.ChanField.__members__) == 13, "Don't forget to update tests!" assert np.array_equal(p.field(client.ChanField.RANGE), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.REFLECTIVITY), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.SIGNAL), np.zeros((h, w))) assert np.array_equal(p.field(client.ChanField.NEAR_IR), np.zeros((h, w))) assert len( client.ColHeader.__members__) == 5, "Don't forget to update tests!" assert np.array_equal(p.header(client.ColHeader.TIMESTAMP), np.zeros(w)) assert np.array_equal(p.timestamp, np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.FRAME_ID), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.MEASUREMENT_ID), np.zeros(w)) assert np.array_equal(p.measurement_id, np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.ENCODER_COUNT), np.zeros(w)) assert np.array_equal(p.header(client.ColHeader.STATUS), np.zeros(w)) assert np.array_equal(p.status, np.zeros(w)) assert p.frame_id == 0 # should not be able to modify packet data with pytest.raises(ValueError): p.field(client.ChanField.SIGNAL)[0] = 1 with pytest.raises(ValueError): p.header(client.ColHeader.MEASUREMENT_ID)[0] = 1 with pytest.raises(ValueError): p.status[:] = 1 with pytest.raises(AttributeError): p.frame_id = 1 # type: ignore
def fake_packets(metadata: client.SensorInfo, n_lidar: int = 0, n_imu: int = 0, timestamped: bool = False) -> Iterator[client.Packet]: pf = _client.PacketFormat.from_info(metadata) current_ts = time.time() choices = [True] * n_lidar + [False] * n_imu shuffle(choices) for is_lidar in choices: current_ts += random() packet_ts = current_ts if timestamped else None if is_lidar: buf = bytearray( getrandbits(8) for _ in range(pf.lidar_packet_size)) yield client.LidarPacket(buf, metadata, packet_ts) else: buf = bytearray(getrandbits(8) for _ in range(pf.imu_packet_size)) yield client.ImuPacket(buf, metadata, packet_ts)