LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

NaN != NaN?

Solved!
Go to solution

Hey all,

 

I currently have a VI with a nested subVI. The subVI is reading data from an Arduino about IMU data. I basically want the subVI to output the average of the last 10 sensor readings from the Arduino. The readings from the Arduino do not always initially come in 'clean' so I am changing those values to NaN. Once there are 10 values that are not NaN, I want the subVI to terminate and push the averaged 'IMU data' to my VI.

 

My problem is coming from the comparison I am doing when checking if the output array is NaN or a valid number. Even when the variables are NaN, the comparison is giving True for NaN != NaN and terminating the while loop.

 

Also is there any ways to speed up the 'IMU_Data' subVI, ideally I would like it to be continuously streaming data into my VI so that there are no gaps in the graph.

 

Thanks,

Kfly

Download All
0 Kudos
Message 1 of 5
(2,160 Views)

By convention, all equal comparisons with NaN (Including comparing NaN with NaN!) are false and all "not equal" comparisons with NaN are true.

(see also here for more details, A fact well know to all experienced programmers)

 

altenbach_0-1617126305086.png

 

To detect NaN, use this function .

 

 

0 Kudos
Message 2 of 5
(2,159 Views)
Solution
Accepted by topic author Kfly

On a side note, your subVI is highly flawed and will not work as expected due to serious race conditions due to the overuse of local variables. It is also overly complicated.

 

  • Why are your terminals controls if they are actually constantly written by the program?
  • Since these terminals get read way before the local vairables are written, you will get the values from the previous iteration when the loop ends and the last valid data is irreversibly lost. Just bundle the 10 values where you currently have the local variables and wire to the output terminals. If you want to see the scalars, make them indicators and wire them istead of the locals.
  • Index array is resizable. You only need one instance configured for 10 outputs.
  • Your NaN array in the false case is empty. That's not right (container size != array size!)
  • Since you know exactly how many iterations it will take for a full set, use a FOR loop. You can add a conditional terminal to stop early if needed.
  • You should also stop on errors.
  • All this could be done with 10% of the code!
  • It is difficult to test if the read buffer contains incorrect default data. Give it a reasonable default string, including the "T:" and "end" parts before saving, so we can test. Ideally, we want the raw string directly from the read function, before you do any substitutions!
  • etc.etc.!
  •  
Message 3 of 5
(2,141 Views)

Put the string parsing and validity checking inside a subVI, and all you need is the following code draft:

 

altenbach_1-1617128924271.png

 

Message 4 of 5
(2,117 Views)

NaN really is not a number!  To be compared as a number a value must be a number so yes, a value that is not a number will fall every comparison.   Not equals is the negation of an equality comparison so any number is not equal to any value that isn't a number. 

 

Under the hood Is NaN? Is true if and only if its input is not equal to itself

 

Use this to your advantage and terminate your loop when the array sum is equal to itself (hence no NaN exists)


"Should be" isn't "Is" -Jay
Message 5 of 5
(2,061 Views)