clc;
clearvars;

% Simulation parameters
timeStep = 0.01;
simulationDuration = 5;
numberOfSteps = simulationDuration / timeStep;

% Initial conditions
vehicleSpeed = 100 / 3.6; % Initial speed (100 km/h in m/s)
wheelSpeed = vehicleSpeed; % Assuming no slip initially
brakePressure = 0; % No braking at the start
normalLoad = 4000; % Normal force in Newtons
frictionCoeff = 0.7; % Coefficient of friction
vehicleMass = 1500; % Vehicle mass in kg (example value)

for step = 1:numberOfSteps
currentTime = step * timeStep;

% Gradually apply braking torque using a ramp function
appliedBrake = RampFunction(currentTime, 0.5) * 10000;

% Calculate slip ratio
slipRatio = SlipFunction(wheelSpeed, vehicleSpeed);

% ABS control logic
[brakePressure, isABSActive] = ABSCycle(slipRatio, brakePressure, 0.15);

% Calculate the braking force
brakingTorque = BrakingTorque(min(brakePressure, appliedBrake), frictionCoeff, 0.3);

% Calculate tire forces
[longitudinalForce, ~] = TireModel(slipRatio, normalLoad, frictionCoeff);

% Update vehicle dynamics
% This is where you would include vehicle and wheel dynamics equations
% Assuming simplified linear deceleration for this example
deceleration = longitudinalForce / vehicleMass;

% Update speeds
vehicleSpeed = max(vehicleSpeed - deceleration * timeStep, 0);
wheelSpeed = vehicleSpeed - slipRatio * vehicleSpeed; % Simplified relationship between wheel and vehicle speed

% Store data for post-processing and plotting
brakePressureHistory(step) = brakePressure;
wheelSpeedHistory(step) = wheelSpeed;
vehicleSpeedHistory(step) = vehicleSpeed;
slipRatioHistory(step) = slipRatio;
isABSActiveHistory(step) = isABSActive;
end

% Post-processing: plot results
timeVector = (0 : numberOfSteps - 1) * timeStep;
figure;
subplot(3,1,1);
plot(timeVector, vehicleSpeedHistory * 3.6);
xlabel(“Time (s)”); ylabel(“Vehicle Speed (km/h)”);
title(“Vehicle Speed vs. Time”);

subplot(3,1,2);
plot(timeVector, wheelSpeedHistory * 3.6);
xlabel(“Time (s)”); ylabel(“Wheel Speed (km/h)”);
title(“Wheel Speed vs. Time”);

subplot(3,1,3);
plot(timeVector, isABSActiveHistory);
xlabel(“Time (s)”); ylabel(“ABS Active (0 or 1)”);
title(“ABS Activity vs. Time”);

linkaxes(findall(gcf,“type”,“axes”), “x”);


this code gives me absactivity vs time =0 and velocity and wheels speed =100 over the 5 seconds of simulation fix it for me and make the simulation time 20 seconds

these are my functions

Ramp.m

function [PL_RAMP] = Ramp(PL, PL_0, T1, PL_PRE, CONTROL)

% Define the step size for changing the pressure
PLINE_STEP = 0.2;

% Pressure Adjustment Based on Control Flag
% CONTROL = 1: Increase Pressure
% CONTROL = -1: Decrease Pressure
% CONTROL = 0: Maintain Current Pressure
if CONTROL == 1 
    PL_RAMP = PL_PRE + PLINE_STEP;  % Increase pressure by PLINE_STEP
elseif CONTROL == -1 
    PL_RAMP = PL_PRE - PLINE_STEP;  % Decrease pressure by PLINE_STEP
elseif CONTROL == 0 
    PL_RAMP = PL_PRE;               % Keep pressure constant
end

% Initial Time Condition Check
% If T1 is 0 (start of simulation), set pressure to initial value PL_0
if T1 == 0
    PL_RAMP = PL_0;
end

% Pressure Boundary Conditions
% Ensure PL_RAMP does not exceed maximum pressure PL
% Ensure PL_RAMP does not fall below zero
if PL_RAMP > PL
    PL_RAMP = PL;
elseif PL_RAMP < 0 
    PL_RAMP = 0;
end

end



tiremodel.m

function [FX] = tiremodel(I, N, MU_MAX)

% Tire Slip Ratio vs. Normalized Force Lookup Table
% Column 1: Slip ratio (I)
% Column 2: Corresponding normalized force
TIRE_W = [0 0; -0.05 0.6; -0.1 0.8; -0.15 0.95; -0.2 1; -0.25 0.97; -0.3 0.94; -0.4 0.87; -0.6 0.8; -0.8 0.75; -1 0.7];

% Longitudinal Force Calculation (FX)
% Interpolate the normalized force from the lookup table based on the slip ratio (I)
% Multiply the interpolated value by the normal force (N) and the maximum friction coefficient (MU_MAX)
FX = N * (interp1(TIRE_W(:,1), TIRE_W(:,2), I)) * MU_MAX;

end


slip.m

function [FB_S, SKID] = slip(FB, N, MU_MAX, MU_S)

% Checking Skid Condition Based on Applied Braking Force and Maximum Frictional Force
% FB: Applied Braking Force
% N: Normal Force
% MU_MAX: Maximum Friction Coefficient
% MU_S: Static Friction Coefficient

% Determine if Tire is Skidding
if FB <= MU_MAX * N
% No Skid: Effective Braking Force equals Applied Force
FB_S = FB;
SKID = ‘NO’; % Set SKID status to ‘NO’ indicating no skidding
else
% Skid: Effective Braking Force limited by Static Frictional Force
FB_S = MU_S * N;
SKID = ‘YES’; % Set SKID status to ‘YES’ indicating skidding
end

end


Braking_torque.m

function [T_f, T_r] = Braking_torque(Pl, Af, Ar, mu_f, mu_r, N_f, Rmean_f, Rmean_r)
FRIC_F = Pl * Af * mu_f * N_f;
FRIC_R = Pl * Ar * mu_r * N_f;
TFRIC_F = FRIC_F * Rmean_f;
TFRIC_R = FRIC_R * Rmean_r;
T_f = TFRIC_F;
T_r = TFRIC_R;
end


ABScycle.m

function [control] = ABScycle(a1, a2, a3, a4, aw, aw_pre)

% Determine the direction of change in angular acceleration
if (aw - aw_pre) <= 0
slope = -1; % Negative slope indicates deceleration
else
slope = 1; % Positive slope indicates acceleration
end

% ABS logic based on current angular acceleration (aw) and its change (slope)
if aw <= 0
% If angular acceleration is negative or zero (decelerating wheel)
if aw > a1
% If angular acceleration is greater than threshold a1
if slope == -1
control = 1; % Increase brake pressure
elseif slope == 1
control = 0; % Hold brake pressure
end
elseif aw <= a1
% If angular acceleration is less than or equal to threshold a1
if slope == -1
control = -1; % Decrease brake pressure
elseif slope == 1
control = 0; % Hold brake pressure
end
end
else
% If angular acceleration is positive (accelerating wheel)
if aw <= a4
% If angular acceleration is less than or equal to threshold a4
if slope == 1
control = 0; % Hold brake pressure
elseif slope == -1
% If the wheel is starting to decelerate
if aw >= a3
control = 0; % Hold brake pressure
else
control = 1; % Increase brake pressure
end
end
elseif aw > a4
% If angular acceleration is greater than threshold a4
control = 1; % Increase brake pressure
end
end
end