Issue #26: Add reverse lookup function to temp converter that converts a temperature to a PT1000 resistance value
This commit is contained in:
		@@ -29,4 +29,13 @@
 | 
			
		||||
 */
 | 
			
		||||
int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Convert temperature to PT1000 resistance
 | 
			
		||||
 * @param temp Temperature in degrees celsius
 | 
			
		||||
 * @param[out] resistance_out Resistance value
 | 
			
		||||
 * @return 0 if ok, -1 if tmeperature is below the lookup table, 1 if value is above the lookup table, -1000 in case of a pointer error,
 | 
			
		||||
 *	   -100 if an internal error occured. This should never happen, if the lookup table is correct
 | 
			
		||||
 */
 | 
			
		||||
int temp_convertet_convert_temp_to_resistance(float temp, float *resistance_out);
 | 
			
		||||
 | 
			
		||||
#endif /* __TEMP_CONVERTER_H__ */
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,7 @@
 | 
			
		||||
#include <reflow-controller/temp-converter.h>
 | 
			
		||||
#include <reflow-controller/temp-converter-data.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
static const float temp_lookup[(TEMP_CONVERSION_MAX_RES-TEMP_CONVERSION_MIN_RES) / TEMP_CONVERSION_RES_STEP+1] = {TEMP_CONVERSION_ARRAY_DATA};
 | 
			
		||||
 | 
			
		||||
@@ -73,3 +74,49 @@ int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out)
 | 
			
		||||
return_ret_val:
 | 
			
		||||
	return ret_val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int temp_convertet_convert_temp_to_resistance(float temp, float *resistance_out)
 | 
			
		||||
{
 | 
			
		||||
	int retcode = 0;
 | 
			
		||||
	unsigned int i;
 | 
			
		||||
	float lower_temp;
 | 
			
		||||
	float upper_temp;
 | 
			
		||||
	float lower_resistance;
 | 
			
		||||
	float upper_resistance;
 | 
			
		||||
	float temp_ratio;
 | 
			
		||||
	bool found = false;
 | 
			
		||||
 | 
			
		||||
	if (!resistance_out)
 | 
			
		||||
		return -1000;
 | 
			
		||||
 | 
			
		||||
	if (temp < temp_lookup[0]) {
 | 
			
		||||
		/* Requested temperature is smaller than minimum value in lookup table */
 | 
			
		||||
		*resistance_out = temp_lookup[0];
 | 
			
		||||
		retcode = -1;
 | 
			
		||||
		goto exit;
 | 
			
		||||
	} else if (temp > temp_lookup[COUNT_OF(temp_lookup) - 1]) {
 | 
			
		||||
		/* Requested temperature is higher than maximum value in lookup table */
 | 
			
		||||
		*resistance_out = temp_lookup[COUNT_OF(temp_lookup) -1];
 | 
			
		||||
		retcode = 1;
 | 
			
		||||
		goto exit;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for (i = 0U; i < (COUNT_OF(temp_lookup) - 1); i++) {
 | 
			
		||||
		if (temp >= temp_lookup[i] && temp <= temp_lookup[i+1]) {
 | 
			
		||||
			upper_temp = temp_lookup[i+1];
 | 
			
		||||
			lower_temp = temp_lookup[i];
 | 
			
		||||
			lower_resistance = TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * (i);
 | 
			
		||||
			upper_resistance = lower_resistance + TEMP_CONVERSION_RES_STEP;
 | 
			
		||||
			found = true;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!found)
 | 
			
		||||
		return -100;
 | 
			
		||||
 | 
			
		||||
	temp_ratio = (temp - lower_temp) / (upper_temp - lower_temp);
 | 
			
		||||
	*resistance_out = lower_resistance + (upper_resistance - lower_resistance) * temp_ratio;
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
	return retcode;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user