How do I create a switch statement using 1 variable rather than multiple separate ones in Stateflow?
Mostrar comentarios más antiguos
I'm tyring to setup a stateflow chart in simulink right now, and a problem I am running into is the way the cases are defined. The goal is to recreate a system we already have in place using Stateflow, and that requires the state machine's cases to have the ability to be changed outside the state machine itself.
As an example, (EDITED)
int ExampleCase = 0; % global defined at the start of the program
main(){
while(1)
% Main while loop
ExampleFunction();
Reset();
end
}
ExampleFunction(void){
switch(ExampleCase)
{
case 0:
% Case 0 Functions
if (%Some condition is met)
ExampleCase = 1;
end
break;
case 1:
% Case 1 Functions
if (//Some condition is met)
ExampleCase = 2;
else if (//some other condition is met)
ExampleCase = 0;
end
break;
case 2:
% Case 2 fucntions
if (//some condition is met)
ExampleCase = 1;
end
break;
}
}
Reset(void)
{
if (%FailureFlag or something similar is tripped)
ExampleCase = 0; % RESET THE MACHINE
end
}
In our current project, we would call on this switch statement each iteration of our main while loop and it would go through up to case 2, but there are times where the machine needs to be sent to a different case outside of this statement.
Somewhere else in the code ExampleCase would be set to something other than what the current active case is (i.e. if the switch statement was currently at case 2, which would be a normal operating state, an interrupt outside of this statement could set ExampleCase to 0). This can be done easily in code, but to simulate it in Stateflow has been a bit difficult.
How can we force the input variable of the statement, ExampleCase, to be a specific value? Rather than whatever it was set to during the previous run of the stateflow chart. Could this be done with the default transitions?
EDIT:
So I reworked the code snippet for a better idea of what I am trying to do, as in heinsight it had little info. I may be confusing the terminology for switch-cases and state machines, as I had thought they were similar. But the general idea is that I have a main program loop with a global variable that represents the current state of that machine. While in Stateflow I can go through the steps inside the chart itself, we have other functions in the program that could influence the global state variable, i.e. a reset function. I attatched below a stateflow chart with how I was attempting to setup the machine. The question I have, is whether it is possible to recreate what I have in that code snippet example into something in stateflow? Or, would it be more benificial to create it in a different method? Unfortunatley, the actual code I am attempting to recreate in this model cannot be changed, so I need to work in that format. Hopefully this info helps, thank you for taking the time

2 comentarios
Jonas
el 13 de Oct. de 2021
You mention you are recreating some other code in Stateflow, and you quote a switch-case. Is this because it is a switch-case in that external code representing a statemachine? If that's the case, you would want to ditch the switch-case and create proper states and transitions in Stateflow. In code generation for example, the states of a Stateflow end up as a switch case.
Just a comment on my side because I have seen people trying to recreate switch-cases from other code, which actually represent state machines, and then it is much better to implement it using States and Transitions instead of a switch-case in Stateflow.
Besides this, it is a bit difficult to visualise what you want to do without a first try of your Stateflow chart.
Thomas MacGregor
el 13 de Oct. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Applications en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

