%% Abgabe Übung 01 Fahrzeugparameter
% @Author: HTWG/ Chin-I Feng
% @Matrikel-Nr.: 300545 (EIM)

%% Environmental conditions
g = 9.81; % [m/s^2] gravity - Schwerebeschleunigung (Naturkonstante)
roh_air = 1.204; % [kg/m^3] air thickness - Luftdichte auf 0m
r_roll = 0.0125; % [-] rolling resistance coefficient - Rollwiderstandsbeiwert
m_pay = 50; % [kg] payload - Zuladung (z.B. 75kg Personen ohne Zuladung)

%% Vehicle constants (Mercedes A-Klasse)
cw_drag = 0.27; % [-] drag coefficient - Strömungswiderstandsbeiwert
A_face = 2.20; % [m^2] vehicle face - Projizierte Stirnfläche
r_air = cw_drag * A_face; % [m^2] air resistance - Luftwiderstand
m_veh = 1395; % [kg] Mittleres Leergewicht Mercedes A180 W176
m_tot = m_veh + m_pay; % [kg] Gesamtmasse des Fahrzeugs ohne Trailer

%% Vehicle powertrain
gear = {'N', 'I', 'II', 'III', 'IV', 'V', 'VI', 'R'}; % Gänge 
r_gear = [0.00, 4.31, 2.44, 1.35, 0.94, 0.82, 0.70, 3.38]; 
% Massenfaktoren der Trägheitsmomente für Gänge [N, 1, 2, 3, 4, 5, 6, R]
i_gear = [1.04, 1.32, 1.15, 1.10, 1.07, 1.06, 1.05, 1.35]; 
r_axle = 3.35; % rear axle ratio - Übersetzung Hinterachsgetriebe
fs = 10; % [Hz] Abtastfrequenz
ts = 1/fs; % [s] Abtastzeit

%% Wheel properties
r_whl = 0.32; % [m] wheel radius - Radhalbmesser
c_whl = 2 * pi * r_whl; % [m] wheel circumference - Radumfang

%% Messdaten aus Übung 4a (Schleppmoment)
% Drehzahlen für Schleppmoment (U/min) (Breakpoints für RPM)
rpm_drag = [0, 1, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500];

% Schleppmoment (Nm)
torque_drag = [0, 10.27, 10.53, 10.93, 11.46, 12.12, 12.91, 13.84, 14.90, 16.09, 17.41, 18.86, 20.45];

%% Messdaten aus Übung 4b (Motormoment)
% Drehzahlen für Motormoment (U/min) (Breakpoints für RPM)
rpm_motor = [0, 608, 640, 736, 800, 992, 1248, 1504, 1760, 2016, 2240, 2496, 2752, 3008, 3264, 3488, 3744, 4000, 4512, 4992, 5248, 5500];

% Fahrpedalstellungen (Breakpoints für %ACC)
acc_motor = [0, 0.13, 6.25, 12.5, 18.75, 25, 31.25, 37.5, 43.75, 50, 56.25, 62.5, 68.75, 75, 81.25, 90.63, 100];

