From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Alternative to nested case structures??

Hello, 

 

I am currenly making a simulation of using a chromatography column using LabVIEW NXG. I haven't come far yet, but I already see that I have too many nested case structures. 

 

Some of the logic I have in there is: 

Has the user pressed "condition"-button?

       If True: Is the column empty?

               If True: Is the selected conditioning volume less than or equal to 0? (invalid number)

                         If False: Is the conditioning volume less than or equal to 10? (maximum volume this specific column can hold)

                                      If True: valves open and conditioning starts. 

 

The question is: Is there a cleaner way to do this?

 

Thank you in advance!

0 Kudos
Message 1 of 9
(2,415 Views)

Start reading up on state machines.   Each of the "landing points" inside your nested case structure (the places where there's some code rather than just another nested case structure) will probably end up being distinct states.

 

Instead of evaluating all the logic for the case structures every iteration, you'll only be evaluating the logic that applies to the particular state you're in.  What could cause you to stay in the same state?  What could cause you to leave it, and what state(s) could you go to next?   These kinds of questions help you work out a state diagram like the one shown in the linked article.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 2 of 9
(2,390 Views)

Please, explain me, how does it work? I tryed to do it and I do not understand. The result from type cast is always the same - the last value of enum - it doesn't depends on the loop index. 

bob3ro_1-1687373575043.png

 

 

So, in this case, what is the sence and why I can't just use a while loop with this bool connected to the terminal if I want to wait untill the condition will be true? 

 

This is from oficial NI documentation (https://www.ni.com/pl-pl/support/documentation/supplemental/16/simple-state-machine-template-documen...), so, please explain it to me.

0 Kudos
Message 3 of 9
(904 Views)

@bob3ro wrote:

Please, explain me, how does it work? I tryed to do it and I do not understand. The result from type cast is always the same - the last value of enum - it doesn't depends on the loop index. 

bob3ro_1-1687373575043.png

 

 

So, in this case, what is the sence and why I can't just use a while loop with this bool connected to the terminal if I want to wait untill the condition will be true? 

 

This is from oficial NI documentation (https://www.ni.com/pl-pl/support/documentation/supplemental/16/simple-state-machine-template-documen...), so, please explain it to me.


It really looks like that, doesn't it?  But looks can be deceiving, and in this case, if you turned on "Highlight Execution" mode, you would discover that it goes through all the different enum items until the iteration number is greater than what can be represented by the enum, in which case it will be coerced to the last enum item.  The problem is it happens too fast for you to see and it just seems like it is always the last item.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 9
(882 Views)

You might also want to look into something known as a "Natt Sequence".  

 

Natt Sequence runs through each case once and only once per item in the enum.  I find it to be a pretty good alternative to multiple nested case structures.

 

If you post more details about what you specifically are trying to do then we might be able to give more specific advice.  If you do, it might help to start a new thread since you've just posted in one that's 3 years old.

Message 5 of 9
(868 Views)

Sorry, but I think it's always the first value, and nothing changes trough the cycle. Ii tryed to test it again, and I always see "zero" on both indicators.

bob3ro_2-1692259923404.png

 bob3ro_1-1692259907002.png

bob3ro_3-1692259959845.png

 

0 Kudos
Message 6 of 9
(710 Views)

Hi bob,

 


@bob3ro wrote:

Sorry, but I think it's always the first value, and nothing changes trough the cycle. Ii tryed to test it again, and I always see "zero" on both indicators.


You forgot to attach your code - we cannot debug/run/edit images with LabVIEW!

 

  • What is the underlying datatype of the enum?
  • Why do you use Typecast (with its limitations) instead of CoerceToType?
  • Why do you use TypeCast at all? Why not wire loop iterator directly to your enum array???

Example:

My enum consists of {3, 2, 1, 0}…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 9
(700 Views)

@bob3ro wrote:

Sorry, but I think it's always the first value, and nothing changes trough the cycle. Ii tryed to test it again, and I always see "zero" on both indicators.

bob3ro_2-1692259923404.png

 bob3ro_1-1692259907002.png

bob3ro_3-1692259959845.png

 


"i" is I32, Enums are typicaly U16, you need to convert.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 9
(691 Views)

Stop using "type cast" and start using "coerce to type".

 

"Type cast" could be thought of as being a brute force conversion, it just tells LabVIEW to do its best to change the raw bit data of one data type to another.

 

"Coerce to type" is a much more intelligent conversion, which is what you need in this case.

0 Kudos
Message 9 of 9
(656 Views)