This repository was archived by the owner on Jun 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealSimInterpSpeed.m
More file actions
315 lines (238 loc) · 11.9 KB
/
RealSimInterpSpeed.m
File metadata and controls
315 lines (238 loc) · 11.9 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
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
classdef RealSimInterpSpeed < matlab.System & matlab.system.mixin.Propagates & matlab.system.mixin.CustomIcon
% RealSimInterpSpeed Interpolate ego vehicle speed command from interface
% Public, tunable properties
properties
initialSpeed = 0; % initial vehicle speed
% speedInterpolationMode = 0; % 0: no interpolation, 1: linear interpolation
speedLookAheadHorizon = 0; % look ahead horizon in (seconds)
speedUpperBound = 30; % upper bound of speed command in (meter/second)
end
% Public, non-tunable properties
properties(Nontunable)
smoothWindow (1,1){mustBeInteger,mustBePositive} = 5; % number of data point to smooth speed command
end
properties(DiscreteState)
end
% Pre-computed constants
properties(Access = private)
timePrevious;
speedPrevious;
timeNext;
speedNext;
connectionState;
timeSimulator;
simulatorStartTime;
timeStepSimulator;
timeStepSimulatorPrevious;
timeReceivePrevious;
speedReceivePrevious;
simState;
RealSimDelay;
speedLookAheadPrevious;
accelerationDesiredPrevious;
speedInterpolationMode;
speedInterpPreviousList;
accelerationDesiredPreviousList;
end
methods
% Constructor
function obj = RealSimInterpSpeed(varargin)
% Support name-value pair arguments when constructing object
% setProperties(obj,nargin,varargin{:})
% coder.allowpcode('plain')
end
end
methods(Access = protected)
%% Common functions
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
obj.timePrevious = 0;
obj.speedPrevious = 0;
obj.timeNext = 0;
obj.speedNext = 0;
obj.connectionState = 0;
obj.timeSimulator = 0;
obj.simulatorStartTime = 0;
obj.timeStepSimulator = 0;
obj.timeStepSimulatorPrevious = 0;
obj.timeReceivePrevious = 0;
obj.speedReceivePrevious = 0;
obj.simState = 0;
obj.RealSimDelay = 0;
obj.speedLookAheadPrevious = 0;
obj.accelerationDesiredPrevious = 0;
obj.speedInterpolationMode = 0;
obj.speedInterpPreviousList = zeros(obj.smoothWindow,1);
obj.accelerationDesiredPreviousList = zeros(obj.smoothWindow,1);
end
function [speedLookAhead, accelerationDesired, timeSimulator, timeStepSimulator, timeStepTrigger, RealSimDelay] = ...
stepImpl(obj, simulationTime, connectionState, simState, timeReceive, speedReceive, isVehicleInNetwork, speedActual)
% INPUTS
% ByteData: bytes vector received from TrafficSimualtor
% nByte: size of bytes received
% initSpeed: initial speed command sent to controller m/s
% speedPrev: previous speed command received from TrafficSimulator
%
% OUTPUTS
% simState: simulation state. NOT actively used
% t: simulation time of TrafficSimulator
% nVeh: number of vehicle data received.
% speed: speed of ego vehicle in TrafficSimulator m/s
speedInterp = 0;
timeSimulator = 0;
timeStepSimulator = 0;
timeStepTrigger = 0;
RealSimDelay = 0;
speedLookAhead = 0;
accelerationDesired = 0;
% if just connected to server at current step
% if abs(connectionState-3) < 1e-5 && abs(obj.connectionState-3) > 1e-5
if abs(simState - 1) < 1e-5 && abs(obj.simState) < 1e-5 && abs(obj.connectionState-3) < 1e-5
% set connectionState and start time
obj.simulatorStartTime = simulationTime;
obj.simState = simState;
% temp = 1;
% if just disconnected to server at current step
elseif abs(obj.connectionState-3) < 1e-5 && abs(connectionState-3) > 1e-5
% resetting
obj.connectionState = 0;
obj.timeSimulator = 0;
obj.simulatorStartTime = 0;
obj.timeStepSimulator = 0;
obj.timeStepSimulatorPrevious = 0;
obj.simState = 0;
obj.RealSimDelay = 0;
end
% temp = obj.connectionState;
obj.connectionState = connectionState;
% get absolute time in Simulator, increase every Simulink step
if obj.simState && abs(obj.connectionState-3) < 1e-5
obj.timeSimulator = simulationTime-obj.simulatorStartTime;
else
obj.timeSimulator = -1;
end
% get time step in Simulator, change every VISSIM/SUMO step
% as byproduct, get triggered time every 0.1 second
if obj.simState && abs(obj.connectionState-3) < 1e-5 && (obj.timeSimulator >= obj.timeStepSimulatorPrevious + 0.1 - 1e-5 || abs(obj.simulatorStartTime-simulationTime) < 1e-5)
obj.timeStepSimulator = obj.timeSimulator;
obj.timeStepSimulatorPrevious = obj.timeStepSimulator;
timeStepTrigger = 1;
else
obj.timeStepSimulator = obj.timeStepSimulatorPrevious;
end
timeSimulator = obj.timeSimulator;
timeStepSimulator = obj.timeStepSimulator;
% check if has new data from traffic simulator
if timeReceive >= 0.2 && abs(timeReceive-obj.timeReceivePrevious) > 1e-5
hasNewData = 1;
else
hasNewData = 0;
end
% speed handling
if obj.simState && obj.connectionState && isVehicleInNetwork
%%% interpolation
if obj.speedInterpolationMode == 0
speedInterp = speedReceive;
elseif obj.speedInterpolationMode == 1
% update interpolation point
if hasNewData == 1
obj.timePrevious = obj.timeNext;
obj.timeNext = obj.timeStepSimulator+0.1;
obj.speedPrevious = obj.speedNext;
obj.speedNext = speedReceive;
end
% doing interpolation
speedTemp = interp1([obj.timePrevious, obj.timeNext], [obj.speedPrevious, obj.speedNext], obj.timeSimulator, 'linear');
if obj.timeSimulator > obj.timeNext
speedInterp = obj.speedNext;
else
speedInterp = speedTemp;
end
end
else
% use initial speed
speedInterp = obj.initialSpeed;
end
% calculate a lookahead speed accounting for dynamics if
% traffic simulator is determining the desired speed
if isVehicleInNetwork
% should update lookahead speed whenever receive new data from traffic simulator
if hasNewData == 1
% speedInterp is from SUMO, speedActual
% due to tcp/ip delay, the remaining time could be less
% than 0.1 seconds
dtRem = 0.1-mod(obj.timeSimulator,0.1);
accelerationDesiredCurrent = min(max(-5, (speedInterp-speedActual)/dtRem), 5);
speedLookAheadCurrent = min(max(0, speedActual + obj.speedLookAheadHorizon*accelerationDesiredCurrent), obj.speedUpperBound);
if obj.smoothWindow >= 2
obj.speedInterpPreviousList(1:obj.smoothWindow-1) = obj.speedInterpPreviousList(2:obj.smoothWindow);
obj.speedInterpPreviousList(end) = speedInterp;
obj.accelerationDesiredPreviousList(1:obj.smoothWindow-1) = obj.accelerationDesiredPreviousList(2:obj.smoothWindow);
obj.accelerationDesiredPreviousList(end)=accelerationDesiredCurrent;
accelerationDesired = min(max(-5, (mean(obj.speedInterpPreviousList)-speedActual)/dtRem), 5);
% accelerationDesired = mean(obj.accelerationDesiredPreviousList);
speedLookAhead = min(max(0, speedActual + obj.speedLookAheadHorizon*accelerationDesired), obj.speedUpperBound);
else
accelerationDesired = accelerationDesiredCurrent;
speedLookAhead = speedLookAheadCurrent;
end
obj.accelerationDesiredPrevious = accelerationDesired;
obj.speedLookAheadPrevious = speedLookAhead;
else
accelerationDesired = obj.accelerationDesiredPrevious;
speedLookAhead = obj.speedLookAheadPrevious;
end
else
accelerationDesired = 0;
speedLookAhead = speedInterp;
end
temp = obj.simulatorStartTime;
% calc delay
if timeReceive >= 0.2 && abs(timeReceive-obj.timeReceivePrevious) > 1e-5
obj.RealSimDelay = obj.timeSimulator + 0.2 - timeReceive;
obj.timeReceivePrevious = timeReceive;
end
RealSimDelay = obj.RealSimDelay;
end
function resetImpl(obj)
% Initialize / reset discrete-state properties
end
function validateInputsImpl(obj, simulationTime, connectionState, simState, timeReceive, speedReceive, isVehicleInNetwork, speedActual)
% Validate inputs to the step method at initialization
end
% function [name,name2,name3,name4] = getInputNamesImpl(obj)
% % Return input port names for System block
% name = 'simulationTime';
% name2 = 'connectionState';
% name3 = 'timeReceive';
% name4 = 'speedReceive';
% end
%
% function [name,name2,name3,name4] = getOutputNamesImpl(obj)
% % Return output port names for System block
% name = 'speedInterp';
% name2 = 'timeSimulator';
% name3 = 'timeStepSimulator';
% name4 = 'timeStepTrigger';
% end
function [sz1, sz2, sz3, sz4, sz5, sz6] = getOutputSizeImpl(obj)
% Maximum length of linear indices and element vector is the
% number of elements in the input
sz1 = 1; sz2 = 1; sz3 = 1; sz4 = 1; sz5 = 1; sz6 = 1;
end
function [fz1, fz2, fz3, fz4, fz5, fz6] = isOutputFixedSizeImpl(~)
%Both outputs are always fixed
fz1 = 1; fz2 = 1; fz3 = 1; fz4 = 1; fz5 = 1; fz6 = 1;
end
function [dt1, dt2, dt3, dt4, dt5, dt6] = getOutputDataTypeImpl(obj)
dt1 = 'double'; dt2 = 'double'; dt3 = 'double'; dt4 = 'double'; dt5 = 'double'; dt6 = 'double';
end
function [cp1, cp2, cp3, cp4, cp5, cp6] = isOutputComplexImpl(obj)
cp1 = false; cp2 = false; cp3 = false; cp4 = false; cp5 = false; cp6 = false;
end
end
methods(Access = private)
%%% END OF PRIVATE METHODS
end
%%% END OF CLASS
end