Desktop Real-Time slow initialization

5 views (last 30 days)
william
william on 2 Jul 2024
I am using the input from an mcc usb-231 daq to feed data into my simulink model through a function block. The issue I am running into is that when the simulation first starts up. it takes almost an entire minute before my simulation begins simulating through the desktop realtime kernal.
Here is my function block.
function output = fcn()
coder.extrinsic('daq','read','addinput'); %lets simulink know to read commands as MATLAB commands
d = daq('mcc');
addinput(d,'Board0',0,'Voltage')
data = read(d,'OutputFormat','Matrix');
output = zeros(1,1);
output = data(1,1);
Is there a way to get rid of this initialization buffer at the start?

Answers (1)

Manikanta Aditya
Manikanta Aditya on 5 Jul 2024
Hi,
To improve the initialization time and overall performance, you can try the following workaround approach:
  1. Initialize the DAQ session outside of the function block, perhaps in a MATLAB Function block that runs only once at the start of the simulation.
  2. Use a persistent variable in your function block to maintain the DAQ session across function calls.
Here's how you might modify your code:
  • Create a new MATLAB Function block for initialization:
function d = initDAQ()
coder.extrinsic('daq', 'addinput');
d = daq('mcc');
addinput(d, 'Board0', 0, 'Voltage');
  • Modify your existing function block:
function output = fcn(d)
coder.extrinsic('read');
persistent daqSession
if isempty(daqSession)
daqSession = d;
end
data = read(daqSession, 'OutputFormat', 'Matrix');
output = data(1,1);
  • In your Simulink model, use the initialization function to create the DAQ session, then pass it to your main function block.
This approach should significantly reduce the initialization time because you're setting up the DAQ session only once at the start of the simulation, rather than every time the function block is called.
I hope this helps!
  2 Comments
william
william on 8 Jul 2024
I have incorporated persistance into my function block, but the biggest I learned is that with the data acquisition read is not syncing with the real time kernal and causing a bunch of miss ticks and slowing down the simulation.
Manikanta Aditya
Manikanta Aditya on 9 Jul 2024
  • Perform DAQ read operations in a separate MATLAB Function block that runs asynchronously from the real-time kernel. This will help prevent the DAQ read latency from affecting the real-time execution.
  • Implement a circular buffer to store the DAQ data. The DAQ read operation can write data to this buffer at its own pace, while the real-time kernel can read data from the buffer without waiting for the DAQ operation to complete.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!