-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_hokuyo_4m.py
72 lines (49 loc) · 1.28 KB
/
read_hokuyo_4m.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
# !/usr/bin/python
#
# Example code to go through the hokuyo_4m.bin file, read timestamps and the hits
# in each packet, and plot them.
#
# To call:
#
# python read_hokuyo_4m.py hokuyo_4m.bin
#
import sys
import struct
import numpy as np
import matplotlib.pyplot as plt
def convert(x_s):
scaling = 0.005 # 5 mm
offset = -100.0
x = x_s * scaling + offset
return x
def main(args):
if len(sys.argv) < 2:
print "Please specifiy input bin file"
return 1
# hokuyo_4 always has 726 hits
num_hits = 726
# angles for each range observation
rad0 = -2.0862138
radstep = 0.0061359233
angles = np.linspace(rad0, rad0 + (num_hits-1)*radstep, num_hits)
f_bin = open(sys.argv[1], "r")
plt.ion()
while True:
# Read timestamp
utime = struct.unpack('<Q', f_bin.read(8))[0]
print 'Timestamp', utime
r = np.zeros(num_hits)
for i in range(num_hits):
s = struct.unpack('<H', f_bin.read(2))[0]
r[i] = convert(s)
#print s
x = r * np.cos(angles)
y = r * np.sin(angles)
plt.clf()
plt.plot(x, y, '.')
plt.title(utime)
plt.draw()
f_bin.close()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))