- added max31865
This commit is contained in:
Executable
+122
@@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
import time
|
||||||
|
import spidev
|
||||||
|
from atemperatureSensor import ATemperatureSensor
|
||||||
|
|
||||||
|
|
||||||
|
class TempSensor_max31865(ATemperatureSensor):
|
||||||
|
def name(self):
|
||||||
|
return "Max31865"
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Open SPI bus
|
||||||
|
self.spi = spidev.SpiDev()
|
||||||
|
self.spi.open(0, 0)
|
||||||
|
self.spi.max_speed_hz = 50000
|
||||||
|
self.spi.mode = 0b01
|
||||||
|
self.temp_correction = -0.2
|
||||||
|
self.write_reg(0x00, 0xA3)
|
||||||
|
time.sleep(0.100)
|
||||||
|
self.digits = 0
|
||||||
|
self.log("Created")
|
||||||
|
self.temp = self.temperature()
|
||||||
|
|
||||||
|
def read_reg(self, addr):
|
||||||
|
reg = self.spi.xfer([addr, 0xFF])
|
||||||
|
|
||||||
|
return reg[1]
|
||||||
|
|
||||||
|
def write_reg(self, addr, data):
|
||||||
|
self.spi.xfer([addr+0x80, data])
|
||||||
|
|
||||||
|
def read_digits(self):
|
||||||
|
msb = self.read_reg(0x01)
|
||||||
|
lsb = self.read_reg(0x02)
|
||||||
|
if lsb & 0x01 == 0x00:
|
||||||
|
self.digits = float(256*msb + lsb)/2
|
||||||
|
|
||||||
|
self.write_reg(0x00, 0xA3)
|
||||||
|
return self.digits
|
||||||
|
|
||||||
|
def temperature(self):
|
||||||
|
return self.__tempRaw()
|
||||||
|
|
||||||
|
def __tempRaw(self):
|
||||||
|
try:
|
||||||
|
digits = self.read_digits()
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
R = TempSensor_max31865.calc_R(430, digits)
|
||||||
|
T = TempSensor_max31865.vanDusen_temp(R) + self.temp_correction
|
||||||
|
# print("R={:.3f} Ohm, T={:.2f} degC".format(R, T))
|
||||||
|
return T
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def calc_R(Rref, digits):
|
||||||
|
k = digits / 8192 / 4
|
||||||
|
R = k*Rref
|
||||||
|
|
||||||
|
return R
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def vanDusenLut (R0, Tmin, Tmax, dT):
|
||||||
|
a = +3.90830e-03
|
||||||
|
b = -5.77500e-07
|
||||||
|
c = -4.18301e-12
|
||||||
|
|
||||||
|
Rv = []
|
||||||
|
Tv = []
|
||||||
|
T = Tmin
|
||||||
|
while T <= Tmax:
|
||||||
|
R = R0*(1 + a*T + b*T**2)
|
||||||
|
|
||||||
|
if T < 0.0:
|
||||||
|
R += R0*c*(T - 100)*T**3
|
||||||
|
|
||||||
|
Rv.append(R)
|
||||||
|
Tv.append(T)
|
||||||
|
T += dT
|
||||||
|
|
||||||
|
return Rv, Tv
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def vanDusen_temp(Rmeas):
|
||||||
|
RLut, TLut = TempSensor_max31865.vanDusenLut(100, -150, +500, 1.0)
|
||||||
|
|
||||||
|
# Find candidate
|
||||||
|
i = 0
|
||||||
|
for R in RLut:
|
||||||
|
if R > Rmeas:
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Linear interpolation
|
||||||
|
R0 = RLut[i-1]
|
||||||
|
R1 = RLut[i]
|
||||||
|
T0 = TLut[i-1]
|
||||||
|
T1 = TLut[i]
|
||||||
|
|
||||||
|
dR = R1 - R0
|
||||||
|
dT = T1 - T0
|
||||||
|
|
||||||
|
k = (Rmeas-R0)/dR
|
||||||
|
T = T0 + dT*k
|
||||||
|
|
||||||
|
return T
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
temp = self.temperature()
|
||||||
|
self.temp = temp
|
||||||
|
|
||||||
|
# Main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
||||||
|
TempSensor_max31865 = TempSensor_max31865()
|
||||||
|
for i in range (0, 8):
|
||||||
|
print("Reg[0x{}]: 0x{:02X}".format(i, TempSensor_max31865.read_reg(i)))
|
||||||
|
|
||||||
|
print(TempSensor_max31865.temperature())
|
||||||
|
|
||||||
|
print("End of program")
|
||||||
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from components.sensor.tempSensorSim import TempSensorSim
|
from components.sensor.tempSensorSim import TempSensorSim
|
||||||
|
from components.sensor.tempSensor_max31865 import TempSensor_max31865
|
||||||
|
|
||||||
|
|
||||||
class TempSensorFactory:
|
class TempSensorFactory:
|
||||||
@@ -6,3 +7,5 @@ class TempSensorFactory:
|
|||||||
def create(name):
|
def create(name):
|
||||||
if "sim" in name:
|
if "sim" in name:
|
||||||
return TempSensorSim()
|
return TempSensorSim()
|
||||||
|
elif "31865" in name:
|
||||||
|
return TempSensor_max31865()
|
||||||
|
|||||||
Reference in New Issue
Block a user