- Simulink Bus objects: You can create a “Simulink Bus” object that contains all the variables you need for your internal state. Each state can have its own instance of the bus object, allowing you to keep the variables local to each state. You can then use “Simulink Bus Selector” blocks to access the specific variables you need within each state. https://www.mathworks.com/help/simulink/ug/when-to-use-bus-objects.html#brin2jr-1
- Simulink Function blocks: “Simulink Function” blocks allow you to define reusable functions that can be called from multiple locations within your Simulink model. You can create a “Simulink Function” block for each state and define local variables within each function block. This way, each state will have its own set of local variables. https://www.mathworks.com/help/rtw/ug/generate-code-for-a-model-with-simulink-functions.html
- S-Function block: If you require more flexibility and control over the internal state variables, you can consider using a MATLAB “S-Function” block. S-Function blocks allow you to write custom C or MATLAB code to implement your state machine logic. You can define and update your internal state variables directly within the “S-Function” block’s code. https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.html
Using data internal to simulink state block as condition in transition of stateflow chart
21 views (last 30 days)
Show older comments
Hello,
I have made a finite state machine as a stateflow chart within a simulink model. The states are all "Simulink States." This is nice because the inputs and outputs of the chart as a whole can each be the inputs and outputs of the states. So as it moves between the simulink states, it is just transitioning between operating modes (see https://www.mathworks.com/help/stateflow/ug/about-simulink-states.html).
I am currently using the method from that link where I access the simulink subsystem state variables. The problem is that I do not have any blocks in the simulink functions with states I care about, so I am just making a delay block and reading from its internal state. The delay block then feeds into a terminator. This feels like a hack. I wish there was a dedicated "internal state data block" that I could write to. This technically works, I am just looking to see if there is a better way.
I've considered using a "data store memory block," but that will be messy since all of my states are writing to a variable that I want to stay local to each of the states (plus I can't have a data store memory in a chart, and I would need to put the chart in a subsystem).
I've considered using outputs, but then that gives my chart outputs which really clutters things.
I've tried making these conditions as simulink function calls in the chart level, but they end up having so many inputs that it isn't worth it.
I've tried using events, but I am not sure this is the right call. I basically made a chart in the simulink state block that emitted an event when its called. This doesn't feel right because I still want my transitions from the simulink state block to be evaluated in order as they usually are (maybe it would still work that way and I am misunderstanding how events work)
I've tried mapping an output port from the state to a local data in the chart level (this would be an awesome solution, and one of the more intuitive ones), but it only lets me map outputs of simulink state blocks into outputs of the chart.
Anyway, I hope this was clear. Thank you for any help you can give.
0 Comments
Answers (1)
Yatharth
on 29 Sep 2023
Hi Samuel,
I understand that you have implemented a finite state machine using Stateflow within a Simulink model. Simulink States provide a convenient way to represent operating modes and transition between them.
Regarding your concern about accessing Simulink subsystem state variables, it is understandable that using a “Delay” block as a workaround might feel like a hack. And I also understand your concern about using a “Data Store Memory” block and the potential messiness it may introduce, especially if you want to keep the variable local to each state. While there isn't a dedicated "Internal State Data” block in Simulink, there are alternative approaches you can consider:
I hope this helps.
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!