move thermistor calculation to watch utilities

This commit is contained in:
Joey Castillo 2021-11-22 17:31:51 -05:00
parent fb8f4584a5
commit 0ca729eaba
4 changed files with 66 additions and 65 deletions

View file

@ -1,76 +1,34 @@
#include "thermistor_driver.h"
#include "watch.h"
#define THERMISTOR_B_COEFFICIENT (3950.0)
#define THERMISTOR_NOMINAL_TEMPERATURE (25.0)
#define THERMISTOR_NOMINAL_RESISTANCE (10000.0)
#define THERMISTOR_SERIES_RESISTANCE (10000.0)
// TODO: we really need a math library.
uint32_t msb(uint32_t v);
double ln(double y);
#include "watch_utility.h"
void thermistor_driver_enable() {
// Enable the ADC peripheral, which we'll use to read the thermistor value.
watch_enable_adc();
// Enable analog circuitry on pin A1, which is tied to the thermistor resistor divider.
watch_enable_analog_input(A1);
// Enable digital output on A0, which is the power to the thermistor circuit.
watch_enable_digital_output(A0);
// Enable analog circuitry on the sense pin, which is tied to the thermistor resistor divider.
watch_enable_analog_input(THERMISTOR_SENSE_PIN);
// Enable digital output on the enable pin, which is the power to the thermistor circuit.
watch_enable_digital_output(THERMISTOR_ENABLE_PIN);
// and make sure it's off.
watch_set_pin_level(THERMISTOR_ENABLE_PIN, !THERMISTOR_ENABLE_VALUE);
}
void thermistor_driver_disable() {
// Enable the ADC peripheral, which we'll use to read the thermistor value.
// Disable the ADC peripheral.
watch_disable_adc();
// Disable analog circuitry on pin A1 to save power.
watch_disable_analog_input(A1);
// Disable A0's output circuitry.
watch_disable_digital_output(A0);
// Disable analog circuitry on the sense pin to save power.
watch_disable_analog_input(THERMISTOR_SENSE_PIN);
// Disable the enable pin's output circuitry.
watch_disable_digital_output(THERMISTOR_ENABLE_PIN);
}
float thermistor_driver_get_temperature() {
// set A0 high to power the thermistor circuit.
watch_set_pin_level(A0, true);
// get the pin level
uint16_t val = watch_get_analog_pin_level(A1);
// and then set A0 low to power down the thermistor circuit.
watch_set_pin_level(A0, false);
// set the enable pin to the level that powers the thermistor circuit.
watch_set_pin_level(THERMISTOR_ENABLE_PIN, THERMISTOR_ENABLE_VALUE);
// get the sense pin level
uint16_t value = watch_get_analog_pin_level(THERMISTOR_SENSE_PIN);
// and then set the enable pin to the opposite value to power down the thermistor circuit.
watch_set_pin_level(THERMISTOR_ENABLE_PIN, !THERMISTOR_ENABLE_VALUE);
double reading = (double)val;
reading = (1023.0 * THERMISTOR_SERIES_RESISTANCE) / (reading / 64.0);
reading -= THERMISTOR_SERIES_RESISTANCE;
reading = reading / THERMISTOR_NOMINAL_RESISTANCE;
reading = ln(reading);
reading /= THERMISTOR_B_COEFFICIENT;
reading += 1.0 / (THERMISTOR_NOMINAL_TEMPERATURE + 273.15);
reading = 1.0 / reading;
reading -= 273.15;
return reading;
return watch_utility_thermistor_temperature(value, THERMISTOR_HIGH_SIDE, THERMISTOR_B_COEFFICIENT, THERMISTOR_NOMINAL_TEMPERATURE, THERMISTOR_NOMINAL_RESISTANCE, THERMISTOR_SERIES_RESISTANCE);
}
uint32_t msb(uint32_t v) {
static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v = (v >> 1) + 1;
return pos[(v * 0x077CB531UL) >> 27];
}
double ln(double y) {
int log2;
double divisor, x, result;
log2 = msb((int)y); // See: https://stackoverflow.com/a/4970859/6630230
divisor = (double)(1 << log2);
x = y / divisor; // normalized value between [1.0, 2.0]
result = -1.7417939 + (2.8212026 + (-1.4699568 + (0.44717955 - 0.056570851 * x) * x) * x) * x;
result += ((double)log2) * 0.69314718; // ln(2) = 0.69314718
return result;
}

