9 integralSaturation: false,
10 derivativeInitialized: false,
16 derivativeMeasurement: "Velocity"
18 this.valueLast = {x: 0, y: 0};
19 this.errorLast = {x: 0, y: 0};
20 this.integrationStored = {x: 0, y: 0};
21 this.velocity = {x: 0, y: 0};
25 this.derivativeInitialized = false;
28 Update(dt, currentValue, targetValue) {
34 x: targetValue.x - currentValue.x,
35 y: targetValue.y - currentValue.y
39 x: this.PIDParams.proportionalGain * error.x,
40 y: this.PIDParams.proportionalGain * error.y
43 this.integrationStored = {
45 Math.max(this.integrationStored.x + (error.x * dt), this.PIDParams.iMin),
49 Math.max(this.integrationStored.y + (error.y * dt), this.PIDParams.iMin),
55 x: this.PIDParams.integralGain * this.integrationStored.x,
56 y: this.PIDParams.integralGain * this.integrationStored.y
59 const errorRateOfChange = {
60 x: (error.x - this.errorLast.x) / dt,
61 y: (error.y - this.errorLast.y) / dt
64 this.errorLast = error;
66 const valueRateOfChange = {
67 x: (currentValue.x - this.valueLast.x) / dt,
68 y: (currentValue.y - this.valueLast.y) / dt
71 this.valueLast = currentValue;
72 this.velocity = valueRateOfChange;
74 let deriveMeasure = {x: 0, y: 0};
76 if (this.derivativeInitialized) {
77 if (this.PIDParams.derivativeMeasurement === "Velocity") {
79 x: -valueRateOfChange.x,
80 y: -valueRateOfChange.y
83 deriveMeasure = errorRateOfChange;
86 this.derivativeInitialized = true;
90 x: this.PIDParams.derivativeGain * deriveMeasure.x,
91 y: this.PIDParams.derivativeGain * deriveMeasure.y
99 result.x *= this.PIDParams.magnitude;
100 result.y *= this.PIDParams.magnitude;
102 this.PIDParams.force = {
103 x: result.x * 60, // this routine is called ~60 seconds
107 this.PIDParams.forceMagnitude = Math.sqrt(result.x ** 2 + result.y ** 2) * 60;
112 export default PIDController;