1
0

thermistor.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const PULLUP_RESISTOR_KOHM = 10
  2. const RT_TABLE = [
  3. [-40, 345.275], [-39, 322.791], [-38, 301.925], [-37, 282.549],
  4. [-36, 264.549], [-35, 247.816], [-34, 232.254], [-33, 217.774],
  5. [-32, 204.292], [-31, 191.735], [-30, 180.032], [-29, 169.12],
  6. [-28, 158.941], [-27, 149.441], [-26, 140.571], [-25, 132.284],
  7. [-24, 124.522], [-23, 117.266], [-22, 110.48], [-21, 104.13],
  8. [-20, 98.185], [-19, 92.618], [-18, 87.402], [-17, 82.513],
  9. [-16, 77.927], [-15, 73.626], [-14, 69.588], [-13, 65.797],
  10. [-12, 62.237], [-11, 58.89], [-10, 55.744], [-9, 52.786],
  11. [-8, 50.002], [-7, 47.382], [-6, 44.916], [-5, 42.592],
  12. [-4, 40.4], [-3, 38.333], [-2, 36.385], [-1, 34.548],
  13. [0, 32.814], [1, 31.179], [2, 29.636], [3, 28.178],
  14. [4, 26.8], [5, 25.497], [6, 24.263], [7, 23.096],
  15. [8, 21.992], [9, 20.947], [10, 19.958], [11, 19.022],
  16. [12, 18.135], [13, 17.294], [14, 16.498], [15, 15.742],
  17. [16, 15.025], [17, 14.345], [18, 13.699], [19, 13.086],
  18. [20, 12.504], [21, 11.951], [22, 11.426], [23, 10.926],
  19. [24, 10.452], [25, 10], [26, 9.57], [27, 9.162],
  20. [28, 8.773], [29, 8.402], [30, 8.049], [31, 7.713],
  21. [32, 7.393], [33, 7.088], [34, 6.797], [35, 6.52],
  22. [36, 6.255], [37, 6.003], [38, 5.762], [39, 5.532],
  23. [40, 5.313], [41, 5.103], [42, 4.903], [43, 4.711],
  24. [44, 4.529], [45, 4.354], [46, 4.187], [47, 4.027],
  25. [48, 3.874], [49, 3.728], [50, 3.588], [51, 3.454],
  26. [52, 3.326], [53, 3.203], [54, 3.086], [55, 2.973],
  27. [56, 2.865], [57, 2.761], [58, 2.662], [59, 2.567],
  28. [60, 2.476], [61, 2.388], [62, 2.304], [63, 2.224],
  29. [64, 2.146], [65, 2.072], [66, 2.001], [67, 1.932],
  30. [68, 1.866], [69, 1.803], [70, 1.742], [71, 1.684],
  31. [72, 1.628], [73, 1.574], [74, 1.522], [75, 1.472],
  32. [76, 1.424], [77, 1.378], [78, 1.333], [79, 1.29],
  33. [80, 1.249], [81, 1.209], [82, 1.171], [83, 1.134],
  34. [84, 1.099], [85, 1.065], [86, 1.032], [87, 1],
  35. [88, 0.969], [89, 0.94], [90, 0.911], [91, 0.884],
  36. [92, 0.857], [93, 0.831], [94, 0.807], [95, 0.783],
  37. [96, 0.76], [97, 0.738], [98, 0.716], [99, 0.695],
  38. [100, 0.675], [101, 0.656], [102, 0.637], [103, 0.619],
  39. [104, 0.602], [105, 0.585], [106, 0.569], [107, 0.553],
  40. [108, 0.538], [109, 0.523], [110, 0.508], [111, 0.495],
  41. [112, 0.481], [113, 0.468], [114, 0.456], [115, 0.443],
  42. [116, 0.432], [117, 0.42], [118, 0.409], [119, 0.399],
  43. [120, 0.388], [121, 0.378], [122, 0.368], [123, 0.359],
  44. [124, 0.35], [125, 0.341]
  45. ]
  46. function interpolate(x, x0, y0, x1, y1) {
  47. if (x1 === x0) return y0
  48. return y0 + (x - x0) * (y1 - y0) / (x1 - x0)
  49. }
  50. function resistanceToTemperature(resistanceKohm) {
  51. const resistance = Number(resistanceKohm)
  52. if (!Number.isFinite(resistance) || resistance <= 0) return null
  53. if (resistance >= RT_TABLE[0][1]) return RT_TABLE[0][0]
  54. const last = RT_TABLE[RT_TABLE.length - 1]
  55. if (resistance <= last[1]) return last[0]
  56. for (let index = 0; index < RT_TABLE.length - 1; index += 1) {
  57. const [temp0, resistance0] = RT_TABLE[index]
  58. const [temp1, resistance1] = RT_TABLE[index + 1]
  59. if (resistance <= resistance0 && resistance >= resistance1) {
  60. return interpolate(resistance, resistance0, temp0, resistance1, temp1)
  61. }
  62. }
  63. return null
  64. }
  65. function temperatureToResistance(temperatureCelsius) {
  66. const temperature = Number(temperatureCelsius)
  67. if (!Number.isFinite(temperature)) return null
  68. if (temperature <= RT_TABLE[0][0]) return RT_TABLE[0][1]
  69. const last = RT_TABLE[RT_TABLE.length - 1]
  70. if (temperature >= last[0]) return last[1]
  71. for (let index = 0; index < RT_TABLE.length - 1; index += 1) {
  72. const [temp0, resistance0] = RT_TABLE[index]
  73. const [temp1, resistance1] = RT_TABLE[index + 1]
  74. if (temperature >= temp0 && temperature <= temp1) {
  75. return interpolate(temperature, temp0, resistance0, temp1, resistance1)
  76. }
  77. }
  78. return null
  79. }
  80. function voltageRatioToResistance(voltageRatio) {
  81. const ratio = Number(voltageRatio)
  82. if (!Number.isFinite(ratio) || ratio <= 0 || ratio >= 1) return null
  83. return PULLUP_RESISTOR_KOHM * ratio / (1 - ratio)
  84. }
  85. function resistanceToVoltageRatio(resistanceKohm) {
  86. const resistance = Number(resistanceKohm)
  87. if (!Number.isFinite(resistance) || resistance <= 0) return null
  88. return resistance / (PULLUP_RESISTOR_KOHM + resistance)
  89. }
  90. function rawToTemperature(rawValue, scaleMax) {
  91. const rawNumber = Number(rawValue)
  92. const maxValue = Number(scaleMax)
  93. if (!Number.isFinite(rawNumber) || !Number.isFinite(maxValue) || maxValue <= 0) return null
  94. if (rawNumber <= 0) return RT_TABLE[RT_TABLE.length - 1][0]
  95. if (rawNumber >= maxValue) return RT_TABLE[0][0]
  96. return resistanceToTemperature(voltageRatioToResistance(rawNumber / maxValue))
  97. }
  98. function temperatureToRaw(temperatureCelsius, scaleMax) {
  99. const resistance = temperatureToResistance(temperatureCelsius)
  100. const ratio = resistanceToVoltageRatio(resistance)
  101. const maxValue = Number(scaleMax)
  102. if (!Number.isFinite(ratio) || !Number.isFinite(maxValue) || maxValue <= 0) return null
  103. return ratio * maxValue
  104. }
  105. module.exports = {
  106. rawToTemperature,
  107. temperatureToRaw
  108. }