# Hello. If I have included delays in my code for my controller, how do I implement it so that the delays are considered when the code is executed?

1 view (last 30 days)
Danikha Shyken on 13 Dec 2023
Answered: Joel Van Sickel on 3 Jan 2024
this is the simulink model where the buck-boost controller is connected to the gate ports of each switch to power up the converter. And Im thinking that the delays in my code are not considered when the code is executed because the ouput if too low
Danikha Shyken on 13 Dec 2023
function [q1, q2, q3, q4, duty_cycle] = buckBoostController(vin)
q1 = 0;
q2 = 0;
q3 = 0;
q4 = 0;
duty_cycle = 0;
delay = 0;
% Define the duty cycle and delay values for each case of input voltage
if vin == 0.6
duty_cycle = 0.6667;
delay = 5e-7*(240.012)/360;
elseif vin == 0.77
duty_cycle = 0.7004;
delay = 5e-7*(252.144)/360;
elseif vin == 1.2
duty_cycle = 0.6000;
delay = 5e-7*(216)/360;
elseif vin == 2
duty_cycle = 0.4737;
delay = 5e-7*(170.532)/360;
end
% Calculate the duty cycles for q1, q2, q3, and q4 based on the input voltage and duty cycle
if vin == 2
% Operate in Buck Mode
q1 = duty_cycle;
q2 = 0;
q3 =1-duty_cycle;
q4 = 1.8;
elseif vin == 1.8
% Operate in Battery Voltage Mode
q1 = 1.8;
q2 = 0;
q3 = 0;
q4 = 0;
else
% Operate in Boost Mode or Battery Voltage Mode
q1 = 1.8;
q2 = duty_cycle;
q3 = 0;
q4 = 1-duty_cycle;
end
end
Danikha Shyken on 13 Dec 2023
the code above is the code for the buck-boost controller

Joel Van Sickel on 3 Jan 2024
The correct way to simulate a digital controller, so that you are incorporating the delay of the digital system is to utilize some of our discrete time simulation capabilities. I would recommend declaring a variable ts (any name will do) that you set to the update rate of your digital system that the code is running on. Then on all the inputs to the digital system, place a zero order hold block with time step of ts, then at the output of the digital system, place a unit delay with the same ts value. This will simulate the delay of the digital system. However, what you are doing is a bit more sophisticated, so you might want to use this block instead, it will simulate slower, but will give you more control over the signal: https://www.mathworks.com/help/simulink/slref/variabletimedelay.html