control.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. #include <control.h>
  2. /*
  3. * Copyright (c) 2006-2020, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-05-30 Administrator the first version
  10. */
  11. #include "beep.h"
  12. #include "hmi.h"
  13. #include "easyflash.h"
  14. #include "stdio.h"
  15. #include "sound.h"
  16. #include "dtu_uart2.h"
  17. #include "sht30_collect.h"
  18. #include <user_mb_app.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 WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  26. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  27. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  28. //工作流程
  29. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  30. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  31. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  32. #define PWM_EN 1 //0关闭,1-打开
  33. //设备状态表结构体
  34. Device_Status_typedef device_status;
  35. rt_thread_t tid_workloop = RT_NULL;
  36. rt_thread_t tid_wkstc = RT_NULL;
  37. rt_thread_t tid_ctr = RT_NULL;
  38. rt_thread_t tid_heat = RT_NULL;
  39. rt_thread_t tid_485_play = RT_NULL;
  40. rt_event_t event_485_play;
  41. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  42. /***************************************************************************/
  43. int thread_delete_workloop(void)
  44. {
  45. rt_err_t result;
  46. if(tid_workloop!=RT_NULL)
  47. {
  48. result = rt_thread_delete(tid_workloop);
  49. }
  50. return result;
  51. }
  52. int thread_restart_workloop(void)
  53. {
  54. rt_err_t result;
  55. result = Work_Loop_Task();
  56. return result;
  57. }
  58. /**************************1.0定制2000W*************************************/
  59. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  60. {
  61. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  62. if(status_ws==ON)
  63. {
  64. rt_pin_write(FAN_P,ON);
  65. }
  66. else {
  67. rt_pin_write(FAN_P, OFF);
  68. }
  69. return 0;
  70. }
  71. uint8_t ctr_Fan_IN(uint8_t status_ws)
  72. {
  73. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  74. if(status_ws==ON)
  75. {
  76. rt_pin_write(FAN_I,ON);
  77. }
  78. else {
  79. rt_pin_write(FAN_I, OFF);
  80. }
  81. return 0;
  82. }
  83. /****************************5.0***********************************/
  84. void ctr_turnv5(void)
  85. {
  86. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  87. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  88. // 转出
  89. rt_pin_write(CLEAR_R,ON);
  90. rt_pin_write(CLEAR_L,OFF);
  91. rt_thread_mdelay(300);
  92. //停止
  93. rt_pin_write(CLEAR_R,OFF);
  94. rt_pin_write(CLEAR_L,OFF);
  95. rt_thread_mdelay(300);
  96. //转入
  97. rt_pin_write(CLEAR_R,OFF);
  98. rt_pin_write(CLEAR_L,ON);
  99. rt_thread_mdelay(300);
  100. //停止
  101. rt_pin_write(CLEAR_R,OFF);
  102. rt_pin_write(CLEAR_L,OFF);
  103. rt_thread_mdelay(300);
  104. }
  105. /**************************机器人********************************/
  106. void ctr_Motor_Robot(uint8_t status_ws)
  107. {
  108. unsigned int close_timeout = 0;
  109. unsigned int open_timeout = 0;
  110. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  111. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  112. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  113. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  114. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  115. rt_pin_write(Motor_Power,OFF);
  116. rt_pin_write(Motor_Direction_Ctr,ON);
  117. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  118. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  119. if(status_ws == ON)
  120. {
  121. //检测按键状态
  122. while(rt_pin_read(Motor_Direction_Button)==1)
  123. {
  124. if(rt_pin_read(LIMIT_CLOSE)==1)
  125. {
  126. rt_pin_write(Motor_Direction_Ctr,ON);
  127. close_timeout++;
  128. //打开电机驱动器电源,默认关闭
  129. rt_pin_write(Motor_Power,ON);
  130. if(close_timeout==2)
  131. {
  132. rt_pin_write(CLEAR_R,ON);
  133. rt_pin_write(CLEAR_L,OFF);
  134. rt_pin_write(Motor_Power,OFF);
  135. rt_thread_delay(20);
  136. rt_pin_write(Motor_Power,ON);
  137. }
  138. rt_thread_delay(20);
  139. if(close_timeout>500)
  140. {
  141. close_timeout = 600;
  142. rt_pin_write(Motor_Power,OFF);
  143. rt_pin_write(CLEAR_R,OFF);
  144. rt_pin_write(CLEAR_L,OFF);
  145. }
  146. }
  147. else
  148. {
  149. close_timeout = 0;
  150. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  151. rt_pin_write(CLEAR_R,OFF);
  152. rt_pin_write(CLEAR_L,OFF);
  153. }
  154. rt_thread_delay(20);
  155. }
  156. rt_pin_write(Motor_Power,OFF);
  157. rt_thread_delay(1000*2);
  158. while(rt_pin_read(Motor_Direction_Button)==0)
  159. {
  160. if(rt_pin_read(LIMIT_OPEN)==1)
  161. {
  162. open_timeout ++;
  163. //打开电机驱动器电源,默认关闭
  164. if(open_timeout==2){
  165. rt_pin_write(CLEAR_R,OFF);
  166. rt_pin_write(CLEAR_L,ON);
  167. }
  168. rt_pin_write(Motor_Power,ON);
  169. rt_pin_write(Motor_Direction_Ctr,OFF);
  170. if(open_timeout>500)
  171. {
  172. open_timeout = 600;
  173. rt_pin_write(Motor_Power,OFF);
  174. rt_pin_write(CLEAR_R,OFF);
  175. rt_pin_write(CLEAR_L,OFF);
  176. }
  177. rt_thread_delay(20);
  178. }
  179. if(rt_pin_read(LIMIT_OPEN)==0)
  180. {
  181. open_timeout = 0;
  182. rt_pin_write(Motor_Power,OFF);
  183. rt_pin_write(CLEAR_R,OFF);
  184. rt_pin_write(CLEAR_L,OFF);
  185. }
  186. rt_thread_delay(20);
  187. close_timeout = 0;
  188. }
  189. open_timeout = 0;
  190. close_timeout = 0;
  191. }
  192. else {
  193. rt_pin_write(Motor_Power,OFF);
  194. }
  195. }
  196. /*************************吸虫塔*********************************/
  197. //吸虫电机
  198. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  199. {
  200. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  201. if(status_ws==ON)
  202. {
  203. rt_pin_write(MOTOR1_CTR_PIN,ON);
  204. }
  205. else {
  206. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  207. }
  208. return 0;
  209. }
  210. uint8_t ctr_Camera_Power(uint8_t status_ws)
  211. {
  212. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  213. if(status_ws==ON)
  214. {
  215. rt_pin_write(MOTOR1_CTR_PIN,ON);
  216. }
  217. else {
  218. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  219. }
  220. return 0;
  221. }
  222. //吸附电机
  223. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  224. {
  225. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  226. if(status_ws==ON)
  227. {
  228. rt_pin_write(MOTOR2_CTR_PIN,ON);
  229. }
  230. else {
  231. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  232. }
  233. return 0;
  234. }
  235. //清扫-卷带电机
  236. void ctr_JD_Clear(uint8_t shake_sw)
  237. {
  238. rt_uint16_t turn_timeout = 0;
  239. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  240. ctr_cangmen_up(OFF); //清扫传送带的电机
  241. if(shake_sw==ON)
  242. {
  243. ctr_Heat(ON); //清洁风机
  244. Motor_shake(ON, 0);
  245. ctr_cangmen_up(OPEN); //打开清扫
  246. }
  247. else {
  248. Motor_shake(OFF, 0);
  249. ctr_cangmen_up(OPEN); //打开清扫
  250. }
  251. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  252. {
  253. Motor_Juandai(ON);
  254. rt_thread_delay(50);
  255. turn_timeout++;
  256. if(turn_timeout==1000) break;
  257. }
  258. turn_timeout = 0;
  259. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  260. {
  261. Motor_Juandai(ON);
  262. turn_timeout++;
  263. rt_thread_delay(50);
  264. if(turn_timeout==1000) break;
  265. }
  266. Motor_shake(OFF, 1);
  267. Motor_Juandai(OFF);
  268. ctr_cangmen_up(OFF); //关闭清扫
  269. ctr_Heat(OFF);
  270. }
  271. /**********************************************************************/
  272. /*光控*/
  273. uint8_t In_Light_status(void)
  274. {
  275. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  276. return rt_pin_read(LIGHT_PIN);
  277. }
  278. /*雨控*/
  279. uint8_t In_Rain_status(void)
  280. {
  281. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  282. return rt_pin_read(RAIN_PIN);
  283. }
  284. /*加热*/
  285. uint8_t ctr_Heat(uint8_t status_heat)
  286. {
  287. device_status.heat_status = status_heat;
  288. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  289. rt_pin_write(Heat_Pin,status_heat);
  290. return status_heat;
  291. }
  292. /*制冷*/
  293. uint8_t ctr_Cold(uint8_t status_cold)
  294. {
  295. device_status.cold_status = status_cold;
  296. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  297. rt_pin_write(Cold_Pin,status_cold);
  298. return status_cold;
  299. }
  300. /*相机供电*/
  301. void ctr_photo(uint8_t status_photo)
  302. {
  303. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  304. rt_pin_write(PHOTO,status_photo);
  305. }
  306. /*************************************************
  307. Function: ctr_Cool_Fan
  308. Description: 散热风机的开关
  309. Calls:
  310. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  311. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  312. Input: ON-开,OFF-关
  313. Output: NULL
  314. Return: ON-开,OFF-关
  315. Others: NULL
  316. *************************************************/
  317. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  318. {
  319. device_status.cool_fan_status = status_cool;
  320. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  321. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  322. if(status_cool==OFF)
  323. {
  324. rt_pin_write(Cool_Fan_L,OFF);
  325. rt_pin_write(Cool_Fan_R,OFF);
  326. }
  327. if(status_cool==ON)
  328. {
  329. rt_pin_write(Cool_Fan_L,ON);
  330. rt_pin_write(Cool_Fan_R,OFF);
  331. }
  332. return device_status.cool_fan_status;
  333. }
  334. /*************************************************
  335. Function: ctr_Collect_Fan
  336. Description: 采集风机的开关
  337. Calls:
  338. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  339. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  340. Input: ON-开,OFF-关
  341. Output: NULL
  342. Return: ON-开,OFF-关
  343. Others: NULL
  344. *************************************************/
  345. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  346. {
  347. device_status.collect_fan_status = status_collect;
  348. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  349. if(status_collect==OFF)
  350. {
  351. rt_pin_write(Collect_Fan,OFF);
  352. }
  353. if(status_collect==ON)
  354. {
  355. rt_pin_write(Collect_Fan,ON);
  356. }
  357. return device_status.collect_fan_status;
  358. }
  359. /*************************************************
  360. Function: ctr_develop
  361. Description: 培养载玻片孢子
  362. Calls:
  363. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  364. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  365. Input: v=0(复位) v=90转90度
  366. Output: NULL
  367. Return: NULL
  368. Others: NULL
  369. *************************************************/
  370. /*上仓门*/
  371. uint8_t ctr_cangmen_up(uint8_t status_up)
  372. {
  373. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  374. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  375. if(status_up==OFF)
  376. {
  377. rt_pin_write(Collect_fan_R,ON);
  378. rt_pin_write(Collect_fan_L,ON);
  379. device_status.ctr_up_motor = 0;
  380. rt_thread_delay(1000);
  381. }
  382. if(status_up==OPEN)
  383. {
  384. device_status.up_status = 1;
  385. rt_pin_write(Collect_fan_R,OFF);
  386. rt_pin_write(Collect_fan_L,OFF);
  387. rt_pin_write(Collect_fan_R,ON);
  388. rt_pin_write(Collect_fan_L,OFF);
  389. }
  390. if(status_up==CLOSE)
  391. {
  392. device_status.up_status = 0;
  393. rt_pin_write(Collect_fan_R,OFF);
  394. rt_pin_write(Collect_fan_L,OFF);
  395. rt_pin_write(Collect_fan_R,OFF);
  396. rt_pin_write(Collect_fan_L,ON);
  397. }
  398. return device_status.up_status;
  399. }
  400. //下仓门
  401. uint8_t ctr_cangmen_down(uint8_t status_down)
  402. {
  403. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  404. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  405. if(status_down==OFF)
  406. {
  407. rt_pin_write(C_DOWN_R,ON);
  408. rt_pin_write(C_DOWN_L,ON);
  409. device_status.ctr_down_motor = 0;
  410. rt_thread_delay(1000);
  411. }
  412. if(status_down==OPEN)
  413. {
  414. device_status.down_status = 1;
  415. rt_pin_write(C_DOWN_R,OFF);
  416. rt_pin_write(C_DOWN_L,OFF);
  417. rt_pin_write(C_DOWN_R,ON);
  418. rt_pin_write(C_DOWN_L,OFF);
  419. }
  420. if(status_down==CLOSE)
  421. {
  422. device_status.down_status = 0;
  423. rt_pin_write(C_DOWN_R,OFF);
  424. rt_pin_write(C_DOWN_L,OFF);
  425. rt_pin_write(C_DOWN_R,OFF);
  426. rt_pin_write(C_DOWN_L,ON);
  427. }
  428. return device_status.down_status;
  429. }
  430. //镇流器
  431. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  432. {
  433. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  434. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  435. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  436. if(status_lamp==ON)
  437. {
  438. switch(lamp_num){
  439. case 1:
  440. rt_pin_write(LAMP_SW,ON);
  441. device_status.lamp_status = status_lamp;
  442. break;
  443. case 2:
  444. rt_pin_write(LAMP_1_SW,ON);
  445. device_status.lamp_status = status_lamp;
  446. break;
  447. case 3:
  448. rt_pin_write(LAMP_2_SW,ON);
  449. device_status.lamp_status = status_lamp;
  450. break;
  451. default:
  452. break;
  453. }
  454. }
  455. else {
  456. switch(lamp_num){
  457. case 1:
  458. rt_pin_write(LAMP_SW,OFF);
  459. device_status.lamp_status = status_lamp;
  460. break;
  461. case 2:
  462. rt_pin_write(LAMP_1_SW,OFF);
  463. device_status.lamp1_status = status_lamp;
  464. break;
  465. case 3:
  466. rt_pin_write(LAMP_2_SW,OFF);
  467. device_status.lamp2_status = status_lamp;
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  474. return device_status.lamp_status;
  475. }
  476. //补光
  477. uint8_t ctr_led(uint8_t status_led)
  478. {
  479. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  480. if(device_status.version_type==6)
  481. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  482. if(status_led==ON)
  483. {
  484. rt_pin_write(LED_SW,ON);
  485. if(device_status.version_type==6)
  486. rt_pin_write(LED_P,ON);
  487. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  488. }
  489. else if(status_led==OFF){
  490. rt_pin_write(LED_SW,OFF);
  491. if(device_status.version_type==6)
  492. rt_pin_write(LED_P,OFF);
  493. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  494. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  495. }
  496. return status_led;
  497. }
  498. /*
  499. *
  500. * */
  501. void ctr_Motor_Driver_Power(uint8_t status_ws)
  502. {
  503. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  504. rt_pin_write(SHAKE_SW, status_ws);
  505. }
  506. /*************************************************
  507. Function: ctr_Motor_Driver
  508. Description: 步进电机驱动
  509. Calls:
  510. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  511. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  512. Input: 50000000
  513. Output: NULL
  514. Return: shake_sec
  515. Others: NULL
  516. *************************************************/
  517. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  518. {
  519. beep_init();
  520. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  521. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  522. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  523. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  524. if(EN==0)
  525. {
  526. rt_pin_write(Motor_Power,0);
  527. }
  528. else{
  529. rt_pin_write(Motor_EN, EN);
  530. }
  531. rt_pin_write(Motor_DIR, DIR);
  532. beep_set(pwm, pwm/2); //200kzh - 50%
  533. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  534. {
  535. if(time_out==0)
  536. {
  537. break;
  538. }
  539. rt_thread_delay(50);
  540. time_out --;
  541. }
  542. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  543. {
  544. if(time_out==0)
  545. {
  546. break;
  547. }
  548. rt_thread_delay(50);
  549. time_out --;
  550. }
  551. //rt_pin_write(Motor_EN, OFF);
  552. //if(device_status.autotakepic==0)
  553. //ctr_lamp(OFF, 1);
  554. beep_set(0, 0); //200kzh - 50%
  555. rt_thread_delay(50);
  556. }
  557. /*************************************************
  558. Function: ctr_Petroleum_jelly
  559. Description: 涂凡士林
  560. Calls:
  561. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  562. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  563. Input: NULL
  564. Output: NULL
  565. Return: NULL
  566. Others: NULL
  567. *************************************************/
  568. void ctr_Petroleum_jelly(rt_uint32_t delay)
  569. {
  570. beep_init();
  571. pwm_on(2,50000);
  572. rt_thread_delay(delay*1000);
  573. pwm_off(2,0);
  574. rt_thread_delay(1000);
  575. pwm_on(3, 50000);
  576. rt_thread_delay(delay*1000);
  577. pwm_off(3,0);
  578. device_status.fsl_sw = 0;
  579. }
  580. //1.0转仓
  581. void turnzp(void)
  582. {
  583. static uint16_t turn_timeout = 0;
  584. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  585. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  586. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  587. // rt_pin_write(CLEAR_R,OFF);
  588. // rt_pin_write(CLEAR_L,OFF);
  589. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  590. {
  591. rt_thread_delay(20);
  592. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  593. {
  594. // rt_pin_write(CLEAR_R,ON);
  595. // rt_pin_write(CLEAR_L,OFF);
  596. Motor_Juandai(ON);
  597. rt_thread_delay(50);
  598. turn_timeout++;
  599. if(turn_timeout==600)
  600. {
  601. LOG_E("检测不到限位,电机强制停止");
  602. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  603. DTU_Write("%s",ctr_zp_limit_warn);
  604. rt_free(ctr_zp_limit_warn);
  605. break;
  606. }
  607. }
  608. break;
  609. }
  610. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  611. {
  612. rt_thread_delay(20);
  613. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  614. {
  615. // rt_pin_write(CLEAR_R,ON);
  616. // rt_pin_write(CLEAR_L,OFF);
  617. Motor_Juandai(ON);
  618. rt_thread_delay(50);
  619. turn_timeout++;
  620. if(turn_timeout==600)
  621. {
  622. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  623. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  624. DTU_Write("%s",ctr_zp_limit_warn);
  625. rt_free(ctr_zp_limit_warn);
  626. break;
  627. }
  628. }
  629. break;
  630. }
  631. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  632. {
  633. rt_thread_delay(20);
  634. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  635. {
  636. // rt_pin_write(CLEAR_R,OFF);
  637. // rt_pin_write(CLEAR_L,OFF);
  638. Motor_Juandai(OFF);
  639. break;
  640. }
  641. else {
  642. break;
  643. }
  644. }
  645. Motor_Juandai(OFF);
  646. // rt_pin_write(CLEAR_R,OFF);
  647. // rt_pin_write(CLEAR_L,OFF);
  648. turn_timeout = 0;
  649. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  650. HMI_WRITE("sysTest.switchClear.val=%d",0);
  651. rt_thread_delay(1000);
  652. }
  653. //清扫-常闭型限位开关
  654. //新结构使用推杆电机做清扫
  655. uint8_t ctr_clear(uint16_t status_clear)
  656. {
  657. static uint16_t clear_num = 0;
  658. #if PWM_EN == 0
  659. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  660. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  661. #endif
  662. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  663. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  664. /*************************************************************外接清扫
  665. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  666. rt_pin_write(POWER_CLEAR,ON);
  667. rt_thread_delay(1000*100);
  668. rt_pin_write(POWER_CLEAR,OFF);
  669. ************************************************************/
  670. //停止
  671. #if PWM_EN == 0
  672. rt_pin_write(CLEAR_R,OFF);
  673. rt_pin_write(CLEAR_L,OFF);
  674. #else
  675. Motor_Qingsao_R_PWM(OFF);
  676. Motor_Qingsao_L_PWM(OFF);
  677. #endif
  678. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  679. rt_thread_delay(100);
  680. //如果清扫位置在中间
  681. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  682. {
  683. #if PWM_EN == 0
  684. rt_pin_write(CLEAR_R,ON);
  685. rt_pin_write(CLEAR_L,OFF);
  686. #else
  687. Motor_Qingsao_R_PWM(ON);
  688. Motor_Qingsao_L_PWM(OFF);
  689. #endif
  690. rt_thread_delay(50);
  691. clear_num++;
  692. //已到达检测位置
  693. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  694. {
  695. #if PWM_EN == 0
  696. rt_pin_write(CLEAR_R,ON);
  697. rt_pin_write(CLEAR_L,ON);
  698. #else
  699. Motor_Qingsao_R_PWM(OFF);
  700. Motor_Qingsao_L_PWM(OFF);
  701. #endif
  702. rt_thread_delay(2000);
  703. break;
  704. }
  705. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  706. {
  707. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  708. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  709. #if PWM_EN == 0
  710. rt_pin_write(CLEAR_R,ON);
  711. rt_pin_write(CLEAR_L,ON);
  712. #else
  713. Motor_Qingsao_R_PWM(OFF);
  714. Motor_Qingsao_L_PWM(OFF);
  715. #endif
  716. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  717. if(Read_Real_Voltage()/100<12)
  718. {
  719. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  720. HMI_WRITE("sysTest.switchClear.val=%d",0);
  721. device_status.clear_status = 0;
  722. return 0;
  723. }
  724. break;
  725. }
  726. }
  727. clear_num = 0;
  728. //如果清扫位置在左侧
  729. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  730. {
  731. #if PWM_EN == 0
  732. rt_pin_write(CLEAR_R,ON);
  733. rt_pin_write(CLEAR_L,OFF);
  734. #else
  735. Motor_Qingsao_R_PWM(ON);
  736. Motor_Qingsao_L_PWM(OFF);
  737. #endif
  738. rt_thread_delay(50);
  739. clear_num++;
  740. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  741. {
  742. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  743. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  744. #if PWM_EN == 0
  745. rt_pin_write(CLEAR_R,ON);
  746. rt_pin_write(CLEAR_L,ON);
  747. #else
  748. Motor_Qingsao_R_PWM(OFF);
  749. Motor_Qingsao_L_PWM(OFF);
  750. #endif
  751. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  752. if(Read_Real_Voltage()/100<12)
  753. {
  754. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  755. HMI_WRITE("sysTest.switchClear.val=%d",0);
  756. device_status.clear_status = 0;
  757. return 0;
  758. }
  759. break;
  760. }
  761. }
  762. clear_num = 0;
  763. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  764. {
  765. #if PWM_EN == 0
  766. rt_pin_write(CLEAR_R,ON);
  767. rt_pin_write(CLEAR_L,ON);
  768. #else
  769. Motor_Qingsao_R_PWM(OFF);
  770. Motor_Qingsao_L_PWM(OFF);
  771. #endif
  772. if(Read_Real_Voltage()/100<12)
  773. {
  774. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  775. HMI_WRITE("sysTest.switchClear.val=%d",0);
  776. device_status.clear_status = 0;
  777. return 0;
  778. }
  779. rt_thread_delay(2000);
  780. break;
  781. }
  782. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  783. {
  784. #if PWM_EN == 0
  785. rt_pin_write(CLEAR_R,OFF);
  786. rt_pin_write(CLEAR_L,ON);
  787. #else
  788. Motor_Qingsao_R_PWM(OFF);
  789. Motor_Qingsao_L_PWM(ON);
  790. #endif
  791. rt_thread_delay(50);
  792. clear_num++;
  793. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  794. {
  795. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  796. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  797. //DTU_Write("%s",Clear_ERR);
  798. #if PWM_EN == 0
  799. rt_pin_write(CLEAR_R,ON);
  800. rt_pin_write(CLEAR_L,ON);
  801. #else
  802. Motor_Qingsao_R_PWM(OFF);
  803. Motor_Qingsao_L_PWM(OFF);
  804. #endif
  805. rt_free(Clear_ERR);
  806. if(Read_Real_Voltage()/100<12)
  807. {
  808. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  809. HMI_WRITE("sysTest.switchClear.val=%d",0);
  810. device_status.clear_status = 0;
  811. return 0;
  812. }
  813. break;
  814. }
  815. }
  816. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  817. {
  818. #if PWM_EN == 0
  819. rt_pin_write(CLEAR_R,ON);
  820. rt_pin_write(CLEAR_L,ON);
  821. #else
  822. Motor_Qingsao_R_PWM(OFF);
  823. Motor_Qingsao_L_PWM(OFF);
  824. #endif
  825. rt_thread_delay(2000);
  826. break;
  827. }
  828. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  829. {
  830. #if PWM_EN == 0
  831. rt_pin_write(CLEAR_R,ON);
  832. rt_pin_write(CLEAR_L,ON);
  833. #else
  834. Motor_Qingsao_R_PWM(OFF);
  835. Motor_Qingsao_L_PWM(OFF);
  836. #endif
  837. if(device_status.version_type==41)
  838. {
  839. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  840. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  841. //DTU_Write("%s",Clear_ERR);
  842. rt_free(Clear_ERR);
  843. }
  844. return 0;
  845. }
  846. clear_num = 0;
  847. rt_thread_delay(2000);
  848. #if PWM_EN == 0
  849. rt_pin_write(CLEAR_R,ON);
  850. rt_pin_write(CLEAR_L,ON);
  851. #else
  852. Motor_Qingsao_R_PWM(OFF);
  853. Motor_Qingsao_L_PWM(OFF);
  854. #endif
  855. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  856. HMI_WRITE("sysTest.switchClear.val=%d",0);
  857. device_status.clear_status = 0;
  858. return 1;
  859. }
  860. //路由器供电
  861. void ctr_router(uint8_t status_router)
  862. {
  863. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  864. if(device_status.version_type==6)
  865. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  866. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  867. if(status_router==ON)
  868. {
  869. rt_pin_write(ROUTER,ON);
  870. LOG_W("打开路由器电源!");
  871. }
  872. else {
  873. rt_pin_write(ROUTER,OFF);
  874. LOG_W("关闭路由器电源!");
  875. }
  876. }
  877. //清扫-推杆电机
  878. void ctr_TG_Clear(uint16_t clear_timeout)
  879. {
  880. #if 0
  881. int current_clear[200];
  882. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  883. LOG_I("清扫电机待机:%d",Electric_current);
  884. #endif
  885. static uint16_t clear_count = 0;
  886. clear_count = clear_timeout;
  887. #if PWM_EN == 0
  888. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  889. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  890. #endif
  891. while(clear_count!=0)
  892. {
  893. #if PWM_EN == 0
  894. rt_pin_write(CLEAR_R,ON);
  895. rt_pin_write(CLEAR_L,OFF);
  896. #else
  897. Motor_Qingsao_R_PWM(ON);
  898. Motor_Qingsao_L_PWM(OFF);
  899. #endif
  900. clear_count--;
  901. rt_thread_delay(100);
  902. }
  903. #if PWM_EN == 0
  904. rt_pin_write(CLEAR_R,ON);
  905. rt_pin_write(CLEAR_L,ON);
  906. #else
  907. Motor_Qingsao_R_PWM(OFF);
  908. Motor_Qingsao_L_PWM(OFF);
  909. #endif
  910. rt_thread_delay(3000);
  911. clear_count = 0;
  912. while(clear_timeout!=clear_count)
  913. {
  914. #if PWM_EN == 0
  915. rt_pin_write(CLEAR_R,OFF);
  916. rt_pin_write(CLEAR_L,ON);
  917. #else
  918. Motor_Qingsao_R_PWM(OFF);
  919. Motor_Qingsao_L_PWM(ON);
  920. #endif
  921. // if(clear_count<200)
  922. // current_clear[clear_count] = atoi(device_status.power);
  923. clear_count++;
  924. rt_thread_delay(100);
  925. }
  926. #if PWM_EN == 0
  927. rt_pin_write(CLEAR_R,ON);
  928. rt_pin_write(CLEAR_L,ON);
  929. #else
  930. Motor_Qingsao_R_PWM(OFF);
  931. Motor_Qingsao_L_PWM(OFF);
  932. #endif
  933. #if 0
  934. bubblesort(current_clear,200);
  935. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  936. {
  937. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  938. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  939. DTU_Write("%s",ctr_clear_warn);
  940. rt_free(ctr_clear_warn);
  941. }
  942. #endif
  943. rt_thread_delay(2000);
  944. device_status.clear_status = 0;
  945. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  946. HMI_WRITE("sysTest.switchClear.val=%d",0);
  947. }
  948. //DTU,RTU供电
  949. void ctr_RTU_DTU(uint8_t status_rtu)
  950. {
  951. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  952. if(status_rtu==ON)
  953. {
  954. LOG_W("RTU电源启动!!!");
  955. rt_pin_write(RTU_DTU,ON);
  956. }
  957. else
  958. {
  959. LOG_W("RTU电源关闭!!!");
  960. rt_pin_write(RTU_DTU,OFF);
  961. }
  962. }
  963. //海康拍照
  964. void Hk_Take_io(void)
  965. {
  966. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  967. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  968. rt_pin_write(HK_TAKE, 1);
  969. rt_pin_write(HK_IO, 0);
  970. if(device_status.dtype==3)
  971. Beep_Tip(1000);
  972. if(device_status.dtype==7)
  973. rt_thread_mdelay(1000);
  974. rt_pin_write(HK_TAKE, 0);
  975. rt_pin_write(HK_IO, 1);
  976. }
  977. void calorstat(uint16_t heat_time)
  978. {
  979. //uint8_t heat_up_cnt = 0;
  980. uint16_t n = 0;
  981. uint16_t delay_heat = 60;
  982. FlagStatus heat_flg = RESET;
  983. device_status.heat_time_min = 0;
  984. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  985. ctr_Heat(ON);
  986. // UpLoad_Data(); //开始加热时的第一条数据
  987. //恒温模式
  988. for(n=0;n < heat_time;n++)
  989. {
  990. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  991. {
  992. heat_flg = SET;
  993. ctr_Heat(OFF);
  994. while(delay_heat!=0)
  995. {
  996. delay_heat --;
  997. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  998. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  999. rt_thread_delay(1000);
  1000. if(device_status.heattem>device_status.run_sta + 1){
  1001. device_status.run_sta = device_status.heattem;
  1002. }
  1003. }
  1004. delay_heat = 60;
  1005. //跳出FOR循环
  1006. n = n + 60;
  1007. }
  1008. if(device_status.heattem>device_status.run_sta + 1){
  1009. device_status.run_sta = device_status.heattem;
  1010. }
  1011. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1012. {
  1013. //device_status.run_sta = device_status.heattem;
  1014. ctr_Heat(ON);
  1015. }
  1016. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1017. {
  1018. // if(heat_up_cnt==0)
  1019. // {
  1020. // UpLoad_Data(); //加热到设定温度的数据
  1021. // heat_up_cnt = 1;
  1022. // }
  1023. //device_status.run_sta = device_status.heattem;
  1024. ctr_Heat(OFF);
  1025. }
  1026. if((n % 60)==0)
  1027. {
  1028. device_status.heat_time_min = device_status.heat_time_min + 1;
  1029. // if(device_status.heat_time_min % 5 == 0)
  1030. // {
  1031. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1032. // }
  1033. }
  1034. //如果触发异常退出加热
  1035. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1036. {
  1037. rt_thread_delay(1000);
  1038. if(device_status.bat_status == 1)
  1039. {
  1040. rt_kprintf("============================触发异常退出加热==========================");
  1041. ctr_lamp(OFF,1);
  1042. ctr_Heat(OFF);
  1043. break;
  1044. }
  1045. }
  1046. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1047. {
  1048. ctr_Heat(OFF);
  1049. rt_kprintf("异常退出!!!\n");
  1050. break;
  1051. }
  1052. rt_thread_delay(1000);
  1053. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1054. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1055. }
  1056. ctr_Heat(OFF);
  1057. //UpLoad_Data(); //加热结束的数据
  1058. }
  1059. //初始化所有控制驱动,关闭
  1060. void HardWare_Init(uint8_t boot)
  1061. {
  1062. Beep_Tip(200);
  1063. //关闭上仓门
  1064. ctr_cangmen_up(OFF);
  1065. if(device_status.dtype==3){
  1066. //关闭下仓门电机
  1067. ctr_cangmen_down(OFF);
  1068. }
  1069. ctr_lamp(OFF,1);
  1070. //关闭加热
  1071. ctr_Heat(OFF);
  1072. //关闭补光灯
  1073. ctr_led(OFF);
  1074. //关闭12V供电(海康相机,RTU)
  1075. ctr_photo(OFF);
  1076. //关闭清扫电机驱动
  1077. ctr_clear(OFF);
  1078. }
  1079. //拍照
  1080. void HK_TakePhoto(void)
  1081. {
  1082. //打开补光灯
  1083. DTU_Write("takephoto.mp3");
  1084. rt_thread_delay(10000);
  1085. char *takephotojson = makeJson_takephoto();
  1086. DTU_Write("%s",takephotojson);
  1087. rt_free(takephotojson);
  1088. //吸虫塔
  1089. if(device_status.version_type!=6)
  1090. {
  1091. ctr_led(ON);
  1092. if(device_status.version_type==48)
  1093. {
  1094. rt_thread_delay(1000*60);
  1095. }
  1096. else {
  1097. rt_thread_delay(1000*15);
  1098. }
  1099. }
  1100. //触发海康相机拍照
  1101. Hk_Take_io();
  1102. if(device_status.version_type!=6)
  1103. {
  1104. rt_thread_delay(1000*45);
  1105. }
  1106. else {
  1107. ctr_led(ON);
  1108. rt_thread_delay(1000*19);
  1109. }
  1110. ctr_led(OFF);
  1111. }
  1112. //光控测试任务
  1113. void SunTest(void)
  1114. {
  1115. rt_uint16_t ui_timeout_l = 0;
  1116. //未检测到光照,一秒扫描等待,超时30S
  1117. while(Read_Light_Voltage()<500)
  1118. {
  1119. ui_timeout_l ++;
  1120. rt_thread_delay(1000);
  1121. if(ui_timeout_l==30) break;
  1122. }
  1123. rt_thread_delay(3000);
  1124. if(Read_Light_Voltage()<500)
  1125. {
  1126. device_status.sun_status = 0;//光控状态
  1127. }
  1128. if(Read_Light_Voltage()>500)
  1129. {
  1130. rt_thread_delay(1000);
  1131. }
  1132. rt_thread_delay(3000);
  1133. if(Read_Light_Voltage()>500)
  1134. {
  1135. device_status.sun_status = 1;
  1136. ctr_lamp(OFF,1);
  1137. }
  1138. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1139. device_status.sun_test = 0;
  1140. device_status.work_status=0;
  1141. }
  1142. /*上电初始化*/
  1143. void ResetInit(void)
  1144. {
  1145. LOG_I("ResetInit...");
  1146. //1.打开路由器供电
  1147. ctr_router(ON);
  1148. //2. 打开RTU供电
  1149. ctr_RTU_DTU(ON);
  1150. //3.打开光栅供电
  1151. ctr_photo(ON);
  1152. //5.关闭加热
  1153. //6.关闭补光灯
  1154. ctr_led(OFF);
  1155. }
  1156. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1157. void HardWare_Check(uint8_t boot)
  1158. {
  1159. if(boot==0&&device_status.version_type!=6)
  1160. {
  1161. //停止所有动作 -完成
  1162. HardWare_Init(boot);
  1163. }
  1164. else {
  1165. //停止所有动作
  1166. //读取记录待机功耗(实测大概70mA)
  1167. //(需减去屏幕的功耗大概120mA)
  1168. //动作上仓门,读取功率值保存
  1169. //动作下仓门,读取功率值保存
  1170. //动作清扫,读取功率值保存
  1171. //打开镇流器,读取功率值保存
  1172. //打开海康相机供电,读取功率值保存
  1173. //打开补光灯,读取功率值保存
  1174. //打开路由器/RTU供电,读取功率值保存
  1175. //打开加热,读取功率值保存
  1176. }
  1177. }
  1178. void WorkCheck(void)
  1179. {
  1180. static rt_uint32_t light_timeout = 3600;
  1181. while(device_status.ds == 1 &&
  1182. device_status.rain_status == 0 &&
  1183. device_status.temp_status == 0 &&
  1184. device_status.bat_status == 0)
  1185. {
  1186. if(device_status.timeSwitch == 1) // 时控模式
  1187. {
  1188. LOG_I("***********时控*****************");
  1189. if(device_status.startBar == device_status.stopBar)
  1190. {
  1191. // 开始结束时间相同则24小时模式
  1192. device_status.work_status = 1;
  1193. rt_thread_delay(10000);
  1194. continue;
  1195. }
  1196. time_t now;
  1197. time(&now);
  1198. struct tm now_tm = *localtime(&now);
  1199. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1200. rt_uint32_t start_sec = device_status.startBar * 3600;
  1201. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1202. // 修复跨天判断逻辑
  1203. if (start_sec < end_sec)
  1204. {
  1205. // 不跨天模式
  1206. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1207. }
  1208. else
  1209. {
  1210. // 跨天模式
  1211. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1212. }
  1213. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1214. device_status.work_status ? "工作" : "待机",
  1215. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1216. device_status.startBar, device_status.stopBar);
  1217. rt_thread_delay(10000);
  1218. }
  1219. else // 光控模式
  1220. {
  1221. LOG_I("光控定时模式!");
  1222. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1223. {
  1224. device_status.light_over_tim--;
  1225. light_timeout = 3600; // 重置超时计数器
  1226. device_status.work_status = 1;
  1227. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1228. }
  1229. else
  1230. {
  1231. // 仅在白天递减超时计数器
  1232. if(device_status.sun_status == 1 && light_timeout > 0) {
  1233. light_timeout--;
  1234. }
  1235. device_status.work_status = 0;
  1236. //没有在测试并且没有在自检
  1237. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1238. {
  1239. ctr_lamp(OFF,1);
  1240. }
  1241. // 连续1小时白天后重置
  1242. if(light_timeout == 0) {
  1243. device_status.light_over_tim = 0;
  1244. light_timeout = 3600; // 重置防止负数
  1245. }
  1246. // 白天且倒计时结束:重置夜晚时长
  1247. if(device_status.light_over_tim == 0 &&
  1248. device_status.sun_status == 1)
  1249. {
  1250. device_status.light_over_tim = device_status.sunBar * 3600;
  1251. }
  1252. }
  1253. }
  1254. rt_thread_delay(1000);
  1255. }
  1256. // 异常状态处理
  1257. device_status.work_status = 0;
  1258. rt_thread_delay(1000);
  1259. }
  1260. //拍照性诱工作任务流程
  1261. void WorkTask_XYV3(void)
  1262. {
  1263. static uint8_t gaoya_on = 0;
  1264. static uint8_t gaoya_on_sec = 0;
  1265. rt_uint32_t cal_n = device_status.cul_time * 60;
  1266. LOG_I("工作状态:%d",device_status.work_status);
  1267. LOG_I("开关机状态:%d",device_status.ds);
  1268. while(device_status.work_status==1&&
  1269. device_status.bat_status==0&&
  1270. device_status.ds==1&&
  1271. device_status.temp_status==0)
  1272. {
  1273. //高压板通电
  1274. ctr_lamp(ON, 1); //清扫-DC12v电机正反转
  1275. rt_thread_delay(10000);
  1276. ctr_lamp(OFF, 1); //清扫-DC12v电机正反转
  1277. //光栅传感器通电
  1278. ctr_photo(ON);
  1279. //拍照(?触发条件)计数?没有虫呢?定时
  1280. while(1){
  1281. ctr_router(ON);
  1282. ctr_RTU_DTU(ON);
  1283. cal_n--;
  1284. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  1285. if(cal_n==0){
  1286. ctr_lamp(OFF, 1); //拍照时关闭高压
  1287. //发送拍照计数指令
  1288. device_status.photo_ws = 2;
  1289. cal_n = device_status.cul_time * 60;
  1290. }
  1291. if(cal_n>1000&&cal_n%1200==0) //每隔20分钟高压通电5S
  1292. {
  1293. ctr_lamp(ON, 1);
  1294. gaoya_on = 1;
  1295. }
  1296. if(gaoya_on){
  1297. gaoya_on_sec++;
  1298. if(gaoya_on_sec>5){
  1299. gaoya_on=0;
  1300. gaoya_on_sec = 0;
  1301. ctr_lamp(OFF, 1);
  1302. }
  1303. }
  1304. if(device_status.temp_status==1 || device_status.bat_status==1 || device_status.work_status==0){
  1305. ctr_lamp(OFF, 1);
  1306. device_status.photo_ws = 2;
  1307. break;
  1308. }
  1309. rt_thread_delay(1000);
  1310. }
  1311. rt_thread_delay(10000);
  1312. }
  1313. ctr_lamp(OFF, 1);
  1314. ctr_photo(OFF);
  1315. }
  1316. /* 入口函数-工作状态检测 */
  1317. static void Work_status_thread_entry(void *parameter)
  1318. {
  1319. static uint16_t sun_status_num = 120;
  1320. //如果是光控定时,进行光控监测的消抖
  1321. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1322. {
  1323. rt_kprintf("光控定时!!!\n");
  1324. while(sun_status_num!=0)
  1325. {
  1326. sun_status_num--;
  1327. rt_thread_delay(1000);
  1328. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1329. }
  1330. device_status.light_over_tim = device_status.sunBar * 3600;
  1331. device_status.test_flag = 0;
  1332. }
  1333. while(1)
  1334. {
  1335. WorkCheck();
  1336. }
  1337. }
  1338. /*入口函数-工作流程*/
  1339. static void Work_Loop_thread_entry(void *parameter)
  1340. {
  1341. rt_thread_delay(1000);
  1342. ResetInit();
  1343. while(1)
  1344. {
  1345. WorkTask_XYV3();
  1346. rt_thread_delay(1000);
  1347. }
  1348. }
  1349. /*入口函数清扫*/
  1350. static void Control_Clear_thread_entry(void *parameter)
  1351. {
  1352. static int selfcheck_cnt = 0;
  1353. while(1)
  1354. {
  1355. if(device_status.selfcheck_ws==1){
  1356. selfcheck_cnt++;
  1357. }else{
  1358. selfcheck_cnt = 0;
  1359. }
  1360. if(selfcheck_cnt>1200) //超过3分钟
  1361. {
  1362. device_status.selfcheck_ws = 0;
  1363. device_status.camera_sta = 1; //相机异常
  1364. //上报自检结果
  1365. //上报自检结果
  1366. char *json_temp = makeJson_selfCheck_(1);
  1367. DTU_Write("%s",json_temp);
  1368. free(json_temp);
  1369. }
  1370. if(device_status.clear_status==10){
  1371. //ctr_turn(90,0);
  1372. Motor_shake(1,800);
  1373. device_status.clear_status = 0;
  1374. }
  1375. rt_thread_delay(100);
  1376. }
  1377. }
  1378. /*入口函数加热*/
  1379. static void Control_Heat_thread_entry(void *parameter)
  1380. {
  1381. rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1382. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1383. static int GaoyYa_cnt,GuangShan_cnt = 500; //10s
  1384. bool GaoYa = false;
  1385. bool GuangShan = false;
  1386. while(1)
  1387. {
  1388. if(rt_pin_read(SIGNAL_GaoYa)==0)
  1389. {
  1390. LOG_I("检测到高压计数!");
  1391. GaoYa = true;
  1392. }
  1393. if(rt_pin_read(SIGNAL_GuangShan)==0)
  1394. {
  1395. LOG_I("检测到光栅计数!");
  1396. GuangShan = true;
  1397. }
  1398. if(GaoYa)
  1399. {
  1400. GaoyYa_cnt --;
  1401. if(GaoyYa_cnt==0)
  1402. {
  1403. GaoyYa_cnt = 500;
  1404. GaoYa = false;
  1405. }
  1406. }
  1407. if(GuangShan)
  1408. {
  1409. GuangShan_cnt--;
  1410. if(GuangShan_cnt==0)
  1411. {
  1412. GuangShan_cnt = 500;
  1413. GuangShan = false;
  1414. }
  1415. }
  1416. if(GaoYa&&GuangShan)
  1417. {
  1418. device_status.all_ycct += 1; //计数值增加
  1419. //拍照
  1420. if(device_status.all_ycct>=1){
  1421. //device_status.photo_ws = 1;
  1422. }
  1423. GaoYa = false;
  1424. GuangShan = false;
  1425. GaoyYa_cnt = 500;
  1426. GuangShan_cnt = 500;
  1427. rt_thread_delay(1000);
  1428. }
  1429. rt_thread_delay(20);
  1430. }
  1431. }
  1432. //static void Control_Heat_thread_entry(void *parameter) {
  1433. // rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1434. // rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1435. //
  1436. // // 修正变量名和初始化
  1437. // static int GaoYa_cnt = 500, GuangShan_cnt = 500;
  1438. // static bool GaoYa = false, GuangShan = false;
  1439. // static int highPressureDuration = 0;
  1440. //
  1441. //
  1442. // while (1) {
  1443. // // 高压信号去抖动
  1444. // if (rt_pin_read(SIGNAL_GaoYa) == 0) {
  1445. // LOG_I("高压信号稳定触发");
  1446. // GaoYa = true;
  1447. // highPressureDuration ++;
  1448. // }else{
  1449. // highPressureDuration = 0;
  1450. // }
  1451. //
  1452. // // 光栅信号去抖动(类似逻辑)
  1453. // if(rt_pin_read(SIGNAL_GuangShan)==0)
  1454. // {
  1455. // LOG_I("检测到光栅计数!");
  1456. // GuangShan = true;
  1457. // }
  1458. // /* 高压持续时间监测 */
  1459. // if(GaoYa)
  1460. // {
  1461. // // 持续时间累计(每次循环+1,20ms/次)
  1462. // if(highPressureDuration >= 50)
  1463. // {
  1464. // LOG_E("高压持续超过5秒!");
  1465. // // 执行关闭高压操作(示例函数)
  1466. // //ctr_lamp(OFF, 1);
  1467. // // 可选:自动复位保护
  1468. //// rt_thread_mdelay(10000); // 等待10秒
  1469. //// ctr_lamp(ON, 1);
  1470. // highPressureDuration = 0;
  1471. // }
  1472. // }
  1473. // // 计数器逻辑
  1474. // if (GaoYa) {
  1475. // if (--GaoYa_cnt <= 0) {
  1476. // GaoYa = false;
  1477. // GaoYa_cnt = 500;
  1478. // }
  1479. // }
  1480. //
  1481. // if (GuangShan) {
  1482. // if (--GuangShan_cnt <= 0) {
  1483. // GuangShan = false;
  1484. // GuangShan_cnt = 500;
  1485. // }
  1486. // }
  1487. //
  1488. // // 触发拍照逻辑
  1489. // if (GaoYa && GuangShan) {
  1490. // device_status.all_ycct++;
  1491. // GaoYa = GuangShan = false;
  1492. // GaoYa_cnt = GuangShan_cnt = 500;
  1493. // }
  1494. // rt_thread_delay(20); // 保持20ms周期
  1495. // }
  1496. //}
  1497. /*清扫线程*/
  1498. int Control_clear(void)
  1499. {
  1500. /* 创建线程*/
  1501. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1502. Control_Clear_thread_entry, RT_NULL,
  1503. 2048, //2048
  1504. 24, //23
  1505. WORK_CHECK_THREAD_TIMESLICE); //5
  1506. if(tid_ctr!=RT_NULL)
  1507. {
  1508. rt_thread_startup(tid_ctr);
  1509. }
  1510. return 0;
  1511. }
  1512. /*恒温加热线程*/
  1513. int Control_heat(void)
  1514. {
  1515. /* 创建线程*/
  1516. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  1517. Control_Heat_thread_entry, RT_NULL,
  1518. 1024, //2048
  1519. 25, //23
  1520. WORK_CHECK_THREAD_TIMESLICE); //5
  1521. if(tid_heat!=RT_NULL)
  1522. {
  1523. rt_thread_startup(tid_heat);
  1524. }
  1525. return 0;
  1526. }
  1527. /* 创建线程 -工作状态检测*/
  1528. int Workstatus_check(void)
  1529. {
  1530. /* 创建线程*/
  1531. tid_wkstc = rt_thread_create("work_status_check_thread",
  1532. Work_status_thread_entry, RT_NULL,
  1533. WORK_CHECK_THREAD_STACK_SIZE, //1024
  1534. WORK_CHECK_THREAD_PRIORITY, //23
  1535. WORK_CHECK_THREAD_TIMESLICE); //5
  1536. if(tid_wkstc!=RT_NULL)
  1537. {
  1538. rt_thread_startup(tid_wkstc);
  1539. }
  1540. return 0;
  1541. }
  1542. /*工作流程线程*/
  1543. int Work_Loop_Task(void)
  1544. {
  1545. rt_err_t result_loop = 0;
  1546. /* 创建线程*/
  1547. tid_workloop = rt_thread_create("work_loop_thread",
  1548. Work_Loop_thread_entry, RT_NULL,
  1549. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1550. WORK_LOOP_THREAD_PRIORITY, //12
  1551. WORK_LOOP_THREAD_TIMESLICE); //5
  1552. if(tid_workloop!=RT_NULL)
  1553. {
  1554. result_loop = rt_thread_startup(tid_workloop);
  1555. }
  1556. return result_loop;
  1557. }
  1558. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  1559. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1560. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  1561. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1562. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  1563. //INIT_APP_EXPORT(Control_heat);