control.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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. #define DBG_ENABLE
  19. #define DBG_COLOR
  20. #define DBG_TAG "control"
  21. #define DBG_LEVEL DBG_LOG
  22. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  23. //工作检测
  24. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  25. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  26. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  27. //工作流程
  28. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  29. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  30. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  31. #define PWM_EN 1 //0关闭,1-打开
  32. //设备状态表结构体
  33. Device_Status_typedef device_status;
  34. rt_thread_t tid_workloop = RT_NULL;
  35. rt_thread_t tid_wkstc = RT_NULL;
  36. rt_thread_t tid_ctr = RT_NULL;
  37. rt_thread_t tid_heat = RT_NULL;
  38. /***************************************************************************/
  39. int thread_delete_workloop(void)
  40. {
  41. rt_err_t result;
  42. if(tid_workloop!=RT_NULL)
  43. {
  44. result = rt_thread_delete(tid_workloop);
  45. }
  46. return result;
  47. }
  48. int thread_restart_workloop(void)
  49. {
  50. rt_err_t result;
  51. result = Work_Loop_Task();
  52. return result;
  53. }
  54. /**************************1.0定制2000W*************************************/
  55. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  56. {
  57. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  58. if(status_ws==ON)
  59. {
  60. rt_pin_write(FAN_P,ON);
  61. }
  62. else {
  63. rt_pin_write(FAN_P, OFF);
  64. }
  65. return 0;
  66. }
  67. uint8_t ctr_Fan_IN(uint8_t status_ws)
  68. {
  69. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  70. if(status_ws==ON)
  71. {
  72. rt_pin_write(FAN_I,ON);
  73. }
  74. else {
  75. rt_pin_write(FAN_I, OFF);
  76. }
  77. return 0;
  78. }
  79. /****************************定制色诱*************************************/
  80. /**/
  81. #if Color_EN == 1
  82. uint8_t Read_Color_Key(rt_base_t key_pin)
  83. {
  84. rt_uint32_t sta_s = 0;
  85. rt_pin_mode(key_pin, PIN_MODE_INPUT);
  86. while(rt_pin_read(key_pin)==1)
  87. {
  88. sta_s++;
  89. /*持续的*/
  90. rt_thread_delay(50);
  91. }
  92. while(rt_pin_read(key_pin)==0)
  93. {
  94. rt_thread_delay(50);
  95. if(sta_s>0) break;
  96. }
  97. LOG_W("检测到IO0翻转!!!");
  98. return 1;
  99. }
  100. /*读限位开关状态*/
  101. uint8_t Read_Color_Limit(rt_base_t pin_limit)
  102. {
  103. uint8_t l_sta = 0;
  104. rt_pin_mode(pin_limit, PIN_MODE_INPUT);
  105. l_sta = rt_pin_read(pin_limit);
  106. rt_thread_delay(20);
  107. l_sta = rt_pin_read(pin_limit);
  108. return l_sta;
  109. }
  110. /*色诱板*/
  111. int ctr_Color_board(uint8_t forward,uint16_t time_board)
  112. {
  113. if(forward==CLOSE) //归位
  114. {
  115. if(device_status.cul_time>0)
  116. device_status.cul_time--;
  117. ctr_cangmen_down(OFF);
  118. ctr_cangmen_up(OFF);
  119. rt_thread_delay(1000);
  120. // if(device_status.cul_time>7) device_status.cul_time=7;
  121. // if(device_status.cul_time!=7)
  122. // {
  123. // uint8_t ct = device_status.cul_time;
  124. // while(1)
  125. // {
  126. // Beep_Tip(100);
  127. // rt_thread_delay(100);
  128. // ct--;
  129. // if(ct==0) break;
  130. // }
  131. // //return device_status.cul_time;
  132. // }
  133. if(Read_Color_Limit(LIMIT_RESET)==0)
  134. {
  135. device_status.cul_time = 0;
  136. LOG_W("已在初始位置!");
  137. Beep_Tip(1000);
  138. return 0;
  139. //黄板已经在复位状态,直接返回0;
  140. }
  141. ctr_cangmen_up(CLOSE);
  142. rt_thread_delay(20000);
  143. ctr_cangmen_up(OFF);
  144. // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
  145. // {
  146. // LOG_W("第七天!!!");
  147. // ctr_cangmen_down(CLOSE);
  148. // rt_thread_delay(500);
  149. // }
  150. LOG_W("先打开运行到限位的右侧!");
  151. while(Read_Color_Limit(LIMIT_BOARD)==0)
  152. {
  153. LOG_W("归位:到达限位中间");
  154. ctr_cangmen_down(CLOSE);
  155. if(Read_Color_Limit(LIMIT_RESET)==0)
  156. {
  157. ctr_cangmen_down(OFF);
  158. break;
  159. }
  160. }
  161. //先打开运行到限位的右侧
  162. while(Read_Color_Limit(LIMIT_BOARD)==1)
  163. {
  164. LOG_W("归位:运行!");
  165. ctr_cangmen_down(CLOSE);
  166. if(Read_Color_Limit(LIMIT_RESET)==0)
  167. {
  168. ctr_cangmen_down(OFF);
  169. break;
  170. }
  171. }
  172. while(Read_Color_Limit(LIMIT_BOARD)==0) //到达限位中间 继续运行
  173. {
  174. LOG_W("归位:到达限位中间");
  175. ctr_cangmen_down(CLOSE);
  176. if(Read_Color_Limit(LIMIT_RESET)==0)
  177. {
  178. ctr_cangmen_down(OFF);
  179. break;
  180. }
  181. }
  182. if(Read_Color_Limit(LIMIT_RESET)==0) return 0;
  183. LOG_W("到达限位左侧!");
  184. //停止电机
  185. ctr_cangmen_down(OFF);
  186. rt_thread_delay(2000);
  187. //将推杆电机归位
  188. LOG_D("*推杆电机归位*");
  189. /*推杆电机伸出*/
  190. ctr_cangmen_up(OPEN);
  191. rt_thread_delay(20000);
  192. ctr_cangmen_up(OFF);
  193. return 2;
  194. }
  195. else if(forward==OPEN)
  196. {
  197. ctr_cangmen_down(OFF);
  198. ctr_cangmen_up(OFF);
  199. rt_thread_delay(1000);
  200. if(device_status.cul_time==7)
  201. {
  202. Beep_Tip(5000);
  203. return 7;
  204. }
  205. LOG_W("打开推杆电机!");
  206. //2023-04-18:add
  207. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  208. {
  209. LOG_D("*推杆电机归位*");
  210. /*推杆电机归位*/
  211. ctr_cangmen_up(CLOSE);
  212. rt_thread_delay(20000);
  213. ctr_cangmen_up(OFF);
  214. }
  215. if(device_status.cul_time<7)
  216. device_status.cul_time++;
  217. while(Read_Color_Limit(LIMIT_BOARD)==0)
  218. {
  219. ctr_cangmen_down(forward);
  220. rt_thread_delay(50);
  221. }
  222. while(Read_Color_Limit(LIMIT_BOARD)==1)
  223. {
  224. time_board--;
  225. if(time_board==0) break;
  226. ctr_cangmen_down(forward);
  227. rt_thread_delay(50);
  228. }
  229. ctr_cangmen_down(OFF);
  230. rt_thread_delay(1000);
  231. ctr_cangmen_up(OPEN);
  232. rt_thread_delay(20000);
  233. ctr_cangmen_up(OFF);
  234. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  235. return OPEN;
  236. }
  237. else if (forward==RESET) {
  238. ctr_cangmen_up(OFF);
  239. ctr_cangmen_down(OFF);
  240. if(Read_Color_Limit(LIMIT_RESET)==0)
  241. {
  242. device_status.cul_time = 0;
  243. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  244. LOG_W("已在初始位置!");
  245. Beep_Tip(1000);
  246. ctr_cangmen_up(CLOSE);
  247. rt_thread_delay(20000);
  248. ctr_cangmen_up(OFF);
  249. device_status.cul_time = 0;
  250. return 0;
  251. //黄板已经在复位状态,直接返回0;
  252. }
  253. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  254. {
  255. LOG_D("*推杆电机归位*");
  256. /*推杆电机归位*/
  257. ctr_cangmen_up(CLOSE);
  258. rt_thread_delay(20000);
  259. ctr_cangmen_up(OFF);
  260. }
  261. while(Read_Color_Limit(LIMIT_RESET)==1)
  262. {
  263. time_board--;
  264. if(time_board==0) break;
  265. ctr_cangmen_down(CLOSE);
  266. rt_thread_delay(20);
  267. }
  268. device_status.cul_time = 0;
  269. ctr_cangmen_down(OFF);
  270. }
  271. // device_status.cul_time = 0;
  272. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  273. return RESET; //复位返回0
  274. }
  275. #endif
  276. /****************************5.0***********************************/
  277. void ctr_turnv5(void)
  278. {
  279. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  280. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  281. // 转出
  282. rt_pin_write(CLEAR_R,ON);
  283. rt_pin_write(CLEAR_L,OFF);
  284. rt_thread_mdelay(300);
  285. //停止
  286. rt_pin_write(CLEAR_R,OFF);
  287. rt_pin_write(CLEAR_L,OFF);
  288. rt_thread_mdelay(300);
  289. //转入
  290. rt_pin_write(CLEAR_R,OFF);
  291. rt_pin_write(CLEAR_L,ON);
  292. rt_thread_mdelay(300);
  293. //停止
  294. rt_pin_write(CLEAR_R,OFF);
  295. rt_pin_write(CLEAR_L,OFF);
  296. rt_thread_mdelay(300);
  297. }
  298. /**************************机器人********************************/
  299. void ctr_Motor_Robot(uint8_t status_ws)
  300. {
  301. unsigned int close_timeout = 0;
  302. unsigned int open_timeout = 0;
  303. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  304. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  305. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  306. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  307. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  308. rt_pin_write(Motor_Power,OFF);
  309. rt_pin_write(Motor_Direction_Ctr,ON);
  310. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  311. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  312. if(status_ws == ON)
  313. {
  314. //检测按键状态
  315. while(rt_pin_read(Motor_Direction_Button)==1)
  316. {
  317. if(rt_pin_read(LIMIT_CLOSE)==1)
  318. {
  319. rt_pin_write(Motor_Direction_Ctr,ON);
  320. close_timeout++;
  321. //打开电机驱动器电源,默认关闭
  322. rt_pin_write(Motor_Power,ON);
  323. if(close_timeout==2)
  324. {
  325. rt_pin_write(CLEAR_R,ON);
  326. rt_pin_write(CLEAR_L,OFF);
  327. rt_pin_write(Motor_Power,OFF);
  328. rt_thread_delay(20);
  329. rt_pin_write(Motor_Power,ON);
  330. }
  331. rt_thread_delay(20);
  332. if(close_timeout>500)
  333. {
  334. close_timeout = 600;
  335. rt_pin_write(Motor_Power,OFF);
  336. rt_pin_write(CLEAR_R,OFF);
  337. rt_pin_write(CLEAR_L,OFF);
  338. }
  339. }
  340. else
  341. {
  342. close_timeout = 0;
  343. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  344. rt_pin_write(CLEAR_R,OFF);
  345. rt_pin_write(CLEAR_L,OFF);
  346. }
  347. rt_thread_delay(20);
  348. }
  349. rt_pin_write(Motor_Power,OFF);
  350. rt_thread_delay(1000*2);
  351. while(rt_pin_read(Motor_Direction_Button)==0)
  352. {
  353. if(rt_pin_read(LIMIT_OPEN)==1)
  354. {
  355. open_timeout ++;
  356. //打开电机驱动器电源,默认关闭
  357. if(open_timeout==2){
  358. rt_pin_write(CLEAR_R,OFF);
  359. rt_pin_write(CLEAR_L,ON);
  360. }
  361. rt_pin_write(Motor_Power,ON);
  362. rt_pin_write(Motor_Direction_Ctr,OFF);
  363. if(open_timeout>500)
  364. {
  365. open_timeout = 600;
  366. rt_pin_write(Motor_Power,OFF);
  367. rt_pin_write(CLEAR_R,OFF);
  368. rt_pin_write(CLEAR_L,OFF);
  369. }
  370. rt_thread_delay(20);
  371. }
  372. if(rt_pin_read(LIMIT_OPEN)==0)
  373. {
  374. open_timeout = 0;
  375. rt_pin_write(Motor_Power,OFF);
  376. rt_pin_write(CLEAR_R,OFF);
  377. rt_pin_write(CLEAR_L,OFF);
  378. }
  379. rt_thread_delay(20);
  380. close_timeout = 0;
  381. }
  382. open_timeout = 0;
  383. close_timeout = 0;
  384. }
  385. else {
  386. rt_pin_write(Motor_Power,OFF);
  387. }
  388. }
  389. /*************************吸虫塔*********************************/
  390. //吸虫电机
  391. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  392. {
  393. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  394. if(status_ws==ON)
  395. {
  396. rt_pin_write(MOTOR1_CTR_PIN,ON);
  397. }
  398. else {
  399. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  400. }
  401. return 0;
  402. }
  403. uint8_t ctr_Camera_Power(uint8_t status_ws)
  404. {
  405. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  406. if(status_ws==ON)
  407. {
  408. rt_pin_write(MOTOR1_CTR_PIN,ON);
  409. }
  410. else {
  411. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  412. }
  413. return 0;
  414. }
  415. //吸附电机
  416. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  417. {
  418. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  419. if(status_ws==ON)
  420. {
  421. rt_pin_write(MOTOR2_CTR_PIN,ON);
  422. }
  423. else {
  424. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  425. }
  426. return 0;
  427. }
  428. //清扫-卷带电机
  429. void ctr_JD_Clear(uint8_t shake_sw)
  430. {
  431. rt_uint16_t turn_timeout = 0;
  432. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  433. ctr_cangmen_up(OFF); //清扫传送带的电机
  434. if(shake_sw==ON)
  435. {
  436. ctr_Heat(ON); //清洁风机
  437. Motor_shake(ON, 0);
  438. ctr_cangmen_up(OPEN); //打开清扫
  439. }
  440. else {
  441. Motor_shake(OFF, 0);
  442. ctr_cangmen_up(OPEN); //打开清扫
  443. }
  444. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  445. {
  446. Motor_Juandai(ON);
  447. rt_thread_delay(50);
  448. turn_timeout++;
  449. if(turn_timeout==1000) break;
  450. }
  451. turn_timeout = 0;
  452. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  453. {
  454. Motor_Juandai(ON);
  455. turn_timeout++;
  456. rt_thread_delay(50);
  457. if(turn_timeout==1000) break;
  458. }
  459. Motor_shake(OFF, 1);
  460. Motor_Juandai(OFF);
  461. ctr_cangmen_up(OFF); //关闭清扫
  462. ctr_Heat(OFF);
  463. }
  464. //工作任务流程
  465. void WorkTask_XCT(void)
  466. {
  467. while(device_status.work_status==1&&device_status.ds==1)
  468. {
  469. ctr_Motor_xifu(ON);
  470. //打开相机供电
  471. ctr_Camera_Power(ON);
  472. //ctr_Heat(ON); //清洁风机
  473. uint32_t photo_fre = 0;
  474. photo_fre = device_status.dattim * 60;
  475. while(photo_fre!=0)
  476. {
  477. photo_fre --;
  478. rt_thread_delay(1000);
  479. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  480. }
  481. //卷带
  482. ctr_JD_Clear(OFF);
  483. //拍照
  484. HK_TakePhoto();
  485. //关闭吸附风机
  486. ctr_Motor_xifu(OFF);
  487. //关闭相机供电
  488. //ctr_Heat(OFF);
  489. ctr_Camera_Power(OFF);
  490. //卷带
  491. ctr_JD_Clear(ON);
  492. // rt_thread_delay(1000);
  493. // ctr_JD_Clear(ON);
  494. //震动
  495. Motor_shake(ON, device_status.shake_sec);
  496. }
  497. //关闭风机
  498. ctr_Motor_xichong(OFF);
  499. ctr_Motor_xifu(OFF);
  500. LOG_I("不在工作时间,未工作!!!");
  501. }
  502. /**********************************************************************/
  503. /*光控*/
  504. uint8_t In_Light_status(void)
  505. {
  506. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  507. return rt_pin_read(LIGHT_PIN);
  508. }
  509. /*雨控*/
  510. uint8_t In_Rain_status(void)
  511. {
  512. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  513. return rt_pin_read(RAIN_PIN);
  514. }
  515. /*加热*/
  516. uint8_t ctr_Heat(uint8_t status_heat)
  517. {
  518. device_status.heat_status = status_heat;
  519. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  520. rt_pin_write(Heat_Pin,status_heat);
  521. return status_heat;
  522. }
  523. /*制冷*/
  524. uint8_t ctr_Cold(uint8_t status_cold)
  525. {
  526. device_status.cold_status = status_cold;
  527. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  528. rt_pin_write(Cold_Pin,status_cold);
  529. return status_cold;
  530. }
  531. /*相机供电*/
  532. void ctr_photo(uint8_t status_photo)
  533. {
  534. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  535. rt_pin_write(PHOTO,status_photo);
  536. }
  537. /*************************************************
  538. Function: ctr_Cool_Fan
  539. Description: 散热风机的开关
  540. Calls:
  541. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  542. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  543. Input: ON-开,OFF-关
  544. Output: NULL
  545. Return: ON-开,OFF-关
  546. Others: NULL
  547. *************************************************/
  548. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  549. {
  550. device_status.cool_fan_status = status_cool;
  551. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  552. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  553. if(status_cool==OFF)
  554. {
  555. rt_pin_write(Cool_Fan_L,OFF);
  556. rt_pin_write(Cool_Fan_R,OFF);
  557. }
  558. if(status_cool==ON)
  559. {
  560. rt_pin_write(Cool_Fan_L,ON);
  561. rt_pin_write(Cool_Fan_R,OFF);
  562. }
  563. return device_status.cool_fan_status;
  564. }
  565. /*************************************************
  566. Function: ctr_Collect_Fan
  567. Description: 采集风机的开关
  568. Calls:
  569. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  570. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  571. Input: ON-开,OFF-关
  572. Output: NULL
  573. Return: ON-开,OFF-关
  574. Others: NULL
  575. *************************************************/
  576. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  577. {
  578. device_status.collect_fan_status = status_collect;
  579. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  580. if(status_collect==OFF)
  581. {
  582. rt_pin_write(Collect_Fan,OFF);
  583. }
  584. if(status_collect==ON)
  585. {
  586. rt_pin_write(Collect_Fan,ON);
  587. }
  588. return device_status.collect_fan_status;
  589. }
  590. /*************************************************
  591. Function: ctr_develop
  592. Description: 培养载玻片孢子
  593. Calls:
  594. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  595. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  596. Input: v=0(复位) v=90转90度
  597. Output: NULL
  598. Return: NULL
  599. Others: NULL
  600. *************************************************/
  601. /*上仓门*/
  602. uint8_t ctr_cangmen_up(uint8_t status_up)
  603. {
  604. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  605. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  606. if(status_up==OFF)
  607. {
  608. rt_pin_write(Collect_fan_R,ON);
  609. rt_pin_write(Collect_fan_L,ON);
  610. device_status.ctr_up_motor = 0;
  611. rt_thread_delay(1000);
  612. }
  613. if(status_up==OPEN)
  614. {
  615. device_status.up_status = 1;
  616. rt_pin_write(Collect_fan_R,OFF);
  617. rt_pin_write(Collect_fan_L,OFF);
  618. rt_pin_write(Collect_fan_R,ON);
  619. rt_pin_write(Collect_fan_L,OFF);
  620. }
  621. if(status_up==CLOSE)
  622. {
  623. device_status.up_status = 0;
  624. rt_pin_write(Collect_fan_R,OFF);
  625. rt_pin_write(Collect_fan_L,OFF);
  626. rt_pin_write(Collect_fan_R,OFF);
  627. rt_pin_write(Collect_fan_L,ON);
  628. }
  629. return device_status.up_status;
  630. }
  631. //下仓门
  632. uint8_t ctr_cangmen_down(uint8_t status_down)
  633. {
  634. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  635. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  636. if(status_down==OFF)
  637. {
  638. rt_pin_write(C_DOWN_R,ON);
  639. rt_pin_write(C_DOWN_L,ON);
  640. device_status.ctr_down_motor = 0;
  641. rt_thread_delay(1000);
  642. }
  643. if(status_down==OPEN)
  644. {
  645. device_status.down_status = 1;
  646. rt_pin_write(C_DOWN_R,OFF);
  647. rt_pin_write(C_DOWN_L,OFF);
  648. rt_pin_write(C_DOWN_R,ON);
  649. rt_pin_write(C_DOWN_L,OFF);
  650. }
  651. if(status_down==CLOSE)
  652. {
  653. device_status.down_status = 0;
  654. rt_pin_write(C_DOWN_R,OFF);
  655. rt_pin_write(C_DOWN_L,OFF);
  656. rt_pin_write(C_DOWN_R,OFF);
  657. rt_pin_write(C_DOWN_L,ON);
  658. }
  659. return device_status.down_status;
  660. }
  661. //镇流器
  662. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  663. {
  664. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  665. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  666. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  667. if(status_lamp==ON)
  668. {
  669. switch(lamp_num){
  670. case 1:
  671. rt_pin_write(LAMP_SW,ON);
  672. device_status.lamp_status = status_lamp;
  673. break;
  674. case 2:
  675. rt_pin_write(LAMP_1_SW,ON);
  676. device_status.lamp_status = status_lamp;
  677. break;
  678. case 3:
  679. rt_pin_write(LAMP_2_SW,ON);
  680. device_status.lamp_status = status_lamp;
  681. break;
  682. default:
  683. break;
  684. }
  685. }
  686. else {
  687. switch(lamp_num){
  688. case 1:
  689. rt_pin_write(LAMP_SW,OFF);
  690. device_status.lamp_status = status_lamp;
  691. break;
  692. case 2:
  693. rt_pin_write(LAMP_1_SW,OFF);
  694. device_status.lamp1_status = status_lamp;
  695. break;
  696. case 3:
  697. rt_pin_write(LAMP_2_SW,OFF);
  698. device_status.lamp2_status = status_lamp;
  699. break;
  700. default:
  701. break;
  702. }
  703. }
  704. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  705. return device_status.lamp_status;
  706. }
  707. //震动电机
  708. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  709. {
  710. static rt_uint16_t shake_i = 0;
  711. shake_i = atoi(device_status.power);
  712. LOG_I("震动电机待机电流:%d",shake_i);
  713. if(device_status.dtype==7&&shake_sec!=0)
  714. DTU_Write("drop");
  715. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  716. #if 0
  717. rt_pin_write(SHAKE_SW,ON);
  718. return 0;
  719. #endif
  720. if(status_shake==ON&&device_status.shake_ws==1)
  721. {
  722. #if 0
  723. for(int8_t s = 0;s++;s<2)
  724. {
  725. rt_pin_write(SHAKE_SW,ON);
  726. rt_thread_delay(100);
  727. rt_pin_write(SHAKE_SW,OFF);
  728. rt_thread_delay(1000);
  729. }
  730. #endif
  731. rt_pin_write(SHAKE_SW,ON);
  732. if(device_status.dtype==3)
  733. {
  734. while(shake_sec--)
  735. {
  736. rt_thread_delay(100);
  737. 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)
  738. {
  739. char * shake_warn = makeJson_Warn("电流不足", "震动电机");
  740. DTU_Write("%s",shake_warn);
  741. rt_free(shake_warn);
  742. }
  743. LOG_D("震动电机:%d",atoi(device_status.power));
  744. }
  745. }
  746. if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
  747. rt_pin_write(SHAKE_SW,OFF);
  748. HMI_WRITE("cangWei.switchShake.val=%d",0);
  749. }
  750. else
  751. {
  752. rt_pin_write(SHAKE_SW,OFF);
  753. HMI_WRITE("cangWei.switchShake.val=%d",0);
  754. }
  755. if(device_status.dtype==7)
  756. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  757. return status_shake;
  758. }
  759. //补光
  760. uint8_t ctr_led(uint8_t status_led)
  761. {
  762. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  763. if(device_status.version_type==6)
  764. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  765. if(status_led==ON)
  766. {
  767. rt_pin_write(LED_SW,ON);
  768. if(device_status.version_type==6)
  769. rt_pin_write(LED_P,ON);
  770. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  771. }
  772. else if(status_led==OFF){
  773. rt_pin_write(LED_SW,OFF);
  774. if(device_status.version_type==6)
  775. rt_pin_write(LED_P,OFF);
  776. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  777. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  778. }
  779. return status_led;
  780. }
  781. /*************************************************
  782. Function: ctr_turn
  783. Description: 转盘驱动
  784. Calls:
  785. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  786. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  787. Input: v=0(复位) v=90转90度
  788. Output: NULL
  789. Return: NULL
  790. Others: NULL
  791. *************************************************/
  792. void ctr_turn(uint8_t v,uint8_t sta)
  793. {
  794. DTU_Write("turn.mp3");
  795. LOG_I("================v===============:%d",v);
  796. rt_uint16_t turn_cnt = 0;
  797. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  798. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  799. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  800. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  801. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  802. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  803. {
  804. rt_pin_write(TURN_ZP_R, ON);
  805. rt_pin_write(TURN_ZP_L, OFF);
  806. //Motor_shake(ON,0);
  807. turn_cnt ++;
  808. rt_thread_delay(100);
  809. if(turn_cnt==1000)
  810. {
  811. turn_cnt = 0;
  812. rt_pin_write(TURN_ZP_R, OFF);
  813. rt_pin_write(TURN_ZP_L, OFF);
  814. //Motor_shake(OFF,0);
  815. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  816. DTU_Write("%s",turn_warn);
  817. rt_free(turn_warn);
  818. break;
  819. }
  820. }
  821. turn_cnt = 0;
  822. while(v==0&&rt_pin_read(LIMIT_0)==1)
  823. {
  824. rt_pin_write(TURN_ZP_R, ON);
  825. rt_pin_write(TURN_ZP_L, OFF);
  826. Motor_shake(ON,0);
  827. rt_thread_delay(200); //10 to 100
  828. turn_cnt ++;
  829. if(turn_cnt==300) //6000 to 600
  830. {
  831. turn_cnt = 0;
  832. // rt_pin_write(TURN_ZP_R, OFF);
  833. // rt_pin_write(TURN_ZP_L, OFF);
  834. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  835. // DTU_Write("%s",turn_warn);
  836. // rt_free(turn_warn);
  837. break;
  838. }
  839. }
  840. while(v==0&&rt_pin_read(LIMIT_0)==1)
  841. {
  842. rt_pin_write(TURN_ZP_R, ON);
  843. rt_pin_write(TURN_ZP_L, OFF);
  844. //Motor_shake(ON,0);
  845. rt_thread_delay(50); //10 to 50
  846. turn_cnt ++;
  847. if(turn_cnt==10) //6000 to 1200
  848. {
  849. turn_cnt = 0;
  850. rt_pin_write(TURN_ZP_R, OFF);
  851. rt_pin_write(TURN_ZP_L, OFF);
  852. //Motor_shake(OFF,0);
  853. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  854. DTU_Write("%s",turn_warn);
  855. rt_free(turn_warn);
  856. break;
  857. }
  858. }
  859. turn_cnt = 0;
  860. while(v==90&&rt_pin_read(LIMIT_90)==1)
  861. {
  862. rt_pin_write(TURN_ZP_R, ON);
  863. rt_pin_write(TURN_ZP_L, OFF);
  864. //Motor_shake(ON,0);
  865. rt_thread_delay(200);
  866. turn_cnt ++;
  867. if(turn_cnt==150)
  868. {
  869. turn_cnt = 0;
  870. // rt_pin_write(TURN_ZP_R, OFF);
  871. // rt_pin_write(TURN_ZP_L, OFF);
  872. // char *turn_warn =makeJson_Warn("超时", "转盘");
  873. // DTU_Write(turn_warn);
  874. // rt_free(turn_warn);
  875. break;
  876. }
  877. }
  878. while(v==90&&rt_pin_read(LIMIT_90)==1)
  879. {
  880. // rt_pin_write(TURN_ZP_R, ON);
  881. // rt_pin_write(TURN_ZP_L, OFF);
  882. Motor_shake(ON,0);
  883. rt_thread_delay(50);
  884. turn_cnt ++;
  885. if(turn_cnt==10)
  886. {
  887. turn_cnt = 0;
  888. rt_pin_write(TURN_ZP_R, OFF);
  889. rt_pin_write(TURN_ZP_L, OFF);
  890. //Motor_shake(OFF,0);
  891. char *turn_warn =makeJson_Warn("超时", "转盘");
  892. DTU_Write(turn_warn);
  893. rt_free(turn_warn);
  894. break;
  895. }
  896. }
  897. if(sta!=0)
  898. {
  899. //保存位置
  900. device_status.run_sta = sta;
  901. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  902. }
  903. turn_cnt = 0;
  904. rt_pin_write(TURN_ZP_R, OFF);
  905. rt_pin_write(TURN_ZP_L, OFF);
  906. Motor_shake(OFF,0);
  907. HMI_WRITE("cangWei.rotateFlag.val=0");
  908. }
  909. /*************************************************
  910. Function: ctr_Motor_Driver
  911. Description: 步进电机驱动
  912. Calls:
  913. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  914. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  915. Input: 50000000
  916. Output: NULL
  917. Return: shake_sec
  918. Others: NULL
  919. *************************************************/
  920. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  921. {
  922. beep_init();
  923. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  924. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  925. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  926. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  927. if(EN==0)
  928. {
  929. rt_pin_write(Motor_Power,0);
  930. }
  931. else{
  932. rt_pin_write(Motor_EN, EN);
  933. }
  934. rt_pin_write(Motor_DIR, DIR);
  935. beep_set(pwm, pwm/2); //200kzh - 50%
  936. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  937. {
  938. if(time_out==0)
  939. {
  940. break;
  941. }
  942. rt_thread_delay(50);
  943. time_out --;
  944. }
  945. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  946. {
  947. if(time_out==0)
  948. {
  949. break;
  950. }
  951. rt_thread_delay(50);
  952. time_out --;
  953. }
  954. rt_pin_write(Motor_EN, OFF);
  955. if(device_status.autotakepic==0)
  956. ctr_lamp(OFF, 1);
  957. beep_set(0, 0); //200kzh - 50%
  958. }
  959. /*************************************************
  960. Function: ctr_Petroleum_jelly
  961. Description: 涂凡士林
  962. Calls:
  963. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  964. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  965. Input: NULL
  966. Output: NULL
  967. Return: NULL
  968. Others: NULL
  969. *************************************************/
  970. void ctr_Petroleum_jelly(rt_uint32_t delay)
  971. {
  972. beep_init();
  973. pwm_on(2,50000);
  974. rt_thread_delay(delay*1000);
  975. pwm_off(2,0);
  976. rt_thread_delay(1000);
  977. pwm_on(3, 50000);
  978. rt_thread_delay(delay*1000);
  979. pwm_off(3,0);
  980. device_status.fsl_sw = 0;
  981. }
  982. //1.0转仓
  983. void turnzp(void)
  984. {
  985. static uint16_t turn_timeout = 0;
  986. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  987. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  988. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  989. // rt_pin_write(CLEAR_R,OFF);
  990. // rt_pin_write(CLEAR_L,OFF);
  991. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  992. {
  993. rt_thread_delay(20);
  994. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  995. {
  996. // rt_pin_write(CLEAR_R,ON);
  997. // rt_pin_write(CLEAR_L,OFF);
  998. Motor_Juandai(ON);
  999. rt_thread_delay(50);
  1000. turn_timeout++;
  1001. if(turn_timeout==600)
  1002. {
  1003. LOG_E("检测不到限位,电机强制停止");
  1004. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1005. DTU_Write("%s",ctr_zp_limit_warn);
  1006. rt_free(ctr_zp_limit_warn);
  1007. break;
  1008. }
  1009. }
  1010. break;
  1011. }
  1012. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1013. {
  1014. rt_thread_delay(20);
  1015. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1016. {
  1017. // rt_pin_write(CLEAR_R,ON);
  1018. // rt_pin_write(CLEAR_L,OFF);
  1019. Motor_Juandai(ON);
  1020. rt_thread_delay(50);
  1021. turn_timeout++;
  1022. if(turn_timeout==600)
  1023. {
  1024. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1025. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1026. DTU_Write("%s",ctr_zp_limit_warn);
  1027. rt_free(ctr_zp_limit_warn);
  1028. break;
  1029. }
  1030. }
  1031. break;
  1032. }
  1033. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1034. {
  1035. rt_thread_delay(20);
  1036. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1037. {
  1038. // rt_pin_write(CLEAR_R,OFF);
  1039. // rt_pin_write(CLEAR_L,OFF);
  1040. Motor_Juandai(OFF);
  1041. break;
  1042. }
  1043. else {
  1044. break;
  1045. }
  1046. }
  1047. Motor_Juandai(OFF);
  1048. // rt_pin_write(CLEAR_R,OFF);
  1049. // rt_pin_write(CLEAR_L,OFF);
  1050. turn_timeout = 0;
  1051. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1052. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1053. rt_thread_delay(1000);
  1054. }
  1055. //清扫-常闭型限位开关
  1056. //新结构使用推杆电机做清扫
  1057. uint8_t ctr_clear(uint16_t status_clear)
  1058. {
  1059. static uint16_t clear_num = 0;
  1060. #if PWM_EN == 0
  1061. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1062. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1063. #endif
  1064. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1065. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1066. /*************************************************************外接清扫
  1067. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1068. rt_pin_write(POWER_CLEAR,ON);
  1069. rt_thread_delay(1000*100);
  1070. rt_pin_write(POWER_CLEAR,OFF);
  1071. ************************************************************/
  1072. //停止
  1073. #if PWM_EN == 0
  1074. rt_pin_write(CLEAR_R,OFF);
  1075. rt_pin_write(CLEAR_L,OFF);
  1076. #else
  1077. Motor_Qingsao_R_PWM(OFF);
  1078. Motor_Qingsao_L_PWM(OFF);
  1079. #endif
  1080. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1081. rt_thread_delay(100);
  1082. //如果清扫位置在中间
  1083. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1084. {
  1085. #if PWM_EN == 0
  1086. rt_pin_write(CLEAR_R,ON);
  1087. rt_pin_write(CLEAR_L,OFF);
  1088. #else
  1089. Motor_Qingsao_R_PWM(ON);
  1090. Motor_Qingsao_L_PWM(OFF);
  1091. #endif
  1092. rt_thread_delay(50);
  1093. clear_num++;
  1094. //已到达检测位置
  1095. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1096. {
  1097. #if PWM_EN == 0
  1098. rt_pin_write(CLEAR_R,ON);
  1099. rt_pin_write(CLEAR_L,ON);
  1100. #else
  1101. Motor_Qingsao_R_PWM(OFF);
  1102. Motor_Qingsao_L_PWM(OFF);
  1103. #endif
  1104. rt_thread_delay(2000);
  1105. break;
  1106. }
  1107. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1108. {
  1109. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1110. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1111. #if PWM_EN == 0
  1112. rt_pin_write(CLEAR_R,ON);
  1113. rt_pin_write(CLEAR_L,ON);
  1114. #else
  1115. Motor_Qingsao_R_PWM(OFF);
  1116. Motor_Qingsao_L_PWM(OFF);
  1117. #endif
  1118. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1119. if(Read_Real_Voltage()/100<12)
  1120. {
  1121. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1122. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1123. device_status.clear_status = 0;
  1124. return 0;
  1125. }
  1126. break;
  1127. }
  1128. }
  1129. clear_num = 0;
  1130. //如果清扫位置在左侧
  1131. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1132. {
  1133. #if PWM_EN == 0
  1134. rt_pin_write(CLEAR_R,ON);
  1135. rt_pin_write(CLEAR_L,OFF);
  1136. #else
  1137. Motor_Qingsao_R_PWM(ON);
  1138. Motor_Qingsao_L_PWM(OFF);
  1139. #endif
  1140. rt_thread_delay(50);
  1141. clear_num++;
  1142. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1143. {
  1144. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1145. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1146. #if PWM_EN == 0
  1147. rt_pin_write(CLEAR_R,ON);
  1148. rt_pin_write(CLEAR_L,ON);
  1149. #else
  1150. Motor_Qingsao_R_PWM(OFF);
  1151. Motor_Qingsao_L_PWM(OFF);
  1152. #endif
  1153. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1154. if(Read_Real_Voltage()/100<12)
  1155. {
  1156. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1157. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1158. device_status.clear_status = 0;
  1159. return 0;
  1160. }
  1161. break;
  1162. }
  1163. }
  1164. clear_num = 0;
  1165. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1166. {
  1167. #if PWM_EN == 0
  1168. rt_pin_write(CLEAR_R,ON);
  1169. rt_pin_write(CLEAR_L,ON);
  1170. #else
  1171. Motor_Qingsao_R_PWM(OFF);
  1172. Motor_Qingsao_L_PWM(OFF);
  1173. #endif
  1174. if(Read_Real_Voltage()/100<12)
  1175. {
  1176. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1177. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1178. device_status.clear_status = 0;
  1179. return 0;
  1180. }
  1181. rt_thread_delay(2000);
  1182. break;
  1183. }
  1184. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1185. {
  1186. #if PWM_EN == 0
  1187. rt_pin_write(CLEAR_R,OFF);
  1188. rt_pin_write(CLEAR_L,ON);
  1189. #else
  1190. Motor_Qingsao_R_PWM(OFF);
  1191. Motor_Qingsao_L_PWM(ON);
  1192. #endif
  1193. rt_thread_delay(50);
  1194. clear_num++;
  1195. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1196. {
  1197. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1198. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1199. //DTU_Write("%s",Clear_ERR);
  1200. #if PWM_EN == 0
  1201. rt_pin_write(CLEAR_R,ON);
  1202. rt_pin_write(CLEAR_L,ON);
  1203. #else
  1204. Motor_Qingsao_R_PWM(OFF);
  1205. Motor_Qingsao_L_PWM(OFF);
  1206. #endif
  1207. rt_free(Clear_ERR);
  1208. if(Read_Real_Voltage()/100<12)
  1209. {
  1210. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1211. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1212. device_status.clear_status = 0;
  1213. return 0;
  1214. }
  1215. break;
  1216. }
  1217. }
  1218. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1219. {
  1220. #if PWM_EN == 0
  1221. rt_pin_write(CLEAR_R,ON);
  1222. rt_pin_write(CLEAR_L,ON);
  1223. #else
  1224. Motor_Qingsao_R_PWM(OFF);
  1225. Motor_Qingsao_L_PWM(OFF);
  1226. #endif
  1227. rt_thread_delay(2000);
  1228. break;
  1229. }
  1230. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1231. {
  1232. #if PWM_EN == 0
  1233. rt_pin_write(CLEAR_R,ON);
  1234. rt_pin_write(CLEAR_L,ON);
  1235. #else
  1236. Motor_Qingsao_R_PWM(OFF);
  1237. Motor_Qingsao_L_PWM(OFF);
  1238. #endif
  1239. if(device_status.version_type==41)
  1240. {
  1241. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1242. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1243. //DTU_Write("%s",Clear_ERR);
  1244. rt_free(Clear_ERR);
  1245. }
  1246. return 0;
  1247. }
  1248. clear_num = 0;
  1249. rt_thread_delay(2000);
  1250. #if PWM_EN == 0
  1251. rt_pin_write(CLEAR_R,ON);
  1252. rt_pin_write(CLEAR_L,ON);
  1253. #else
  1254. Motor_Qingsao_R_PWM(OFF);
  1255. Motor_Qingsao_L_PWM(OFF);
  1256. #endif
  1257. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1258. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1259. device_status.clear_status = 0;
  1260. return 1;
  1261. }
  1262. //路由器供电
  1263. void ctr_router(uint8_t status_router)
  1264. {
  1265. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1266. if(device_status.version_type==6)
  1267. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1268. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1269. if(status_router==ON)
  1270. {
  1271. rt_pin_write(ROUTER,ON);
  1272. //
  1273. #if LAMP_EN == 0
  1274. if(device_status.version_type==6)
  1275. rt_pin_write(ROUTER_P,ON);
  1276. #endif
  1277. }
  1278. else {
  1279. rt_pin_write(ROUTER,OFF);
  1280. //
  1281. #if LAMP_EN == 0
  1282. if(device_status.version_type==6)
  1283. rt_pin_write(ROUTER_P,OFF);
  1284. #endif
  1285. }
  1286. }
  1287. //清扫-推杆电机
  1288. void ctr_TG_Clear(uint16_t clear_timeout)
  1289. {
  1290. #if 0
  1291. int current_clear[200];
  1292. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1293. LOG_I("清扫电机待机:%d",Electric_current);
  1294. #endif
  1295. static uint16_t clear_count = 0;
  1296. clear_count = clear_timeout;
  1297. #if PWM_EN == 0
  1298. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1299. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1300. #endif
  1301. while(clear_count!=0)
  1302. {
  1303. #if PWM_EN == 0
  1304. rt_pin_write(CLEAR_R,ON);
  1305. rt_pin_write(CLEAR_L,OFF);
  1306. #else
  1307. Motor_Qingsao_R_PWM(ON);
  1308. Motor_Qingsao_L_PWM(OFF);
  1309. #endif
  1310. clear_count--;
  1311. rt_thread_delay(100);
  1312. }
  1313. #if PWM_EN == 0
  1314. rt_pin_write(CLEAR_R,ON);
  1315. rt_pin_write(CLEAR_L,ON);
  1316. #else
  1317. Motor_Qingsao_R_PWM(OFF);
  1318. Motor_Qingsao_L_PWM(OFF);
  1319. #endif
  1320. rt_thread_delay(3000);
  1321. clear_count = 0;
  1322. while(clear_timeout!=clear_count)
  1323. {
  1324. #if PWM_EN == 0
  1325. rt_pin_write(CLEAR_R,OFF);
  1326. rt_pin_write(CLEAR_L,ON);
  1327. #else
  1328. Motor_Qingsao_R_PWM(OFF);
  1329. Motor_Qingsao_L_PWM(ON);
  1330. #endif
  1331. // if(clear_count<200)
  1332. // current_clear[clear_count] = atoi(device_status.power);
  1333. clear_count++;
  1334. rt_thread_delay(100);
  1335. }
  1336. #if PWM_EN == 0
  1337. rt_pin_write(CLEAR_R,ON);
  1338. rt_pin_write(CLEAR_L,ON);
  1339. #else
  1340. Motor_Qingsao_R_PWM(OFF);
  1341. Motor_Qingsao_L_PWM(OFF);
  1342. #endif
  1343. #if 0
  1344. bubblesort(current_clear,200);
  1345. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1346. {
  1347. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1348. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1349. DTU_Write("%s",ctr_clear_warn);
  1350. rt_free(ctr_clear_warn);
  1351. }
  1352. #endif
  1353. rt_thread_delay(2000);
  1354. device_status.clear_status = 0;
  1355. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1356. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1357. }
  1358. //DTU,RTU供电
  1359. void ctr_RTU_DTU(uint8_t status_rtu)
  1360. {
  1361. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1362. if(status_rtu==ON)
  1363. {
  1364. rt_pin_write(RTU_DTU,ON);
  1365. }
  1366. else
  1367. {
  1368. LOG_W("RTU电源复位!!!");
  1369. rt_pin_write(RTU_DTU,OFF);
  1370. }
  1371. }
  1372. //海康拍照
  1373. void Hk_Take_io(void)
  1374. {
  1375. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1376. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1377. rt_pin_write(HK_TAKE, 1);
  1378. rt_pin_write(HK_IO, 0);
  1379. if(device_status.dtype==3)
  1380. Beep_Tip(1000);
  1381. if(device_status.dtype==7)
  1382. rt_thread_mdelay(1000);
  1383. rt_pin_write(HK_TAKE, 0);
  1384. rt_pin_write(HK_IO, 1);
  1385. }
  1386. void calorstat(uint16_t heat_time)
  1387. {
  1388. //uint8_t heat_up_cnt = 0;
  1389. uint16_t n = 0;
  1390. uint16_t delay_heat = 60;
  1391. FlagStatus heat_flg = RESET;
  1392. device_status.heat_time_min = 0;
  1393. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1394. ctr_Heat(ON);
  1395. // UpLoad_Data(); //开始加热时的第一条数据
  1396. //恒温模式
  1397. for(n=0;n < heat_time;n++)
  1398. {
  1399. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1400. {
  1401. heat_flg = SET;
  1402. ctr_Heat(OFF);
  1403. while(delay_heat!=0)
  1404. {
  1405. delay_heat --;
  1406. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1407. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1408. rt_thread_delay(1000);
  1409. if(device_status.heattem>device_status.run_sta + 1){
  1410. device_status.run_sta = device_status.heattem;
  1411. }
  1412. }
  1413. delay_heat = 60;
  1414. //跳出FOR循环
  1415. n = n + 60;
  1416. }
  1417. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1418. {
  1419. device_status.run_sta = device_status.heattem;
  1420. ctr_Heat(ON);
  1421. }
  1422. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1423. {
  1424. // if(heat_up_cnt==0)
  1425. // {
  1426. // UpLoad_Data(); //加热到设定温度的数据
  1427. // heat_up_cnt = 1;
  1428. // }
  1429. device_status.run_sta = device_status.heattem;
  1430. ctr_Heat(OFF);
  1431. }
  1432. if((n % 60)==0)
  1433. {
  1434. device_status.heat_time_min = device_status.heat_time_min + 1;
  1435. // if(device_status.heat_time_min % 5 == 0)
  1436. // {
  1437. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1438. // }
  1439. }
  1440. //如果触发异常退出加热
  1441. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1442. {
  1443. rt_thread_delay(1000);
  1444. if(device_status.bat_status == 1)
  1445. {
  1446. rt_kprintf("============================触发异常退出加热==========================");
  1447. ctr_lamp(OFF,1);
  1448. ctr_Heat(OFF);
  1449. break;
  1450. }
  1451. }
  1452. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1453. {
  1454. ctr_Heat(OFF);
  1455. rt_kprintf("异常退出!!!\n");
  1456. break;
  1457. }
  1458. rt_thread_delay(1000);
  1459. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1460. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1461. }
  1462. ctr_Heat(OFF);
  1463. //UpLoad_Data(); //加热结束的数据
  1464. }
  1465. //初始化所有控制驱动,关闭
  1466. void HardWare_Init(uint8_t boot)
  1467. {
  1468. Beep_Tip(200);
  1469. //关闭上仓门
  1470. ctr_cangmen_up(OFF);
  1471. if(device_status.dtype==3){
  1472. //关闭下仓门电机
  1473. ctr_cangmen_down(OFF);
  1474. }
  1475. //关闭镇流器
  1476. if(device_status.autotakepic==0)
  1477. ctr_lamp(OFF,1);
  1478. //关闭震动电机
  1479. ctr_shake(OFF,0);
  1480. //关闭加热
  1481. ctr_Heat(OFF);
  1482. //关闭补光灯
  1483. ctr_led(OFF);
  1484. //关闭12V供电(海康相机,RTU)
  1485. ctr_photo(OFF);
  1486. //关闭清扫电机驱动
  1487. ctr_clear(OFF);
  1488. }
  1489. //拍照
  1490. void HK_TakePhoto(void)
  1491. {
  1492. //打开补光灯
  1493. DTU_Write("takephoto.mp3");
  1494. rt_thread_delay(10000);
  1495. char *takephotojson = makeJson_takephoto();
  1496. DTU_Write("%s",takephotojson);
  1497. rt_free(takephotojson);
  1498. //吸虫塔
  1499. if(device_status.version_type!=6)
  1500. {
  1501. ctr_led(ON);
  1502. if(device_status.version_type==48)
  1503. {
  1504. rt_thread_delay(1000*60);
  1505. }
  1506. else {
  1507. rt_thread_delay(1000*15);
  1508. }
  1509. }
  1510. //触发海康相机拍照
  1511. Hk_Take_io();
  1512. if(device_status.version_type!=6)
  1513. {
  1514. rt_thread_delay(1000*45);
  1515. }
  1516. else {
  1517. ctr_led(ON);
  1518. rt_thread_delay(1000*19);
  1519. }
  1520. ctr_led(OFF);
  1521. }
  1522. //光控测试任务
  1523. void SunTest(void)
  1524. {
  1525. rt_uint16_t ui_timeout_l = 0;
  1526. //未检测到光照,一秒扫描等待,超时30S
  1527. while(Read_Light_Voltage()<500)
  1528. {
  1529. ui_timeout_l ++;
  1530. rt_thread_delay(1000);
  1531. if(ui_timeout_l==30) break;
  1532. }
  1533. rt_thread_delay(3000);
  1534. if(Read_Light_Voltage()<500)
  1535. {
  1536. device_status.sun_status = 0;//光控状态
  1537. }
  1538. if(Read_Light_Voltage()>500)
  1539. {
  1540. rt_thread_delay(1000);
  1541. }
  1542. rt_thread_delay(3000);
  1543. if(Read_Light_Voltage()>500)
  1544. {
  1545. device_status.sun_status = 1;
  1546. ctr_lamp(OFF,1);
  1547. }
  1548. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1549. device_status.sun_test = 0;
  1550. device_status.work_status=0;
  1551. }
  1552. /*上电初始化*/
  1553. void ResetInit(void)
  1554. {
  1555. if(device_status.dtype==7)
  1556. {
  1557. ctr_Motor_Driver(0, 0, 0, 0);
  1558. //关闭电磁阀
  1559. ctr_shake(OFF, 0);
  1560. //关闭加热
  1561. ctr_Heat(OFF);
  1562. //关闭制冷
  1563. ctr_Cold(OFF);
  1564. //关闭补光灯
  1565. ctr_led(OFF);
  1566. //转盘复位
  1567. if(device_status.run_sta==0)
  1568. ctr_turn(0, 0);
  1569. }
  1570. if(device_status.version_type!=6&&device_status.version_type!=7)
  1571. {
  1572. device_status.work_done = 1;
  1573. //1.打开路由器供电
  1574. if(device_status.power_status==1)
  1575. {
  1576. ctr_router(ON);
  1577. ctr_RTU_DTU(ON);
  1578. //2.打开相机供电
  1579. ctr_photo(ON);
  1580. }
  1581. //3.关闭镇流器
  1582. ctr_lamp(OFF,1);
  1583. #if LAMP_EN == 1
  1584. ctr_lamp(OFF,2);
  1585. ctr_lamp(OFF,3);
  1586. #endif
  1587. //4.关闭震动电机
  1588. ctr_shake(OFF, 0);
  1589. //5.关闭加热
  1590. ctr_Heat(OFF);
  1591. //6.关闭补光灯
  1592. ctr_led(OFF);
  1593. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1594. {
  1595. LOG_W("欠压!");
  1596. return;
  1597. }
  1598. //7.关闭上仓门
  1599. device_status.ctr_up_motor = 2;
  1600. // while(device_status.ctr_up_motor!=0)
  1601. // {
  1602. // LOG_I("关闭上仓门...");
  1603. // rt_thread_delay(100);
  1604. // }
  1605. //8.关闭下仓门
  1606. device_status.ctr_down_motor = 2;
  1607. while(device_status.ctr_down_motor!=0)
  1608. {
  1609. LOG_I("关闭下仓门....");
  1610. rt_thread_delay(100);
  1611. }
  1612. //9.清扫
  1613. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1614. {
  1615. LOG_W("异常退出!!!\n");
  1616. return;
  1617. }
  1618. #if 0//LAMP_EN == 0
  1619. ctr_Fan_PUT(OFF);
  1620. ctr_Fan_IN(OFF);
  1621. #endif
  1622. if(device_status.version_type==4||device_status.version_type==41)
  1623. {
  1624. device_status.clear_status=1;
  1625. while(device_status.clear_status==1){
  1626. rt_thread_delay(1000);
  1627. }
  1628. }
  1629. //排虫
  1630. if(device_status.handle_bugger==0){
  1631. device_status.run_sta = 111; //排虫
  1632. }else{
  1633. device_status.run_sta = 112; //收虫
  1634. }
  1635. }
  1636. else {
  1637. if(device_status.power_status==1)
  1638. {
  1639. //1.打开路由器供电
  1640. ctr_router(ON);
  1641. ctr_RTU_DTU(ON);
  1642. //2.打开相机供电
  1643. ctr_photo(ON);
  1644. }
  1645. //5.关闭加热
  1646. //6.关闭补光灯
  1647. ctr_led(OFF);
  1648. }
  1649. }
  1650. /*三色光源*/
  1651. void Led_Check(uint8_t tp)
  1652. {
  1653. if(tp==3)
  1654. {
  1655. if(device_status.light_over_tim>2*3600)
  1656. {
  1657. device_status.work_status = 1;
  1658. device_status.lamp_status = 1;
  1659. device_status.lamp1_status = 0;
  1660. device_status.lamp2_status = 0;
  1661. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1662. }
  1663. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1664. device_status.work_status = 1;
  1665. device_status.lamp_status = 0;
  1666. device_status.lamp1_status = 1;
  1667. device_status.lamp2_status = 0;
  1668. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1669. }
  1670. else if (device_status.light_over_tim<1*3600) {
  1671. device_status.work_status = 1;
  1672. device_status.lamp_status = 0;
  1673. device_status.lamp1_status = 0;
  1674. device_status.lamp2_status = 1;
  1675. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1676. }
  1677. }
  1678. else if (tp==6) {
  1679. if(device_status.light_over_tim>4*3600)
  1680. {
  1681. device_status.work_status = 1;
  1682. device_status.lamp_status = 1;
  1683. device_status.lamp1_status = 0;
  1684. device_status.lamp2_status = 0;
  1685. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1686. }
  1687. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1688. device_status.work_status = 1;
  1689. device_status.lamp_status = 0;
  1690. device_status.lamp1_status = 1;
  1691. device_status.lamp2_status = 0;
  1692. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1693. }
  1694. else if (device_status.light_over_tim<7200) {
  1695. device_status.work_status = 1;
  1696. device_status.lamp_status = 0;
  1697. device_status.lamp1_status = 0;
  1698. device_status.lamp2_status = 1;
  1699. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1700. }
  1701. }
  1702. else if (tp==9) {
  1703. if(device_status.light_over_tim>6*3600)
  1704. {
  1705. device_status.work_status = 1;
  1706. device_status.lamp_status = 1;
  1707. device_status.lamp1_status = 0;
  1708. device_status.lamp2_status = 0;
  1709. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1710. }
  1711. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1712. device_status.work_status = 1;
  1713. device_status.lamp_status = 0;
  1714. device_status.lamp1_status = 1;
  1715. device_status.lamp2_status = 0;
  1716. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1717. }
  1718. else if (device_status.light_over_tim<3*3600) {
  1719. device_status.work_status = 1;
  1720. device_status.lamp_status = 0;
  1721. device_status.lamp1_status = 0;
  1722. device_status.lamp2_status = 1;
  1723. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1724. }
  1725. }
  1726. }
  1727. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1728. void HardWare_Check(uint8_t boot)
  1729. {
  1730. if(boot==0&&device_status.version_type!=6)
  1731. {
  1732. //停止所有动作 -完成
  1733. HardWare_Init(boot);
  1734. }
  1735. else {
  1736. //停止所有动作
  1737. //读取记录待机功耗(实测大概70mA)
  1738. //(需减去屏幕的功耗大概120mA)
  1739. //动作上仓门,读取功率值保存
  1740. //动作下仓门,读取功率值保存
  1741. //动作清扫,读取功率值保存
  1742. //打开镇流器,读取功率值保存
  1743. //打开海康相机供电,读取功率值保存
  1744. //打开补光灯,读取功率值保存
  1745. //打开路由器/RTU供电,读取功率值保存
  1746. //打开加热,读取功率值保存
  1747. }
  1748. }
  1749. //工作状态检测
  1750. void WorkCheck(void)
  1751. {
  1752. //雨控检测,温控检测,电池电压检测
  1753. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1754. {
  1755. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1756. if(device_status.timeSwitch==1)
  1757. {
  1758. LOG_I("***********时控*****************");
  1759. if(device_status.startBar==device_status.stopBar&&device_status.startBar==0){
  1760. device_status.work_status = 1;
  1761. LOG_I("24 hours work...");
  1762. #if LAMP_EN == 1
  1763. device_status.lamp_status = 1;
  1764. device_status.lamp1_status = 0;
  1765. device_status.lamp2_status = 0;
  1766. if(device_status.sun_status==1){
  1767. ctr_lamp(OFF, 1);
  1768. ctr_lamp(OFF, 2);
  1769. ctr_lamp(OFF, 3);
  1770. }
  1771. #endif
  1772. }
  1773. //如果开始和结束是同一天
  1774. if(device_status.startBar<device_status.stopBar)
  1775. {
  1776. LOG_I("开始和结束是同一天");
  1777. time_t now;
  1778. time(&now);
  1779. static struct tm nowtime; //现在
  1780. static struct tm starttime; //开始
  1781. static struct tm endtime; //结束
  1782. //获取当前时间戳
  1783. nowtime =*localtime(&now);
  1784. nowtime.tm_hour = nowtime.tm_hour-8;
  1785. //获取定时开始的时间戳
  1786. starttime = *localtime(&now);
  1787. starttime.tm_hour = device_status.startBar-8;
  1788. starttime.tm_min = 0;
  1789. starttime.tm_sec = 0;
  1790. //获取定时结束的时间戳
  1791. endtime = *localtime(&now);
  1792. endtime.tm_hour = device_status.stopBar-8;
  1793. endtime.tm_min = 0;
  1794. endtime.tm_sec = 0;
  1795. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1796. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1797. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1798. {
  1799. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1800. device_status.work_status = 1;
  1801. }
  1802. //如果当前时间小于开始时间
  1803. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1804. {
  1805. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1806. device_status.work_status = 0;
  1807. }
  1808. //当前时间大于结束时间
  1809. else {
  1810. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1811. device_status.work_status = 0;
  1812. }
  1813. #if LAMP_EN == 1
  1814. //如果当前时间超过20点,未到21点
  1815. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1816. {
  1817. LOG_I("3led:20~21");
  1818. device_status.lamp_status = 1;
  1819. device_status.lamp1_status = 0;
  1820. device_status.lamp2_status = 0;
  1821. }
  1822. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1823. LOG_I("3led:21~22");
  1824. device_status.lamp1_status = 1;
  1825. device_status.lamp_status = 0;
  1826. device_status.lamp2_status = 0;
  1827. }
  1828. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1829. LOG_I("3led:22~23");
  1830. device_status.lamp2_status = 1;
  1831. device_status.lamp_status = 0;
  1832. device_status.lamp1_status =0;
  1833. }
  1834. #endif
  1835. }
  1836. //如果结束时间是第二天
  1837. else if (device_status.startBar>device_status.stopBar) {
  1838. time_t now;
  1839. time(&now);
  1840. static struct tm nowtime; //现在
  1841. static struct tm starttime; //开始
  1842. static struct tm endtime; //结束
  1843. //获取当前时间戳
  1844. nowtime =*localtime(&now);
  1845. nowtime.tm_hour = nowtime.tm_hour-8;
  1846. //获取定时开始的时间戳
  1847. starttime = *localtime(&now);
  1848. starttime.tm_hour = device_status.startBar-8;
  1849. starttime.tm_min = 0;
  1850. starttime.tm_sec = 0;
  1851. //获取定时结束的时间戳
  1852. endtime = *localtime(&now);
  1853. endtime.tm_hour = device_status.stopBar-8;
  1854. endtime.tm_min = 0;
  1855. endtime.tm_sec = 0;
  1856. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1857. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1858. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1859. {
  1860. //获取定时结束的时间戳
  1861. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1862. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1863. endtime.tm_min = 0;
  1864. endtime.tm_sec = 0;
  1865. device_status.work_status = 1;
  1866. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1867. }
  1868. //当前时间小于开始时间
  1869. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1870. {
  1871. //获取定时结束的时间戳
  1872. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1873. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1874. endtime.tm_min = 0;
  1875. endtime.tm_sec = 0;
  1876. //如果当前时间戳小于结束时间戳
  1877. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1878. {
  1879. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1880. device_status.work_status = 1;
  1881. }
  1882. else
  1883. {
  1884. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1885. device_status.work_status = 0; //不在工作时间,待机
  1886. }
  1887. }
  1888. }
  1889. }
  1890. else {
  1891. LOG_I("光控定时模式!");
  1892. /*
  1893. time_t now;
  1894. time(&now);
  1895. static struct tm nowstamp;
  1896. nowstamp =*localtime(&now);
  1897. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1898. static rt_uint32_t nowstamp_num = 0;
  1899. nowstamp_num = mktime(&nowstamp);
  1900. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1901. */
  1902. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1903. {
  1904. device_status.light_over_tim--;
  1905. #if LAMP_EN == 1
  1906. Led_Check(device_status.sunBar);
  1907. #else
  1908. device_status.work_status = 1;;
  1909. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1910. #endif
  1911. }
  1912. else
  1913. {
  1914. //没有在测试并且没有在自检
  1915. if(device_status.test_flag==0&&device_status.selfcheck_ws==0)
  1916. {
  1917. ctr_lamp(OFF,1);
  1918. #if LAMP_EN == 1
  1919. ctr_lamp(OFF,2);
  1920. ctr_lamp(OFF,3);
  1921. #endif
  1922. }
  1923. device_status.work_status = 0;
  1924. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1925. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1926. }
  1927. }
  1928. rt_thread_delay(1000);
  1929. }
  1930. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1931. device_status.work_status = 0;
  1932. rt_thread_delay(1000);
  1933. }
  1934. //工作任务流程s
  1935. void WorkTask(void)
  1936. {
  1937. static int clt_time = 0; //收集时间差秒数
  1938. static int heat_time = 0;
  1939. LOG_I("工作状态:%d",device_status.work_status);
  1940. LOG_I("开关机状态:%d",device_status.ds);
  1941. while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1&&device_status.clear_status==0)
  1942. {
  1943. device_status.work_done = 0;
  1944. if(clt_time==0)
  1945. {
  1946. #if 0
  1947. if(device_status.version_type==41&&atoi(device_status.power)<350)
  1948. {
  1949. LOG_W("相机电流不足!");
  1950. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  1951. DTU_Write("%s",ctr_hk_p_warn);
  1952. rt_free(ctr_hk_p_warn);
  1953. }
  1954. #endif
  1955. #if 0//LAMP_EN == 0
  1956. ctr_Fan_PUT(ON);
  1957. #endif
  1958. DTU_Write("workstart.mp3");
  1959. }
  1960. //1.打开灯管
  1961. #if LAMP_EN == 1
  1962. if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
  1963. {
  1964. ctr_lamp(ON,1);
  1965. ctr_lamp(OFF,2);
  1966. ctr_lamp(OFF,3);
  1967. }
  1968. else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
  1969. ctr_lamp(OFF,1);
  1970. ctr_lamp(ON,2);
  1971. ctr_lamp(OFF,3);
  1972. }
  1973. else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
  1974. ctr_lamp(OFF,1);
  1975. ctr_lamp(OFF,2);
  1976. ctr_lamp(ON,3);
  1977. }
  1978. #else
  1979. ctr_lamp(ON,1);
  1980. #endif
  1981. LOG_I("打开灯管!!!");
  1982. #if 0
  1983. rt_thread_delay(2000);
  1984. if(atoi(device_status.power)<450)
  1985. {
  1986. LOG_I("Current is:%d",atoi(device_status.power));
  1987. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  1988. {
  1989. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  1990. DTU_Write("%s",lamp_err_warn);
  1991. rt_free(lamp_err_warn);
  1992. }
  1993. }
  1994. #endif
  1995. if(device_status.version_type!=5)
  1996. {
  1997. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  1998. //2.打开上仓门
  1999. LOG_I("打开上仓门!!!");
  2000. device_status.ctr_up_motor = 1;
  2001. //3.收集
  2002. clt_time = device_status.clt_time * 60;
  2003. while(clt_time!=1)
  2004. {
  2005. #if 0//LAMP_EN == 0
  2006. ctr_Fan_IN(ON);
  2007. #endif
  2008. clt_time --;
  2009. rt_thread_delay(1000);
  2010. #if 0//LAMP_EN == 0
  2011. if(clt_time%30==0)
  2012. ctr_Fan_PUT(OFF);
  2013. #endif
  2014. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2015. //如果触发异常
  2016. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2017. {
  2018. ctr_lamp(OFF,1);
  2019. #if LAMP_EN == 1
  2020. ctr_lamp(OFF,2);
  2021. ctr_lamp(OFF,3);
  2022. #endif
  2023. #if 0//LAMP_EN == 0
  2024. ctr_Fan_IN(OFF);
  2025. #endif
  2026. rt_kprintf("异常退出!!!\n");
  2027. break;
  2028. }
  2029. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2030. {
  2031. ctr_lamp(OFF,1);
  2032. #if LAMP_EN == 1
  2033. ctr_lamp(OFF,2);
  2034. ctr_lamp(OFF,3);
  2035. #endif
  2036. #if 0//LAMP_EN == 0
  2037. ctr_Fan_IN(OFF);
  2038. #endif
  2039. rt_kprintf("异常退出!!!\n");
  2040. break;
  2041. }
  2042. }
  2043. clt_time = 1;
  2044. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2045. {
  2046. ctr_lamp(OFF,1);
  2047. #if LAMP_EN == 1
  2048. ctr_lamp(OFF,2);
  2049. ctr_lamp(OFF,3);
  2050. #endif
  2051. #if 0//LAMP_EN == 0
  2052. ctr_Fan_IN(OFF);
  2053. #endif
  2054. rt_kprintf("异常退出!!!\n");
  2055. break;
  2056. }
  2057. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2058. {
  2059. ctr_lamp(OFF,1);
  2060. #if LAMP_EN == 1
  2061. ctr_lamp(OFF,2);
  2062. ctr_lamp(OFF,3);
  2063. #endif
  2064. #if 0//LAMP_EN == 0
  2065. ctr_Fan_IN(OFF);
  2066. #endif
  2067. rt_kprintf("异常退出!!!\n");
  2068. break;
  2069. }
  2070. #if 0//LAMP_EN == 0
  2071. ctr_Fan_IN(OFF);
  2072. #endif
  2073. //4.关闭上仓门
  2074. device_status.ctr_up_motor = 2;
  2075. rt_kprintf("关闭上仓门!!!\n");
  2076. while(device_status.ctr_up_motor!=0)
  2077. {
  2078. rt_thread_delay(100);
  2079. LOG_I("等待上仓门关闭!!!");
  2080. }
  2081. //5.加热
  2082. if(device_status.heat_sw==0)
  2083. {
  2084. calorstat(device_status.heattime*60);
  2085. }
  2086. else {
  2087. heat_time = device_status.heattime * 60;
  2088. while(heat_time!=0)
  2089. {
  2090. heat_time--;
  2091. LOG_I("正在加热状态!!!");
  2092. rt_thread_delay(1000);
  2093. }
  2094. }
  2095. //6.打开下仓门
  2096. if(device_status.rain_status==0)
  2097. {
  2098. device_status.ctr_down_motor =1;
  2099. }
  2100. else {
  2101. device_status.ctr_down_motor = 2;
  2102. device_status.ctr_up_motor = 2;
  2103. }
  2104. //待下仓门完全打开
  2105. while(device_status.ctr_down_motor!=0)
  2106. {
  2107. rt_thread_delay(100);
  2108. rt_kprintf("等待下仓门打开!!!\n");
  2109. }
  2110. rt_thread_delay(3000);
  2111. //关闭下仓门
  2112. device_status.ctr_down_motor = 2;
  2113. while(device_status.ctr_down_motor!=0)
  2114. {
  2115. rt_thread_delay(100);
  2116. rt_kprintf("等待下仓门关闭!!!\n");
  2117. }
  2118. //2022-04-12add:
  2119. LOG_I("打开上仓门!!!");
  2120. device_status.ctr_up_motor = 1;
  2121. if(device_status.version_type==4||device_status.version_type==41)
  2122. {
  2123. //震动
  2124. ctr_shake(ON, device_status.shake_sec);
  2125. //补光&&拍照
  2126. LOG_I("拍照!!!");
  2127. ctr_led(ON);
  2128. HK_TakePhoto();
  2129. //清扫
  2130. device_status.clear_status=1;
  2131. //清扫失效则使用震动
  2132. //ctr_shake(ON, 100);
  2133. }
  2134. }
  2135. else {
  2136. //打开高压
  2137. ctr_Heat(ON);
  2138. //打开补光
  2139. ctr_led(ON);
  2140. LOG_I("打开补光!!");
  2141. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  2142. //2.翻转清虫
  2143. while(rt_pin_read(CLEAR_L_LIMIT)==1) rt_thread_delay(20);
  2144. rt_thread_delay(500);
  2145. Hk_Take_io(); //触发海康相机拍照
  2146. rt_thread_delay(500);
  2147. ctr_turnv5(); //摆动清虫
  2148. LOG_I("摆动清虫!");
  2149. }
  2150. }
  2151. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2152. {
  2153. LOG_I("-------------------not work--------------------");
  2154. device_status.work_status = 0;
  2155. }
  2156. //工作结束
  2157. if(device_status.work_done==0&&device_status.work_status==0)
  2158. {
  2159. DTU_Write("workdone");
  2160. if (device_status.version_type==1) {
  2161. turnzp(); //2024-03-21 :开机不再转仓
  2162. }
  2163. ResetInit();
  2164. clt_time = 0;
  2165. }
  2166. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2167. }
  2168. void WorkTask_CB5(void)
  2169. {
  2170. while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1)
  2171. {
  2172. //打开灯管
  2173. ctr_lamp(ON,1);
  2174. //推杆电机打开,导流板到落虫通道
  2175. //发送串口消息,通知yolo是被及相机采集模块启动,定期将模块的运行状态展示到屏幕上
  2176. //收到益虫信号->
  2177. //收到害虫信号->
  2178. }
  2179. device_status.work_status = 0;
  2180. //工作结束
  2181. if(device_status.work_done==0&&device_status.work_status==0)
  2182. {
  2183. DTU_Write("workdone");
  2184. ResetInit();
  2185. }
  2186. LOG_I("WorkTask_CB5 sleeping......");
  2187. }
  2188. /* 入口函数-工作状态检测 */
  2189. static void Work_status_thread_entry(void *parameter)
  2190. {
  2191. static uint16_t sun_status_num = 120;
  2192. //如果是光控定时,进行光控监测的消抖
  2193. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2194. {
  2195. rt_kprintf("光控定时!!!\n");
  2196. while(sun_status_num!=0)
  2197. {
  2198. sun_status_num--;
  2199. rt_thread_delay(1000);
  2200. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2201. }
  2202. device_status.light_over_tim = device_status.sunBar * 3600;
  2203. device_status.test_flag = 0;
  2204. }
  2205. while(1)
  2206. {
  2207. WorkCheck();
  2208. }
  2209. }
  2210. /*入口函数-工作流程*/
  2211. static void Work_Loop_thread_entry(void *parameter)
  2212. {
  2213. rt_thread_delay(1000);
  2214. ResetInit();
  2215. while(1)
  2216. {
  2217. if(device_status.version_type!=6&&device_status.dtype==3)
  2218. {
  2219. WorkTask();
  2220. }
  2221. if(device_status.version_type==6)
  2222. {
  2223. WorkTask_XCT();
  2224. }
  2225. if(device_status.version_type==5)
  2226. {
  2227. }
  2228. rt_thread_delay(1000);
  2229. }
  2230. }
  2231. /*入口函数清扫*/
  2232. static void Control_Clear_thread_entry(void *parameter)
  2233. {
  2234. static int selfcheck_cnt = 0;
  2235. float voltage_f = 0.0;
  2236. while(1)
  2237. {
  2238. #if ROBOT_EN ==1
  2239. ctr_Motor_Robot(ON);
  2240. rt_thread_delay(100);
  2241. #else
  2242. #if Color_EN ==1
  2243. /*复位*/
  2244. if(Read_Color_Key(KEY_RESET)==1)
  2245. {
  2246. ctr_Color_board(RESET, 1200);
  2247. rt_thread_delay(5000);
  2248. }
  2249. #else
  2250. if(device_status.selfcheck_ws==1){
  2251. selfcheck_cnt ++;
  2252. if(selfcheck_cnt==1){
  2253. ctr_lamp(ON,1);
  2254. rt_thread_delay(1000);
  2255. device_status.ctr_up_motor = 1;
  2256. device_status.ctr_down_motor = 1;
  2257. //记录当前电压
  2258. voltage_f = atof(device_status.voltage);
  2259. rt_thread_delay(1500);
  2260. //开启加热
  2261. ctr_Heat(ON);
  2262. }
  2263. if(selfcheck_cnt==50){
  2264. double voltage_n = atof(device_status.voltage);
  2265. //判断当前电压和加热前的电压差
  2266. if(voltage_f-voltage_n<0.5){
  2267. device_status.heat_sta = 1; //加热异常
  2268. //上报预警,加热故障
  2269. char *heat_warn = makeJson_Warn("加热报警", "heat-error");
  2270. DTU_Write("%s", heat_warn);
  2271. rt_free(heat_warn);
  2272. }else{
  2273. device_status.heat_sta = 0;
  2274. }
  2275. rt_thread_delay(1000);
  2276. //关闭加热
  2277. ctr_Heat(OFF);
  2278. //振动
  2279. ctr_shake(ON, 1);
  2280. //device_status.clear_status = 1;
  2281. ctr_lamp(OFF, 1);
  2282. }
  2283. if(selfcheck_cnt>1800){ //超过3分钟
  2284. device_status.selfcheck_ws = 0;
  2285. device_status.ctr_up_motor = 0;
  2286. device_status.ctr_down_motor = 0;
  2287. //
  2288. device_status.camsta = 1; //相机异常
  2289. device_status.led_sta = 1;
  2290. //上报自检结果
  2291. //上报自检结果
  2292. char *json_temp = makeJson_selfCheck_(1);
  2293. DTU_Write("%s",json_temp);
  2294. free(json_temp);
  2295. }
  2296. }
  2297. if(device_status.selfcheck_ws==0)
  2298. {
  2299. selfcheck_cnt = 0;
  2300. }
  2301. if(device_status.clear_status==1)
  2302. {
  2303. if(ctr_clear(1200)==0)
  2304. ctr_TG_Clear(400);
  2305. }
  2306. rt_thread_delay(100);
  2307. #endif
  2308. #endif
  2309. }
  2310. }
  2311. /*入口函数加热*/
  2312. static void Control_Heat_thread_entry(void *parameter)
  2313. {
  2314. while(1)
  2315. {
  2316. if(device_status.work_status==1&&device_status.heat_sw==1)
  2317. {
  2318. ctr_lamp(ON, 1);
  2319. rt_thread_delay(10000);
  2320. calorstat(device_status.heattime*60);
  2321. }
  2322. else
  2323. {
  2324. if(device_status.selfcheck_ws==2)
  2325. {
  2326. static int total_seconds = 200;
  2327. static int current_second = 0;
  2328. while(current_second <= total_seconds)
  2329. {
  2330. int process = (current_second * 100) / total_seconds;
  2331. device_status.process = process / 100.0;
  2332. LOG_I("process:%d",process);
  2333. HMI_WRITE("selfcheck.s106.val=%d",process);
  2334. rt_thread_delay(1000);
  2335. current_second++;
  2336. if(device_status.selfcheck_ws==0)
  2337. {
  2338. HMI_WRITE("selfcheck.s106.val=100");
  2339. break;
  2340. }
  2341. }
  2342. HMI_WRITE("selfcheck.s106.val=100");
  2343. }
  2344. }
  2345. rt_thread_delay(1000);
  2346. }
  2347. }
  2348. /*清扫线程*/
  2349. int Control_clear(void)
  2350. {
  2351. /* 创建线程*/
  2352. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2353. Control_Clear_thread_entry, RT_NULL,
  2354. 2048, //2048
  2355. 24, //23
  2356. WORK_CHECK_THREAD_TIMESLICE); //5
  2357. if(tid_ctr!=RT_NULL)
  2358. {
  2359. rt_thread_startup(tid_ctr);
  2360. }
  2361. return 0;
  2362. }
  2363. /*恒温加热线程*/
  2364. int Control_heat(void)
  2365. {
  2366. /* 创建线程*/
  2367. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  2368. Control_Heat_thread_entry, RT_NULL,
  2369. 1024, //2048
  2370. 25, //23
  2371. WORK_CHECK_THREAD_TIMESLICE); //5
  2372. if(tid_heat!=RT_NULL)
  2373. {
  2374. rt_thread_startup(tid_heat);
  2375. }
  2376. return 0;
  2377. }
  2378. /* 创建线程 -工作状态检测*/
  2379. int Workstatus_check(void)
  2380. {
  2381. /* 创建线程*/
  2382. tid_wkstc = rt_thread_create("work_status_check_thread",
  2383. Work_status_thread_entry, RT_NULL,
  2384. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2385. WORK_CHECK_THREAD_PRIORITY, //23
  2386. WORK_CHECK_THREAD_TIMESLICE); //5
  2387. if(tid_wkstc!=RT_NULL)
  2388. {
  2389. rt_thread_startup(tid_wkstc);
  2390. }
  2391. return 0;
  2392. }
  2393. /*工作流程线程*/
  2394. int Work_Loop_Task(void)
  2395. {
  2396. rt_err_t result_loop = 0;
  2397. /* 创建线程*/
  2398. tid_workloop = rt_thread_create("work_loop_thread",
  2399. Work_Loop_thread_entry, RT_NULL,
  2400. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2401. WORK_LOOP_THREAD_PRIORITY, //12
  2402. WORK_LOOP_THREAD_TIMESLICE); //5
  2403. if(tid_workloop!=RT_NULL)
  2404. {
  2405. result_loop = rt_thread_startup(tid_workloop);
  2406. }
  2407. return result_loop;
  2408. }
  2409. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2410. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2411. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2412. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2413. //INIT_APP_EXPORT(Control_heat);