% Motormoment-Kennfeld (2D-Kennfeld)
torque_motor = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
    0, 65.16, 63.91, 53.88, 45.13, 15, 5.69, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0;
    0, 95.22, 93.38, 78.31, 67.94, 39.56, 19.47, 6.34, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0;
    0, 125.28, 124.44, 111.44, 101.91, 77.19, 51.47, 37.72, 28.66, 21.97, 17.47, 14.47, 12.22, 11.47, 10.94, 10.56, 10.19, 9.84, 9.31, 8.88, 5.53, 0;
    0, 126, 126, 126, 126, 109.19, 79.09, 55.88, 43.53, 34.91, 29.09, 24.84, 21.69, 20.06, 18.88, 17.97, 17.06, 16.28, 15.03, 14.06, 6.22, 0;
    0, 126, 126, 126, 126, 126, 101.28, 79.63, 64.31, 53, 45.34, 39.41, 34.88, 32.13, 29.97, 28.38, 26.75, 25.34, 23.03, 21.28, 7.19, 0;
    0, 126, 126, 126, 126, 126.5, 131.09, 109.69, 91.28, 77.69, 68.5, 60.75, 54.56, 50.19, 46.63, 43.94, 41.22, 38.91, 35.03, 32.13, 8.66, 0;
    0, 126, 126, 126, 126, 131.47, 182.66, 155.41, 131.56, 113.91, 102.03, 91.31, 82.72, 75.97, 70.31, 66.13, 61.91, 58.22, 52.16, 47.59, 10.75, 0;
    0, 126, 126, 126, 126, 133.41, 202.47, 199.13, 173.38, 151.53, 136.81, 123.19, 112.16, 103.16, 95.63, 89.97, 84.31, 79.38, 71.25, 65.16, 13.13, 0;
    0, 126, 126, 126, 126, 133.41, 208.5, 248.19, 217, 189.5, 170.91, 153.94, 140.25, 129.22, 119.97, 113.09, 106.16, 100.16, 90.19, 82.75, 15.5, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 257.94, 254, 244.53, 202.44, 182.63, 166.75, 154.31, 143.97, 136.25, 128.47, 121.69, 110.56, 102.19, 18.13, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.69, 289.16, 257.16, 231.78, 209.5, 191.75, 178.25, 167.09, 158.75, 150.41, 143.13, 131.06, 122.06, 20.81, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.75, 290.78, 285.94, 259.66, 235.19, 215.75, 201.5, 189.78, 181.03, 172.25, 164.59, 151.97, 142.5, 23.59, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.75, 291, 291, 282.56, 260.13, 239.22, 224.47, 212.44, 203.41, 194.38, 186.5, 173.5, 153.06, 24.91, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.75, 291, 291, 291, 282.41, 262.47, 247.44, 236.22, 226.09, 216.97, 209, 186.31, 154.03, 24.91, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.75, 291, 291, 291, 291, 281.31, 266.06, 253.38, 241.63, 230, 218.72, 186.69, 154.03, 24.91, 0;
    0, 126, 126, 126, 126, 133.41, 209.59, 262.75, 291, 291, 291, 291, 281.31, 266.06, 253.38, 241.63, 230, 218.72, 186.69, 154.03, 24.91, 0;
];

%% Messdaten aus Übung 4c (Reibmoment)
% Drehzahl (RPM)
rpm_loss = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500];

% Drehmoment (EngTrq)
eng_trq = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300];

% Reibmoment-Kennfeld (2D-Kennfeld)
torque_loss = [
    0.95, 0.90, 0.87, 0.82, 0.81, 0.76, 0.70, 0.69, 0.70, 0.66, 0.62, 0.59;
    1.30, 1.24, 1.18, 1.12, 1.08, 1.03, 0.97, 0.95, 0.94, 0.89, 0.85, 0.80;
    1.62, 1.56, 1.49, 1.43, 1.35, 1.28, 1.23, 1.21, 1.18, 1.12, 1.06, 1.01;
    1.98, 1.89, 1.80, 1.71, 1.62, 1.55, 1.48, 1.45, 1.42, 1.35, 1.28, 1.22;
    2.35, 2.24, 2.15, 2.04, 1.94, 1.85, 1.77, 1.74, 1.71, 1.63, 1.56, 1.48;
    2.76, 2.63, 2.51, 2.38, 2.27, 2.18, 2.07, 2.03, 1.99, 1.92, 1.84, 1.77;
    3.13, 2.99, 2.87, 2.75, 2.60, 2.50, 2.38, 2.34, 2.28, 2.21, 2.13, 2.04;
    3.52, 3.37, 3.23, 3.09, 2.93, 2.81, 2.68, 2.63, 2.57, 2.48, 2.38, 2.29;
    3.91, 3.75, 3.58, 3.42, 3.24, 3.12, 2.98, 2.90, 2.82, 2.73, 2.63, 2.54;
    4.34, 4.15, 3.96, 3.77, 3.58, 3.43, 3.29, 3.19, 3.11, 3.01, 2.93, 2.83;
    4.80, 4.57, 4.34, 4.10, 3.91, 3.74, 3.59, 3.48, 3.39, 3.30, 3.22, 3.14;
    5.25, 4.99, 4.71, 4.45, 4.26, 4.05, 3.90, 3.76, 3.67, 3.58, 3.51, 3.44;
    5.71, 5.41, 5.09, 4.79, 4.60, 4.36, 4.21, 4.05, 3.95, 3.87, 3.80, 3.74
];

% Das Ratio-Array für alle Gänge
gear_ratios_to_neutral = [1, 1.0526, 1.1368, 1.2421, 1.3263, 1.4737, 1.6421, 1.1895];

%% Bremspapameter aus Übung 5
r_brems = 0.1;   % [m] Mittlerer Radius der Bremsscheibe
i_brems = 12;    % Kraftverstärkung der Bremsanlage
mu_brems = 0.8;  % Reibungskoeffizient der Bremsbeläge
n_brems = 2;     % Anzahl der Bremskolben pro Rad