From 90922dc11f15689529512e076b9a416230b561c1 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Sun, 19 Mar 2017 06:53:37 +0000 Subject: [PATCH] Added PID Controller --- BoneCentral/Brain/Utilities/pid.js | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 BoneCentral/Brain/Utilities/pid.js diff --git a/BoneCentral/Brain/Utilities/pid.js b/BoneCentral/Brain/Utilities/pid.js new file mode 100644 index 0000000..6950b27 --- /dev/null +++ b/BoneCentral/Brain/Utilities/pid.js @@ -0,0 +1,45 @@ +module.exports = (function() { + function PID() { + this.dt = 0.1; + this.perr = 0; + this.min = -100; + this.max = 100; + this.kp = 0.1; + this.ki = 0.5; + this.kd = 0.01; + } + + PID.prototype.update = function(sp, pv) { + var err = sp-pv; + var op = this.kp*err; + var oi = this.ki*err*this.dt; + var od = this.kd*(err-this.perr)/this.dt; + var res = op + oi + od; + if(res > this.max) + res = this.max; + else if(res < this.min) + res = this.min; + return res; + }; + + PID.prototype.setBounds = function(min, max) { + this.min = min; + this.max = max; + }; + + PID.prototype.setDelta = function(time) { + this.dt = time; + } + + PID.prototype.calibrate = function(p, i, d) { + this.kp = p; + this.ki = i; + this.kd = d; + } + + PID.prototype.reset = function() { + this.perr = 0; + } + + return PID; +})(); \ No newline at end of file