Skip to content

Commit 9e42d15

Browse files
committed
first model
1 parent b76030c commit 9e42d15

File tree

10 files changed

+533
-2
lines changed

10 files changed

+533
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
classdef Simulator<handle
2+
% Simulator Vehicle dynamics simulator
3+
% The simulator receives a vehicle object that inherits from VehicleSimple, simulates its behavior during a given time span and provides its behavior during time via its properties. Each property is a (timespan, 1) vector in which each value represents that parameter's value in time.
4+
methods
5+
% Constructor
6+
function self = Simulator(vehicle, tspan)
7+
self.Vehicle = vehicle;
8+
self.TSpan = tspan;
9+
% Initial conditions
10+
self.X0 = 0;
11+
self.V0 = 20;
12+
end
13+
14+
function f = getInitialState(self)
15+
% Transforms properties into a vector so it can be used by the integrator
16+
f = [self.X0 self.V0];
17+
end
18+
19+
function Simulate(self)
20+
options = odeset('AbsTol',1e-6,'RelTol',1e-6)
21+
[TOUT, XOUT] = ode45(@(t, estados) self.Vehicle.Model(t, estados,self.TSpan), self.TSpan, self.getInitialState(),options);
22+
% retrieve states exclusive to the vehicle
23+
self.X = XOUT(:, 1);
24+
self.V = XOUT(:, 2);
25+
26+
% TSpan and TOUT contain the same values, but the first is passed in columns, while the second is a vector
27+
self.TSpan = TOUT;
28+
end
29+
end
30+
31+
properties
32+
Vehicle % Vehicle model to be used in the simulation
33+
TSpan % a vector indicating the intervals in which the simulation steps will be conducted
34+
X0 % Initial position [m]
35+
V0 % Initial speed [m/s]
36+
X % Longitudinal position [m]
37+
V % Longitudinal speed [m/s]
38+
end
39+
end
40+
41+
%% See Also
42+
%
43+
% <../../index.html Home>
44+
%
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classdef (Abstract) VehicleModel
2+
% VehicleModel Simple vehicle abstract class.
3+
%
4+
% Abstract class representing a simple vehicle.
5+
%
6+
% Extend this class in order to create a new vehicle model to be used with the simulator.
7+
8+
methods(Abstract)
9+
Model(self, t, estados)
10+
end
11+
12+
properties
13+
m % Mass of the vehicle [kg]
14+
Ft % Traction force [N]
15+
Fb % Brake force [N]
16+
Rx % Rolling resistance [N]
17+
Dx % Drag force [N]
18+
Gx % Gravity force [N]
19+
end
20+
21+
methods
22+
23+
24+
end
25+
26+
end
27+
28+
%% See Also
29+
%
30+
% <../../index.html Home>
31+
%
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
classdef VehicleModelNonlinear < VehicleDynamicsLongitudinal.VehicleModel
2+
% VehicleSimpleNonlinear Nonlinear vehicle model.
3+
%
4+
% It inherits properties from VehicleModel.
5+
6+
methods
7+
% Constructor
8+
function self = VehicleModelNonlinear()
9+
self.m = 1000; % Mass of the vehicle [kg]
10+
self.Ft = 133.7000; % Traction force [N]
11+
self.Fb = 0; % Brake force [N]
12+
end
13+
14+
function dx = Model(self, t, states,~)
15+
16+
% Parameters
17+
m = self.m;
18+
g = 9.81; % Gravity [m/s2]
19+
Cd = 0.3; % Drag coefficient [-]
20+
A = 2; % Frontal area [m2]
21+
rho = 1; % Air density [kg/m2]
22+
23+
% States
24+
X = states(1);
25+
V = states(2);
26+
27+
% Drag resistance
28+
C = 0.5*rho*Cd*A;
29+
Dx = 0.5*rho*Cd*A*V^2;
30+
31+
% Rolling resistance
32+
vKPH = V*3.6;
33+
W = m*g; % Weight [N]
34+
vMPH = vKPH/1.609; % Velocidade [mph]
35+
% Model 1 - Tire pressure (28 psi)
36+
f0 = 0.012;
37+
fs = 0.007;
38+
fr = f0 + 3.24*fs*(vMPH/100).^2.5;
39+
Rx = fr*W; % [N]
40+
41+
% Gravity
42+
Gx = 0;
43+
% Traction force
44+
% TODO:
45+
% if isa(self.Ft,'function_handle')
46+
% Ft = self.Ft([X;V],t);
47+
% elseif length(self.Ft)>1
48+
% Ft = interp1(tspan,self.Ft,t);
49+
% else
50+
% Ft = self.Ft;
51+
% end
52+
53+
Kp = 100;
54+
Ki = 0.03;
55+
vOp = 90/3.6; % Ref. speed [m/s]
56+
Clinear = 2*C*vOp;
57+
Ft = Kp*(vOp-V)+Ki*(vOp*t - X) + C*vOp^2 + (V - vOp)*Clinear;
58+
59+
% Dynamics
60+
dx(1,1) = V;
61+
dx(2,1) = (Ft - Dx - Rx - Gx)/m;
62+
63+
end
64+
end
65+
end
66+
67+
%% See Also
68+
%
69+
% <../../index.html Home>
70+
%
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
classdef VehicleModelNonlinear < VehicleDynamicsLongitudinal.VehicleModel
2+
% VehicleSimpleNonlinear Nonlinear vehicle model.
3+
%
4+
% It inherits properties from VehicleModel.
5+
6+
methods
7+
% Constructor
8+
function self = VehicleModelNonlinear()
9+
self.m = 1000; % Mass of the vehicle [kg]
10+
self.Ft = 133.7000; % Traction force [N]
11+
self.Fb = 0; % Brake force [N]
12+
end
13+
14+
function dx = Model(self, t, states,~)
15+
16+
% Parameters
17+
m = self.m;
18+
g = 9.81; % Gravity [m/s2]
19+
Cd = 0.3; % Drag coefficient [-]
20+
A = 2; % Frontal area [m2]
21+
rho = 1; % Air density [kg/m2]
22+
23+
% States
24+
X = states(1);
25+
V = states(2);
26+
27+
% Drag resistance
28+
C = 0.5*rho*Cd*A;
29+
Dx = 0.5*rho*Cd*A*V^2;
30+
31+
% Rolling resistance
32+
% vKPH = V*3.6;
33+
% W = m*g; % Weight [N]
34+
% vMPH = vKPH/1.609; % Velocidade [mph]
35+
% % Model 1 - Tire pressure (28 psi)
36+
% f0 = 0.012;
37+
% fs = 0.007;
38+
% fr = f0 + 3.24*fs*(vMPH/100).^2.5;
39+
% Rx = fr*W; % [N]
40+
Rx=0;
41+
% Gravity force
42+
Gx = 0;
43+
if t > 20
44+
theta = 2*pi/180; % Slope [rad]
45+
Gx = m*g*sin(theta); % Gravity force [N]
46+
end
47+
48+
% Traction force
49+
% TODO: PI control as Ft model using function handle.
50+
% if isa(self.Ft,'function_handle')
51+
% Ft = self.Ft([X;V],t);
52+
% elseif length(self.Ft)>1
53+
% Ft = interp1(tspan,self.Ft,t);
54+
% else
55+
% Ft = self.Ft;
56+
% end
57+
Kp = 300;
58+
Ki = 100;
59+
vOp = 72/3.6; % Ref. speed [m/s]
60+
Clinear = 2*C*vOp;
61+
Ft = Kp*(vOp-V)+Ki*(vOp*t - X) + C*vOp^2 + (V - vOp)*Clinear;
62+
63+
% Dynamics
64+
dx(1,1) = V;
65+
dx(2,1) = (Ft - Dx - Rx - Gx)/m;
66+
67+
end
68+
end
69+
end
70+
71+
%% See Also
72+
%
73+
% <../../index.html Home>
74+
%
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
%% Template Simplified
2+
% This template shows how to simulate a simple vehicle and plot the
3+
% results.
4+
%
5+
%% Simulation models and parameters
6+
% First, all classes of the package are imported with
7+
8+
clear ; close all ; clc
9+
10+
import VehicleDynamicsLongitudinal.*
11+
12+
%%
13+
% Choosing vehicle model.
14+
15+
% Choosing vehicle
16+
VehicleModel = VehicleModelNonlinear();
17+
18+
%%
19+
% Choosing simulation time span
20+
21+
T = 70; % Total simulation time [s]
22+
resol = 100; % Resolution
23+
TSPAN = 0:T/resol:T; % Time span [s]
24+
25+
%%
26+
% To define a simulation object (simulator) the arguments must be the
27+
% vehicle object and the time span.
28+
29+
simulator = Simulator(VehicleModel, TSPAN);
30+
31+
%% Run simulation
32+
% To simulate the system we run the Simulate method of the simulation
33+
% object.
34+
35+
simulator.Simulate();
36+
37+
%% Results
38+
% The time series of each state is stored in separate variables. Retrieving
39+
% states
40+
41+
X = simulator.X;
42+
V = simulator.V;
43+
44+
%%
45+
% Plotting the states
46+
47+
figure
48+
hold on ; grid on ; box on
49+
plot(TSPAN,V)
50+
xlabel('Time [s]')
51+
ylabel('Speed [m/s]')
52+
53+
%% See Also
54+
%
55+
% <../../../index.html Home>
56+
%

0 commit comments

Comments
 (0)