-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
241 lines (206 loc) · 6.8 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import _ from "lodash";
import { fk_data as DataProto } from "fk-data-protocol/fk-data";
import { levenbergMarquardt } from "ml-levenberg-marquardt";
export type ModuleConfiguration = DataProto.ModuleConfiguration;
export function unixNow(): number {
return Math.round(new Date().getTime() / 1000);
}
const debug = console;
export class CalibrationError extends Error {
public readonly calibration = true;
constructor(message: string) {
super(message);
}
public static isInstance(error: CalibrationError): boolean {
if (!error) return false;
return error?.calibration === true;
}
}
export abstract class CalibrationValue {}
export class PendingCalibrationPoint {
constructor(
public readonly index: number,
public readonly references: number[],
public readonly uncalibrated: number[],
public readonly factory: number[]
) {}
}
export class PendingCalibration {
constructor(public readonly points: PendingCalibrationPoint[] = []) {}
public append(pcp: PendingCalibrationPoint): PendingCalibration {
const newPoints = _.clone(this.points);
newPoints[pcp.index] = pcp;
return new PendingCalibration(newPoints);
}
}
export abstract class CalibrationCurve {
public calculate(pending: PendingCalibration): DataProto.Calibration {
const points = pending.points.map(
(p) =>
new DataProto.CalibrationPoint({
references: p.references,
uncalibrated: p.uncalibrated,
factory: p.factory,
})
);
if (points.length == 0)
throw new CalibrationError(`calibration failed: empty`);
const coefficients = this.calculateCoefficients(pending);
return DataProto.Calibration.create({
type: this.curveType,
time: unixNow(),
points: points,
coefficients: coefficients,
});
}
public abstract get curveType(): DataProto.CurveType;
public abstract calculateCoefficients(
pending: PendingCalibration
): DataProto.CalibrationCoefficients;
}
function acceptableCoefficient(value: number): boolean {
if (value === null || isNaN(value)) return false;
return Math.abs(value) > 0.0001;
}
function acceptableOffset(value: number): boolean {
if (value === null || isNaN(value)) return false;
return true;
}
export class ExponentialCalibrationCurve extends CalibrationCurve {
public get curveType(): DataProto.CurveType {
return DataProto.CurveType.CURVE_EXPONENTIAL;
}
public calculateCoefficients(
pending: PendingCalibration
): DataProto.CalibrationCoefficients {
const x = pending.points.map((p) => p.uncalibrated[0]);
const y = pending.points.map((p) => p.references[0]);
function calibrationFunction([a, b, c]: [number, number, number]): (
v: number
) => number {
return (t) => a + b * Math.exp(t * c);
}
const data = {
x: x,
y: y,
};
// Pete 4/6/2022
const options = {
damping: 1.5,
initialValues: _.clone([1000, 1500000, -7]),
gradientDifference: 10e-2,
maxIterations: 100,
errorTolerance: 10e-3,
};
const fittedParams = levenbergMarquardt(data, calibrationFunction, options);
const [a, b, c] = fittedParams.parameterValues;
const coefficients = { a, b, c };
debug.log(`cal:exponential ${JSON.stringify({ x, y, coefficients })}`);
if (!acceptableOffset(coefficients.a))
throw new CalibrationError(
`calibration failed: ${JSON.stringify(coefficients)}`
);
if (!acceptableCoefficient(coefficients.b))
throw new CalibrationError(
`calibration failed: ${JSON.stringify(coefficients)}`
);
if (!acceptableCoefficient(coefficients.c))
throw new CalibrationError(
`calibration failed: ${JSON.stringify(coefficients)}`
);
return new DataProto.CalibrationCoefficients({
values: [coefficients.a, coefficients.b, coefficients.c],
});
}
}
export class PowerCalibrationCurve extends CalibrationCurve {
public get curveType(): DataProto.CurveType {
return DataProto.CurveType.CURVE_POWER;
}
public calculateCoefficients(
pending: PendingCalibration
): DataProto.CalibrationCoefficients {
const len = pending.points.length;
const x = pending.points.map((p) => p.uncalibrated[0]);
const y = pending.points.map((p) => p.references[0]);
const indices = _.range(0, len);
const xSum = _.sum(x.map((x) => Math.log(x)));
const xySum = _.sum(indices.map((i) => Math.log(x[i]) * Math.log(y[i])));
const ySum = _.sum(y.map((y) => Math.log(y)));
const xSquaredSum = _.sum(
indices.map((i) => Math.log(x[i]) * Math.log(x[i]))
);
const b = (len * xySum - xSum * ySum) / (len * xSquaredSum - xSum ** 2);
const a = Math.exp((ySum - b * xSum) / len);
debug.log(
`cal:power ${JSON.stringify({
x,
y,
len,
xSum,
ySum,
xySum,
xSquaredSum,
})}`
);
if (!acceptableCoefficient(a))
throw new CalibrationError(`calibration failed: a=${a}`);
if (!acceptableCoefficient(b))
throw new CalibrationError(`calibration failed: b=${b}`);
return new DataProto.CalibrationCoefficients({ values: [a, b] });
}
}
export class LinearCalibrationCurve extends CalibrationCurve {
public get curveType(): DataProto.CurveType {
return DataProto.CurveType.CURVE_LINEAR;
}
public calculateCoefficients(
pending: PendingCalibration
): DataProto.CalibrationCoefficients {
const n = pending.points.length;
const x = pending.points.map((p) => p.uncalibrated[0]);
const y = pending.points.map((p) => p.references[0]);
const indices = _.range(0, n);
const xMean = _.mean(x);
const yMean = _.mean(y);
const numerParts = indices.map((i) => (x[i] - xMean) * (y[i] - yMean));
const denomParts = indices.map((i) => (x[i] - xMean) ** 2);
const numer = _.sum(numerParts);
const denom = _.sum(denomParts);
const m = numer / denom;
const b = yMean - m * xMean;
debug.log(
`cal:linear ${JSON.stringify({
x,
y,
xMean,
yMean,
numerParts,
denomParts,
numer,
denom,
b,
m,
})}`
);
if (!acceptableCoefficient(m))
throw new CalibrationError(`calibration failed: m=${m}`);
if (!acceptableOffset(b))
throw new CalibrationError(`calibration failed: b=${b}`);
return new DataProto.CalibrationCoefficients({ values: [b, m] });
}
}
export function getCurveForSensor(
curveType: DataProto.CurveType
): CalibrationCurve {
switch (curveType) {
case DataProto.CurveType.CURVE_POWER:
return new PowerCalibrationCurve();
case DataProto.CurveType.CURVE_EXPONENTIAL:
return new ExponentialCalibrationCurve();
case DataProto.CurveType.CURVE_LINEAR:
return new LinearCalibrationCurve();
default:
throw new Error(`unkonwn calibration curve type`);
}
}