You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
9.6 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "hardware.hpp"
  2. #include "adc.h"
  3. #include "tim.h"
  4. #include "zsdk/zcanreceiver/zcanreceiver.hpp"
  5. #define TAG "HARD"
  6. using namespace iflytop;
  7. /***********************************************************************************************************************
  8. * EXT *
  9. ***********************************************************************************************************************/
  10. static osThreadId H2O2CaptureThreadId;
  11. static osThreadId AdcCaptureThreadId;
  12. /***********************************************************************************************************************
  13. * FUNC *
  14. ***********************************************************************************************************************/
  15. static void c_onAdcCaptureThread(void const* argument) { Hardware::ins().onAdcCaptureThread(); }
  16. static void c_onH2O2CaptureThread(void const* argument) { Hardware::ins().onH2O2CaptureThread(); }
  17. void Hardware::init() {
  18. /***********************************************************************************************************************
  19. * *
  20. ***********************************************************************************************************************/
  21. m_Heater_ctrlGpio.initAsOutput(HEATER_CTRL_GPIO, kxs_gpio_nopull, true, false);
  22. m_Heater_safeCtrlGpio.initAsOutput(HEATER_SAFE_CTRL_GPIO, kxs_gpio_nopull, true, false);
  23. m_Heater_electricCurrentAdc.initialize(&hadc1, HEATER_ELECTRIC_CURRENT_ADC_CH);
  24. m_Heater_temperatureAdc.initialize(&hadc1, HEATER_TEMPERATURE_ADC_CH);
  25. /***********************************************************************************************************************
  26. * *
  27. ***********************************************************************************************************************/
  28. m_Blowser_ctrlGpio.initAsOutput(BLOWER_CTRL_GPIO, kxs_gpio_nopull, true, false);
  29. m_Blowser_safeCtrlGpio.initAsOutput(BLOWER_SAFE_CTRL_GPIO, kxs_gpio_nopull, true, false);
  30. m_Blowser_electricCurrentAdc.initialize(&hadc1, BLOWER_ELECTRIC_CURRENT_ADC_CH);
  31. /***********************************************************************************************************************
  32. * *
  33. ***********************************************************************************************************************/
  34. m_AirCompressor_ctrlGpio.initAsOutput(AIRCOMPRESSOR_CTRL_GPIO, kxs_gpio_nopull, true, false);
  35. m_AirCompressor_safeCtrlGpio.initAsOutput(AIRCOMPRESSOR_SAFE_CTRL_GPIO, kxs_gpio_nopull, true, false);
  36. m_AirCompressor_electricCurrentAdc.initialize(&hadc1, AIRCOMPRESSOR_ELECTRIC_CURRENT_ADC_CH);
  37. /***********************************************************************************************************************
  38. * H2O2传感器 *
  39. ***********************************************************************************************************************/
  40. /**
  41. * @brief HMP110
  42. */
  43. osDelay(2000); // 等待传感器上电
  44. #ifdef H2O2_SENSOR_TYPE_HMP110
  45. ZASSERT(huart2.Init.BaudRate == 19200);
  46. m_H2o2Sensor_ModbusBlockHost.initialize(&huart2);
  47. m_H2o2Sensor_H2O2Adc.initialize(&hadc1, ADC_CHANNEL_0); // PA0
  48. m_H2o2Sensor_HMP110.init(&m_H2o2Sensor_ModbusBlockHost);
  49. if (m_H2o2Sensor_HMP110.ping(1)) {
  50. m_h2o2sensor_detectId = 1;
  51. }
  52. if (m_H2o2Sensor_HMP110.ping(240)) {
  53. m_h2o2sensor_detectId = 240;
  54. }
  55. m_H2o2Sensor_HMP110.setid(m_h2o2sensor_detectId);
  56. ZLOGI(TAG, "H2O2 HMP110 Sensor detect id: %d", m_h2o2sensor_detectId);
  57. #endif
  58. #ifdef H2O2_SENSOR_TYPE_HPP272
  59. ZASSERT(huart3.Init.BaudRate == 19200);
  60. ZASSERT(huart3.Init.StopBits == UART_STOPBITS_2);
  61. m_H2o2Sensor_ModbusBlockHost.initialize(&huart3);
  62. m_H2o2Sensor_HPP272.init(&m_H2o2Sensor_ModbusBlockHost);
  63. if (m_H2o2Sensor_HPP272.ping(1)) {
  64. m_h2o2sensor_detectId = 1;
  65. }
  66. if (m_H2o2Sensor_HPP272.ping(240)) {
  67. m_h2o2sensor_detectId = 240;
  68. }
  69. m_H2o2Sensor_HPP272.setid(m_h2o2sensor_detectId);
  70. ZLOGI(TAG, "H2O2 HPP272 Sensor detect id: %d", m_h2o2sensor_detectId);
  71. #endif
  72. osThreadDef(AdcCaptureThread, c_onAdcCaptureThread, osPriorityNormal, 0, 1024);
  73. AdcCaptureThreadId = osThreadCreate(osThread(AdcCaptureThread), NULL);
  74. osThreadDef(H2O2CaptureThread, c_onH2O2CaptureThread, osPriorityNormal, 0, 1024);
  75. H2O2CaptureThreadId = osThreadCreate(osThread(H2O2CaptureThread), NULL);
  76. }
  77. /***********************************************************************************************************************
  78. * H2O2 *
  79. ***********************************************************************************************************************/
  80. bool Hardware::h2o2_sensor_is_online() {
  81. #ifdef H2O2_SENSOR_TYPE_HMP110
  82. if (m_h2o2sensor_detectId <= 0) return false;
  83. int32_t ecode = m_H2o2Sensor_HMP110.read_cache_errorcode();
  84. if (ecode == -1) return false;
  85. #endif
  86. #ifdef H2O2_SENSOR_TYPE_HPP272
  87. if (m_h2o2sensor_detectId <= 0) return false;
  88. int32_t ecode = m_H2o2Sensor_HPP272.read_cache_errorcode();
  89. if (ecode == -1) return false;
  90. #endif
  91. return true;
  92. }
  93. int32_t Hardware::h2o2_sensor_read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { //
  94. *year = 1;
  95. *month = 2;
  96. *day = 3;
  97. return 0;
  98. }
  99. int32_t Hardware::h2o2_sensor_read_sub_ic_errorcode() { //
  100. #ifdef H2O2_SENSOR_TYPE_HPP272
  101. return m_H2o2Sensor_HPP272.read_cache_errorcode();
  102. #endif
  103. #ifdef H2O2_SENSOR_TYPE_HMP110
  104. return m_H2o2Sensor_HMP110.read_cache_errorcode();
  105. #endif
  106. }
  107. int32_t Hardware::h2o2_sensor_read_sub_ic_reg(int32_t add, uint16_t* val, size_t len) { //
  108. #ifdef H2O2_SENSOR_TYPE_HPP272
  109. return m_H2o2Sensor_HPP272.read_reg(add, val, len);
  110. #endif
  111. #ifdef H2O2_SENSOR_TYPE_HMP110
  112. return m_H2o2Sensor_HMP110.read_reg(add, val, len);
  113. #endif
  114. }
  115. int32_t Hardware::h2o2_sensor_data(report_h2o2_data_t* readdata) {
  116. #ifdef H2O2_SENSOR_TYPE_HMP110
  117. int32_t ecode = m_H2o2Sensor_HMP110.read_cache_errorcode();
  118. int32_t h2o2adcVal = m_H2o2Sensor_H2O2Adc.getCacheVal();
  119. HMP110::hmp110_sensordata_t sensordata;
  120. m_H2o2Sensor_HMP110.read_cache_sensor_data(&sensordata);
  121. // float mv = adcv / 4095.0 * 3.3 * 1000;
  122. // float ma = mv / 150.0;
  123. // float ppm = (ma - 4) / (20 - 4) * 2000;
  124. int32_t h2o2ma = (h2o2adcVal / 4095.0 * 3.3 * 1000) / 150.0;
  125. int32_t h2o2ppm = (h2o2ma - 4) / (20 - 4) * 2000;
  126. readdata->subid = 0;
  127. readdata->sensor_error = ecode != 0;
  128. readdata->h2o2 = h2o2ppm;
  129. readdata->humid = sensordata.rh;
  130. readdata->temp = sensordata.temp;
  131. readdata->saturation = 0;
  132. ZLOGI(TAG, "ppm:%d, rh:%d, temp:%d, df_ptemp:%d, ah:%d, mr:%d, wbt:%d, eh:%d", //
  133. h2o2ppm, //
  134. sensordata.rh, //
  135. sensordata.temp, //
  136. sensordata.df_ptemp, //
  137. sensordata.ah, //
  138. sensordata.mr, //
  139. sensordata.wet_bulb_temp, //
  140. sensordata.enthalpy);
  141. return 0;
  142. #endif
  143. #ifdef H2O2_SENSOR_TYPE_HPP272
  144. int32_t ecode = m_H2o2Sensor_HPP272.read_cache_errorcode();
  145. HPP272::hpp272_data_t sensordata;
  146. m_H2o2Sensor_HPP272.read_cache_sensor_data(&sensordata);
  147. readdata->sensor_error = ecode != 0;
  148. readdata->h2o2 = sensordata.hydrogen_peroxide_volume / 10.0;
  149. readdata->humid = sensordata.relative_humidity / 10.0;
  150. readdata->temp = sensordata.temperature1 / 10.0;
  151. readdata->saturation = sensordata.h2o_h2o2_rs / 10.0;
  152. ZLOGI(TAG, "h2o2 %d, humid %d, temp %d, sat %d",
  153. sensordata.hydrogen_peroxide_volume / 10, //
  154. sensordata.relative_humidity / 10, //
  155. sensordata.temperature1 / 10, //
  156. sensordata.h2o_h2o2_rs / 10);
  157. return 0;
  158. #endif
  159. }
  160. void Hardware::onAdcCaptureThread() {
  161. while (1) {
  162. // osDelay(30);
  163. m_Heater_electricCurrentAdc.updateAdcValToCache();
  164. m_Heater_temperatureAdc.updateAdcValToCache();
  165. m_AirCompressor_electricCurrentAdc.updateAdcValToCache();
  166. m_Blowser_electricCurrentAdc.updateAdcValToCache();
  167. osDelay(1000);
  168. int32_t adcv1 = m_Blowser_electricCurrentAdc.getCacheVal();
  169. int32_t adcv2 = m_AirCompressor_electricCurrentAdc.getCacheVal();
  170. int32_t adcv3 = m_Heater_electricCurrentAdc.getCacheVal();
  171. ZLOGI(TAG, "b:%d, a:%d, h:%d", adcv1, adcv2, adcv3);
  172. }
  173. }
  174. void Hardware::onH2O2CaptureThread() {
  175. while (1) {
  176. osDelay(1000);
  177. #ifdef H2O2_SENSOR_TYPE_HMP110
  178. if (m_h2o2sensor_detectId > 0) {
  179. m_H2o2Sensor_H2O2Adc.updateAdcValToCache();
  180. m_H2o2Sensor_HMP110.updateSensorDataAndErrorcode();
  181. }
  182. #endif
  183. #ifdef H2O2_SENSOR_TYPE_HPP272
  184. if (m_h2o2sensor_detectId > 0) {
  185. m_H2o2Sensor_HPP272.updateSensorDataAndErrorcode();
  186. }
  187. #endif
  188. }
  189. }