forked from kimiya66/master-thesis-autonomous-parking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parkingManeuver.py
executable file
·461 lines (360 loc) · 16.6 KB
/
parkingManeuver.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env python
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import glob
import os
import sys
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import argparse
import math
import random
import time
import struct, binascii
import config
import numpy
def get_transform(vehicle_location, angle, d=6.4):
a = math.radians(angle)
location = carla.Location(d * math.cos(a), d * math.sin(a), 2.0) + vehicle_location
return carla.Transform(location, carla.Rotation(yaw=180 + angle, pitch=-15))
def main():
vehicle = None
vehicle1=None
vehicle2=None
vhcl3=None
vhcl4=None
vhcl5=None
actor_list = []
sensors = []
argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--host',
metavar='H',
default='127.0.0.1',
help='IP of the host server (default: 127.0.0.1)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
args = argparser.parse_args()
try:
client = carla.Client(args.host, args.port)
client.set_timeout(10.0)
world = client.get_world()
ourMap = world.get_map()
#+++++setting the fixed time stamp+++++++++
settings = world.get_settings();
settings.fixed_delta_seconds = 0.05;
#eeedddsettings.synchronous_mode = True;
world.apply_settings(settings);
spectator = world.get_spectator()
#snapShot = world.get_snapshot();
#sensor deffinition
lidar_blueprint = world.get_blueprint_library().find('sensor.lidar.ray_cast')
camera_blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
GnssSensor_blueprint = world.get_blueprint_library().find('sensor.other.gnss')
obstacleSensor_blueprint = world.get_blueprint_library().find('sensor.other.obstacle')
blueprint = world.get_blueprint_library().find('vehicle.audi.tt')
blueprint.set_attribute('role_name', 'hero')
non_blueprint2 = random.choice(world.get_blueprint_library().filter('vehicle.tesla.*'))
non_blueprint = random.choice(world.get_blueprint_library().filter('vehicle.bmw.grandtourer'))
non_blueprint5 = random.choice(world.get_blueprint_library().filter('vehicle.nissan.micra'))
non_blueprint4 = random.choice(world.get_blueprint_library().filter('vehicle.toyota.*'))
#------------parking maneuver----------------------
def parking(t):
#print('*****parking control*****');
#print('time before calling calculate_maneuverTime func:', config.T)
#+++++++++++++++++calculate maneuver-time and phi_max++++++++++++++++++++++++++++
angle=steeringAngle(t);
v=velocity(t);
print('steer angle = ', angle,'velocity function:',v);
#physics_control=vehicle.get_physics_control()
#vehicle.set_simulate_physics(enabled=True);
velo = vehicle.get_velocity().y;
control=vehicle.get_control();
throttle = control.throttle;
brake = control.brake;
'''if velo < abs(v):
print('throttle:',throttle,'control.throttle:',control.throttle);
throttle = throttle + 0.5;
else:
brake = brake + 0.5;
print('brake value:',control.brake);'''
#print('throttle=',throttle,'brake=',brake);
vehicle.apply_control(carla.VehicleControl(throttle=v, manual_gear_shift=True, gear=0,steer=angle,reverse=True));
#print('vehicle speed of vx:',vehicle.get_velocity().x,'vy:',vehicle.get_velocity().y,'vz:',vehicle.get_velocity().z);
# print(vehicle.get_speed_limit());
print('steer:',control.steer);
#..............calculation of steeringAngl at each time of maneuver..............
def steeringAngle(t):
#print('phi_max which is used here:', config.phi_max);
result=config.phi_max * config.sideOfParking * _A(t);
#print('A(t)= ', _A(t));
#print('phi value:', result);
return result;
#..............calculation of velocity at each time of maneuver....................
def velocity(t):
#print('v_max=',config.v_max,'direction=',config.direction,'B func result:',_B(t));
for type_error in _B(t):
result = config.v_max * config.direction * type_error;
#print('result of multiply:',result);
return result;
#...............calculation of _A and _B functions to control phi and v...............
def _A(t):
#print('what we have as T in A(t): ', config.T);
#print('T value at the moment: ', config.T, 'value for T-start:', config.T_star);
t_prime = (config.T - config.T_star)/2;
#print('t_prime=',t_prime,'t value = ',t);
result = 0; #output of the function
if 0 <= t < t_prime:
result = 1;
elif t_prime <= t <= config.T-t_prime:
result = math.cos((math.pi*(t-t_prime))/config.T_star)
elif config.T-t_prime < t <= config.T:
result = -1;
return result;
def _B(t):
# print('what we have as T in B(t): ', config.T);
result = 0,5*(1-math.cos((4*math.pi*t)/config.T));
return result;
#.....................calculation of T (whole time of parking maneuver)................
def calculate_maneuverTime():
x=vehicle.get_location().x;
y=vehicle.get_location().y;
orientAngl=vehicle.get_transform().rotation.yaw;
ts=0;
cond = True;
while cond:
for ts in numpy.arange(0,config.T,config.sampling_period):
s_angle = steeringAngle(ts);
#print('s_angle value:',s_angle);
velo = velocity(ts);
if(s_angle == 0):
orientAngl_lastStep = orientAngl;
orientAngl = orientAngl;
x = x + (velo * config.sampling_period * math.cos(orientAngl));
y = y + (velo * config.sampling_period * math.sin(orientAngl));
#print('x,y in if-clause:',x,y);
else:
orientAngl_lastStep = orientAngl;
orientAngl = orientAngl + (((velo * config.sampling_period)/config.vehicle_length)*math.sin(s_angle));
x = x + ((config.vehicle_length / math.tan(s_angle)) * (math.sin(orientAngl) - math.sin(orientAngl_lastStep)));
y = y - ((config.vehicle_length / math.tan(s_angle)) * (math.cos(orientAngl) - math.cos(orientAngl_lastStep)));
#print('x,y in else clause:',x,y);
cond=longitudinal_condition(vehicle.get_location().x,x,vehicle.get_location().y,y,vehicle.get_transform().rotation.yaw);
print('longitudinal cond:', cond);
config.T += config.sampling_period;
print('T calc values',config.T);
config.T -= config.sampling_period;
#.....................calculation of phi_max ..................................................
def calculate_max_steeringAng():
x=vehicle.get_location().x;
y=vehicle.get_location().y;
orientAngl=vehicle.get_transform().rotation.yaw;
ts=0;
cond = False;
#print('+++++++++phi_max calculation++++++++++++++++');
#print('T_max first value:',config.T);
while not cond:
config.phi_max -= 0.0872665
#print('config.phi_max value:',config.phi_max);
#print('value of T_max in phi_max calculation:',config.T);
for ts in numpy.arange(0,config.T,config.sampling_period):
s_angle = steeringAngle(ts);
velo = velocity(ts);
if(s_angle == 0):
orientAngl_lastStep = orientAngl;
orientAngl = orientAngl;
#print('orientationAngl for the last step:',orientAngl_lastStep,'orientAngl for this step:',orientAngl)
x = x + (velo * config.sampling_period * math.cos(orientAngl));
y = y + (velo * config.sampling_period * math.sin(orientAngl));
else:
orientAngl_lastStep = orientAngl;
orientAngl = orientAngl + (((velo * config.sampling_period)/config.vehicle_length)*math.sin(s_angle));
x = x + ((config.vehicle_length / math.tan(s_angle)) * (math.sin(orientAngl) - math.sin(orientAngl_lastStep)));
y = y - ((config.vehicle_length / math.tan(s_angle)) * (math.cos(orientAngl) - math.cos(orientAngl_lastStep)));
cond=lateral_condition(vehicle.get_location().x,x,vehicle.get_location().y,y,vehicle.get_transform().rotation.yaw);
print('max steeringAngle from calculation:',config.phi_max);
#............limitation for calculating time.........................
def longitudinal_condition(x0,xT,y0,yT,orientAngl):
x = math.fabs(((xT-x0)*math.cos(orientAngl))+((yT-y0)*math.sin(orientAngl)));
print('value of lon calc:', x);
cond = x < config.parkingLength;
print('longitudinal condition result',cond);
return cond;
#.............condition to calculate phi_max............................
def lateral_condition(x0,xT,y0,yT,orientAngl):
x = math.fabs(((x0-xT)*math.sin(orientAngl))+((yT-y0)*math.cos(orientAngl)));
print('value of lat calc:', x);
cond = x < config.parkingWidth;
#print('lateral condition result:',cond);
return cond;
#********************non-player vehicles and sensors************************************
# first non-player vehicle
if vehicle1 is not None:
coordinate1 = vehicle1.get_transform()
coordinate1.location.z += 2.0
coordinate1.rotation.roll = 0.0
coordinate1.rotation.yaw = 0.0
for actor in actor_list:
if actor is not None:
actor.destroy()
# staticPoint1 = carla.Transform(carla.Location(200, 303), carla.Rotation(yaw=180))
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x -=12.0
vehicle1 =world.spawn_actor(non_blueprint2, spawn_point)
print('location of static-vehicle1: ')
print(vehicle1.get_transform().location.x, vehicle1.get_transform().location.y)
actor_list.append(vehicle1)
while vehicle1 is None:
#staticPoint1 = carla.Transform(carla.Location(200, 303), carla.Rotation(yaw=180))
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x -=12.0
vehicle1 =world.spawn_actor(non_blueprint2, spawn_point)
print('location of static-vehicle1: ')
print(vehicle1.get_transform().location.x, vehicle1.get_transform().location.y)
actor_list.append(vehicle1)
# 2nd non-player vehicle
if vehicle2 is not None:
coordinate2 = vehicle1.get_transform()
coordinate2.location.z += 2.0
coordinate2.rotation.roll = 0.0
coordinate2.rotation.yaw = 0.0
for actor in actor_list:
if actor is not None:
actor.destroy()
spawn_points2 = ourMap.get_spawn_points()
spawn_point2 = spawn_points2[30]
spawn_point2.location.y +=1.2
spawn_point2.location.x -=20.0
vehicle2 =world.spawn_actor(non_blueprint, spawn_point2)
#print('location of static-vehicle2: ')
#print(vehicle2.get_transform().location.x, vehicle2.get_transform().location.y)
actor_list.append(vehicle2)
while vehicle2 is None:
spawn_points2 = ourMap.get_spawn_points()
spawn_point2 = spawn_points2[30]
spawn_point2.location.y +=1.2
spawn_point2.location.x -=20.0
vehicle2 =world.spawn_actor(non_blueprint, spawn_point2)
# print('location of static-vehicle2: ')
# print(vehicle2.get_transform().location.x, vehicle2.get_transform().location.y)
actor_list.append(vehicle2)
# 4rd non-player vehicle
if vhcl4 is not None:
coordinate4 = vhcl4.get_transform()
coordinate4.location.z += 2.0
coordinate4.rotation.roll = 0.0
coordinate4.rotation.yaw = 0.0
for actor in actor_list:
if actor is not None:
actor.destroy()
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x +=2.0
vhcl4 =world.spawn_actor(non_blueprint4, spawn_point)
actor_list.append(vhcl4)
while vhcl4 is None:
#staticPoint1 = carla.Transform(carla.Location(200, 303), carla.Rotation(yaw=180))
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x +=2.0
vhcl4=world.spawn_actor(non_blueprint4, spawn_point)
actor_list.append(vhcl4)
#5th nonplayer vehicle
if vhcl5 is not None:
coordinate5 = vhcl5.get_transform()
coordinate5.location.z += 2.0
coordinate5.rotation.roll = 0.0
coordinate5.rotation.yaw = 0.0
for actor in actor_list:
if actor is not None:
actor.destroy()
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x +=10.0
vhcl5 =world.spawn_actor(non_blueprint5, spawn_point)
actor_list.append(vhcl5)
while vhcl5 is None:
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30]
spawn_point.location.y +=1.0
spawn_point.location.x +=10.0
vhcl5=world.spawn_actor(non_blueprint5, spawn_point)
actor_list.append(vhcl5)
#-----------------spawn main vehicle---------------------------------
if vehicle is not None:
spawn_point = vehicle.get_transform()
spawn_point.location.z += 2.5
spawn_point.rotation.roll = 0.0
spawn_point.rotation.yaw = 0.0
for actor in actor_list:
if actor is not None:
actor.destroy()
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30];
spawn_point.location.x += 2;
spawn_point.location.y -= 1.5;
vehicle = world.spawn_actor(blueprint, spawn_point)
actor_list.append(vehicle)
while vehicle is None:
spawn_points = ourMap.get_spawn_points()
spawn_point = spawn_points[30];
spawn_point.location.x += 2;
spawn_point.location.y -= 1.5;
vehicle = world.spawn_actor(blueprint, spawn_point)
actor_list.append(vehicle)
#========inja bayad check konam ke age car be yek noghteie resid az halghe biad biroon======get_location ghablo bad loop chek beshe*
time.sleep(3);
calculate_maneuverTime();
calculate_max_steeringAng();
time.sleep(3);
print('time duration:',config.T,'max steeringAngle:',config.phi_max);
print('maneuver time.2')
for t in numpy.arange(0,config.T,config.sampling_period):
parking(t);
time.sleep(0.05);
'''while True:
t=0;
while t < config.T:
parking(t);
t += config.sampling_period;
time.sleep(0.5)'''
#print('maneuver time:',t);
#=======after loop car be vaziate parked(sabet) bereseh=====apply_control meghdare steer ra 0 kone*
while True:
time.sleep(10)
finally:
print('\ndestroying %d actors' % len(actor_list))
for actor in actor_list:
if actor is not None:
actor.destroy()
for sen in sensors:
if sen is not None:
sen.destroy();
if __name__ == '__main__':
try:
main();
except KeyboardInterrupt:
pass;
finally:
print('\ndone.');