Electronics > Projects, Designs, and Technical Stuff
Understanding MATLAB function blocks in Simulink
(1/1)
XaviPacheco:
Before I ask my question, let me explain what I'm trying to do. I want to design a simple system in Simulink with the features shown below. When I say simple is that I don't need complex and large block systems. I just want to represent a basic behavior.

Features:

1- A way to turn the system on or off. It could be just a variable (1 or 0) to indicate whether is off or not.

2- After turning on the system, turn a valve on during 15 seconds and then turn it off.

4- After the 15 seconds, give the setpoint level to a water tank system. Turn water pump on.

5- When the level setpoint is reached, turn a burner on until the pressure setpoint is reached.

6- Always checking important variables such as pressure and level to make some kind of decision like turn a valve on or off.

I was trying to build a Matlab function in Simulink that decides what to do, as shown in the first attachment.

I'm making the big mistake of thinking that I'm programming a microcontroller because I can't run loops inside this function and can't play with output variable values. I tried to use switch-case statements, while loops, etc, but it doesn't work as expected. For example, suppose I have the matlab function shown in the second attachment.


--- Code: ---function y = fcn(u)
%#codegen

u=0;
x=10;
z=5;
y=x;
pause(10);
y=z;
--- End code ---

The line
--- Code: ---y=x;
--- End code ---
is never executed.

Basically I need some advice on how to do this. I'm not expert in Matlab/Simulink. How can I effectively simulate a loop which continuously checks for inputs variables and updates output variables?

rstofer:
Have you looked through the Simulink->View->Library Browser?  There are a lot of time related functions including a Clock that tracks the simulation time.  I would imagine there is a way to test Clock=15 and things like that.

I would always try to use the pre-built simulation blocks.

Try Sources->Clock wired to Logic and Bit Operations->Compare To Constant wired to Sinks->Scope.  Set the constant to 1 second and Run.  You will get a scope trace with a high for 1 second and low for the  rest of the simulation time.

I have never tried to write a function for Simulink.  I still have a lot to learn!

Apparently, Simulink Coder will create C++ from Simulink models.  I haven't tried this either!

In Simulink try Help->Simulink->Block Creation  I see I can write my simulation block code in Fortran!  That's great!

boB:
I ran fcn as an .m  file and it works as expected.

Remember that all variables ran in this function are "local" to that function and will either go away if
not previously  declared, or return to their previous value after the function is run.

If you change it momentarily to something like this, and using your pause to view things (I temporarily changed x to something else),


function y = fcn(u)
%#codegen
u=0;
x=14;
z=5;
y=x;
who
x
pause(10);
y=z;


It will show what's up.

Your variables are:

u  x  y  z 


x =

    14

Operation terminated by user during fcn (line 10)


Navigation
Message Index
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod