63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
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 = [];
|
|
for (var i = 0; i < channels; i++) {
|
|
this.pids.push(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)));
|
|
}
|
|
}
|