-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicle.py
151 lines (122 loc) · 5.28 KB
/
Vehicle.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
import os
import Consts
class Vehicle(object):
def __init__(self, _id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model, weight):
# Paramaters from the driver model
self.desired_velocity = desired_velocity
self.max_acceleration = max_acceleration
self.max_deceleration = max_deceleration
self.minimum_distance = minimum_distance
self.length = length
# Other parameters
self._id = _id
self._label = 'Base'
self._road = None
self.model = model
self.weight = weight
# Running values
self.velocity = 0
self.acceleration = 0
self.position = 0
self.gap = 0
self.lead_vehicle = None
self.lane = 0
# Previous values
self.prev_velocity = 0
self.prev_acceleration = 0
self.prev_position = 0
self.prev_gap = 0
# Update values
self._new_acceleration = None
self._new_velocity = None
self._new_position = None
self._new_gap = None
self._file = None
def calc_new_params(self):
self._new_acceleration = self.model.calc_acceleration(self)
self._new_velocity = self.model.calc_velocity(self)
self._new_position = self.model.calc_position(self)
self._new_gap = self.model.calc_gap(self)
def update_new_params(self, simulated_time):
# Update previous positions
self.prev_velocity = self.velocity
self.prev_acceleration = self.acceleration
self.prev_position = self.position
self.prev_gap = self.gap
# Update new positions
self.velocity = self._new_velocity
self.acceleration = self._new_acceleration
self.position = self._new_position
self.gap = self._new_gap
assert(self.velocity >= 0)
assert(self.position >= 0)
if self.lead_vehicle:
assert(self.lead_vehicle.position - self.lead_vehicle.length >= self.position)
if Consts.DEBUG_MODE:
self._file.write('{},{},{},{}\n'.format(simulated_time,
self.velocity,
self.position, self.gap))
def set_desired_speed(self, desired_velocity):
self.desired_velocity = desired_velocity
def set_lead_vehicle(self, vehicle):
self.lead_vehicle = vehicle
self.gap = self.model.calc_gap(self)
def add_to_road(self, road, lead_vehicle):
self._road = road
self.lead_vehicle = lead_vehicle
if lead_vehicle:
self.gap = self.lead_vehicle.position - self.lead_vehicle.length
self.velocity = min(self.get_desired_velocity(),
(self.gap / self.get_safetime_headway()))
else:
self.gap = Consts.ROAD_LENGTH + 100
self.velocity = self.desired_velocity
if Consts.DEBUG_MODE:
os.makedirs('debug/lane_{}'.format(self.lane), exist_ok=True)
self._file = open('debug/lane_{}/{}.txt'.format(self.lane,
self._id), 'w')
def set_lane(self, lane):
self.lane = lane
def get_safetime_headway(self):
return self._road.get_safetime_headway(self.lane, self.position)
def get_desired_velocity(self):
road_limit = self._road.get_speed_limit(self.lane, self.position)
return min(road_limit, self.desired_velocity) if road_limit else self.desired_velocity
def finalise(self):
if Consts.DEBUG_MODE:
self._file.close()
def __str__(self):
return '{}({}, {}, {}, {})'.format(self._label, self._id,
self.desired_velocity,
self.minimum_distance, self.weight)
class Car(Vehicle):
def __init__(self, _id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model, weight):
super().__init__(_id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model,
weight)
self._label = 'Car'
class Truck(Vehicle):
def __init__(self, _id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model, weight):
super().__init__(_id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model,
weight)
self._label = 'Truck'
class PlatoonedTruck(Truck):
def __init__(self, _id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model, weight,
is_leader, follow_distance):
super().__init__(_id, desired_velocity, max_acceleration,
max_deceleration, minimum_distance, length, model,
weight)
self._label = 'Platooned Truck'
self.is_leader = is_leader
self.follow_distance = follow_distance
def set_as_lead_vehicle(self):
self.is_leader = True
def set_lead_vehicle(self, vehicle):
if vehicle is None:
self.set_as_lead_vehicle()
super().set_lead_vehicle(vehicle)