-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTool.py
355 lines (308 loc) · 9.13 KB
/
Tool.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python3
"""
author: zj
file: TorcsTool.py
time: 17-6-20
"""
from ctypes import cdll, c_int, c_uint8, c_double, c_bool,Structure, POINTER,c_float, sizeof,create_string_buffer,c_char_p,c_char,c_void_p
import numpy as np
import time
import os
from numpy.ctypeslib import ndpointer
import scipy.misc
import scipy
import operator
import glob
dir_path = os.path.dirname(os.path.realpath(__file__))
lib = cdll.LoadLibrary('{}/Torcs_tool.so'.format(dir_path))
STUCK_ANGLE = 0.8
def clip(lo, x, hi):
"""
:param lo:
:param x:
:param hi:
:return: x -> [lo , hi]
"""
# print(x)
if x < lo:
return lo
elif x > hi:
return hi
else:
return x
class torcs_tool(object):
class _29data(Structure):
_fields_ = [
("angle", c_float),
("track", c_float * 19),
("opponents", c_float * 36),
("focus", c_float * 5),
("trackPos", c_float),
("speedX", c_float),
("speedY", c_float),
("speedZ", c_float),
("wheelSpinVel", c_float * 4),
("rpm", c_float),
("damage", c_float),
("curLapTime", c_float),
("lastLapTime", c_float),
("distFromStart", c_float),
("distRaced", c_float),
("fuel", c_float),
("racePos", c_int),
("gear", c_int),
("z", c_float),
("toleft", c_float),
("toright", c_float),
("radius", c_float)
]
class read(Structure):
_fields_ = [
("steer",
c_double),
("brake",
c_double),
("accel",
c_double),
("gear",
c_int),
("clutch",
c_double),
("speed_x",
c_double),
("speed_y",
c_double),
("speed_z",
c_double),
("track_angle", # -pi,pi
c_double),
("track_pos",
c_double),
("rpm",
c_double),
("radius",
c_double)]
def __init__(self,grab_shot=False,key=1234,resize = False):
#self.ipcrm(key)
lib.init.argtypes = [c_int]
lib.init(key)
if grab_shot:
self.reserveScreenShotFlag()
self.gear = 1
self.resize = resize
self.pre_damage = 0
def __str__(self):
return("speed:{} steer:{} gear:{} clutch:{} accel:{} brake:{}"
.format(str(self.speed), str(self.steer), str(self.gear), str(self.clutch), str(self.accel), str(self.brake)))
def ipcrm(self,key):
os.system('ipcrm -M {}'.format(key))
def reserveScreenShotFlag(self):
lib.reserveScreenShotFlag()
def changeTrack(self,track):
lib.setTrack.argtypes = [c_char_p]
lib.setTrack(track)
def changeTrackOk(self,track):
lib.setTrackOk.argtypes = [c_char]
lib.setTrackOk(track)
def getStateCount(self):
return len(self.allData)
@property
def _29data_size(self):
return int(sizeof(self._29data) / 4)
@property
def get29Data(self):
lib.get29Data.restype = POINTER(self._29data)
x = lib.get29Data().contents
result = {k[0]:np.asarray(getattr(x,k[0])) for k in x._fields_}
result['img'] = self.image # 阻塞抓取图片,决定交互的fps
result['hit'] = result['damage'] - self.pre_damage > 0
result['is_finish'] = self.is_finish
self.pre_damage = result['damage']
#result['radius'] = self.radius
return result
@property
def allData(self):
lib.getStruct.restype = POINTER(self.read)
x = lib.getStruct().contents
# y = [x.speed_x,x.speed_y,x.speed_z,x.steer,x.brake]
array = np.asarray([getattr(x, key[0]) for key in x._fields_])
# array[0] =
# array[1] =
# array[2] =
array[3] /= 6.
array[5] /= 300.
array[6] /= 300.
array[7] /= 300.
array[8] /= 3.1415
# array[9] = clip(-1,array[9],1)
array[10] /= 10000.
array[11] /= 500.
# if not self.pre:
return array
@property
def image(self):
lib.getScreenshot.restype = ndpointer(
dtype=c_uint8, shape=(480, 640, 3))
image = np.flipud(lib.getScreenshot())
if self.resize:
image = scipy.misc.imresize(image, [240, 320])
return image
def stop(self):
lib.stopTorcsTool()
lib.clearFinish()
@property
def track_angle(self):
lib.getTrackAngle.restype = c_double
angle = lib.getTrackAngle()
return angle
@property
def is_hit_wall(self):
lib.isHitWall.restype = c_bool
return lib.isHitWall()
@property
def speed(self):
lib.getSpeedX.restype = c_double
speed = lib.getSpeedX()
return speed
@property
def speed_y(self):
lib.getSpeedY.restype = c_double
speed = lib.getSpeedY()
return speed
@property
def speed_z(self):
lib.getSpeedY.restype = c_double
speed = lib.getSpeedZ()
return speed
@property
def radius(self):
lib.getRadius.restype = c_double
radius = lib.getRadius()
return radius
@property
def rpm(self):
lib.getRpm.restype = c_double
rpm = lib.getRpm()
return rpm
@property
def track_pos(self):
lib.getTrackPos.restype = c_double
pos = lib.getTrackPos()
return pos
@property
def steer(self):
lib.getSteer.restype = c_double
steer = lib.getSteer()
return steer
@steer.setter
def steer(self, _steer):
lib.setSteer.argtypes = [c_double]
_steer = clip(-1, _steer, 1)
lib.setSteer(_steer)
@property
def brake(self):
lib.getBrake.restype = c_double
brake = lib.getBrake()
return brake
@brake.setter
def brake(self, _brake):
lib.setBrake.argtypes = [c_double]
_brake = clip(0, _brake, 1)
lib.setBrake(_brake)
@property
def clutch(self):
lib.getClutch.restype = c_double
clutch = lib.getClutch()
return clutch
@clutch.setter
def clutch(self, _clutch):
lib.setClutch.argtypes = [c_double]
lib.setClutch(_clutch)
@property
def gear(self):
lib.getGear.restype = c_int
gear = lib.getGear()
return gear
@gear.setter
def gear(self, _gear):
lib.setGear.argtypes = [c_int]
lib.setGear(_gear)
# print("gear set to {}".format(_gear))
@property
def accel(self):
lib.getAccel.restype = c_double
accel = lib.getAccel()
return accel
@accel.setter
def accel(self, _accel):
lib.setAccel.argtypes = [c_double]
_accel = clip(0, _accel, 1)
lib.setAccel(_accel)
@property
def is_finish(self):
lib.isFinish.restype = c_bool
is_finish = lib.isFinish()
return is_finish
# lib.clearFinish()
# print(f)
def restart(self):
lib.restart()
lib.clearFinish()
lib.clearHitWall()
lib.clearStuck()
self.pre_damage = 0
@property
def is_stuck(self):
lib.isStuck.restype = c_bool
stuck = lib.isStuck()
lib.clearStuck()
return stuck or np.cos(self.track_angle) < 0
def process_img(self,img):
img = scipy.misc.imread( img )[ 90:156, 60:260 ]
img = img / 127.5 - 1.
return img
@property
def track(self):
lib.getTrack.restype = ndpointer(
dtype=c_float, shape=(19,))
x = lib.getTrack()
return np.array(lib.getTrack())
@property
def angle(self):
lib.getAngle.restype = c_float
return lib.getAngle()
def robot_count(self):
lib.getRobotCount.restype = c_int
while lib.getRobotCount() == 0:
time.sleep(0.1)
return lib.getRobotCount()
def set_adv_speed(self,speed):
lib.setSpeed.argtypes = [c_void_p]
#print(self.robot_count())
assert len(speed) == self.robot_count() , 'Speed vector size must equal robot_count'
arr = (c_float * self.robot_count())(*speed)
#print(arr[0])
lib.setSpeed(arr)
def filter_track(self,filtered_track_path):
assert isinstance(filtered_track_path,str)
pre_track = None
s = None
with open('{}/track.txt'.format(dir_path)) as tracks, open(filtered_track_path,'w') as save_track:
for line in tracks:
line = line.strip()
track_path , _track_category , _track_name = line.split('\t')
self.changeTrack(track_path.encode('utf-8'))
self.changeTrackOk('1'.encode('utf-8'))
self.restart()
s = input()
if s:
save_track.write(line + '\n')
if __name__ == '__main__':
import pprint
s = torcs_tool(grab_shot=True)
i = 0
start = time.time()
while True:
# all 29 datas from scr_server
data = s.get29Data
print(data)