-
Notifications
You must be signed in to change notification settings - Fork 1
/
hokuyo_4m_to_rosbag.py
85 lines (60 loc) · 1.73 KB
/
hokuyo_4m_to_rosbag.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
# !/usr/bin/python
#
# Convert the hokuyo_4m binary files to a rosbag
#
# To call:
#
# python hokuyo_4m_to_rosbag.py hokuyo_4m.bin hokuyo_4m.bag
#
import rosbag, rospy
from std_msgs.msg import Float64MultiArray, MultiArrayDimension, MultiArrayLayout
import sys
import numpy as np
import struct
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 specify hokuyo_4m file'
return 1
if len(sys.argv) < 3:
print 'Please specify output rosbag file'
return 1
# hokuyo_4m always has 726 hits
num_hits = 726
f_bin = open(sys.argv[1], "r")
bag = rosbag.Bag(sys.argv[2], 'w')
try:
while True:
# check for eof
data = f_bin.read(8)
if data == '':
break
# Read timestamp
utime = struct.unpack('<Q', data)[0]
r = np.zeros(num_hits)
for i in range(num_hits):
s = struct.unpack('<H', f_bin.read(2))[0]
r[i] = convert(s)
# Now write out to rosbag
timestamp = rospy.Time.from_sec(utime/1e6)
layout = MultiArrayLayout()
layout.dim = [MultiArrayDimension()]
layout.dim[0].label = "r"
layout.dim[0].size = num_hits
layout.dim[0].stride = 1
hits = Float64MultiArray()
hits.data = r
hits.layout = layout
bag.write('hokuyo_4m_packet', hits, t=timestamp)
except Exception:
print 'End of File'
finally:
f_bin.close()
bag.close()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))