Embedded Matlab Function Block
3 views (last 30 days)
Show older comments
Hi All
I am using the embedded Matlab function block to write some code for a state machine. Within the block i have the following code:
function [AC_Contactor,DC_Contactor,AC_Precharge,DC_Precharge,X] = V_ctrl_AC(B1,DC_Voltage_Ref,DC_Link_Voltage)
AC_Contactor=0;
DC_Contactor=0;
AC_Precharge=0;
DC_Voltage_Ref=0;
DC_Precharge=0;
X=0;
if B1<1
AC_Contactor=0;
DC_Contactor=0;
AC_Precharge=0;
DC_Precharge=0;
elseif (B1==1 && X==0 && DC_Link_Voltage<70) || (B1== && X==1 && DC_Link_Voltage<40)
AC_Contactor=0;
DC_Contactor=0;
AC_Precharge=1;
DC_Precharge=0;
X=0;
elseif (B1==1 && DC_Link_Voltage >= 70)
AC_Contactor=1;
DC_Contactor=1;
AC_Precharge=0;
DC_Precharge=1;
X=1;
end;
What i want the programme to do is execute the first command if B1<1. Then if the variables B1, X and DC_Link_Voltage are 1, 0 and less than 70 respectively i want to execute the second statement.Then once B1 and DC_Link_Voltage are 1 and >=70 i want to execute the third statement.
I use the variable X to add hysteresis because i do not want to jump back to the second statement until the Dc_Link_Voltage has dropped back BELOW 40. Unfortunately this code does not seem to work as everytime the Dc_Link_Voltage goes back below 70 i fall back into statemnent 2. I have a Ccode version of this which works. I am new to programming in Matlab any assistance would be appreciated.;
0 Comments
Answers (1)
Kaustubha Govind
on 25 Apr 2012
That's because you've assigned X=0; at the top of the function, so your statement in Condition#3 for X=1; is effectively overwritten. A better way might be to make X persistent and assign it to zero only the first time:
persistent X;
if isempty(X) % only run the first time the function executes
X = 0;
end
Use this instead of the "X=0;" which you have above the if-block.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!