- Home /
Question by
BrentShanahan1 · Apr 10, 2015 at 07:06 PM ·
javascriptnan
Getting NAN for simple equation
I currently have 7 different PID controllers controlling a simulation, all with different tuning values. They are all coded the exact same aside from variable names and tuning values. The strangest thing is that the latest one I added is giving me NAN despite the simple equation outputting what I would think is a usable value.
Here is my code:
//Input Values in fixedUpdate
var dist : float = Vector3.Distance(pathpoint, uavPos);
var zVel : float = UAV.transform.InverseTransformDirection(GetComponent.<Rigidbody>().velocity).z;
//Runs through 2 PIDs in fixedUpdate
var distErr : float= locZError(0, dist);
var velErr : float = velZError(zVel, distErr);
//PID with NAN Error
private var errSumVelZ : float;
private var lastTimeVelZ : float;
private var lastErrVelZ : float;
function velZError(input : float, setPoint : float){
//input=0.0001448985 setPoint= 181.3836
var now : float = Time.time;
var timeChange : float = (now - lastTimeVelZ);
var error : float = setPoint - input;
Debug.Log(error); // outputs 181.3835
Debug.Log(timeChange); // outputs 0.01999998
Debug.Log(error * timeChange); // outputs 3.627666
errSumVelZ += (error * timeChange);
Debug.Log(errSumVelZ); //outputs NAN
var dErr : float = (error - lastErrVelZ) / timeChange;
var Output : float = (.3 * error) + (0 * errSumVelZ) + (2 * dErr);
lastErrVelZ = error;
lastTimeVelZ = now;
return Output;
}
What would cause 'errSumVelZ += 3.627666' to create NAN?
Comment
Answer by UNZoOM · Apr 10, 2015 at 07:34 PM
Initialize errSumVelZ like
private var errSumVelZ : float =0.0;
It's pointless because the default value for floats is 0.0 anyway.