sht30_collect.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-23 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include <board.h>
  12. #include <sht3x.h>
  13. #include "sht30_collect.h"
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include "control.h"
  17. #include "hmi.h"
  18. #include "math.h"
  19. #include "test.h"
  20. #define DBG_ENABLE
  21. #define DBG_COLOR
  22. #define DBG_TAG "control"
  23. #define DBG_LEVEL DBG_LOG
  24. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  25. // 温湿度
  26. #define THREAD_PRIORITY 21 // 线程优先级
  27. #define THREAD_STACK_SIZE 1024 // 栈大小
  28. #define THREAD_TIMESLICE 5 // 时间片
  29. // 电压,电流
  30. #define P_THREAD_PRIORITY 13 // 线程优先级
  31. #define P_THREAD_STACK_SIZE 1024 // 栈大小
  32. #define P_THREAD_TIMESCLICE 5 // 时间片
  33. // ADC
  34. #define ADC_DEV_NAME "adc1" /*ADC设备名称*/
  35. #define ADC_DEV_CHANNLE_0 0 /*ADC通道*/ // 电压
  36. #define ADC_DEV_CHANNLE_1 1 /*ADC通道*/ // 电流
  37. #define ADC_DEV_CHANNLE_2 10 /*光控*/
  38. #define ADC_DEV_CHANNLE_3 11 /*雨控*/ // 现在只有一个adc 给雨控用吧 小虫体可以用光照度传感器
  39. #define ADC_DEV_CHANNLE_4 12 /*加热仓温度*/
  40. #define ADC_DEV_CHANNLE_5 13 /*保险丝之后的电压*/
  41. #define ADC_DEV_CHANNLE_6 16 /*内部温度传感器*/
  42. #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
  43. #define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
  44. Device_Status_typedef device_status;
  45. rt_device_t serial_485; /*串口设备句柄*/
  46. struct serial_configure uart_config_485 = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  47. rt_uint32_t adc0_value, adc1_value, in_adc_17, adc13_value, adc16_value;
  48. rt_uint32_t adc10_rain, adc11_light, adc12_ntc;
  49. int8_t hum_value, tem_value;
  50. static rt_thread_t tid1 = RT_NULL;
  51. static rt_thread_t tid2 = RT_NULL;
  52. ADC_HandleTypeDef hadc1;
  53. int32_t Read_Filtered_Current(void);
  54. /*串口初始化*/
  55. int UART_485_INIT(void)
  56. {
  57. serial_485 = rt_device_find(UART_485);
  58. /*修改串口配置参数*/
  59. uart_config_485.baud_rate = BAUD_RATE_9600; // 修改波特率9600
  60. uart_config_485.data_bits = DATA_BITS_8; // 数据位8
  61. uart_config_485.stop_bits = STOP_BITS_1; // 停止位1
  62. uart_config_485.bufsz = 512; // 缓冲区1k
  63. uart_config_485.parity = PARITY_NONE; // 奇校验
  64. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  65. rt_device_control(serial_485, RT_DEVICE_CTRL_CONFIG, &uart_config_485);
  66. /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
  67. rt_device_open(serial_485, RT_DEVICE_FLAG_INT_RX);
  68. return 0;
  69. }
  70. /* 入口函数 */
  71. static void sht30_collect_thread_entry(void *parameter)
  72. {
  73. sht3x_device_t sht3x_device;
  74. static int checklightnum = 0;
  75. static int checknightnum = 0;
  76. sht3x_device = sht3x_init("i2c1", 0x44);
  77. // 此处需要短暂的延时,否则会出现连续读取失败的问题
  78. rt_thread_mdelay(150);
  79. uint16_t log_en_inr = 30; // 打印log的速度 数值越小 打印越频繁
  80. static int16_t log_en_cnt = -1;
  81. while (1)
  82. {
  83. if (sht3x_device)
  84. {
  85. if (RT_EOK == sht3x_read_singleshot(sht3x_device))
  86. {
  87. hum_value = sht3x_device->humidity;
  88. tem_value = sht3x_device->temperature;
  89. if (hum_value != 0)
  90. {
  91. rt_sprintf(device_status.humidity, "%d.%d", (int)sht3x_device->humidity, (uint)(sht3x_device->humidity * 10) % 10);
  92. rt_sprintf(device_status.tempture, "%d.%d", (int)sht3x_device->temperature, (uint)(sht3x_device->temperature * 10) % 10);
  93. }
  94. else
  95. {
  96. rt_kprintf("sht3x_read_singleshot eRR:%d", hum_value);
  97. }
  98. }
  99. else
  100. {
  101. sht3x_softreset(sht3x_device);
  102. }
  103. //// rt_kprintf("sht30 humidity : %d.%d ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
  104. //// rt_kprintf("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
  105. }
  106. else
  107. {
  108. rt_kprintf("read sht3x fail.\r\n");
  109. // 重新初始化
  110. sht3x_deinit(sht3x_device);
  111. sht3x_device = sht3x_init("i2c1", 0x44);
  112. rt_thread_mdelay(150);
  113. }
  114. if (Read_Light_Voltage() > 500)
  115. {
  116. checklightnum++;
  117. if (checklightnum == 20)
  118. {
  119. device_status.sun_status = 1;
  120. checklightnum = 0;
  121. }
  122. }
  123. else
  124. {
  125. checknightnum++;
  126. if (checknightnum == 20)
  127. {
  128. device_status.sun_status = 0;
  129. checknightnum = 0;
  130. }
  131. }
  132. // 温控
  133. // if (atoi(device_status.tempture) >= device_status.tph || atoi(device_status.tempture) <= device_status.tpl)
  134. // {
  135. // device_status.temp_status = 1;
  136. // }
  137. // else if (atoi(device_status.tempture) >= (device_status.tpl + 5) && atoi(device_status.tempture) <= (device_status.tph - 10))
  138. // {
  139. // device_status.temp_status = 0;
  140. // }
  141. if (chk_log_time)
  142. {
  143. LOG_I("光控计数:%d-%d", checklightnum, checknightnum);
  144. }
  145. rt_thread_mdelay(5000);
  146. }
  147. }
  148. /**
  149. * @brief 250411 适配新的电流采集芯片
  150. *
  151. */
  152. // 定义滑动平均滤波器结构体
  153. typedef struct
  154. {
  155. rt_uint16_t *buffer;
  156. int bufferSize;
  157. int currentIndex;
  158. rt_uint16_t sum;
  159. } MovingAverageFilter;
  160. // 初始化滑动平均滤波器
  161. MovingAverageFilter *moving_average_filter_init(int size)
  162. {
  163. MovingAverageFilter *filter = (MovingAverageFilter *)malloc(sizeof(MovingAverageFilter));
  164. if (filter == NULL)
  165. {
  166. return NULL;
  167. }
  168. filter->buffer = (rt_uint16_t *)calloc(size, sizeof(rt_uint16_t));
  169. if (filter->buffer == NULL)
  170. {
  171. free(filter);
  172. return NULL;
  173. }
  174. filter->bufferSize = size;
  175. filter->currentIndex = 0;
  176. filter->sum = 0.0;
  177. return filter;
  178. }
  179. // 更新滑动平均滤波器
  180. rt_uint16_t moving_average_filter_update(MovingAverageFilter *filter, rt_uint16_t newValue)
  181. {
  182. filter->sum -= filter->buffer[filter->currentIndex];
  183. filter->buffer[filter->currentIndex] = newValue;
  184. filter->sum += newValue;
  185. filter->currentIndex = (filter->currentIndex + 1) % filter->bufferSize;
  186. return filter->sum / filter->bufferSize;
  187. }
  188. // 释放滑动平均滤波器资源
  189. void moving_average_filter_free(MovingAverageFilter *filter)
  190. {
  191. if (filter != NULL)
  192. {
  193. free(filter->buffer);
  194. free(filter);
  195. }
  196. }
  197. /**
  198. * @brief 电流读取
  199. *
  200. * @param parameter
  201. */
  202. static void Power_ReadCheck(void *parameter)
  203. {
  204. int rain_cnt = 0;
  205. //int bat_cnt = 0;
  206. int RV;
  207. //MovingAverageFilter *filter = moving_average_filter_init(10);
  208. // MovingAverageFilter* filter_hi = moving_average_filter_init(10);
  209. MovingAverageFilter *filter_v = moving_average_filter_init(10);
  210. rt_thread_mdelay(1000);
  211. while (1)
  212. {
  213. //rt_kprintf("Read_Filtered_Current() > %d \n", Read_Filtered_Current());
  214. // #if PCB_V1 == 0
  215. // rt_uint16_t _24v_current_int = moving_average_filter_update(filter, Read_Real_Current());
  216. // if (_24v_current_int - 2430 > 0)
  217. // {
  218. // sprintf(device_status.power, "%.2f", (float)(_24v_current_int - 2430) / 0.4);
  219. // }
  220. // else
  221. // {
  222. // sprintf(device_status.power, "0.0");
  223. // }
  224. // #else
  225. // rt_uint16_t _24v_current_int = moving_average_filter_update(filter,Read_Real_Current());
  226. // sprintf(device_status.power,"%.2f",(float)_24v_current_int/10);
  227. sprintf(device_status.power, "%d", Read_Filtered_Current());
  228. // #endif
  229. //LOG_I("负载电流:%s", device_status.power);
  230. if ((Read_Real_Voltage() / 10) < 105) // 设备状态低于10.5 欠压保护 此处需要在屏幕明显位置进行提示
  231. {
  232. // 关闭外设
  233. // ctr_photo(OFF); // 关闭相机供电
  234. rt_kprintf("detact low volage %d.%d\n", Read_Real_Voltage() / 100, Read_Real_Voltage() % 100);
  235. // ctr_led(OFF); // 关闭补光灯
  236. device_status.bat_status = 0;
  237. }
  238. else
  239. {
  240. //rt_kprintf("Read_Real_Voltage() / 10 < 105 >>> %d \n" , Read_Real_Voltage() / 10);
  241. device_status.bat_status = 1;
  242. }
  243. RV = 10000 * (4096 - Read_Ntc_Voltage()) / Read_Ntc_Voltage();
  244. if (TempCalculate(RV, 3950, 10000) - TempCalculate(RV, 3950, 10000) > 10)
  245. {
  246. LOG_E("temp value err!!!!");
  247. }
  248. else
  249. {
  250. device_status.heattem = TempCalculate(RV, 3950, 10000);
  251. if (device_status.heattem > 160)
  252. {
  253. device_status.heattem = 25;
  254. }
  255. }
  256. // rt_kprintf("Read_Rain_Voltage > %d\n" , Read_Rain_Voltage());
  257. // 雨控
  258. if (Read_Rain_Voltage() > 250)
  259. {
  260. device_status.C01 |= (1 << 5);
  261. device_status.rain_update = 1;
  262. ctr_lamp(OFF, 1);
  263. ctr_lamp(OFF, 2);
  264. ctr_lamp(OFF, 3);
  265. rain_cnt = 0;
  266. LOG_W("雨控状态触发\n");
  267. }
  268. // else if (device_status.rain_status)
  269. // {
  270. // rain_cnt++;
  271. // if(Read_Rain_Voltage() < 250)
  272. // {
  273. // rain_cnt++;
  274. // if(rain_cnt >= 10 * 4)
  275. // {
  276. // device_status.rain_status = 0;
  277. // LOG_I("雨控状态恢复\n");
  278. // rain_cnt = 0;
  279. // }
  280. // }
  281. // else
  282. // {
  283. // rain_cnt = 0;
  284. // }
  285. // }
  286. // rt_sprintf(device_status.voltage,"%d.%02d",filter_voltage()/100,filter_voltage()%100);
  287. rt_uint16_t in_voltage_int = moving_average_filter_update(filter_v, Read_Real_Voltage());
  288. sprintf(device_status.voltage, "%d.%02d", in_voltage_int / 100, in_voltage_int % 100);
  289. // LOG_D("voltage is:%sV",device_status.voltage);
  290. sprintf(device_status.fuse_voltage, "%d.%02d", Read_Fuse_Voltage() / 100, Read_Fuse_Voltage() % 100);
  291. rt_thread_mdelay(10);
  292. }
  293. }
  294. /**
  295. * @brief 250411 适配新的电流采集芯片
  296. *
  297. */
  298. /**
  299. *
  300. * @return 250411 修改电流采集函数 过去的太为杂乱
  301. *
  302. *
  303. */
  304. // ADC参数
  305. #define ADC_MAX 4095u // 12位ADC最大值
  306. #define VREF_ADC_MV 3300 // ADC参考电压(单位:mV)
  307. #define DIVIDER_RATIO_NUM 2 // 分压比分子
  308. #define DIVIDER_RATIO_DEN 3 // 分压比分母
  309. // 传感器参数
  310. #define V_SUPPLY_MV 5100 // 传感器供电电压(单位:mV)// 先不要动这个
  311. #define V_OFFSET_MV (V_SUPPLY_MV / 2) // 零点电压(2755mV)
  312. #define SENSITIVITY_MA 100 // 灵敏度(0.1V/A → 100mV/A)
  313. //#define SENSITIVITY_MA 400 // 灵敏度(0.4V/A → 400mV/A,对应±5A) 若修改为5A芯片 用这个
  314. // 低通滤波参数(α=0.2,整数运算实现)
  315. #define FILTER_ALPHA 1 // α分子(α = 1/5 = 0.2)
  316. #define FILTER_BETA 4 // β分母(β = 1 - α = 4/5)
  317. // 250603 新
  318. // 硬件参数定义
  319. #define VCC 5000 // 电源电压5V (使用mV单位)
  320. #define ADC_REF 3300 // STM32 ADC参考电压3.3V (mV)
  321. #define ADC_RES 4095 // 12位ADC分辨率
  322. // 分压电阻参数 (放大1000倍避免浮点)
  323. #define R1 10000 // 10K上拉电阻
  324. #define R2 20000 // 20K下拉电阻
  325. #define RATIO_NUM (R1 + R2) // 分子
  326. #define RATIO_DEN R2 // 分母
  327. // 芯片选择(取消注释切换量程)
  328. #define CC6900_5A
  329. // #define CC6900_10A
  330. #ifdef CC6900_5A
  331. #define SENSITIVITY 400 // 5A芯片灵敏度(mV/A)
  332. #define CURRENT_RANGE 5000 // 5A量程(mA)
  333. #else
  334. #define SENSITIVITY 200 // 10A芯片灵敏度(mV/A)
  335. #define CURRENT_RANGE 10000 // 10A量程(mA)
  336. #endif
  337. // 精度放大因子 (Q10格式,放大1024倍)
  338. #define SCALE_FACTOR 1024
  339. /**
  340. * @brief 将ADC值转换为电流值(mA) - 定点数版本
  341. * @param adcValue ADC读取的原始值(0-4095)
  342. * @return 电流值(mA),正值表示正向电流,负值表示反向电流
  343. */
  344. int32_t ADC_to_Current_FixedPoint(uint16_t adcValue)
  345. {
  346. // rt_kprintf("adc >>> %d \n" , adcValue);
  347. // 1. 计算ADC电压 (mV) = adcValue * ADC_REF / ADC_RES
  348. uint32_t adcVoltage = ((uint32_t)adcValue * ADC_REF) / ADC_RES;
  349. // 2. 计算VOUT实际电压 (mV) = adcVoltage * (R1+R2)/R2
  350. uint32_t vout = (adcVoltage * RATIO_NUM) / RATIO_DEN;
  351. // 3. 计算相对于VCC/2的电压差 (mV)
  352. int32_t voltageDiff = (int32_t)vout - (VCC / 2);
  353. // 4. 计算电流 (mA) = (voltageDiff * 1000) / SENSITIVITY
  354. // 使用SCALE_FACTOR保持精度,最后再除回去
  355. int32_t current = (voltageDiff * 1000 * SCALE_FACTOR) / SENSITIVITY;
  356. // 5. 移除缩放因子并返回
  357. return current / SCALE_FACTOR;
  358. }
  359. // 250603 新
  360. /**
  361. * @brief 读取并滤波后的电流值(单位:mA,整数)
  362. * @return int32_t 滤波后的电流值
  363. */
  364. int32_t Read_Filtered_Current(void)
  365. {
  366. static int32_t filtered_current = 0; // 静态变量保存滤波结果
  367. // 1. 读取原始电流值
  368. int32_t raw_current = ADC_to_Current_FixedPoint(Read_Real_Current());
  369. // rt_kprintf("raw_current >>> %d \n" , raw_current);
  370. // 2. 一阶低通滤波(整数运算)
  371. // 公式:filtered = (α * raw + β * filtered_prev) / (α + β)
  372. filtered_current = (FILTER_ALPHA * raw_current + FILTER_BETA * filtered_current) /
  373. (FILTER_ALPHA + FILTER_BETA);
  374. filtered_current = abs(filtered_current);
  375. return filtered_current;
  376. }
  377. /*读取电流值*/
  378. int Read_Real_Current(void)
  379. {
  380. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  381. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  382. if (adc_dev == RT_NULL)
  383. {
  384. LOG_E("adc sample run failed! can't find %s device!", ADC_DEV_NAME);
  385. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  386. }
  387. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  388. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_1);
  389. // rt_adc_enable(adc_dev, 17);
  390. adc1_value = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_1));
  391. in_adc_17 = filter_Mid(rt_adc_read(adc_dev, 17));
  392. /* 关闭通道 */
  393. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_1);
  394. // rt_adc_disable(adc_dev, 17);
  395. // rt_kprintf("read in adc:ADC17:%d-ADC1:%d\n",in_adc_17,adc1_value);
  396. return adc1_value;
  397. }
  398. /**
  399. *
  400. * @return 250411 修改电流采集函数 过去的太为杂乱
  401. *
  402. *
  403. */
  404. /*
  405. * https://blog.csdn.net/loop222/article/details/120497425
  406. 采样电阻0.01 Ω
  407. 运放为INA240A2,放大倍数50
  408. 背面需分别短接A0/A2至输出
  409. C1_OUT=INA_VCC/2 + 0.01 * I * 50
  410. 如果INA_VCC=3.3V,C1_OUT=1.65 + 0.01 * I * 50,电流范围(-3.3A,3.3A)
  411. 如果INA_VCC=5.0V,C1_OUT=2.50 + 0.01 * I * 50,电流范围(-5A,5A)
  412. B相采样输入在PCB上为走线方便做了反向输入,所以放大倍数需乘以 -1(移植后的代码已经做了处理)
  413. */
  414. /*读取电压值*/
  415. int Read_Real_Voltage(void)
  416. {
  417. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  418. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  419. if (adc_dev == RT_NULL)
  420. {
  421. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  422. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  423. }
  424. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  425. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_0);
  426. // adc0_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_0);
  427. adc0_value = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_0));
  428. /* 关闭通道 */
  429. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_0);
  430. return (adc0_value * REFER_VOLTAGE / CONVERT_BITS * 12 + 20);
  431. }
  432. /*读取保险丝的电压值*/
  433. int Read_Fuse_Voltage(void)
  434. {
  435. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  436. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  437. if (adc_dev == RT_NULL)
  438. {
  439. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  440. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  441. }
  442. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  443. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_5);
  444. adc13_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_5);
  445. /* 关闭通道 */
  446. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_5);
  447. return (adc13_value * REFER_VOLTAGE / CONVERT_BITS * 12 + 8);
  448. }
  449. /*读取雨控*/
  450. int Read_Rain_Voltage(void)
  451. {
  452. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  453. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  454. if (adc_dev == RT_NULL)
  455. {
  456. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  457. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  458. }
  459. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_3);
  460. adc11_light = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_3);
  461. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_3);
  462. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  463. // LOG_D("read rain value:%d",adc11_light);
  464. return adc11_light;
  465. // rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  466. // return rt_pin_read(RAIN_PIN);
  467. }
  468. /*读取光控*/
  469. int Read_Light_Voltage(void)
  470. {
  471. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  472. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  473. if (adc_dev == RT_NULL)
  474. {
  475. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  476. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  477. }
  478. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_2);
  479. adc10_rain = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_2);
  480. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_2);
  481. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  482. // rt_kprintf("read light value:%d\n",adc10_rain);
  483. return adc10_rain;
  484. }
  485. // 获取内部温度传感器值
  486. short Get_Temprate(void)
  487. {
  488. short result;
  489. double temperate;
  490. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  491. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  492. if (adc_dev == RT_NULL)
  493. {
  494. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  495. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  496. }
  497. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  498. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_6);
  499. adc16_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_6);
  500. /* 关闭通道 */
  501. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_6);
  502. temperate = (float)adc16_value * (3.3 / 4096); // 电压值
  503. temperate = (1.43 - temperate) / 0.0043 + 25; // 转换为温度值
  504. result = temperate *= 100; // 扩大100倍.
  505. return result;
  506. }
  507. /**
  508. * Rv: 热敏电阻当前阻值
  509. * B: 热敏电阻参数B值
  510. * Revise: 校正温度
  511. * BasicRx: 热敏电阻25度时电阻(标称电阻数值)
  512. * 返回: 摄氏度
  513. */
  514. int TempCalculate(float Rv, int B, int BasicRx)
  515. {
  516. float Rx;
  517. Rx = Rv * 1.0 / BasicRx;
  518. Rx = log(Rx);
  519. Rx = Rx / B;
  520. Rx = 1 / (Rx + 0.003354);
  521. // Rx = Rx + 0.003356;
  522. // Rx = 1 / Rx;
  523. Rx = Rx - 273.13;
  524. return (int)(Rx + 0.5);
  525. }
  526. /*
  527. * 中位值平均滤波法
  528. * */
  529. unsigned int filter_Mid(int adc_return)
  530. {
  531. unsigned char count, i, j;
  532. unsigned int value_buf[N], temp = 0;
  533. int sum = 0;
  534. for (count = 0; count < N; count++)
  535. {
  536. value_buf[count] = adc_return;
  537. // rt_kprintf("value_buf:%d-%d\n",value_buf[count],count);
  538. rt_thread_delay(10);
  539. }
  540. for (j = 0; j < N - 1; j++)
  541. {
  542. for (i = 0; i < N - j - 1; i++)
  543. {
  544. if (value_buf[i] > value_buf[i + 1])
  545. {
  546. temp = value_buf[i];
  547. value_buf[i] = value_buf[i + 1];
  548. value_buf[i + 1] = temp;
  549. }
  550. }
  551. }
  552. for (count = 1; count < N - 1; count++)
  553. {
  554. sum += value_buf[count];
  555. }
  556. return (int)(sum / (N - 2));
  557. }
  558. /*
  559. 限幅消抖滤波法
  560. A、方法:
  561. 相当于“限幅滤波法”+“消抖滤波法”
  562. 先限幅,后消抖
  563. B、优点:
  564. 继承了“限幅”和“消抖”的优点
  565. 改进了“消抖滤波法”中的某些缺陷,避免将干扰值导入系统
  566. C、缺点:
  567. 对于快速变化的参数不宜
  568. */
  569. unsigned int filter_voltage(void)
  570. {
  571. uint8_t data_cnt = 0;
  572. int value = 0, new_value = 0;
  573. new_value = Read_Real_Voltage();
  574. while (value != new_value)
  575. {
  576. data_cnt++;
  577. if (data_cnt >= N)
  578. return new_value;
  579. rt_thread_delay(10);
  580. new_value = Read_Real_Voltage();
  581. }
  582. return value;
  583. }
  584. /*读取加热仓温度*/
  585. int Read_Ntc_Voltage(void)
  586. {
  587. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  588. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  589. if (adc_dev == RT_NULL)
  590. {
  591. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  592. return RT_ERROR; // adc 采样值不可能为负值,返回负值表示出错
  593. }
  594. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_4);
  595. adc12_ntc = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_4));
  596. // LOG_D("Read_Ntc_Voltage:%d",adc12_ntc);
  597. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_4);
  598. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  599. return adc12_ntc;
  600. }
  601. /* 创建线程 */
  602. int sht30_collect(void)
  603. {
  604. /* 创建线程*/
  605. //tid1 = rt_thread_create("sht30_collect_thread", sht30_collect_thread_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  606. tid2 = rt_thread_create("Power Check thread", Power_ReadCheck, RT_NULL, P_THREAD_STACK_SIZE + 512, P_THREAD_PRIORITY, P_THREAD_TIMESCLICE);
  607. /* 如果获得线程控制块,启动这个线程 */
  608. // if (tid1 != RT_NULL)
  609. // rt_thread_startup(tid1);
  610. if (tid2 != RT_NULL)
  611. rt_thread_startup(tid2);
  612. return 0;
  613. }
  614. // 控制台打印传感器信息
  615. void Msh_DisplaySensor(void)
  616. {
  617. rt_kprintf("温度:%d\n", tem_value);
  618. rt_kprintf("湿度:%d\n", hum_value);
  619. rt_kprintf("电压:%s\n", device_status.voltage);
  620. rt_kprintf("电流:%s\n", device_status.power);
  621. }
  622. MSH_CMD_EXPORT(Msh_DisplaySensor, Msh_DisplaySensor);
  623. MSH_CMD_EXPORT(Read_Rain_Voltage, Read_Rain_Voltage);
  624. MSH_CMD_EXPORT(Read_Light_Voltage, Read_Light_Voltage);