From fa263595e3ff71eb656c9c3f1d6a8a4effc46ada Mon Sep 17 00:00:00 2001 From: AlexWells Date: Tue, 19 Mar 2024 10:54:46 +0000 Subject: [PATCH] Use AD8494 Thermocouple Amplifier for temperature We are no longer using a Type K thermocouple. --- src/psc_datalogger/connection.py | 2 +- .../{thermocouple => temperature}/__init__.py | 0 src/psc_datalogger/temperature/converter.py | 25 +++++++++ .../thermocouple/thermocouple.py | 54 ------------------- tests/test_connection.py | 2 +- ...st_thermocouple.py => test_temperature.py} | 18 +++---- 6 files changed, 36 insertions(+), 65 deletions(-) rename src/psc_datalogger/{thermocouple => temperature}/__init__.py (100%) create mode 100644 src/psc_datalogger/temperature/converter.py delete mode 100644 src/psc_datalogger/thermocouple/thermocouple.py rename tests/{test_thermocouple.py => test_temperature.py} (62%) diff --git a/src/psc_datalogger/connection.py b/src/psc_datalogger/connection.py index a518d5f..d94a0e1 100644 --- a/src/psc_datalogger/connection.py +++ b/src/psc_datalogger/connection.py @@ -12,7 +12,7 @@ from pyvisa.resources import Resource, SerialInstrument from .statusbar import StatusBar -from .thermocouple.thermocouple import volts_to_celcius +from .temperature.converter import volts_to_celcius class ConnectionManager: diff --git a/src/psc_datalogger/thermocouple/__init__.py b/src/psc_datalogger/temperature/__init__.py similarity index 100% rename from src/psc_datalogger/thermocouple/__init__.py rename to src/psc_datalogger/temperature/__init__.py diff --git a/src/psc_datalogger/temperature/converter.py b/src/psc_datalogger/temperature/converter.py new file mode 100644 index 0000000..e3e9972 --- /dev/null +++ b/src/psc_datalogger/temperature/converter.py @@ -0,0 +1,25 @@ +# Handles converting millivolt readings into degrees Celcius for an Analog Devices +# AD8494 Thermocouple Amplifier +""" +The equation governing this is specified as: +V_OUT = (T_MJ * 5 mV/°C) + V_REF +where +- T_MJ is the thermocouple measurement junction temperature. +- V_OUT is the measured output voltage +- V_REF is the reference voltage, which has been configured to 0 on the hardware. +Substituting V_REF=0 and rearranging this equation gives: + +T_MJ = V_OUT / 5 mV + +i.e. every 5 mV gives 1 degree celcius increase + +""" + + +def volts_to_celcius(volts: str) -> float: + # Convert to millivolts + mv = float(volts) * 1000 + temp = mv / 5.0 + # Check the result is at least vaguely sensible + assert -10.0 <= temp <= 60.0 + return temp diff --git a/src/psc_datalogger/thermocouple/thermocouple.py b/src/psc_datalogger/thermocouple/thermocouple.py deleted file mode 100644 index 1c994c0..0000000 --- a/src/psc_datalogger/thermocouple/thermocouple.py +++ /dev/null @@ -1,54 +0,0 @@ -from collections import namedtuple - -# Handles converting millivolt readings into degrees Celcius for a Type K Thermocouple -# Valid for -10 -> 59 degrees Celcius. - -""" -The conversion is done by using the calculated coefficients of a cubic equation that -models the data found here: -https://es.omega.com/temperature/pdf/Type_K_Thermocouple_Reference_Table.pdf -(Relevant range copied into data.csv ) - -To recreate the polynomial follow these steps: -- Format the data.csv file: - - Remove comments - - Delete first column (the degrees celcius) - - Delete the penultimate column (These values are repeated as the first element - of the next row) - - Delete the final column (another degrees celcius reading) - - Save as "formatted_data.txt" -- Read it into matlab (and transpose): - - v = load("formatted_data.txt")'; -- Create time series: - - t = (-10:59)'; -- Set long format: - - format long -- Fit the polynomial: - - f3 = polyfit(v, t, 3) - -The final line should print out the polynomials: -f3 = - 0.039232630522331 -0.391455002477827 25.346167279763222 -0.000725393446104 -""" - -# The coefficients of the equation y = a + bx + cx^2 + dx^3 -Coeffs = namedtuple("Coeffs", ["a", "b", "c", "d"]) -coefficients = Coeffs( - -0.000725393446104, - 25.346167279763222, - -0.391455002477827, - 0.039232630522331, -) - - -def volts_to_celcius(volts: str) -> float: - # Convert to millivolts - mv = float(volts) * 1000 - assert -0.392 <= mv <= 2.395, f"millivolt reading {mv} outside of modelled range" - - return ( - coefficients.a - + coefficients.b * mv - + coefficients.c * mv**2 - + coefficients.d * mv**3 - ) diff --git a/tests/test_connection.py b/tests/test_connection.py index c58f373..ce49d8f 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -461,7 +461,7 @@ def test_query_instruments_temperature( address = 22 worker.instrument_addresses[1] = InstrumentConfig(address, convert_to_temp=True) voltage_str = "1E-03" - temperature = "24.993219514361623" # degrees Celcius, calculated + temperature = "0.2" # degrees Celcius, calculated from voltage_str mocked_write = MagicMock() mocked_query = MagicMock(return_value=voltage_str) worker.connection = MagicMock() diff --git a/tests/test_thermocouple.py b/tests/test_temperature.py similarity index 62% rename from tests/test_thermocouple.py rename to tests/test_temperature.py index fcbabb8..7a10ae3 100644 --- a/tests/test_thermocouple.py +++ b/tests/test_temperature.py @@ -2,7 +2,7 @@ import pytest -from psc_datalogger.thermocouple.thermocouple import volts_to_celcius +from psc_datalogger.temperature.converter import volts_to_celcius # Tests for the thermocouple voltage -> degrees Celcius conversion. @@ -11,24 +11,24 @@ @pytest.mark.parametrize( - "input_millivolts, expected_temp", + "input_volts, expected_temp", [ - (-0.392 * 10**-3, -10), - (0.0 * 10**-3, 0.0), - (1.203 * 10**-3, 30), - (2.395 * 10**-3, 59), + (0.2, 40.0), + (-0.01, -2.0), + (0.0, 0.0), + (0.11, 22.0), ], ) -def test_volts_to_celcius_valid(input_millivolts, expected_temp): +def test_volts_to_celcius_valid(input_volts, expected_temp): """Test that a handful of millivolt readings produce the correct output temperature to within TOLERANCE.""" - calculated_temp = volts_to_celcius(input_millivolts) + calculated_temp = volts_to_celcius(input_volts) assert math.isclose(expected_temp, calculated_temp, abs_tol=TOLERANCE) @pytest.mark.parametrize( "invalid_value", - [-1 * 10**-3, -0.393 * 10**-3, 2.396 * 10**-3, 3 * 10**-3, 10 * 10**-3], + [-5.1 * 10**-2, 3.1 * 10**-1], ) def test_volts_to_celcius_invalid_values(invalid_value): """Test that various values are all invalid i.e. outside modelled range"""