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
(999 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
CLAD expired, using 2009SP1 + LV2011SP1 + LV2017 (+LV2018 sometimes) on Win7+cRIO
Kudos are welcome Smiley Wink

0 Kudos
Message 2 of 8
(988 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. Smiley Surprised

 

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.


LabVIEW Champion Do more with less code and in less time
0 Kudos
Message 3 of 8
(972 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
(937 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
CLAD expired, using 2009SP1 + LV2011SP1 + LV2017 (+LV2018 sometimes) on Win7+cRIO
Kudos are welcome Smiley Wink

0 Kudos
Message 5 of 8
(931 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
(913 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
LabVIEW 2017
LabVIEW Programming
0 Kudos
Message 7 of 8
(890 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
CLAD expired, using 2009SP1 + LV2011SP1 + LV2017 (+LV2018 sometimes) on Win7+cRIO
Kudos are welcome Smiley Wink

0 Kudos
Message 8 of 8
(884 Views)