spi_temperature: Simplify and comment MAX31865 temperature calculations

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-09-02 09:37:13 -04:00
parent 37d7742468
commit c69527e2b0
2 changed files with 22 additions and 19 deletions

View File

@ -262,7 +262,7 @@ AD8497 = [
] ]
def calc_pt100(base=100.): def calc_pt100(base=100.):
# Calc PT100/PT1000 temperature/resistance pairs using formula # Calc PT100/PT1000 resistances using Callendar-Van Dusen formula
A, B = (3.9083e-3, -5.775e-7) A, B = (3.9083e-3, -5.775e-7)
return [(float(t), base * (1. + A*t + B*t*t)) for t in range(0, 500, 10)] return [(float(t), base * (1. + A*t + B*t*t)) for t in range(0, 500, 10)]

View File

@ -267,15 +267,18 @@ MAX31865_FAULT_REFINHIGH = 0x10
MAX31865_FAULT_RTDINLOW = 0x08 MAX31865_FAULT_RTDINLOW = 0x08
MAX31865_FAULT_OVUV = 0x04 MAX31865_FAULT_OVUV = 0x04
VAL_A = 0.00390830 MAX31865_ADC_MAX = 1<<15
VAL_B = -0.0000005775
VAL_C = -0.00000000000418301 # Callendar-Van Dusen constants for platinum resistance thermometers (RTD)
VAL_ADC_MAX = 32768.0 # 2^15 CVD_A = 3.9083e-3
CVD_B = -5.775e-7
class MAX31865(SensorBase): class MAX31865(SensorBase):
def __init__(self, config): def __init__(self, config):
self.rtd_nominal_r = config.getint('rtd_nominal_r', 100) rtd_nominal_r = config.getfloat('rtd_nominal_r', 100., above=0.)
self.reference_r = config.getfloat('rtd_reference_r', 430., above=0.) rtd_reference_r = config.getfloat('rtd_reference_r', 430., above=0.)
adc_to_resist = rtd_reference_r / float(MAX31865_ADC_MAX)
self.adc_to_resist_div_nominal = adc_to_resist / rtd_nominal_r
SensorBase.__init__(self, config, "MAX31865", SensorBase.__init__(self, config, "MAX31865",
self.build_spi_init(config)) self.build_spi_init(config))
def calc_temp(self, adc, fault): def calc_temp(self, adc, fault):
@ -295,20 +298,20 @@ class MAX31865(SensorBase):
if fault & 0x03: if fault & 0x03:
self.fault("Max31865 Unspecified error") self.fault("Max31865 Unspecified error")
adc = adc >> 1 # remove fault bit adc = adc >> 1 # remove fault bit
R_rtd = (self.reference_r * adc) / VAL_ADC_MAX R_div_nominal = adc * self.adc_to_resist_div_nominal
temp = ((( ( -1 * self.rtd_nominal_r ) * VAL_A ) # Resistance (relative to rtd_nominal_r) is calculated using:
+ math.sqrt( ( self.rtd_nominal_r**2 * VAL_A * VAL_A ) # R_div_nominal = 1. + CVD_A * temp + CVD_B * temp**2
- ( 4 * self.rtd_nominal_r * VAL_B # Solve for temp using quadratic equation:
* ( self.rtd_nominal_r - R_rtd ) ))) # temp = (-b +- sqrt(b**2 - 4ac)) / 2a
/ (2 * self.rtd_nominal_r * VAL_B)) discriminant = math.sqrt(CVD_A**2 - 4. * CVD_B * (1. - R_div_nominal))
temp = (-CVD_A + discriminant) / (2. * CVD_B)
return temp return temp
def calc_adc(self, temp): def calc_adc(self, temp):
R_rtd = temp * ( 2 * self.rtd_nominal_r * VAL_B ) # Calculate relative resistance via Callendar-Van Dusen formula:
R_rtd = math.pow( ( R_rtd + ( self.rtd_nominal_r * VAL_A ) ), 2) # resistance = rtd_nominal_r * (1 + CVD_A * temp + CVD_B * temp**2)
R_rtd = -1 * ( R_rtd - (self.rtd_nominal_r**2 * VAL_A * VAL_A ) ) R_div_nominal = 1. + CVD_A * temp + CVD_B * temp * temp
R_rtd = R_rtd / ( 4 * self.rtd_nominal_r * VAL_B ) adc = int(R_div_nominal / self.adc_to_resist_div_nominal + 0.5)
R_rtd = ( -1 * R_rtd ) + self.rtd_nominal_r adc = max(0, min(MAX31865_ADC_MAX, adc))
adc = int( ( ( R_rtd * VAL_ADC_MAX ) / self.reference_r) + 0.5 )
adc = adc << 1 # Add fault bit adc = adc << 1 # Add fault bit
return adc return adc
def build_spi_init(self, config): def build_spi_init(self, config):