forked from ajinkya-khade/ACC_Vehicle_MPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getXref.m
32 lines (26 loc) · 956 Bytes
/
getXref.m
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
function X = getXref(a, v0, vdes, ts, tsim)
% getXref Generate reference trajectory for tracking vehicle
% This function generates the reference trajectory for tracking
% vehicle to follow for a given acceleration and desired steady-state
% velocity
%
% Author : Ajinkya Khade, [email protected]
% Initializing arrays to store simulation time and tracking vehicle
% position
t = 0:ts:tsim;
X = zeros(3,length(t));
% Setting velocity and acceleration value at beginning of simulation
X(2,1) = v0;
X(3,1) = a;
Nsim = floor(tsim/ts) + 1;
for i = 1:Nsim
X(1,i+1) = X(1,i) + X(2,i)*ts;
X(2,i+1) = X(2,i) + X(3,i)*ts;
% Setting acceleration value for next time step
if sign(a)*X(2,i+1) >= sign(a)*vdes
X(3,i+1) = 0; %acceleration = 0, if desired velocity reached
else
X(3,i+1) = a; %acceleration = a, if desired velocity not reached
end
end
end