01-15-2009 12:17 PM - edited 01-15-2009 12:21 PM
I have a question that came to me from some high school students doing the FRC (First Robotics Competition).
They wanted to know how to properly write a if-then structure in LabVIEW. I know there are simple expressions in the comparison pallet that would allow me to do this but I want to teach them proper coding up front. Also I would like the code to stay neat and allow for expansion of the else expressions. What do advance LabVIEW users recommend? I was thinking of converting everything into array and just using a while loop with a shift register to do the comparison. Let me know what you think?
Regards, -SS
The example ANSII C code (from DevC) would look something like the following:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double gyro, x = 0.0;
for (gyro = 0.0; gyro <=0.9; gyro += 0.01)
{
if (gyro <= 0.3)
x = 0.373;
else if (gyro > 0.3 && gyro <= 0.6)
x = 2.123;
else if(gyro > 0.6 && gyro <= 0.8)
x = 4.323;
else
x = 5.533;
printf("gyro = %f, x = %f\n", gyro, x) ;
};
system( "PAUSE" );
return 0;
}
Solved! Go to Solution.
01-15-2009 12:33 PM
One approach (assuming only positive values)
Multiply by "10 then run into a "to I32" converter.
Wire the I32 to the selector of the case structure.
Create a case for each
Enter the ranges...
0 .. 3
4 .. 6
7 .. 8
9, default (make this the default)
Ben
01-15-2009 12:58 PM - edited 01-15-2009 12:58 PM
I will suggest something like that:
But be careful with doubles in this case 😉 (Ben's idea with multiplication is really good).
Andrey.
01-15-2009 01:15 PM - edited 01-15-2009 01:16 PM
You could always "cheat".
01-15-2009 02:03 PM - edited 01-15-2009 02:04 PM
Here's yet another approach. Note that this isn't really an if-else problem, it's a lookup table, and I've implemented it that way. The "Search 1D Array" is necessary for the case when the input value exactly equals one of the lookup table values, due to the way "Threshold 1D Array" works.
01-15-2009 02:20 PM
nathand wrote:The "Search 1D Array" is necessary for the case when the input value exactly equals one of the lookup table values, due to the way "Threshold 1D Array" works.
That's why I used 0.3001 in my array instead of 0.3. I just made sure it was one order of magnitude smaller than the step size.
01-15-2009 02:21 PM - edited 01-15-2009 02:28 PM
With all respect is not this a job for a formula node? I just did a copy and paste from your C code 😉
and it is still Labview 100%. Even if Labview a puritan may doubt it
01-15-2009 02:44 PM - edited 01-15-2009 02:49 PM
t06afre wrote:....
and it is still Labview 100%. Even if Labview a puritan may doubt it
As a LabVIEW Puritan I would want to benchmark that implementaion vs an equivent LV operator version.
Ben
01-15-2009 03:14 PM
A LabVIEW puritan might also argue that you cannot debug the contents of a formula node.
Chris M
01-15-2009 03:48 PM