def record_pcap(hostname: str, lidar_port: int = 7502, imu_port: int = 7503, n_seconds: int = 10) -> None: """Record data from live sensor to pcap file. Note that pcap files recorded this way only preserve the UDP data stream and not networking information, unlike capturing packets directly from a network interface with tools like tcpdump or wireshark. See the API docs of :py:func:`.pcap.record` for additional options for writing pcap files. Args: hostname: hostname of the sensor lidar_port: UDP port to listen on for lidar data imu_port: UDP port to listen on for imu data n_seconds: max seconds of time to record. (Ctrl-Z correctly closes streams) """ import ouster.pcap as pcap from datetime import datetime # [doc-stag-pcap-record] from more_itertools import time_limited # connect to sensor and record lidar/imu packets with closing(client.Sensor(hostname, lidar_port, imu_port, buf_size=640)) as source: # make a descriptive filename for metadata/pcap files time_part = datetime.now().strftime("%Y%m%d_%H%M%S") meta = source.metadata fname_base = f"{meta.prod_line}_{meta.sn}_{meta.mode}_{time_part}" print(f"Saving sensor metadata to: {fname_base}.json") source.write_metadata(f"{fname_base}.json") print(f"Writing to: {fname_base}.pcap (Ctrl-C to stop early)") source_it = time_limited(n_seconds, source) n_packets = pcap.record(source_it, f"{fname_base}.pcap") print(f"Captured {n_packets} packets")
def plot_imu_z_accel(hostname: str, lidar_port: int = 7502, imu_port: int = 7503, n_seconds: int = 5) -> None: """Plot the z acceleration from the IMU over time Args: hostname: hostname of the sensor imu_port: UDP port to listen on for imu data n_seconds: seconds of time to take a sample over """ import matplotlib.pyplot as plt # type: ignore # [doc-stag-imu-z-accel] from more_itertools import time_limited # connect to sensor and get imu packets within n_seconds source = client.Sensor(hostname, lidar_port, imu_port, buf_size=640) with closing(source): ts, z_accel = zip(*[(p.sys_ts, p.accel[2]) for p in time_limited(n_seconds, source) if isinstance(p, client.ImuPacket)]) # initialize plot fig, ax = plt.subplots(figsize=(12.0, 2)) ax.plot(ts, z_accel) # [doc-etag-imu-z-accel] plt.title("Z Accel from IMU over {} Seconds".format(n_seconds)) ax.set_xticks(np.arange(min(ts), max(ts), step=((max(ts) - min(ts)) / 5))) # add end ticker to x axis ax.set_xticks(list(ax.get_xticks()) + [max(ts)]) ax.set_xlim([min(ts), max(ts)]) ax.set_ylabel("z accel") ax.set_xlabel("timestamp (ns)") ax.ticklabel_format(useOffset=False, style="plain") plt.show()