feat(new tools): 4-20 mA, 0-10 V and analog scaling converters (#596)

Add an Automation category with three tools for PLC programmers and
instrumentation technicians, built on a shared AnalogSignalConverter
component and a tested src/utils/analog-signal.ts module:

- 4-20 mA converter: current <-> engineering value, 4-20 / 0-20 mA, with the
  NAMUR NE 43 loop diagnostic (failure, undefined zone, under/over-range)
- 0-10 V converter: 0-10, 2-10, 0-5, 1-5 and ±10 V, warns below the live
  zero (broken wire)
- Analog scaling calculator: raw PLC values (Siemens 0-27648 / ±27648,
  Schneider 0-10000, 12/15/16 bits) <-> engineering value, with the rounded
  raw value

Three-column layout: input, parameters and output, each side with a gauge and
a coloured status. A swap button inverts the conversion direction and keeps
the operating point. Also shows the y = gain × x + offset formula for the PLC
and a 0-25-50-75-100 % reference table. Ranges are validated (min < max).

English and French translations.
This commit is contained in:
AlgoVeria authored and GitHub committed 2026-11-08 18:45:54 +01:00
1 parent e7758934a8
commit b5eace85db
15 files changed
+1137

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
import { describe, expect, it } from 'vitest';
import {
formatSignalNumber,
getCurrentLoopStatus,
getLinearTransform,
getSignalRangeStatus,
scaleValue,
toPercentOfRange,
} from './analog-signal';
describe('analog-signal', () => {
describe('scaleValue', () => {
it('maps a 4-20 mA current onto an engineering range', () => {
const from = { min: 4, max: 20 };
const to = { min: 0, max: 10 };
expect(scaleValue({ value: 4, from, to })).toBe(0);
expect(scaleValue({ value: 12, from, to })).toBe(5);
expect(scaleValue({ value: 20, from, to })).toBe(10);
});
it('maps the other way round, from engineering value to current', () => {
expect(scaleValue({ value: 2.5, from: { min: 0, max: 10 }, to: { min: 4, max: 20 } })).toBe(8);
});
it('handles negative and inverted ranges', () => {
expect(scaleValue({ value: 0, from: { min: -27648, max: 27648 }, to: { min: -10, max: 10 } })).toBe(0);
expect(scaleValue({ value: 27648, from: { min: -27648, max: 27648 }, to: { min: -10, max: 10 } })).toBe(10);
expect(scaleValue({ value: 4, from: { min: 4, max: 20 }, to: { min: 100, max: 0 } })).toBe(100);
});
it('scales Siemens raw values', () => {
expect(scaleValue({ value: 13824, from: { min: 0, max: 27648 }, to: { min: 0, max: 100 } })).toBe(50);
});
it('extrapolates outside of the range', () => {
expect(scaleValue({ value: 0, from: { min: 4, max: 20 }, to: { min: 0, max: 16 } })).toBe(-4);
});
it('throws on an empty input range', () => {
expect(() => scaleValue({ value: 1, from: { min: 4, max: 4 }, to: { min: 0, max: 10 } })).toThrow();
expect(() => scaleValue({ value: 1, from: { min: Number.NaN, max: 4 }, to: { min: 0, max: 10 } })).toThrow();
});
});
describe('getLinearTransform', () => {
it('returns gain and offset of y = gain * x + offset', () => {
expect(getLinearTransform({ from: { min: 4, max: 20 }, to: { min: 0, max: 16 } })).toEqual({
gain: 1,
offset: -4,
});
});
});
describe('toPercentOfRange', () => {
it('gives the position in the range', () => {
expect(toPercentOfRange({ value: 8, range: { min: 4, max: 20 } })).toBe(25);
expect(toPercentOfRange({ value: 2, range: { min: 2, max: 10 } })).toBe(0);
});
});
describe('getSignalRangeStatus', () => {
it('detects values outside of the range', () => {
expect(getSignalRangeStatus({ value: 5, range: { min: 0, max: 10 } })).toBe('ok');
expect(getSignalRangeStatus({ value: -0.1, range: { min: 0, max: 10 } })).toBe('below-range');
expect(getSignalRangeStatus({ value: 10.1, range: { min: 0, max: 10 } })).toBe('above-range');
expect(getSignalRangeStatus({ value: 5, range: { min: 10, max: 0 } })).toBe('ok');
});
});
describe('getCurrentLoopStatus', () => {
it('follows NAMUR NE 43 limits', () => {
expect(getCurrentLoopStatus(0)).toBe('failure-low');
expect(getCurrentLoopStatus(3.6)).toBe('failure-low');
expect(getCurrentLoopStatus(3.7)).toBe('uncertain-low');
expect(getCurrentLoopStatus(3.8)).toBe('under-range');
expect(getCurrentLoopStatus(4)).toBe('ok');
expect(getCurrentLoopStatus(20)).toBe('ok');
expect(getCurrentLoopStatus(20.5)).toBe('over-range');
expect(getCurrentLoopStatus(20.8)).toBe('uncertain-high');
expect(getCurrentLoopStatus(21)).toBe('failure-high');
});
});
describe('formatSignalNumber', () => {
it('removes float noise', () => {
expect(formatSignalNumber(0.1 + 0.2)).toBe('0.3');
expect(formatSignalNumber(12.345678)).toBe('12.3457');
expect(formatSignalNumber(-0.00001)).toBe('0');
expect(formatSignalNumber(Number.NaN)).toBe('');
});
});
});
+95
View File
@@ -0,0 +1,95 @@
export interface SignalRange {
min: number;
max: number;
}
export interface LinearTransform {
gain: number;
offset: number;
}
export type CurrentLoopStatus =
| 'ok'
| 'under-range'
| 'over-range'
| 'uncertain-low'
| 'uncertain-high'
| 'failure-low'
| 'failure-high';
export type SignalRangeStatus = 'ok' | 'below-range' | 'above-range';
function assertNonEmptyRange(range: SignalRange) {
if (!Number.isFinite(range.min) || !Number.isFinite(range.max) || range.min === range.max) {
throw new Error('Invalid range: min and max must be finite and different');
}
}
// y = gain * x + offset, mapping `from` onto `to` (what PLC scaling blocks compute).
export function getLinearTransform({ from, to }: { from: SignalRange; to: SignalRange }): LinearTransform {
assertNonEmptyRange(from);
const gain = (to.max - to.min) / (from.max - from.min);
return { gain, offset: to.min - gain * from.min };
}
export function scaleValue({ value, from, to }: { value: number; from: SignalRange; to: SignalRange }): number {
const { gain, offset } = getLinearTransform({ from, to });
return gain * value + offset;
}
export function toPercentOfRange({ value, range }: { value: number; range: SignalRange }): number {
return scaleValue({ value, from: range, to: { min: 0, max: 100 } });
}
export function getSignalRangeStatus({ value, range }: { value: number; range: SignalRange }): SignalRangeStatus {
const low = Math.min(range.min, range.max);
const high = Math.max(range.min, range.max);
if (value < low) {
return 'below-range';
}
if (value > high) {
return 'above-range';
}
return 'ok';
}
// NAMUR NE 43 limits for 4-20 mA transmitters.
export const NAMUR_NE43 = {
failureLowMax: 3.6,
measurementMin: 3.8,
measurementMax: 20.5,
failureHighMin: 21,
} as const;
export function getCurrentLoopStatus(current: number): CurrentLoopStatus {
if (current <= NAMUR_NE43.failureLowMax) {
return 'failure-low';
}
if (current < NAMUR_NE43.measurementMin) {
return 'uncertain-low';
}
if (current < 4) {
return 'under-range';
}
if (current <= 20) {
return 'ok';
}
if (current <= NAMUR_NE43.measurementMax) {
return 'over-range';
}
if (current < NAMUR_NE43.failureHighMin) {
return 'uncertain-high';
}
return 'failure-high';
}
// Avoids float noise such as 0.30000000000000004 in displayed results.
export function formatSignalNumber(value: number, maxDecimals = 4): string {
if (!Number.isFinite(value)) {
return '';
}
const rounded = Number(value.toFixed(maxDecimals));
return Object.is(rounded, -0) ? '0' : String(rounded);
}