heat.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # PWM
  2. import math
  3. import time
  4. from cmath import log
  5. import micropython
  6. import gc
  7. import uasyncio
  8. from machine import Pin,PWM,ADC
  9. def average(series, new_value):
  10. series.append(new_value)
  11. if len(series) > 8:
  12. series.pop(0)
  13. total = sum(series)
  14. # print(series)
  15. return total/len(series)
  16. class Ntc():
  17. def __init__(self, pin) -> None:
  18. self.adc = ADC(Pin(pin), atten=ADC.ATTN_11DB)
  19. self.VCC = 3344
  20. self.temp = 0
  21. self.temp_list = []
  22. def cal_temperature(self):
  23. val_uv = self.adc.read_uv()/1000
  24. Rt = val_uv / ((self.VCC - val_uv)/10000)
  25. T1 = 1/(log(Rt/10000)/3950 + 1/(273.15+25)) - 273.15
  26. # 0.5 是误差。T1是复数,T1.real是取它的实部
  27. temp = T1.real + 0.5
  28. self.temp = average(self.temp_list, temp)
  29. # print("Rt:", Rt)
  30. # print("voltage:", val_uv)
  31. # print("T1:", T1.real)
  32. def get_temperature(self):
  33. return self.temp
  34. class HeatPower():
  35. _instance = None
  36. def __new__(cls, *args, **kwargs):
  37. if cls._instance is None:
  38. cls._instance = super().__new__(cls)
  39. return cls._instance # 返回的是内存地址
  40. def __init__(self) -> None:
  41. self.sw_pin = PWM(Pin(15))
  42. self.sw_pin.freq(1000)
  43. # self.sw_pin.duty(180)
  44. self.status = 0
  45. self.duty = 0.0
  46. def duty_percent(self, percent):
  47. if percent > 1:
  48. percent == 1
  49. elif percent < 0:
  50. percent = 0
  51. self.duty = percent
  52. set_value = round(self.duty * 1023)
  53. # print(self.duty)
  54. # print(set_value)
  55. self.sw_pin.duty(set_value)
  56. def off(self):
  57. self.status = 0
  58. self.duty_percent(0)
  59. # print("heat off")
  60. def on(self, power_percent=0.8):
  61. self.status = 1
  62. self.duty_percent(power_percent)
  63. # print("heat on")
  64. def ajust_duty(self, step=0):
  65. self.duty += step
  66. if self.duty > 1:
  67. self.duty = 1
  68. elif self.duty < 0:
  69. self.duty = 0
  70. self.duty_percent(self.duty)
  71. def ajust_power(self, wt, ct):
  72. if not self.status:
  73. return
  74. if wt > 88:
  75. step = (88-wt)/100
  76. self.ajust_duty(step)
  77. # print('wt:', wt, "step:", step)
  78. elif ct > 45:
  79. step = (45-ct)/100
  80. self.ajust_duty(step)
  81. else:
  82. step = (42-ct)/100
  83. # print('ct:', ct, "step:", step)
  84. self.ajust_duty(step)
  85. wire_ntc = Ntc(34)
  86. center_ntc = Ntc(36)
  87. heat = HeatPower()
  88. async def cal_ntc():
  89. while True:
  90. wire_ntc.cal_temperature()
  91. center_ntc.cal_temperature()
  92. await uasyncio.sleep_ms(200)
  93. async def sensor():
  94. uasyncio.create_task(cal_ntc())
  95. heat.on()
  96. cnt = 0
  97. while True:
  98. cnt += 1
  99. if cnt > 10:
  100. cnt = 0
  101. print("wire_temp:", wire_temp, "\tcenter_temp:", center_temp, '\tduty:', heat.duty)
  102. gc.collect()
  103. wire_temp = wire_ntc.get_temperature()
  104. center_temp = center_ntc.get_temperature()
  105. heat.ajust_power(wire_ntc.get_temperature(), center_ntc.get_temperature())
  106. # 主动垃圾回收是个好习惯,如果没有经常开辟内存,可不用
  107. await uasyncio.sleep_ms(300)
  108. def test():
  109. wire_ntc = Ntc(34)
  110. center_ntc = Ntc(36)
  111. print("class Ntc:", wire_ntc, center_ntc)
  112. print("class adc:", wire_ntc.adc, center_ntc.adc)
  113. print("tim:", wire_ntc.tim, center_ntc.tim)
  114. if __name__ == '__main__':
  115. uasyncio.run(sensor())
  116. # test()