72 lines
2.0 KiB
Python
Executable File
72 lines
2.0 KiB
Python
Executable File
#!/opt/srasenv/bin/python3
|
|
"""
|
|
Test temperature scaling calculations.
|
|
"""
|
|
|
|
import math
|
|
|
|
# Constants
|
|
ADC_TO_VOLTS = 0.000244140625
|
|
STEINHART_A = 0.0011279
|
|
STEINHART_B = 0.00023429
|
|
STEINHART_C = 8.7298e-8
|
|
|
|
def test_main_temp_calculation(raw_adc):
|
|
"""Test main temperature calculation with different circuit assumptions."""
|
|
|
|
print(f"\n{'='*60}")
|
|
print(f"Testing Main Temperature with RAW ADC = {raw_adc}")
|
|
print(f"{'='*60}")
|
|
|
|
v_thermistor = raw_adc * ADC_TO_VOLTS
|
|
print(f"V_thermistor = {v_thermistor:.6f} V")
|
|
|
|
# Try different circuit configurations
|
|
configs = [
|
|
(10000, 5.0, "10kΩ series, 5V ref"),
|
|
(10000, 3.3, "10kΩ series, 3.3V ref"),
|
|
(100000, 5.0, "100kΩ series, 5V ref"),
|
|
(10000, 2.5, "10kΩ series, 2.5V ref"),
|
|
]
|
|
|
|
for r_series, vref, desc in configs:
|
|
print(f"\n{desc}:")
|
|
print(f" R_series = {r_series} Ω, Vref = {vref} V")
|
|
|
|
if v_thermistor >= vref:
|
|
print(f" ERROR: V_thermistor >= Vref")
|
|
continue
|
|
|
|
r_thermistor = r_series * v_thermistor / (vref - v_thermistor)
|
|
print(f" R_thermistor = {r_thermistor:.2f} Ω")
|
|
|
|
if r_thermistor <= 0:
|
|
print(f" ERROR: Invalid resistance")
|
|
continue
|
|
|
|
# Steinhart-Hart
|
|
ln_r = math.log(r_thermistor)
|
|
inv_t = STEINHART_A + STEINHART_B * ln_r + STEINHART_C * (ln_r ** 3)
|
|
temp_k = 1.0 / inv_t
|
|
temp_c = temp_k - 273.15
|
|
|
|
print(f" ln(R) = {ln_r:.6f}")
|
|
print(f" Temperature = {temp_c:.2f} °C")
|
|
|
|
def test_current_scaling(raw_adc):
|
|
"""Test current scaling."""
|
|
print(f"\n{'='*60}")
|
|
print(f"Testing Current Scaling with RAW ADC = {raw_adc}")
|
|
print(f"{'='*60}")
|
|
|
|
current = raw_adc * 12.0 * ADC_TO_VOLTS
|
|
print(f"Current = {current:.6f} A")
|
|
|
|
if __name__ == '__main__':
|
|
# Test with the reported raw values
|
|
test_main_temp_calculation(2)
|
|
test_main_temp_calculation(3)
|
|
test_current_scaling(0)
|
|
test_current_scaling(100)
|
|
test_current_scaling(4095)
|