this is a matlab script that i wrote to simulate car braking ABS system

clc;
clearvars;

% Simulation parameters
timeStep = 0.01;
simulationDuration = 20; % Changed to 20 seconds
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)

% Create storage for data
brakePressureHistory = zeros(1, numberOfSteps);
wheelSpeedHistory = zeros(1, numberOfSteps);
vehicleSpeedHistory = zeros(1, numberOfSteps);
slipRatioHistory = zeros(1, numberOfSteps);
isABSActiveHistory = zeros(1, numberOfSteps);

% Define ABS function parameters
absThreshold = 0.15; % ABS intervention threshold for slip ratio
absDropPressure = 0.2; % Pressure drop per cycle when ABS is active

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

    % Gradually apply braking torque using a linear increase function
    % Update: Using a simpler approach without the RampFunction
    appliedBrake = min(currentTime / 0.5, 1) * 10000;

    % Calculate slip ratio
    % Update: Assuming slipRatio is wheelSpeed / vehicleSpeed - 1
    slipRatio = max((wheelSpeed / vehicleSpeed) - 1, 0);

    % Update ABS control logic based on the slip ratio
    if slipRatio > absThreshold
        brakePressure = max(brakePressure - absDropPressure, 0);
        isABSActive = 1;
    else
        brakePressure = min(brakePressure + 0.01, appliedBrake);
        isABSActive = 0;
    end

    % Calculate the braking force based on brake pressure
    brakingTorque = brakePressure; % Simplified model, assuming direct proportion

    % Calculate tire forces
    % Update: Using a simpler approach with assumed coefficients
    longitudinalForce = min(brakingTorque * frictionCoeff, normalLoad * frictionCoeff);

    % Update vehicle dynamics
    deceleration = longitudinalForce / vehicleMass;

    % Update speeds
    vehicleSpeed = max(vehicleSpeed - deceleration * timeStep, 0);
    wheelSpeed = max(wheelSpeed - (brakingTorque / vehicleMass) * timeStep, 0);

    % 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");

and these are the functions i used

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


this script gives me abs activity vs time =0 and the velocity is still 100 i want you to fix my script and functions and add any missing logic this code should calculate every possible thing and the car should reach 0 speed and acceleration as a perfect