forked from robotika/osgar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
velodyne.py
177 lines (154 loc) · 5.85 KB
/
velodyne.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
"""
Velodyne VLP-16 wrapper
usage:
./velodyne.py <task|thread> [<metalog> [<F>]]
"""
import sys
import socket
from datetime import datetime, timedelta
import struct
import time
import numpy as np
from apyros.metalog import MetaLog, disableAsserts
from threading import Thread,Event,Lock
HOST = "192.168.1.201"
PORT = 2368
# bug in datasheet "Table 6. Laser ID"
LASER_ANGLES = [-15, 1, -13, 3, -11, 5, -9, 7, -7, 9, -5, 11, -3, 13, -1, 15]
NUM_LASERS = 16
EXPECTED_PACKET_TIME = 0.001327 # valid only in "the strongest return mode"
EXPECTED_SCAN_DURATION = 0.1
def min_dist(data):
mask = (data > 0)
if np.any(mask):
return np.min(data[mask]) * 0.002
return None
def min_dist_index(data):
mask = (data > 0)
if np.any(mask):
# dirty hack for argmin counting only masked elements :(
ugly = data.copy()
inv_mask = ugly == 0
ugly[inv_mask] = 100000
return np.unravel_index(np.argmin(ugly), data.shape)[0], np.min(data[mask]) * 0.002
return None
class Velodyne:
def __init__(self, metalog=None):
if metalog is None:
metalog = MetaLog()
self.soc = metalog.createLoggedSocket("velodyne", headerFormat="<BBBI")
self.soc.bind( ('',PORT) )
self.metalog = metalog
self.buf = ""
self.time = None
self.last_blocked = None
self.dist = np.zeros( (360, NUM_LASERS), dtype=np.uint16)
self.scan_index = 0
self.prev_azimuth = None
self.safe_dist = None
self.dist_index = None
def parse(self, data):
assert len(data) == 1206, len(data)
timestamp, factory = struct.unpack_from("<IH", data, offset=1200)
assert factory == 0x2237, hex(factory) # 0x22=VLP-16, 0x37=Strongest Return
time = timestamp/1000000.0
if self.time is not None:
lost_packets = int(round((time - self.time)/EXPECTED_PACKET_TIME)) - 1
else:
lost_packets = 0
self.time = time
if lost_packets > 0 and (self.last_blocked is None or self.time > self.last_blocked + EXPECTED_SCAN_DURATION):
self.last_blocked = self.time + EXPECTED_SCAN_DURATION
self.scan_index += 1
print "DROPPED index", self.scan_index
if self.last_blocked is not None and self.time < self.last_blocked:
return # to catch up-to-date packets again ...
for offset in xrange(0, 1200, 200): # skip every second packet (we need 1deg resolution input 0.4)
flag, azi = struct.unpack_from("<HH", data, offset)
assert flag == 0xEEFF, hex(flag)
azimuth = azi/100.0
if self.prev_azimuth is not None and azimuth < self.prev_azimuth:
self.scan_index += 1
# rotated 180deg -> this is back now: self.safe_dist = min_dist(self.dist[160:200])
self.safe_dist = (min_dist(self.dist[335:340]), min_dist(self.dist[340:345]), min_dist(self.dist[345:360]),
min_dist(self.dist[0:15]), min_dist(self.dist[15:20]), min_dist(self.dist[20:25])) # (left, right)
left = min_dist_index(self.dist[-45:])
if left is not None:
left = (left[0]-45, left[1])
right = min_dist_index(self.dist[:45])
self.dist_index = left
if left is None or (right is not None and left[1] > right[1]):
self.dist_index = right
self.prev_azimuth = azimuth
# H-distance (2mm step), B-reflectivity (0
arr = struct.unpack_from('<' + "HB"*32, data, offset + 4)
for i in xrange(NUM_LASERS):
self.dist[int(azimuth)][i] = arr[i*2]
def update(self):
while True:
data = self.soc.recv(2000)
if len(data) > 0:
assert len(data) == 1206, len(data)
break
self.parse(data)
# TODO general wrapper for threaded sources
class VelodyneThread(Thread):
def __init__(self, sensor):
Thread.__init__(self)
self.setDaemon(True)
self.lock = Lock()
self.shouldIRun = Event()
self.shouldIRun.set()
self.sensor = sensor
self._result = None
self._last_index = None
def run(self):
while self.shouldIRun.isSet():
self.sensor.update()
if self._last_index != self.sensor.scan_index:
self.lock.acquire()
self._result = self.sensor.scan_index, self.sensor.safe_dist, self.sensor.dist_index
self._last_index = self.sensor.scan_index
self.lock.release()
def scan_safe_dist(self):
self.lock.acquire()
ret = self._result # copy?
self.lock.release()
return ret
def requestStop(self):
self.shouldIRun.clear()
if __name__ == "__main__":
if len(sys.argv) < 2:
print __doc__
sys.exit(2)
metalog=None
if 'meta_' in sys.argv[1]:
metalog = MetaLog(filename=sys.argv[1])
elif len(sys.argv) > 2:
metalog = MetaLog(filename=sys.argv[2])
if len(sys.argv) > 2 and sys.argv[-1] == 'F':
disableAsserts()
sensor = Velodyne(metalog=metalog)
if sys.argv[1] == 'thread':
thr = VelodyneThread(sensor)
start_time = datetime.now()
thr.start()
prev = None
while datetime.now() - start_time < timedelta(seconds=3.0):
curr = thr.scan_safe_dist()
if prev != curr:
print curr
prev = curr
thr.requestStop()
thr.join()
else:
prev = None
for i in xrange(10000):
sensor.update()
curr = sensor.scan_index, sensor.dist_index
if prev != curr:
if sensor.scan_index % 10 == 0:
print curr
prev = curr
# vim: expandtab sw=4 ts=4