03-25-2019 09:03 AM
I know what functionality I want to implement, yet I do not know how to approach this. I have commands I have loaded from a database. Some of these need to be executed every iteration until stopped, some need to be executed for a set amount of time. I want them to be executed sequentially each time. There can be many commands that run until stopped, however, only one that has a set amount of time at once. For example :
Command A should run until stopped.
Command B should run until stopped.
Command C should run 500ms.
Command D should run 800ms, but not until C has finished.
I imagined that there could be a queue of tasks, where these commands reside. So first there would be a queue with A,B,C, and after 500ms, when C is done, D is loaded in the queue, and the queue is executed all the time as a loop. The program is done once all commands are loaded and executed.
What I have so far is an array of clusters, that each contain the command and the time of execution.
Are there any design patterns for such a system, or any ideas on how to implement it? Maybe there are some resources you can point me to?
03-25-2019 11:30 AM
I feel that you need to provide more detail. For instance, commands C and D both list a runtime, and I think it needs explaining.
Are these commands something that can run for any amount of time as an input variable? For instance, turning on a power supply for exactly 500 ms, or something like that?
Or are they loops where you can check once per loop to see if they've gone over their time limit?
Or are these commands a series of steps that you want to interrupt if they take longer than 500 ms to run?
On a basic level, this seems like something that would work well with a queued state machine. Put all of your steps in a queue, with the data you put in the queue containing the command name as well as any parameters, and then feed it into a case structure with one case per command name, then put that in a While loop. Either the last command or a special "quit" command would exit the loop.
It's when you get to command C that the question I asked starts to become important. In some cases you could just run a timer for 500 ms in parallel with the loop to make sure it takes at least that long, but in other cases you need other ways of making the case continue.
03-25-2019 05:02 PM
I assume command execution is short compared to the desired loop precision. If not, long commands' loop should run in parallel and main loop (queued state machine) will send control commands there.
1) "Check" case that will check conditions for each command
2) "Execute" case loops through all commands and conditions array from shift register (either boolean or task names).
Or but I like it less
2) A, B, ... are separate cases, Check populates queue
03-26-2019 03:04 AM
The ideas given so far are all good.
How many different commands do you have that you need to execute "at the same time"?
On a different level there are some more potential approaches:
You could have every type of command handled by a subVI. For each command of one type call that VI asynchronously. Easier to implement if you don't need any result data from the command.
In case each command is basically it's own, big application (like "serve webpages for 20 minutes", "control power plant for 10 minutes"), you could also implement every command as an Actor (using e.g. Actor Framework), keeping them separate.
03-26-2019 04:48 AM
Point taken, I will try to elaborate on the idea.
My idea is that there is a loop that gets executed again and again, and each iteration it is checked whether C has gone over the time limit. If it has, it is removed and D is loaded, and again the loop goes through the list, executing A,B,D A,B,D until D timer is expired, D is removed and another command is loaded, for example E.
I do not wish to interrupt them, no.