-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestVehicleClass.m
More file actions
186 lines (153 loc) · 4.15 KB
/
TestVehicleClass.m
File metadata and controls
186 lines (153 loc) · 4.15 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
%% Scenario 3
%make two cars, have the lead car have a slower max speed, make sure the
%second car follows at a safe distance
%Start the car1 at pos 1.
%Start the car2 at pos 0.
% Accelerate to desired speed
% Travel to desired distance
% stop simulation when pos >= destination
% Collect and plot time, speed, and distance information
clear;
clear Vehicle;
clear VehicleMgr;
clc;
disp('Scenario 3');
global car1;
global car2;
dest = 50;
car1 = Vehicle;
car1.id = 'car1';
car1.preferredSpeed = 45;
ds = car1.preferredSpeed;
car1.targetVelocity = ds;
car1.destination = dest; %miles
acc = car1.acceleration;
car1.targetRate = acc;
pos1 = 20/5280;
car1.posY = pos1; %miles
car1.lane = 1;
car2 = Vehicle;
car2.id = 'car2';
car2.preferredSpeed = 55;
ds = car2.preferredSpeed;
car2.targetVelocity = ds;
car2.destination = dest; %miles
acc = car2.acceleration;
car2.targetRate = acc;
pos2 = 0;
car2.posY = pos2; %miles
car2.lane = 1;
done = false;
vm=VehicleMgr.getInstance;
% vm.AddCar(car1);
% vm.AddCar(car2);
car3 = car2;
car3.id = 'car3';
pos3 = (car1.posY + car2.posY)/2;
car3.posY = pos3;
car1.lane = 3;
% vm.AddCar(car3);
vm = AddVehicles(vm, [car1, car2, car3]);
n = 1; %array index
t = 0; %seconds
tinc = 1; %seconds
while( done == false )
vm.TimeStep(tinc);
if car1.posY>= dest || car2.posY >= dest || car3.posY >= dest
done = true;
else
fprintf('t=%d, p=%f, v=%f, a=%f, p=%f, v=%f, a=%f\n',t, car1.posY, car1.velocity, car1.acceleration, car2.posY, car2.velocity, car2.acceleration);
%collect results for plots
x(n) = t;
p1(n) = car1.posY;
s1(n) = car1.velocity;
a1(n) = car1.acceleration;
p2(n) = car2.posY;
s2(n) = car2.velocity;
a2(n) = car2.acceleration;
p3(n) = car3.posY;
s3(n) = car3.velocity;
a3(n) = car3.acceleration;
n=n+1;
t=t+tinc;
end
end
%% Scenario 1
%travel in a straight line, accelerating to max speed, for desired distance
%Start the car at pos 0.
% Accelerate to desired speed
% Travel to desired distance
% stop simulation when pos >= destination
% Collect and plot time, speed, and distance information
clear;
clc;
v = Vehicle;
ds = get(v,'preferredSpeed');
set(v,'targetVelocity', ds);
dest = 30;
set(v,'destination', dest); %miles
acc = get(v,'acceleration');
set(v,'targetRate', acc);
pos = 0;
set(v,'posY', pos); %miles
n = 1; %array index
t = 0; %seconds
tinc = 1; %seconds
while( pos < dest )
[pos,vel,acc] = v.Advance(tinc); % step in 60 second increments
fprintf('t=%d, p=%f, v=%f. a=%f\n',t,pos, vel, acc);
%collect results for plots
x(n) = t;
p(n) = pos;
s(n) = vel;
a(n) = acc;
n=n+1;
t=t+tinc;
end
%% Scenario 2
%travel in a straight line, accelerating to max speed, for desired distance
%then decelerate
%Start the car at pos 0.
% Accelerate to desired speed
% Travel to desired distance
% stop simulation when pos >= destination
% Collect and plot time, speed, and distance information
clear;
clc;
v = Vehicle;
ds = get(v,'preferredSpeed');
set(v,'targetVelocity', ds);
dest = 30;
set(v,'destination', dest); %miles
acc = get(v,'acceleration');
set(v,'targetRate', acc);
pos = 0;
set(v,'posY', pos); %miles
n = 1; %array index
t = 0; %seconds
tinc = 1; %seconds
while( pos < dest )
[pos,vel,acc] = v.Advance(tinc); % step in 60 second increments
fprintf('t=%d, p=%f, v=%f. a=%f\n',t,pos, vel, acc);
%collect results for plots
x(n) = t;
p(n) = pos;
s(n) = vel;
a(n) = acc;
n=n+1;
t=t+tinc;
end
%switch over to stopping mode by decelerating
acc = get(v,'deceleration');
set(v,'targetRate', acc);
while( vel > 0 ) %wait for car to stop
[pos,vel,acc] = v.Advance(tinc); % step in 60 second increments
fprintf('t=%d, p=%f, v=%f. a=%f\n',t,pos, vel, acc);
%collect results for plots
x(n) = t;
p(n) = pos;
s(n) = vel;
a(n) = acc;
n=n+1;
t=t+tinc;
end