Created
March 2, 2022 17:42
-
-
Save LuisThiamNye/6bcaf2d63276d8509f5800580ce07750 to your computer and use it in GitHub Desktop.
Code used to determine the optimal values for resistance of the subtractor amplifier in an electronic level for the IEP design project given a limited selection of resistor values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns subtractor-resistance | |
(:require | |
[babashka.fs :as fs] | |
[clojure.pprint :as pp] | |
[clojure.string :as str] | |
;; [clojure.core.async :as async :refer [go <!]] | |
[clojure.java.io :as io] | |
[babashka.process :as proc :refer [process sh]] | |
#?@(:bb [[babashka.deps :as deps]] | |
:clj [[oz.core :as oz] | |
[borkdude.dynaload :refer [dynaload]]]))) | |
#?(:bb (deps/add-deps '{:deps {org.clojure/math.combinatorics {:mvn/version "0.1.6"}}})) | |
(require '[clojure.math.combinatorics :as combo]) | |
;; in kΩ | |
(def values #{1 | |
1.5 | |
2.2 | |
4.7 | |
6.8 | |
10 | |
20 | |
47 | |
68 | |
100 | |
200 | |
470}) | |
(def all-circuits | |
(let [resistor-permutations (combo/permuted-combinations (vec values) 4)] | |
(into [] | |
(map (fn [[r1 r2 r3 r4]] | |
{:r1 r1 :r2 r2 :r3 r3 :r4 r4 | |
:vs 5})) | |
resistor-permutations))) | |
(def max-vin 2.07) | |
(def min-vin 1.18) | |
(def ^:dynamic *min-ofs* 0.2) | |
(def ^:dynamic *max-ofs* 0.24) | |
(def ^:dynamic *v0-tol* 0.18) | |
;;;;; | |
(defn a$ [{:keys [r1 r2]}] | |
(float (/ r2 (+ r1 r2)))) | |
(defn b$ [{:keys [r3 r4]}] | |
(float (/ r4 (+ r3 r4)))) | |
(defn g$ [circuit] | |
(float (/ (a$ circuit) (- 1 (b$ circuit))))) | |
(defn ofs$ [circuit] | |
(float (/ (b$ circuit) (a$ circuit)))) | |
(defn vo$ [{:keys [vs] :as circuit}] | |
(* vs (ofs$ circuit))) | |
(defn vout$ [vin {:keys[r1 r2 r3 r3] :as circuit}] | |
(* (g$ circuit) (- vin (vo$ circuit)))) | |
(defn vmax$ [circuit] | |
(vout$ max-vin circuit)) | |
(defn vrange$ [circuit] | |
(- (vmax$ circuit) (vout$ min-vin circuit))) | |
(defn select-good-gain [circuits] | |
(eduction (filter (fn [c] | |
(< 1 (vmax$ c) 2))) | |
circuits)) | |
(defn select-best-gain [circuits] | |
(reduce (fn [c c2] | |
(if (< (vmax$ c) (vmax$ c2)) | |
c2 c)) | |
(first circuits) | |
(next circuits))) | |
(defn select-good-offset [circuits] | |
(let [mno *min-ofs* | |
mxo *max-ofs* | |
v0-tol *v0-tol*] | |
(eduction (filter (fn [c] | |
#_(< mno (ofs$ c) mxo) | |
(< (- v0-tol) (vout$ min-vin c) v0-tol))) | |
circuits))) | |
(defn add-details [c] | |
(assoc c | |
:vrange (vrange$ c) | |
:vmax (vmax$ c) | |
:v0 (vout$ min-vin c))) | |
(comment | |
(def data | |
(binding [*v0-tol* 0.4] | |
(vec(eduction | |
(filter #(< 0.04 (vout$ min-vin %) 0.08)) | |
(map add-details) | |
(sort-by vmax$ > | |
(-> (select-good-offset all-circuits) | |
select-good-gain)))))) | |
#_(pp/pprint | |
(take 5 (filter #(< -0.08 (vout$ min-vin %) -0.04)data))) | |
(pp/pprint (first data)) | |
(oz/start-server!) | |
(oz/view! | |
[:div {:style {:display :flex}} | |
[:vega {:data {:values data} | |
:width 700 | |
:height 800 | |
:encoding {:x {:field :v0 :type "quantitative"} | |
:y {:field :vrange :type "quantitative"} | |
:color {:field "item" :type "nominal"}} | |
:mark :point}] | |
[:vega {:data {:values data} | |
:width 700 | |
:height 800 | |
:encoding {:x {:field :v0 :type "quantitative"} | |
:y {:field :vmax :type "quantitative"} | |
:color {:field "item" :type "nominal"}} | |
:mark :point}]]) | |
#! | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment