node-pid-controller
Advanced tools
Comparing version 0.0.3 to 0.1.1
/** | ||
* PID Controller. | ||
*/ | ||
var Controller = function(k_p, k_i, k_d) { | ||
this.k_p = k_p || 1; | ||
var Controller = function(k_p, k_i, k_d, dt) { | ||
// PID constants | ||
this.k_p = (typeof k_p === 'number') ? k_p : 1; | ||
this.k_i = k_i || 0; | ||
this.k_d = k_d || 0; | ||
// Interval of time between two updates | ||
// If not set, it will be automatically calculated | ||
this.dt = dt || 0; | ||
this.sumError = 0; | ||
@@ -20,8 +25,23 @@ this.lastError = 0; | ||
Controller.prototype.update = function(current_value) { | ||
this.current_value = current_value; | ||
Controller.prototype.update = function(currentValue) { | ||
this.currentValue = currentValue; | ||
var error = (this.target - this.current_value); | ||
this.sumError = this.sumError + error; | ||
var dError = error - this.lastError; | ||
// Calculate dt | ||
var dt = this.dt; | ||
if (!dt) { | ||
var currentTime = Date.now(); | ||
if (this.lastTime === 0) { // First time update() is called | ||
dt = 0; | ||
} else { | ||
dt = (currentTime - this.lastTime) / 1000; // in seconds | ||
} | ||
this.lastTime = currentTime; | ||
} | ||
if (typeof dt !== 'number' || dt === 0) { | ||
dt = 1; | ||
} | ||
var error = (this.target - this.currentValue); | ||
this.sumError = this.sumError + error*dt; | ||
var dError = (error - this.lastError)/dt; | ||
this.lastError = error; | ||
@@ -32,2 +52,8 @@ | ||
module.exports = Controller; | ||
Controller.prototype.reset = function() { | ||
this.sumError = 0; | ||
this.lastError = 0; | ||
this.lastTime = 0; | ||
}; | ||
module.exports = Controller; |
{ | ||
"name": "node-pid-controller", | ||
"version": "0.0.3", | ||
"version": "0.1.1", | ||
"description": "Node.js PID controller", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -15,5 +15,6 @@ # node-pid-controller | ||
### Create a Controller instance | ||
`k_p`, `k_i` and `k_d` are the proportional, integral and derivative terms. `dt` is the interval of time between two measures. If not set, it will be automatically calculated. | ||
```js | ||
var Controller = require('node-pid-controller'); | ||
var ctr = new Controller(0.25, 0.01, 0.01); // k_p, k_i, k_d | ||
var ctr = new Controller(0.25, 0.01, 0.01, 1); // k_p, k_i, k_d, dt | ||
``` | ||
@@ -50,2 +51,2 @@ | ||
Philmod <philippe.modard@gmail.com> | ||
Philmod <philippe.modard@gmail.com> |
@@ -15,9 +15,9 @@ /** | ||
var k_p = 0.5 | ||
, k_i = 0.1 | ||
, k_d = 0.2 | ||
; | ||
var k_p = 0.5, | ||
k_i = 0.1, | ||
k_d = 0.2, | ||
dt = 1; | ||
// Create the controller | ||
var ctr = new Controller(k_p, k_i, k_d); | ||
var ctr = new Controller(k_p, k_i, k_d, dt); | ||
@@ -41,3 +41,23 @@ it('should have set the coefficient', function() { | ||
}); | ||
it('should reset the controller', function(){ | ||
ctr.reset(); | ||
ctr.sumError.should.equal(0); | ||
ctr.lastError.should.equal(0); | ||
ctr.lastTime.should.equal(0); | ||
}); | ||
it('should return the correction for the given update interval', function(){ | ||
ctr.dt = 2; // 2 seconds between updates | ||
var correction = ctr.update(115); | ||
correction.should.equal(4); | ||
}); | ||
it('should return a null correction', function(){ | ||
var ctr = new Controller(0, 0, 0); | ||
ctr.setTarget(120); | ||
var correction = ctr.update(110); | ||
correction.should.equal(0); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5074
97
51