From: rearman Date: Tue, 26 Aug 2025 16:33:33 +0000 (-0400) Subject: Add wrapper to scale to convert signals to counts X-Git-Url: https://git.earman.xyz/?a=commitdiff_plain;h=5fad6fd9268a101179f51d0ccc26f2aeac14f040;p=emacsinit.git Add wrapper to scale to convert signals to counts --- diff --git a/re/re-plc-defuns.el b/re/re-plc-defuns.el index 554bece..b55020e 100644 --- a/re/re-plc-defuns.el +++ b/re/re-plc-defuns.el @@ -55,11 +55,24 @@ Converts the amperage to a voltage using `amps->volts',then applies "Calculate offset given Divisor and both minimums." (- scaled-min (/ raw-min divisor))) -(defun scale (raw-max raw-min scaled-max scaled-min) - "Calculate the divisor and offset, given the parameters." +(defun count-scale (raw-max raw-min scaled-max scaled-min) + "Calculate the divisor and offset, from counts and eng values." (let* ((div (divisor raw-max raw-min scaled-max scaled-min))) (list div (offset div scaled-min raw-min)))) +(defun scale (signal-max signal-min engineering-max engineering-min &optional impedance) + "Calculate the divisor and offset, from signal and eng values. +Current signal if `impedance' is not nil, otherwise voltage signal. +Uses `count-range', which uses `*plc-resolution*' and `*plc-max-voltage*' +variables." + (let* ((rng (if impedance + (count-range (amps->volts signal-max impedance) + (amps->volts signal-min impedance)) + (count-range signal-max signal-min))) + (pmax (car rng)) + (pmin (cadr rng))) + (count-scale pmax pmin engineering-max engineering-min))) + (defun final (analog-input divisor offset) "Calculate the scaled value of an input." (+ (/ analog-input divisor) offset))