03-30-2021 12:37 PM
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
Solved! Go to Solution.
03-30-2021 12:41 PM - edited 03-30-2021 12:47 PM
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)
To detect NaN, use this function .
03-30-2021 12:59 PM - edited 03-30-2021 01:08 PM
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.
03-30-2021 01:29 PM
Put the string parsing and validity checking inside a subVI, and all you need is the following code draft:
03-30-2021 10:12 PM - edited 03-30-2021 10:19 PM
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)