How to implement this c-code: if(Flag == 1) {Flag = 0;} ?

I used a signal (this is what I needed) for Flag at an input port of stateflow, but Flag is not recognized inside the stateflow. If I add an input data in the stateflow, it will not be allowed to change the value (through a transit with a statement {Flag=0;} inside the stateflow).
Please help. Thanks!

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 14 de Oct. de 2011

0 votos

This is one of the difference between C and Simulink. In C, you can re-use the variable name. In Simulink, the signal name represents the data flow. You can't re-use the signal name. You have to declare an output for the Stateflow and then assign value accordingly.

12 comentarios

Charles Wang
Charles Wang el 14 de Oct. de 2011
Can I use an output data Flag1 to carry the value of Flag, then add the same signal Flag on the output port of the stateflow chart?
In this case the chart has the same signal on its input and output ports. Will this be a problem when generating code?
Fangjun Jiang
Fangjun Jiang el 14 de Oct. de 2011
You certainly can use Flag1 to carry the value of Flag. But you can't add Flag as an output since it's already an input.
Don't think Flag or Flag1 as variables. They are signals, as the signals in a circuit board.
There won't be any problem for code generation. The code might just not be the same as you would write it manually.
Charles Wang
Charles Wang el 17 de Oct. de 2011
If I name the output port Flag1 and add Flag as the signal name outside the stateflow chart output, I got an error because of the mismatch (Flag is already the name of the input signal).
In my case Flag is actually the data from the board level driver, I always use Signal to represent it. My purpose is to modify this data. Is there a way to do "if(Flag == 1) {Flag = 0;}" using the stateflow?
Please help. Thanks.
Fangjun Jiang
Fangjun Jiang el 17 de Oct. de 2011
In Stateflow, if you define Flag as input, you can not do "if(Flag == 1) {Flag = 0;}".
If you define Flag as output, you can do "if(Flag == 1) {Flag = 0;}".
Charles Wang
Charles Wang el 18 de Oct. de 2011
Thank you, Fangjun. This really works!
Charles Wang
Charles Wang el 26 de Oct. de 2011
I have a more common situation about this question. Often the time this variable (global) will be set to 0 or 1 in different blocks in a model. I found:
(1) I can't assign the same name Flag to a second output port in the same model. I have to assign different port names (cause problem since there is only one variable?), but using the same signal name is allowed.
(2) If I use several GOTO blocks and 1 FROM block ending at 1 output port, this is not allowed. (For input ports, it's allowed.)
This is another awkward situation in Simulink vs C programming. Please help. Thanks!
Fangjun Jiang
Fangjun Jiang el 26 de Oct. de 2011
Each block in a subsystem needs to have a unique name so you can't have multiple blocks named as Flag in a subsystem, but you could have many Inport or Outport named as Flag in a model but in different subsystems. A signal has only one source but can be used in many destinations. Goto and From blocks are for connection. You can have only one Goto block (source for the connection) that has "Flag" as the goto tag, but you can have many From blocks (destination for the connection) that have "Flag" as the goto tag. For your purpose, you many need to consider the Memory Store/Read/Write block.
Try to use Simulink in its natural way, not to twist it trying to match the C code behavior. Sometimes you have to accept that apple and orange are different.
Charles Wang
Charles Wang el 31 de Oct. de 2011
Thanks for the advice!
However in my application in the same level, there are two outputs (in two different subsystems) with the same (global) name Flag which needs to be written into a Memory-Write. I got the same error as before, because I have to use two GOTO blocks pointing to one FROM block ending at the Memory-Write.
Is there a way to get around it? Thanks!
Fangjun Jiang
Fangjun Jiang el 31 de Oct. de 2011
If you are using Memory Store Write block, why do you still need the Goto and From block? You can use Memory Store Write block wherever need to write and use the Memory Store Read block wherever you need to read. Check the help of Memory Store Memory/Read/Write block, there are different options regarding the scope of the memory.
Charles Wang
Charles Wang el 1 de Nov. de 2011
Thanks for the advice! (I found that extra work was needed for initializing Memory block and dealing with "first read then write, etc" errors.)
For implementing "if(Flag == 1) {Flag = 0;}", since there are signals Flag on both my inport and output ports, I also found that I could name the input port "Check_Flag" while typing "[Flag==1]" on a transition, i.e. a signal is visible inside a stateflow chart. It is misleading that Stateflow calls the name of a port "data". I don't think that the port name would go into the generated code in my case. Correct me if I am wrong.
Fangjun Jiang
Fangjun Jiang el 1 de Nov. de 2011
You are right. Port name is used to identify the port block. The generated code will not necessarily use port name for variable name.
Try with connecting Unit Delay(1/z) block and take the feedback.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Functions en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Oct. de 2011

Comentada:

el 14 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by