엔지니어 동행하기

Python, LiDAR pcap데이터 Parsing 코드 구현 본문

Perception Engineering (LiDAR)/LiDAR

Python, LiDAR pcap데이터 Parsing 코드 구현

엔지니어 설리번 2022. 7. 25. 21:02
반응형
LiDAR에서 Point Cloud 데이터, 즉 (x, y, z, intesntiy, timestamp) 배열 형태의 데이터가 바로 얻어지는 것이 아닙니다. LiDAR는 데이터에 대한 추가 정보를 담고 있는 묶음 (Data Packet) 형태로 출력을 합니다. 때문에 PC는 이 데이터를 Parsing 해서 Point Cloud 데이터를 얻어야 합니다. 

(Python) .pcap data Parsing Code

Wireshark를 통해 LiDAR에서 PC로 입력되는 데이터를 확인할 수 있고, 이를 .pcap 확장자로 저장할 수 있습니다. 일회적으로 데이터를 분석하는 구체적인 방법은 아래 포스팅을 읽어 보시면 됩니다.

2022.07.24 - [Perception Engineering/LiDAR] - Robosense LiDAR의 Data Packet 분석: Wireshark 활용

 

Robosense LiDAR의 Data Packet 분석: Wireshark 활용

Wireshark를 이용하면 장비 간 주고받는 데이터를 확인할 수 있습니다. LiDAR를 PC에 연결하고 PC에서 LiDAR의 Data Packet을 파싱해 Point Cloud 데이터를 사용합니다. 이때 이를 계산하는 일련의 과정에서

engineer-sullivan.tistory.com

포스팅 내용을 바탕으로 .pcap 데이터 분석을 코드로 구현해 자동화할 수 있습니다. Python으로 구현할 경우 dpkt을 활용해 .pcap 파일을 파싱 할 수 있습니다. 

import dpkt

filename='/home/user/Desktop/robosense_data_packet.pcap'

for eth_timestamp, pkt in dpkt.pcap.Reader(open(filename,'rb')):

    #  print(type(pkt)) # <class 'bytes'>    

    # Unpack the Ethernet frame
    eth=dpkt.ethernet.Ethernet(pkt) 
    if eth.type!=dpkt.ethernet.ETH_TYPE_IP:
       continue
    
    # Now unpack the data within the Ethernet frame (the IP packet)
    ip=eth.data
    
   #  ip.data는 wireshark에서 확인한 Frame (1290bytes)
   #  print(ip.data.sport) #6699
   #  print(ip.data.dport) #6699
    
    print("eth_timestamp: ", eth_timestamp)
    count =1
    
    # ip.data.data는 wireshark에서 확인한 Data (1248bytes)
    for item in ip.data.data : 
      print(count, ": ",hex(item))
      count +=1

해당 코드는 dpkt Tutorial을 보고 직접 결과를 확인하면서 작성한 코드입니다. 다른 부분은 그대로 사용하시면 되고, ip.data.sport, ip.data.dport, ip.data.data로 데이터에 접근할 수 있다고 생각하면 됩니다. Data의 경우, 특정 bytes에 접근하기 위해서 ip.data.data[9:16] 방식으로 slicing을 활용하면 됩니다. 이런 방식으로 사용하고 있는 Protocol과 매칭해서 Parsing 코드를 구현할 수 있습니다.

 

(참고) (C++) Data Packet Parsing Code

데이터 패킷의 구조, bytes를 어떻게 사용하는지에 대한 규격(Communication Protocol)에 따라 Data Parsing Code를 작성할 수 있습니다. Robosense 공식 Github에서는 C++로 이를 구현해 제공하고 있습니다.

https://github.com/RoboSense-LiDAR/rs_driver/blob/main/src/rs_driver/driver/decoder/decoder_RS128.hpp

 

GitHub - RoboSense-LiDAR/rs_driver: RoboSense LiDAR cross-platform driver kernel for advanced development

RoboSense LiDAR cross-platform driver kernel for advanced development - GitHub - RoboSense-LiDAR/rs_driver: RoboSense LiDAR cross-platform driver kernel for advanced development

github.com


 

https://jon.oberheide.org/blog/2008/10/15/dpkt-tutorial-2-parsing-a-pcap-file/

 

dpkt Tutorial #2: Parsing a PCAP File | Jon Oberheide

dpkt Tutorial #2: Parsing a PCAP File Wednesday, October 15, 2008 As we showed in the first dpkt tutorial, dpkt makes it simple to construct packets. dpkt is equally useful for parsing packets and files, so in this second tutorial we will demonstrate parsi

jon.oberheide.org

 

반응형
Comments