Skip to content

Commit

Permalink
Added PID Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TekuConcept authored and Nathan Floris Copier committed Jul 12, 2017
1 parent abcfb7f commit 90922dc
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions BoneCentral/Brain/Utilities/pid.js
Original file line number Diff line number Diff line change
@@ -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;
})();

0 comments on commit 90922dc

Please sign in to comment.