-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_vel_sync.py
86 lines (60 loc) · 1.75 KB
/
read_vel_sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# !/usr/bin/python
#
# Example code to read a velodyne_sync/[utime].bin file
# Plots the point cloud using matplotlib. Also converts
# to a CSV if desired.
#
# To call:
#
# python read_vel_sync.py velodyne.bin [out.csv]
#
import sys
import struct
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def convert(x_s, y_s, z_s):
scaling = 0.005 # 5 mm
offset = -100.0
x = x_s * scaling + offset
y = y_s * scaling + offset
z = z_s * scaling + offset
return x, y, z
def main(args):
if len(sys.argv) < 2:
print 'Please specify velodyne file'
return 1
f_bin = open(sys.argv[1], "r")
if len(sys.argv) >= 3:
print 'Writing to ', sys.argv[2]
f_csv = open(sys.argv[2], "w")
else:
f_csv = None
hits = []
while True:
x_str = f_bin.read(2)
if x_str == '': # eof
break
x = struct.unpack('<H', x_str)[0]
y = struct.unpack('<H', f_bin.read(2))[0]
z = struct.unpack('<H', f_bin.read(2))[0]
i = struct.unpack('B', f_bin.read(1))[0]
l = struct.unpack('B', f_bin.read(1))[0]
print ('meas raw: %1.2f, %1.2f, %1.2f'%(x,y,z))
x, y, z = convert(x, y, z)
print ('meas conv: %1.2f, %1.2f, %1.2f'%(x,y,z))
s = "%5.3f, %5.3f, %5.3f, %d, %d" % (x, y, z, i, l)
if f_csv:
f_csv.write('%s\n' % s)
hits += [[x, y, z]]
f_bin.close()
if f_csv:
f_csv.close()
hits = np.asarray(hits)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(hits[:, 0], hits[:, 1], -hits[:, 2], c=-hits[:, 2], s=5, linewidths=0)
plt.show()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))