Hi Will,
I understand that you are trying to implement a variable delay in Simulink to simulate a hardware delay of 0 ms, 4 ms, 8 ms, or 10 ms, with the delay value dynamically set from the MATLAB workspace. Also, you will need this to work inside an “atomic subsystem operating” at a fixed rate, with a specified initial condition for the delayed signal.
The current setup errors out inside an atomic subsystem because the block requires inherited sample time from a non-atomic block. The “Delay” block also causes issues when it is set to zero, since it requires an initial condition vector whose size depends on the number of delay steps.
To resolve this, I recommend using the “Variable Integer Delay” block instead which will solve the issue you are encountering. Kindly refer to the following implementation steps:
- Add the “Variable Integer Delay” block ( Simulink → Discrete → Variable Integer Delay) and set Maximum delay to the highest expected sample delay (e.g., 10 for 10 ms at 1 kHz).
- Set Initial condition to the desired starting value of the signal and enable Allow delay length to be zero.
- Convert delay in milliseconds to delay in samples.
- Feed the delay length from the workspace by creating a workspace variable “delay_ms” that you set before simulation. You can use a “MATLAB Function block” to convert “delay_ms” to “delay_samples” and connect it to the delay length input port of the “Variable Integer Delay” block. An example MATLAB Function block:
function N = ms2samples(delay_ms)
Fs = 1000; % Sampling frequency in Hz
N = uint32(round(delay_ms * Fs / 1000));
End
This method resolves the Transport Delay’s atomic subsystem compatibility issue and the “Delay” block’s zero-delay initial condition problem, while allowing full dynamic control from the workspace.
For more details on the “Variable Integer Delay” block, you can refer to the documentation below:
