max31865 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6650202e6b271c41a4f1356c3df2102442c93cd8f355d44c748dc73a460310dc
4
- data.tar.gz: c23a50da188f3b31979d991f0ff7767c61a0e437c2cbf454ea80fc695b3c6b61
3
+ metadata.gz: 69cbff5d02f5701e1b98a9f7b67017facfa78b84455c5200d16ff4ebe925f6c1
4
+ data.tar.gz: 613d468a902e9b4bc369ae10bcbbec41ecc832900ec934ad2808cba8ce3ce29e
5
5
  SHA512:
6
- metadata.gz: 646d1d5d29079cbb6935f2dedae557a7d4963e7e62455a9fdd8c18818a0ae16c7f900e29e0516bdf09d929b73a6c7599201d84d4d3393b483ac0575b3950c5dd
7
- data.tar.gz: d6bc5d85c7c99e2abed7059993b78fb75abc75db7e2a171cee575ce9952a471590489c806e26771e75bbc834f4093513983624f2469d32789dc4bfe18eea031c
6
+ metadata.gz: 8c2bf110fc27a4d63634de577d12d22c41291a57695f98ba46cb75e579f5eb05ead43697ed72a7eceb91eb7c5b81b55385c6ce267fd423ccea901a64491fa2c7
7
+ data.tar.gz: 7eef1c2d368568968289955f83a6ae314f33fb1eefa7eb94568828db1cf6c1fb260883a6f18aa49ba0fd689b74bbd4bfa397a957dad3f15f00cca78863ed1b9d
data/README.md CHANGED
@@ -26,9 +26,9 @@ Or install it yourself as:
26
26
 
27
27
  m = MAX31865.new(chip = 0, type = :k, clock = 2_000_000)
28
28
 
29
- m.config # Must be run once (shutdown resets chip)
29
+ m.config # Run once (shutdown resets chip)
30
30
 
31
- m.read # [Cold Junction Temp, Thermocouple Temp] Floats in celsius
31
+ m.read # RTD Temp (Float in celsius)
32
32
 
33
33
 
34
34
  ## Raspberry v3
@@ -60,6 +60,12 @@ and contributors are expected to adhere to the [Contributor Covenant](http://con
60
60
 
61
61
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
62
 
63
- ## Code of Conduct
63
+
64
+ ## Notes
65
+
66
+ For Thermocouples, check out https://github.com/nofxx/MAX31856
67
+
68
+ Thanks to https://github.com/hackenbergstefan/MAX31865
69
+
64
70
 
65
71
  Everyone interacting in the MAX31865 project’s codebases and issue trackers is expected to follow the [code of conduct](https://github.com/nofxx/max31865/blob/master/CODE_OF_CONDUCT.md).
@@ -5,13 +5,14 @@ require 'pi_piper'
5
5
  # MAX31865
6
6
  #
7
7
  # Thanks to https://github.com/steve71/MAX31865/blob/master/max31865.py
8
+ # https://github.com/hackenbergstefan/MAX31865/blob/master/max31865.py
8
9
  class MAX31865
9
- attr_accessor :chip, :clock, :wires, :hz
10
+ attr_accessor :chip, :clock, :type, :ref, :wires, :hz
10
11
 
11
- # Read registers
12
- READ_REG = [0, 8].freeze
13
- R_REF = 430.0 # Reference Resistor
14
- R0 = 100.0 # Resistance at 0 degC for 400ohm R_Ref
12
+ # Resistance at 0 degC for 400ohm R_Ref
13
+ R0 = 100.0
14
+
15
+ # Constants for Callendar-Van-Dusen equation
15
16
  A = 0.00390830
16
17
  B = -0.000000577500
17
18
  # C = -4.18301e-12 # for -200 <= T <= 0 (degC)
@@ -29,11 +30,12 @@ class MAX31865
29
30
  0x80 => 'High threshold limit (Cable fault/open)',
30
31
  0x40 => 'Low threshold limit (Cable fault/short)',
31
32
  0x04 => 'Overvoltage or Undervoltage Error',
32
- 0x01 => 'RTD Open-Circuit Fault'
33
+ 0xff => 'Circuit Fault'
33
34
  }.freeze
34
35
 
35
- def initialize(chip = 0, clock = 2_000_000)
36
- @chip = CHIPS[chip]
36
+ def initialize(chip: 0, ref: 430.0, clock: 2_000_000)
37
+ @chip = CHIPS[chip]
38
+ @ref = ref
37
39
  @clock = clock
38
40
  end
39
41
 
@@ -80,7 +82,7 @@ class MAX31865
80
82
  # 0b11010010 or 0xD2 for continuous auto conversion
81
83
  # at 60Hz (faster conversion)
82
84
  # 0b10110010 = 0xB2
83
- def config(byte = 0xB2)
85
+ def config(byte = 0b11010010)
84
86
  spi_work do |spi|
85
87
  spi.write(0x80, byte)
86
88
  end
@@ -99,28 +101,39 @@ class MAX31865
99
101
  private
100
102
 
101
103
  #
102
- # Callendar-Van Dusen equation
103
- # Res_RTD = Res0 * (1 + a*T + b*T**2 + c*(T-100)*T**3)
104
- # Res_RTD = Res0 + a*Res0*T + b*Res0*T**2 # c = 0
105
- # (c*Res0)T**4 - (c*Res0)*100*T**3
106
- # + (b*Res0)*T**2 + (a*Res0)*T + (Res0 - Res_RTD) = 0
104
+ # Callendar-Van Dusen equations
105
+ #
106
+ # Convert 0 <= raw_temp < 2^l5 to temperature in oC using quadratic polynom:
107
+ #
108
+ # R(T)=R(0)(1+A*T+B*T^{2})
109
+ #
110
+ # => T = (-A*R(0) + sqrt(A^2 R(0)^2 - 4*B*R(0)*(R(0) - R(T)))) / (2*B*R(0))
107
111
  #
108
112
  # quadratic formula:
109
113
  # for 0 <= T <= 850 (degC)
110
114
  #
111
- def callendar_van_dusen(rtd)
112
- temp = -(A * R0) +
113
- Math.sqrt(A * A * R0 * R0 - 4 * (B * R0) * (R0 - rtd))
114
- temp /= (2 * (B * R0))
115
- # temp_line = (rtd_adc_code / 32.0) - 256.0
116
- # puts "Straight Line Approx. Temp: #{temp_line}C"
117
- # removing numpy.roots will greatly speed things up
118
- # temp_C_numpy = numpy.roots([c*R0, -c*R0*100, b*R0, a*R0, (R0 - rtd)])
119
- # temp_C_numpy = abs(temp_C_numpy[-1])
120
- # print "Solving Full Callendar-Van Dusen using numpy: %f" % temp_C_numpy
121
- # use straight line approximation if less than 0
122
- # Can also use python lib numpy to solve cubic
123
- temp < 0 ? (rtd_adc_code / 32) - 256 : temp
115
+ def quadratic(adc, rtd)
116
+ temp = -(A * R0)
117
+ temp += Math.sqrt(A * A * R0 * R0 - 4 * B * R0 * (R0 - rtd))
118
+ temp /= 2 * B * R0
119
+ # puts "Callendar-Van Dusen Temp (degC > 0): #{temp}C"
120
+ temp < 0 ? (adc / 32) - 256 : temp
121
+ end
122
+
123
+ # """
124
+ # Convert 0 <= raw_temp < 2^15 to temperature in oC using quartic polynom:
125
+ #
126
+ # R(T) = R(0)[1+A*T+B*T^{2}+(T-100)C*T^{3}]
127
+ #
128
+ # => 0 = R(0)[1 + A*T + B*T^2 - 100*C*T^3 + C*T^4] - R(T)
129
+ #
130
+ # Solution is calculated numerically.
131
+ #
132
+ def quartic(rtd)
133
+ temp = rtd * R0 / (2**15 - 1)
134
+ roots = [C * R0, -100 * R0 * C, B * R0, A * R0, R0 - temp]
135
+ .map { |n| Math.sqrt(n) }
136
+ Math.abs(roots[-1])
124
137
  end
125
138
 
126
139
  #
@@ -129,13 +142,13 @@ class MAX31865
129
142
  def read_temp(raw)
130
143
  read_fault(raw[7])
131
144
  rtd_msb, rtd_lsb = raw[1], raw[2]
132
- rtd_adc_code = ((rtd_msb << 8) | rtd_lsb) >> 1
133
-
134
- puts "RTD ADC Code: #{rtd_adc_code}"
135
- rtd = (rtd_adc_code * R_REF) / 32_768.0 # PT100 Resistance
136
- temp = callendar_van_dusen(rtd)
137
- puts "PT100 Resistance: #{rtd} ohms"
138
- puts "Callendar-Van Dusen Temp (degC > 0): #{temp}C"
145
+ adc = ((rtd_msb << 8) | rtd_lsb) >> 1
146
+ # puts "RTD ADC Code: #{adc}"
147
+ # temp_line = (adc / 32.0) - 256.0
148
+ # puts "Straight Line Approx. Temp: #{temp_line}C"
149
+ rtd = (adc * ref) / 32_768.0 # PT100 Resistance
150
+ # puts "PT100 Resistance: #{rtd} ohms"
151
+ quadratic(adc, rtd)
139
152
  end
140
153
 
141
154
  #
@@ -1,3 +1,3 @@
1
1
  class MAX31865
2
- VERSION = '0.1.7'.freeze
2
+ VERSION = '0.1.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: max31865
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Piccinini