initial commit
This commit is contained in:
61
src/multi-pid.ts
Normal file
61
src/multi-pid.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { PID, PIDCallback, PIDParameters, PIDReport } from "./pid";
|
||||
|
||||
export type MultiPIDCallback = (channel: number, report: PIDReport) => any;
|
||||
|
||||
export class MultiPID {
|
||||
private pids: PID[];
|
||||
private channels: number;
|
||||
|
||||
public constructor(channels: number, params: PIDParameters) {
|
||||
this.channels = channels;
|
||||
this.pids = Array(channels)
|
||||
.fill(0)
|
||||
.map((_) => new PID(params));
|
||||
}
|
||||
|
||||
public setSetpoint(channel: number, setpoint: number) {
|
||||
if (channel > this.channels - 1)
|
||||
throw new Error("Channel index out of range: " + channel);
|
||||
this.pids[channel].setSetpoint(setpoint);
|
||||
}
|
||||
|
||||
public setSetpoints(setpoints: (number | null)[]) {
|
||||
if (setpoints.length != this.channels)
|
||||
throw new Error("Setpoint length mismatch: " + setpoints.length);
|
||||
setpoints
|
||||
.filter((x) => x != null)
|
||||
.forEach((x, i) => this.pids[i].setSetpoint(x));
|
||||
}
|
||||
|
||||
public setParameter(channel: number, params: PIDParameters) {
|
||||
if (channel > this.channels - 1)
|
||||
throw new Error("Channel index out of range: " + channel);
|
||||
this.pids[channel].setParameters(params);
|
||||
}
|
||||
|
||||
public setParameters(params: (PIDParameters | null)[]) {
|
||||
if (params.length != this.channels)
|
||||
throw new Error("Setpoint length mismatch: " + params.length);
|
||||
params
|
||||
.filter((x) => x != null)
|
||||
.forEach((x, i) => this.pids[i].setParameters(x));
|
||||
}
|
||||
|
||||
public updateOne(channel: number, measuredValue: number) {
|
||||
if (channel > this.channels - 1)
|
||||
throw new Error("Channel index out of range: " + channel);
|
||||
return this.pids[channel].update(measuredValue);
|
||||
}
|
||||
|
||||
public updateAll(measuredValues: (number | null)[]) {
|
||||
if (measuredValues.length != this.channels)
|
||||
throw new Error("Values length mismatch: " + measuredValues.length);
|
||||
return measuredValues
|
||||
.filter((x) => x != null)
|
||||
.map((x, i) => this.pids[i].update(x));
|
||||
}
|
||||
|
||||
public setCallback(cb: MultiPIDCallback) {
|
||||
this.pids.forEach((p, i) => p.setCallback((report) => cb(i, report)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user