From 6b5df732eb3d3ab4ae9b991b859377694a396235 Mon Sep 17 00:00:00 2001 From: rearman Date: Fri, 12 Sep 2025 15:11:57 -0400 Subject: [PATCH] Cleanup, add docstrings --- electrical.lisp | 17 +++++++-- plc.lisp | 65 +++++++++++++++++++++++++--------- refrigeration.lisp | 45 ++++++++++++++++-------- unit-conv.lisp | 88 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 172 insertions(+), 43 deletions(-) diff --git a/electrical.lisp b/electrical.lisp index c3f9474..b20e288 100644 --- a/electrical.lisp +++ b/electrical.lisp @@ -2,8 +2,19 @@ (in-package #:work-env) -(defun eff-imp (r1 r2) (/ (+ (inv r1) (inv r2)))) +(defun eff-imp (r1 r2) + "Calculate the effective input impedance, given two resistances. +For use in `amps->volts' and related." + (/ (+ (inv r1) (inv r2)))) -(defun amps->volts (amps imp) (* amps imp)) +(defun amps->volts (amps imp) + "Calculate the voltage, given amperage and impedance. +Multiplies the amperage by the effective impedance calculated +with `eff-imp'." + (* amps imp)) -(defun volts->amps (volts imp) (/ volts imp)) +(defun volts->amps (volts imp) + "Calculate the amperage, given voltage and impedance. +Divides the voltage by the effective impedance calculated with +`eff-imp'." + (/ volts imp)) diff --git a/plc.lisp b/plc.lisp index 9434c41..f4ae218 100644 --- a/plc.lisp +++ b/plc.lisp @@ -2,46 +2,77 @@ (in-package #:work-env) -(defparameter *volt-max* 10.0) -(defparameter *resolution* 16) +(defparameter *plc-max-voltage* 10.0) +(defparameter *plc-resolution* 16) (defparameter *impedance* 249) -(defun 13-bit! () (setq *resolution* 13 *volt-max* 10.0)) -(defun 16-bit! () (setq *resolution* 16 *volt-max* 10.0)) +(defun 13-bit! () (setq *plc-resolution* 13 *plc-max-voltage* 10.0)) +(defun 16-bit! () (setq *plc-resolution* 16 *plc-max-voltage* 10.0)) (setf (fdefinition 'p3k!) #'16-bit!) (setf (fdefinition 'slc!) #'16-bit!) (setf (fdefinition 'p1k!) #'13-bit!) (setf (fdefinition 'dl4!) #'13-bit!) -(defun max-counts (resolution) (- (^ 2 resolution) 1)) +(defun max-counts (resolution) + "Calculate the max count for a PLC analog, given card's bit-resolution." + (- (^ 2 resolution) 1)) (defun volts->counts (vin) - (float (* (/ vin *volt-max*) (max-counts *resolution*)))) + "Convert a voltage signal to a PLC count. +Calculates a ratio of vin/vmax, then scales by `max-counts'." + (float (* (/ vin *plc-max-voltage*) (max-counts *plc-resolution*)))) (defun counts->volts (cin) - (float (* (/ cin (max-counts *resolution*)) *volt-max*))) + "Convert a PLC count to a voltage. +Calculates a ratio of cin/`max-counts', then multiplies by vmax." + (float (* (/ cin (max-counts *plc-resolution*)) *plc-max-voltage*))) -(defun amps->counts (amps imp) (volts->counts (amps->volts amps imp))) +(defun amps->counts (amps imp) + "Convert a current signal to PLC Counts. +Converts the amperage to a voltage using `amps->volts',then applies + `volts->counts' to the resulting voltage,vmax, and resolution." + (volts->counts (amps->volts amps imp))) (defun count-range (upper lower) + "Given an upper and lower voltage, return the list of upper and lower PLC counts." (list (volts->counts upper) (volts->counts lower))) (defun volt-range (upper lower) + "Given an upper and lower PLC count, return the list of upper and lower voltage." (list (counts->volts upper) (counts->volts lower))) -(defun divisor (count-max count-min scaled-max scaled-min) - (/ (float (- count-max count-min)) (- scaled-max scaled-min))) +(defun divisor (count-max count-min engineering-max engineering-min) + "Calculate 1/slope given PLC Max/Min and Eng. Max/Min." + (/ (float (- count-max count-min)) (- engineering-max engineering-min))) -(defun offset (count-min scaled-min divisor) - (- scaled-min (/ count-min divisor))) +(defun offset (divisor engineering-min count-min) + "Calculate offset given Divisor and both minimums." + (- engineering-min (/ count-min divisor))) -(defun scale (count-max count-min scaled-max scaled-min) - (let ((div (divisor count-max count-min scaled-max scaled-min))) - (list div (offset count-min scaled-min div)))) +(defun count-scale (counts-max counts-min engineering-max engineering-min) + "Calculate the divisor and offset, from counts and eng values." + (let* ((div (divisor counts-max counts-min engineering-max engineering-min)) + (ofst (offset div engineering-min counts-min))) + (list div ofst))) -(defun final (analog-input divisor offset) - (+ offset (/ analog-input divisor))) +(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 (null impedance) + (count-range signal-max signal-min) + (count-range (amps->volts signal-max impedance) + (amps->volts signal-min impedance)))) + (counts-max (first rng)) + (counts-min (second rng))) + (count-scale counts-max counts-min engineering-max engineering-min))) + +(defun final (counts divisor offset) + "Calculate the scaled value of an input." + (+ (/ counts divisor) offset)) (defun input (scaled-value divisor offset) + "Calculate the input counts from a scaled value." (* divisor (- scaled-value offset))) diff --git a/refrigeration.lisp b/refrigeration.lisp index 105a4e5..2b33f2c 100644 --- a/refrigeration.lisp +++ b/refrigeration.lisp @@ -2,24 +2,39 @@ (in-package #:work-env) -(defun cfm-circ (fpm radius) (float (* pi fpm (square (/ radius 12))))) +;;; Cooling + +(defun cfm-circ (fpm radius) + "Calculate the CFM of a circular duct. +Inputs are feet/minute and radius (ft)." + (float (* pi fpm (square radius)))) (defun cfm-rect (fpm width height) - (float (* fpm (/ width 12) (/ height 12)))) + "Calculate the CFM of a rectangular duct. +Inputs are feet/minute, width (ft) and height (ft)." + (float (* fpm width height))) (defun sat-water-press (temp) - (+ 0.0182795 - (* temp 0.001029904) - (* (square temp) 2.579408e-05) - (* (cube temp) 2.400493e-07) - (* (^ temp 4) 8.100939e-10) - (* (^ temp 5) 3.256805e-11) - (* (^ temp 6) -1.001922e-13) - (* (^ temp 7) 2.44161e-16))) - -(defun hum-ratio (humidity swp) - (let ((hum-press (* (/ humidity 100.0) swp))) - (/ (* hum-press 0.62198) (- 14.7 hum-press)))) + "Calculate the saturated water pressure at a temp (F)." + (+ (E 1.82795 -2) + (* temp (E 1.029904 -3)) + (* (^ temp 2) (E 2.579408 -5)) + (* (^ temp 3) (E 2.400493 -7)) + (* (^ temp 4) (E 8.100939 -10)) + (* (^ temp 5) (E 3.256805 -11)) + (* (^ temp 6) (E -1.001922 -13)) + (* (^ temp 7) (E 2.44161 -16)))) + +(defun hum-press (humidity swp) + "Calculate the Humidity-Pressure." + (* (/ humidity 100.0) swp)) + +(defun hum-ratio (humidity-pressure) + "Calulate the humidity ratio. +Takes humidity-pressure." + (/ (* humidity-pressure 0.62198) (- 14.7 humidity-pressure))) (defun gn-water-per-lb (humidity temp) - (* 7000 (hum-ratio humidity (sat-water-press temp)))) + "Calculate the grains of water per lb of air. +Takes humidity in %RH, and temp in F." + (* 7000 (hum-ratio (hum-press humidity (sat-water-press temp))))) diff --git a/unit-conv.lisp b/unit-conv.lisp index 87ee679..e145a82 100644 --- a/unit-conv.lisp +++ b/unit-conv.lisp @@ -2,18 +2,90 @@ (in-package #:work-env) -(defun mm->in (mm) (/ mm 25.4)) +;;; Length -(defun in->mm (in) (* in 25.4)) +(defun mm->in (mm) + "Convert milimeters to inches." + (/ mm 25.4)) -(defun k->c (tempk) (- tempk 273.15)) +(defun in->mm (in) + "Convert inches to milimeters." + (* in 25.4)) + +;;; Heat -(defun c->k (tempc) (+ tempc 273.15)) +(defun k->c (tempk) + "Convert Degrees Kelvin to Degrees Celsius." + (- tempk 273.15)) -(defun c->f (tempc) (+ (* tempc 1.8) 32)) +(defun c->k (tempc) + "Convert Degrees Celsius to Degrees Kelvin." + (+ tempc 273.15)) -(defun f->c (tempf) (/ (- tempf 32) 1.8)) +(defun c->f (tempc) + "Convert Degrees Celsius to Degrees Fahrenheit." + (+ (* tempc 1.8) 32)) -(defun k->f (tempk) (c->f (k->c tempk))) +(defun f->c (tempf) + "Convert Degrees Fahrenheit to Degrees Celsius." + (/ (- tempf 32) 1.8)) -(defun f->k (tempf) (c->k (f->c tempf))) +(defun k->f (tempk) + "Convert Degrees Kelvin to Degrees Fahrenheit." + (c->f (k->c tempk))) + +(defun f->k (tempf) + "Convert Degrees Fahrenheit to Degrees Kelvin." + (c->k (f->c tempf))) + +;;; Pressure + +(defun bar->psi (bar) + "Convert bar to psi." + (* 14.5 bar)) + +(defun psi->bar (psi) + "Convert psi to bar." + (/ psi 14.5)) + +(defun kpa->psi (kpa) + "Convert kpa to psi." + (* 6.89 kpa)) + +(defun psi->kpa (psi) + "Convert psi to kpa." + (/ psi 6.89)) + +(defun mmhg->psi (mmhg) + "Convert mmhg to psi." + (/ mmhg 51.71)) + +(defun psi->mmhg (psi) + "Convert psi to mmhg." + (* 51.71 psi)) + +(defun inhg->psi (inhg) + "Convert inhg to psi." + (mmhg->psi (in->mm inhg))) + +(defun psi->inhg (psi) + "Convert psi to inhg." + (mm->in (psi->mmhg psi))) + +;;; Weight + +(defun g->oz (grams) + "Convert grams to ounces." + (/ grams 28.34)) + +(defun oz->g (ounces) + "Convert ounces to grams." + (* ounces 28.34)) + +(defun kg->lb (kilograms) + "Convert kilograms to pounds." + (* kilograms 2.2)) + +(defun lb->kg (pounds) + "Convert pounds to kilograms." + (/ pounds 2.2)) -- 2.39.5