Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-pid-controller

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-pid-controller - npm Package Compare versions

Comparing version 0.0.3 to 0.1.1

42

lib/index.js
/**
* 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;

2

package.json
{
"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);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc