08-27-2008 10:41 AM
Is it possible to use a FOR loop to create multiple instances of an asynchronous statechart?
See attached code, it works fine with only one instance in the array, but as soon as one expands the array to two instances, error 1100 occurs.
Thanks in advance...
08-27-2008 10:46 AM
Hi tobrr,
Please go through this thread
08-27-2008 11:17 AM
I already went through that thread, but it did not quite help me with my problem.
As you can see in my VIs I am calling the run Statechart VI before sending any triggers. And as I said, with only one instance it works fine...
still confused...
08-27-2008 12:01 PM
Hi TOBBR,
There are a couple reasons for the problems.
The first is that the run statechart node has an optional "Init?" parameter, which uses the First Call primitive to initialize the statechart the first time it calls. This means for the first name you provide, the run statechart node initializes, but for the second, it does not. This means that when you try to talk to send an event to the second statechart, its resources have not been initialized so you get an error.
The second issue you would have, even if you appropriately manually initialized the second call, is that the run statechart node stores all of the data for the instance, so it cannot be shared between two different instances.
This doesn't mean you can't do what you want to, though, it just means there is a little more work to be done.
What you can do is wrap your run statechart node in a reeentrant subVI. Have this subVI provide the same inputs, outputs and instance name terminals.
Then each subVI pointing to this reentrant VI will get its own instance of the statechart. To spawn multiple instances of this VI from a for loop, you would have to use VI server to open references to them and run them via the call by reference node. If you don't know how to do that let me know, and I should be able to draw up an example pretty quickly.
08-28-2008 04:29 AM
09-02-2008 01:07 PM
aggieNick02 wrote:Hi TOBBR,
"If you don't know how to do that let me know, and I should be able to draw up an example pretty quickly. "
A quick example would be extremely useful for a lot of us, I think. For myself, definitely. I'm not using a for-loop but it's the same situation - I have a motor controller class, with multiple instances, and each motor instance needs an instance of my motor control statechart. But a simple for-loop example should be sufficient.
Thanks,
Mike
09-02-2008 04:21 PM
Here is a quick and dirty example.
For this example, I chose to use a synchronous statechart because it actually translates much more naturally to the idea of spawning several instances of a statechart and then talking to the instances. You can use an asynchronous statechart as well, but it makes the spawning and talking processes significantly more complicated/involved (I can talk more about that in another post if anyone is really interested). Anyways,
In the attached files, spawner.vi is the top-level VI. It begins by creating a number of instances of a simple statechart. The statechart doesn't do much. It just throws up a one button dialog with a particular string when a certain transition is taken.
With the instances spawned, we then enter into a while loop with event structure, which waits for user input to cause us to talk to one of the statecharts. You can pick which statechart to talk to (numbers 0,1,... n-1, if you spawned n statechart instances). When you press the talk button, we will send a "SayHi" trigger to that statechart. It will take one step and throw a dialog telling you hi and what statechart number you are talking to (to prove you are actually talking to different statecharts).
Hope that helps. Let me know if any of it isn't clear.