Help with nan issue

I'm smoothing position data read in from an external source then executing a finite difference to get velocity... I am also smoothing both results. The smoothing returns a number for the position but nan for the velocity... Can anyone help me with this? Below is just the relevant portion of code.

//position
double ptau = 1000.;
double p = getAx(ppin);
double pw = min(1.0, dt/ptau);
static double ps = 0;
ps = p*pw + ps*(1-pw); //smoothing returns valid output

//velocity
double vtau=100.;
double v=(ps-psold)/dt;
double vw = min(1.0, dt/vtau);
static double vs = 0;
vs = v*vw + vs*(1-vw); //smoothing returns nan
What are the values of dt and psold?
dt is a double calculated each time the loop is run (around 1-2 milliseconds), psold is initially set to 0 but updated to equal ps each time the loop is run (also a double)
You’ll have to show complete runnable code. And input that creates a problem.

Why the static variables?
Last edited on
void setup(){
Serial.begin(115200);
told = millis();
vsold=0.;
psold=0.;
}



void loop(){
double t = millis();
double dt = t - told;
told = t;

//position
double ptau = 1000.;
double p = getAx(ppin);
double pw = min(1.0, dt/ptau);
static double ps = 0;
ps = p*pw + ps*(1-pw); //returns valid output

//velocity
double vtau=100.;
double v=(ps-psold)/dt;
double vw = min(1.0, dt/vtau);
static double vs = 0;
vs = v*vw + vs*(1-vw); //returns nan
}
There is more but this is the only relevant portion, input comes from an averaged analog read of a potentiometer
Below is just the relevant portion of code.

Our crystal balls are on the fritz, and we ain't mind readers. We need a Short, Self Contained, Correct (Compilable), Example.

http://www.sscce.org/

That includes test input that demonstrates the problem.
The error only occurs as a result the []term;
vs = v*vw + [vs]*(1-vw);
verify everything you divided by isnt zero.
@jreef, if you knew this was the "relevant" portion you would have solved the problem, wouldn't you?

Supply a complete compilable example, with input, that reproduces the problem. If the input can only come from a serial port then, nevertheless, supply complete code and we can probably "fake" the input.

Your function (as supplied here) doesn't "return" anything, so what you have written is nonsense. Moreover, we can't see what interaction you have with global variables (because you refuse to supply complete compilable code).

In your function loop() (as supplied here, but who knows if it is your actual function, because you haven't supplied complete code) ps and vs are local variables, so their value isn't going to be known outside that function, irrespective of whether you have yet more variables with the same name outside the function. In particular, you couldn't set psold to the value of ps calculated in loop().

If you think a particular statement produces a NaN then simply print out the value of all variables in it on the line before.


BTW, you are not really "smoothing" data - you are effectively increasing the inertia of the system and making its response more sluggish.
Last edited on
I suspect you're loop is running multiple times in the same millisecond:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void loop(){
double t = millis();
double dt = t - told;   // dt = 0
told = t;

//position
double ptau = 1000.;
double p = getAx(ppin);
double pw = min(1.0, dt/ptau);    //pw = 0
static double ps = 0;
ps = p*pw + ps*(1-pw); //returns valid output  // ps = p*0+ps*(1-0) == psold?

//velocity
double vtau=100.;
double v=(ps-psold)/dt;  // v = 0/0 == NAN
double vw = min(1.0, dt/vtau);
static double vs = 0;
vs = v*vw + vs*(1-vw); //returns nan
} 
Topic archived. No new replies allowed.