control.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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. void WorkTask_XCT(void)
  273. {
  274. while(device_status.work_status==1&&device_status.ds==1)
  275. {
  276. ctr_Motor_xifu(ON);
  277. //打开相机供电
  278. ctr_Camera_Power(ON);
  279. //ctr_Heat(ON); //清洁风机
  280. uint32_t photo_fre = 0;
  281. photo_fre = device_status.dattim * 60;
  282. while(photo_fre!=0)
  283. {
  284. photo_fre --;
  285. rt_thread_delay(1000);
  286. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  287. }
  288. //卷带
  289. ctr_JD_Clear(OFF);
  290. //拍照
  291. HK_TakePhoto();
  292. //关闭吸附风机
  293. ctr_Motor_xifu(OFF);
  294. //关闭相机供电
  295. //ctr_Heat(OFF);
  296. ctr_Camera_Power(OFF);
  297. //卷带
  298. ctr_JD_Clear(ON);
  299. // rt_thread_delay(1000);
  300. // ctr_JD_Clear(ON);
  301. //震动
  302. Motor_shake(ON, device_status.shake_sec);
  303. }
  304. //关闭风机
  305. ctr_Motor_xichong(OFF);
  306. ctr_Motor_xifu(OFF);
  307. LOG_I("不在工作时间,未工作!!!");
  308. }
  309. /**********************************************************************/
  310. /*光控*/
  311. uint8_t In_Light_status(void)
  312. {
  313. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  314. return rt_pin_read(LIGHT_PIN);
  315. }
  316. /*雨控*/
  317. uint8_t In_Rain_status(void)
  318. {
  319. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  320. return rt_pin_read(RAIN_PIN);
  321. }
  322. /*加热*/
  323. uint8_t ctr_Heat(uint8_t status_heat)
  324. {
  325. device_status.heat_status = status_heat;
  326. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  327. rt_pin_write(Heat_Pin,status_heat);
  328. return status_heat;
  329. }
  330. /*制冷*/
  331. uint8_t ctr_Cold(uint8_t status_cold)
  332. {
  333. device_status.cold_status = status_cold;
  334. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  335. rt_pin_write(Cold_Pin,status_cold);
  336. return status_cold;
  337. }
  338. /*相机供电*/
  339. void ctr_photo(uint8_t status_photo)
  340. {
  341. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  342. rt_pin_write(PHOTO,status_photo);
  343. }
  344. /*************************************************
  345. Function: ctr_Cool_Fan
  346. Description: 散热风机的开关
  347. Calls:
  348. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  349. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  350. Input: ON-开,OFF-关
  351. Output: NULL
  352. Return: ON-开,OFF-关
  353. Others: NULL
  354. *************************************************/
  355. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  356. {
  357. device_status.cool_fan_status = status_cool;
  358. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  359. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  360. if(status_cool==OFF)
  361. {
  362. rt_pin_write(Cool_Fan_L,OFF);
  363. rt_pin_write(Cool_Fan_R,OFF);
  364. }
  365. if(status_cool==ON)
  366. {
  367. rt_pin_write(Cool_Fan_L,ON);
  368. rt_pin_write(Cool_Fan_R,OFF);
  369. }
  370. return device_status.cool_fan_status;
  371. }
  372. /*************************************************
  373. Function: ctr_Collect_Fan
  374. Description: 采集风机的开关
  375. Calls:
  376. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  377. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  378. Input: ON-开,OFF-关
  379. Output: NULL
  380. Return: ON-开,OFF-关
  381. Others: NULL
  382. *************************************************/
  383. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  384. {
  385. device_status.collect_fan_status = status_collect;
  386. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  387. if(status_collect==OFF)
  388. {
  389. rt_pin_write(Collect_Fan,OFF);
  390. }
  391. if(status_collect==ON)
  392. {
  393. rt_pin_write(Collect_Fan,ON);
  394. }
  395. return device_status.collect_fan_status;
  396. }
  397. /*************************************************
  398. Function: ctr_develop
  399. Description: 培养载玻片孢子
  400. Calls:
  401. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  402. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  403. Input: v=0(复位) v=90转90度
  404. Output: NULL
  405. Return: NULL
  406. Others: NULL
  407. *************************************************/
  408. /*上仓门*/
  409. uint8_t ctr_cangmen_up(uint8_t status_up)
  410. {
  411. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  412. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  413. if(status_up==OFF)
  414. {
  415. rt_pin_write(Collect_fan_R,ON);
  416. rt_pin_write(Collect_fan_L,ON);
  417. device_status.ctr_up_motor = 0;
  418. rt_thread_delay(1000);
  419. }
  420. if(status_up==OPEN)
  421. {
  422. device_status.up_status = 1;
  423. rt_pin_write(Collect_fan_R,OFF);
  424. rt_pin_write(Collect_fan_L,OFF);
  425. rt_pin_write(Collect_fan_R,ON);
  426. rt_pin_write(Collect_fan_L,OFF);
  427. }
  428. if(status_up==CLOSE)
  429. {
  430. device_status.up_status = 0;
  431. rt_pin_write(Collect_fan_R,OFF);
  432. rt_pin_write(Collect_fan_L,OFF);
  433. rt_pin_write(Collect_fan_R,OFF);
  434. rt_pin_write(Collect_fan_L,ON);
  435. }
  436. return device_status.up_status;
  437. }
  438. //下仓门
  439. uint8_t ctr_cangmen_down(uint8_t status_down)
  440. {
  441. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  442. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  443. if(status_down==OFF)
  444. {
  445. rt_pin_write(C_DOWN_R,ON);
  446. rt_pin_write(C_DOWN_L,ON);
  447. device_status.ctr_down_motor = 0;
  448. rt_thread_delay(1000);
  449. }
  450. if(status_down==OPEN)
  451. {
  452. device_status.down_status = 1;
  453. rt_pin_write(C_DOWN_R,OFF);
  454. rt_pin_write(C_DOWN_L,OFF);
  455. rt_pin_write(C_DOWN_R,ON);
  456. rt_pin_write(C_DOWN_L,OFF);
  457. }
  458. if(status_down==CLOSE)
  459. {
  460. device_status.down_status = 0;
  461. rt_pin_write(C_DOWN_R,OFF);
  462. rt_pin_write(C_DOWN_L,OFF);
  463. rt_pin_write(C_DOWN_R,OFF);
  464. rt_pin_write(C_DOWN_L,ON);
  465. }
  466. return device_status.down_status;
  467. }
  468. //镇流器
  469. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  470. {
  471. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  472. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  473. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  474. if(status_lamp==ON)
  475. {
  476. switch(lamp_num){
  477. case 1:
  478. rt_pin_write(LAMP_SW,ON);
  479. device_status.lamp_status = status_lamp;
  480. break;
  481. case 2:
  482. rt_pin_write(LAMP_1_SW,ON);
  483. device_status.lamp_status = status_lamp;
  484. break;
  485. case 3:
  486. rt_pin_write(LAMP_2_SW,ON);
  487. device_status.lamp_status = status_lamp;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. else {
  494. switch(lamp_num){
  495. case 1:
  496. rt_pin_write(LAMP_SW,OFF);
  497. device_status.lamp_status = status_lamp;
  498. break;
  499. case 2:
  500. rt_pin_write(LAMP_1_SW,OFF);
  501. device_status.lamp1_status = status_lamp;
  502. break;
  503. case 3:
  504. rt_pin_write(LAMP_2_SW,OFF);
  505. device_status.lamp2_status = status_lamp;
  506. break;
  507. default:
  508. break;
  509. }
  510. }
  511. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  512. return device_status.lamp_status;
  513. }
  514. //震动电机
  515. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  516. {
  517. static rt_uint16_t shake_i = 0;
  518. shake_i = atoi(device_status.power);
  519. LOG_I("震动电机待机电流:%d",shake_i);
  520. if(device_status.dtype==7&&shake_sec!=0)
  521. DTU_Write("drop");
  522. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  523. #if 0
  524. rt_pin_write(SHAKE_SW,ON);
  525. return 0;
  526. #endif
  527. if(status_shake==ON&&device_status.shake_ws==1)
  528. {
  529. #if 0
  530. for(int8_t s = 0;s++;s<2)
  531. {
  532. rt_pin_write(SHAKE_SW,ON);
  533. rt_thread_delay(100);
  534. rt_pin_write(SHAKE_SW,OFF);
  535. rt_thread_delay(1000);
  536. }
  537. #endif
  538. rt_pin_write(SHAKE_SW,ON);
  539. if(device_status.dtype==3)
  540. {
  541. while(shake_sec--)
  542. {
  543. rt_thread_delay(100);
  544. if(shake_sec==20&&device_status.clear_status==0&&device_status.work_status==1&&atoi(device_status.power)-shake_i<100&&shake_i!=0&&shake_i<5000)
  545. {
  546. char * shake_warn = makeJson_Warn("电流不足", "震动电机");
  547. DTU_Write("%s",shake_warn);
  548. rt_free(shake_warn);
  549. }
  550. LOG_D("震动电机:%d",atoi(device_status.power));
  551. }
  552. }
  553. if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
  554. rt_pin_write(SHAKE_SW,OFF);
  555. HMI_WRITE("cangWei.switchShake.val=%d",0);
  556. }
  557. else
  558. {
  559. rt_pin_write(SHAKE_SW,OFF);
  560. HMI_WRITE("cangWei.switchShake.val=%d",0);
  561. }
  562. if(device_status.dtype==7)
  563. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  564. return status_shake;
  565. }
  566. //补光
  567. uint8_t ctr_led(uint8_t status_led)
  568. {
  569. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  570. if(device_status.version_type==6)
  571. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  572. if(status_led==ON)
  573. {
  574. rt_pin_write(LED_SW,ON);
  575. if(device_status.version_type==6)
  576. rt_pin_write(LED_P,ON);
  577. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  578. }
  579. else if(status_led==OFF){
  580. rt_pin_write(LED_SW,OFF);
  581. if(device_status.version_type==6)
  582. rt_pin_write(LED_P,OFF);
  583. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  584. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  585. }
  586. return status_led;
  587. }
  588. /*
  589. *
  590. * */
  591. void ctr_Motor_Driver_Power(uint8_t status_ws)
  592. {
  593. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  594. rt_pin_write(SHAKE_SW, status_ws);
  595. }
  596. /*************************************************
  597. Function: ctr_Motor_Driver
  598. Description: 步进电机驱动
  599. Calls:
  600. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  601. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  602. Input: 50000000
  603. Output: NULL
  604. Return: shake_sec
  605. Others: NULL
  606. *************************************************/
  607. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  608. {
  609. beep_init();
  610. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  611. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  612. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  613. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  614. if(EN==0)
  615. {
  616. rt_pin_write(Motor_Power,0);
  617. }
  618. else{
  619. rt_pin_write(Motor_EN, EN);
  620. }
  621. rt_pin_write(Motor_DIR, DIR);
  622. beep_set(pwm, pwm/2); //200kzh - 50%
  623. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  624. {
  625. if(time_out==0)
  626. {
  627. break;
  628. }
  629. rt_thread_delay(50);
  630. time_out --;
  631. }
  632. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  633. {
  634. if(time_out==0)
  635. {
  636. break;
  637. }
  638. rt_thread_delay(50);
  639. time_out --;
  640. }
  641. //rt_pin_write(Motor_EN, OFF);
  642. //if(device_status.autotakepic==0)
  643. //ctr_lamp(OFF, 1);
  644. beep_set(0, 0); //200kzh - 50%
  645. rt_thread_delay(50);
  646. }
  647. /*************************************************
  648. Function: ctr_Petroleum_jelly
  649. Description: 涂凡士林
  650. Calls:
  651. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  652. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  653. Input: NULL
  654. Output: NULL
  655. Return: NULL
  656. Others: NULL
  657. *************************************************/
  658. void ctr_Petroleum_jelly(rt_uint32_t delay)
  659. {
  660. beep_init();
  661. pwm_on(2,50000);
  662. rt_thread_delay(delay*1000);
  663. pwm_off(2,0);
  664. rt_thread_delay(1000);
  665. pwm_on(3, 50000);
  666. rt_thread_delay(delay*1000);
  667. pwm_off(3,0);
  668. device_status.fsl_sw = 0;
  669. }
  670. //1.0转仓
  671. void turnzp(void)
  672. {
  673. static uint16_t turn_timeout = 0;
  674. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  675. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  676. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  677. // rt_pin_write(CLEAR_R,OFF);
  678. // rt_pin_write(CLEAR_L,OFF);
  679. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  680. {
  681. rt_thread_delay(20);
  682. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  683. {
  684. // rt_pin_write(CLEAR_R,ON);
  685. // rt_pin_write(CLEAR_L,OFF);
  686. Motor_Juandai(ON);
  687. rt_thread_delay(50);
  688. turn_timeout++;
  689. if(turn_timeout==600)
  690. {
  691. LOG_E("检测不到限位,电机强制停止");
  692. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  693. DTU_Write("%s",ctr_zp_limit_warn);
  694. rt_free(ctr_zp_limit_warn);
  695. break;
  696. }
  697. }
  698. break;
  699. }
  700. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  701. {
  702. rt_thread_delay(20);
  703. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  704. {
  705. // rt_pin_write(CLEAR_R,ON);
  706. // rt_pin_write(CLEAR_L,OFF);
  707. Motor_Juandai(ON);
  708. rt_thread_delay(50);
  709. turn_timeout++;
  710. if(turn_timeout==600)
  711. {
  712. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  713. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  714. DTU_Write("%s",ctr_zp_limit_warn);
  715. rt_free(ctr_zp_limit_warn);
  716. break;
  717. }
  718. }
  719. break;
  720. }
  721. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  722. {
  723. rt_thread_delay(20);
  724. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  725. {
  726. // rt_pin_write(CLEAR_R,OFF);
  727. // rt_pin_write(CLEAR_L,OFF);
  728. Motor_Juandai(OFF);
  729. break;
  730. }
  731. else {
  732. break;
  733. }
  734. }
  735. Motor_Juandai(OFF);
  736. // rt_pin_write(CLEAR_R,OFF);
  737. // rt_pin_write(CLEAR_L,OFF);
  738. turn_timeout = 0;
  739. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  740. HMI_WRITE("sysTest.switchClear.val=%d",0);
  741. rt_thread_delay(1000);
  742. }
  743. //清扫-常闭型限位开关
  744. //新结构使用推杆电机做清扫
  745. uint8_t ctr_clear(uint16_t status_clear)
  746. {
  747. static uint16_t clear_num = 0;
  748. #if PWM_EN == 0
  749. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  750. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  751. #endif
  752. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  753. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  754. /*************************************************************外接清扫
  755. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  756. rt_pin_write(POWER_CLEAR,ON);
  757. rt_thread_delay(1000*100);
  758. rt_pin_write(POWER_CLEAR,OFF);
  759. ************************************************************/
  760. //停止
  761. #if PWM_EN == 0
  762. rt_pin_write(CLEAR_R,OFF);
  763. rt_pin_write(CLEAR_L,OFF);
  764. #else
  765. Motor_Qingsao_R_PWM(OFF);
  766. Motor_Qingsao_L_PWM(OFF);
  767. #endif
  768. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  769. rt_thread_delay(100);
  770. //如果清扫位置在中间
  771. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  772. {
  773. #if PWM_EN == 0
  774. rt_pin_write(CLEAR_R,ON);
  775. rt_pin_write(CLEAR_L,OFF);
  776. #else
  777. Motor_Qingsao_R_PWM(ON);
  778. Motor_Qingsao_L_PWM(OFF);
  779. #endif
  780. rt_thread_delay(50);
  781. clear_num++;
  782. //已到达检测位置
  783. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  784. {
  785. #if PWM_EN == 0
  786. rt_pin_write(CLEAR_R,ON);
  787. rt_pin_write(CLEAR_L,ON);
  788. #else
  789. Motor_Qingsao_R_PWM(OFF);
  790. Motor_Qingsao_L_PWM(OFF);
  791. #endif
  792. rt_thread_delay(2000);
  793. break;
  794. }
  795. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  796. {
  797. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  798. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  799. #if PWM_EN == 0
  800. rt_pin_write(CLEAR_R,ON);
  801. rt_pin_write(CLEAR_L,ON);
  802. #else
  803. Motor_Qingsao_R_PWM(OFF);
  804. Motor_Qingsao_L_PWM(OFF);
  805. #endif
  806. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  807. if(Read_Real_Voltage()/100<12)
  808. {
  809. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  810. HMI_WRITE("sysTest.switchClear.val=%d",0);
  811. device_status.clear_status = 0;
  812. return 0;
  813. }
  814. break;
  815. }
  816. }
  817. clear_num = 0;
  818. //如果清扫位置在左侧
  819. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  820. {
  821. #if PWM_EN == 0
  822. rt_pin_write(CLEAR_R,ON);
  823. rt_pin_write(CLEAR_L,OFF);
  824. #else
  825. Motor_Qingsao_R_PWM(ON);
  826. Motor_Qingsao_L_PWM(OFF);
  827. #endif
  828. rt_thread_delay(50);
  829. clear_num++;
  830. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  831. {
  832. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  833. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  834. #if PWM_EN == 0
  835. rt_pin_write(CLEAR_R,ON);
  836. rt_pin_write(CLEAR_L,ON);
  837. #else
  838. Motor_Qingsao_R_PWM(OFF);
  839. Motor_Qingsao_L_PWM(OFF);
  840. #endif
  841. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  842. if(Read_Real_Voltage()/100<12)
  843. {
  844. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  845. HMI_WRITE("sysTest.switchClear.val=%d",0);
  846. device_status.clear_status = 0;
  847. return 0;
  848. }
  849. break;
  850. }
  851. }
  852. clear_num = 0;
  853. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  854. {
  855. #if PWM_EN == 0
  856. rt_pin_write(CLEAR_R,ON);
  857. rt_pin_write(CLEAR_L,ON);
  858. #else
  859. Motor_Qingsao_R_PWM(OFF);
  860. Motor_Qingsao_L_PWM(OFF);
  861. #endif
  862. if(Read_Real_Voltage()/100<12)
  863. {
  864. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  865. HMI_WRITE("sysTest.switchClear.val=%d",0);
  866. device_status.clear_status = 0;
  867. return 0;
  868. }
  869. rt_thread_delay(2000);
  870. break;
  871. }
  872. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  873. {
  874. #if PWM_EN == 0
  875. rt_pin_write(CLEAR_R,OFF);
  876. rt_pin_write(CLEAR_L,ON);
  877. #else
  878. Motor_Qingsao_R_PWM(OFF);
  879. Motor_Qingsao_L_PWM(ON);
  880. #endif
  881. rt_thread_delay(50);
  882. clear_num++;
  883. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  884. {
  885. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  886. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  887. //DTU_Write("%s",Clear_ERR);
  888. #if PWM_EN == 0
  889. rt_pin_write(CLEAR_R,ON);
  890. rt_pin_write(CLEAR_L,ON);
  891. #else
  892. Motor_Qingsao_R_PWM(OFF);
  893. Motor_Qingsao_L_PWM(OFF);
  894. #endif
  895. rt_free(Clear_ERR);
  896. if(Read_Real_Voltage()/100<12)
  897. {
  898. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  899. HMI_WRITE("sysTest.switchClear.val=%d",0);
  900. device_status.clear_status = 0;
  901. return 0;
  902. }
  903. break;
  904. }
  905. }
  906. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  907. {
  908. #if PWM_EN == 0
  909. rt_pin_write(CLEAR_R,ON);
  910. rt_pin_write(CLEAR_L,ON);
  911. #else
  912. Motor_Qingsao_R_PWM(OFF);
  913. Motor_Qingsao_L_PWM(OFF);
  914. #endif
  915. rt_thread_delay(2000);
  916. break;
  917. }
  918. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  919. {
  920. #if PWM_EN == 0
  921. rt_pin_write(CLEAR_R,ON);
  922. rt_pin_write(CLEAR_L,ON);
  923. #else
  924. Motor_Qingsao_R_PWM(OFF);
  925. Motor_Qingsao_L_PWM(OFF);
  926. #endif
  927. if(device_status.version_type==41)
  928. {
  929. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  930. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  931. //DTU_Write("%s",Clear_ERR);
  932. rt_free(Clear_ERR);
  933. }
  934. return 0;
  935. }
  936. clear_num = 0;
  937. rt_thread_delay(2000);
  938. #if PWM_EN == 0
  939. rt_pin_write(CLEAR_R,ON);
  940. rt_pin_write(CLEAR_L,ON);
  941. #else
  942. Motor_Qingsao_R_PWM(OFF);
  943. Motor_Qingsao_L_PWM(OFF);
  944. #endif
  945. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  946. HMI_WRITE("sysTest.switchClear.val=%d",0);
  947. device_status.clear_status = 0;
  948. return 1;
  949. }
  950. //路由器供电
  951. void ctr_router(uint8_t status_router)
  952. {
  953. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  954. if(device_status.version_type==6)
  955. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  956. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  957. if(status_router==ON)
  958. {
  959. rt_pin_write(ROUTER,ON);
  960. //
  961. #if LAMP_EN == 0
  962. if(device_status.version_type==6)
  963. rt_pin_write(ROUTER_P,ON);
  964. #endif
  965. }
  966. else {
  967. rt_pin_write(ROUTER,OFF);
  968. //
  969. #if LAMP_EN == 0
  970. if(device_status.version_type==6)
  971. rt_pin_write(ROUTER_P,OFF);
  972. #endif
  973. }
  974. }
  975. //清扫-推杆电机
  976. void ctr_TG_Clear(uint16_t clear_timeout)
  977. {
  978. #if 0
  979. int current_clear[200];
  980. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  981. LOG_I("清扫电机待机:%d",Electric_current);
  982. #endif
  983. static uint16_t clear_count = 0;
  984. clear_count = clear_timeout;
  985. #if PWM_EN == 0
  986. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  987. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  988. #endif
  989. while(clear_count!=0)
  990. {
  991. #if PWM_EN == 0
  992. rt_pin_write(CLEAR_R,ON);
  993. rt_pin_write(CLEAR_L,OFF);
  994. #else
  995. Motor_Qingsao_R_PWM(ON);
  996. Motor_Qingsao_L_PWM(OFF);
  997. #endif
  998. clear_count--;
  999. rt_thread_delay(100);
  1000. }
  1001. #if PWM_EN == 0
  1002. rt_pin_write(CLEAR_R,ON);
  1003. rt_pin_write(CLEAR_L,ON);
  1004. #else
  1005. Motor_Qingsao_R_PWM(OFF);
  1006. Motor_Qingsao_L_PWM(OFF);
  1007. #endif
  1008. rt_thread_delay(3000);
  1009. clear_count = 0;
  1010. while(clear_timeout!=clear_count)
  1011. {
  1012. #if PWM_EN == 0
  1013. rt_pin_write(CLEAR_R,OFF);
  1014. rt_pin_write(CLEAR_L,ON);
  1015. #else
  1016. Motor_Qingsao_R_PWM(OFF);
  1017. Motor_Qingsao_L_PWM(ON);
  1018. #endif
  1019. // if(clear_count<200)
  1020. // current_clear[clear_count] = atoi(device_status.power);
  1021. clear_count++;
  1022. rt_thread_delay(100);
  1023. }
  1024. #if PWM_EN == 0
  1025. rt_pin_write(CLEAR_R,ON);
  1026. rt_pin_write(CLEAR_L,ON);
  1027. #else
  1028. Motor_Qingsao_R_PWM(OFF);
  1029. Motor_Qingsao_L_PWM(OFF);
  1030. #endif
  1031. #if 0
  1032. bubblesort(current_clear,200);
  1033. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1034. {
  1035. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1036. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1037. DTU_Write("%s",ctr_clear_warn);
  1038. rt_free(ctr_clear_warn);
  1039. }
  1040. #endif
  1041. rt_thread_delay(2000);
  1042. device_status.clear_status = 0;
  1043. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1044. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1045. }
  1046. //DTU,RTU供电
  1047. void ctr_RTU_DTU(uint8_t status_rtu)
  1048. {
  1049. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1050. if(status_rtu==ON)
  1051. {
  1052. LOG_W("RTU电源启动!!!");
  1053. rt_pin_write(RTU_DTU,ON);
  1054. }
  1055. else
  1056. {
  1057. LOG_W("RTU电源关闭!!!");
  1058. rt_pin_write(RTU_DTU,OFF);
  1059. }
  1060. }
  1061. //海康拍照
  1062. void Hk_Take_io(void)
  1063. {
  1064. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1065. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1066. rt_pin_write(HK_TAKE, 1);
  1067. rt_pin_write(HK_IO, 0);
  1068. if(device_status.dtype==3)
  1069. Beep_Tip(1000);
  1070. if(device_status.dtype==7)
  1071. rt_thread_mdelay(1000);
  1072. rt_pin_write(HK_TAKE, 0);
  1073. rt_pin_write(HK_IO, 1);
  1074. }
  1075. void calorstat(uint16_t heat_time)
  1076. {
  1077. //uint8_t heat_up_cnt = 0;
  1078. uint16_t n = 0;
  1079. uint16_t delay_heat = 60;
  1080. FlagStatus heat_flg = RESET;
  1081. device_status.heat_time_min = 0;
  1082. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1083. ctr_Heat(ON);
  1084. // UpLoad_Data(); //开始加热时的第一条数据
  1085. //恒温模式
  1086. for(n=0;n < heat_time;n++)
  1087. {
  1088. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1089. {
  1090. heat_flg = SET;
  1091. ctr_Heat(OFF);
  1092. while(delay_heat!=0)
  1093. {
  1094. delay_heat --;
  1095. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1096. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1097. rt_thread_delay(1000);
  1098. if(device_status.heattem>device_status.run_sta + 1){
  1099. device_status.run_sta = device_status.heattem;
  1100. }
  1101. }
  1102. delay_heat = 60;
  1103. //跳出FOR循环
  1104. n = n + 60;
  1105. }
  1106. if(device_status.heattem>device_status.run_sta + 1){
  1107. device_status.run_sta = device_status.heattem;
  1108. }
  1109. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1110. {
  1111. //device_status.run_sta = device_status.heattem;
  1112. ctr_Heat(ON);
  1113. }
  1114. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1115. {
  1116. // if(heat_up_cnt==0)
  1117. // {
  1118. // UpLoad_Data(); //加热到设定温度的数据
  1119. // heat_up_cnt = 1;
  1120. // }
  1121. //device_status.run_sta = device_status.heattem;
  1122. ctr_Heat(OFF);
  1123. }
  1124. if((n % 60)==0)
  1125. {
  1126. device_status.heat_time_min = device_status.heat_time_min + 1;
  1127. // if(device_status.heat_time_min % 5 == 0)
  1128. // {
  1129. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1130. // }
  1131. }
  1132. //如果触发异常退出加热
  1133. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1134. {
  1135. rt_thread_delay(1000);
  1136. if(device_status.bat_status == 1)
  1137. {
  1138. rt_kprintf("============================触发异常退出加热==========================");
  1139. ctr_lamp(OFF,1);
  1140. ctr_Heat(OFF);
  1141. break;
  1142. }
  1143. }
  1144. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1145. {
  1146. ctr_Heat(OFF);
  1147. rt_kprintf("异常退出!!!\n");
  1148. break;
  1149. }
  1150. rt_thread_delay(1000);
  1151. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1152. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1153. }
  1154. ctr_Heat(OFF);
  1155. //UpLoad_Data(); //加热结束的数据
  1156. }
  1157. //初始化所有控制驱动,关闭
  1158. void HardWare_Init(uint8_t boot)
  1159. {
  1160. Beep_Tip(200);
  1161. //关闭上仓门
  1162. ctr_cangmen_up(OFF);
  1163. if(device_status.dtype==3){
  1164. //关闭下仓门电机
  1165. ctr_cangmen_down(OFF);
  1166. }
  1167. //关闭镇流器
  1168. if(device_status.autotakepic==0)
  1169. ctr_lamp(OFF,1);
  1170. //关闭震动电机
  1171. ctr_shake(OFF,0);
  1172. //关闭加热
  1173. ctr_Heat(OFF);
  1174. //关闭补光灯
  1175. ctr_led(OFF);
  1176. //关闭12V供电(海康相机,RTU)
  1177. ctr_photo(OFF);
  1178. //关闭清扫电机驱动
  1179. ctr_clear(OFF);
  1180. }
  1181. //拍照
  1182. void HK_TakePhoto(void)
  1183. {
  1184. //打开补光灯
  1185. DTU_Write("takephoto.mp3");
  1186. rt_thread_delay(10000);
  1187. char *takephotojson = makeJson_takephoto();
  1188. DTU_Write("%s",takephotojson);
  1189. rt_free(takephotojson);
  1190. //吸虫塔
  1191. if(device_status.version_type!=6)
  1192. {
  1193. ctr_led(ON);
  1194. if(device_status.version_type==48)
  1195. {
  1196. rt_thread_delay(1000*60);
  1197. }
  1198. else {
  1199. rt_thread_delay(1000*15);
  1200. }
  1201. }
  1202. //触发海康相机拍照
  1203. Hk_Take_io();
  1204. if(device_status.version_type!=6)
  1205. {
  1206. rt_thread_delay(1000*45);
  1207. }
  1208. else {
  1209. ctr_led(ON);
  1210. rt_thread_delay(1000*19);
  1211. }
  1212. ctr_led(OFF);
  1213. }
  1214. //光控测试任务
  1215. void SunTest(void)
  1216. {
  1217. rt_uint16_t ui_timeout_l = 0;
  1218. //未检测到光照,一秒扫描等待,超时30S
  1219. while(Read_Light_Voltage()<500)
  1220. {
  1221. ui_timeout_l ++;
  1222. rt_thread_delay(1000);
  1223. if(ui_timeout_l==30) break;
  1224. }
  1225. rt_thread_delay(3000);
  1226. if(Read_Light_Voltage()<500)
  1227. {
  1228. device_status.sun_status = 0;//光控状态
  1229. }
  1230. if(Read_Light_Voltage()>500)
  1231. {
  1232. rt_thread_delay(1000);
  1233. }
  1234. rt_thread_delay(3000);
  1235. if(Read_Light_Voltage()>500)
  1236. {
  1237. device_status.sun_status = 1;
  1238. ctr_lamp(OFF,1);
  1239. }
  1240. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1241. device_status.sun_test = 0;
  1242. device_status.work_status=0;
  1243. }
  1244. /*上电初始化*/
  1245. void ResetInit(void)
  1246. {
  1247. LOG_I("ResetInit...");
  1248. if(device_status.sleep_mode==0)
  1249. {
  1250. //1.打开路由器供电
  1251. ctr_router(ON);
  1252. //2. 打开RTU供电
  1253. ctr_RTU_DTU(ON);
  1254. }
  1255. //3.打开光栅供电
  1256. ctr_photo(ON);
  1257. //5.关闭加热
  1258. //6.关闭补光灯
  1259. ctr_led(OFF);
  1260. }
  1261. /*三色光源*/
  1262. void Led_Check(uint8_t tp)
  1263. {
  1264. if(tp==3)
  1265. {
  1266. if(device_status.light_over_tim>2*3600)
  1267. {
  1268. device_status.work_status = 1;
  1269. device_status.lamp_status = 1;
  1270. device_status.lamp1_status = 0;
  1271. device_status.lamp2_status = 0;
  1272. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1273. }
  1274. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1275. device_status.work_status = 1;
  1276. device_status.lamp_status = 0;
  1277. device_status.lamp1_status = 1;
  1278. device_status.lamp2_status = 0;
  1279. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1280. }
  1281. else if (device_status.light_over_tim<1*3600) {
  1282. device_status.work_status = 1;
  1283. device_status.lamp_status = 0;
  1284. device_status.lamp1_status = 0;
  1285. device_status.lamp2_status = 1;
  1286. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1287. }
  1288. }
  1289. else if (tp==6) {
  1290. if(device_status.light_over_tim>4*3600)
  1291. {
  1292. device_status.work_status = 1;
  1293. device_status.lamp_status = 1;
  1294. device_status.lamp1_status = 0;
  1295. device_status.lamp2_status = 0;
  1296. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1297. }
  1298. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1299. device_status.work_status = 1;
  1300. device_status.lamp_status = 0;
  1301. device_status.lamp1_status = 1;
  1302. device_status.lamp2_status = 0;
  1303. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1304. }
  1305. else if (device_status.light_over_tim<7200) {
  1306. device_status.work_status = 1;
  1307. device_status.lamp_status = 0;
  1308. device_status.lamp1_status = 0;
  1309. device_status.lamp2_status = 1;
  1310. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1311. }
  1312. }
  1313. else if (tp==9) {
  1314. if(device_status.light_over_tim>6*3600)
  1315. {
  1316. device_status.work_status = 1;
  1317. device_status.lamp_status = 1;
  1318. device_status.lamp1_status = 0;
  1319. device_status.lamp2_status = 0;
  1320. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1321. }
  1322. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1323. device_status.work_status = 1;
  1324. device_status.lamp_status = 0;
  1325. device_status.lamp1_status = 1;
  1326. device_status.lamp2_status = 0;
  1327. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1328. }
  1329. else if (device_status.light_over_tim<3*3600) {
  1330. device_status.work_status = 1;
  1331. device_status.lamp_status = 0;
  1332. device_status.lamp1_status = 0;
  1333. device_status.lamp2_status = 1;
  1334. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1335. }
  1336. }
  1337. }
  1338. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1339. void HardWare_Check(uint8_t boot)
  1340. {
  1341. if(boot==0&&device_status.version_type!=6)
  1342. {
  1343. //停止所有动作 -完成
  1344. HardWare_Init(boot);
  1345. }
  1346. else {
  1347. //停止所有动作
  1348. //读取记录待机功耗(实测大概70mA)
  1349. //(需减去屏幕的功耗大概120mA)
  1350. //动作上仓门,读取功率值保存
  1351. //动作下仓门,读取功率值保存
  1352. //动作清扫,读取功率值保存
  1353. //打开镇流器,读取功率值保存
  1354. //打开海康相机供电,读取功率值保存
  1355. //打开补光灯,读取功率值保存
  1356. //打开路由器/RTU供电,读取功率值保存
  1357. //打开加热,读取功率值保存
  1358. }
  1359. }
  1360. //工作状态检测
  1361. void WorkCheck(void)
  1362. {
  1363. static rt_uint32_t light_timeout = 3600;
  1364. //雨控检测,温控检测,电池电压检测
  1365. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1366. {
  1367. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1368. if(device_status.timeSwitch==1)
  1369. {
  1370. LOG_I("***********时控*****************");
  1371. if(device_status.startBar==device_status.stopBar&&device_status.startBar==0){
  1372. device_status.work_status = 1;
  1373. LOG_I("24 hours work...");
  1374. #if LAMP_EN == 1
  1375. device_status.lamp_status = 1;
  1376. device_status.lamp1_status = 0;
  1377. device_status.lamp2_status = 0;
  1378. if(device_status.sun_status==1){
  1379. ctr_lamp(OFF, 1);
  1380. ctr_lamp(OFF, 2);
  1381. ctr_lamp(OFF, 3);
  1382. }
  1383. #endif
  1384. }
  1385. //如果开始和结束是同一天
  1386. if(device_status.startBar<device_status.stopBar)
  1387. {
  1388. LOG_I("开始和结束是同一天");
  1389. time_t now;
  1390. time(&now);
  1391. static struct tm nowtime; //现在
  1392. static struct tm starttime; //开始
  1393. static struct tm endtime; //结束
  1394. //获取当前时间戳
  1395. nowtime =*localtime(&now);
  1396. nowtime.tm_hour = nowtime.tm_hour-8;
  1397. //获取定时开始的时间戳
  1398. starttime = *localtime(&now);
  1399. starttime.tm_hour = device_status.startBar-8;
  1400. starttime.tm_min = 0;
  1401. starttime.tm_sec = 0;
  1402. //获取定时结束的时间戳
  1403. endtime = *localtime(&now);
  1404. endtime.tm_hour = device_status.stopBar-8;
  1405. endtime.tm_min = 0;
  1406. endtime.tm_sec = 0;
  1407. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1408. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1409. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1410. {
  1411. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1412. device_status.work_status = 1;
  1413. }
  1414. //如果当前时间小于开始时间
  1415. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1416. {
  1417. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1418. device_status.work_status = 0;
  1419. }
  1420. //当前时间大于结束时间
  1421. else {
  1422. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1423. device_status.work_status = 0;
  1424. }
  1425. #if LAMP_EN == 1
  1426. //如果当前时间超过20点,未到21点
  1427. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1428. {
  1429. LOG_I("3led:20~21");
  1430. device_status.lamp_status = 1;
  1431. device_status.lamp1_status = 0;
  1432. device_status.lamp2_status = 0;
  1433. }
  1434. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1435. LOG_I("3led:21~22");
  1436. device_status.lamp1_status = 1;
  1437. device_status.lamp_status = 0;
  1438. device_status.lamp2_status = 0;
  1439. }
  1440. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1441. LOG_I("3led:22~23");
  1442. device_status.lamp2_status = 1;
  1443. device_status.lamp_status = 0;
  1444. device_status.lamp1_status =0;
  1445. }
  1446. #endif
  1447. }
  1448. //如果结束时间是第二天
  1449. else if (device_status.startBar>device_status.stopBar) {
  1450. time_t now;
  1451. time(&now);
  1452. static struct tm nowtime; //现在
  1453. static struct tm starttime; //开始
  1454. static struct tm endtime; //结束
  1455. //获取当前时间戳
  1456. nowtime =*localtime(&now);
  1457. nowtime.tm_hour = nowtime.tm_hour-8;
  1458. //获取定时开始的时间戳
  1459. starttime = *localtime(&now);
  1460. starttime.tm_hour = device_status.startBar-8;
  1461. starttime.tm_min = 0;
  1462. starttime.tm_sec = 0;
  1463. //获取定时结束的时间戳
  1464. endtime = *localtime(&now);
  1465. endtime.tm_hour = device_status.stopBar-8;
  1466. endtime.tm_min = 0;
  1467. endtime.tm_sec = 0;
  1468. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1469. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1470. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1471. {
  1472. //获取定时结束的时间戳
  1473. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1474. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1475. endtime.tm_min = 0;
  1476. endtime.tm_sec = 0;
  1477. device_status.work_status = 1;
  1478. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1479. }
  1480. //当前时间小于开始时间
  1481. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1482. {
  1483. //获取定时结束的时间戳
  1484. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1485. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1486. endtime.tm_min = 0;
  1487. endtime.tm_sec = 0;
  1488. //如果当前时间戳小于结束时间戳
  1489. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1490. {
  1491. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1492. device_status.work_status = 1;
  1493. }
  1494. else
  1495. {
  1496. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1497. device_status.work_status = 0; //不在工作时间,待机
  1498. }
  1499. }
  1500. }
  1501. }
  1502. else {
  1503. LOG_I("光控定时模式!");
  1504. /*
  1505. time_t now;
  1506. time(&now);
  1507. static struct tm nowstamp;
  1508. nowstamp =*localtime(&now);
  1509. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1510. static rt_uint32_t nowstamp_num = 0;
  1511. nowstamp_num = mktime(&nowstamp);
  1512. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1513. */
  1514. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1515. {
  1516. device_status.light_over_tim--;
  1517. light_timeout = 3600;
  1518. #if LAMP_EN == 1
  1519. Led_Check(device_status.sunBar);
  1520. #else
  1521. device_status.work_status = 1;;
  1522. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1523. #endif
  1524. }
  1525. else
  1526. {
  1527. light_timeout --;
  1528. //没有在测试并且没有在自检
  1529. if(device_status.test_flag==0&&device_status.selfcheck_ws==0)
  1530. {
  1531. ctr_lamp(OFF,1);
  1532. #if LAMP_EN == 1
  1533. ctr_lamp(OFF,2);
  1534. ctr_lamp(OFF,3);
  1535. #endif
  1536. }
  1537. device_status.work_status = 0;
  1538. if(light_timeout==0) device_status.light_over_tim = 0; //持续1小时白天,清除光控计时,20250519
  1539. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1540. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1541. }
  1542. }
  1543. rt_thread_delay(1000);
  1544. }
  1545. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1546. device_status.work_status = 0;
  1547. rt_thread_delay(1000);
  1548. }
  1549. static uint8_t sleep_cnt = 0;
  1550. //拍照性诱工作任务流程
  1551. void WorkTask_XYV3(void)
  1552. {
  1553. int cal_n = device_status.cul_time * 60;
  1554. LOG_I("工作状态:%d",device_status.work_status);
  1555. LOG_I("开关机状态:%d",device_status.ds);
  1556. while(device_status.work_status==1&&device_status.bat_status==0&&device_status.ds==1&&device_status.temp_status==0)
  1557. {
  1558. //高压板通电
  1559. ctr_lamp(ON, 1); //清扫-DC12v电机正反转
  1560. //光栅传感器通电
  1561. ctr_photo(ON);
  1562. //拍照(?触发条件)计数?没有虫呢?定时
  1563. while(1){
  1564. cal_n--;
  1565. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  1566. if(cal_n==0){
  1567. //自动进入省电模式
  1568. device_status.sleep_mode = 1;
  1569. //发送拍照计数指令
  1570. device_status.photo_ws = 2;
  1571. cal_n = device_status.cul_time * 60;
  1572. }
  1573. if(device_status.sleep_mode>0){
  1574. device_status.cul_time = 120;
  1575. if(cal_n==60){
  1576. ctr_router(ON);
  1577. ctr_RTU_DTU(ON);
  1578. }
  1579. if(cal_n==7000){
  1580. ctr_router(OFF);
  1581. ctr_RTU_DTU(OFF);
  1582. }
  1583. }
  1584. if(device_status.sleep_mode==10)
  1585. {
  1586. LOG_I("倒计时关闭联网模块电源:%d",sleep_cnt);
  1587. sleep_cnt++;
  1588. if(sleep_cnt>=60)
  1589. {
  1590. sleep_cnt = 0;
  1591. ctr_router(OFF);
  1592. ctr_RTU_DTU(OFF);
  1593. device_status.sleep_mode = 1;
  1594. }
  1595. }
  1596. if(device_status.temp_status==1 || device_status.bat_status==1 ||device_status.work_status==0) break;
  1597. rt_thread_delay(1000);
  1598. }
  1599. }
  1600. ctr_lamp(OFF, 1);
  1601. ctr_photo(OFF);
  1602. }
  1603. /* 入口函数-工作状态检测 */
  1604. static void Work_status_thread_entry(void *parameter)
  1605. {
  1606. static uint16_t sun_status_num = 120;
  1607. //如果是光控定时,进行光控监测的消抖
  1608. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1609. {
  1610. rt_kprintf("光控定时!!!\n");
  1611. while(sun_status_num!=0)
  1612. {
  1613. sun_status_num--;
  1614. rt_thread_delay(1000);
  1615. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1616. }
  1617. device_status.light_over_tim = device_status.sunBar * 3600;
  1618. device_status.test_flag = 0;
  1619. }
  1620. while(1)
  1621. {
  1622. WorkCheck();
  1623. }
  1624. }
  1625. /*入口函数-工作流程*/
  1626. static void Work_Loop_thread_entry(void *parameter)
  1627. {
  1628. rt_thread_delay(1000);
  1629. ResetInit();
  1630. while(1)
  1631. {
  1632. WorkTask_XYV3();
  1633. rt_thread_delay(1000);
  1634. }
  1635. }
  1636. /*入口函数清扫*/
  1637. static void Control_Clear_thread_entry(void *parameter)
  1638. {
  1639. static int selfcheck_cnt = 0;
  1640. while(1)
  1641. {
  1642. if(device_status.selfcheck_ws==1){
  1643. selfcheck_cnt++;
  1644. }else{
  1645. selfcheck_cnt = 0;
  1646. }
  1647. if(selfcheck_cnt>1200) //超过3分钟
  1648. {
  1649. device_status.selfcheck_ws = 0;
  1650. device_status.camera_sta = 1; //相机异常
  1651. //上报自检结果
  1652. //上报自检结果
  1653. char *json_temp = makeJson_selfCheck_(1);
  1654. DTU_Write("%s",json_temp);
  1655. free(json_temp);
  1656. }
  1657. if(device_status.clear_status==10){
  1658. //ctr_turn(90,0);
  1659. Motor_shake(1,800);
  1660. device_status.clear_status = 0;
  1661. }
  1662. rt_thread_delay(100);
  1663. }
  1664. }
  1665. /*入口函数加热*/
  1666. //static void Control_Heat_thread_entry(void *parameter)
  1667. //{
  1668. // rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1669. // rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1670. // static int GaoyYa_cnt,GuangShan_cnt = 500; //10s
  1671. // bool GaoYa = false;
  1672. // bool GuangShan = false;
  1673. // while(1)
  1674. // {
  1675. //
  1676. // if(rt_pin_read(SIGNAL_GaoYa)==0)
  1677. // {
  1678. // LOG_I("检测到高压计数!");
  1679. // GaoYa = true;
  1680. // }
  1681. //
  1682. // if(rt_pin_read(SIGNAL_GuangShan)==0)
  1683. // {
  1684. // LOG_I("检测到光栅计数!");
  1685. // GuangShan = true;
  1686. // }
  1687. //
  1688. // if(GaoYa)
  1689. // {
  1690. // GaoyYa_cnt --;
  1691. // if(GaoyYa_cnt==0)
  1692. // {
  1693. // GaoyYa_cnt = 500;
  1694. // GaoYa = false;
  1695. // }
  1696. // }
  1697. //
  1698. // if(GuangShan)
  1699. // {
  1700. // GuangShan_cnt--;
  1701. // if(GuangShan_cnt==0)
  1702. // {
  1703. // GuangShan_cnt = 500;
  1704. // GuangShan = false;
  1705. // }
  1706. // }
  1707. //
  1708. // if(GaoYa&&GuangShan)
  1709. // {
  1710. // device_status.all_ycct += 1; //计数值增加
  1711. // //拍照
  1712. // if(device_status.all_ycct>=1){
  1713. // //device_status.photo_ws = 1;
  1714. // }
  1715. // GaoYa = false;
  1716. // GuangShan = false;
  1717. // GaoyYa_cnt = 500;
  1718. // GuangShan_cnt = 500;
  1719. // rt_thread_delay(1000);
  1720. // }
  1721. // rt_thread_delay(20);
  1722. // }
  1723. //}
  1724. static void Control_Heat_thread_entry(void *parameter) {
  1725. rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1726. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1727. // 修正变量名和初始化
  1728. static int GaoYa_cnt = 500, GuangShan_cnt = 500;
  1729. static bool GaoYa = false, GuangShan = false;
  1730. static int highPressureDuration = 0;
  1731. while (1) {
  1732. // 高压信号去抖动
  1733. if (rt_pin_read(SIGNAL_GaoYa) == 0) {
  1734. LOG_I("高压信号稳定触发");
  1735. GaoYa = true;
  1736. highPressureDuration ++;
  1737. }else{
  1738. highPressureDuration = 0;
  1739. }
  1740. // 光栅信号去抖动(类似逻辑)
  1741. if(rt_pin_read(SIGNAL_GuangShan)==0)
  1742. {
  1743. LOG_I("检测到光栅计数!");
  1744. GuangShan = true;
  1745. }
  1746. /* 高压持续时间监测 */
  1747. if(GaoYa)
  1748. {
  1749. // 持续时间累计(每次循环+1,20ms/次)
  1750. if(highPressureDuration >= 50)
  1751. {
  1752. LOG_E("高压持续超过5秒!");
  1753. // 执行关闭高压操作(示例函数)
  1754. ctr_lamp(OFF, 1);
  1755. // 可选:自动复位保护
  1756. rt_thread_mdelay(10000); // 等待2秒
  1757. ctr_lamp(ON, 1);
  1758. highPressureDuration = 0;
  1759. }
  1760. }
  1761. // 计数器逻辑
  1762. if (GaoYa) {
  1763. if (--GaoYa_cnt <= 0) {
  1764. GaoYa = false;
  1765. GaoYa_cnt = 500;
  1766. }
  1767. }
  1768. if (GuangShan) {
  1769. if (--GuangShan_cnt <= 0) {
  1770. GuangShan = false;
  1771. GuangShan_cnt = 500;
  1772. }
  1773. }
  1774. // 触发拍照逻辑
  1775. if (GaoYa && GuangShan) {
  1776. device_status.all_ycct++;
  1777. GaoYa = GuangShan = false;
  1778. GaoYa_cnt = GuangShan_cnt = 500;
  1779. }
  1780. rt_thread_delay(20); // 保持20ms周期
  1781. }
  1782. }
  1783. /*清扫线程*/
  1784. int Control_clear(void)
  1785. {
  1786. /* 创建线程*/
  1787. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1788. Control_Clear_thread_entry, RT_NULL,
  1789. 2048, //2048
  1790. 24, //23
  1791. WORK_CHECK_THREAD_TIMESLICE); //5
  1792. if(tid_ctr!=RT_NULL)
  1793. {
  1794. rt_thread_startup(tid_ctr);
  1795. }
  1796. return 0;
  1797. }
  1798. /*恒温加热线程*/
  1799. int Control_heat(void)
  1800. {
  1801. /* 创建线程*/
  1802. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  1803. Control_Heat_thread_entry, RT_NULL,
  1804. 1024, //2048
  1805. 25, //23
  1806. WORK_CHECK_THREAD_TIMESLICE); //5
  1807. if(tid_heat!=RT_NULL)
  1808. {
  1809. rt_thread_startup(tid_heat);
  1810. }
  1811. return 0;
  1812. }
  1813. /* 创建线程 -工作状态检测*/
  1814. int Workstatus_check(void)
  1815. {
  1816. /* 创建线程*/
  1817. tid_wkstc = rt_thread_create("work_status_check_thread",
  1818. Work_status_thread_entry, RT_NULL,
  1819. WORK_CHECK_THREAD_STACK_SIZE, //1024
  1820. WORK_CHECK_THREAD_PRIORITY, //23
  1821. WORK_CHECK_THREAD_TIMESLICE); //5
  1822. if(tid_wkstc!=RT_NULL)
  1823. {
  1824. rt_thread_startup(tid_wkstc);
  1825. }
  1826. return 0;
  1827. }
  1828. /*工作流程线程*/
  1829. int Work_Loop_Task(void)
  1830. {
  1831. rt_err_t result_loop = 0;
  1832. /* 创建线程*/
  1833. tid_workloop = rt_thread_create("work_loop_thread",
  1834. Work_Loop_thread_entry, RT_NULL,
  1835. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1836. WORK_LOOP_THREAD_PRIORITY, //12
  1837. WORK_LOOP_THREAD_TIMESLICE); //5
  1838. if(tid_workloop!=RT_NULL)
  1839. {
  1840. result_loop = rt_thread_startup(tid_workloop);
  1841. }
  1842. return result_loop;
  1843. }
  1844. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  1845. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1846. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  1847. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1848. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  1849. //INIT_APP_EXPORT(Control_heat);