-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dacf154
commit e239c07
Showing
16 changed files
with
642 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
classdef Graphics | ||
% Graphics Functions for graphics generation. | ||
% | ||
|
||
methods | ||
% Constructor | ||
function self = Graphics(simulator) | ||
self.Simulator = simulator; | ||
self.VehicleColor = 'r'; | ||
end | ||
|
||
function Animation(self, varargin) | ||
|
||
vehicle_position = self.Simulator.X; | ||
vehicle_speed = self.Simulator.V; | ||
vehicle_acc = self.Simulator.A; | ||
TOUT = self.Simulator.TSpan; | ||
force_long = self.Simulator.F; | ||
speed_ref = self.Simulator.Vr; | ||
|
||
vehicle_Length = self.Simulator.Vehicle.L; | ||
vehicle_Width = self.Simulator.Vehicle.W; | ||
|
||
% Road | ||
road_Width = 10; % Road width [m] | ||
road_Margin = 2; % Road margin [m] | ||
road_Dist_Analysis = 100; % Road distance analysis [m] | ||
|
||
if nargin == 2 | ||
name = varargin{1}; | ||
else | ||
name = 'animacao'; | ||
end | ||
|
||
figure | ||
% set(gcf,'Position',[50 50 1280 720]) % 720p | ||
set(gcf,'Position',[50 50 854 480]) % 480p | ||
|
||
% Create and open video writer object | ||
v = VideoWriter(strcat(name,'.mp4'),'MPEG-4'); | ||
v.Quality = 100; | ||
open(v); | ||
|
||
for i=1:length(TOUT) | ||
subplot(3,2,1) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[0 1.2*max(vehicle_position)]) | ||
cla | ||
plot(TOUT,vehicle_position,'b') | ||
plot([TOUT(i) TOUT(i)],[0 1.2*max(vehicle_position)],'k--') | ||
xlabel('Time [s]') | ||
ylabel('Position [m]') | ||
title('Position') | ||
subplot(3,2,2) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[0 1.2*max(vehicle_speed)]) | ||
cla | ||
plot(TOUT,speed_ref,'k') | ||
plot(TOUT,vehicle_speed,'b') | ||
plot([TOUT(i) TOUT(i)],[0 1.2*max(vehicle_speed)],'k--') | ||
xlabel('Time [s]') | ||
ylabel('Speed [m/s]') | ||
title('Speed (Black=Reference, Blue=Actual)') | ||
subplot(3,2,3) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[min(vehicle_acc)-1 max(vehicle_acc)+1]) | ||
cla | ||
plot(TOUT,vehicle_acc,'b') | ||
plot([TOUT(i) TOUT(i)],[min(vehicle_acc)-1 max(vehicle_acc)+1],'k--') | ||
xlabel('Time [s]') | ||
ylabel('Acceleration [m/s2]') | ||
title('Acceleration') | ||
subplot(3,2,4) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[min(force_long)-500 max(force_long)+500]) | ||
cla | ||
plot(TOUT,force_long,'b') | ||
plot([TOUT(i) TOUT(i)],[min(force_long)-500 max(force_long)+500],'k--') | ||
xlabel('Time [s]') | ||
ylabel('Lon. force [N]') | ||
title('Longitudinal force') | ||
subplot(3,2,5:6) | ||
hold on ; axis equal ; box on | ||
cla | ||
% Position of the vehicle at current instant [m] | ||
vehicle_position_inst = vehicle_position(i); | ||
|
||
sideMarkingsX = [vehicle_position_inst-road_Dist_Analysis/2 vehicle_position_inst+road_Dist_Analysis/2]; | ||
set(gca,'xlim',sideMarkingsX,'ylim',[-road_Width/2-road_Margin +road_Width/2+road_Margin]) | ||
|
||
plot(sideMarkingsX,[+road_Width/2 +road_Width/2],'k--') % Left marking | ||
plot(sideMarkingsX,[-road_Width/2 -road_Width/2],'k--') % Right marking | ||
|
||
% Dimensions | ||
vehicle_dimension_X = [vehicle_position_inst vehicle_position_inst vehicle_position_inst-vehicle_Length vehicle_position_inst-vehicle_Length]; | ||
vehicle_dimension_Y = [+vehicle_Width/2 -vehicle_Width/2 -vehicle_Width/2 +vehicle_Width/2]; | ||
% Plotting | ||
fill(vehicle_dimension_X,vehicle_dimension_Y,'r') | ||
|
||
xlabel('Lon. distance [m]') | ||
ylabel('Lat. distance [m]') | ||
|
||
frame = getframe(gcf); | ||
writeVideo(v,frame); | ||
end | ||
|
||
close(v); | ||
|
||
end | ||
|
||
function Plot_Signals(self, varargin) | ||
|
||
vehicle_position = self.Simulator.X; | ||
vehicle_speed = self.Simulator.V; | ||
vehicle_acc = self.Simulator.A; | ||
TOUT = self.Simulator.TSpan; | ||
force_long = self.Simulator.F; | ||
speed_ref = self.Simulator.Vr; | ||
|
||
figure | ||
% set(gcf,'Position',[50 50 1280 720]) % 720p | ||
set(gcf,'Position',[50 50 854 480]) % 480p | ||
|
||
subplot(2,2,1) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[0 1.2*max(vehicle_position)]) | ||
cla | ||
plot(TOUT,vehicle_position,'b') | ||
xlabel('Time [s]') | ||
ylabel('Position [m]') | ||
title('Position') | ||
subplot(2,2,2) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[0 1.2*max(vehicle_speed)]) | ||
cla | ||
plot(TOUT,speed_ref,'k') | ||
plot(TOUT,vehicle_speed,'b') | ||
xlabel('Time [s]') | ||
ylabel('Speed [m/s]') | ||
title('Speed (Black=Reference, Blue=Actual)') | ||
subplot(2,2,3) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[min(vehicle_acc)-1 max(vehicle_acc)+1]) | ||
cla | ||
plot(TOUT,vehicle_acc,'b') | ||
xlabel('Time [s]') | ||
ylabel('Acceleration [m/s2]') | ||
title('Acceleration') | ||
subplot(2,2,4) | ||
hold on ; grid on ; box on | ||
set(gca,'xlim',[0 TOUT(end)],'ylim',[min(force_long)-500 max(force_long)+500]) | ||
cla | ||
plot(TOUT,force_long,'b') | ||
xlabel('Time [s]') | ||
ylabel('Lon. force [N]') | ||
title('Longitudinal force') | ||
|
||
end | ||
end | ||
|
||
%% Properties | ||
% | ||
|
||
properties | ||
Simulator | ||
VehicleColor | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,59 @@ | ||
classdef Simulator<handle | ||
% Simulator Vehicle dynamics simulator | ||
% 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. | ||
% Simulator The simulator receives a vehicle object, simulates its | ||
% behavior during a given time span and provides its motion during time | ||
% via its properties. Each property is a (timespan, 1) vector in which | ||
% each value represents that parameter's value in time. | ||
methods | ||
% Constructor | ||
function self = Simulator(vehicle, tspan) | ||
self.Vehicle = vehicle; | ||
self.TSpan = tspan; | ||
self.Vehicle = vehicle; % Vehicle object. | ||
self.TSpan = tspan; % Time span [s] | ||
% Initial conditions | ||
self.X0 = 0; | ||
self.V0 = 20; | ||
self.X0 = 0; % Initial position [m] | ||
self.V0 = 20; % Initial speed [m/s] | ||
end | ||
|
||
function f = getInitialState(self) | ||
% Transforms properties into a vector so it can be used by the integrator | ||
% Transforms properties into a vector so it can be used by the | ||
% integrator | ||
f = [self.X0 self.V0]; | ||
end | ||
|
||
function Simulate(self) | ||
options = odeset('AbsTol',1e-6,'RelTol',1e-6); | ||
[TOUT, XOUT] = ode45(@(t, estados) self.Vehicle.Model(t, estados,self.TSpan), self.TSpan, self.getInitialState(),options); | ||
[TOUT, ZOUT] = ode45(@(t, estados) self.Vehicle.Model(t, estados,self.TSpan), self.TSpan, self.getInitialState(),options); | ||
% retrieve states exclusive to the vehicle | ||
self.X = XOUT(:, 1); | ||
self.V = XOUT(:, 2); | ||
self.X = ZOUT(:, 1); | ||
self.V = ZOUT(:, 2); | ||
|
||
% TSpan and TOUT contain the same values, but the first is passed in columns, while the second is a vector | ||
% TSpan and TOUT contain the same values, but the first is | ||
% passed in columns, while the second is a vector | ||
self.TSpan = TOUT; | ||
|
||
% Acceleration | Force | Reference speed | ||
% Preallocating | ||
self.A = zeros(length(TOUT),1); | ||
self.F = zeros(length(TOUT),1); | ||
self.Vr = zeros(length(TOUT),1); | ||
for i=1:length(TOUT) | ||
[dz,F_l,V_r] = self.Vehicle.Model(TOUT(i),ZOUT(i,:),self.TSpan); | ||
self.A(i) = dz(2); | ||
self.F(i) = F_l; | ||
self.Vr(i) = V_r; | ||
end | ||
|
||
end | ||
end | ||
|
||
properties | ||
Vehicle % Vehicle model to be used in the simulation | ||
TSpan % a vector indicating the intervals in which the simulation steps will be conducted | ||
X0 % Initial position [m] | ||
V0 % Initial speed [m/s] | ||
X % Longitudinal position [m] | ||
V % Longitudinal speed [m/s] | ||
X0 % Initial position [m] | ||
V0 % Initial speed [m/s] | ||
X % Longitudinal position [m] | ||
V % Longitudinal speed [m/s] | ||
A % Longitudinal acceleration [m/s2] | ||
F % Longitudinal force [N] | ||
Vr % Reference speed [m/s] | ||
end | ||
end | ||
|
||
%% See Also | ||
% | ||
% <https://github.com/andresmendes/Vehicle-Dynamics-Longitudinal Home> | ||
% | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.