Can I store and access a data with two different sampling time (First store adequate data and the fetch with higher speed) in real time in Simulink?

3 次查看(过去 30 天)
I need to stora a data (real number) for entire time of simulation with 2 milliseconds of sampling time. After a certain time of simulation I need the data in FIFO mode to be used in a faster loop of say 0.2 milliseconds. Queue block of simulation does not support Push and Pop operation with two sampling rate i suppose and so can not use it. Please suggest me a solution. I hope this can be done in Simulink.

回答(1 个)

Abhas
Abhas 2024-5-22
Hi Rintu,
It requires a bit of a workaround because Simulink generally expects consistent sample times within a given subsystem. A combination of blocks that allow you to store data at one rate and then read it at a faster rate when needed can be used. You can follow the below MATLAB Function Block Implementation to achieve so:
  1. Initialization: Initialize a global variable or persistent variable within the MATLAB Function block to act as your data buffer.
  2. Writing Data (2 ms rate): On each execution at the slower rate, append the incoming data to your buffer.
  3. Reading Data (0.2 ms rate): Since you're reading faster than writing, manage the buffer size and handle cases where the buffer might be empty.
  4. Synchronize and Manage the Sampling Rate:  Use control logic in Simulink that signals the MATLAB Function block when to read or write based on the simulation time. Ensure the part of your model that reads data operates at the faster rate.
Here's an example MATLAB Code to implement this:
function out = fifoBuffer(in, control, timestep)
persistent buffer bufferIndex;
if isempty(buffer)
% Initialize the buffer with a large enough size and index
buffer = zeros(1, 5000); % Adjust based on expected data size
bufferIndex = 1;
end
if control == 1
% Store data at 2 ms rate
buffer(bufferIndex) = in;
bufferIndex = bufferIndex + 1;
out = 0; % Output doesn't matter during storage
elseif control == 2 && bufferIndex > 1
% Output data at 0.2 ms rate, FIFO mode
out = buffer(1);
buffer(1:bufferIndex-2) = buffer(2:bufferIndex-1); % Shift data
bufferIndex = bufferIndex - 1;
else
out = 0; % Default output, or handle underflow
end
end
In the above code, "control" is a flag that determines whether the block should store data ("control" == 1) or output data in FIFO mode ("control" == 2) and "timestep" could be used to adjust the operation based on the simulation time.
You may refer to the following documentation links to have a better understanding on sampling time:
  1. https://www.mathworks.com/help/simulink/ug/how-to-specify-the-sample-time.html
  2. https://www.mathworks.com/help/simulink/sample-time.html

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by