]> git.earman.xyz Git - emacsinit.git/commitdiff
Add wrapper to scale to convert signals to counts
authorrearman <rearman@r717.net>
Tue, 26 Aug 2025 16:33:33 +0000 (12:33 -0400)
committerrearman <rearman@r717.net>
Tue, 26 Aug 2025 16:33:33 +0000 (12:33 -0400)
re/re-plc-defuns.el

index 554beceae345b422ea72878771eeeb833e701273..b55020e42e55d71134ddb6dd9d47e5e9f58ace2f 100644 (file)
@@ -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))