control.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. //关闭镇流器
  1070. if(device_status.autotakepic==0)
  1071. ctr_lamp(OFF,1);
  1072. //关闭加热
  1073. ctr_Heat(OFF);
  1074. //关闭补光灯
  1075. ctr_led(OFF);
  1076. //关闭12V供电(海康相机,RTU)
  1077. ctr_photo(OFF);
  1078. //关闭清扫电机驱动
  1079. ctr_clear(OFF);
  1080. }
  1081. //拍照
  1082. void HK_TakePhoto(void)
  1083. {
  1084. //打开补光灯
  1085. DTU_Write("takephoto.mp3");
  1086. rt_thread_delay(10000);
  1087. char *takephotojson = makeJson_takephoto();
  1088. DTU_Write("%s",takephotojson);
  1089. rt_free(takephotojson);
  1090. //吸虫塔
  1091. if(device_status.version_type!=6)
  1092. {
  1093. ctr_led(ON);
  1094. if(device_status.version_type==48)
  1095. {
  1096. rt_thread_delay(1000*60);
  1097. }
  1098. else {
  1099. rt_thread_delay(1000*15);
  1100. }
  1101. }
  1102. //触发海康相机拍照
  1103. Hk_Take_io();
  1104. if(device_status.version_type!=6)
  1105. {
  1106. rt_thread_delay(1000*45);
  1107. }
  1108. else {
  1109. ctr_led(ON);
  1110. rt_thread_delay(1000*19);
  1111. }
  1112. ctr_led(OFF);
  1113. }
  1114. //光控测试任务
  1115. void SunTest(void)
  1116. {
  1117. rt_uint16_t ui_timeout_l = 0;
  1118. //未检测到光照,一秒扫描等待,超时30S
  1119. while(Read_Light_Voltage()<500)
  1120. {
  1121. ui_timeout_l ++;
  1122. rt_thread_delay(1000);
  1123. if(ui_timeout_l==30) break;
  1124. }
  1125. rt_thread_delay(3000);
  1126. if(Read_Light_Voltage()<500)
  1127. {
  1128. device_status.sun_status = 0;//光控状态
  1129. }
  1130. if(Read_Light_Voltage()>500)
  1131. {
  1132. rt_thread_delay(1000);
  1133. }
  1134. rt_thread_delay(3000);
  1135. if(Read_Light_Voltage()>500)
  1136. {
  1137. device_status.sun_status = 1;
  1138. ctr_lamp(OFF,1);
  1139. }
  1140. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1141. device_status.sun_test = 0;
  1142. device_status.work_status=0;
  1143. }
  1144. /*上电初始化*/
  1145. void ResetInit(void)
  1146. {
  1147. LOG_I("ResetInit...");
  1148. //1.打开路由器供电
  1149. ctr_router(ON);
  1150. //2. 打开RTU供电
  1151. ctr_RTU_DTU(ON);
  1152. //3.打开光栅供电
  1153. ctr_photo(ON);
  1154. //5.关闭加热
  1155. //6.关闭补光灯
  1156. ctr_led(OFF);
  1157. }
  1158. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1159. void HardWare_Check(uint8_t boot)
  1160. {
  1161. if(boot==0&&device_status.version_type!=6)
  1162. {
  1163. //停止所有动作 -完成
  1164. HardWare_Init(boot);
  1165. }
  1166. else {
  1167. //停止所有动作
  1168. //读取记录待机功耗(实测大概70mA)
  1169. //(需减去屏幕的功耗大概120mA)
  1170. //动作上仓门,读取功率值保存
  1171. //动作下仓门,读取功率值保存
  1172. //动作清扫,读取功率值保存
  1173. //打开镇流器,读取功率值保存
  1174. //打开海康相机供电,读取功率值保存
  1175. //打开补光灯,读取功率值保存
  1176. //打开路由器/RTU供电,读取功率值保存
  1177. //打开加热,读取功率值保存
  1178. }
  1179. }
  1180. void WorkCheck(void)
  1181. {
  1182. static rt_uint32_t light_timeout = 3600;
  1183. while(device_status.ds == 1 &&
  1184. device_status.rain_status == 0 &&
  1185. device_status.temp_status == 0 &&
  1186. device_status.bat_status == 0)
  1187. {
  1188. if(device_status.timeSwitch == 1) // 时控模式
  1189. {
  1190. LOG_I("***********时控*****************");
  1191. if(device_status.startBar == device_status.stopBar)
  1192. {
  1193. // 开始结束时间相同则24小时模式
  1194. device_status.work_status = 1;
  1195. rt_thread_delay(10000);
  1196. continue;
  1197. }
  1198. time_t now;
  1199. time(&now);
  1200. struct tm now_tm = *localtime(&now);
  1201. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1202. rt_uint32_t start_sec = device_status.startBar * 3600;
  1203. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1204. // 修复跨天判断逻辑
  1205. if (start_sec < end_sec)
  1206. {
  1207. // 不跨天模式
  1208. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1209. }
  1210. else
  1211. {
  1212. // 跨天模式
  1213. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1214. }
  1215. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1216. device_status.work_status ? "工作" : "待机",
  1217. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1218. device_status.startBar, device_status.stopBar);
  1219. rt_thread_delay(10000);
  1220. }
  1221. else // 光控模式
  1222. {
  1223. LOG_I("光控定时模式!");
  1224. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1225. {
  1226. device_status.light_over_tim--;
  1227. light_timeout = 3600; // 重置超时计数器
  1228. device_status.work_status = 1;
  1229. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1230. }
  1231. else
  1232. {
  1233. // 仅在白天递减超时计数器
  1234. if(device_status.sun_status == 1 && light_timeout > 0) {
  1235. light_timeout--;
  1236. }
  1237. device_status.work_status = 0;
  1238. //没有在测试并且没有在自检
  1239. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1240. {
  1241. ctr_lamp(OFF,1);
  1242. }
  1243. // 连续1小时白天后重置
  1244. if(light_timeout == 0) {
  1245. device_status.light_over_tim = 0;
  1246. light_timeout = 3600; // 重置防止负数
  1247. }
  1248. // 白天且倒计时结束:重置夜晚时长
  1249. if(device_status.light_over_tim == 0 &&
  1250. device_status.sun_status == 1)
  1251. {
  1252. device_status.light_over_tim = device_status.sunBar * 3600;
  1253. }
  1254. }
  1255. }
  1256. rt_thread_delay(1000);
  1257. }
  1258. // 异常状态处理
  1259. device_status.work_status = 0;
  1260. rt_thread_delay(1000);
  1261. }
  1262. //拍照性诱工作任务流程
  1263. void WorkTask_XYV3(void)
  1264. {
  1265. int 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. //光栅传感器通电
  1276. ctr_photo(ON);
  1277. //拍照(?触发条件)计数?没有虫呢?定时
  1278. while(device_status.sleep_mode==0){
  1279. cal_n--;
  1280. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  1281. if(cal_n==0){
  1282. //发送拍照计数指令
  1283. device_status.photo_ws = 2;
  1284. cal_n = device_status.cul_time * 60;
  1285. }
  1286. if(device_status.temp_status==1 || device_status.bat_status==1 ||device_status.work_status==0) break;
  1287. rt_thread_delay(1000);
  1288. }
  1289. rt_thread_delay(10000);
  1290. }
  1291. ctr_lamp(OFF, 1);
  1292. ctr_photo(OFF);
  1293. }
  1294. /* 入口函数-工作状态检测 */
  1295. static void Work_status_thread_entry(void *parameter)
  1296. {
  1297. static uint16_t sun_status_num = 120;
  1298. //如果是光控定时,进行光控监测的消抖
  1299. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1300. {
  1301. rt_kprintf("光控定时!!!\n");
  1302. while(sun_status_num!=0)
  1303. {
  1304. sun_status_num--;
  1305. rt_thread_delay(1000);
  1306. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1307. }
  1308. device_status.light_over_tim = device_status.sunBar * 3600;
  1309. device_status.test_flag = 0;
  1310. }
  1311. while(1)
  1312. {
  1313. WorkCheck();
  1314. }
  1315. }
  1316. /*入口函数-工作流程*/
  1317. static void Work_Loop_thread_entry(void *parameter)
  1318. {
  1319. rt_thread_delay(1000);
  1320. ResetInit();
  1321. while(1)
  1322. {
  1323. WorkTask_XYV3();
  1324. rt_thread_delay(1000);
  1325. }
  1326. }
  1327. /*入口函数清扫*/
  1328. static void Control_Clear_thread_entry(void *parameter)
  1329. {
  1330. static int selfcheck_cnt = 0;
  1331. while(1)
  1332. {
  1333. if(device_status.selfcheck_ws==1){
  1334. selfcheck_cnt++;
  1335. }else{
  1336. selfcheck_cnt = 0;
  1337. }
  1338. if(selfcheck_cnt>1200) //超过3分钟
  1339. {
  1340. device_status.selfcheck_ws = 0;
  1341. device_status.camera_sta = 1; //相机异常
  1342. //上报自检结果
  1343. //上报自检结果
  1344. char *json_temp = makeJson_selfCheck_(1);
  1345. DTU_Write("%s",json_temp);
  1346. free(json_temp);
  1347. }
  1348. if(device_status.clear_status==10){
  1349. //ctr_turn(90,0);
  1350. Motor_shake(1,800);
  1351. device_status.clear_status = 0;
  1352. }
  1353. rt_thread_delay(100);
  1354. }
  1355. }
  1356. /*入口函数加热*/
  1357. //static void Control_Heat_thread_entry(void *parameter)
  1358. //{
  1359. // rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1360. // rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1361. // static int GaoyYa_cnt,GuangShan_cnt = 500; //10s
  1362. // bool GaoYa = false;
  1363. // bool GuangShan = false;
  1364. // while(1)
  1365. // {
  1366. //
  1367. // if(rt_pin_read(SIGNAL_GaoYa)==0)
  1368. // {
  1369. // LOG_I("检测到高压计数!");
  1370. // GaoYa = true;
  1371. // }
  1372. //
  1373. // if(rt_pin_read(SIGNAL_GuangShan)==0)
  1374. // {
  1375. // LOG_I("检测到光栅计数!");
  1376. // GuangShan = true;
  1377. // }
  1378. //
  1379. // if(GaoYa)
  1380. // {
  1381. // GaoyYa_cnt --;
  1382. // if(GaoyYa_cnt==0)
  1383. // {
  1384. // GaoyYa_cnt = 500;
  1385. // GaoYa = false;
  1386. // }
  1387. // }
  1388. //
  1389. // if(GuangShan)
  1390. // {
  1391. // GuangShan_cnt--;
  1392. // if(GuangShan_cnt==0)
  1393. // {
  1394. // GuangShan_cnt = 500;
  1395. // GuangShan = false;
  1396. // }
  1397. // }
  1398. //
  1399. // if(GaoYa&&GuangShan)
  1400. // {
  1401. // device_status.all_ycct += 1; //计数值增加
  1402. // //拍照
  1403. // if(device_status.all_ycct>=1){
  1404. // //device_status.photo_ws = 1;
  1405. // }
  1406. // GaoYa = false;
  1407. // GuangShan = false;
  1408. // GaoyYa_cnt = 500;
  1409. // GuangShan_cnt = 500;
  1410. // rt_thread_delay(1000);
  1411. // }
  1412. // rt_thread_delay(20);
  1413. // }
  1414. //}
  1415. static void Control_Heat_thread_entry(void *parameter) {
  1416. rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1417. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1418. // 修正变量名和初始化
  1419. static int GaoYa_cnt = 500, GuangShan_cnt = 500;
  1420. static bool GaoYa = false, GuangShan = false;
  1421. static int highPressureDuration = 0;
  1422. while (1) {
  1423. // 高压信号去抖动
  1424. if (rt_pin_read(SIGNAL_GaoYa) == 0) {
  1425. LOG_I("高压信号稳定触发");
  1426. GaoYa = true;
  1427. highPressureDuration ++;
  1428. }else{
  1429. highPressureDuration = 0;
  1430. }
  1431. // 光栅信号去抖动(类似逻辑)
  1432. if(rt_pin_read(SIGNAL_GuangShan)==0)
  1433. {
  1434. LOG_I("检测到光栅计数!");
  1435. GuangShan = true;
  1436. }
  1437. /* 高压持续时间监测 */
  1438. if(GaoYa)
  1439. {
  1440. // 持续时间累计(每次循环+1,20ms/次)
  1441. if(highPressureDuration >= 50)
  1442. {
  1443. LOG_E("高压持续超过5秒!");
  1444. // 执行关闭高压操作(示例函数)
  1445. ctr_lamp(OFF, 1);
  1446. // 可选:自动复位保护
  1447. rt_thread_mdelay(10000); // 等待10秒
  1448. ctr_lamp(ON, 1);
  1449. highPressureDuration = 0;
  1450. }
  1451. }
  1452. // 计数器逻辑
  1453. if (GaoYa) {
  1454. if (--GaoYa_cnt <= 0) {
  1455. GaoYa = false;
  1456. GaoYa_cnt = 500;
  1457. }
  1458. }
  1459. if (GuangShan) {
  1460. if (--GuangShan_cnt <= 0) {
  1461. GuangShan = false;
  1462. GuangShan_cnt = 500;
  1463. }
  1464. }
  1465. // 触发拍照逻辑
  1466. if (GaoYa && GuangShan) {
  1467. device_status.all_ycct++;
  1468. GaoYa = GuangShan = false;
  1469. GaoYa_cnt = GuangShan_cnt = 500;
  1470. }
  1471. rt_thread_delay(20); // 保持20ms周期
  1472. }
  1473. }
  1474. /*清扫线程*/
  1475. int Control_clear(void)
  1476. {
  1477. /* 创建线程*/
  1478. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1479. Control_Clear_thread_entry, RT_NULL,
  1480. 2048, //2048
  1481. 24, //23
  1482. WORK_CHECK_THREAD_TIMESLICE); //5
  1483. if(tid_ctr!=RT_NULL)
  1484. {
  1485. rt_thread_startup(tid_ctr);
  1486. }
  1487. return 0;
  1488. }
  1489. /*恒温加热线程*/
  1490. int Control_heat(void)
  1491. {
  1492. /* 创建线程*/
  1493. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  1494. Control_Heat_thread_entry, RT_NULL,
  1495. 1024, //2048
  1496. 25, //23
  1497. WORK_CHECK_THREAD_TIMESLICE); //5
  1498. if(tid_heat!=RT_NULL)
  1499. {
  1500. rt_thread_startup(tid_heat);
  1501. }
  1502. return 0;
  1503. }
  1504. /* 创建线程 -工作状态检测*/
  1505. int Workstatus_check(void)
  1506. {
  1507. /* 创建线程*/
  1508. tid_wkstc = rt_thread_create("work_status_check_thread",
  1509. Work_status_thread_entry, RT_NULL,
  1510. WORK_CHECK_THREAD_STACK_SIZE, //1024
  1511. WORK_CHECK_THREAD_PRIORITY, //23
  1512. WORK_CHECK_THREAD_TIMESLICE); //5
  1513. if(tid_wkstc!=RT_NULL)
  1514. {
  1515. rt_thread_startup(tid_wkstc);
  1516. }
  1517. return 0;
  1518. }
  1519. /*工作流程线程*/
  1520. int Work_Loop_Task(void)
  1521. {
  1522. rt_err_t result_loop = 0;
  1523. /* 创建线程*/
  1524. tid_workloop = rt_thread_create("work_loop_thread",
  1525. Work_Loop_thread_entry, RT_NULL,
  1526. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1527. WORK_LOOP_THREAD_PRIORITY, //12
  1528. WORK_LOOP_THREAD_TIMESLICE); //5
  1529. if(tid_workloop!=RT_NULL)
  1530. {
  1531. result_loop = rt_thread_startup(tid_workloop);
  1532. }
  1533. return result_loop;
  1534. }
  1535. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  1536. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1537. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  1538. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1539. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  1540. //INIT_APP_EXPORT(Control_heat);