//Proportional
double pTerm = pGain * error;
iState += error;
if(iState > iMax) iState = iMax;
if(iState < iMin) iState = iMin;
double iTerm = iGain * iState;
double d = dState - position;
double dTerm = dGain * d;
dState = position;
double result = pTerm + iTerm - dTerm;
System.out.println("PID p,i,d (" + pGain + "," + iGain + "," + dGain +") : is,ds (" + iState + "," + d + ") p,i,d (" + pTerm + "," + iTerm + "," + dTerm + ") => " + result);
return result;