ouster-lidar / ouster-sdk

Ouster, Inc. sample code
Other
469 stars 438 forks source link

Read certain range of packets repeatedly from a pcap file #444

Closed dasol92 closed 2 years ago

dasol92 commented 2 years ago

Hello, experts

I faced an issue that read certain range of packets repeatedly from a pcap file. Tins::Packet should read the next packet sequentially, but after reading a few packets, it came back to the first frame of the pcap file, so it goes on over again and again certain range of packets.

I'm working with the examples/helpers function. from examples/helpers.cpp

while (ouster::sensor_utils::next_packet_info(*handle, packet_info)) {  // @dasol: Ready for the next packet 
        auto packet_size = ouster::sensor_utils::read_packet(           // @dasol: Read once a packet
            *handle, packet_buf.get(), BUF_SIZE);
        ...
}

How can i solve it? Thanks.

kairenw commented 2 years ago

Hi @dasol92,

That seems strange! Mind submitting your pcap through our customer support here?

We can take a look at it on our side

dasol92 commented 2 years ago

I attached pcap&json to the customer support. (SUPPORT-4725) Could you check the files? Thanks.

kairenw commented 2 years ago

Hi @dasol92,

So - a few things. First of all, the get_complete_scan function in helpers.cpp is not meant to be used as part of an API. I wrote that custom for the other examples and it was meant to specifically give the first complete frame back every time it was called on the pcap. That's why I call replay_reset at the beginning of the function here: https://github.com/ouster-lidar/ouster_example/blob/master/examples/helpers.cpp#L20

So for a simple solution to your observed issue, I'd comment line 20 out.

However, that's insufficient for your current pcap. You also need to change this check:

            packet_info.dst_port == info.udp_port_lidar) { ...

to something that's only checking the packet size, assuming that your .pcap contains data from only one sensor.

That would look like:
if (packet_size == pf.lidar_packet_size) {...

The reason you have to do this is because your metadata json does not specify ports. I think that's because you probably recorded this pcap with Ouster Studio. That's fine -- it's just that we expected the example to be written against something recoded with tools from this repo.

Anyways, these are just some slight suggestions and it may be that for your use case keeping the structure of the get_complete_scan function doesn't actually make sense. The way it's written right now, if you keep calling it, you'll only get every other frame because it's written with the specific aim of always returning the first complete frame from the .pcap instead of consecutive complete scans.

You can also use the Ouster Python SDK to record and visualize your data, by the way, if you ever need!

Let me know if this answers your question.

dasol92 commented 2 years ago

Hi @kairenw, That works! (BTW it's too fast) I'd comment line 20 not to check udp lidar port.

packet_info.dst_port == info.udp_port_lidar) { ...

As i mentioned above, by the way, the playback speed is too fast. I can't find parsing timestamp or sleep code (for sync with sensor real-time) in sdk C++ examples. However, Python SDK viz example displays at the recorded speed (recorded with Ouster Studio).

How do I play the recorded speed with the C++ codes?

Thanks a lot!

kairenw commented 2 years ago

Hi @dasol92,

If you want to play it back at recorded speed, you'll have to handle the timestamps yourself. You can use the timestamp in packet_info.timestamp and std::chrono to keep track of the current time. The logic that is used here in the Python iterator for pcap is basically what you want, but to duplicate it into C++.

Hope that helps!

kairenw commented 2 years ago

Closing this out. Thanks! Let us know if you need anything else.