-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathclient.py
More file actions
220 lines (178 loc) · 7.82 KB
/
client.py
File metadata and controls
220 lines (178 loc) · 7.82 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from __future__ import print_function
from .utils import *
from .types import *
import msgpackrpc # pip install msgpack-rpc-python
import numpy as np # pip install numpy
import msgpack
import time
import math
import logging
class FSDSClient:
def __init__(self, ip = "", port = 41451, timeout_value = 3):
if (ip == ""):
ip = "127.0.0.1"
self.client = msgpackrpc.Client(msgpackrpc.Address(ip, port), timeout = timeout_value, pack_encoding = 'utf-8', unpack_encoding = 'utf-8')
# ----------------------------------- Common vehicle APIs ---------------------------------------------
def reset(self):
"""
Reset the vehicle to its original starting state
Note that you must call `enableApiControl` again after the call to reset
"""
self.client.call('reset')
def restart(self):
"""
Reset the vehicle AND THE CONES to their original starting state
"""
try:
self.client.call('restart')
except Exception as e:
print(e)
time.sleep(0.1) # VERY IMPORTANT!
self.__init__(self.ip, self.port, self.timeout_value)
def ping(self):
"""
If connection is established then this call will return true otherwise it will be blocked until timeout
Returns:
bool:
"""
return self.client.call('ping')
def enableApiControl(self, is_enabled, vehicle_name = 'FSCar'):
"""
Enables or disables API control for vehicle corresponding to vehicle_name
Args:
is_enabled (bool): True to enable, False to disable API control
vehicle_name (str, optional): Name of the vehicle to send this command to
"""
self.client.call('enableApiControl', is_enabled, vehicle_name)
def isApiControlEnabled(self, vehicle_name = 'FSCar'):
"""
Returns true if API control is established.
If false (which is default) then API calls would be ignored. After a successful call to `enableApiControl`, `isApiControlEnabled` should return true.
Args:
vehicle_name (str, optional): Name of the vehicle
Returns:
bool: If API control is enabled
"""
return self.client.call('isApiControlEnabled', vehicle_name)
def confirmConnection(self):
"""
Checks state of connection every 1 sec and reports it in Console so user can see the progress for connection.
If ping fails the program is exited.
"""
if self.ping():
print("Ping to simulator OK")
else:
print("Ping to simulator FAIL")
exit(1)
# camera control
# simGetImage returns compressed png in array of bytes
# image_type uses one of the ImageType members
def simGetImage(self, camera_name, image_type, vehicle_name = 'FSCar'):
"""
Get a single image
Returns bytes of png format image which can be dumped into abinary file to create .png image
`string_to_uint8_array()` can be used to convert into Numpy unit8 array
See https://microsoft.github.io/AirSim/image_apis/ for details
Args:
camera_name (str): Name of the camera, for backwards compatibility, ID numbers such as 0,1,etc. can also be used
image_type (ImageType): Type of image required
vehicle_name (str, optional): Name of the vehicle with the camera
Returns:
Binary string literal of compressed png image
"""
# todo: in future remove below, it's only for compatibility to pre v1.2
camera_name = str(camera_name)
# because this method returns std::vector<uint8>, msgpack decides to encode it as a string unfortunately.
result = self.client.call('simGetImage', camera_name, image_type, vehicle_name)
if (result == "" or result == "\0"):
return None
return result
# camera control
# simGetImage returns compressed png in array of bytes
# image_type uses one of the ImageType members
def simGetImages(self, requests, vehicle_name = 'FSCar'):
"""
Get multiple images
See https://microsoft.github.io/AirSim/image_apis/ for details and examples
Args:
requests (list[ImageRequest]): Images required
vehicle_name (str, optional): Name of vehicle associated with the camera
Returns:
list[ImageResponse]:
"""
responses_raw = self.client.call('simGetImages', requests, vehicle_name)
return [ImageResponse.from_msgpack(response_raw) for response_raw in responses_raw]
def simGetGroundTruthKinematics(self, vehicle_name = 'FSCar'):
"""
Get Ground truth kinematics of the vehicle
Args:
vehicle_name (str, optional): Name of the vehicle
Returns:
KinematicsState: Ground truth of the vehicle
"""
kinematics_state = self.client.call('simGetGroundTruthKinematics', vehicle_name)
return KinematicsState.from_msgpack(kinematics_state)
simGetGroundTruthKinematics.__annotations__ = {'return': KinematicsState}
# sensor APIs
def getLidarData(self, lidar_name = '', vehicle_name = 'FSCar'):
"""
Args:
lidar_name (str, optional): Name of Lidar to get data from, specified in settings.json
vehicle_name (str, optional): Name of vehicle to which the sensor corresponds to
Returns:
LidarData:
"""
return LidarData.from_msgpack(self.client.call('getLidarData', lidar_name, vehicle_name))
def getImuData(self, imu_name = '', vehicle_name = 'FSCar'):
"""
Args:
imu_name (str, optional): Name of IMU to get data from, specified in settings.json. When no name is provided the last imu will be used.
vehicle_name (str, optional): Name of vehicle to which the sensor corresponds to
Returns:
ImuData:
"""
return ImuData.from_msgpack(self.client.call('getImuData', imu_name, vehicle_name))
def getGpsData(self, gps_name = '', vehicle_name = 'FSCar'):
"""
Args:
gps_name (str, optional): Name of GPS to get data from, specified in settings.json
vehicle_name (str, optional): Name of vehicle to which the sensor corresponds to
Returns:
GpsData:
"""
return GpsData.from_msgpack(self.client.call('getGpsData', gps_name, vehicle_name))
def getGroundSpeedSensorData(self, vehicle_name = 'FSCar'):
"""
Args:
vehicle_name (str, optional): Name of vehicle to which the sensor corresponds to. Default FSCar.
Returns:
GroundSpeedSensorData:
"""
return GroundSpeedSensorData.from_msgpack(self.client.call('getGroundSpeedSensorData', vehicle_name))
def setCarControls(self, controls, vehicle_name = 'FSCar'):
"""
Control the car using throttle, steering, brake, etc.
Args:
controls (CarControls): Struct containing control values
vehicle_name (str, optional): Name of vehicle to be controlled
"""
self.client.call('setCarControls', controls, vehicle_name)
def getCarState(self, vehicle_name = 'FSCar'):
"""
Args:
vehicle_name (str, optional): Name of vehicle
Returns:
CarState:
"""
state_raw = self.client.call('getCarState', vehicle_name)
return CarState.from_msgpack(state_raw)
def getRefereeState(self):
referee_state_raw = self.client.call('getRefereeState')
return RefereeState.from_msgpack(referee_state_raw)
def getSettingsString(self):
"""
Fetch the settings text being used by AirSim
Returns:
str: Settings text in JSON format
"""
return self.client.call('getSettingsString')