From 7df97831be6d36b6c5589022f6b29c410d3482b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 15 Feb 2020 20:31:38 +0100 Subject: [PATCH] Add conversion function for resistance to temperature conversion and add it to thept command of the shell --- stm-firmware/Makefile | 2 + .../reflow-controller/temp-converter.h | 31 ++++++++ stm-firmware/shell.c | 21 +++++- stm-firmware/temp-converter.c | 74 +++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 stm-firmware/include/reflow-controller/temp-converter.h create mode 100644 stm-firmware/temp-converter.c diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index 217687d..909192a 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -43,6 +43,8 @@ CFILES += stm-periph/unique-id.c CFILES += calibration.c +CFILES += temp-converter.c + DEFINES += -DDEBUGBUILD #TODO diff --git a/stm-firmware/include/reflow-controller/temp-converter.h b/stm-firmware/include/reflow-controller/temp-converter.h new file mode 100644 index 0000000..a6a27e2 --- /dev/null +++ b/stm-firmware/include/reflow-controller/temp-converter.h @@ -0,0 +1,31 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2020 Mario Hüttel + * + * This file is part of the reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * GDSII-Converter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GDSII-Converter. If not, see . + */ + +#ifndef __TEMP_CONVERTER_H__ +#define __TEMP_CONVERTER_H__ + +/** + * @brief Convert PT1000 resistance to temperature in degrees celsius + * @param resistance PT1000 resistance value + * @param[out] temp_out Temperature output + * @return 0 if ok, -1 if value is below conversion range, 1 if value is above conversion range,-1000 in case of pointer error + */ +int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out); + +#endif /* __TEMP_CONVERTER_H__ */ diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index ca8e00e..97d14a0 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -8,6 +8,7 @@ #include #include #include +#include #ifndef GIT_VER #define GIT_VER "VERSION NOT SET" @@ -106,6 +107,9 @@ static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t han int pt1000_status; enum adc_pt1000_error pt1000_flags; char display_status[100]; + float temp; + int temp_conv_status; + const char *temp_prefix; display_status[0] = 0; @@ -125,7 +129,22 @@ static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t han strcpy(display_status, "VALID"); } - shellmatta_printf(handle, "PT1000 resistance: %.2f Ohm [%s]\r\n", resistance, display_status); + temp_conv_status = temp_converter_convert_resistance_to_temp(resistance, &temp); + switch (temp_conv_status) { + case 1: + temp_prefix = ">"; + break; + case -1: + temp_prefix = "<"; + break; + case 0: + /* Expected fallthrough */ + default: + temp_prefix = ""; + break; + } + + shellmatta_printf(handle, "PT1000 resistance: %.2f Ohm (%s%.1f °C) [%s]\r\n", resistance, temp_prefix,temp, display_status); return SHELLMATTA_OK; } diff --git a/stm-firmware/temp-converter.c b/stm-firmware/temp-converter.c new file mode 100644 index 0000000..b930cd4 --- /dev/null +++ b/stm-firmware/temp-converter.c @@ -0,0 +1,74 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2020 Mario Hüttel + * + * This file is part of the reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * GDSII-Converter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GDSII-Converter. If not, see . + */ + +#include +#include +#include + +static const float temp_lookup[(TEMP_CONVERSION_MAX_RES-TEMP_CONVERSION_MIN_RES) / TEMP_CONVERSION_RES_STEP+1] = {TEMP_CONVERSION_ARRAY_DATA}; + +int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out) +{ + int ret_val; + unsigned int i; + unsigned int lower_idx = COUNT_OF(temp_lookup) - 1; + float diff_to_low_resistance; + float diff_high_low; + float low; + + /* Check pointer */ + if (!temp_out) + return -1000; + + if (resistance < TEMP_CONVERSION_MIN_RES) { + *temp_out = temp_lookup[0]; + ret_val = -1; + goto return_ret_val; + } else if (resistance > TEMP_CONVERSION_MAX_RES) { + *temp_out = temp_lookup[COUNT_OF(temp_lookup)-1]; + ret_val = 1; + goto return_ret_val; + } + + /* Resistance is in range */ + ret_val = 0; + + /* Calculate lower index for given reistance */ + for (i = 1; i < COUNT_OF(temp_lookup); i++) { + if (resistance <= TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * i) { + lower_idx = i - 1; + break; + } + } + + /* Get the lower temperature limit of the current range */ + low = temp_lookup[lower_idx]; + + /* Difference to higher limit */ + diff_high_low = temp_lookup[lower_idx+1] - low; + + /* Resistance difference to lower limit's resistance value */ + diff_to_low_resistance = resistance - (float)(TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * lower_idx); + + /* Calculate output temperature */ + *temp_out = (diff_to_low_resistance / TEMP_CONVERSION_RES_STEP) * diff_high_low + low; + +return_ret_val: + return ret_val; +}