LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement several multi conditional statements?

Hey, guys, I need some advice.

I've got a state machine which will run on a real-time system. In it, there is a case that should decide what the next state is. The decision should be based on a couple of int values, some boolean values, and a string. The output should be a string of the states which should follow.

 

I'm still relatively new to LV but it seems rather complex to create multiple multi conditional if statements using the graphical blocks. 
That is why I'm not sure whether to use the formula block or use an external DLL or maybe there is an even better way to solve my problem.

But I think I would have a hard time with the formula block and the strings. So I'm tending towards implementing my function in some C code. 

Any suggestions?
Thanks for the help.

Daniel

0 Kudos
Message 1 of 8
(3,846 Views)

Hi schpongo,

 

but it seems rather complex to create multiple multi conditional if statements using the graphical blocks. 

Why should it be more "complex" than using textual programming?

Hint: There is the CompoundArithmetic being able to do also boolean arithmetic on multiple inputs…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 8
(3,835 Views)

schpongo wrote:

The decision should be based on a couple of int values, some boolean values, and a string. The output should be a string of the states which should follow.

 

I'm still relatively new to LV but it seems rather complex to create multiple multi conditional if statements using the graphical blocks. 
That is why I'm not sure whether to use the formula block or use an external DLL or maybe there is an even better way to solve my problem.

But I think I would have a hard time with the formula block and the strings. So I'm tending towards implementing my function in some C code. 


I guess the question is "How many states do you actually have". Once you have a couple of booleans, integers, and strings, you get a combinatorial explosion of possible outcomes. I doubt you want a case structure with millions of cases. 😮

 

Can you give an actual example of the calculations involved?

 

And yes, I agree with Gerd that if the complexity is in the task itself, the programming language is irrelevant to the complexity. Most contributors here are LabVIEW programmers and my guess is that a pure G implementation is probably the best. I definitely would go with LabVIEW. Explain the problem and we will show you how.

0 Kudos
Message 3 of 8
(3,819 Views)

Here are the variables and a brief description of them:

  • #Elements_fifo [int]: Holds the number of elements left in a RT-fifo after being read
  • fifo_empty [boolean]: is set if the fifo is empty on reading
  • buffer_size [int]: I have a buffer in form of a 2D array in which I hold up to 100 coordinate points. The size describes how many points have been stored in it.
  • Parameter_Const [boolean]: Each coordinate point which is read from the fifo also contains some data on how to travel to that point (speed, acceleration). If two moves which are being compared have the same parameters this is set to true.
  • Last_update [boolean]: This variable indicates if an update to a table of points is the last one.
  • move_complete [boolean]: If a current move is complete this is set to true.
  • Last_case [string]: This string holds the last case which came before the current one.

I only have 6 states and have mapped out a couple of scenarios on paper. 
Here is an example:

if( Last_case== update && move_complete==true) { next_case="sart_contour","Read"}

else 

if( Last_case== update && move_complete==false) {next_state= "Read"}

else if( Last_case==compare && #Elements_fifo===99 && Parameter_Const==true){next_state=''add'',''create poly'',}

 

and I've got quite a few more depending on which case I'm coming from. 
I hope I've managed to clarify my problem at hand and not make it too confusing.

 

Even though in my opinion it is not elegant to use a DLL as it is more difficult to read and edit, but I feel like many if conditions are very easy to implement in a written language.
But I might be wrong. If someone could maybe point me to an example of how this could be handled, I would be very thankful.

 

0 Kudos
Message 4 of 8
(3,784 Views)

Hi schpongo,

 

the "LastCase==" comparison reduces to a case structure with LastCase wired to the selector input. I hope you have created an enum type definition for this data type...

 

In the "update" case you only have to check your MoveComplete condition once to handle both first and second IF statement...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 8
(3,778 Views)

Uh good input there. 
I didn't think of replacing my strings with an enum, as my state machine is base on strings. But if I convert the 

But if I convert the strings to an enum and the booleans to 0 and 1 I might be able to the formula block. 
For multiple output strings, I could store the enum in an array and convert that back into strings.

 

0 Kudos
Message 6 of 8
(3,760 Views)

I personally like string-based state machines.  They can be harder to debug if, for example, you make a typo when selecting a state.  But there is a lot more you can do with a string based, IMO.  I recommend that you have a gander at the JKI state machine.  When you open it up, it may be a little intimidating at first but just throw the VI into highlight mode and watch it go.  And then mess with it a bit to see how it operates.  Make it go to a state that doesn't exist and see how it handles a typo. 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 7 of 8
(3,737 Views)

Hi schpongo,

 

I don't like to convert booleans to 0/1 just for "cosmetic" reasons…

 

Your statement

if( Last_case== update && move_complete==true) { next_case="sart_contour","Read"}

else 

if( Last_case== update && move_complete==false) {next_state= "Read"}

could be reduced to

switch Last_case
  "update":
    IF move_complete THEN
       next := [sart_contour, Read]
    ELSE
       next := [Read]
    ENDIF
  "other cases":
… end_switch
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 8
(3,731 Views)