control.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  1. #include <control.h>
  2. #include <stdint.h>
  3. /*
  4. * Copyright (c) 2006-2020, RT-Thread Development Team
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2020-05-30 Administrator the first version
  11. */
  12. #include "beep.h"
  13. #include "hmi.h"
  14. #include "easyflash.h"
  15. #include "stdio.h"
  16. #include "sound.h"
  17. #include "dtu_uart2.h"
  18. #include "sht30_collect.h"
  19. #include "string.h"
  20. #define DBG_ENABLE
  21. #define DBG_COLOR
  22. #define DBG_TAG "control"
  23. #define DBG_LEVEL DBG_LOG
  24. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  25. #include "sys_monitor.h"
  26. //工作检测
  27. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  28. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  29. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  30. //工作流程
  31. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  32. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  33. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  34. #define PWM_EN 1 //0关闭,1-打开
  35. //设备状态表结构体
  36. Device_Status_typedef device_status;
  37. System_stats_t system_status;
  38. rt_device_t dtuserial; /*串口设备句柄*/
  39. rt_thread_t tid_workloop = RT_NULL;
  40. rt_thread_t tid_wkstc = RT_NULL;
  41. rt_thread_t tid_ctr = RT_NULL;
  42. rt_thread_t tid_heat = RT_NULL;
  43. int thread_delete_workloop(void)
  44. {
  45. LOG_W("thread_delete_workloop.");
  46. rt_err_t result;
  47. if(tid_workloop!=RT_NULL)
  48. {
  49. result = rt_thread_delete(tid_workloop);
  50. }
  51. if(tid_wkstc!=RT_NULL)
  52. {
  53. result = rt_thread_delete(tid_wkstc);
  54. }
  55. if(tid_heat!=RT_NULL)
  56. {
  57. result = rt_thread_delete(tid_heat);
  58. }
  59. if(tid_ctr!=RT_NULL)
  60. {
  61. result = rt_thread_delete(tid_ctr);
  62. }
  63. return 0;
  64. }
  65. int thread_restart_workloop(void)
  66. {
  67. rt_err_t result;
  68. result = Work_Loop_Task();
  69. return result;
  70. }
  71. /**************************1.0定制2000W*************************************/
  72. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  73. {
  74. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  75. if(status_ws==ON)
  76. {
  77. rt_pin_write(FAN_P,ON);
  78. }
  79. else {
  80. rt_pin_write(FAN_P, OFF);
  81. }
  82. return 0;
  83. }
  84. uint8_t ctr_Fan_IN(uint8_t status_ws)
  85. {
  86. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  87. if(status_ws==ON)
  88. {
  89. rt_pin_write(FAN_I,ON);
  90. }
  91. else {
  92. rt_pin_write(FAN_I, OFF);
  93. }
  94. return 0;
  95. }
  96. /****************************定制色诱*************************************/
  97. /**/
  98. #if Color_EN == 1
  99. uint8_t Read_Color_Key(rt_base_t key_pin)
  100. {
  101. rt_uint32_t sta_s = 0;
  102. rt_pin_mode(key_pin, PIN_MODE_INPUT);
  103. while(rt_pin_read(key_pin)==1)
  104. {
  105. sta_s++;
  106. /*持续的*/
  107. rt_thread_delay(50);
  108. }
  109. while(rt_pin_read(key_pin)==0)
  110. {
  111. rt_thread_delay(50);
  112. if(sta_s>0) break;
  113. }
  114. LOG_W("检测到IO0翻转!!!");
  115. return 1;
  116. }
  117. /*读限位开关状态*/
  118. uint8_t Read_Color_Limit(rt_base_t pin_limit)
  119. {
  120. uint8_t l_sta = 0;
  121. rt_pin_mode(pin_limit, PIN_MODE_INPUT);
  122. l_sta = rt_pin_read(pin_limit);
  123. rt_thread_delay(20);
  124. l_sta = rt_pin_read(pin_limit);
  125. return l_sta;
  126. }
  127. /*色诱板*/
  128. int ctr_Color_board(uint8_t forward,uint16_t time_board)
  129. {
  130. if(forward==CLOSE) //归位
  131. {
  132. if(device_status.cul_time>0)
  133. device_status.cul_time--;
  134. ctr_cangmen_down(OFF);
  135. ctr_cangmen_up(OFF);
  136. rt_thread_delay(1000);
  137. // if(device_status.cul_time>7) device_status.cul_time=7;
  138. // if(device_status.cul_time!=7)
  139. // {
  140. // uint8_t ct = device_status.cul_time;
  141. // while(1)
  142. // {
  143. // Beep_Tip(100);
  144. // rt_thread_delay(100);
  145. // ct--;
  146. // if(ct==0) break;
  147. // }
  148. // //return device_status.cul_time;
  149. // }
  150. if(Read_Color_Limit(LIMIT_RESET)==0)
  151. {
  152. device_status.cul_time = 0;
  153. LOG_W("已在初始位置!");
  154. Beep_Tip(1000);
  155. return 0;
  156. //黄板已经在复位状态,直接返回0;
  157. }
  158. ctr_cangmen_up(CLOSE);
  159. rt_thread_delay(20000);
  160. ctr_cangmen_up(OFF);
  161. // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
  162. // {
  163. // LOG_W("第七天!!!");
  164. // ctr_cangmen_down(CLOSE);
  165. // rt_thread_delay(500);
  166. // }
  167. LOG_W("先打开运行到限位的右侧!");
  168. while(Read_Color_Limit(LIMIT_BOARD)==0)
  169. {
  170. LOG_W("归位:到达限位中间");
  171. ctr_cangmen_down(CLOSE);
  172. if(Read_Color_Limit(LIMIT_RESET)==0)
  173. {
  174. ctr_cangmen_down(OFF);
  175. break;
  176. }
  177. }
  178. //先打开运行到限位的右侧
  179. while(Read_Color_Limit(LIMIT_BOARD)==1)
  180. {
  181. LOG_W("归位:运行!");
  182. ctr_cangmen_down(CLOSE);
  183. if(Read_Color_Limit(LIMIT_RESET)==0)
  184. {
  185. ctr_cangmen_down(OFF);
  186. break;
  187. }
  188. }
  189. while(Read_Color_Limit(LIMIT_BOARD)==0) //到达限位中间 继续运行
  190. {
  191. LOG_W("归位:到达限位中间");
  192. ctr_cangmen_down(CLOSE);
  193. if(Read_Color_Limit(LIMIT_RESET)==0)
  194. {
  195. ctr_cangmen_down(OFF);
  196. break;
  197. }
  198. }
  199. if(Read_Color_Limit(LIMIT_RESET)==0) return 0;
  200. LOG_W("到达限位左侧!");
  201. //停止电机
  202. ctr_cangmen_down(OFF);
  203. rt_thread_delay(2000);
  204. //将推杆电机归位
  205. LOG_D("*推杆电机归位*");
  206. /*推杆电机伸出*/
  207. ctr_cangmen_up(OPEN);
  208. rt_thread_delay(20000);
  209. ctr_cangmen_up(OFF);
  210. return 2;
  211. }
  212. else if(forward==OPEN)
  213. {
  214. ctr_cangmen_down(OFF);
  215. ctr_cangmen_up(OFF);
  216. rt_thread_delay(1000);
  217. if(device_status.cul_time==7)
  218. {
  219. Beep_Tip(5000);
  220. return 7;
  221. }
  222. LOG_W("打开推杆电机!");
  223. //2023-04-18:add
  224. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  225. {
  226. LOG_D("*推杆电机归位*");
  227. /*推杆电机归位*/
  228. ctr_cangmen_up(CLOSE);
  229. rt_thread_delay(20000);
  230. ctr_cangmen_up(OFF);
  231. }
  232. if(device_status.cul_time<7)
  233. device_status.cul_time++;
  234. while(Read_Color_Limit(LIMIT_BOARD)==0)
  235. {
  236. ctr_cangmen_down(forward);
  237. rt_thread_delay(50);
  238. }
  239. while(Read_Color_Limit(LIMIT_BOARD)==1)
  240. {
  241. time_board--;
  242. if(time_board==0) break;
  243. ctr_cangmen_down(forward);
  244. rt_thread_delay(50);
  245. }
  246. ctr_cangmen_down(OFF);
  247. rt_thread_delay(1000);
  248. ctr_cangmen_up(OPEN);
  249. rt_thread_delay(20000);
  250. ctr_cangmen_up(OFF);
  251. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  252. return OPEN;
  253. }
  254. else if (forward==RESET) {
  255. ctr_cangmen_up(OFF);
  256. ctr_cangmen_down(OFF);
  257. if(Read_Color_Limit(LIMIT_RESET)==0)
  258. {
  259. device_status.cul_time = 0;
  260. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  261. LOG_W("已在初始位置!");
  262. Beep_Tip(1000);
  263. ctr_cangmen_up(CLOSE);
  264. rt_thread_delay(20000);
  265. ctr_cangmen_up(OFF);
  266. device_status.cul_time = 0;
  267. return 0;
  268. //黄板已经在复位状态,直接返回0;
  269. }
  270. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  271. {
  272. LOG_D("*推杆电机归位*");
  273. /*推杆电机归位*/
  274. ctr_cangmen_up(CLOSE);
  275. rt_thread_delay(20000);
  276. ctr_cangmen_up(OFF);
  277. }
  278. while(Read_Color_Limit(LIMIT_RESET)==1)
  279. {
  280. time_board--;
  281. if(time_board==0) break;
  282. ctr_cangmen_down(CLOSE);
  283. rt_thread_delay(20);
  284. }
  285. device_status.cul_time = 0;
  286. ctr_cangmen_down(OFF);
  287. }
  288. // device_status.cul_time = 0;
  289. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  290. return RESET; //复位返回0
  291. }
  292. #endif
  293. /****************************5.0***********************************/
  294. void ctr_turnv5(void)
  295. {
  296. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  297. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  298. // 转出
  299. rt_pin_write(CLEAR_R,ON);
  300. rt_pin_write(CLEAR_L,OFF);
  301. rt_thread_mdelay(300);
  302. //停止
  303. rt_pin_write(CLEAR_R,OFF);
  304. rt_pin_write(CLEAR_L,OFF);
  305. rt_thread_mdelay(300);
  306. //转入
  307. rt_pin_write(CLEAR_R,OFF);
  308. rt_pin_write(CLEAR_L,ON);
  309. rt_thread_mdelay(300);
  310. //停止
  311. rt_pin_write(CLEAR_R,OFF);
  312. rt_pin_write(CLEAR_L,OFF);
  313. rt_thread_mdelay(300);
  314. }
  315. /**************************机器人********************************/
  316. void ctr_Motor_Robot(uint8_t status_ws)
  317. {
  318. unsigned int close_timeout = 0;
  319. unsigned int open_timeout = 0;
  320. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  321. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  322. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  323. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  324. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  325. rt_pin_write(Motor_Power,OFF);
  326. rt_pin_write(Motor_Direction_Ctr,ON);
  327. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  328. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  329. if(status_ws == ON)
  330. {
  331. //检测按键状态
  332. while(rt_pin_read(Motor_Direction_Button)==1)
  333. {
  334. if(rt_pin_read(LIMIT_CLOSE)==1)
  335. {
  336. rt_pin_write(Motor_Direction_Ctr,ON);
  337. close_timeout++;
  338. //打开电机驱动器电源,默认关闭
  339. rt_pin_write(Motor_Power,ON);
  340. if(close_timeout==2)
  341. {
  342. rt_pin_write(CLEAR_R,ON);
  343. rt_pin_write(CLEAR_L,OFF);
  344. rt_pin_write(Motor_Power,OFF);
  345. rt_thread_delay(20);
  346. rt_pin_write(Motor_Power,ON);
  347. }
  348. rt_thread_delay(20);
  349. if(close_timeout>500)
  350. {
  351. close_timeout = 600;
  352. rt_pin_write(Motor_Power,OFF);
  353. rt_pin_write(CLEAR_R,OFF);
  354. rt_pin_write(CLEAR_L,OFF);
  355. }
  356. }
  357. else
  358. {
  359. close_timeout = 0;
  360. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  361. rt_pin_write(CLEAR_R,OFF);
  362. rt_pin_write(CLEAR_L,OFF);
  363. }
  364. rt_thread_delay(20);
  365. }
  366. rt_pin_write(Motor_Power,OFF);
  367. rt_thread_delay(1000*2);
  368. while(rt_pin_read(Motor_Direction_Button)==0)
  369. {
  370. if(rt_pin_read(LIMIT_OPEN)==1)
  371. {
  372. open_timeout ++;
  373. //打开电机驱动器电源,默认关闭
  374. if(open_timeout==2){
  375. rt_pin_write(CLEAR_R,OFF);
  376. rt_pin_write(CLEAR_L,ON);
  377. }
  378. rt_pin_write(Motor_Power,ON);
  379. rt_pin_write(Motor_Direction_Ctr,OFF);
  380. if(open_timeout>500)
  381. {
  382. open_timeout = 600;
  383. rt_pin_write(Motor_Power,OFF);
  384. rt_pin_write(CLEAR_R,OFF);
  385. rt_pin_write(CLEAR_L,OFF);
  386. }
  387. rt_thread_delay(20);
  388. }
  389. if(rt_pin_read(LIMIT_OPEN)==0)
  390. {
  391. open_timeout = 0;
  392. rt_pin_write(Motor_Power,OFF);
  393. rt_pin_write(CLEAR_R,OFF);
  394. rt_pin_write(CLEAR_L,OFF);
  395. }
  396. rt_thread_delay(20);
  397. close_timeout = 0;
  398. }
  399. open_timeout = 0;
  400. close_timeout = 0;
  401. }
  402. else {
  403. rt_pin_write(Motor_Power,OFF);
  404. }
  405. }
  406. /*************************吸虫塔*********************************/
  407. //吸虫电机
  408. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  409. {
  410. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  411. if(status_ws==ON)
  412. {
  413. rt_pin_write(MOTOR1_CTR_PIN,ON);
  414. }
  415. else {
  416. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  417. }
  418. return 0;
  419. }
  420. uint8_t ctr_Camera_Power(uint8_t status_ws)
  421. {
  422. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  423. if(status_ws==ON)
  424. {
  425. rt_pin_write(MOTOR1_CTR_PIN,ON);
  426. }
  427. else {
  428. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  429. }
  430. return 0;
  431. }
  432. //吸附电机
  433. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  434. {
  435. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  436. if(status_ws==ON)
  437. {
  438. rt_pin_write(MOTOR2_CTR_PIN,ON);
  439. }
  440. else {
  441. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  442. }
  443. return 0;
  444. }
  445. //清扫-卷带电机
  446. void ctr_JD_Clear(uint8_t shake_sw)
  447. {
  448. rt_uint16_t turn_timeout = 0;
  449. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  450. ctr_cangmen_up(OFF); //清扫传送带的电机
  451. if(shake_sw==ON)
  452. {
  453. ctr_Heat(ON); //清洁风机
  454. Motor_shake(ON, 0);
  455. ctr_cangmen_up(OPEN); //打开清扫
  456. }
  457. else {
  458. Motor_shake(OFF, 0);
  459. ctr_cangmen_up(OPEN); //打开清扫
  460. }
  461. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  462. {
  463. Motor_Juandai(ON);
  464. rt_thread_delay(50);
  465. turn_timeout++;
  466. if(turn_timeout==1000) break;
  467. }
  468. turn_timeout = 0;
  469. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  470. {
  471. Motor_Juandai(ON);
  472. turn_timeout++;
  473. rt_thread_delay(50);
  474. if(turn_timeout==1000) break;
  475. }
  476. Motor_shake(OFF, 1);
  477. Motor_Juandai(OFF);
  478. ctr_cangmen_up(OFF); //关闭清扫
  479. ctr_Heat(OFF);
  480. }
  481. //工作任务流程
  482. void WorkTask_XCT(void)
  483. {
  484. while(device_status.work_status==1&&device_status.ds==1)
  485. {
  486. ctr_Motor_xifu(ON);
  487. //打开相机供电
  488. ctr_Camera_Power(ON);
  489. //ctr_Heat(ON); //清洁风机
  490. uint32_t photo_fre = 0;
  491. photo_fre = device_status.dattim * 60;
  492. while(photo_fre!=0)
  493. {
  494. photo_fre --;
  495. rt_thread_delay(1000);
  496. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  497. }
  498. //卷带
  499. ctr_JD_Clear(OFF);
  500. //拍照
  501. HK_TakePhoto();
  502. //关闭吸附风机
  503. ctr_Motor_xifu(OFF);
  504. //关闭相机供电
  505. //ctr_Heat(OFF);
  506. ctr_Camera_Power(OFF);
  507. //卷带
  508. ctr_JD_Clear(ON);
  509. // rt_thread_delay(1000);
  510. // ctr_JD_Clear(ON);
  511. //震动
  512. Motor_shake(ON, device_status.shake_sec);
  513. }
  514. //关闭风机
  515. ctr_Motor_xichong(OFF);
  516. ctr_Motor_xifu(OFF);
  517. LOG_I("不在工作时间,未工作!!!");
  518. }
  519. /**********************************************************************/
  520. /*光控*/
  521. uint8_t In_Light_status(void)
  522. {
  523. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  524. return rt_pin_read(LIGHT_PIN);
  525. }
  526. /*雨控*/
  527. uint8_t In_Rain_status(void)
  528. {
  529. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  530. return rt_pin_read(RAIN_PIN);
  531. }
  532. /*加热*/
  533. uint8_t ctr_Heat(uint8_t status_heat)
  534. {
  535. rt_bool_t is_currently_on;
  536. rt_tick_t now;
  537. now = rt_tick_get();
  538. is_currently_on = (system_status.heat_start_time != 0);
  539. device_status.heat_status = status_heat;
  540. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  541. rt_pin_write(Heat_Pin,status_heat);
  542. if(status_heat==ON){
  543. // 已经开着 → 不重复计时
  544. if (is_currently_on) {
  545. return ON;
  546. }
  547. // 关 → 开,记录起始时间
  548. system_status.heat_start_time = now;
  549. }else{
  550. // 已经关着 → 不重复结算
  551. if (!is_currently_on) {
  552. return OFF;
  553. }
  554. // 开 → 关,结算运行时间
  555. system_status.heater_running_time +=
  556. (now - system_status.heat_start_time);
  557. system_status.heat_start_time = 0;
  558. }
  559. return status_heat;
  560. }
  561. /*制冷*/
  562. uint8_t ctr_Cold(uint8_t status_cold)
  563. {
  564. device_status.cold_status = status_cold;
  565. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  566. rt_pin_write(Cold_Pin,status_cold);
  567. return status_cold;
  568. }
  569. /*相机供电*/
  570. void ctr_photo(uint8_t status_photo)
  571. {
  572. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  573. rt_pin_write(PHOTO,status_photo);
  574. }
  575. /*************************************************
  576. Function: ctr_Cool_Fan
  577. Description: 散热风机的开关
  578. Calls:
  579. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  580. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  581. Input: ON-开,OFF-关
  582. Output: NULL
  583. Return: ON-开,OFF-关
  584. Others: NULL
  585. *************************************************/
  586. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  587. {
  588. device_status.cool_fan_status = status_cool;
  589. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  590. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  591. if(status_cool==OFF)
  592. {
  593. rt_pin_write(Cool_Fan_L,OFF);
  594. rt_pin_write(Cool_Fan_R,OFF);
  595. }
  596. if(status_cool==ON)
  597. {
  598. rt_pin_write(Cool_Fan_L,ON);
  599. rt_pin_write(Cool_Fan_R,OFF);
  600. }
  601. return device_status.cool_fan_status;
  602. }
  603. /*************************************************
  604. Function: ctr_Collect_Fan
  605. Description: 采集风机的开关
  606. Calls:
  607. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  608. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  609. Input: ON-开,OFF-关
  610. Output: NULL
  611. Return: ON-开,OFF-关
  612. Others: NULL
  613. *************************************************/
  614. void ctr_develop(void)
  615. {
  616. rt_kprintf("===============开始培养=================:%d\n",device_status.cul_time);
  617. //已经培养的时间-分钟
  618. device_status.stay_time = 0;
  619. rt_uint32_t ii = 0;
  620. while(device_status.develop_flag==1)
  621. {
  622. float stayedtime = 0.01;
  623. char stayed[5];
  624. stayedtime = (float)device_status.stay_time / 60.0;
  625. sprintf(stayed,"%0.02f",stayedtime);
  626. HMI_WRITE("workSta.staytime.txt=\"%s\"",stayed);
  627. ii++;
  628. LOG_I("developing...................................%d\n",ii);
  629. LOG_I("============================设定的培养温度:%d\n",device_status.heatvalue);
  630. LOG_I("============================培养仓实时温度:%d\n",device_status.pre_temp);
  631. if(device_status.pre_temp<(device_status.heatvalue-5))
  632. {
  633. //未制冷
  634. if(device_status.cold_status==0)
  635. {
  636. ctr_Heat(ON);
  637. }
  638. }
  639. else
  640. {
  641. ctr_Heat(OFF);
  642. }
  643. LOG_I("============================制冷开关:%d\n",device_status.cold_sw);
  644. if(device_status.cold_sw==1)
  645. {
  646. if(device_status.pre_temp>(device_status.heatvalue-10))
  647. {
  648. if(device_status.heat_status==0)
  649. {
  650. ctr_Cold(ON);
  651. }
  652. }
  653. else
  654. {
  655. ctr_Cold(OFF);
  656. }
  657. }
  658. if(ii%60==0)
  659. {
  660. device_status.stay_time++;
  661. if(device_status.stay_time / 60 == device_status.cul_time) device_status.develop_flag = 0;
  662. LOG_I("developed...................................%d分钟\n",device_status.stay_time);
  663. }
  664. if(device_status.box_temp>35)
  665. {
  666. ctr_Cool_Fan(ON);
  667. }
  668. else if (device_status.box_temp<30)
  669. {
  670. ctr_Cool_Fan(OFF);
  671. }
  672. rt_thread_delay(1000);
  673. }
  674. ctr_Cold(OFF);
  675. ctr_Heat(OFF);
  676. LOG_I("=========!!!培养完成!!!==========");
  677. }
  678. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  679. {
  680. device_status.collect_fan_status = status_collect;
  681. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  682. if(status_collect==OFF)
  683. {
  684. rt_pin_write(Collect_Fan,OFF);
  685. }
  686. if(status_collect==ON)
  687. {
  688. rt_pin_write(Collect_Fan,ON);
  689. }
  690. return device_status.collect_fan_status;
  691. }
  692. /*************************************************
  693. Function: ctr_develop
  694. Description: 培养载玻片孢子
  695. Calls:
  696. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  697. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  698. Input: v=0(复位) v=90转90度
  699. Output: NULL
  700. Return: NULL
  701. Others: NULL
  702. *************************************************/
  703. /*上仓门*/
  704. uint8_t ctr_cangmen_up(uint8_t status_up)
  705. {
  706. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  707. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  708. if(status_up==OFF)
  709. {
  710. rt_pin_write(Collect_fan_R,ON);
  711. rt_pin_write(Collect_fan_L,ON);
  712. device_status.ctr_up_motor = 0;
  713. rt_thread_delay(1000);
  714. }
  715. if(status_up==OPEN)
  716. {
  717. device_status.up_status = 1;
  718. rt_pin_write(Collect_fan_R,OFF);
  719. rt_pin_write(Collect_fan_L,OFF);
  720. rt_pin_write(Collect_fan_R,ON);
  721. rt_pin_write(Collect_fan_L,OFF);
  722. }
  723. if(status_up==CLOSE)
  724. {
  725. device_status.up_status = 0;
  726. rt_pin_write(Collect_fan_R,OFF);
  727. rt_pin_write(Collect_fan_L,OFF);
  728. rt_pin_write(Collect_fan_R,OFF);
  729. rt_pin_write(Collect_fan_L,ON);
  730. }
  731. return device_status.up_status;
  732. }
  733. //下仓门
  734. uint8_t ctr_cangmen_down(uint8_t status_down)
  735. {
  736. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  737. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  738. if(status_down==OFF)
  739. {
  740. rt_pin_write(C_DOWN_R,ON);
  741. rt_pin_write(C_DOWN_L,ON);
  742. device_status.ctr_down_motor = 0;
  743. rt_thread_delay(1000);
  744. }
  745. if(status_down==OPEN)
  746. {
  747. device_status.down_status = 1;
  748. rt_pin_write(C_DOWN_R,OFF);
  749. rt_pin_write(C_DOWN_L,OFF);
  750. rt_pin_write(C_DOWN_R,ON);
  751. rt_pin_write(C_DOWN_L,OFF);
  752. }
  753. if(status_down==CLOSE)
  754. {
  755. device_status.down_status = 0;
  756. rt_pin_write(C_DOWN_R,OFF);
  757. rt_pin_write(C_DOWN_L,OFF);
  758. rt_pin_write(C_DOWN_R,OFF);
  759. rt_pin_write(C_DOWN_L,ON);
  760. }
  761. return device_status.down_status;
  762. }
  763. //镇流器
  764. uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
  765. {
  766. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  767. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  768. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  769. rt_bool_t is_currently_on;
  770. rt_tick_t now;
  771. now = rt_tick_get();
  772. is_currently_on = (system_status.lamp_start_time != 0);
  773. if (status_lamp == ON)
  774. {
  775. // ====== 开灯 ======
  776. switch (lamp_num) {
  777. case 1:
  778. rt_pin_write(LAMP_SW, ON);
  779. device_status.lamp_status = ON;
  780. break;
  781. case 2:
  782. rt_pin_write(LAMP_1_SW, ON);
  783. device_status.lamp1_status = ON;
  784. break;
  785. case 3:
  786. rt_pin_write(LAMP_2_SW, ON);
  787. device_status.lamp2_status = ON;
  788. break;
  789. default:
  790. break;
  791. }
  792. // 已经开着 → 不重复计时
  793. if (is_currently_on) {
  794. return ON;
  795. }
  796. // 关 → 开,记录起始时间
  797. system_status.lamp_start_time = now;
  798. }
  799. else
  800. {
  801. // ====== 关灯 ======
  802. switch (lamp_num) {
  803. case 1:
  804. rt_pin_write(LAMP_SW, OFF);
  805. device_status.lamp_status = OFF;
  806. break;
  807. case 2:
  808. rt_pin_write(LAMP_1_SW, OFF);
  809. device_status.lamp1_status = OFF;
  810. break;
  811. case 3:
  812. rt_pin_write(LAMP_2_SW, OFF);
  813. device_status.lamp2_status = OFF;
  814. break;
  815. default:
  816. break;
  817. }
  818. // 已经关着 → 不重复结算
  819. if (!is_currently_on) {
  820. return OFF;
  821. }
  822. // 开 → 关,结算运行时间
  823. system_status.lamp_running_time +=
  824. (now - system_status.lamp_start_time);
  825. system_status.lamp_start_time = 0;
  826. }
  827. HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
  828. return device_status.lamp_status;
  829. }
  830. //震动电机
  831. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  832. {
  833. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  834. #if 0
  835. rt_pin_write(SHAKE_SW,ON);
  836. return 0;
  837. #endif
  838. if(status_shake==ON&&device_status.shake_ws==1)
  839. {
  840. if(device_status.dtype==3)
  841. {
  842. for(uint8_t shake_i = 0;shake_i<shake_sec;shake_i++)
  843. {
  844. rt_pin_write(SHAKE_SW,ON);
  845. rt_thread_delay(100);
  846. rt_pin_write(SHAKE_SW,OFF);
  847. rt_thread_delay(1000);
  848. }
  849. }
  850. rt_pin_write(SHAKE_SW,OFF);
  851. HMI_WRITE("cangWei.switchShake.val=%d",0);
  852. system_status.vibration_count += 1;
  853. }
  854. else
  855. {
  856. rt_pin_write(SHAKE_SW,OFF);
  857. HMI_WRITE("cangWei.switchShake.val=%d",0);
  858. }
  859. return status_shake;
  860. }
  861. //补光
  862. uint8_t ctr_led(uint8_t status_led)
  863. {
  864. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  865. if(device_status.version_type==6)
  866. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  867. if(status_led==ON)
  868. {
  869. rt_pin_write(LED_SW,ON);
  870. if(device_status.version_type==6)
  871. rt_pin_write(LED_P,ON);
  872. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  873. }
  874. else if(status_led==OFF){
  875. rt_pin_write(LED_SW,OFF);
  876. if(device_status.version_type==6)
  877. rt_pin_write(LED_P,OFF);
  878. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  879. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  880. }
  881. return status_led;
  882. }
  883. /*************************************************
  884. Function: ctr_turn
  885. Description: 转盘驱动
  886. Calls:
  887. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  888. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  889. Input: v=0(复位) v=90转90度
  890. Output: NULL
  891. Return: NULL
  892. Others: NULL
  893. *************************************************/
  894. void ctr_turn(uint8_t v,uint8_t sta)
  895. {
  896. DTU_Write("turn.mp3");
  897. LOG_I("================v===============:%d",v);
  898. rt_uint16_t turn_cnt = 0;
  899. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  900. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  901. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  902. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  903. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  904. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  905. {
  906. rt_pin_write(TURN_ZP_R, ON);
  907. rt_pin_write(TURN_ZP_L, OFF);
  908. //Motor_shake(ON,0);
  909. turn_cnt ++;
  910. rt_thread_delay(100);
  911. if(turn_cnt==1000)
  912. {
  913. turn_cnt = 0;
  914. rt_pin_write(TURN_ZP_R, OFF);
  915. rt_pin_write(TURN_ZP_L, OFF);
  916. //Motor_shake(OFF,0);
  917. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  918. DTU_Write("%s",turn_warn);
  919. rt_free(turn_warn);
  920. break;
  921. }
  922. }
  923. turn_cnt = 0;
  924. while(v==0&&rt_pin_read(LIMIT_0)==1)
  925. {
  926. rt_pin_write(TURN_ZP_R, ON);
  927. rt_pin_write(TURN_ZP_L, OFF);
  928. Motor_shake(ON,0);
  929. rt_thread_delay(200); //10 to 100
  930. turn_cnt ++;
  931. if(turn_cnt==300) //6000 to 600
  932. {
  933. turn_cnt = 0;
  934. // rt_pin_write(TURN_ZP_R, OFF);
  935. // rt_pin_write(TURN_ZP_L, OFF);
  936. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  937. // DTU_Write("%s",turn_warn);
  938. // rt_free(turn_warn);
  939. break;
  940. }
  941. }
  942. while(v==0&&rt_pin_read(LIMIT_0)==1)
  943. {
  944. rt_pin_write(TURN_ZP_R, ON);
  945. rt_pin_write(TURN_ZP_L, OFF);
  946. //Motor_shake(ON,0);
  947. rt_thread_delay(50); //10 to 50
  948. turn_cnt ++;
  949. if(turn_cnt==10) //6000 to 1200
  950. {
  951. turn_cnt = 0;
  952. rt_pin_write(TURN_ZP_R, OFF);
  953. rt_pin_write(TURN_ZP_L, OFF);
  954. //Motor_shake(OFF,0);
  955. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  956. DTU_Write("%s",turn_warn);
  957. rt_free(turn_warn);
  958. break;
  959. }
  960. }
  961. turn_cnt = 0;
  962. while(v==90&&rt_pin_read(LIMIT_90)==1)
  963. {
  964. rt_pin_write(TURN_ZP_R, ON);
  965. rt_pin_write(TURN_ZP_L, OFF);
  966. //Motor_shake(ON,0);
  967. rt_thread_delay(200);
  968. turn_cnt ++;
  969. if(turn_cnt==150)
  970. {
  971. turn_cnt = 0;
  972. // rt_pin_write(TURN_ZP_R, OFF);
  973. // rt_pin_write(TURN_ZP_L, OFF);
  974. // char *turn_warn =makeJson_Warn("超时", "转盘");
  975. // DTU_Write(turn_warn);
  976. // rt_free(turn_warn);
  977. break;
  978. }
  979. }
  980. while(v==90&&rt_pin_read(LIMIT_90)==1)
  981. {
  982. // rt_pin_write(TURN_ZP_R, ON);
  983. // rt_pin_write(TURN_ZP_L, OFF);
  984. Motor_shake(ON,0);
  985. rt_thread_delay(50);
  986. turn_cnt ++;
  987. if(turn_cnt==10)
  988. {
  989. turn_cnt = 0;
  990. rt_pin_write(TURN_ZP_R, OFF);
  991. rt_pin_write(TURN_ZP_L, OFF);
  992. //Motor_shake(OFF,0);
  993. char *turn_warn =makeJson_Warn("超时", "转盘");
  994. DTU_Write(turn_warn);
  995. rt_free(turn_warn);
  996. break;
  997. }
  998. }
  999. if(sta!=0)
  1000. {
  1001. //保存位置
  1002. device_status.run_sta = sta;
  1003. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  1004. }
  1005. turn_cnt = 0;
  1006. rt_pin_write(TURN_ZP_R, OFF);
  1007. rt_pin_write(TURN_ZP_L, OFF);
  1008. Motor_shake(OFF,0);
  1009. HMI_WRITE("cangWei.rotateFlag.val=0");
  1010. }
  1011. /*************************************************
  1012. Function: ctr_Motor_Driver
  1013. Description: 步进电机驱动
  1014. Calls:
  1015. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1016. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1017. Input: 50000000
  1018. Output: NULL
  1019. Return: shake_sec
  1020. Others: NULL
  1021. *************************************************/
  1022. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  1023. {
  1024. beep_init();
  1025. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  1026. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  1027. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  1028. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  1029. if(EN==0)
  1030. {
  1031. rt_pin_write(Motor_Power,0);
  1032. }
  1033. else{
  1034. rt_pin_write(Motor_EN, EN);
  1035. }
  1036. rt_pin_write(Motor_DIR, DIR);
  1037. beep_set(pwm, pwm/2); //200kzh - 50%
  1038. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  1039. {
  1040. if(time_out==0)
  1041. {
  1042. break;
  1043. }
  1044. rt_thread_delay(50);
  1045. time_out --;
  1046. }
  1047. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  1048. {
  1049. if(time_out==0)
  1050. {
  1051. break;
  1052. }
  1053. rt_thread_delay(50);
  1054. time_out --;
  1055. }
  1056. rt_pin_write(Motor_EN, OFF);
  1057. if(device_status.autotakepic==0)
  1058. ctr_lamp(OFF, 1);
  1059. beep_set(0, 0); //200kzh - 50%
  1060. }
  1061. /*************************************************
  1062. Function: ctr_Petroleum_jelly
  1063. Description: 涂凡士林
  1064. Calls:
  1065. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1066. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1067. Input: NULL
  1068. Output: NULL
  1069. Return: NULL
  1070. Others: NULL
  1071. *************************************************/
  1072. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1073. {
  1074. beep_init();
  1075. pwm_on(2,50000);
  1076. rt_thread_delay(delay*1000);
  1077. pwm_off(2,0);
  1078. rt_thread_delay(1000);
  1079. pwm_on(3, 50000);
  1080. rt_thread_delay(delay*1000);
  1081. pwm_off(3,0);
  1082. device_status.fsl_sw = 0;
  1083. }
  1084. //1.0转仓
  1085. void turnzp(void)
  1086. {
  1087. static uint16_t turn_timeout = 0;
  1088. //rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1089. //rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1090. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1091. // rt_pin_write(CLEAR_R,OFF);
  1092. // rt_pin_write(CLEAR_L,OFF);
  1093. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1094. {
  1095. rt_thread_delay(20);
  1096. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1097. {
  1098. // rt_pin_write(CLEAR_R,ON);
  1099. // rt_pin_write(CLEAR_L,OFF);
  1100. Motor_Juandai(ON);
  1101. rt_thread_delay(50);
  1102. turn_timeout++;
  1103. if(turn_timeout==600)
  1104. {
  1105. LOG_E("检测不到限位,电机强制停止");
  1106. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1107. DTU_Write("%s",ctr_zp_limit_warn);
  1108. rt_free(ctr_zp_limit_warn);
  1109. break;
  1110. }
  1111. }
  1112. break;
  1113. }
  1114. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1115. {
  1116. rt_thread_delay(20);
  1117. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1118. {
  1119. // rt_pin_write(CLEAR_R,ON);
  1120. // rt_pin_write(CLEAR_L,OFF);
  1121. Motor_Juandai(ON);
  1122. rt_thread_delay(50);
  1123. turn_timeout++;
  1124. if(turn_timeout==600)
  1125. {
  1126. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1127. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1128. DTU_Write("%s",ctr_zp_limit_warn);
  1129. rt_free(ctr_zp_limit_warn);
  1130. break;
  1131. }
  1132. }
  1133. break;
  1134. }
  1135. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1136. {
  1137. rt_thread_delay(20);
  1138. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1139. {
  1140. // rt_pin_write(CLEAR_R,OFF);
  1141. // rt_pin_write(CLEAR_L,OFF);
  1142. Motor_Juandai(OFF);
  1143. break;
  1144. }
  1145. else {
  1146. break;
  1147. }
  1148. }
  1149. Motor_Juandai(OFF);
  1150. // rt_pin_write(CLEAR_R,OFF);
  1151. // rt_pin_write(CLEAR_L,OFF);
  1152. turn_timeout = 0;
  1153. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1154. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1155. rt_thread_delay(1000);
  1156. }
  1157. //清扫-常闭型限位开关
  1158. //新结构使用推杆电机做清扫
  1159. uint8_t ctr_clear(uint16_t status_clear)
  1160. {
  1161. static uint16_t clear_num = 0;
  1162. #if PWM_EN == 0
  1163. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1164. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1165. #endif
  1166. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1167. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1168. /*************************************************************外接清扫
  1169. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1170. rt_pin_write(POWER_CLEAR,ON);
  1171. rt_thread_delay(1000*100);
  1172. rt_pin_write(POWER_CLEAR,OFF);
  1173. ************************************************************/
  1174. //停止
  1175. #if PWM_EN == 0
  1176. rt_pin_write(CLEAR_R,OFF);
  1177. rt_pin_write(CLEAR_L,OFF);
  1178. #else
  1179. Motor_Qingsao_R_PWM(OFF);
  1180. Motor_Qingsao_L_PWM(OFF);
  1181. #endif
  1182. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1183. rt_thread_delay(100);
  1184. //如果清扫位置在中间
  1185. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1186. {
  1187. #if PWM_EN == 0
  1188. rt_pin_write(CLEAR_R,ON);
  1189. rt_pin_write(CLEAR_L,OFF);
  1190. #else
  1191. Motor_Qingsao_R_PWM(ON);
  1192. Motor_Qingsao_L_PWM(OFF);
  1193. #endif
  1194. rt_thread_delay(50);
  1195. clear_num++;
  1196. //已到达检测位置
  1197. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1198. {
  1199. #if PWM_EN == 0
  1200. rt_pin_write(CLEAR_R,ON);
  1201. rt_pin_write(CLEAR_L,ON);
  1202. #else
  1203. Motor_Qingsao_R_PWM(OFF);
  1204. Motor_Qingsao_L_PWM(OFF);
  1205. #endif
  1206. rt_thread_delay(2000);
  1207. break;
  1208. }
  1209. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1210. {
  1211. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1212. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1213. #if PWM_EN == 0
  1214. rt_pin_write(CLEAR_R,ON);
  1215. rt_pin_write(CLEAR_L,ON);
  1216. #else
  1217. Motor_Qingsao_R_PWM(OFF);
  1218. Motor_Qingsao_L_PWM(OFF);
  1219. #endif
  1220. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1221. if(Read_Real_Voltage()/100<12)
  1222. {
  1223. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1224. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1225. device_status.clear_status = 0;
  1226. return 0;
  1227. }
  1228. break;
  1229. }
  1230. }
  1231. clear_num = 0;
  1232. //如果清扫位置在左侧
  1233. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1234. {
  1235. #if PWM_EN == 0
  1236. rt_pin_write(CLEAR_R,ON);
  1237. rt_pin_write(CLEAR_L,OFF);
  1238. #else
  1239. Motor_Qingsao_R_PWM(ON);
  1240. Motor_Qingsao_L_PWM(OFF);
  1241. #endif
  1242. rt_thread_delay(50);
  1243. clear_num++;
  1244. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1245. {
  1246. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1247. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1248. #if PWM_EN == 0
  1249. rt_pin_write(CLEAR_R,ON);
  1250. rt_pin_write(CLEAR_L,ON);
  1251. #else
  1252. Motor_Qingsao_R_PWM(OFF);
  1253. Motor_Qingsao_L_PWM(OFF);
  1254. #endif
  1255. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1256. if(Read_Real_Voltage()/100<12)
  1257. {
  1258. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1259. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1260. device_status.clear_status = 0;
  1261. return 0;
  1262. }
  1263. break;
  1264. }
  1265. }
  1266. clear_num = 0;
  1267. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1268. {
  1269. #if PWM_EN == 0
  1270. rt_pin_write(CLEAR_R,ON);
  1271. rt_pin_write(CLEAR_L,ON);
  1272. #else
  1273. Motor_Qingsao_R_PWM(OFF);
  1274. Motor_Qingsao_L_PWM(OFF);
  1275. #endif
  1276. if(Read_Real_Voltage()/100<12)
  1277. {
  1278. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1279. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1280. device_status.clear_status = 0;
  1281. return 0;
  1282. }
  1283. rt_thread_delay(2000);
  1284. break;
  1285. }
  1286. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1287. {
  1288. #if PWM_EN == 0
  1289. rt_pin_write(CLEAR_R,OFF);
  1290. rt_pin_write(CLEAR_L,ON);
  1291. #else
  1292. Motor_Qingsao_R_PWM(OFF);
  1293. Motor_Qingsao_L_PWM(ON);
  1294. #endif
  1295. rt_thread_delay(50);
  1296. clear_num++;
  1297. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1298. {
  1299. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1300. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1301. //DTU_Write("%s",Clear_ERR);
  1302. #if PWM_EN == 0
  1303. rt_pin_write(CLEAR_R,ON);
  1304. rt_pin_write(CLEAR_L,ON);
  1305. #else
  1306. Motor_Qingsao_R_PWM(OFF);
  1307. Motor_Qingsao_L_PWM(OFF);
  1308. #endif
  1309. rt_free(Clear_ERR);
  1310. if(Read_Real_Voltage()/100<12)
  1311. {
  1312. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1313. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1314. device_status.clear_status = 0;
  1315. return 0;
  1316. }
  1317. break;
  1318. }
  1319. }
  1320. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1321. {
  1322. #if PWM_EN == 0
  1323. rt_pin_write(CLEAR_R,ON);
  1324. rt_pin_write(CLEAR_L,ON);
  1325. #else
  1326. Motor_Qingsao_R_PWM(OFF);
  1327. Motor_Qingsao_L_PWM(OFF);
  1328. #endif
  1329. rt_thread_delay(2000);
  1330. break;
  1331. }
  1332. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1333. {
  1334. #if PWM_EN == 0
  1335. rt_pin_write(CLEAR_R,ON);
  1336. rt_pin_write(CLEAR_L,ON);
  1337. #else
  1338. Motor_Qingsao_R_PWM(OFF);
  1339. Motor_Qingsao_L_PWM(OFF);
  1340. #endif
  1341. if(device_status.version_type==41)
  1342. {
  1343. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1344. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1345. //DTU_Write("%s",Clear_ERR);
  1346. rt_free(Clear_ERR);
  1347. }
  1348. return 0;
  1349. }
  1350. clear_num = 0;
  1351. rt_thread_delay(2000);
  1352. #if PWM_EN == 0
  1353. rt_pin_write(CLEAR_R,ON);
  1354. rt_pin_write(CLEAR_L,ON);
  1355. #else
  1356. Motor_Qingsao_R_PWM(OFF);
  1357. Motor_Qingsao_L_PWM(OFF);
  1358. #endif
  1359. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1360. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1361. device_status.clear_status = 0;
  1362. return 1;
  1363. }
  1364. //路由器供电
  1365. void ctr_router(uint8_t status_router)
  1366. {
  1367. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1368. if(device_status.version_type==6)
  1369. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1370. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1371. if(status_router==ON)
  1372. {
  1373. rt_pin_write(ROUTER,ON);
  1374. //
  1375. #if LAMP_EN == 0
  1376. if(device_status.version_type==6)
  1377. rt_pin_write(ROUTER_P,ON);
  1378. #endif
  1379. }
  1380. else {
  1381. rt_pin_write(ROUTER,OFF);
  1382. //
  1383. #if LAMP_EN == 0
  1384. if(device_status.version_type==6)
  1385. rt_pin_write(ROUTER_P,OFF);
  1386. #endif
  1387. }
  1388. }
  1389. //清扫-推杆电机
  1390. void ctr_TG_Clear(uint16_t clear_timeout)
  1391. {
  1392. #if 0
  1393. int current_clear[200];
  1394. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1395. LOG_I("清扫电机待机:%d",Electric_current);
  1396. #endif
  1397. static uint16_t clear_count = 0;
  1398. clear_count = clear_timeout;
  1399. #if PWM_EN == 0
  1400. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1401. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1402. #endif
  1403. while(clear_count!=0)
  1404. {
  1405. #if PWM_EN == 0
  1406. rt_pin_write(CLEAR_R,ON);
  1407. rt_pin_write(CLEAR_L,OFF);
  1408. #else
  1409. Motor_Qingsao_R_PWM(ON);
  1410. Motor_Qingsao_L_PWM(OFF);
  1411. #endif
  1412. clear_count--;
  1413. rt_thread_delay(100);
  1414. }
  1415. #if PWM_EN == 0
  1416. rt_pin_write(CLEAR_R,ON);
  1417. rt_pin_write(CLEAR_L,ON);
  1418. #else
  1419. Motor_Qingsao_R_PWM(OFF);
  1420. Motor_Qingsao_L_PWM(OFF);
  1421. #endif
  1422. rt_thread_delay(3000);
  1423. clear_count = 0;
  1424. while(clear_timeout!=clear_count)
  1425. {
  1426. #if PWM_EN == 0
  1427. rt_pin_write(CLEAR_R,OFF);
  1428. rt_pin_write(CLEAR_L,ON);
  1429. #else
  1430. Motor_Qingsao_R_PWM(OFF);
  1431. Motor_Qingsao_L_PWM(ON);
  1432. #endif
  1433. // if(clear_count<200)
  1434. // current_clear[clear_count] = atoi(device_status.power);
  1435. clear_count++;
  1436. rt_thread_delay(100);
  1437. }
  1438. #if PWM_EN == 0
  1439. rt_pin_write(CLEAR_R,ON);
  1440. rt_pin_write(CLEAR_L,ON);
  1441. #else
  1442. Motor_Qingsao_R_PWM(OFF);
  1443. Motor_Qingsao_L_PWM(OFF);
  1444. #endif
  1445. #if 0
  1446. bubblesort(current_clear,200);
  1447. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1448. {
  1449. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1450. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1451. DTU_Write("%s",ctr_clear_warn);
  1452. rt_free(ctr_clear_warn);
  1453. }
  1454. #endif
  1455. rt_thread_delay(2000);
  1456. device_status.clear_status = 0;
  1457. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1458. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1459. }
  1460. // 全局静态变量,保存上次关闭的 tick
  1461. static rt_uint32_t s_last_off_tick = 0;
  1462. void ctr_RTU_DTU(uint8_t status_rtu)
  1463. {
  1464. // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
  1465. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1466. if (status_rtu == ON)
  1467. {
  1468. // 开机逻辑:必须满足冷却时间才能开机
  1469. rt_uint32_t current_tick = rt_tick_get();
  1470. rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
  1471. #if 1
  1472. if (s_last_off_tick != 0)
  1473. {
  1474. rt_uint32_t diff_tick = current_tick - s_last_off_tick;
  1475. if (diff_tick < cool_ticks)
  1476. {
  1477. // 未冷却完成,计算剩余时间
  1478. rt_uint32_t wait_ticks = cool_ticks - diff_tick;
  1479. rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
  1480. LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
  1481. // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
  1482. rt_thread_delay(wait_ticks);
  1483. }
  1484. }
  1485. #endif
  1486. // 冷却完成,执行开机
  1487. rt_pin_write(RTU_DTU, ON);
  1488. s_last_off_tick = 0; // 清除关机时间
  1489. LOG_W("RTU电源启动!!!");
  1490. }
  1491. else
  1492. {
  1493. // 关机逻辑:记录关机时刻tick
  1494. rt_pin_write(RTU_DTU, OFF);
  1495. s_last_off_tick = rt_tick_get();
  1496. LOG_W("RTU电源关闭:%d",s_last_off_tick);
  1497. }
  1498. }
  1499. /**
  1500. * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
  1501. */
  1502. void RTU_Power_Off_Safe(void)
  1503. {
  1504. /* 2. 【关键】关闭串口设备。
  1505. * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
  1506. * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
  1507. */
  1508. rt_device_close(dtuserial);
  1509. dtuserial->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  1510. /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
  1511. * extern UART_HandleTypeDef huart2;
  1512. * HAL_UART_DeInit(&huart2);
  1513. */
  1514. /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
  1515. * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
  1516. */
  1517. rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
  1518. rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
  1519. /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
  1520. rt_thread_mdelay(50);
  1521. /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
  1522. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1523. rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
  1524. LOG_I("RTU 12V Power OFF safely.\n");
  1525. }
  1526. /**
  1527. * @brief 恢复 RTU 12V 电源并重新初始化串口
  1528. */
  1529. void RTU_Power_On_Restore(void)
  1530. {
  1531. rt_thread_mdelay(5000);
  1532. /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
  1533. rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
  1534. LOG_I("RTU 12V Power ON.\n");
  1535. /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
  1536. * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
  1537. */
  1538. rt_thread_mdelay(5000);
  1539. __HAL_RCC_USART2_FORCE_RESET();
  1540. rt_thread_mdelay(1);
  1541. __HAL_RCC_USART2_RELEASE_RESET();
  1542. rt_err_t res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
  1543. if(res!=RT_EOK)
  1544. {
  1545. LOG_E("uart2打开失败!");
  1546. }else{
  1547. LOG_I("uart2 Open Success!");
  1548. }
  1549. /* 4. 【关键】重新注册接收回调
  1550. * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
  1551. * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
  1552. * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
  1553. */
  1554. dtu_uart2_reinit_rx();
  1555. }
  1556. /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
  1557. void cmd_rtu_restart(int argc, char** argv)
  1558. {
  1559. rt_kprintf("Starting RTU safe restart sequence...\n");
  1560. // 1. 安全断电
  1561. RTU_Power_Off_Safe();
  1562. // 2. 保持断电 30 秒 (确保电容彻底放电)
  1563. rt_kprintf("Waiting 30s for capacitor discharge...\n");
  1564. rt_thread_mdelay(30000);
  1565. // 3. 恢复供电并重建串口
  1566. RTU_Power_On_Restore();
  1567. rt_kprintf("RTU restart sequence completed.\n");
  1568. }
  1569. //海康拍照
  1570. void Hk_Take_io(void)
  1571. {
  1572. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1573. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1574. rt_pin_write(HK_TAKE, 1);
  1575. rt_pin_write(HK_IO, 0);
  1576. if(device_status.dtype==3)
  1577. Beep_Tip(1000);
  1578. if(device_status.dtype==7)
  1579. rt_thread_mdelay(1000);
  1580. rt_pin_write(HK_TAKE, 0);
  1581. rt_pin_write(HK_IO, 1);
  1582. }
  1583. void calorstat(uint16_t heat_time)
  1584. {
  1585. //uint8_t heat_up_cnt = 0;
  1586. uint16_t n = 0;
  1587. uint16_t delay_heat = 60;
  1588. FlagStatus heat_flg = RESET;
  1589. device_status.heat_time_min = 0;
  1590. device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
  1591. ctr_Heat(ON);
  1592. // UpLoad_Data(); //开始加热时的第一条数据
  1593. //恒温模式
  1594. for(n=0;n < heat_time;n++)
  1595. {
  1596. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1597. {
  1598. heat_flg = SET;
  1599. ctr_Heat(OFF);
  1600. while(delay_heat!=0)
  1601. {
  1602. delay_heat --;
  1603. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1604. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1605. rt_thread_delay(1000);
  1606. if(device_status.heattem>device_status.heat_tem_max + 1){
  1607. device_status.heat_tem_max = device_status.heattem;
  1608. }
  1609. //如果触发异常退出加热
  1610. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1611. {
  1612. rt_thread_delay(1000);
  1613. if(device_status.bat_status == 1)
  1614. {
  1615. LOG_W("============================触发异常退出加热==========================");
  1616. ctr_lamp(OFF,1);
  1617. ctr_Heat(OFF);
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. delay_heat = 60;
  1623. //跳出FOR循环
  1624. n = n + 60;
  1625. }
  1626. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1627. {
  1628. device_status.heat_tem_max = device_status.heattem;
  1629. ctr_Heat(ON);
  1630. }
  1631. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1632. {
  1633. // if(heat_up_cnt==0)
  1634. // {
  1635. // UpLoad_Data(); //加热到设定温度的数据
  1636. // heat_up_cnt = 1;
  1637. // }
  1638. device_status.heat_tem_max = device_status.heattem;
  1639. ctr_Heat(OFF);
  1640. }
  1641. if((n % 60)==0)
  1642. {
  1643. device_status.heat_time_min = device_status.heat_time_min + 1;
  1644. // if(device_status.heat_time_min % 5 == 0)
  1645. // {
  1646. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1647. // }
  1648. }
  1649. //如果触发异常退出加热
  1650. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1651. {
  1652. rt_thread_delay(1000);
  1653. if(device_status.bat_status == 1)
  1654. {
  1655. rt_kprintf("============================触发异常退出加热==========================");
  1656. ctr_lamp(OFF,1);
  1657. ctr_Heat(OFF);
  1658. break;
  1659. }
  1660. }
  1661. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1662. {
  1663. ctr_Heat(OFF);
  1664. rt_kprintf("异常退出!!!\n");
  1665. break;
  1666. }
  1667. rt_thread_delay(1000);
  1668. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1669. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1670. }
  1671. ctr_Heat(OFF);
  1672. //UpLoad_Data(); //加热结束的数据
  1673. }
  1674. //初始化所有控制驱动,关闭
  1675. void HardWare_Init(uint8_t boot)
  1676. {
  1677. Beep_Tip(200);
  1678. //关闭上仓门
  1679. ctr_cangmen_up(OFF);
  1680. if(device_status.dtype==3){
  1681. //关闭下仓门电机
  1682. ctr_cangmen_down(OFF);
  1683. }
  1684. //关闭镇流器
  1685. if(device_status.autotakepic==0)
  1686. ctr_lamp(OFF,1);
  1687. //关闭震动电机
  1688. ctr_shake(OFF,0);
  1689. //关闭加热
  1690. ctr_Heat(OFF);
  1691. //关闭补光灯
  1692. ctr_led(OFF);
  1693. //关闭12V供电(海康相机,RTU)
  1694. //ctr_photo(OFF);
  1695. }
  1696. //拍照
  1697. void HK_TakePhoto(void)
  1698. {
  1699. //打开补光灯
  1700. ctr_led(ON);
  1701. //播放语音
  1702. DTU_Write("takephoto.mp3");
  1703. rt_thread_delay(1000);
  1704. //吸虫塔,水稻
  1705. if(device_status.version_type!=6&&device_status.version_type!=4)
  1706. {
  1707. rt_thread_delay(1000*15);
  1708. }
  1709. char *takephotojson = makeJson_takephoto();
  1710. DTU_Write("%s",takephotojson);
  1711. rt_free(takephotojson);
  1712. //触发海康相机拍照
  1713. Hk_Take_io();
  1714. if(device_status.version_type!=6)
  1715. {
  1716. rt_thread_delay(1000*45);
  1717. }
  1718. else {
  1719. ctr_led(ON);
  1720. rt_thread_delay(1000*19);
  1721. }
  1722. ctr_led(OFF);
  1723. }
  1724. //光控测试任务
  1725. void SunTest(void)
  1726. {
  1727. rt_uint16_t ui_timeout_l = 0;
  1728. //未检测到光照,一秒扫描等待,超时30S
  1729. while(Read_Light_Voltage()<500)
  1730. {
  1731. ui_timeout_l ++;
  1732. rt_thread_delay(1000);
  1733. if(ui_timeout_l==30) break;
  1734. }
  1735. rt_thread_delay(3000);
  1736. if(Read_Light_Voltage()<500)
  1737. {
  1738. device_status.sun_status = 0;//光控状态
  1739. }
  1740. if(Read_Light_Voltage()>500)
  1741. {
  1742. rt_thread_delay(1000);
  1743. }
  1744. rt_thread_delay(3000);
  1745. if(Read_Light_Voltage()>500)
  1746. {
  1747. device_status.sun_status = 1;
  1748. ctr_lamp(OFF,1);
  1749. }
  1750. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1751. device_status.sun_test = 0;
  1752. device_status.work_status=0;
  1753. }
  1754. /*上电初始化*/
  1755. void ResetInit(void)
  1756. {
  1757. if(device_status.dtype==7)
  1758. {
  1759. ctr_Motor_Driver(0, 0, 0, 0);
  1760. //关闭电磁阀
  1761. ctr_shake(OFF, 0);
  1762. //关闭加热
  1763. ctr_Heat(OFF);
  1764. //关闭制冷
  1765. ctr_Cold(OFF);
  1766. //关闭补光灯
  1767. ctr_led(OFF);
  1768. //转盘复位
  1769. if(device_status.run_sta==0)
  1770. ctr_turn(0, 0);
  1771. }
  1772. if(device_status.version_type!=6&&device_status.version_type!=7)
  1773. {
  1774. device_status.work_done = 1;
  1775. //1.打开路由器供电
  1776. if(device_status.power_status==1)
  1777. {
  1778. ctr_router(ON);
  1779. //2.打开相机供电
  1780. ctr_photo(ON);
  1781. }
  1782. //3.关闭镇流器
  1783. ctr_lamp(OFF,1);
  1784. #if LAMP_EN == 1
  1785. ctr_lamp(OFF,2);
  1786. ctr_lamp(OFF,3);
  1787. #endif
  1788. //4.关闭震动电机
  1789. ctr_shake(OFF, 0);
  1790. //5.关闭加热
  1791. ctr_Heat(OFF);
  1792. //6.关闭补光灯
  1793. ctr_led(OFF);
  1794. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1795. {
  1796. LOG_W("欠压!");
  1797. return;
  1798. }
  1799. //7.关闭上仓门
  1800. device_status.ctr_up_motor = 2;
  1801. // while(device_status.ctr_up_motor!=0)
  1802. // {
  1803. // LOG_I("关闭上仓门...");
  1804. // rt_thread_delay(100);
  1805. // }
  1806. //8.关闭下仓门
  1807. device_status.ctr_down_motor = 2;
  1808. while(device_status.ctr_down_motor!=0)
  1809. {
  1810. LOG_I("关闭下仓门....");
  1811. rt_thread_delay(100);
  1812. }
  1813. //9.清扫
  1814. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1815. {
  1816. LOG_W("异常退出!!!\n");
  1817. return;
  1818. }
  1819. #if 0//LAMP_EN == 0
  1820. ctr_Fan_PUT(OFF);
  1821. ctr_Fan_IN(OFF);
  1822. #endif
  1823. if(device_status.version_type==4||
  1824. device_status.version_type==41||
  1825. device_status.version_type==42||
  1826. device_status.version_type==43||
  1827. device_status.version_type==12)
  1828. {
  1829. if(device_status.photo_ws==0){ //2026-06-05 修复工作结束后最后一张照片拍到清扫电机问题;
  1830. device_status.clear_status=1;
  1831. }
  1832. while(device_status.clear_status==1){
  1833. rt_thread_delay(1000);
  1834. }
  1835. #if DZ_260917_1 == 1
  1836. //再执行4次清扫
  1837. for(int i=0;i<4;i++)
  1838. {
  1839. if(device_status.photo_ws==0) device_status.clear_status=1;
  1840. while(device_status.clear_status==1){
  1841. rt_thread_delay(1000);
  1842. }
  1843. }
  1844. #endif
  1845. }
  1846. #if DZ_260519 == 1
  1847. device_status.clear_status = 2;
  1848. #endif
  1849. #if LAMP_EN == 0
  1850. //排虫
  1851. if(device_status.handle_bugger==0){
  1852. device_status.run_sta = 111; //排虫
  1853. }else{
  1854. device_status.run_sta = 112; //收虫
  1855. }
  1856. #endif
  1857. }
  1858. else {
  1859. if(device_status.power_status==1)
  1860. {
  1861. //1.打开路由器供电
  1862. ctr_router(ON);
  1863. ctr_RTU_DTU(ON);
  1864. //2.打开相机供电
  1865. ctr_photo(ON);
  1866. }
  1867. //5.关闭加热
  1868. //6.关闭补光灯
  1869. if(device_status.photo_ws==0){
  1870. ctr_led(OFF);
  1871. }
  1872. }
  1873. }
  1874. /*三色光源*/
  1875. void Led_Check(uint8_t tp)
  1876. {
  1877. if(tp==3)
  1878. {
  1879. if(device_status.light_over_tim>2*3600)
  1880. {
  1881. device_status.work_status = 1;
  1882. device_status.lamp_status = 1;
  1883. device_status.lamp1_status = 0;
  1884. device_status.lamp2_status = 0;
  1885. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1886. }
  1887. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1888. device_status.work_status = 1;
  1889. device_status.lamp_status = 0;
  1890. device_status.lamp1_status = 1;
  1891. device_status.lamp2_status = 0;
  1892. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1893. }
  1894. else if (device_status.light_over_tim<1*3600) {
  1895. device_status.work_status = 1;
  1896. device_status.lamp_status = 0;
  1897. device_status.lamp1_status = 0;
  1898. device_status.lamp2_status = 1;
  1899. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1900. }
  1901. }
  1902. else if (tp==6) {
  1903. if(device_status.light_over_tim>4*3600)
  1904. {
  1905. device_status.work_status = 1;
  1906. device_status.lamp_status = 1;
  1907. device_status.lamp1_status = 0;
  1908. device_status.lamp2_status = 0;
  1909. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1910. }
  1911. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1912. device_status.work_status = 1;
  1913. device_status.lamp_status = 0;
  1914. device_status.lamp1_status = 1;
  1915. device_status.lamp2_status = 0;
  1916. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1917. }
  1918. else if (device_status.light_over_tim<7200) {
  1919. device_status.work_status = 1;
  1920. device_status.lamp_status = 0;
  1921. device_status.lamp1_status = 0;
  1922. device_status.lamp2_status = 1;
  1923. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1924. }
  1925. }
  1926. else if (tp==9) {
  1927. if(device_status.light_over_tim>6*3600)
  1928. {
  1929. device_status.work_status = 1;
  1930. device_status.lamp_status = 1;
  1931. device_status.lamp1_status = 0;
  1932. device_status.lamp2_status = 0;
  1933. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1934. }
  1935. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1936. device_status.work_status = 1;
  1937. device_status.lamp_status = 0;
  1938. device_status.lamp1_status = 1;
  1939. device_status.lamp2_status = 0;
  1940. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1941. }
  1942. else if (device_status.light_over_tim<3*3600) {
  1943. device_status.work_status = 1;
  1944. device_status.lamp_status = 0;
  1945. device_status.lamp1_status = 0;
  1946. device_status.lamp2_status = 1;
  1947. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1948. }
  1949. }
  1950. }
  1951. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1952. void HardWare_Check(uint8_t boot)
  1953. {
  1954. if(boot==0&&device_status.version_type!=6)
  1955. {
  1956. //停止所有动作 -完成
  1957. HardWare_Init(boot);
  1958. }
  1959. else {
  1960. //停止所有动作
  1961. //读取记录待机功耗(实测大概70mA)
  1962. //(需减去屏幕的功耗大概120mA)
  1963. //动作上仓门,读取功率值保存
  1964. //动作下仓门,读取功率值保存
  1965. //动作清扫,读取功率值保存
  1966. //打开镇流器,读取功率值保存
  1967. //打开海康相机供电,读取功率值保存
  1968. //打开补光灯,读取功率值保存
  1969. //打开路由器/RTU供电,读取功率值保存
  1970. //打开加热,读取功率值保存
  1971. }
  1972. }
  1973. static void handle_rain(void)
  1974. {
  1975. //执行预处理
  1976. //打开上下仓门
  1977. device_status.ctr_up_motor = 1;
  1978. device_status.ctr_down_motor = 1;
  1979. //等待5分钟
  1980. for(int i=0;i<300;i++)
  1981. {
  1982. rt_thread_delay(1000);
  1983. LOG_I("等待5分钟...%d",i);
  1984. }
  1985. //清扫10次
  1986. for(int i=0;i<10;i++)
  1987. {
  1988. device_status.clear_status = 1;
  1989. while (device_status.clear_status==1) {
  1990. rt_thread_delay(10000);
  1991. LOG_I("清扫中...%d",i);
  1992. }
  1993. }
  1994. //关闭上下仓门
  1995. device_status.ctr_up_motor = 2;
  1996. device_status.ctr_down_motor = 2;
  1997. }
  1998. static uint8_t prework_state = 0;
  1999. static uint32_t prework_timer = 0;
  2000. static uint8_t clean_count = 0;
  2001. void WorkCheck(void)
  2002. {
  2003. static rt_uint32_t light_timeout = 3600;
  2004. while(device_status.ds == 1 &&
  2005. device_status.rain_status == 0 &&
  2006. device_status.temp_status == 0 &&
  2007. device_status.bat_status == 0)
  2008. {
  2009. if(device_status.timeSwitch == 1) // 时控模式
  2010. {
  2011. LOG_I("***********时控*****************");
  2012. if(device_status.startBar == device_status.stopBar)
  2013. {
  2014. // 开始结束时间相同则24小时模式
  2015. device_status.work_status = 1;
  2016. rt_thread_delay(10000);
  2017. continue;
  2018. }
  2019. time_t now;
  2020. time(&now);
  2021. struct tm now_tm = *localtime(&now);
  2022. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  2023. rt_uint32_t start_sec = device_status.startBar * 3600;
  2024. rt_uint32_t end_sec = device_status.stopBar * 3600;
  2025. #if DZ_260623_2 == 1
  2026. //判断当前时间是否是开始时间的前30分钟
  2027. if(curr_sec >= start_sec - 1800 && prework_state == 0)
  2028. {
  2029. prework_state = 1;
  2030. }
  2031. // 预处理状态机
  2032. switch(prework_state) {
  2033. case 1: // 打开上下仓门
  2034. device_status.ctr_up_motor = 1;
  2035. device_status.ctr_down_motor = 1;
  2036. // 仓门已打开,进入等待阶段
  2037. prework_state = 2;
  2038. prework_timer = 5 * 60; // 5分钟(1000ms为单位)
  2039. break;
  2040. case 2: // 等待5分钟
  2041. prework_timer--;
  2042. if(prework_timer == 0) {
  2043. prework_state = 3;
  2044. clean_count = 0;
  2045. LOG_I("DZ260623_2: 等待5分钟结束,开始清扫");
  2046. }
  2047. break;
  2048. case 3: // 清扫10次
  2049. if(device_status.clear_status == 0) {
  2050. device_status.clear_status = 1;
  2051. clean_count++;
  2052. LOG_I("DZ260623_2: 第%d次清扫", clean_count);
  2053. }
  2054. if(clean_count >= 10) {
  2055. prework_state = 4;
  2056. }
  2057. break;
  2058. case 4: // 关闭上下仓门
  2059. device_status.ctr_up_motor = 2;
  2060. device_status.ctr_down_motor = 2;
  2061. prework_state = 0; // 完成
  2062. LOG_I("DZ260623_2: 预处理完成");
  2063. break;
  2064. }
  2065. #endif
  2066. // 修复跨天判断逻辑
  2067. if (start_sec < end_sec)
  2068. {
  2069. // 不跨天模式
  2070. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  2071. }
  2072. else
  2073. {
  2074. // 跨天模式
  2075. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  2076. }
  2077. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  2078. device_status.work_status ? "工作" : "待机",
  2079. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  2080. device_status.startBar, device_status.stopBar);
  2081. rt_thread_delay(10000);
  2082. }
  2083. else // 光控模式
  2084. {
  2085. LOG_I("光控定时模式!");
  2086. if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
  2087. {
  2088. // 夜晚且有剩余时间:工作
  2089. if(device_status.light_over_tim > 0) {
  2090. device_status.light_over_tim--;
  2091. }
  2092. light_timeout = 3600; // 重置超时计数器
  2093. device_status.work_status = 1;
  2094. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  2095. }
  2096. else
  2097. {
  2098. // 仅在白天递减超时计数器
  2099. if(device_status.sun_status == 1 && light_timeout > 0) {
  2100. light_timeout--;
  2101. }
  2102. device_status.work_status = 0;
  2103. //没有在测试并且没有在自检
  2104. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  2105. {
  2106. ctr_lamp(OFF,1);
  2107. }
  2108. // 连续1小时白天后重置
  2109. if(light_timeout == 0) {
  2110. device_status.light_over_tim = device_status.sunBar * 3600;
  2111. light_timeout = 3600; // 重置防止负数
  2112. }
  2113. }
  2114. }
  2115. rt_thread_delay(1000);
  2116. }
  2117. // 异常状态处理while之外
  2118. device_status.work_status = 0;
  2119. prework_state = 0;
  2120. rt_thread_delay(1000);
  2121. }
  2122. void WorkTask_BZY(void)
  2123. {
  2124. if(device_status.ds==1)
  2125. {
  2126. //时间段判断
  2127. //先判断第一个时间段
  2128. time_t now;
  2129. time(&now);
  2130. static struct tm now_stamp; //现在
  2131. static struct tm cul_sh1_stamp; //开始1
  2132. static struct tm cul_eh1_stamp; //结束1
  2133. static struct tm cul_sh2_stamp; //开始2
  2134. static struct tm cul_eh2_stamp; //结束2
  2135. static struct tm cul_sh3_stamp; //开始3
  2136. static struct tm cul_eh3_stamp; //结束3
  2137. now_stamp = *localtime(&now);
  2138. //获取定时开始的时间戳
  2139. cul_sh1_stamp = *localtime(&now);
  2140. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2141. cul_sh1_stamp.tm_min = 0;
  2142. cul_sh1_stamp.tm_sec = 0;
  2143. cul_sh2_stamp = *localtime(&now);
  2144. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2145. cul_sh2_stamp.tm_min = 0;
  2146. cul_sh2_stamp.tm_sec = 0;
  2147. cul_sh3_stamp = *localtime(&now);
  2148. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2149. cul_sh3_stamp.tm_min = 0;
  2150. cul_sh3_stamp.tm_sec = 0;
  2151. //获取定时结束的时间戳
  2152. cul_eh1_stamp = *localtime(&now);
  2153. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2154. cul_eh1_stamp.tm_min = 0;
  2155. cul_eh1_stamp.tm_sec = 0;
  2156. cul_eh2_stamp = *localtime(&now);
  2157. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2158. cul_eh2_stamp.tm_min = 0;
  2159. cul_eh2_stamp.tm_sec = 0;
  2160. cul_eh3_stamp = *localtime(&now);
  2161. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2162. cul_eh3_stamp.tm_min = 0;
  2163. cul_eh3_stamp.tm_sec = 0;
  2164. LOG_I("采集时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh1_stamp.tm_year+1900,cul_sh1_stamp.tm_mon+1,cul_sh1_stamp.tm_mday,cul_sh1_stamp.tm_hour,cul_sh1_stamp.tm_min,cul_sh1_stamp.tm_sec);
  2165. LOG_I("采集时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh2_stamp.tm_year+1900,cul_sh2_stamp.tm_mon+1,cul_sh2_stamp.tm_mday,cul_sh2_stamp.tm_hour,cul_sh2_stamp.tm_min,cul_sh2_stamp.tm_sec);
  2166. LOG_I("采集时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh3_stamp.tm_year+1900,cul_sh3_stamp.tm_mon+1,cul_sh3_stamp.tm_mday,cul_sh3_stamp.tm_hour,cul_sh3_stamp.tm_min,cul_sh3_stamp.tm_sec);
  2167. LOG_I("结束时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh1_stamp.tm_year+1900,cul_eh1_stamp.tm_mon+1,cul_eh1_stamp.tm_mday,cul_eh1_stamp.tm_hour,cul_eh1_stamp.tm_min,cul_eh1_stamp.tm_sec);
  2168. LOG_I("结束时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh2_stamp.tm_year+1900,cul_eh2_stamp.tm_mon+1,cul_eh2_stamp.tm_mday,cul_eh2_stamp.tm_hour,cul_eh2_stamp.tm_min,cul_eh2_stamp.tm_sec);
  2169. LOG_I("结束时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh3_stamp.tm_year+1900,cul_eh3_stamp.tm_mon+1,cul_eh3_stamp.tm_mday,cul_eh3_stamp.tm_hour,cul_eh3_stamp.tm_min,cul_eh3_stamp.tm_sec);
  2170. //此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2171. //当前时间戳小于第一段开始时间戳
  2172. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2173. {
  2174. time_t now;
  2175. time(&now);
  2176. cul_sh1_stamp = *localtime(&now);
  2177. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2178. cul_sh1_stamp.tm_min = 0;
  2179. cul_sh1_stamp.tm_sec = 0;
  2180. now_stamp = *localtime(&now);
  2181. LOG_I("======收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh1_stamp.tm_year+1900,cul_sh1_stamp.tm_mon+1,cul_sh1_stamp.tm_mday,cul_sh1_stamp.tm_hour,cul_sh1_stamp.tm_min,cul_sh1_stamp.tm_sec);
  2182. LOG_I("======未到收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.",now_stamp.tm_year+1900,now_stamp.tm_mon+1,now_stamp.tm_mday,now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec);
  2183. LOG_D("run_sta:%d",device_status.run_sta);
  2184. }
  2185. //当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2186. if(difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0&&difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2187. {
  2188. time_t now;
  2189. time(&now);
  2190. cul_sh2_stamp = *localtime(&now);
  2191. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2192. cul_sh2_stamp.tm_min = 0;
  2193. cul_sh2_stamp.tm_sec = 0;
  2194. now_stamp = *localtime(&now);
  2195. LOG_I("==============收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n",cul_sh2_stamp.tm_year+1900,cul_sh2_stamp.tm_mon+1,cul_sh2_stamp.tm_mday,cul_sh2_stamp.tm_hour,cul_sh2_stamp.tm_min,cul_sh2_stamp.tm_sec);
  2196. LOG_I("==============未到收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n",now_stamp.tm_year+1900,now_stamp.tm_mon+1,now_stamp.tm_mday,now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec);
  2197. LOG_D("run_sta:%d",device_status.run_sta);
  2198. }
  2199. //如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2200. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0&&difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2201. {
  2202. //转盘到涂凡士林位置
  2203. if(device_status.run_sta==0)
  2204. {
  2205. LOG_I("start to collect!!!");
  2206. DTU_Write("collect.mp3");
  2207. ctr_turn(90,4);
  2208. rt_thread_delay(2000);
  2209. //涂凡士林
  2210. ctr_Petroleum_jelly(10);
  2211. rt_thread_delay(5000);
  2212. }
  2213. /*转盘到采集位置*/
  2214. if(device_status.run_sta==4)
  2215. ctr_turn(90,1);
  2216. //只有开始和结束时刻不同的时间段,才有效
  2217. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)||(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2218. {
  2219. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)&& difftime(mktime(&now_stamp),mktime(&cul_sh1_stamp)) > 0)
  2220. {
  2221. device_status.work_status = 1;
  2222. ctr_Collect_Fan(ON);
  2223. //当前时间戳小于第一段结束时间戳
  2224. while(difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp))>0)
  2225. {
  2226. time(&now);
  2227. now_stamp = *localtime(&now);
  2228. cul_eh1_stamp = *localtime(&now);
  2229. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2230. cul_eh1_stamp.tm_min = 0;
  2231. cul_eh1_stamp.tm_sec = 0;
  2232. if(device_status.run_sta!=1) break;
  2233. LOG_I("1===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh1_stamp));
  2234. rt_thread_mdelay(10000);
  2235. }
  2236. }
  2237. else {
  2238. device_status.work_status=0;
  2239. ctr_Collect_Fan(OFF);
  2240. LOG_I("未到收集1时间!");
  2241. }
  2242. if(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp),mktime(&cul_sh2_stamp)) > 0)
  2243. {
  2244. device_status.work_status = 1;
  2245. ctr_Collect_Fan(ON);
  2246. while(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp))>0)
  2247. {
  2248. time(&now);
  2249. now_stamp = *localtime(&now);
  2250. cul_eh2_stamp = *localtime(&now);
  2251. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2252. cul_eh2_stamp.tm_min = 0;
  2253. cul_eh2_stamp.tm_sec = 0;
  2254. if(device_status.run_sta!=1) break;
  2255. LOG_I("2===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh2_stamp));
  2256. rt_thread_mdelay(10000);
  2257. }
  2258. }
  2259. else {
  2260. device_status.work_status=0;
  2261. ctr_Collect_Fan(OFF);
  2262. LOG_I("未到收集2时间!");
  2263. }
  2264. }
  2265. else
  2266. {
  2267. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2268. }
  2269. }
  2270. if(device_status.run_sta==2)
  2271. {
  2272. ctr_Collect_Fan(OFF);
  2273. device_status.develop_flag = 1;
  2274. device_status.work_status = 2;
  2275. ctr_develop(); //培养
  2276. ctr_turn(90,3);
  2277. }
  2278. if(device_status.run_sta==3)
  2279. {
  2280. ctr_Collect_Fan(OFF);
  2281. device_status.work_status = 3;
  2282. device_status.autotakepic=1; //对焦拍照
  2283. }
  2284. //在培养之前再次确认时间戳
  2285. time(&now);
  2286. now_stamp = *localtime(&now);
  2287. cul_eh2_stamp = *localtime(&now);
  2288. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2289. cul_eh2_stamp.tm_min = 0;
  2290. cul_eh2_stamp.tm_sec = 0;
  2291. //当前时间戳大于收集结束时间戳并且run_sta==1
  2292. if(device_status.run_sta==1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2293. {
  2294. ctr_Collect_Fan(OFF);
  2295. LOG_I("===============collect ok,turn to drop!!!");
  2296. device_status.develop_flag=1;
  2297. device_status.work_status=2; //现在是培养
  2298. DTU_Write("develop.mp3");
  2299. if(device_status.run_sta==1)
  2300. ctr_turn(90,2); /*转盘到培养位置*/
  2301. rt_thread_delay(1000);
  2302. ctr_shake(ON,device_status.shake_sec);
  2303. rt_thread_delay(1000);
  2304. ctr_develop(); //培养
  2305. if(device_status.run_sta==2)
  2306. ctr_turn(90,3); /*转盘到拍照位置*/
  2307. rt_thread_delay(1000);
  2308. device_status.work_status=3; //现在是拍照
  2309. device_status.autotakepic=1; //对焦拍照
  2310. while(device_status.autotakepic==0)
  2311. {
  2312. rt_thread_delay(1000);
  2313. LOG_I("正在对焦拍照............");
  2314. }
  2315. device_status.work_status = 0; //待机
  2316. DTU_Write("wait.mp3");
  2317. LOG_I("=====================work finished==================");
  2318. }
  2319. rt_thread_delay(1000);
  2320. }
  2321. device_status.work_status = 0; //待机
  2322. LOG_W("==================!!!设备已待机!!!===================");
  2323. }
  2324. void WorkTask_BZYV2(void)
  2325. {
  2326. //打开采集风机
  2327. ctr_Collect_Fan(ON);
  2328. //打开胶带步进电机
  2329. //等待采集结束
  2330. //拍照
  2331. //打开相机供电
  2332. //打开补光灯
  2333. //滑台归位
  2334. //滑台启动
  2335. }
  2336. //工作任务流程s
  2337. void WorkTask(void)
  2338. {
  2339. static int clt_time = 0; //收集时间差秒数
  2340. static int heat_time = 0;
  2341. static uint8_t workloop = 0;
  2342. LOG_I("工作状态:%d",device_status.work_status);
  2343. LOG_I("开关机状态:%d",device_status.ds);
  2344. 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)
  2345. {
  2346. workloop++;
  2347. device_status.work_done = 0;
  2348. if(clt_time==0)
  2349. {
  2350. #if 0
  2351. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2352. {
  2353. LOG_W("相机电流不足!");
  2354. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2355. DTU_Write("%s",ctr_hk_p_warn);
  2356. rt_free(ctr_hk_p_warn);
  2357. }
  2358. #endif
  2359. #if 0//LAMP_EN == 0
  2360. ctr_Fan_PUT(ON);
  2361. #endif
  2362. DTU_Write("workstart.mp3");
  2363. }
  2364. //1.打开灯管
  2365. #if LAMP_EN == 1
  2366. if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
  2367. {
  2368. ctr_lamp(ON,1);
  2369. ctr_lamp(OFF,2);
  2370. ctr_lamp(OFF,3);
  2371. }
  2372. else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
  2373. ctr_lamp(OFF,1);
  2374. ctr_lamp(ON,2);
  2375. ctr_lamp(OFF,3);
  2376. }
  2377. else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
  2378. ctr_lamp(OFF,1);
  2379. ctr_lamp(OFF,2);
  2380. ctr_lamp(ON,3);
  2381. }
  2382. #else
  2383. ctr_lamp(ON,1);
  2384. if(device_status.version_type==12)
  2385. {
  2386. ctr_Heat(ON);
  2387. }
  2388. #endif
  2389. LOG_I("打开灯管!!!");
  2390. #if 0
  2391. rt_thread_delay(2000);
  2392. if(atoi(device_status.power)<450)
  2393. {
  2394. LOG_I("Current is:%d",atoi(device_status.power));
  2395. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  2396. {
  2397. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  2398. DTU_Write("%s",lamp_err_warn);
  2399. rt_free(lamp_err_warn);
  2400. }
  2401. }
  2402. #endif
  2403. if(device_status.version_type!=5)
  2404. {
  2405. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  2406. //2.打开上仓门
  2407. LOG_I("打开上仓门!!!");
  2408. device_status.ctr_up_motor = 1;
  2409. //3.收集
  2410. clt_time = device_status.clt_time * 60;
  2411. while(clt_time!=1)
  2412. {
  2413. #if 0//LAMP_EN == 0
  2414. ctr_Fan_IN(ON);
  2415. #endif
  2416. clt_time --;
  2417. rt_thread_delay(1000);
  2418. #if 0//LAMP_EN == 0
  2419. if(clt_time%30==0)
  2420. ctr_Fan_PUT(OFF);
  2421. #endif
  2422. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2423. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"",device_status.clt_time,clt_time);
  2424. //如果触发异常
  2425. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2426. {
  2427. ctr_lamp(OFF,1);
  2428. #if LAMP_EN == 1
  2429. ctr_lamp(OFF,2);
  2430. ctr_lamp(OFF,3);
  2431. #endif
  2432. #if 0//LAMP_EN == 0
  2433. ctr_Fan_IN(OFF);
  2434. #endif
  2435. rt_kprintf("异常退出!!!\n");
  2436. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2437. break;
  2438. }
  2439. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2440. {
  2441. ctr_lamp(OFF,1);
  2442. #if LAMP_EN == 1
  2443. ctr_lamp(OFF,2);
  2444. ctr_lamp(OFF,3);
  2445. #endif
  2446. #if 0//LAMP_EN == 0
  2447. ctr_Fan_IN(OFF);
  2448. #endif
  2449. rt_kprintf("异常退出!!!\n");
  2450. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2451. break;
  2452. }
  2453. }
  2454. clt_time = 1;
  2455. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2456. {
  2457. ctr_lamp(OFF,1);
  2458. #if LAMP_EN == 1
  2459. ctr_lamp(OFF,2);
  2460. ctr_lamp(OFF,3);
  2461. #endif
  2462. #if 0//LAMP_EN == 0
  2463. ctr_Fan_IN(OFF);
  2464. #endif
  2465. rt_kprintf("异常退出!!!\n");
  2466. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2467. break;
  2468. }
  2469. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2470. {
  2471. ctr_lamp(OFF,1);
  2472. #if LAMP_EN == 1
  2473. ctr_lamp(OFF,2);
  2474. ctr_lamp(OFF,3);
  2475. #endif
  2476. #if 0//LAMP_EN == 0
  2477. ctr_Fan_IN(OFF);
  2478. #endif
  2479. rt_kprintf("异常退出!!!\n");
  2480. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2481. break;
  2482. }
  2483. #if 0//LAMP_EN == 0
  2484. ctr_Fan_IN(OFF);
  2485. #endif
  2486. //4.关闭上仓门
  2487. device_status.ctr_up_motor = 2;
  2488. rt_kprintf("关闭上仓门!!!\n");
  2489. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  2490. while(device_status.ctr_up_motor!=0)
  2491. {
  2492. rt_thread_delay(100);
  2493. LOG_I("等待上仓门关闭!!!");
  2494. }
  2495. //5.加热
  2496. if(device_status.heat_sw==0&&device_status.version_type!=12)
  2497. {
  2498. calorstat(device_status.heattime*60);
  2499. }
  2500. else {
  2501. heat_time = device_status.heattime * 60;
  2502. while(heat_time!=0)
  2503. {
  2504. heat_time--;
  2505. LOG_I("正在加热状态!!!");
  2506. rt_thread_delay(1000);
  2507. }
  2508. }
  2509. //6.打开下仓门
  2510. if(device_status.rain_status==0)
  2511. {
  2512. uint16_t cnt_photo = 0;
  2513. while(device_status.photo_ws==1) //如果正在拍照,要等拍照结束
  2514. {
  2515. rt_thread_delay(1000);
  2516. cnt_photo++;
  2517. if(cnt_photo>=60) break;
  2518. }
  2519. device_status.ctr_down_motor =1;
  2520. }
  2521. else {
  2522. device_status.ctr_down_motor = 2;
  2523. device_status.ctr_up_motor = 2;
  2524. }
  2525. //待下仓门完全打开
  2526. while(device_status.ctr_down_motor!=0)
  2527. {
  2528. rt_thread_delay(100);
  2529. rt_kprintf("等待下仓门打开!!!\n");
  2530. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  2531. }
  2532. rt_thread_delay(3000);
  2533. //关闭下仓门
  2534. device_status.ctr_down_motor = 2;
  2535. while(device_status.ctr_down_motor!=0)
  2536. {
  2537. rt_thread_delay(100);
  2538. rt_kprintf("等待下仓门关闭!!!\n");
  2539. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  2540. }
  2541. //2022-04-12add:
  2542. LOG_I("打开上仓门!!!");
  2543. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  2544. if(device_status.rain_status==0)
  2545. {
  2546. device_status.ctr_up_motor = 1;
  2547. }
  2548. if(device_status.version_type==4||
  2549. device_status.version_type==41||
  2550. device_status.version_type==42||
  2551. device_status.version_type==43||
  2552. device_status.version_type==12)
  2553. {
  2554. //震动
  2555. ctr_shake(ON, device_status.shake_sec);
  2556. device_status.photo_ws = 1;
  2557. }
  2558. #if DZ_260519 == 1
  2559. if(workloop==3&&device_status.clt_time==10&&device_status.heattime==10){
  2560. workloop = 0;
  2561. device_status.clear_status = 2;
  2562. }
  2563. #endif
  2564. }
  2565. }
  2566. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2567. {
  2568. rt_kprintf("-------------------not work--------------------\n");
  2569. device_status.work_status = 0;
  2570. }
  2571. //工作结束
  2572. if(device_status.work_done==0&&device_status.work_status==0)
  2573. {
  2574. DTU_Write("workdone");
  2575. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  2576. ResetInit();
  2577. clt_time = 0;
  2578. upload_sys_monitor_info();
  2579. }
  2580. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2581. }
  2582. /* 入口函数-工作状态检测 */
  2583. static void Work_status_thread_entry(void *parameter)
  2584. {
  2585. static uint16_t sun_status_num = 120;
  2586. //如果是光控定时,进行光控监测的消抖
  2587. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2588. {
  2589. rt_kprintf("光控定时!!!\n");
  2590. while(sun_status_num!=0)
  2591. {
  2592. sun_status_num--;
  2593. rt_thread_delay(1000);
  2594. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2595. }
  2596. device_status.light_over_tim = device_status.sunBar * 3600;
  2597. device_status.test_flag = 0;
  2598. }
  2599. while(1)
  2600. {
  2601. WorkCheck();
  2602. }
  2603. }
  2604. /*入口函数-工作流程*/
  2605. static void Work_Loop_thread_entry(void *parameter)
  2606. {
  2607. rt_thread_delay(1000);
  2608. ResetInit();
  2609. while(1)
  2610. {
  2611. if(device_status.dtype==7)
  2612. {
  2613. WorkTask_BZY();
  2614. }
  2615. if(device_status.version_type!=6&&device_status.dtype==3)
  2616. {
  2617. WorkTask();
  2618. }
  2619. if(device_status.version_type==6)
  2620. {
  2621. WorkTask_XCT();
  2622. }
  2623. rt_thread_delay(1000);
  2624. }
  2625. }
  2626. /*入口函数清扫*/
  2627. static void Control_Clear_thread_entry(void *parameter)
  2628. {
  2629. static int selfcheck_cnt = 0;
  2630. float voltage_f = 0.0;
  2631. while(1)
  2632. {
  2633. #if ROBOT_EN ==1
  2634. ctr_Motor_Robot(ON);
  2635. rt_thread_delay(100);
  2636. #else
  2637. #if Color_EN ==1
  2638. /*复位*/
  2639. if(Read_Color_Key(KEY_RESET)==1)
  2640. {
  2641. ctr_Color_board(RESET, 1200);
  2642. rt_thread_delay(5000);
  2643. }
  2644. #else
  2645. if(device_status.selfcheck_ws==1){
  2646. selfcheck_cnt ++;
  2647. if(selfcheck_cnt==1){
  2648. ctr_lamp(ON,1);
  2649. rt_thread_delay(1000);
  2650. device_status.ctr_up_motor = 1;
  2651. device_status.ctr_down_motor = 1;
  2652. //记录当前电压
  2653. voltage_f = atof(device_status.voltage);
  2654. rt_thread_delay(1500);
  2655. //开启加热
  2656. ctr_Heat(ON);
  2657. }
  2658. if(selfcheck_cnt==50){
  2659. double voltage_n = atof(device_status.voltage);
  2660. //判断当前电压和加热前的电压差
  2661. if(voltage_f-voltage_n<0.5){
  2662. device_status.heat_sta = 1; //加热异常
  2663. //上报预警,加热故障
  2664. char *heat_warn = makeJson_Warn("加热报警", "heat-error");
  2665. DTU_Write("%s", heat_warn);
  2666. rt_free(heat_warn);
  2667. }else{
  2668. device_status.heat_sta = 0;
  2669. }
  2670. rt_thread_delay(1000);
  2671. //关闭加热
  2672. ctr_Heat(OFF);
  2673. //振动
  2674. ctr_shake(ON, 1);
  2675. //device_status.clear_status = 1;
  2676. ctr_lamp(OFF, 1);
  2677. }
  2678. if(selfcheck_cnt>1800){ //超过3分钟
  2679. device_status.selfcheck_ws = 0;
  2680. device_status.ctr_up_motor = 0;
  2681. device_status.ctr_down_motor = 0;
  2682. //
  2683. device_status.camsta = 1; //相机异常
  2684. device_status.led_sta = 1;
  2685. //上报自检结果
  2686. //上报自检结果
  2687. char *json_temp = makeJson_selfCheck_(1);
  2688. DTU_Write("%s",json_temp);
  2689. free(json_temp);
  2690. }
  2691. }
  2692. if(device_status.selfcheck_ws==0){
  2693. selfcheck_cnt = 0;
  2694. }
  2695. if(device_status.clear_status==1)
  2696. {
  2697. ///if(ctr_clear(1200)==0)
  2698. ctr_TG_Clear(500); //2025-1021,由400修改为500,解决金朵推杆慢导致行程走不完
  2699. system_status.cleaning_count +=1; //清扫次数
  2700. }
  2701. if(device_status.clear_status==2){
  2702. turnzp();
  2703. device_status.clear_status=0;
  2704. }
  2705. rt_thread_delay(100);
  2706. #endif
  2707. #endif
  2708. }
  2709. }
  2710. /*入口函数加热*/
  2711. static void Control_Heat_thread_entry(void *parameter)
  2712. {
  2713. while(1)
  2714. {
  2715. if(device_status.work_status==1&&device_status.heat_sw==1)
  2716. {
  2717. calorstat(device_status.heattime*60);
  2718. rt_thread_delay(10000);
  2719. }
  2720. else
  2721. {
  2722. LOG_I("Not calorstat!%d",device_status.selfcheck_ws);
  2723. if(device_status.selfcheck_ws==2)
  2724. {
  2725. static int total_seconds = 200;
  2726. static int current_second = 0;
  2727. while(current_second <= total_seconds)
  2728. {
  2729. int process = (current_second * 100) / total_seconds;
  2730. device_status.process = process / 100.0;
  2731. LOG_I("process:%d",process);
  2732. HMI_WRITE("selfcheck.s106.val=%d",process);
  2733. rt_thread_delay(1000);
  2734. current_second++;
  2735. if(device_status.selfcheck_ws==0)
  2736. {
  2737. HMI_WRITE("selfcheck.s106.val=100");
  2738. break;
  2739. }
  2740. }
  2741. HMI_WRITE("selfcheck.s106.val=100");
  2742. }
  2743. }
  2744. rt_thread_delay(1000);
  2745. }
  2746. }
  2747. /*清扫线程*/
  2748. int Control_clear(void)
  2749. {
  2750. /* 创建线程*/
  2751. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2752. Control_Clear_thread_entry, RT_NULL,
  2753. 2048, //2048
  2754. 24, //23
  2755. WORK_CHECK_THREAD_TIMESLICE); //5
  2756. if(tid_ctr!=RT_NULL)
  2757. {
  2758. rt_thread_startup(tid_ctr);
  2759. }
  2760. return 0;
  2761. }
  2762. /*恒温加热线程*/
  2763. int Control_heat(void)
  2764. {
  2765. /* 创建线程*/
  2766. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  2767. Control_Heat_thread_entry, RT_NULL,
  2768. 1024, //2048
  2769. 25, //23
  2770. WORK_CHECK_THREAD_TIMESLICE); //5
  2771. if(tid_heat!=RT_NULL)
  2772. {
  2773. rt_thread_startup(tid_heat);
  2774. }
  2775. return 0;
  2776. }
  2777. /* 创建线程 -工作状态检测*/
  2778. int Workstatus_check(void)
  2779. {
  2780. /* 创建线程*/
  2781. tid_wkstc = rt_thread_create("work_status_check_thread",
  2782. Work_status_thread_entry, RT_NULL,
  2783. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2784. WORK_CHECK_THREAD_PRIORITY, //23
  2785. WORK_CHECK_THREAD_TIMESLICE); //5
  2786. if(tid_wkstc!=RT_NULL)
  2787. {
  2788. rt_thread_startup(tid_wkstc);
  2789. }
  2790. return 0;
  2791. }
  2792. /*工作流程线程*/
  2793. int Work_Loop_Task(void)
  2794. {
  2795. /* 创建线程*/
  2796. tid_workloop = rt_thread_create("work_loop_thread",
  2797. Work_Loop_thread_entry, RT_NULL,
  2798. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2799. WORK_LOOP_THREAD_PRIORITY, //12
  2800. WORK_LOOP_THREAD_TIMESLICE); //5
  2801. #if Color_EN == 0
  2802. if(tid_workloop!=RT_NULL)
  2803. {
  2804. rt_thread_startup(tid_workloop);
  2805. }
  2806. #endif
  2807. return 0;
  2808. }
  2809. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2810. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2811. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2812. // RTU/DTU 电源控制 shell 命令
  2813. static void ctr_rtu_dtu(int argc, char** argv)
  2814. {
  2815. if(argc != 2)
  2816. {
  2817. rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
  2818. rt_kprintf(" ON : RTU/DTU Power ON\n");
  2819. rt_kprintf(" OFF : RTU/DTU Power OFF\n");
  2820. rt_kprintf(" 0 : ON-OFF Pulse (10ms interval)\n");
  2821. return;
  2822. }
  2823. if(strcmp(argv[1], "ON") == 0 || strcmp(argv[1], "on") == 0)
  2824. {
  2825. ctr_RTU_DTU(ON);
  2826. rt_kprintf("RTU/DTU Power ON\n");
  2827. }
  2828. else if(strcmp(argv[1], "OFF") == 0 || strcmp(argv[1], "off") == 0)
  2829. {
  2830. ctr_RTU_DTU(OFF);
  2831. rt_kprintf("RTU/DTU Power OFF\n");
  2832. }
  2833. else if(strcmp(argv[1], "0") == 0)
  2834. {
  2835. rt_kprintf("RTU/DTU ON-OFF Pulse (10ms interval)\n");
  2836. ctr_RTU_DTU(OFF);
  2837. rt_thread_mdelay(1);
  2838. ctr_RTU_DTU(ON);
  2839. rt_kprintf("Pulse complete\n");
  2840. }
  2841. else
  2842. {
  2843. rt_kprintf("Invalid parameter: %s\n", argv[1]);
  2844. rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
  2845. }
  2846. }
  2847. MSH_CMD_EXPORT(ctr_rtu_dtu,ctr_rtu_dtu<ON|OFF>);
  2848. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2849. MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);