Initial proof of concept
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
#[cfg(feature = "resistive-divider")]
|
||||
pub mod resistive_divider;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user