sht30_collect.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #define DBG_ENABLE
  20. #define DBG_COLOR
  21. #define DBG_TAG "control"
  22. #define DBG_LEVEL DBG_LOG
  23. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  24. //温湿度
  25. #define THREAD_PRIORITY 21 //线程优先级
  26. #define THREAD_STACK_SIZE 1024 //栈大小
  27. #define THREAD_TIMESLICE 5 //时间片
  28. //电压,电流
  29. #define P_THREAD_PRIORITY 13 //线程优先级
  30. #define P_THREAD_STACK_SIZE 1024 //栈大小
  31. #define P_THREAD_TIMESCLICE 5 //时间片
  32. //ADC
  33. #define ADC_DEV_NAME "adc1" /*ADC设备名称*/
  34. #define ADC_DEV_CHANNLE_0 0 /*ADC通道*///电流
  35. #define ADC_DEV_CHANNLE_1 1 /*ADC通道*///电压
  36. #define ADC_DEV_CHANNLE_2 10 /*雨控*/
  37. #define ADC_DEV_CHANNLE_3 11 /*光控*/
  38. #define ADC_DEV_CHANNLE_4 12 /*加热仓温度*/
  39. #define ADC_DEV_CHANNLE_5 13 /*保险丝之后的电压*/
  40. #define ADC_DEV_CHANNLE_6 16 /*内部温度传感器*/
  41. #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
  42. #define CONVERT_BITS (1<<12) /* 转换位数为12位 */
  43. Device_Status_typedef device_status;
  44. rt_device_t serial_485; /*串口设备句柄*/
  45. struct serial_configure uart_config_485 = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  46. rt_uint32_t adc0_value,adc1_value,in_adc_17,adc13_value,adc16_value;
  47. rt_uint32_t adc10_rain,adc11_light,adc12_ntc;
  48. int8_t hum_value,tem_value;
  49. static rt_thread_t tid1 = RT_NULL;
  50. static rt_thread_t tid2 = RT_NULL;
  51. ADC_HandleTypeDef hadc1;
  52. /*串口初始化*/
  53. int UART_485_INIT(void)
  54. {
  55. serial_485 = rt_device_find(UART_485);
  56. /*修改串口配置参数*/
  57. uart_config_485.baud_rate = BAUD_RATE_9600; //修改波特率9600
  58. uart_config_485.data_bits = DATA_BITS_8; //数据位8
  59. uart_config_485.stop_bits = STOP_BITS_1; //停止位1
  60. uart_config_485.bufsz = 512; //缓冲区1k
  61. uart_config_485.parity = PARITY_NONE; //奇校验
  62. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  63. rt_device_control(serial_485, RT_DEVICE_CTRL_CONFIG, &uart_config_485);
  64. /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
  65. rt_device_open(serial_485, RT_DEVICE_FLAG_INT_RX);
  66. return 0;
  67. }
  68. /* 入口函数 */
  69. static void sht30_collect_thread_entry(void *parameter)
  70. {
  71. sht3x_device_t sht3x_device;
  72. static int checklightnum = 0;
  73. static int checknightnum = 0;
  74. sht3x_device = sht3x_init("i2c1", 0x44);
  75. //此处需要短暂的延时,否则会出现连续读取失败的问题
  76. rt_thread_mdelay(150);
  77. while (1)
  78. {
  79. if(sht3x_device)
  80. {
  81. if(RT_EOK == sht3x_read_singleshot(sht3x_device))
  82. {
  83. hum_value = sht3x_device->humidity;
  84. tem_value = sht3x_device->temperature;
  85. if(hum_value!=0)
  86. {
  87. rt_sprintf(device_status.humidity,"%d.%d",(int)sht3x_device->humidity, (uint)(sht3x_device->humidity * 10) % 10);
  88. rt_sprintf(device_status.tempture,"%d.%d",(int)sht3x_device->temperature, (uint)(sht3x_device->temperature * 10) % 10);
  89. }
  90. else {
  91. rt_kprintf("sht3x_read_singleshot eRR:%d",hum_value);
  92. }
  93. }
  94. else {
  95. sht3x_softreset(sht3x_device);
  96. }
  97. //// rt_kprintf("sht30 humidity : %d.%d ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
  98. //// rt_kprintf("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
  99. }
  100. else
  101. {
  102. rt_kprintf("read sht3x fail.\r\n");
  103. //重新初始化
  104. sht3x_deinit(sht3x_device);
  105. sht3x_device = sht3x_init("i2c1", 0x44);
  106. rt_thread_mdelay(150);
  107. }
  108. if(Read_Light_Voltage()>500)
  109. {
  110. checklightnum++;
  111. if(checklightnum==20)
  112. {
  113. device_status.sun_status = 1;
  114. checklightnum =0;
  115. }
  116. }
  117. else {
  118. checknightnum ++;
  119. if(checknightnum==20)
  120. {
  121. device_status.sun_status = 0;
  122. checknightnum = 0;
  123. }
  124. }
  125. //温控
  126. if(atoi(device_status.tempture)>=device_status.tph||atoi(device_status.tempture)<=device_status.tpl)
  127. {
  128. device_status.temp_status = 1;
  129. }
  130. else if(atoi(device_status.tempture)>=(device_status.tpl+5)&&atoi(device_status.tempture)<=(device_status.tph-10)) {
  131. device_status.temp_status = 0;
  132. }
  133. LOG_I("光控计数:%d-%d",checklightnum,checknightnum);
  134. rt_thread_mdelay(5000);
  135. }
  136. }
  137. static void Power_ReadCheck(void *parameter)
  138. {
  139. int rain_cnt = 0;
  140. int RV;
  141. while(1)
  142. {
  143. #if ROBOT_EN == 0
  144. sprintf(device_status.power,"%.2f",Read_Real_Current()/100.0*2.5);
  145. #endif
  146. if (Read_Real_Voltage()/100<12) {
  147. //关闭外设
  148. ctr_photo(OFF); //关闭相机供电
  149. ctr_led(OFF); //关闭补光灯
  150. ctr_Heat(OFF); //关闭加热
  151. ctr_Cold(OFF); //关闭制冷
  152. #if Color_EN == 0
  153. ctr_cangmen_down(OFF); //关闭下仓门输出(转盘)
  154. ctr_cangmen_up(OFF); //关闭上仓门输出(散热)
  155. #endif
  156. }
  157. if(Read_Real_Voltage()/100<21)
  158. {
  159. #if ROBOT_EN == 1
  160. ctr_led(OFF);
  161. HMI_WRITE("sleep=%d",1);
  162. #endif
  163. device_status.bat_status = 1; //欠压
  164. }
  165. else {
  166. device_status.bat_status = 0; //解除欠压
  167. //ctr_router(ON); //打开路由器供电
  168. ctr_photo(ON); //打开相机供电
  169. #if ROBOT_EN == 1
  170. ctr_led(ON); //打开补光灯
  171. ctr_lamp(ON, 1); //打开镇流器输出,用于24V转12V给相机供电
  172. ctr_router(ON);
  173. HMI_WRITE("sleep=%d",0);
  174. #endif
  175. }
  176. //
  177. if(device_status.dtype==7)
  178. {
  179. //箱体温度
  180. RV = 10000 * (4096 - Read_Rain_Voltage()) / Read_Rain_Voltage();
  181. if(TempCalculate(RV, 3950, 10000)-TempCalculate(RV, 3950, 10000)>10)
  182. {
  183. LOG_E("temp value err!!!!");
  184. }
  185. else
  186. {
  187. device_status.box_temp = TempCalculate(RV, 3950, 10000);
  188. if(device_status.box_temp>160)
  189. {
  190. device_status.box_temp = 25;
  191. }
  192. }
  193. //培养仓温度
  194. RV = 10000 * (4096 - Read_Light_Voltage()) / Read_Light_Voltage();
  195. if(TempCalculate(RV, 3950, 10000)-TempCalculate(RV, 3950, 10000)>10)
  196. {
  197. LOG_E("temp value err!!!!");
  198. }
  199. else
  200. {
  201. device_status.pre_temp = TempCalculate(RV, 3950, 10000);
  202. if(device_status.pre_temp>160)
  203. {
  204. device_status.pre_temp = 25;
  205. }
  206. }
  207. }
  208. else {
  209. RV = 10000 * (4096 - Read_Ntc_Voltage()) / Read_Ntc_Voltage();
  210. if(TempCalculate(RV, 3950, 10000)-TempCalculate(RV, 3950, 10000)>10)
  211. {
  212. LOG_E("temp value err!!!!");
  213. }
  214. else {
  215. device_status.heattem = TempCalculate(RV, 3950, 10000);
  216. if(device_status.heattem>160)
  217. {
  218. device_status.heattem = 25;
  219. }
  220. }
  221. //雨控
  222. if(Read_Rain_Voltage()>100)
  223. {
  224. device_status.rain_status = 1;
  225. ctr_lamp(OFF,1);
  226. ctr_lamp(OFF,2);
  227. ctr_lamp(OFF,3);
  228. //关闭上仓门?
  229. rain_cnt = 0;
  230. }
  231. else {
  232. rain_cnt++;
  233. //雨控消抖,1分钟
  234. LOG_D("rain_cnt:%d-value:%d",rain_cnt,Read_Rain_Voltage());
  235. if(rain_cnt==60&&Read_Rain_Voltage()<100)
  236. {
  237. rain_cnt = 0;
  238. device_status.rain_status = 0;
  239. }
  240. }
  241. }
  242. #if ROBOT_EN == 0
  243. rt_sprintf(device_status.voltage,"%d.%02d",filter_voltage()/100,filter_voltage()%100);
  244. //LOG_D("voltage is:%sV",device_status.voltage);
  245. sprintf(device_status.fuse_voltage, "%d.%02d",Read_Fuse_Voltage()/100,Read_Fuse_Voltage()%100);
  246. #endif
  247. rt_thread_mdelay(10);
  248. }
  249. }
  250. /*读取电流值*/
  251. int Read_Real_Current(void)
  252. {
  253. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  254. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  255. if (adc_dev == RT_NULL)
  256. {
  257. LOG_E("adc sample run failed! can't find %s device!", ADC_DEV_NAME);
  258. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  259. }
  260. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  261. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_1);
  262. //rt_adc_enable(adc_dev, 17);
  263. adc1_value = filter_Mid(rt_adc_read(adc_dev, ADC_DEV_CHANNLE_1));
  264. in_adc_17 = filter_Mid(rt_adc_read(adc_dev, 17));
  265. /* 关闭通道 */
  266. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_1);
  267. //rt_adc_disable(adc_dev, 17);
  268. //rt_kprintf("read in adc:ADC17:%d-ADC1:%d\n",in_adc_17,adc1_value);
  269. return (int)((ADC_VOL_BASE * (adc1_value*1.0)/(in_adc_17*1.0))*100000);
  270. }
  271. /*读取电压值*/
  272. int Read_Real_Voltage(void)
  273. {
  274. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  275. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  276. if (adc_dev == RT_NULL)
  277. {
  278. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  279. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  280. }
  281. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  282. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_0);
  283. adc0_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_0);
  284. /* 关闭通道 */
  285. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_0);
  286. return (adc0_value * REFER_VOLTAGE/CONVERT_BITS*12+20);
  287. }
  288. /*读取保险丝的电压值*/
  289. int Read_Fuse_Voltage(void)
  290. {
  291. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  292. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  293. if (adc_dev == RT_NULL)
  294. {
  295. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  296. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  297. }
  298. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  299. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_5);
  300. adc13_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_5);
  301. /* 关闭通道 */
  302. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_5);
  303. return (adc13_value * REFER_VOLTAGE/CONVERT_BITS*12+8);
  304. }
  305. /*读取雨控*/
  306. int Read_Rain_Voltage(void)
  307. {
  308. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  309. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  310. if (adc_dev == RT_NULL)
  311. {
  312. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  313. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  314. }
  315. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_3);
  316. adc11_light = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_3);
  317. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_3);
  318. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  319. //rt_kprintf("read rain value:%d\n",adc11_light);
  320. return adc11_light;
  321. // rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  322. // return rt_pin_read(RAIN_PIN);
  323. }
  324. /*读取光控*/
  325. int Read_Light_Voltage(void)
  326. {
  327. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  328. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  329. if (adc_dev == RT_NULL)
  330. {
  331. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  332. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  333. }
  334. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_2);
  335. adc10_rain = rt_adc_read(adc_dev,ADC_DEV_CHANNLE_2);
  336. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_2);
  337. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  338. //rt_kprintf("read light value:%d\n",adc10_rain);
  339. return adc10_rain;
  340. }
  341. //获取内部温度传感器值
  342. short Get_Temprate(void)
  343. {
  344. short result;
  345. double temperate;
  346. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  347. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  348. if (adc_dev == RT_NULL)
  349. {
  350. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  351. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  352. }
  353. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  354. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_6);
  355. adc16_value = rt_adc_read(adc_dev, ADC_DEV_CHANNLE_6);
  356. /* 关闭通道 */
  357. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_6);
  358. temperate = (float)adc16_value*(3.3/4096); //电压值
  359. temperate = (1.43-temperate)/0.0043+25; //转换为温度值
  360. result = temperate*=100; //扩大100倍.
  361. return result;
  362. }
  363. /**
  364. * Rv: 热敏电阻当前阻值
  365. * B: 热敏电阻参数B值
  366. * Revise: 校正温度
  367. * BasicRx: 热敏电阻25度时电阻(标称电阻数值)
  368. * 返回: 摄氏度
  369. */
  370. int TempCalculate(float Rv,int B,int BasicRx)
  371. {
  372. float Rx;
  373. Rx = Rv * 1.0 / BasicRx;
  374. Rx = log(Rx);
  375. Rx = Rx / B;
  376. Rx = 1/(Rx+0.003354);
  377. //Rx = Rx + 0.003356;
  378. //Rx = 1 / Rx;
  379. Rx = Rx - 273.13;
  380. return (int)(Rx+0.5);
  381. }
  382. /*
  383. * 中位值平均滤波法
  384. * */
  385. unsigned int filter_Mid(int adc_return)
  386. {
  387. unsigned char count,i,j;
  388. unsigned int value_buf[N],temp=0;
  389. int sum = 0;
  390. for(count = 0;count<N;count++)
  391. {
  392. value_buf[count] = adc_return;
  393. //rt_kprintf("value_buf:%d-%d\n",value_buf[count],count);
  394. rt_thread_delay(10);
  395. }
  396. for(j=0;j<N-1;j++)
  397. {
  398. for(i=0;i<N-j-1;i++)
  399. {
  400. if(value_buf[i]>value_buf[i+1])
  401. {
  402. temp = value_buf[i];
  403. value_buf[i] = value_buf[i+1];
  404. value_buf[i+1] = temp;
  405. }
  406. }
  407. }
  408. for(count = 1;count < N - 1;count++)
  409. {
  410. sum+= value_buf[count];
  411. }
  412. return (int)(sum/(N-2));
  413. }
  414. /*
  415. 限幅消抖滤波法
  416. A、方法:
  417. 相当于“限幅滤波法”+“消抖滤波法”
  418. 先限幅,后消抖
  419. B、优点:
  420. 继承了“限幅”和“消抖”的优点
  421. 改进了“消抖滤波法”中的某些缺陷,避免将干扰值导入系统
  422. C、缺点:
  423. 对于快速变化的参数不宜
  424. */
  425. unsigned int filter_voltage(void)
  426. {
  427. uint8_t data_cnt = 0;
  428. int value = 0,new_value = 0;
  429. new_value = Read_Real_Voltage();
  430. while(value!=new_value)
  431. {
  432. data_cnt++;
  433. if(data_cnt>=N) return new_value;
  434. rt_thread_delay(10);
  435. new_value = Read_Real_Voltage();
  436. }
  437. return value;
  438. }
  439. /*读取加热仓温度*/
  440. int Read_Ntc_Voltage(void)
  441. {
  442. rt_adc_device_t adc_dev; /*ADC设备句柄*/
  443. adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
  444. if (adc_dev == RT_NULL)
  445. {
  446. rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
  447. return RT_ERROR;// adc 采样值不可能为负值,返回负值表示出错
  448. }
  449. rt_adc_enable(adc_dev, ADC_DEV_CHANNLE_4);
  450. adc12_ntc = filter_Mid(rt_adc_read(adc_dev,ADC_DEV_CHANNLE_4));
  451. //LOG_D("Read_Ntc_Voltage:%d",adc12_ntc);
  452. rt_adc_disable(adc_dev, ADC_DEV_CHANNLE_4);
  453. HAL_ADCEx_Calibration_Start(adc_dev->parent.user_data);
  454. return adc12_ntc;
  455. }
  456. /* 创建线程 */
  457. int sht30_collect(void)
  458. {
  459. /* 创建线程*/
  460. tid1 = rt_thread_create("sht30_collect_thread",sht30_collect_thread_entry, RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY, THREAD_TIMESLICE);
  461. tid2 = rt_thread_create("Power Check thread", Power_ReadCheck, RT_NULL, P_THREAD_STACK_SIZE, P_THREAD_PRIORITY, P_THREAD_TIMESCLICE);
  462. /* 如果获得线程控制块,启动这个线程 */
  463. if (tid1 != RT_NULL)
  464. rt_thread_startup(tid1);
  465. if (tid2 != RT_NULL)
  466. rt_thread_startup(tid2);
  467. return 0;
  468. }
  469. //控制台打印传感器信息
  470. void Msh_DisplaySensor(void)
  471. {
  472. rt_kprintf("温度:%d\n",tem_value);
  473. rt_kprintf("湿度:%d\n",hum_value);
  474. rt_kprintf("电压:%s\n",device_status.voltage);
  475. rt_kprintf("电流:%s\n",device_status.power);
  476. }
  477. MSH_CMD_EXPORT(Msh_DisplaySensor,Msh_DisplaySensor);
  478. MSH_CMD_EXPORT(Read_Rain_Voltage,Read_Rain_Voltage);
  479. MSH_CMD_EXPORT(Read_Light_Voltage,Read_Light_Voltage);