% % % % % % % % % % % % % % % % % 
%
% TSFS02
% Exercise 2 - Lateral Dynamics
%
% Task 6: Identify Tire Parameters
% 
% % % % % % % % % % % % % % % % % 

% Cell 1
clear all

%% Cell 2: Load data
% Load vehicle parameters
load vehicleParameters.mat % L, l1, m, Iz, g

% Load measurement data from double lane change tests
load tireParaMeas.mat % delta, ay, Omegaz, dOmegaz, vx, vy

%% Cell 3: Calculate slip angles and lateral forces
# alphaf = 
# alphar = 
# Fyf = 
# Fyr = 

%% Cell 4: Plot Fy-alpha diagram of measurement data
fHandle = figure('position',[560 527 1116 420]);
subplot(121)
plot(alphaf*180/pi,Fyf,'.c')
title('Front suspension')
ylabel('F_{y,f}, [N]')
xlabel('\alpha_f , [deg]')
subplot(122)
plot(alphar*180/pi,Fyr,'.c')
title('Rear suspension')
ylabel('F_{y,r}, [N]')
xlabel('\alpha_r , [deg]')

%% Cell 5: Determine cornering stiffness
% Cornering stiffnesses
# Caf = 
# Car = 

% slip angle range (only for plotting)
af = [-8:.01:8]*pi/180;
ar = [-6:.01:6]*pi/180;

% Force equations, linear model
FyfL = 2*Caf*af;
FyrL = 2*Car*ar;

%% Cell 6: Plot cornering stiffness
figure(fHandle);
subplot(121)
plot(alphaf*180/pi,Fyf,'.c'); hold on
plot(af*180/pi,FyfL,'--r'); hold off
title('Front suspension')
ylabel('F_{y,f}, [N]')
xlabel('\alpha_f , [deg]')
subplot(122)
plot(alphar*180/pi,Fyr,'.c'); hold on
plot(ar*180/pi,FyrL,'--r'); hold off
title('Rear suspension')
ylabel('F_{y,r}, [N]')
xlabel('\alpha_r , [deg]')

%% Cell 7: Determine Magic Formula parameters

% Front Magic Formula parameters
# Cf = 
# Df = 
# Bf = 

% Rear Magic Formula parameters
# Cr = 
# Dr = 
# Br = 

% Force equations, Magic Formula
FyfMF = Df*sin(Cf*atan(Bf*af));
FyrMF = Dr*sin(Cr*atan(Br*ar));

%% Cell 8: Plot tire models on top of measurement data
figure(fHandle);
subplot(121)
plot(alphaf*180/pi,Fyf,'.c'); hold on
plot(af*180/pi,FyfL,'--r')
plot(af*180/pi,FyfMF,'b'); hold off
title('Front suspension')
ylabel('F_{y,f}, [N]')
xlabel('\alpha_f , [deg]')
subplot(122)
plot(alphar*180/pi,Fyr,'.c'); hold on
plot(ar*180/pi,FyrL,'--r')
plot(ar*180/pi,FyrMF,'b'); hold off
title('Rear suspension')
ylabel('F_{y,r}, [N]')
xlabel('\alpha_r , [deg]')

%% Cell 9: Save tire parameters
MFcoef.Bf = Bf;
MFcoef.Cf = Cf;
MFcoef.Df = Df;
MFcoef.Br = Br;
MFcoef.Cr = Cr;
MFcoef.Dr = Dr;
save('tireParameters.mat','Caf','Car','MFcoef')
