Skip to content

Commit 525524c

Browse files
committed
Tweaked dt behavior
1 parent df8b087 commit 525524c

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var Controller = function(k_p, k_i, k_d, dt) {
77
this.k_i = k_i || 0;
88
this.k_d = k_d || 0;
99

10-
// Time interval between two updates
11-
// If set to true, the interval will be automatically calculated
12-
this.dt = dt;
10+
// Interval of time between two updates
11+
// If not set, it will be automatically calculated
12+
this.dt = dt || 0;
1313

1414
this.sumError = 0;
1515
this.lastError = 0;
@@ -27,10 +27,10 @@ Controller.prototype.update = function(currentValue) {
2727

2828
// Calculate dt
2929
var dt = this.dt;
30-
if (this.dt === true) {
30+
if (!dt) {
3131
var currentTime = Date.now();
3232
if (this.lastTime === 0) { // First time update() is called
33-
dt = 1;
33+
dt = 0;
3434
} else {
3535
dt = (currentTime - this.lastTime) / 1000; // in seconds
3636
}

test/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var Controller = require('../')
1212
*/
1313
describe('pid-controller', function(){
1414

15-
var k_p = 0.5
16-
, k_i = 0.1
17-
, k_d = 0.2
18-
;
15+
var k_p = 0.5,
16+
k_i = 0.1,
17+
k_d = 0.2,
18+
dt = 1;
1919

2020
// Create the controller
21-
var ctr = new Controller(k_p, k_i, k_d);
21+
var ctr = new Controller(k_p, k_i, k_d, dt);
2222

2323
it('should have set the coefficient', function() {
2424
ctr.k_p.should.equal(k_p);
@@ -45,7 +45,7 @@ describe('pid-controller', function(){
4545
ctr.lastTime.should.equal(0);
4646
});
4747

48-
it('should return the correction for a given update interval', function(){
48+
it('should return the correction for the given update interval', function(){
4949
ctr.dt = 2; // 2 seconds between updates
5050
var correction = ctr.update(115);
5151
correction.should.equal(4);

0 commit comments

Comments
 (0)