-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_single_agent.py
More file actions
98 lines (78 loc) · 3.24 KB
/
main_single_agent.py
File metadata and controls
98 lines (78 loc) · 3.24 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
# This is a sample Python script.
import argparse
try:
import numpy as np
import sys
from os import path as osp
except ImportError:
raise RuntimeError('import error!')
import matplotlib.pyplot as plt
from queue import Empty
from env.world_single_agent import WorldInit
from agent.feature import FeatureExt
from agent.path_planner import *
import agent.velocity_planner as velocity_planner
from agent.linear_MPC import MPC
import agent.vehicle_model as model
from agent.rule_decision import *
from agent.pid import *
def main():
argparser = argparse.ArgumentParser(description='Carla ArgParser practice')
argparser.add_argument('--host', metavar='H', default='127.0.0.1', help='IP of the host server')
argparser.add_argument('-p', '--port', default=2000, type=int, help='TCP port to listen to')
argparser.add_argument('-a', '--autopilot', action='store_true', help='enable autopilot')
args = argparser.parse_args()
running = True
try:
env = WorldInit(args)
fea_extract = FeatureExt(env, env.ego_car)
car_model = model.Vehicle(env.ego_car, env.dt, 12)
dec_maker = RuleBased(car_model, fea_extract, env.dt)
path = path_planner(env, car_model, fea_extract)
mpc = MPC(car_model)
pid = PID_controller(car_model, env.dt)
plt.ion()
plt.figure(1)
v_his, ref_v = [], []
tic = 0
while running:
tic += 1
env.world.tick()
if tic > 0:
car_model.update()
spectator = env.world.get_spectator()
transform = env.ego_car.get_transform()
current_loc = transform.location
spectator.set_transform(carla.Transform(current_loc +
carla.Location(x=-20, z=50),
carla.Rotation(pitch=-60)))
fea_extract.update()
fea_extract.lane_display(fea_extract.wp_list)
# if (car_model.status == STATUS.FOLLOWING):
ref_vel = dec_maker.decision()
print("Reference velocity:", ref_vel)
rx, ry, ryaw, s_sum = path.update()
fea_extract.ref_display(rx, ry)
# poly_coe = velocity_planner.speed_profile_quinticPoly(car_model, 0, 0, dec_maker.stop_dist)
poly_coe = velocity_planner.speed_profile_uniform(ref_vel)
control_comd = mpc.update(rx, ry, ryaw, poly_coe)
# control_comd = pid.update(rx, ry, ryaw, poly_coe)
env.ego_car.apply_control(control_comd)
print("yaw:", ryaw[0], car_model.yaw)
v_his.append(car_model.v)
ref_v.append(ref_vel)
plt.plot(v_his, linestyle='--', color='b')
plt.plot(ref_v, linestyle='--', color='r')
plt.pause(0.0001)
print("----------------------")
try:
env.sensor_queue.get()
except Empty:
print('some of the sensor information is missed')
finally:
env.__del__()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Exit by user')