Initializing Simulink Model (Run a function once)

조회 수: 19 (최근 30일)
Amr
Amr 2020년 12월 27일
답변: Amr 2020년 12월 30일
Hi,
I have a model where I get output from an Aurduino Uno board. In this model, I have to make some sort of initialization (to calculate offset and calibrate readings). Typically, this will be done using a loop function that runs only once. Since I couldn't find a way to do that I used MATLAB Function block and made the signal go through before being fed to the model. The function is as follows:
function y = fcn(u)
u_calc = 0;
for i = 1:10
u_calc = u_calc + u;
pause('0.1');
end
u_calc = u_calc/10;
y = u-u_calc;
Basucally there is no way to make the loop conditionally run once, since I can't just set an "if" condition + a flag beacuse of the flag variable scope. How can I get around this? Or is there another approach to perform such task?
Your help is much appreciated.

채택된 답변

Amr
Amr 2020년 12월 30일
Well, the best work around I came to find was to use a flag with a condition to either start calculating the offset of not. The flag is set on a suitable scope by using Data Store Memory block. I got it to be something like this:

추가 답변 (2개)

saiful azimi
saiful azimi 2020년 12월 30일
Maybe you can refer to this: https://www.mathworks.com/matlabcentral/answers/363992-how-to-execute-subsystem-once-at-startup

saiful azimi
saiful azimi 2020년 12월 30일
Can you give some details on what model do you use for Arduino UNO? Do you use MATLAB code or Simulink?
  댓글 수: 1
Amr
Amr 2020년 12월 30일
편집: Amr 2020년 12월 30일
The model is for a complete control/tuning system. But to simplify, the part I am concerned with is like the following:
The readings from Sensor X needs to be calibrated on start. I want the program to be initialized by calculating the offset, then using this offset to calibrate future readings. It is simple with the Arduino IDE since I will just calculate the offset in the "setup" loop, but how can I do that in simulink?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by