Initial proof of concept

This commit is contained in:
Zachary Levy
2025-03-09 12:13:14 -07:00
commit e06e76e46b
55 changed files with 4508 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
#[cfg(feature = "resistive-divider")]
pub mod resistive_divider;
+43
View File
@@ -0,0 +1,43 @@
use crate::quantity::{Ohms, Volts};
/// Given the resistance of the second resistor in a resistive voltage divider, calculate the resistance of the first resistor.
pub fn solve_r1(
voltage_src: Volts<f32>,
voltage_read: Volts<f32>,
r2: Ohms<f32>,
) -> Ohms<f32> {
Ohms(r2.0 * (voltage_src.0 / voltage_read.0 - 1.0))
}
/// Given the resistance of the first resistor in a resistive voltage divider, calculate the resistance of the second resistor.
pub fn solve_r2(
voltage_src: Volts<f32>,
voltage_read: Volts<f32>,
r1: Ohms<f32>,
) -> Ohms<f32> {
Ohms((r1.0 * voltage_read.0) / (voltage_src.0 - voltage_read.0))
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- Tests ------------------------
// ---------------------------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use crate::quantity::{OhmsVal, VoltsVal};
use super::*;
use float_cmp::assert_approx_eq;
#[test]
fn solve_r1_test() {
let resistance = solve_r1(3.3.volts(), 2.0.volts(), 1_000.0.ohms());
assert_approx_eq!(f32, 650.0, resistance.0);
}
#[test]
fn solve_r2_test() {
let resistance = solve_r2(3.3.volts(), 2.0.volts(), 1_000.0.ohms());
assert_approx_eq!(f32, 1538.462, resistance.0);
}
}