-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathcar_lidar.py
95 lines (71 loc) · 2.82 KB
/
car_lidar.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
# Python client example to get Lidar data from a car
#
import setup_path
import airsim
import sys
import math
import time
import argparse
import pprint
import numpy
# Makes the drone fly and get Lidar data
class LidarTest:
def __init__(self):
# connect to the AirSim simulator
self.client = airsim.CarClient()
self.client.confirmConnection()
self.client.enableApiControl(True)
self.car_controls = airsim.CarControls()
def execute(self):
for i in range(3):
state = self.client.getCarState()
s = pprint.pformat(state)
#print("state: %s" % s)
# go forward
self.car_controls.throttle = 0.5
self.car_controls.steering = 0
self.client.setCarControls(self.car_controls)
print("Go Forward")
time.sleep(3) # let car drive a bit
# Go forward + steer right
self.car_controls.throttle = 0.5
self.car_controls.steering = 1
self.client.setCarControls(self.car_controls)
print("Go Forward, steer right")
time.sleep(3) # let car drive a bit
airsim.wait_key('Press any key to get Lidar readings')
for i in range(1,3):
lidarData = self.client.getLidarData();
if (len(lidarData.point_cloud) < 3):
print("\tNo points received from Lidar data")
else:
points = self.parse_lidarData(lidarData)
print("\tReading %d: time_stamp: %d number_of_points: %d" % (i, lidarData.time_stamp, len(points)))
print("\t\tlidar position: %s" % (pprint.pformat(lidarData.pose.position)))
print("\t\tlidar orientation: %s" % (pprint.pformat(lidarData.pose.orientation)))
time.sleep(5)
def parse_lidarData(self, data):
# reshape array of floats to array of [X,Y,Z]
points = numpy.array(data.point_cloud, dtype=numpy.dtype('f4'))
points = numpy.reshape(points, (int(points.shape[0]/3), 3))
return points
def write_lidarData_to_disk(self, points):
# TODO
print("not yet implemented")
def stop(self):
airsim.wait_key('Press any key to reset to original state')
self.client.reset()
self.client.enableApiControl(False)
print("Done!\n")
# main
if __name__ == "__main__":
args = sys.argv
args.pop(0)
arg_parser = argparse.ArgumentParser("Lidar.py makes car move and gets Lidar data")
arg_parser.add_argument('-save-to-disk', type=bool, help="save Lidar data to disk", default=False)
args = arg_parser.parse_args(args)
lidarTest = LidarTest()
try:
lidarTest.execute()
finally:
lidarTest.stop()