forked from microsoft/AirSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_agent_car.py
125 lines (100 loc) · 4.19 KB
/
multi_agent_car.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
import setup_path
import airsim
import time
import os
import numpy as np
# Use below in settings.json with blocks environment
"""
{
"SettingsVersion": 1.2,
"SimMode": "Car",
"Vehicles": {
"Car1": {
"VehicleType": "PhysXCar",
"X": 4, "Y": 0, "Z": -2
},
"Car2": {
"VehicleType": "PhysXCar",
"X": -4, "Y": 0, "Z": -2
}
}
}
"""
# connect to the AirSim simulator
client = airsim.CarClient()
client.confirmConnection()
client.enableApiControl(True, "Car1")
client.enableApiControl(True, "Car2")
car_controls1 = airsim.CarControls()
car_controls2 = airsim.CarControls()
for idx in range(3):
# get state of the car
car_state1 = client.getCarState("Car1")
print("Car1: Speed %d, Gear %d" % (car_state1.speed, car_state1.gear))
car_state2 = client.getCarState("Car2")
print("Car1: Speed %d, Gear %d" % (car_state2.speed, car_state2.gear))
# go forward
car_controls1.throttle = 0.5
car_controls1.steering = 0.5
client.setCarControls(car_controls1, "Car1")
print("Car1: Go Forward")
car_controls2.throttle = 0.5
car_controls2.steering = -0.5
client.setCarControls(car_controls2, "Car2")
print("Car2: Go Forward")
time.sleep(3) # let car drive a bit
# go reverse
car_controls1.throttle = -0.5
car_controls1.is_manual_gear = True;
car_controls1.manual_gear = -1
car_controls1.steering = -0.5
client.setCarControls(car_controls1, "Car1")
print("Car1: Go reverse, steer right")
car_controls1.is_manual_gear = False; # change back gear to auto
car_controls1.manual_gear = 0
car_controls2.throttle = -0.5
car_controls2.is_manual_gear = True;
car_controls2.manual_gear = -1
car_controls2.steering = 0.5
client.setCarControls(car_controls2, "Car2")
print("Car2: Go reverse, steer right")
car_controls2.is_manual_gear = False; # change back gear to auto
car_controls2.manual_gear = 0
time.sleep(3) # let car drive a bit
# apply breaks
car_controls1.brake = 1
client.setCarControls(car_controls1, "Car1")
print("Car1: Apply break")
car_controls1.brake = 0 #remove break
car_controls2.brake = 1
client.setCarControls(car_controls2, "Car2")
print("Car2: Apply break")
car_controls2.brake = 0 #remove break
time.sleep(3) # let car drive a bit
# get camera images from the car
responses1 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car1") #scene vision image in uncompressed RGBA array
print('Car1: Retrieved images: %d' % (len(responses1)))
responses2 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.Segmentation), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car2") #scene vision image in uncompressed RGBA array
print('Car2: Retrieved images: %d' % (len(responses2)))
for response in responses1 + responses2:
filename = 'c:/temp/car_multi_py' + str(idx)
if response.pixels_as_float:
print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))
elif response.compress: #png format
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png
#restore to original state
client.reset()
client.enableApiControl(False)