From b5eace85dbdb247f832b2aa9967b9d570a84d1ce Mon Sep 17 00:00:00 2001 From: AlgoVeria Date: Sun, 8 Nov 2026 18:45:54 +0100 Subject: [PATCH] feat(new tools): 4-20 mA, 0-10 V and analog scaling converters (#596) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- locales/en.yml | 55 ++ locales/fr.yml | 55 ++ src/components/AnalogGauge.vue | 135 +++++ src/components/AnalogSignalConverter.vue | 468 ++++++++++++++++++ .../analog-scaling-calculator.e2e.spec.ts | 39 ++ .../analog-scaling-calculator.vue | 25 + src/tools/analog-scaling-calculator/index.ts | 25 + .../current-loop-4-20ma-converter.e2e.spec.ts | 37 ++ .../current-loop-4-20ma-converter.vue | 22 + .../current-loop-4-20ma-converter/index.ts | 26 + src/tools/voltage-0-10v-converter/index.ts | 13 + .../voltage-0-10v-converter.e2e.spec.ts | 25 + .../voltage-0-10v-converter.vue | 24 + src/utils/analog-signal.test.ts | 93 ++++ src/utils/analog-signal.ts | 95 ++++ 15 files changed, 1137 insertions(+) create mode 100644 src/components/AnalogGauge.vue create mode 100644 src/components/AnalogSignalConverter.vue create mode 100644 src/tools/analog-scaling-calculator/analog-scaling-calculator.e2e.spec.ts create mode 100644 src/tools/analog-scaling-calculator/analog-scaling-calculator.vue create mode 100644 src/tools/analog-scaling-calculator/index.ts create mode 100644 src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.e2e.spec.ts create mode 100644 src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.vue create mode 100644 src/tools/current-loop-4-20ma-converter/index.ts create mode 100644 src/tools/voltage-0-10v-converter/index.ts create mode 100644 src/tools/voltage-0-10v-converter/voltage-0-10v-converter.e2e.spec.ts create mode 100644 src/tools/voltage-0-10v-converter/voltage-0-10v-converter.vue create mode 100644 src/utils/analog-signal.test.ts create mode 100644 src/utils/analog-signal.ts diff --git a/locales/en.yml b/locales/en.yml index 184473c1..bf50ea84 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -5177,6 +5177,7 @@ tools: RAID 0 splits data evenly across 2 or more disks without redunancy or fault tolerance creating one large storage space. categories: + automation: Automation favorite-tools: Your favorite tools crypto: Crypto converter: Converter @@ -5207,6 +5208,27 @@ tools: finance: Finance default: Default toml: TOML + current-loop-4-20ma-converter: + title: 4-20 mA converter + description: Convert a 4-20 mA (or 0-20 mA) current into an engineering value and back, with NAMUR NE 43 loop diagnostic. + texts: + signal-label: Current + voltage-0-10v-converter: + title: 0-10 V converter + description: Convert a 0-10 V, 2-10 V, 0-5 V, 1-5 V or ±10 V voltage into an engineering value and back. + texts: + signal-label: Voltage + analog-scaling-calculator: + title: Analog scaling calculator + description: Scale a raw PLC analog value (Siemens 0-27648, Schneider 0-10000, 12/15/16 bits...) into an engineering value and back. + texts: + signal-label: Raw value + preset-siemens-unipolar: Siemens S7 unipolar (0 to 27648) + preset-siemens-bipolar: Siemens S7 bipolar (-27648 to 27648) + preset-schneider: Schneider M340/M580 (0 to 10000) + preset-12-bits: 12 bits (0 to 4095) + preset-15-bits: 15 bits (0 to 32767) + preset-16-bits: 16 bits (0 to 65535) websocket-tester: title: Websocket tester description: Allows to test WebSocket connections @@ -10108,6 +10130,39 @@ tools: description: Test your speakers and headphones by playing test sounds. Adjust volume and check for audio clarity. externalAccess: "null" +analogSignalConverter: + texts: + title-input: Input + title-parameters: Parameters + title-output: Output + swap-direction: Swap direction + status-ok: In range + copy-output: Copy the output value + title-reference-points: Reference points + label-signal-type: Signal type + preset-custom: Custom + label-engineering-range: Engineering min / max + label-engineering-unit: Engineering unit + placeholder-unit: Unit + error-signal-range: The signal min must be lower than the signal max. + error-engineering-range: The engineering min must be lower than the engineering max. + label-engineering-value: Engineering value + label-rounded-signal: Rounded (integer) + status-below-range: The signal is below the range. + status-above-range: The signal is above the range. + status-below-live-zero: The signal is below the live zero, check for a broken wire or a faulty sensor. + engineering-below-range: The value is below the engineering range, the signal will be out of range. + engineering-above-range: The value is above the engineering range, the signal will be out of range. + loop-status-failure-low: "Low failure (≤ 3.6 mA, NAMUR NE 43): broken loop, unpowered or faulty transmitter." + loop-status-uncertain-low: "Between 3.6 and 3.8 mA: undefined zone, a failure is likely (NAMUR NE 43)." + loop-status-under-range: "Under-range (3.8 to 4 mA): the measurement is saturated at the bottom of the range." + loop-status-over-range: "Over-range (20 to 20.5 mA): the measurement is saturated at the top of the range." + loop-status-uncertain-high: "Between 20.5 and 21 mA: undefined zone, a failure is likely (NAMUR NE 43)." + loop-status-failure-high: "High failure (≥ 21 mA, NAMUR NE 43): faulty transmitter or short circuit." + label-formula: Formula + formula-value: value + formula-signal: signal + column-percent: Range collapsibleToolMenu: text: expanding: Expanding... diff --git a/locales/fr.yml b/locales/fr.yml index 4f51f637..b18834d4 100644 --- a/locales/fr.yml +++ b/locales/fr.yml @@ -147,6 +147,7 @@ tools: label-jpeg: JPEG label-webp: WebP categories: + automation: Automatisme favorite-tools: Vos outils favoris crypto: Cryptographie converter: Convertisseur @@ -177,6 +178,27 @@ tools: finance: Finance default: Défaut toml: TOML + current-loop-4-20ma-converter: + title: Convertisseur 4-20 mA + description: Convertir un courant 4-20 mA (ou 0-20 mA) en valeur physique et inversement, avec le diagnostic de boucle NAMUR NE 43. + texts: + signal-label: Courant + voltage-0-10v-converter: + title: Convertisseur 0-10 V + description: Convertir une tension 0-10 V, 2-10 V, 0-5 V, 1-5 V ou ±10 V en valeur physique et inversement. + texts: + signal-label: Tension + analog-scaling-calculator: + title: Mise à l'échelle analogique + description: Convertir une valeur brute d'entrée analogique d'automate (Siemens 0-27648, Schneider 0-10000, 12/15/16 bits...) en valeur physique et inversement. + texts: + signal-label: Valeur brute + preset-siemens-unipolar: Siemens S7 unipolaire (0 à 27648) + preset-siemens-bipolar: Siemens S7 bipolaire (-27648 à 27648) + preset-schneider: Schneider M340/M580 (0 à 10000) + preset-12-bits: 12 bits (0 à 4095) + preset-15-bits: 15 bits (0 à 32767) + preset-16-bits: 16 bits (0 à 65535) token-generator: title: Générateur de token description: Génère une chaîne aléatoire avec les caractères que vous voulez, @@ -10558,6 +10580,39 @@ textareaCopyable: download: Télécharger locale-selector: select-a-language: Sélectionnez une langue +analogSignalConverter: + texts: + title-input: Entrée + title-parameters: Paramètres + title-output: Sortie + swap-direction: Inverser le sens + status-ok: Dans la plage + copy-output: Copier la valeur de sortie + title-reference-points: Points de référence + label-signal-type: Type de signal + preset-custom: Personnalisé + label-engineering-range: Plage physique min / max + label-engineering-unit: Unité physique + placeholder-unit: Unité + error-signal-range: Le min du signal doit être inférieur au max du signal. + error-engineering-range: Le min de la plage physique doit être inférieur au max. + label-engineering-value: Valeur physique + label-rounded-signal: Arrondi (entier) + status-below-range: Le signal est sous la plage. + status-above-range: Le signal est au-dessus de la plage. + status-below-live-zero: Le signal est sous le zéro décalé, vérifier une rupture de fil ou un capteur en défaut. + engineering-below-range: La valeur est sous la plage physique, le signal sera hors plage. + engineering-above-range: La valeur est au-dessus de la plage physique, le signal sera hors plage. + loop-status-failure-low: "Défaut bas (≤ 3,6 mA, NAMUR NE 43) : boucle coupée, transmetteur non alimenté ou en défaut." + loop-status-uncertain-low: "Entre 3,6 et 3,8 mA : zone indéterminée, défaut probable (NAMUR NE 43)." + loop-status-under-range: "Sous-échelle (3,8 à 4 mA) : mesure saturée en bas de plage." + loop-status-over-range: "Sur-échelle (20 à 20,5 mA) : mesure saturée en haut de plage." + loop-status-uncertain-high: "Entre 20,5 et 21 mA : zone indéterminée, défaut probable (NAMUR NE 43)." + loop-status-failure-high: "Défaut haut (≥ 21 mA, NAMUR NE 43) : transmetteur en défaut ou court-circuit." + label-formula: Formule + formula-value: valeur + formula-signal: signal + column-percent: Plage collapsibleToolMenu: text: expanding: Réduire... diff --git a/src/components/AnalogGauge.vue b/src/components/AnalogGauge.vue new file mode 100644 index 00000000..e62eb176 --- /dev/null +++ b/src/components/AnalogGauge.vue @@ -0,0 +1,135 @@ + + + + + diff --git a/src/components/AnalogSignalConverter.vue b/src/components/AnalogSignalConverter.vue new file mode 100644 index 00000000..a8605556 --- /dev/null +++ b/src/components/AnalogSignalConverter.vue @@ -0,0 +1,468 @@ + + + + + diff --git a/src/tools/analog-scaling-calculator/analog-scaling-calculator.e2e.spec.ts b/src/tools/analog-scaling-calculator/analog-scaling-calculator.e2e.spec.ts new file mode 100644 index 00000000..3855d9cd --- /dev/null +++ b/src/tools/analog-scaling-calculator/analog-scaling-calculator.e2e.spec.ts @@ -0,0 +1,39 @@ +import { expect, test } from '@playwright/test'; + +test.describe('Tool - Analog scaling calculator', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/analog-scaling-calculator'); + }); + + test('Has correct title', async ({ page }) => { + await expect(page).toHaveTitle('Analog scaling calculator - IT Tools'); + }); + + test('Scales a Siemens raw value', async ({ page }) => { + await page.getByTestId('converter-input').locator('input').fill('20736'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('converter-output')).toHaveText('75 %'); + await expect(page.getByTestId('formula').locator('input')).toHaveValue('value = 0.003616898148 × signal'); + }); + + test('Gives the rounded raw value in the other direction', async ({ page }) => { + await page.getByTestId('swap-direction').click(); + await page.getByTestId('converter-input').locator('input').fill('33.3'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('converter-output')).toHaveText('9206.784'); + await expect(page.getByTestId('converter-output-rounded')).toContainText('9207'); + }); + + test('Rejects a range whose min is not lower than its max', async ({ page }) => { + const min = page.getByTestId('engineering-min').locator('input'); + await min.fill('200'); + await min.press('Enter'); + await expect(page.getByText('The engineering min must be lower than the engineering max.')).toBeVisible(); + await expect(page.getByTestId('converter-output')).toHaveText('—'); + await expect(page.getByTestId('output-gauge')).toHaveCount(0); + + await min.fill('-50'); + await min.press('Enter'); + await expect(page.getByTestId('converter-output')).not.toHaveText('—'); + }); +}); diff --git a/src/tools/analog-scaling-calculator/analog-scaling-calculator.vue b/src/tools/analog-scaling-calculator/analog-scaling-calculator.vue new file mode 100644 index 00000000..ea603f86 --- /dev/null +++ b/src/tools/analog-scaling-calculator/analog-scaling-calculator.vue @@ -0,0 +1,25 @@ + + + diff --git a/src/tools/analog-scaling-calculator/index.ts b/src/tools/analog-scaling-calculator/index.ts new file mode 100644 index 00000000..09223eb8 --- /dev/null +++ b/src/tools/analog-scaling-calculator/index.ts @@ -0,0 +1,25 @@ +import { defineTool } from '../tool'; +import { translate as t } from '@/plugins/i18n.plugin'; + +export const tool = defineTool({ + name: t('tools.analog-scaling-calculator.title'), + path: '/analog-scaling-calculator', + description: t('tools.analog-scaling-calculator.description'), + keywords: [ + 'analog', + 'scaling', + 'raw', + 'plc', + 'siemens', + '27648', + 'schneider', + 'norm_x', + 'scale_x', + 'engineering', + 'unit', + ], + component: () => import('./analog-scaling-calculator.vue'), + icon: defineAsyncComponent(() => import('@vicons/tabler/es/AdjustmentsHorizontal')), + createdAt: new Date('2026-11-03'), + category: 'Automation', +}); diff --git a/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.e2e.spec.ts b/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.e2e.spec.ts new file mode 100644 index 00000000..32f54572 --- /dev/null +++ b/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.e2e.spec.ts @@ -0,0 +1,37 @@ +import { expect, test } from '@playwright/test'; + +test.describe('Tool - 4-20 mA converter', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/current-loop-4-20ma-converter'); + }); + + test('Has correct title', async ({ page }) => { + await expect(page).toHaveTitle('4-20 mA converter - IT Tools'); + }); + + test('Converts a current into an engineering value', async ({ page }) => { + await page.getByTestId('converter-input').locator('input').fill('8'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('converter-output')).toHaveText('2.5 bar'); + await expect(page.getByTestId('input-status')).toContainText('In range'); + }); + + test('Swaps the direction and keeps the operating point', async ({ page }) => { + await page.getByTestId('converter-input').locator('input').fill('16'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('converter-output')).toHaveText('7.5 bar'); + + await page.getByTestId('swap-direction').click(); + await expect(page.getByTestId('converter-input').locator('input')).toHaveValue('7.5'); + await expect(page.getByTestId('converter-output')).toHaveText('16 mA'); + await expect(page.getByTestId('formula').locator('input')).toHaveValue('signal = 1.6 × value + 4'); + + await page.getByTestId('swap-direction').click(); + }); + + test('Reports a NAMUR NE 43 loop failure', async ({ page }) => { + await page.getByTestId('converter-input').locator('input').fill('2'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('input-status')).toContainText('Low failure'); + }); +}); diff --git a/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.vue b/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.vue new file mode 100644 index 00000000..c98cfc5c --- /dev/null +++ b/src/tools/current-loop-4-20ma-converter/current-loop-4-20ma-converter.vue @@ -0,0 +1,22 @@ + + + diff --git a/src/tools/current-loop-4-20ma-converter/index.ts b/src/tools/current-loop-4-20ma-converter/index.ts new file mode 100644 index 00000000..028d3689 --- /dev/null +++ b/src/tools/current-loop-4-20ma-converter/index.ts @@ -0,0 +1,26 @@ +import { defineTool } from '../tool'; +import { translate as t } from '@/plugins/i18n.plugin'; + +export const tool = defineTool({ + name: t('tools.current-loop-4-20ma-converter.title'), + path: '/current-loop-4-20ma-converter', + description: t('tools.current-loop-4-20ma-converter.description'), + keywords: [ + '4-20', + 'ma', + 'milliampere', + 'current', + 'loop', + 'analog', + 'sensor', + 'transmitter', + 'namur', + 'ne43', + 'plc', + 'scaling', + ], + component: () => import('./current-loop-4-20ma-converter.vue'), + icon: defineAsyncComponent(() => import('@vicons/tabler/es/Gauge')), + createdAt: new Date('2026-11-03'), + category: 'Automation', +}); diff --git a/src/tools/voltage-0-10v-converter/index.ts b/src/tools/voltage-0-10v-converter/index.ts new file mode 100644 index 00000000..ff693be4 --- /dev/null +++ b/src/tools/voltage-0-10v-converter/index.ts @@ -0,0 +1,13 @@ +import { defineTool } from '../tool'; +import { translate as t } from '@/plugins/i18n.plugin'; + +export const tool = defineTool({ + name: t('tools.voltage-0-10v-converter.title'), + path: '/voltage-0-10v-converter', + description: t('tools.voltage-0-10v-converter.description'), + keywords: ['0-10', 'volt', 'voltage', 'analog', 'sensor', '1-5', '2-10', 'plc', 'scaling'], + component: () => import('./voltage-0-10v-converter.vue'), + icon: defineAsyncComponent(() => import('@vicons/tabler/es/Bolt')), + createdAt: new Date('2026-11-03'), + category: 'Automation', +}); diff --git a/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.e2e.spec.ts b/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.e2e.spec.ts new file mode 100644 index 00000000..63f34708 --- /dev/null +++ b/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.e2e.spec.ts @@ -0,0 +1,25 @@ +import { expect, test } from '@playwright/test'; + +test.describe('Tool - 0-10 V converter', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/voltage-0-10v-converter'); + }); + + test('Has correct title', async ({ page }) => { + await expect(page).toHaveTitle('0-10 V converter - IT Tools'); + }); + + test('Converts a voltage into an engineering value', async ({ page }) => { + await page.getByTestId('converter-input').locator('input').fill('2.5'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('converter-output')).toHaveText('25 %'); + }); + + test('Warns below the live zero of a 2-10 V signal', async ({ page }) => { + await page.getByTestId('signal-min').locator('input').fill('2'); + await page.getByTestId('signal-min').locator('input').press('Enter'); + await page.getByTestId('converter-input').locator('input').fill('1'); + await page.getByTestId('converter-input').locator('input').press('Enter'); + await expect(page.getByTestId('input-status')).toContainText('live zero'); + }); +}); diff --git a/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.vue b/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.vue new file mode 100644 index 00000000..16919111 --- /dev/null +++ b/src/tools/voltage-0-10v-converter/voltage-0-10v-converter.vue @@ -0,0 +1,24 @@ + + + diff --git a/src/utils/analog-signal.test.ts b/src/utils/analog-signal.test.ts new file mode 100644 index 00000000..fbb17535 --- /dev/null +++ b/src/utils/analog-signal.test.ts @@ -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(''); + }); + }); +}); diff --git a/src/utils/analog-signal.ts b/src/utils/analog-signal.ts new file mode 100644 index 00000000..b0b19923 --- /dev/null +++ b/src/utils/analog-signal.ts @@ -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); +}