View file

@ -1,10 +1,16 @@
#ifndef THERMISTOR_DRIVER_H_
#define THERMISTOR_DRIVER_H_
// NOTE: This implementation is specific to one prototype sensor board, OSO-MISC-21-009, but both
// the sensor board design and this implementation are likely to change. Thermistor functionality
// may even end up being baked into the Sensor Watch library. This is all by way of saying this
// code is very temporary and the thermistor screens will likely get a rewrite in the future.
// TODO: Do these belong in movement_config.h? In settings we can set on the watch? In an EEPROM configuration area?
// Think on this. [joey 11/22]
#define THERMISTOR_SENSE_PIN (A2)
#define THERMISTOR_ENABLE_PIN (A0)
#define THERMISTOR_ENABLE_VALUE (false)
#define THERMISTOR_HIGH_SIDE (true)
#define THERMISTOR_B_COEFFICIENT (3380.0)
#define THERMISTOR_NOMINAL_TEMPERATURE (25.0)
#define THERMISTOR_NOMINAL_RESISTANCE (10000.0)
#define THERMISTOR_SERIES_RESISTANCE (10000.0)
void thermistor_driver_enable();
void thermistor_driver_disable();

View file

@ -22,6 +22,7 @@
* SOFTWARE.
*/
#include <math.h>
#include "watch_utility.h"
const char * watch_utility_get_weekday(watch_date_time date_time) {
@ -33,3 +34,23 @@ const char * watch_utility_get_weekday(watch_date_time date_time) {
}
return weekdays[(date_time.unit.day + 13 * (date_time.unit.month + 1) / 5 + date_time.unit.year + date_time.unit.year / 4 + 525) % 7];
}
float watch_utility_thermistor_temperature(uint16_t value, bool highside, float b_coefficient, float nominal_temperature, float nominal_resistance, float series_resistance) {
float reading = (float)value;
if (highside) {
reading = (1023.0 * series_resistance) / (reading / 64.0);
reading -= series_resistance;
} else {
reading = series_resistance / (65535.0 / value - 1.0);
}
reading = reading / nominal_resistance;
reading = log(reading);
reading /= b_coefficient;
reading += 1.0 / (nominal_temperature + 273.15);
reading = 1.0 / reading;
reading -= 273.15;
return reading;
}

View file

@ -38,4 +38,20 @@
*/
const char * watch_utility_get_weekday(watch_date_time date_time);
/** @brief Returns a temperature in degrees Celsius for a given thermistor voltage divider circuit.
* @param value The raw analog reading from the thermistor pin (0-65535)
* @param highside True if the thermistor is connected to VCC and the series resistor is connected
* to GND; false if the thermistor is connected to GND and the series resistor is
* connected to VCC.
* @param b_coefficient From your thermistor's data sheet, the B25/85 coefficient. A typical value
* will be between 2000 and 5000.
* @param nominal_temperature From your thermistor's data sheet, the temperature (in Celsius) at
* which the thermistor's resistance is at its nominal value.
* @param nominal_resistance The thermistor's resistance at the nominal temperature.
* @param series_resistance The value of the other resistor in the voltage divider.
* @note Ported from Adafruit's MIT-licensed CircuitPython thermistor code, (c) 2017 Scott Shawcroft:
* https://github.com/adafruit/Adafruit_CircuitPython_Thermistor/blob/main/adafruit_thermistor.py
*/
float watch_utility_thermistor_temperature(uint16_t value, bool highside, float b_coefficient, float nominal_temperature, float nominal_resistance, float series_resistance);
#endif