Skip to content

Add setTuning method #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
temp.js
temp
temp
package-lock.json
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ You can also pass options as arguments:
let ctr = new Controller(0.25, 0.01, 0.01, 1); // k_p, k_i, k_d, dt
```

### Re-set the tuning

```js
ctr.setTuning(0.25, 0.01, 0.01, 1); // k_p, k_i, k_d, dt

ctr.setTuning({ // as object
k_p: 0.25,
k_i: 0.01,
k_d: 0.01,
dt: 1
});
```

### Set the target

```js
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ declare class Controller {

public setTarget(target: number): void;

public setTuning(options: Controller.Options);
public setTuning(k_p?: number, k_i?: number, k_d?: number, dt?: number);

public update(currentValue: number): number;

public reset(): number;
Expand Down
49 changes: 28 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
*/
class Controller {
constructor(k_p, k_i, k_d, dt) {

this.setTuning(k_p, k_i, k_d, dt);

this.sumError = 0;
this.lastError = 0;
this.lastTime = 0;

this.target = 0; // default value, can be modified with .setTarget
}

setTarget(target) {
this.target = target;
}

setTuning(k_p, k_i, k_d, dt) {
let i_max;
if (typeof k_p === 'object') {
let options = k_p;
Expand All @@ -16,30 +31,22 @@ class Controller {
}

// PID constants
this.k_p = (typeof k_p === 'number') ? k_p : 1;
this.k_i = k_i || 0;
this.k_d = k_d || 0;
if (typeof k_p !== 'number') k_p = this.k_p || 1;

this.k_p = k_p;
this.k_i = k_i || this.k_i || 0;
this.k_d = k_d || this.k_d || 0;

// Interval of time between two updates
// If not set, it will be automatically calculated
this.dt = dt || 0;
this.dt = dt || this.dt || 0;

// Maximum absolute value of sumError
this.i_max = i_max || 0;

this.sumError = 0;
this.lastError = 0;
this.lastTime = 0;

this.target = 0; // default value, can be modified with .setTarget
}

setTarget(target) {
this.target = target;
this.i_max = i_max || this.i_max || 0;
}

update(currentValue) {
if(!currentValue) throw new Error("Invalid argument");
if (isNaN(currentValue)) throw new Error("Invalid argument");
this.currentValue = currentValue;

// Calculate dt
Expand All @@ -58,22 +65,22 @@ class Controller {
}

let error = (this.target - this.currentValue);
this.sumError = this.sumError + error*dt;
this.sumError = this.sumError + error * dt;
if (this.i_max > 0 && Math.abs(this.sumError) > this.i_max) {
let sumSign = (this.sumError > 0) ? 1 : -1;
this.sumError = sumSign * this.i_max;
}

let dError = (error - this.lastError)/dt;
let dError = (error - this.lastError) / dt;
this.lastError = error;

return (this.k_p*error) + (this.k_i * this.sumError) + (this.k_d * dError);
return (this.k_p * error) + (this.k_i * this.sumError) + (this.k_d * dError);
}

reset() {
this.sumError = 0;
this.sumError = 0;
this.lastError = 0;
this.lastTime = 0;
this.lastTime = 0;
}
}

Expand Down
33 changes: 29 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ describe('pid-controller', () => {
ctr.dt.should.equal(options.dt);
});

it('should have set the coefficient from an new options object', () => {
let newOptions = {
k_p: 0.7,
k_i: 0.2,
k_d: 0.3,
dt: 10
}

ctr.setTuning(newOptions.k_p, newOptions.k_i, newOptions.k_d, newOptions.dt);

ctr.k_p.should.equal(newOptions.k_p);
ctr.k_i.should.equal(newOptions.k_i);
ctr.k_d.should.equal(newOptions.k_d);
ctr.dt.should.equal(newOptions.dt);

ctr.setTuning(options); // Reset tunings
});

it('should set the target', () => {
let v = 120; // 120km/h
ctr.setTarget(v);
Expand All @@ -60,15 +78,22 @@ describe('pid-controller', () => {
});

it('should return the correction for the given update interval', () => {
ctr.dt = 2; // 2 seconds between updates
ctr.setTuning({
dt: 2 // 2 seconds between updates
})
let correction = ctr.update(115);
correction.should.equal(4);
ctr.dt = options.dt; // Reset dt

ctr.setTuning({
dt: options.dt // Reset dt
})
});

it('should return the correction with sumError <= i_max', () => {
let ctr = new Controller(options);
ctr.i_max = 5; // sumError will be 10
ctr.setTuning({
i_max: 5 // sumError will be 10
});
ctr.setTarget(120);
let correction = ctr.update(110);
correction.should.equal(7.5);
Expand All @@ -83,7 +108,7 @@ describe('pid-controller', () => {
});

it('should throw error when updating a NaN value', () => {
let ctr = new Controller(0,0,0);
let ctr = new Controller(0, 0, 0);
ctr.setTarget(20);
should.throws(() => {
ctr.update(NaN);
Expand Down