04-09-2020 05:22 AM
Hello!
I have recently started developing an application and currently I am stuck in the architecturing stage. I want the state machine to have the following states.
The state machine will start in the first state and will move into the idle state.
What I want to do (but I havent figured how to do it yet) is to force the SM to run in the Idle state until the user sends a command (in this case either run or shutdown). I would like to have the same behaviour in the Run state. The "problem" is that these states would have several while loops running inside, and this is what is driving me mad 🙂
So I was thinking about what would be the best architecture but I couldn't think about the best design. This is what I have thought so far:
a) SM with 4 states (init, idle, run & shutdown)
b) SM with 2 states (init & other)
Event structure (idle, run & shutdown)
c) Queued SM
The problem I find and dont know how to solve is how can I stop all the while loops inside one state and force to move to the next one. Maybe should I use dynamic user events with one of the previous architectures?
What would be your suggestion?
Thanks,
04-09-2020 06:07 AM
Depending on your requirements, a simple Global variable could solve it. Else you can use User events, Occurance or similar.
04-09-2020 06:51 AM
It's hard to say without seeing your code. Typically you would not have any while loops running inside your states. If you look at what is going on in those loops you will likely find that you have additional states for your state machine since the states that you have defined so far a very high level.
If you need to have different loops running in parallel then a queued message handler is often a nice option. You could then just send a message to each loop to shut down.
04-09-2020 07:03 AM
@Kalandrak wrote:
The "problem" is that these states would have several while loops running inside, and this is what is driving me mad 🙂
This almost makes me think you should be using the Actor Framework. I am not personally a fan of it since it feels like too much OOP, but what you describe here is what it was designed for.
04-09-2020 08:18 AM
I would attach my code, but at this moment, I am thinking in the architecture and havent done any coding yet (as I am not sure which solution could I implement).
It is true that if I put a while loop then is not a real state machine, but it is the only way I have found to suit my application.
I will add some more information about my application, maybe it would lead to a change in the overall architecture. This application would be deployed to a target, hence the initialization and shutdown states to correctly operate the device.
The state machine is in idle, until it receives a signal to start the test.
Then the test runs and after a certain time stops and gets back to idle state.
Here the key point is that during the test, I would like that the user is capable of aborting the test and returning back to idle.
And then in idle, select to run another test or shutdown safely.
Would you advise using a queued message handler inside the idle and run states? (or maybe is not good practice?)
-----------------------------------
At this moment, I am exploring user events and notifiers in order to be able to force to stop the while loops.
I will also explore the Actor Framework (although I have never heard of it). Thanks!
04-09-2020 08:27 AM
@Kalandrak wrote:
It is true that if I put a while loop then is not a real state machine, but it is the only way I have found to suit my application.
You put the code inside of the loop as a state. You can repeat a state until the right parameter is reached.
04-09-2020 11:00 AM
@crossrulz wrote:
@Kalandrak wrote:
It is true that if I put a while loop then is not a real state machine, but it is the only way I have found to suit my application.
You put the code inside of the loop as a state. You can repeat a state until the right parameter is reached.
Yes, you already have a while loop. You do not need another.
04-09-2020
12:31 PM
- last edited on
03-25-2025
07:02 PM
by
Content Cleaner
If you want a good architecture, i would suggest that you can use a SM principle like JKI SM.
This SM is just a reference for your design. This SM is well suitable for host apps, but for RTOS apps you can design in your own way like how they are doing with Enum Data Types.