05-21-2024
08:03 PM
- last edited on
05-24-2024
04:14 PM
by
NI_Community_Su
Hello, I'm new to labview. It seems like there should be an answer to this question somewhere, but I couldn't find it. Hopefully someone can direct me to a good reference.
I have a subvi that needs to maintain its state between calls. This subvi is called in a for loop with 16 iterations - and that for loop is in a while loop. What I want is for labview to treat the 16 calls in the for loop as separate instances of the subvi, each with its own state. Upon the next iteration of the while loop, each instance should retain its state from the previous iteration and update it with the new call.
Right now, I just have the subvi in the for loop. I have tried experimenting with "call setup" and "execution properties" but I think I need to do more restructuring to make it work. Perhaps by working with "open vi by reference" before the while loop?
For context, the goal is to have 16 instances of a controller to control 16 identical, but independent, systems.
Thank you!
05-21-2024 08:10 PM - edited 05-21-2024 08:13 PM
Being new to LabVIEW, this seems convoluted. even a reentrant VI places in a loop is only one instance.
I have the nagging feeling that you are trying to solve whatever problem you are trying to solve in a way that is not great. Instead of telling us how you want to do something, take a step back and tells the overall purpose instead. There are probably better solutions. Can you explain what the "states" are that need to be retained?
05-21-2024 08:26 PM
The goal is to have 16 instances of a controller to control 16 identical, but independent, systems. New sensor readings come in as an array 16 elements long, one from each system. Then I need to calculate the control response for each of the 16 systems and apply that to each of the 16 actuators.
The states are the internal states of each controller instance. Like a PID controller requires tracking the controller's internal state between each call in order to update the I and D terms properly.
05-21-2024 08:54 PM - edited 05-21-2024 09:09 PM
I found a solution that looks like it's working using subvi references. I create 16 references in an array before the main loops and then I call the subvi by reference as elements from that array in my main for loop.
I'm still open to suggestions/discussion about whether this is a good way to do this. I should say, i'm not a complete beginner. I took a semester labview class back in grad school, but that was 10 years ago. I also have a lot of C experience.
Thanks!
------
First image shows initializing the references, second shows calling by reference within the main loops. The red arrow in the second indicates the element from the array created in the first image while the rest of the lines are just the inputs and outputs to my subvi.
05-21-2024 09:19 PM
Why can't you create a single subVI that keeps an array of 16 states.
05-21-2024 09:44 PM
That seems less easily extensible to me. I'd like the subvi code to be the same whether I'm working with 1 instance or 50 instances. Also, this subvi contains multiple calls to labview's built-in PID.vi for different system variables/actuators and i'm not sure how i'd extract those states from those instances in order to track them in the shift register state array of my own subvi. Is that even possible?
05-22-2024 08:15 AM
Perhaps maintain the states outside the sub VI. Like this:
05-22-2024 01:16 PM
That does seem like a reasonable solution if I'm writing all custom code. It's maybe a bit more work to develop, maintain, but the design flow is clear and I guess it probably uses less memory.
But since I'm (currently) using the built-in PID vi which keeps track of its own state, I'd have to modify it to not track it's own state and instead take state as an input and updated state as an output. This seems like a lot of work.
Are there other pros/cons I'm not thinking of?
05-22-2024 01:46 PM
I have done something similar using VI references as you are doing for a filter response. It works fine and adapts to any inputs.
See
https://forums.ni.com/t5/LabVIEW/Independent-instances-of-filter/m-p/3902712/highlight/true#M1107521
05-22-2024 04:52 PM - edited 05-22-2024 04:52 PM
Or you could do something like this:
This opens 16 clones of the sub VI, each retaining its own state.