control.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708
  1. #include <control.h>
  2. /*
  3. * Copyright (c) 2006-2020, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-05-30 Administrator the first version
  10. */
  11. #include "beep.h"
  12. #include "hmi.h"
  13. #include "easyflash.h"
  14. #include "stdio.h"
  15. #include "sound.h"
  16. #include "dtu_uart2.h"
  17. #include "test.h"
  18. #include "sht30_collect.h"
  19. #define DBG_ENABLE
  20. #define DBG_COLOR
  21. #define DBG_TAG "control"
  22. #define DBG_LEVEL DBG_LOG
  23. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  24. // 工作检测
  25. #define WORK_CHECK_THREAD_PRIORITY 23 // 线程优先级
  26. #define WORK_CHECK_THREAD_STACK_SIZE 1024 // 栈大小
  27. #define WORK_CHECK_THREAD_TIMESLICE 5 // 时间片
  28. // 工作流程
  29. #define WORK_LOOP_THREAD_PRIORITY 12 // 线程优先级
  30. #define WORK_LOOP_THREAD_STACK_SIZE 2048 // 栈大小
  31. #define WORK_LOOP_THREAD_TIMESLICE 5 // 时间片
  32. #define PWM_EN 1 // 0关闭,1-打开
  33. #define LOG_INR 10 // 默认的log打印间隔(作用域在本文件)
  34. extern Res_def JD_Check_Test_Sta(JD_Test_Type type);
  35. extern void Report_Limit_Error(le_def type);
  36. extern RTC_DateTypeDef GetDate;
  37. extern RTC_TimeTypeDef GetTime;
  38. // 设备状态表结构体
  39. Device_Status_typedef device_status;
  40. rt_thread_t tid_workloop = RT_NULL;
  41. rt_thread_t tid_wkstc = RT_NULL;
  42. rt_thread_t tid_ctr = RT_NULL;
  43. rt_thread_t tid_heat = RT_NULL;
  44. volatile works_def ws_now = ws_standby;
  45. /**************************1.0定制2000W*************************************/
  46. // rt_pin_mode
  47. /**
  48. * @brief 控制风扇输出
  49. *
  50. * @param status_ws 开关量
  51. * @return uint8_t -
  52. */
  53. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  54. {
  55. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  56. if (status_ws == ON)
  57. {
  58. rt_pin_write(FAN_P, ON);
  59. }
  60. else
  61. {
  62. rt_pin_write(FAN_P, OFF);
  63. }
  64. return 0;
  65. }
  66. /**
  67. * @brief 控制风扇输出
  68. *
  69. * @param status_ws 开关量
  70. * @return uint8_t -
  71. */
  72. uint8_t ctr_Fan_IN(uint8_t status_ws)
  73. {
  74. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  75. if (status_ws == ON)
  76. {
  77. rt_pin_write(FAN_I, ON);
  78. }
  79. else
  80. {
  81. rt_pin_write(FAN_I, OFF);
  82. }
  83. return 0;
  84. }
  85. /****************************定制色诱*************************************/
  86. /**/
  87. #if Color_EN == 1
  88. /**
  89. * @brief 检测色诱按键是否有下降沿出现
  90. *
  91. * @param key_pin 引脚号
  92. * @return uint8_t 无意义
  93. */
  94. uint8_t Read_Color_Key(rt_base_t key_pin)
  95. {
  96. rt_uint32_t sta_s = 0;
  97. rt_pin_mode(key_pin, PIN_MODE_INPUT);
  98. while (rt_pin_read(key_pin) == 1)
  99. {
  100. sta_s++;
  101. /*持续的*/
  102. rt_thread_delay(50);
  103. }
  104. while (rt_pin_read(key_pin) == 0)
  105. {
  106. rt_thread_delay(50);
  107. if (sta_s > 0)
  108. break;
  109. }
  110. LOG_W("检测到IO0翻转!!!");
  111. return 1;
  112. }
  113. /*读限位开关状态*/
  114. /**
  115. * @brief 读取限位开关的状态
  116. *
  117. * @param pin_limit 引脚号
  118. * @return uint8_t 读取结果
  119. */
  120. uint8_t Read_Color_Limit(rt_base_t pin_limit)
  121. {
  122. uint8_t l_sta = 0;
  123. rt_pin_mode(pin_limit, PIN_MODE_INPUT);
  124. l_sta = rt_pin_read(pin_limit);
  125. rt_thread_delay(20);
  126. l_sta = rt_pin_read(pin_limit);
  127. return l_sta;
  128. }
  129. /*色诱板*/
  130. /**
  131. * @brief 色诱板的控制逻辑
  132. *
  133. * @param forward 下一步进行的动作
  134. * @param time_board 计数?
  135. * @return int 操作完成的动作
  136. */
  137. int ctr_Color_board(uint8_t forward, uint16_t time_board)
  138. {
  139. if (forward == CLOSE) // 归位
  140. {
  141. if (device_status.cul_time > 0)
  142. device_status.cul_time--;
  143. ctr_cangmen_down(OFF);
  144. ctr_cangmen_up(OFF);
  145. rt_thread_delay(1000);
  146. // if(device_status.cul_time>7) device_status.cul_time=7;
  147. // if(device_status.cul_time!=7)
  148. // {
  149. // uint8_t ct = device_status.cul_time;
  150. // while(1)
  151. // {
  152. // Beep_Tip(100);
  153. // rt_thread_delay(100);
  154. // ct--;
  155. // if(ct==0) break;
  156. // }
  157. // //return device_status.cul_time;
  158. // }
  159. if (Read_Color_Limit(LIMIT_RESET) == 0)
  160. {
  161. device_status.cul_time = 0;
  162. LOG_W("已在初始位置!");
  163. Beep_Tip(1000);
  164. return 0;
  165. // 黄板已经在复位状态,直接返回0;
  166. }
  167. ctr_cangmen_up(CLOSE);
  168. rt_thread_delay(20000);
  169. ctr_cangmen_up(OFF);
  170. // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
  171. // {
  172. // LOG_W("第七天!!!");
  173. // ctr_cangmen_down(CLOSE);
  174. // rt_thread_delay(500);
  175. // }
  176. LOG_W("先打开运行到限位的右侧!");
  177. while (Read_Color_Limit(LIMIT_BOARD) == 0)
  178. {
  179. LOG_W("归位:到达限位中间");
  180. ctr_cangmen_down(CLOSE);
  181. if (Read_Color_Limit(LIMIT_RESET) == 0)
  182. {
  183. ctr_cangmen_down(OFF);
  184. break;
  185. }
  186. }
  187. // 先打开运行到限位的右侧
  188. while (Read_Color_Limit(LIMIT_BOARD) == 1)
  189. {
  190. LOG_W("归位:运行!");
  191. ctr_cangmen_down(CLOSE);
  192. if (Read_Color_Limit(LIMIT_RESET) == 0)
  193. {
  194. ctr_cangmen_down(OFF);
  195. break;
  196. }
  197. }
  198. while (Read_Color_Limit(LIMIT_BOARD) == 0) // 到达限位中间 继续运行
  199. {
  200. LOG_W("归位:到达限位中间");
  201. ctr_cangmen_down(CLOSE);
  202. if (Read_Color_Limit(LIMIT_RESET) == 0)
  203. {
  204. ctr_cangmen_down(OFF);
  205. break;
  206. }
  207. }
  208. if (Read_Color_Limit(LIMIT_RESET) == 0)
  209. return 0;
  210. LOG_W("到达限位左侧!");
  211. // 停止电机
  212. ctr_cangmen_down(OFF);
  213. rt_thread_delay(2000);
  214. // 将推杆电机归位
  215. LOG_D("*推杆电机归位*");
  216. /*推杆电机伸出*/
  217. ctr_cangmen_up(OPEN);
  218. rt_thread_delay(20000);
  219. ctr_cangmen_up(OFF);
  220. return 2;
  221. }
  222. else if (forward == OPEN)
  223. {
  224. ctr_cangmen_down(OFF);
  225. ctr_cangmen_up(OFF);
  226. rt_thread_delay(1000);
  227. if (device_status.cul_time == 7)
  228. {
  229. Beep_Tip(5000);
  230. return 7;
  231. }
  232. LOG_W("打开推杆电机!");
  233. // 2023-04-18:add
  234. // if(Read_Color_Limit(LIMIT_BOARD)==0)
  235. {
  236. LOG_D("*推杆电机归位*");
  237. /*推杆电机归位*/
  238. ctr_cangmen_up(CLOSE);
  239. rt_thread_delay(20000);
  240. ctr_cangmen_up(OFF);
  241. }
  242. if (device_status.cul_time < 7)
  243. device_status.cul_time++;
  244. while (Read_Color_Limit(LIMIT_BOARD) == 0)
  245. {
  246. ctr_cangmen_down(forward);
  247. rt_thread_delay(50);
  248. }
  249. while (Read_Color_Limit(LIMIT_BOARD) == 1)
  250. {
  251. time_board--;
  252. if (time_board == 0)
  253. break;
  254. ctr_cangmen_down(forward);
  255. rt_thread_delay(50);
  256. }
  257. ctr_cangmen_down(OFF);
  258. rt_thread_delay(1000);
  259. ctr_cangmen_up(OPEN);
  260. rt_thread_delay(20000);
  261. ctr_cangmen_up(OFF);
  262. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  263. return OPEN;
  264. }
  265. else if (forward == RESET)
  266. {
  267. ctr_cangmen_up(OFF);
  268. ctr_cangmen_down(OFF);
  269. if (Read_Color_Limit(LIMIT_RESET) == 0)
  270. {
  271. device_status.cul_time = 0;
  272. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  273. LOG_W("已在初始位置!");
  274. Beep_Tip(1000);
  275. ctr_cangmen_up(CLOSE);
  276. rt_thread_delay(20000);
  277. ctr_cangmen_up(OFF);
  278. device_status.cul_time = 0;
  279. return 0;
  280. // 黄板已经在复位状态,直接返回0;
  281. }
  282. // if(Read_Color_Limit(LIMIT_BOARD)==0)
  283. {
  284. LOG_D("*推杆电机归位*");
  285. /*推杆电机归位*/
  286. ctr_cangmen_up(CLOSE);
  287. rt_thread_delay(20000);
  288. ctr_cangmen_up(OFF);
  289. }
  290. while (Read_Color_Limit(LIMIT_RESET) == 1)
  291. {
  292. time_board--;
  293. if (time_board == 0)
  294. break;
  295. ctr_cangmen_down(CLOSE);
  296. rt_thread_delay(20);
  297. }
  298. device_status.cul_time = 0;
  299. ctr_cangmen_down(OFF);
  300. }
  301. // device_status.cul_time = 0;
  302. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  303. return RESET; // 复位返回0
  304. }
  305. #endif
  306. /****************************5.0***********************************/
  307. void ctr_turnv5(void)
  308. {
  309. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  310. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  311. // 转出
  312. rt_pin_write(CLEAR_R, ON);
  313. rt_pin_write(CLEAR_L, OFF);
  314. rt_thread_mdelay(300);
  315. // 停止
  316. rt_pin_write(CLEAR_R, OFF);
  317. rt_pin_write(CLEAR_L, OFF);
  318. rt_thread_mdelay(300);
  319. // 转入
  320. rt_pin_write(CLEAR_R, OFF);
  321. rt_pin_write(CLEAR_L, ON);
  322. rt_thread_mdelay(300);
  323. // 停止
  324. rt_pin_write(CLEAR_R, OFF);
  325. rt_pin_write(CLEAR_L, OFF);
  326. rt_thread_mdelay(300);
  327. }
  328. /**************************机器人********************************/
  329. void ctr_Motor_Robot(uint8_t status_ws)
  330. {
  331. }
  332. /*************************吸虫塔*********************************/
  333. //// 吸虫电机
  334. // uint8_t ctr_Motor_xichong(uint8_t status_ws)
  335. //{
  336. // rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  337. // if (status_ws == ON)
  338. // {
  339. // rt_pin_write(MOTOR1_CTR_PIN, ON);
  340. // }
  341. // else
  342. // {
  343. // rt_pin_write(MOTOR1_CTR_PIN, OFF);
  344. // }
  345. // return 0;
  346. // }
  347. //
  348. // uint8_t ctr_Camera_Power(uint8_t status_ws)
  349. //{
  350. // rt_pin_mode(CAM_CTL, PIN_MODE_OUTPUT);
  351. // if (status_ws == ON)
  352. // {
  353. // rt_pin_write(CAM_CTL, ON);
  354. // }
  355. // else
  356. // {
  357. // rt_pin_write(CAM_CTL, OFF);
  358. // }
  359. // return 0;
  360. // }
  361. // 吸附电机
  362. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  363. {
  364. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  365. if (status_ws == ON)
  366. {
  367. rt_pin_write(MOTOR2_CTR_PIN, ON);
  368. }
  369. else
  370. {
  371. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  372. }
  373. return 0;
  374. }
  375. // 清扫-卷带电机
  376. void ctr_JD_Clear(uint8_t shake_sw)
  377. {
  378. rt_uint16_t turn_timeout = 0;
  379. rt_pin_mode(MOTOR_C_LIMIT, PIN_MODE_INPUT);
  380. ctr_cangmen_up(OFF); // 清扫传送带的电机
  381. if (shake_sw == ON)
  382. {
  383. ctr_Heat(ON); // 清洁风机
  384. Motor_shake(ON, 0);
  385. ctr_cangmen_up(OPEN); // 打开清扫
  386. }
  387. else
  388. {
  389. Motor_shake(OFF, 0);
  390. ctr_cangmen_up(OPEN); // 打开清扫
  391. }
  392. while (rt_pin_read(MOTOR_C_LIMIT) == 0)
  393. {
  394. Motor_Juandai(ON);
  395. rt_thread_delay(50);
  396. turn_timeout++;
  397. if (turn_timeout == 1000)
  398. break;
  399. }
  400. turn_timeout = 0;
  401. while (rt_pin_read(MOTOR_C_LIMIT) == 1)
  402. {
  403. Motor_Juandai(ON);
  404. turn_timeout++;
  405. rt_thread_delay(50);
  406. if (turn_timeout == 1000)
  407. break;
  408. }
  409. Motor_shake(OFF, 1);
  410. Motor_Juandai(OFF);
  411. ctr_cangmen_up(OFF); // 关闭清扫
  412. ctr_Heat(OFF);
  413. }
  414. // 工作任务流程
  415. void WorkTask_XCTm(void)
  416. {
  417. while (device_status.work_status == 1 && device_status.ds == 1)
  418. {
  419. ctr_Motor_xifu(ON);
  420. // 打开相机供电
  421. ctr_Camera_Power(ON);
  422. // ctr_Heat(ON); //清洁风机
  423. uint32_t photo_fre = 0;
  424. // photo_fre = device_status.dattim * 60;
  425. while (photo_fre != 0)
  426. {
  427. photo_fre--;
  428. rt_thread_delay(1000);
  429. LOG_I("收集中,等待开始拍照:%d", photo_fre);
  430. }
  431. // 卷带
  432. ctr_JD_Clear(OFF);
  433. // 拍照
  434. HK_TakePhoto();
  435. // 关闭吸附风机
  436. ctr_Motor_xifu(OFF);
  437. // 关闭相机供电
  438. // ctr_Heat(OFF);
  439. ctr_Camera_Power(OFF);
  440. // 卷带
  441. ctr_JD_Clear(ON);
  442. // rt_thread_delay(1000);
  443. // ctr_JD_Clear(ON);
  444. // 震动
  445. Motor_shake(ON, device_status.shake_sec);
  446. }
  447. // 关闭风机
  448. ctr_Motor_xichong(OFF);
  449. ctr_Motor_xifu(OFF);
  450. LOG_I("不在工作时间,未工作!!!");
  451. }
  452. /**********************************************************************/
  453. /*光控*/
  454. uint8_t In_Light_status(void)
  455. {
  456. rt_pin_mode(LIGHT_PIN, PIN_MODE_INPUT);
  457. return rt_pin_read(LIGHT_PIN);
  458. }
  459. /*雨控*/
  460. uint8_t In_Rain_status(void)
  461. {
  462. rt_pin_mode(RAIN_PIN, PIN_MODE_INPUT);
  463. return rt_pin_read(RAIN_PIN);
  464. }
  465. /*加热*/
  466. uint8_t ctr_Heat(uint8_t status_heat)
  467. {
  468. device_status.heat_status = status_heat;
  469. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  470. rt_pin_write(Heat_Pin, status_heat);
  471. return status_heat;
  472. }
  473. /*制冷*/
  474. uint8_t ctr_Cold(uint8_t status_cold)
  475. {
  476. device_status.cold_status = status_cold;
  477. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  478. rt_pin_write(Cold_Pin, status_cold);
  479. return status_cold;
  480. }
  481. /*相机供电*/
  482. void ctr_photo(uint8_t status_photo)
  483. {
  484. // rt_pin_mode(PHOTO, PIN_MODE_OUTPUT);
  485. // rt_pin_write(PHOTO, status_photo);
  486. }
  487. /*************************************************
  488. Function: ctr_Cool_Fan
  489. Description: 散热风机的开关
  490. Calls:
  491. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  492. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  493. Input: ON-开,OFF-关
  494. Output: NULL
  495. Return: ON-开,OFF-关
  496. Others: NULL
  497. *************************************************/
  498. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  499. {
  500. device_status.cool_fan_status = status_cool;
  501. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  502. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  503. if (status_cool == OFF)
  504. {
  505. rt_pin_write(Cool_Fan_L, OFF);
  506. rt_pin_write(Cool_Fan_R, OFF);
  507. }
  508. if (status_cool == ON)
  509. {
  510. rt_pin_write(Cool_Fan_L, ON);
  511. rt_pin_write(Cool_Fan_R, OFF);
  512. }
  513. return device_status.cool_fan_status;
  514. }
  515. #include "Work_TN.h"
  516. extern void CollFanSetPWM(uint8_t percent);
  517. extern SYSCFG syscfg;
  518. /*************************************************
  519. Function: ctr_Collect_Fan
  520. Description: 采集风机的开关
  521. Calls:
  522. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  523. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  524. Input: ON-开,OFF-关
  525. Output: NULL
  526. Return: ON-开,OFF-关
  527. Others: NULL
  528. *************************************************/
  529. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  530. {
  531. device_status.collect_fan_status = status_collect;
  532. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  533. if (status_collect == OFF)
  534. {
  535. CollFanSetPWM(0);
  536. rt_pin_write(Collect_Fan, OFF);
  537. }
  538. if (status_collect == ON)
  539. {
  540. CollFanSetPWM(syscfg.fan_speed);
  541. rt_pin_write(Collect_Fan, ON);
  542. }
  543. return device_status.collect_fan_status;
  544. }
  545. /*************************************************
  546. Function: ctr_develop
  547. Description: 培养载玻片孢子
  548. Calls:
  549. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  550. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  551. Input: v=0(复位) v=90转90度
  552. Output: NULL
  553. Return: NULL
  554. Others: NULL
  555. *************************************************/
  556. void ctr_develop(void)
  557. {
  558. rt_kprintf("===============开始培养=================:%d\n", device_status.cul_time);
  559. // 已经培养的时间-分钟
  560. device_status.stay_time = 0;
  561. rt_uint32_t ii = 0;
  562. while (device_status.develop_flag == 1)
  563. {
  564. float stayedtime = 0.01;
  565. char stayed[5];
  566. stayedtime = (float)device_status.stay_time / 60.0;
  567. sprintf(stayed, "%0.02f", stayedtime);
  568. HMI_WRITE("workSta.staytime.txt=\"%s\"", stayed);
  569. ii++;
  570. LOG_I("developing...................................%d\n", ii);
  571. LOG_I("============================设定的培养温度:%d\n", device_status.heatvalue);
  572. LOG_I("============================培养仓实时温度:%d\n", device_status.pre_temp);
  573. if (device_status.pre_temp < (device_status.heatvalue - 5))
  574. {
  575. // 未制冷
  576. if (device_status.cold_status == 0)
  577. {
  578. ctr_Heat(ON);
  579. }
  580. }
  581. else
  582. {
  583. ctr_Heat(OFF);
  584. }
  585. LOG_I("============================制冷开关:%d\n", device_status.cold_sw);
  586. if (device_status.cold_sw == 1)
  587. {
  588. if (device_status.pre_temp > (device_status.heatvalue - 10))
  589. {
  590. if (device_status.heat_status == 0)
  591. {
  592. ctr_Cold(ON);
  593. }
  594. }
  595. else
  596. {
  597. ctr_Cold(OFF);
  598. }
  599. }
  600. if (ii % 60 == 0)
  601. {
  602. device_status.stay_time++;
  603. if (device_status.stay_time / 60 == device_status.cul_time)
  604. device_status.develop_flag = 0;
  605. LOG_I("developed...................................%d分钟\n", device_status.stay_time);
  606. }
  607. if (device_status.box_temp > 35)
  608. {
  609. ctr_Cool_Fan(ON);
  610. }
  611. else if (device_status.box_temp < 30)
  612. {
  613. ctr_Cool_Fan(OFF);
  614. }
  615. rt_thread_delay(1000);
  616. }
  617. ctr_Cold(OFF);
  618. ctr_Heat(OFF);
  619. LOG_I("=========!!!培养完成!!!==========");
  620. }
  621. /*上仓门*/
  622. /**
  623. * @brief 控制仓门升起
  624. *
  625. * @param status_down 指令
  626. * @return uint8_t 执行后的状态
  627. */
  628. uint8_t ctr_cangmen_up(uint8_t status_up)
  629. {
  630. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  631. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  632. if (status_up == OFF)
  633. {
  634. rt_pin_write(Collect_fan_R, ON);
  635. rt_pin_write(Collect_fan_L, ON);
  636. device_status.ctr_up_motor = 0;
  637. rt_thread_delay(1000);
  638. }
  639. if (status_up == OPEN)
  640. {
  641. device_status.up_status = 1;
  642. rt_pin_write(Collect_fan_R, OFF);
  643. rt_pin_write(Collect_fan_L, OFF);
  644. rt_pin_write(Collect_fan_R, ON);
  645. rt_pin_write(Collect_fan_L, OFF);
  646. }
  647. if (status_up == CLOSE)
  648. {
  649. device_status.up_status = 0;
  650. rt_pin_write(Collect_fan_R, OFF);
  651. rt_pin_write(Collect_fan_L, OFF);
  652. rt_pin_write(Collect_fan_R, OFF);
  653. rt_pin_write(Collect_fan_L, ON);
  654. }
  655. return device_status.up_status;
  656. }
  657. // 下仓门
  658. /**
  659. * @brief 控制仓门落下
  660. *
  661. * @param status_down 指令
  662. * @return uint8_t 执行后的状态
  663. */
  664. uint8_t ctr_cangmen_down(uint8_t status_down)
  665. {
  666. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  667. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  668. if (status_down == OFF)
  669. {
  670. rt_pin_write(C_DOWN_R, ON);
  671. rt_pin_write(C_DOWN_L, ON);
  672. device_status.ctr_down_motor = 0;
  673. rt_thread_delay(1000);
  674. }
  675. if (status_down == OPEN)
  676. {
  677. device_status.down_status = 1;
  678. rt_pin_write(C_DOWN_R, OFF);
  679. rt_pin_write(C_DOWN_L, OFF);
  680. rt_pin_write(C_DOWN_R, ON);
  681. rt_pin_write(C_DOWN_L, OFF);
  682. }
  683. if (status_down == CLOSE)
  684. {
  685. device_status.down_status = 0;
  686. rt_pin_write(C_DOWN_R, OFF);
  687. rt_pin_write(C_DOWN_L, OFF);
  688. rt_pin_write(C_DOWN_R, OFF);
  689. rt_pin_write(C_DOWN_L, ON);
  690. }
  691. return device_status.down_status;
  692. }
  693. // 镇流器
  694. /**
  695. * @brief 控制镇流器动作
  696. *
  697. * @param status_lamp 指令
  698. * @param lamp_num 镇流器编号
  699. * @return uint8_t 更新后的镇流器状态
  700. */
  701. uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
  702. {
  703. rt_pin_mode(LAMP_0_SW, PIN_MODE_OUTPUT);
  704. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  705. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  706. if (status_lamp == ON)
  707. {
  708. switch (lamp_num)
  709. {
  710. case 1:
  711. rt_pin_write(LAMP_0_SW, ON);
  712. device_status.lamp_status = status_lamp;
  713. break;
  714. case 2:
  715. rt_pin_write(LAMP_1_SW, ON);
  716. device_status.lamp_status = status_lamp;
  717. break;
  718. case 3:
  719. rt_pin_write(LAMP_2_SW, ON);
  720. device_status.lamp_status = status_lamp;
  721. break;
  722. default:
  723. break;
  724. }
  725. }
  726. else
  727. {
  728. switch (lamp_num)
  729. {
  730. case 1:
  731. rt_pin_write(LAMP_0_SW, OFF);
  732. device_status.lamp_status = status_lamp;
  733. break;
  734. case 2:
  735. rt_pin_write(LAMP_1_SW, OFF);
  736. device_status.lamp1_status = status_lamp;
  737. break;
  738. case 3:
  739. rt_pin_write(LAMP_2_SW, OFF);
  740. device_status.lamp2_status = status_lamp;
  741. break;
  742. default:
  743. break;
  744. }
  745. }
  746. HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
  747. return device_status.lamp_status;
  748. }
  749. // 震动电机
  750. /**
  751. * @brief 控制震动电机动作
  752. *
  753. * @param status_shake 指令
  754. * @param shake_sec 震动时间(秒)
  755. * @return uint8_t 执行后的状态
  756. */
  757. uint8_t ctr_shake(uint8_t status_shake, uint16_t shake_sec)
  758. {
  759. static rt_uint16_t shake_i = 0;
  760. if (device_status.dtype == dt_BZY && device_status.version_type == vt_XCT)
  761. {
  762. return 0;
  763. }
  764. shake_i = atoi(device_status.power);
  765. LOG_I("震动电机待机电流:%d", shake_i);
  766. if (device_status.dtype == 7 && shake_sec != 0)
  767. DTU_Write("drop");
  768. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  769. if (status_shake == ON && device_status.shake_ws == 1)
  770. {
  771. rt_pin_write(SHAKE_SW, ON);
  772. if (device_status.dtype == 3)
  773. {
  774. while (shake_sec--)
  775. {
  776. rt_thread_delay(100);
  777. if (shake_sec == 20 && device_status.clear_status == 0 && device_status.work_status == 1 && atoi(device_status.power) - shake_i < 100 && shake_i != 0 && shake_i < 5000)
  778. {
  779. char *shake_warn = makeJson_Warn("电流不足", "震动电机");
  780. DTU_Write("%s", shake_warn);
  781. rt_free(shake_warn);
  782. }
  783. LOG_D("震动电机:%d", atoi(device_status.power));
  784. }
  785. }
  786. if (device_status.dtype == 7)
  787. rt_thread_delay(shake_sec * 1000);
  788. rt_pin_write(SHAKE_SW, OFF);
  789. HMI_WRITE("cangWei.switchShake.val=%d", 0);
  790. }
  791. else
  792. {
  793. rt_pin_write(SHAKE_SW, OFF);
  794. HMI_WRITE("cangWei.switchShake.val=%d", 0);
  795. }
  796. if (device_status.dtype == 7)
  797. HMI_WRITE("sysTest.switchDrop.val=%d", OFF);
  798. return status_shake;
  799. }
  800. // 补光
  801. extern JD_Test_Sta jts;
  802. /**
  803. * @brief 控制补光灯动作
  804. *
  805. * @param status_led 指令
  806. * @return uint8_t 执行后的状态
  807. */
  808. uint8_t ctr_led(uint8_t status_led)
  809. {
  810. // rt_kprintf("ctr_led %d \n\n", status_led);
  811. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  812. if (device_status.version_type == 6)
  813. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  814. if (status_led == ON)
  815. {
  816. rt_pin_write(LED_SW, ON);
  817. if (device_status.version_type == 6)
  818. rt_pin_write(LED_P, ON);
  819. HMI_WRITE("sysTest.switchFill.val=%d", ON);
  820. }
  821. else if (status_led == OFF)
  822. {
  823. rt_pin_write(LED_SW, OFF);
  824. if (device_status.version_type == 6)
  825. rt_pin_write(LED_P, OFF);
  826. if (jts.switchled_test == ON)
  827. {
  828. jts.switchled_test = OFF;
  829. Recode_e_code(e_TestLed, 2, "外部工作导致补光灯关闭");
  830. }
  831. }
  832. return status_led;
  833. }
  834. /*************************************************
  835. Function: ctr_turn
  836. Description: 转盘驱动
  837. Calls:
  838. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  839. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  840. Input: v=0(复位) v=90转90度
  841. Output: NULL
  842. Return: NULL
  843. Others: NULL
  844. *************************************************/
  845. void ctr_turn(uint8_t v, uint8_t sta)
  846. {
  847. if (device_status.dtype == dt_BZY && device_status.version_type == vt_XCT)
  848. {
  849. return;
  850. }
  851. DTU_Write("turn.mp3");
  852. LOG_I("================v===============:%d", v);
  853. rt_uint16_t turn_cnt = 0;
  854. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  855. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  856. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  857. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  858. // if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  859. while (rt_pin_read(LIMIT_0) == 0 || rt_pin_read(LIMIT_90) == 0)
  860. {
  861. rt_pin_write(TURN_ZP_R, ON);
  862. rt_pin_write(TURN_ZP_L, OFF);
  863. // Motor_shake(ON,0);
  864. turn_cnt++;
  865. rt_thread_delay(100);
  866. if (turn_cnt == 1000)
  867. {
  868. turn_cnt = 0;
  869. rt_pin_write(TURN_ZP_R, OFF);
  870. rt_pin_write(TURN_ZP_L, OFF);
  871. // Motor_shake(OFF,0);
  872. char *turn_warn = makeJson_Warn("未检测到限位", "转盘");
  873. DTU_Write("%s", turn_warn);
  874. rt_free(turn_warn);
  875. break;
  876. }
  877. }
  878. turn_cnt = 0;
  879. while (v == 0 && rt_pin_read(LIMIT_0) == 1)
  880. {
  881. rt_pin_write(TURN_ZP_R, ON);
  882. rt_pin_write(TURN_ZP_L, OFF);
  883. Motor_shake(ON, 0);
  884. rt_thread_delay(200); // 10 to 100
  885. turn_cnt++;
  886. if (turn_cnt == 300) // 6000 to 600
  887. {
  888. turn_cnt = 0;
  889. // rt_pin_write(TURN_ZP_R, OFF);
  890. // rt_pin_write(TURN_ZP_L, OFF);
  891. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  892. // DTU_Write("%s",turn_warn);
  893. // rt_free(turn_warn);
  894. break;
  895. }
  896. }
  897. while (v == 0 && rt_pin_read(LIMIT_0) == 1)
  898. {
  899. rt_pin_write(TURN_ZP_R, ON);
  900. rt_pin_write(TURN_ZP_L, OFF);
  901. // Motor_shake(ON,0);
  902. rt_thread_delay(50); // 10 to 50
  903. turn_cnt++;
  904. if (turn_cnt == 10) // 6000 to 1200
  905. {
  906. turn_cnt = 0;
  907. rt_pin_write(TURN_ZP_R, OFF);
  908. rt_pin_write(TURN_ZP_L, OFF);
  909. // Motor_shake(OFF,0);
  910. char *turn_warn = makeJson_Warn("超时", "转盘复位");
  911. DTU_Write("%s", turn_warn);
  912. rt_free(turn_warn);
  913. break;
  914. }
  915. }
  916. turn_cnt = 0;
  917. while (v == 90 && rt_pin_read(LIMIT_90) == 1)
  918. {
  919. rt_pin_write(TURN_ZP_R, ON);
  920. rt_pin_write(TURN_ZP_L, OFF);
  921. // Motor_shake(ON,0);
  922. rt_thread_delay(200);
  923. turn_cnt++;
  924. if (turn_cnt == 150)
  925. {
  926. turn_cnt = 0;
  927. // rt_pin_write(TURN_ZP_R, OFF);
  928. // rt_pin_write(TURN_ZP_L, OFF);
  929. // char *turn_warn =makeJson_Warn("超时", "转盘");
  930. // DTU_Write(turn_warn);
  931. // rt_free(turn_warn);
  932. break;
  933. }
  934. }
  935. while (v == 90 && rt_pin_read(LIMIT_90) == 1)
  936. {
  937. // rt_pin_write(TURN_ZP_R, ON);
  938. // rt_pin_write(TURN_ZP_L, OFF);
  939. Motor_shake(ON, 0);
  940. rt_thread_delay(50);
  941. turn_cnt++;
  942. if (turn_cnt == 10)
  943. {
  944. turn_cnt = 0;
  945. rt_pin_write(TURN_ZP_R, OFF);
  946. rt_pin_write(TURN_ZP_L, OFF);
  947. // Motor_shake(OFF,0);
  948. char *turn_warn = makeJson_Warn("超时", "转盘");
  949. DTU_Write(turn_warn);
  950. rt_free(turn_warn);
  951. break;
  952. }
  953. }
  954. if (sta != 0)
  955. {
  956. // 保存位置
  957. device_status.run_sta = sta;
  958. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  959. }
  960. turn_cnt = 0;
  961. rt_pin_write(TURN_ZP_R, OFF);
  962. rt_pin_write(TURN_ZP_L, OFF);
  963. Motor_shake(OFF, 0);
  964. HMI_WRITE("cangWei.rotateFlag.val=0");
  965. }
  966. /*************************************************
  967. Function: ctr_Motor_Driver
  968. Description: 步进电机驱动
  969. Calls:
  970. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  971. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  972. Input: 50000000
  973. Output: NULL
  974. Return: shake_sec
  975. Others: NULL
  976. *************************************************/
  977. extern JD_Test_Sta jts;
  978. Res_def ctr_Motor_Driver(uint8_t EN, uint8_t DIR, rt_uint32_t pwm, rt_uint32_t time_out)
  979. {
  980. Res_def result = res_OK;
  981. beep_init();
  982. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  983. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  984. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  985. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  986. JD_Test_Type EN_Type = tt_typemax;
  987. if (jts.autotest == ON)
  988. {
  989. EN_Type = tt_auto;
  990. HMI_WRITE_Force("");
  991. if (DIR == 0)
  992. {
  993. HMI_WRITE("sysTest.result.txt=\"一键测试 > 步进上行中\"");
  994. }
  995. }
  996. if (EN == 0)
  997. {
  998. rt_pin_write(Motor_Power, 0);
  999. }
  1000. else
  1001. {
  1002. rt_pin_write(Motor_EN, EN);
  1003. }
  1004. rt_pin_write(Motor_DIR, DIR);
  1005. beep_set(pwm, pwm / 2); // 200kzh - 50%
  1006. while (DIR == 1 && rt_pin_read(LIMIT_DOWN) == 1)
  1007. {
  1008. if (time_out == 0)
  1009. {
  1010. result = res_Timeout;
  1011. switch (EN_Type)
  1012. {
  1013. case tt_auto:
  1014. Recode_e_code(e_StepDown_Fail, 1, "一键测试:步进下行超时"); // 一键测试
  1015. break;
  1016. default:
  1017. if (pwm != 0)
  1018. {
  1019. Recode_e_code(e_StepDown_Fail, 0, "工作中:步进下行超时"); // 流程中
  1020. }
  1021. break;
  1022. }
  1023. break;
  1024. }
  1025. rt_thread_delay(50);
  1026. time_out--;
  1027. }
  1028. while (DIR == 0 && rt_pin_read(LIMIT_UP) == 1)
  1029. {
  1030. if (time_out == 0)
  1031. {
  1032. result = res_Timeout;
  1033. switch (EN_Type)
  1034. {
  1035. case tt_auto:
  1036. Recode_e_code(e_StepUp_Fail, 1, "一键测试:步进上行超时"); // 一键测试
  1037. break;
  1038. default:
  1039. if (pwm != 0)
  1040. {
  1041. Recode_e_code(e_StepUp_Fail, 0, "工作中:步进上行超时"); // 流程中
  1042. }
  1043. break;
  1044. }
  1045. break;
  1046. }
  1047. rt_thread_delay(50);
  1048. time_out--;
  1049. if (EN_Type == tt_auto && JD_Check_Test_Sta(tt_auto) != res_OK)
  1050. {
  1051. HMI_WRITE_Force("");
  1052. HMI_WRITE("sysTest.result.txt=\"一键测试 > 步进上行退出\"");
  1053. break;
  1054. }
  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. return result;
  1061. }
  1062. /*************************************************
  1063. Function: ctr_Petroleum_jelly
  1064. Description: 涂凡士林
  1065. Calls:
  1066. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1067. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1068. Input: NULL
  1069. Output: NULL
  1070. Return: NULL
  1071. Others: NULL
  1072. *************************************************/
  1073. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1074. {
  1075. beep_init();
  1076. pwm_on(2, 50000);
  1077. rt_thread_delay(delay * 1000);
  1078. pwm_off(2, 0);
  1079. rt_thread_delay(1000);
  1080. pwm_on(3, 50000);
  1081. rt_thread_delay(delay * 1000);
  1082. pwm_off(3, 0);
  1083. device_status.fsl_sw = 0;
  1084. }
  1085. // 1.0转仓
  1086. void turnzp(void)
  1087. {
  1088. static uint16_t turn_timeout = 0;
  1089. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1090. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1091. rt_pin_mode(CLEAR_R_LIMIT, PIN_MODE_INPUT);
  1092. // rt_pin_write(CLEAR_R,OFF);
  1093. // rt_pin_write(CLEAR_L,OFF);
  1094. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1095. {
  1096. rt_thread_delay(20);
  1097. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1098. {
  1099. // rt_pin_write(CLEAR_R,ON);
  1100. // rt_pin_write(CLEAR_L,OFF);
  1101. Motor_Juandai(ON);
  1102. rt_thread_delay(50);
  1103. turn_timeout++;
  1104. if (turn_timeout == 600)
  1105. {
  1106. LOG_E("检测不到限位,电机强制停止");
  1107. char *ctr_zp_limit_warn = makeJson_Warn("电流不足/限位超时", "转盘电机");
  1108. DTU_Write("%s", ctr_zp_limit_warn);
  1109. rt_free(ctr_zp_limit_warn);
  1110. break;
  1111. }
  1112. }
  1113. break;
  1114. }
  1115. while (rt_pin_read(CLEAR_R_LIMIT) == 1)
  1116. {
  1117. rt_thread_delay(20);
  1118. while (rt_pin_read(CLEAR_R_LIMIT) == 1)
  1119. {
  1120. // rt_pin_write(CLEAR_R,ON);
  1121. // rt_pin_write(CLEAR_L,OFF);
  1122. Motor_Juandai(ON);
  1123. rt_thread_delay(50);
  1124. turn_timeout++;
  1125. if (turn_timeout == 600)
  1126. {
  1127. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1128. char *ctr_zp_limit_warn = makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障", "转盘电机");
  1129. DTU_Write("%s", ctr_zp_limit_warn);
  1130. rt_free(ctr_zp_limit_warn);
  1131. break;
  1132. }
  1133. }
  1134. break;
  1135. }
  1136. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1137. {
  1138. rt_thread_delay(20);
  1139. if (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1140. {
  1141. // rt_pin_write(CLEAR_R,OFF);
  1142. // rt_pin_write(CLEAR_L,OFF);
  1143. Motor_Juandai(OFF);
  1144. break;
  1145. }
  1146. else
  1147. {
  1148. break;
  1149. }
  1150. }
  1151. Motor_Juandai(OFF);
  1152. // rt_pin_write(CLEAR_R,OFF);
  1153. // rt_pin_write(CLEAR_L,OFF);
  1154. turn_timeout = 0;
  1155. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1156. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1157. rt_thread_delay(1000);
  1158. }
  1159. // 清扫-常闭型限位开关
  1160. // 新结构使用推杆电机做清扫
  1161. uint8_t ctr_clear(uint16_t status_clear)
  1162. {
  1163. static uint16_t clear_num = 0;
  1164. // #if PWM_EN == 0
  1165. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1166. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1167. // #endif
  1168. // rt_pin_mode(CLEAR_R_LIMIT, PIN_MODE_INPUT);
  1169. // rt_pin_mode(CLEAR_L_LIMIT, PIN_MODE_INPUT);
  1170. /*************************************************************外接清扫
  1171. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1172. rt_pin_write(POWER_CLEAR,ON);
  1173. rt_thread_delay(1000*100);
  1174. rt_pin_write(POWER_CLEAR,OFF);
  1175. ************************************************************/
  1176. // 停止
  1177. #if PWM_EN == 0
  1178. rt_pin_write(CLEAR_R, OFF);
  1179. rt_pin_write(CLEAR_L, OFF);
  1180. #else
  1181. Motor_Qingsao_R_PWM(OFF);
  1182. Motor_Qingsao_L_PWM(OFF);
  1183. #endif
  1184. if (status_clear == OFF || Read_Real_Voltage() / 100 < 12)
  1185. return 0;
  1186. rt_thread_delay(100);
  1187. // 如果清扫位置在中间
  1188. while (rt_pin_read(CLEAR_R_LIMIT) == 0 && rt_pin_read(CLEAR_L_LIMIT) == 0)
  1189. {
  1190. #if PWM_EN == 0
  1191. rt_pin_write(CLEAR_R, ON);
  1192. rt_pin_write(CLEAR_L, OFF);
  1193. #else
  1194. Motor_Qingsao_R_PWM(ON);
  1195. Motor_Qingsao_L_PWM(OFF);
  1196. #endif
  1197. rt_thread_delay(50);
  1198. clear_num++;
  1199. // 已到达检测位置
  1200. if (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1)
  1201. {
  1202. #if PWM_EN == 0
  1203. rt_pin_write(CLEAR_R, ON);
  1204. rt_pin_write(CLEAR_L, ON);
  1205. #else
  1206. Motor_Qingsao_R_PWM(OFF);
  1207. Motor_Qingsao_L_PWM(OFF);
  1208. #endif
  1209. rt_thread_delay(2000);
  1210. break;
  1211. }
  1212. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1213. {
  1214. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1215. // DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1216. #if PWM_EN == 0
  1217. rt_pin_write(CLEAR_R, ON);
  1218. rt_pin_write(CLEAR_L, ON);
  1219. #else
  1220. Motor_Qingsao_R_PWM(OFF);
  1221. Motor_Qingsao_L_PWM(OFF);
  1222. #endif
  1223. rt_free(makeJson_Warn("Clear Motor ERR", "device"));
  1224. if (Read_Real_Voltage() / 100 < 12)
  1225. {
  1226. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1227. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1228. device_status.clear_status = 0;
  1229. return 0;
  1230. }
  1231. break;
  1232. }
  1233. }
  1234. clear_num = 0;
  1235. // 如果清扫位置在左侧
  1236. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1237. {
  1238. #if PWM_EN == 0
  1239. rt_pin_write(CLEAR_R, ON);
  1240. rt_pin_write(CLEAR_L, OFF);
  1241. #else
  1242. Motor_Qingsao_R_PWM(ON);
  1243. Motor_Qingsao_L_PWM(OFF);
  1244. #endif
  1245. rt_thread_delay(50);
  1246. clear_num++;
  1247. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1248. {
  1249. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1250. // DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1251. #if PWM_EN == 0
  1252. rt_pin_write(CLEAR_R, ON);
  1253. rt_pin_write(CLEAR_L, ON);
  1254. #else
  1255. Motor_Qingsao_R_PWM(OFF);
  1256. Motor_Qingsao_L_PWM(OFF);
  1257. #endif
  1258. rt_free(makeJson_Warn("Clear Motor ERR", "device"));
  1259. if (Read_Real_Voltage() / 100 < 12)
  1260. {
  1261. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1262. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1263. device_status.clear_status = 0;
  1264. return 0;
  1265. }
  1266. break;
  1267. }
  1268. }
  1269. clear_num = 0;
  1270. while (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1 || (Read_Real_Voltage() / 100) < 12)
  1271. {
  1272. #if PWM_EN == 0
  1273. rt_pin_write(CLEAR_R, ON);
  1274. rt_pin_write(CLEAR_L, ON);
  1275. #else
  1276. Motor_Qingsao_R_PWM(OFF);
  1277. Motor_Qingsao_L_PWM(OFF);
  1278. #endif
  1279. if (Read_Real_Voltage() / 100 < 12)
  1280. {
  1281. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1282. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1283. device_status.clear_status = 0;
  1284. return 0;
  1285. }
  1286. rt_thread_delay(2000);
  1287. break;
  1288. }
  1289. while (rt_pin_read(CLEAR_L_LIMIT) == 0)
  1290. {
  1291. #if PWM_EN == 0
  1292. rt_pin_write(CLEAR_R, OFF);
  1293. rt_pin_write(CLEAR_L, ON);
  1294. #else
  1295. Motor_Qingsao_R_PWM(OFF);
  1296. Motor_Qingsao_L_PWM(ON);
  1297. #endif
  1298. rt_thread_delay(50);
  1299. clear_num++;
  1300. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1301. {
  1302. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1303. char *Clear_ERR = makeJson_Warn("Clear Motor ERR", "device");
  1304. // DTU_Write("%s",Clear_ERR);
  1305. #if PWM_EN == 0
  1306. rt_pin_write(CLEAR_R, ON);
  1307. rt_pin_write(CLEAR_L, ON);
  1308. #else
  1309. Motor_Qingsao_R_PWM(OFF);
  1310. Motor_Qingsao_L_PWM(OFF);
  1311. #endif
  1312. rt_free(Clear_ERR);
  1313. if (Read_Real_Voltage() / 100 < 12)
  1314. {
  1315. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1316. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1317. device_status.clear_status = 0;
  1318. return 0;
  1319. }
  1320. break;
  1321. }
  1322. }
  1323. while (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1)
  1324. {
  1325. #if PWM_EN == 0
  1326. rt_pin_write(CLEAR_R, ON);
  1327. rt_pin_write(CLEAR_L, ON);
  1328. #else
  1329. Motor_Qingsao_R_PWM(OFF);
  1330. Motor_Qingsao_L_PWM(OFF);
  1331. #endif
  1332. rt_thread_delay(2000);
  1333. break;
  1334. }
  1335. while (rt_pin_read(CLEAR_L_LIMIT) == 1 && rt_pin_read(CLEAR_R_LIMIT) == 1)
  1336. {
  1337. #if PWM_EN == 0
  1338. rt_pin_write(CLEAR_R, ON);
  1339. rt_pin_write(CLEAR_L, ON);
  1340. #else
  1341. Motor_Qingsao_R_PWM(OFF);
  1342. Motor_Qingsao_L_PWM(OFF);
  1343. #endif
  1344. if (device_status.version_type == 41)
  1345. {
  1346. rt_kprintf("Clear Motor Limit Not Access:%d", atoi(device_status.power));
  1347. char *Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access", "device");
  1348. // DTU_Write("%s",Clear_ERR);
  1349. rt_free(Clear_ERR);
  1350. }
  1351. return 0;
  1352. }
  1353. clear_num = 0;
  1354. rt_thread_delay(2000);
  1355. #if PWM_EN == 0
  1356. rt_pin_write(CLEAR_R, ON);
  1357. rt_pin_write(CLEAR_L, ON);
  1358. #else
  1359. Motor_Qingsao_R_PWM(OFF);
  1360. Motor_Qingsao_L_PWM(OFF);
  1361. #endif
  1362. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1363. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1364. device_status.clear_status = 0;
  1365. return 1;
  1366. }
  1367. // 路由器供电
  1368. void ctr_router(uint8_t status_router)
  1369. {
  1370. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1371. if (device_status.version_type == 6)
  1372. rt_pin_mode(ROUTER_P, PIN_MODE_OUTPUT);
  1373. // rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1374. if (status_router == ON)
  1375. {
  1376. rt_pin_write(ROUTER, ON);
  1377. //
  1378. #if LAMP_EN == 0
  1379. if (device_status.version_type == 6)
  1380. rt_pin_write(ROUTER_P, ON);
  1381. #endif
  1382. }
  1383. else
  1384. {
  1385. rt_pin_write(ROUTER, OFF);
  1386. //
  1387. #if LAMP_EN == 0
  1388. if (device_status.version_type == 6)
  1389. rt_pin_write(ROUTER_P, OFF);
  1390. #endif
  1391. }
  1392. }
  1393. // 清扫-推杆电机
  1394. void ctr_TG_Clear(uint16_t clear_timeout)
  1395. {
  1396. #if 0
  1397. int current_clear[200];
  1398. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1399. LOG_I("清扫电机待机:%d",Electric_current);
  1400. #endif
  1401. static uint16_t clear_count = 0;
  1402. clear_count = clear_timeout;
  1403. #if PWM_EN == 0
  1404. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1405. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1406. #endif
  1407. while (clear_count != 0)
  1408. {
  1409. #if PWM_EN == 0
  1410. rt_pin_write(CLEAR_R, ON);
  1411. rt_pin_write(CLEAR_L, OFF);
  1412. #else
  1413. Motor_Qingsao_R_PWM(ON);
  1414. Motor_Qingsao_L_PWM(OFF);
  1415. #endif
  1416. clear_count--;
  1417. rt_thread_delay(100);
  1418. }
  1419. #if PWM_EN == 0
  1420. rt_pin_write(CLEAR_R, ON);
  1421. rt_pin_write(CLEAR_L, ON);
  1422. #else
  1423. Motor_Qingsao_R_PWM(OFF);
  1424. Motor_Qingsao_L_PWM(OFF);
  1425. #endif
  1426. rt_thread_delay(3000);
  1427. clear_count = 0;
  1428. while (clear_timeout != clear_count)
  1429. {
  1430. #if PWM_EN == 0
  1431. rt_pin_write(CLEAR_R, OFF);
  1432. rt_pin_write(CLEAR_L, ON);
  1433. #else
  1434. Motor_Qingsao_R_PWM(OFF);
  1435. Motor_Qingsao_L_PWM(ON);
  1436. #endif
  1437. // if(clear_count<200)
  1438. // current_clear[clear_count] = atoi(device_status.power);
  1439. clear_count++;
  1440. rt_thread_delay(100);
  1441. }
  1442. #if PWM_EN == 0
  1443. rt_pin_write(CLEAR_R, ON);
  1444. rt_pin_write(CLEAR_L, ON);
  1445. #else
  1446. Motor_Qingsao_R_PWM(OFF);
  1447. Motor_Qingsao_L_PWM(OFF);
  1448. #endif
  1449. #if 0
  1450. bubblesort(current_clear,200);
  1451. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1452. {
  1453. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1454. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1455. DTU_Write("%s",ctr_clear_warn);
  1456. rt_free(ctr_clear_warn);
  1457. }
  1458. #endif
  1459. rt_thread_delay(2000);
  1460. device_status.clear_status = 0;
  1461. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1462. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1463. }
  1464. extern void CTL_CAM1(void);
  1465. extern void CTL_CAM0(void);
  1466. // DTU,RTU供电
  1467. extern char CamFree;
  1468. // 海康拍照
  1469. Res_def Hk_Take_io(void)
  1470. {
  1471. Res_def result = res_OK;
  1472. if (CamFree == 0)
  1473. {
  1474. rt_kprintf("Camera busying...\n\n");
  1475. return;
  1476. }
  1477. CamFree = 0;
  1478. syscfg.photo_finsh = 1;
  1479. DTU_Write("takephoto.mp3");
  1480. rt_thread_delay(1000);
  1481. char *takephotojson = makeJson_takephoto();
  1482. DTU_Write("%s", takephotojson);
  1483. rt_free(takephotojson);
  1484. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1485. rt_pin_write(HK_IO, 0);
  1486. if (device_status.dtype == 3)
  1487. Beep_Tip(1000);
  1488. if (device_status.dtype == 7)
  1489. rt_thread_mdelay(1000);
  1490. rt_pin_write(HK_IO, 1);
  1491. uint32_t cnt = 0;
  1492. while (1)
  1493. {
  1494. if (CamFree)
  1495. {
  1496. LOG_I("图片上传成功");
  1497. HMI_WRITE("sysTest.work_sta.txt=\" 图片上传成功\"");
  1498. UpLoad_Data(2); // 拍照完成回复(落虫计数)
  1499. break;
  1500. }
  1501. if (cnt == 0)
  1502. {
  1503. UpLoad_Data(4);
  1504. }
  1505. if (cnt++ > 249)
  1506. {
  1507. rt_kprintf("Waiting for image trans to complete timeout...\n");
  1508. Recode_e_code(e_cam_timeout, (uint8_t)((rt_pin_read(RTU_DTU) << 4) | syscfg.photo_finsh), "等待图像传输完成超时");
  1509. result = res_Error;
  1510. UpLoad_Data(3); // 拍照完成回复(落虫计数)
  1511. break;
  1512. }
  1513. if(syscfg.photo_finsh == 2)
  1514. {
  1515. LOG_I("图片上传开始...");
  1516. syscfg.photo_finsh = 3;
  1517. cnt = 1;
  1518. }
  1519. rt_kprintf("Waiting for image trans >>> %d\n", cnt);
  1520. rt_thread_mdelay(1000);
  1521. Updata_INR_Simple();
  1522. }
  1523. CamFree = 1;
  1524. syscfg.photo_finsh = 0;
  1525. return result;
  1526. }
  1527. MSH_CMD_EXPORT(Hk_Take_io, Hk_Take_io);
  1528. /**
  1529. * @brief 控制执行加热动作
  1530. *
  1531. * @param heat_time 加热时间(秒)
  1532. */
  1533. void calorstat(uint16_t heat_time)
  1534. {
  1535. // uint8_t heat_up_cnt = 0;
  1536. uint16_t n = 0;
  1537. uint16_t delay_heat = 60;
  1538. FlagStatus heat_flg = RESET;
  1539. device_status.heat_time_min = 0;
  1540. device_status.run_sta = device_status.heattem; // 初始化加热实时温度
  1541. ctr_Heat(ON);
  1542. // UpLoad_Data(); //开始加热时的第一条数据
  1543. // 恒温模式
  1544. for (n = 0; n < heat_time; n++)
  1545. {
  1546. if (device_status.heattem > device_status.heatvalue && heat_flg == RESET)
  1547. {
  1548. heat_flg = SET;
  1549. ctr_Heat(OFF);
  1550. while (delay_heat != 0)
  1551. {
  1552. delay_heat--;
  1553. rt_kprintf("正在恒温加热!!!:%d", delay_heat);
  1554. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"", delay_heat);
  1555. rt_thread_delay(1000);
  1556. if (device_status.heattem > device_status.run_sta + 1)
  1557. {
  1558. device_status.run_sta = device_status.heattem;
  1559. }
  1560. }
  1561. delay_heat = 60;
  1562. // 跳出FOR循环
  1563. n = n + 60;
  1564. }
  1565. if (device_status.heattem < (device_status.heatvalue - 5) && heat_flg == SET)
  1566. {
  1567. device_status.run_sta = device_status.heattem;
  1568. ctr_Heat(ON);
  1569. }
  1570. else if (device_status.heattem >= device_status.heatvalue && heat_flg == SET)
  1571. {
  1572. // if(heat_up_cnt==0)
  1573. // {
  1574. // UpLoad_Data(); //加热到设定温度的数据
  1575. // heat_up_cnt = 1;
  1576. // }
  1577. device_status.run_sta = device_status.heattem;
  1578. ctr_Heat(OFF);
  1579. }
  1580. if ((n % 60) == 0)
  1581. {
  1582. device_status.heat_time_min = device_status.heat_time_min + 1;
  1583. // if(device_status.heat_time_min % 5 == 0)
  1584. // {
  1585. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1586. // }
  1587. }
  1588. // 如果触发异常退出加热
  1589. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  1590. {
  1591. rt_thread_delay(1000);
  1592. if (device_status.bat_status == 1)
  1593. {
  1594. rt_kprintf("============================触发异常退出加热==========================");
  1595. ctr_lamp(OFF, 1);
  1596. ctr_Heat(OFF);
  1597. break;
  1598. }
  1599. }
  1600. if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  1601. {
  1602. ctr_Heat(OFF);
  1603. rt_kprintf("异常退出!!!\n");
  1604. break;
  1605. }
  1606. rt_thread_delay(1000);
  1607. rt_kprintf("=======正在加热======:%d-%d\n", n, heat_time);
  1608. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"", n, heat_time);
  1609. }
  1610. ctr_Heat(OFF);
  1611. // UpLoad_Data(); //加热结束的数据
  1612. }
  1613. // 初始化所有控制驱动,关闭
  1614. void HardWare_Init(uint8_t boot)
  1615. {
  1616. Beep_Tip(200);
  1617. // 关闭镇流器
  1618. if (device_status.autotakepic == 0)
  1619. ctr_lamp(OFF, 1);
  1620. // 关闭震动电机
  1621. ctr_shake(OFF, 0);
  1622. // 关闭加热
  1623. ctr_Heat(OFF);
  1624. // 关闭补光灯
  1625. ctr_led(OFF);
  1626. // 关闭12V供电(海康相机,RTU)
  1627. ctr_photo(OFF);
  1628. }
  1629. // 拍照
  1630. void HK_TakePhoto(void)
  1631. {
  1632. // 打开补光灯
  1633. DTU_Write("takephoto.mp3");
  1634. rt_thread_delay(1000);
  1635. // 吸虫塔
  1636. if (device_status.version_type != 6)
  1637. {
  1638. ctr_led(ON);
  1639. if (device_status.version_type == 48)
  1640. {
  1641. rt_thread_delay(1000 * 60);
  1642. }
  1643. else
  1644. {
  1645. rt_thread_delay(1000 * 15);
  1646. }
  1647. }
  1648. // 触发海康相机拍照
  1649. Hk_Take_io();
  1650. if (device_status.version_type != 6)
  1651. {
  1652. rt_thread_delay(1000 * 45);
  1653. }
  1654. else
  1655. {
  1656. ctr_led(ON);
  1657. rt_thread_delay(1000 * 19);
  1658. }
  1659. ctr_led(OFF);
  1660. }
  1661. // 光控测试任务
  1662. void SunTest(void)
  1663. {
  1664. rt_uint16_t ui_timeout_l = 0;
  1665. // 未检测到光照,一秒扫描等待,超时30S
  1666. while (Read_Light_Voltage() < 500)
  1667. {
  1668. ui_timeout_l++;
  1669. rt_thread_delay(1000);
  1670. if (ui_timeout_l == 30)
  1671. break;
  1672. }
  1673. rt_thread_delay(3000);
  1674. if (Read_Light_Voltage() < 500)
  1675. {
  1676. device_status.sun_status = 0; // 光控状态
  1677. }
  1678. if (Read_Light_Voltage() > 500)
  1679. {
  1680. rt_thread_delay(1000);
  1681. }
  1682. rt_thread_delay(3000);
  1683. if (Read_Light_Voltage() > 500)
  1684. {
  1685. device_status.sun_status = 1;
  1686. ctr_lamp(OFF, 1);
  1687. }
  1688. HMI_WRITE("sysTest.switchSun.val=%d", 0);
  1689. device_status.sun_test = 0;
  1690. device_status.work_status = 0;
  1691. }
  1692. /*上电初始化*/
  1693. void ResetInit(void)
  1694. {
  1695. // 关闭补光灯
  1696. ctr_led(OFF);
  1697. // 关诱捕灯
  1698. ctr_lamp(OFF, 1);
  1699. // 关风机
  1700. ctr_Collect_Fan(OFF);
  1701. if (device_status.bat_status == 1 || atoi(device_status.fuse_voltage) < 1)
  1702. {
  1703. // LOG_W("欠压!");
  1704. return;
  1705. }
  1706. // 1.打开路由器供电
  1707. ctr_router(ON);
  1708. ctr_RTU_DTU(ON);
  1709. // 2.打开相机供电
  1710. ctr_photo(ON);
  1711. rt_kprintf("RTU POWER ON");
  1712. }
  1713. /*三色光源*/
  1714. void Led_Check(uint8_t tp)
  1715. {
  1716. if (tp == 3)
  1717. {
  1718. if (device_status.light_over_tim > 2 * 3600)
  1719. {
  1720. device_status.work_status = 1;
  1721. device_status.lamp_status = 1;
  1722. device_status.lamp1_status = 0;
  1723. device_status.lamp2_status = 0;
  1724. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1725. }
  1726. else if (device_status.light_over_tim > 3600 && device_status.light_over_tim < 7200)
  1727. {
  1728. device_status.work_status = 1;
  1729. device_status.lamp_status = 0;
  1730. device_status.lamp1_status = 1;
  1731. device_status.lamp2_status = 0;
  1732. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1733. }
  1734. else if (device_status.light_over_tim < 1 * 3600)
  1735. {
  1736. device_status.work_status = 1;
  1737. device_status.lamp_status = 0;
  1738. device_status.lamp1_status = 0;
  1739. device_status.lamp2_status = 1;
  1740. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1741. }
  1742. }
  1743. else if (tp == 6)
  1744. {
  1745. if (device_status.light_over_tim > 4 * 3600)
  1746. {
  1747. device_status.work_status = 1;
  1748. device_status.lamp_status = 1;
  1749. device_status.lamp1_status = 0;
  1750. device_status.lamp2_status = 0;
  1751. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1752. }
  1753. else if (device_status.light_over_tim < 14400 && device_status.light_over_tim > 7200)
  1754. {
  1755. device_status.work_status = 1;
  1756. device_status.lamp_status = 0;
  1757. device_status.lamp1_status = 1;
  1758. device_status.lamp2_status = 0;
  1759. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1760. }
  1761. else if (device_status.light_over_tim < 7200)
  1762. {
  1763. device_status.work_status = 1;
  1764. device_status.lamp_status = 0;
  1765. device_status.lamp1_status = 0;
  1766. device_status.lamp2_status = 1;
  1767. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1768. }
  1769. }
  1770. else if (tp == 9)
  1771. {
  1772. if (device_status.light_over_tim > 6 * 3600)
  1773. {
  1774. device_status.work_status = 1;
  1775. device_status.lamp_status = 1;
  1776. device_status.lamp1_status = 0;
  1777. device_status.lamp2_status = 0;
  1778. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1779. }
  1780. else if (device_status.light_over_tim < 6 * 3600 && device_status.light_over_tim > 3 * 3600)
  1781. {
  1782. device_status.work_status = 1;
  1783. device_status.lamp_status = 0;
  1784. device_status.lamp1_status = 1;
  1785. device_status.lamp2_status = 0;
  1786. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1787. }
  1788. else if (device_status.light_over_tim < 3 * 3600)
  1789. {
  1790. device_status.work_status = 1;
  1791. device_status.lamp_status = 0;
  1792. device_status.lamp1_status = 0;
  1793. device_status.lamp2_status = 1;
  1794. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1795. }
  1796. }
  1797. }
  1798. // 出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1799. void HardWare_Check(uint8_t boot)
  1800. {
  1801. if (boot == 0 && device_status.version_type != 6)
  1802. {
  1803. // 停止所有动作 -完成
  1804. HardWare_Init(boot);
  1805. }
  1806. else
  1807. {
  1808. // 停止所有动作
  1809. // 读取记录待机功耗(实测大概70mA)
  1810. //(需减去屏幕的功耗大概120mA)
  1811. // 动作上仓门,读取功率值保存
  1812. // 动作下仓门,读取功率值保存
  1813. // 动作清扫,读取功率值保存
  1814. // 打开镇流器,读取功率值保存
  1815. // 打开海康相机供电,读取功率值保存
  1816. // 打开补光灯,读取功率值保存
  1817. // 打开路由器/RTU供电,读取功率值保存
  1818. // 打开加热,读取功率值保存
  1819. }
  1820. }
  1821. // 工作状态检测
  1822. void WorkCheck(void)
  1823. {
  1824. uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  1825. static int16_t log_en_cnt = -1;
  1826. // 雨控检测,温控检测,电池电压检测
  1827. while (device_status.ds == 1 && device_status.rain_status == 0 && device_status.temp_status == 0 && device_status.bat_status == 0)
  1828. {
  1829. // 工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1830. if (device_status.timeSwitch == 1)
  1831. {
  1832. LOG_I("***********时控*****************");
  1833. // 如果开始和结束是同一天
  1834. if (device_status.startBar < device_status.stopBar)
  1835. {
  1836. LOG_I("开始和结束是同一天");
  1837. time_t now;
  1838. time(&now);
  1839. static struct tm nowtime; // 现在
  1840. static struct tm starttime; // 开始
  1841. static struct tm endtime; // 结束
  1842. // 获取当前时间戳
  1843. nowtime = *localtime(&now);
  1844. nowtime.tm_hour = nowtime.tm_hour - 8;
  1845. // 获取定时开始的时间戳
  1846. starttime = *localtime(&now);
  1847. starttime.tm_hour = device_status.startBar - 8;
  1848. starttime.tm_min = 0;
  1849. starttime.tm_sec = 0;
  1850. // 获取定时结束的时间戳
  1851. endtime = *localtime(&now);
  1852. endtime.tm_hour = device_status.stopBar - 8;
  1853. endtime.tm_min = 0;
  1854. endtime.tm_sec = 0;
  1855. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***", mktime(&nowtime), mktime(&starttime), mktime(&endtime));
  1856. // 如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1857. if ((difftime(mktime(&nowtime), mktime(&starttime)) >= 0) && (difftime(mktime(&nowtime), mktime(&endtime)) < 0))
  1858. {
  1859. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1860. device_status.work_status = 1;
  1861. }
  1862. // 如果当前时间小于开始时间
  1863. else if (difftime(mktime(&nowtime), mktime(&starttime)) < 0)
  1864. {
  1865. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d", mktime(&nowtime), mktime(&starttime));
  1866. device_status.work_status = 0;
  1867. }
  1868. // 当前时间大于结束时间
  1869. else
  1870. {
  1871. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d", mktime(&nowtime), mktime(&starttime));
  1872. device_status.work_status = 0;
  1873. }
  1874. #if LAMP_EN == 1
  1875. // 如果当前时间超过20点,未到21点
  1876. if ((difftime(mktime(&nowtime), mktime(&starttime)) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 3600) < 0))
  1877. {
  1878. LOG_I("3led:20~21");
  1879. device_status.lamp_status = 1;
  1880. device_status.lamp1_status = 0;
  1881. device_status.lamp2_status = 0;
  1882. }
  1883. else if ((difftime(mktime(&nowtime), mktime(&starttime) + 3600) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 7200) < 0))
  1884. {
  1885. LOG_I("3led:21~22");
  1886. device_status.lamp1_status = 1;
  1887. device_status.lamp_status = 0;
  1888. device_status.lamp2_status = 0;
  1889. }
  1890. else if ((difftime(mktime(&nowtime), mktime(&starttime) + 7200) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 10800) < 0))
  1891. {
  1892. LOG_I("3led:22~23");
  1893. device_status.lamp2_status = 1;
  1894. device_status.lamp_status = 0;
  1895. device_status.lamp1_status = 0;
  1896. }
  1897. #endif
  1898. }
  1899. // 如果结束时间是第二天
  1900. else if (device_status.startBar > device_status.stopBar)
  1901. {
  1902. time_t now;
  1903. time(&now);
  1904. static struct tm nowtime; // 现在
  1905. static struct tm starttime; // 开始
  1906. static struct tm endtime; // 结束
  1907. // 获取当前时间戳
  1908. nowtime = *localtime(&now);
  1909. nowtime.tm_hour = nowtime.tm_hour - 8;
  1910. // 获取定时开始的时间戳
  1911. starttime = *localtime(&now);
  1912. starttime.tm_hour = device_status.startBar - 8;
  1913. starttime.tm_min = 0;
  1914. starttime.tm_sec = 0;
  1915. // 获取定时结束的时间戳
  1916. endtime = *localtime(&now);
  1917. endtime.tm_hour = device_status.stopBar - 8;
  1918. endtime.tm_min = 0;
  1919. endtime.tm_sec = 0;
  1920. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!", mktime(&nowtime), mktime(&starttime), mktime(&endtime));
  1921. // 如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1922. if ((difftime(mktime(&nowtime), mktime(&starttime)) > 0))
  1923. {
  1924. // 获取定时结束的时间戳
  1925. endtime = *localtime(&now); // 将获取到的时间转换为时间戳
  1926. endtime.tm_hour = device_status.stopBar - 8; // 这里需减8用于时区转换
  1927. endtime.tm_min = 0;
  1928. endtime.tm_sec = 0;
  1929. device_status.work_status = 1;
  1930. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1931. }
  1932. // 当前时间小于开始时间
  1933. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1934. {
  1935. // 获取定时结束的时间戳
  1936. endtime = *localtime(&now); // 将获取到的时间转换为时间戳
  1937. endtime.tm_hour = device_status.stopBar - 8; // 这里需减8用于时区转换
  1938. endtime.tm_min = 0;
  1939. endtime.tm_sec = 0;
  1940. // 如果当前时间戳小于结束时间戳
  1941. if (difftime(mktime(&nowtime), mktime(&endtime)) < 0)
  1942. {
  1943. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1944. device_status.work_status = 1;
  1945. }
  1946. else
  1947. {
  1948. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1949. device_status.work_status = 0; // 不在工作时间,待机
  1950. }
  1951. }
  1952. }
  1953. }
  1954. else
  1955. {
  1956. LOG_I("光控定时模式!");
  1957. /*
  1958. time_t now;
  1959. time(&now);
  1960. static struct tm nowstamp;
  1961. nowstamp =*localtime(&now);
  1962. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1963. static rt_uint32_t nowstamp_num = 0;
  1964. nowstamp_num = mktime(&nowstamp);
  1965. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1966. */
  1967. if (device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1968. {
  1969. device_status.light_over_tim--;
  1970. #if LAMP_EN == 1
  1971. Led_Check(device_status.sunBar);
  1972. #else
  1973. device_status.work_status = 1;
  1974. ;
  1975. LOG_I("光控定时,计时中>>>>>>>>>:%d", device_status.light_over_tim);
  1976. #endif
  1977. }
  1978. else
  1979. {
  1980. if (device_status.test_flag == 0)
  1981. {
  1982. ctr_lamp(OFF, 1);
  1983. #if LAMP_EN == 1
  1984. ctr_lamp(OFF, 2);
  1985. ctr_lamp(OFF, 3);
  1986. #endif
  1987. }
  1988. device_status.work_status = 0;
  1989. if (device_status.light_over_tim == 0 && device_status.sun_status == 1)
  1990. device_status.light_over_tim = device_status.sunBar * 3600;
  1991. LOG_I("光控中,白天:%d!!!", device_status.sun_status);
  1992. }
  1993. }
  1994. rt_thread_delay(1000);
  1995. }
  1996. if (chk_log_time)
  1997. {
  1998. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d", device_status.rain_status, device_status.temp_status, device_status.bat_status);
  1999. }
  2000. device_status.work_status = 0;
  2001. rt_thread_delay(1000);
  2002. }
  2003. /**
  2004. * @brief 孢子仪工作线程
  2005. *
  2006. */
  2007. void WorkTask_BZY(void)
  2008. {
  2009. if (device_status.ds == 1)
  2010. {
  2011. // 时间段判断
  2012. // 先判断第一个时间段
  2013. time_t now;
  2014. time(&now);
  2015. static struct tm now_stamp; // 现在
  2016. static struct tm cul_sh1_stamp; // 开始1
  2017. static struct tm cul_eh1_stamp; // 结束1
  2018. static struct tm cul_sh2_stamp; // 开始2
  2019. static struct tm cul_eh2_stamp; // 结束2
  2020. static struct tm cul_sh3_stamp; // 开始3
  2021. static struct tm cul_eh3_stamp; // 结束3
  2022. now_stamp = *localtime(&now);
  2023. // 获取定时开始的时间戳
  2024. cul_sh1_stamp = *localtime(&now);
  2025. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2026. cul_sh1_stamp.tm_min = 0;
  2027. cul_sh1_stamp.tm_sec = 0;
  2028. cul_sh2_stamp = *localtime(&now);
  2029. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2030. cul_sh2_stamp.tm_min = 0;
  2031. cul_sh2_stamp.tm_sec = 0;
  2032. cul_sh3_stamp = *localtime(&now);
  2033. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2034. cul_sh3_stamp.tm_min = 0;
  2035. cul_sh3_stamp.tm_sec = 0;
  2036. // 获取定时结束的时间戳
  2037. cul_eh1_stamp = *localtime(&now);
  2038. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2039. cul_eh1_stamp.tm_min = 0;
  2040. cul_eh1_stamp.tm_sec = 0;
  2041. cul_eh2_stamp = *localtime(&now);
  2042. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2043. cul_eh2_stamp.tm_min = 0;
  2044. cul_eh2_stamp.tm_sec = 0;
  2045. cul_eh3_stamp = *localtime(&now);
  2046. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2047. cul_eh3_stamp.tm_min = 0;
  2048. cul_eh3_stamp.tm_sec = 0;
  2049. 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);
  2050. 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);
  2051. 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);
  2052. 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);
  2053. 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);
  2054. 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);
  2055. // 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2056. // 当前时间戳小于第一段开始时间戳
  2057. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2058. {
  2059. time_t now;
  2060. time(&now);
  2061. cul_sh1_stamp = *localtime(&now);
  2062. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2063. cul_sh1_stamp.tm_min = 0;
  2064. cul_sh1_stamp.tm_sec = 0;
  2065. now_stamp = *localtime(&now);
  2066. 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);
  2067. 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);
  2068. LOG_D("run_sta:%d", device_status.run_sta);
  2069. }
  2070. // 当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2071. if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2072. {
  2073. time_t now;
  2074. time(&now);
  2075. cul_sh2_stamp = *localtime(&now);
  2076. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2077. cul_sh2_stamp.tm_min = 0;
  2078. cul_sh2_stamp.tm_sec = 0;
  2079. now_stamp = *localtime(&now);
  2080. 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);
  2081. 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);
  2082. LOG_D("run_sta:%d", device_status.run_sta);
  2083. }
  2084. // 如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2085. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2086. {
  2087. // 转盘到涂凡士林位置
  2088. if (device_status.run_sta == 0)
  2089. {
  2090. LOG_I("start to collect!!!");
  2091. DTU_Write("collect.mp3");
  2092. ctr_turn(90, 4);
  2093. rt_thread_delay(2000);
  2094. // 涂凡士林
  2095. ctr_Petroleum_jelly(10);
  2096. rt_thread_delay(5000);
  2097. }
  2098. /*转盘到采集位置*/
  2099. if (device_status.run_sta == 4)
  2100. ctr_turn(90, 1);
  2101. // 只有开始和结束时刻不同的时间段,才有效
  2102. if ((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0) || (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2103. {
  2104. if ((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0) && difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) > 0)
  2105. {
  2106. device_status.work_status = 1;
  2107. ctr_Collect_Fan(ON);
  2108. // 当前时间戳小于第一段结束时间戳
  2109. while (difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)
  2110. {
  2111. time(&now);
  2112. now_stamp = *localtime(&now);
  2113. cul_eh1_stamp = *localtime(&now);
  2114. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2115. cul_eh1_stamp.tm_min = 0;
  2116. cul_eh1_stamp.tm_sec = 0;
  2117. if (device_status.run_sta != 1)
  2118. break;
  2119. LOG_I("1===========采集风机启动,正在收集.....%d-%d", mktime(&now_stamp), mktime(&cul_eh1_stamp));
  2120. rt_thread_mdelay(10000);
  2121. }
  2122. }
  2123. else
  2124. {
  2125. device_status.work_status = 0;
  2126. ctr_Collect_Fan(OFF);
  2127. LOG_I("未到收集1时间!");
  2128. }
  2129. if (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) > 0)
  2130. {
  2131. device_status.work_status = 1;
  2132. ctr_Collect_Fan(ON);
  2133. while (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0)
  2134. {
  2135. time(&now);
  2136. now_stamp = *localtime(&now);
  2137. cul_eh2_stamp = *localtime(&now);
  2138. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2139. cul_eh2_stamp.tm_min = 0;
  2140. cul_eh2_stamp.tm_sec = 0;
  2141. if (device_status.run_sta != 1)
  2142. break;
  2143. LOG_I("2===========采集风机启动,正在收集.....%d-%d", mktime(&now_stamp), mktime(&cul_eh2_stamp));
  2144. rt_thread_mdelay(10000);
  2145. }
  2146. }
  2147. else
  2148. {
  2149. device_status.work_status = 0;
  2150. ctr_Collect_Fan(OFF);
  2151. LOG_I("未到收集2时间!");
  2152. }
  2153. }
  2154. else
  2155. {
  2156. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2157. }
  2158. }
  2159. if (device_status.run_sta == 2)
  2160. {
  2161. ctr_Collect_Fan(OFF);
  2162. device_status.develop_flag = 1;
  2163. device_status.work_status = 2;
  2164. ctr_develop(); // 培养
  2165. ctr_turn(90, 3);
  2166. }
  2167. if (device_status.run_sta == 3)
  2168. {
  2169. ctr_Collect_Fan(OFF);
  2170. device_status.work_status = 3;
  2171. device_status.autotakepic = 1; // 对焦拍照
  2172. }
  2173. // 在培养之前再次确认时间戳
  2174. time(&now);
  2175. now_stamp = *localtime(&now);
  2176. cul_eh2_stamp = *localtime(&now);
  2177. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2178. cul_eh2_stamp.tm_min = 0;
  2179. cul_eh2_stamp.tm_sec = 0;
  2180. // 当前时间戳大于收集结束时间戳并且run_sta==1
  2181. if (device_status.run_sta == 1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2182. {
  2183. ctr_Collect_Fan(OFF);
  2184. LOG_I("===============collect ok,turn to drop!!!");
  2185. device_status.develop_flag = 1;
  2186. device_status.work_status = 2; // 现在是培养
  2187. DTU_Write("develop.mp3");
  2188. if (device_status.run_sta == 1)
  2189. ctr_turn(90, 2); /*转盘到培养位置*/
  2190. rt_thread_delay(1000);
  2191. ctr_shake(ON, device_status.shake_sec);
  2192. rt_thread_delay(1000);
  2193. ctr_develop(); // 培养
  2194. if (device_status.run_sta == 2)
  2195. ctr_turn(90, 3); /*转盘到拍照位置*/
  2196. rt_thread_delay(1000);
  2197. device_status.work_status = 3; // 现在是拍照
  2198. device_status.autotakepic = 1; // 对焦拍照
  2199. while (device_status.autotakepic == 0)
  2200. {
  2201. rt_thread_mdelay(1000);
  2202. LOG_I("正在对焦拍照............B");
  2203. }
  2204. device_status.work_status = 0; // 待机
  2205. DTU_Write("wait.mp3");
  2206. LOG_I("=====================work finished==================");
  2207. }
  2208. rt_thread_delay(1000);
  2209. }
  2210. device_status.work_status = 0; // 待机
  2211. LOG_W("==================!!!设备已待机!!!===================");
  2212. }
  2213. /********************* 胶带结构的孢子仪主工作流程 ************************/
  2214. #define test_min 0
  2215. /**
  2216. * @brief 检查是否到工作时间段
  2217. *
  2218. * @return uint8_t 0-未到 / 已到:时间段编号(1-2-3)
  2219. */
  2220. static uint8_t Confirm_time_period(void)
  2221. {
  2222. static uint8_t once_print = 0;
  2223. uint8_t log_en_inr = LOG_INR * 3; // 打印log的速度 数值越小 打印越频繁
  2224. static int16_t log_en_cnt = -1;
  2225. uint8_t time_period = 0;
  2226. // 时间段判断
  2227. // 先判断第一个时间段
  2228. time_t now;
  2229. time(&now);
  2230. static struct tm now_stamp; // 现在
  2231. static struct tm cul_sh1_stamp; // 开始1
  2232. static struct tm cul_eh1_stamp; // 结束1
  2233. static struct tm cul_sh2_stamp; // 开始2
  2234. static struct tm cul_eh2_stamp; // 结束2
  2235. static struct tm cul_sh3_stamp; // 开始3
  2236. static struct tm cul_eh3_stamp; // 结束3
  2237. now_stamp = *localtime(&now);
  2238. // 获取定时开始的时间戳
  2239. cul_sh1_stamp = *localtime(&now);
  2240. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2241. cul_sh1_stamp.tm_min = 0 + test_min;
  2242. cul_sh1_stamp.tm_sec = 0;
  2243. cul_sh2_stamp = *localtime(&now);
  2244. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2245. cul_sh2_stamp.tm_min = 0 + test_min;
  2246. cul_sh2_stamp.tm_sec = 0;
  2247. cul_sh3_stamp = *localtime(&now);
  2248. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2249. cul_sh3_stamp.tm_min = 0 + test_min;
  2250. cul_sh3_stamp.tm_sec = 0;
  2251. // 获取定时结束的时间戳
  2252. cul_eh1_stamp = *localtime(&now);
  2253. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2254. cul_eh1_stamp.tm_min = 0 + test_min;
  2255. cul_eh1_stamp.tm_sec = 0;
  2256. cul_eh2_stamp = *localtime(&now);
  2257. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2258. cul_eh2_stamp.tm_min = 0 + test_min;
  2259. cul_eh2_stamp.tm_sec = 0;
  2260. cul_eh3_stamp = *localtime(&now);
  2261. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2262. cul_eh3_stamp.tm_min = 0 + test_min;
  2263. cul_eh3_stamp.tm_sec = 0;
  2264. if (once_print == 0 || chk_log_time)
  2265. {
  2266. rt_kprintf("\n\n");
  2267. 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);
  2268. LOG_I("结束时间1-%04d-%02d-%02d %02d:%02d:%02d\n", 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);
  2269. 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);
  2270. 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);
  2271. // 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);
  2272. // 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);
  2273. rt_kprintf("\n\n");
  2274. once_print = 1;
  2275. }
  2276. // 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2277. // 当前时间戳小于第一段开始时间戳
  2278. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2279. {
  2280. if (chk_log_time)
  2281. {
  2282. uint32_t time_cache = cul_sh1_stamp.tm_hour * 3600 + cul_sh1_stamp.tm_min * 60 + cul_sh1_stamp.tm_sec;
  2283. uint32_t time_now = now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec;
  2284. uint32_t time_remainder = time_cache - time_now;
  2285. uint8_t hour = time_remainder / 3600;
  2286. uint8_t min = (time_remainder % 3600) / 60;
  2287. uint8_t sec = time_remainder % 60;
  2288. LOG_I(" ====== 未到收集时间 A :%04d-%02d-%02d %02d:%02d:%02d 剩余:%02d:%02d:%02d ====== run_sta %d",
  2289. cul_sh1_stamp.tm_year + 1900,
  2290. cul_sh1_stamp.tm_mon + 1,
  2291. cul_sh1_stamp.tm_mday,
  2292. cul_sh1_stamp.tm_hour,
  2293. cul_sh1_stamp.tm_min,
  2294. cul_sh1_stamp.tm_sec,
  2295. hour,
  2296. min,
  2297. sec,
  2298. device_status.run_sta);
  2299. }
  2300. time_period = 0;
  2301. }
  2302. // 当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2303. if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2304. {
  2305. if (chk_log_time)
  2306. {
  2307. uint32_t time_cache = cul_sh2_stamp.tm_hour * 3600 + cul_sh2_stamp.tm_min * 60 + cul_sh2_stamp.tm_sec;
  2308. uint32_t time_now = now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec;
  2309. uint32_t time_remainder = time_cache - time_now;
  2310. uint8_t hour = time_remainder / 3600;
  2311. uint8_t min = (time_remainder % 3600) / 60;
  2312. uint8_t sec = time_remainder % 60;
  2313. LOG_I(" ====== 未到收集时间 B :%04d-%02d-%02d %02d:%02d:%02d 剩余:%02d:%02d:%02d ====== run_sta %d",
  2314. cul_sh2_stamp.tm_year + 1900,
  2315. cul_sh2_stamp.tm_mon + 1,
  2316. cul_sh2_stamp.tm_mday,
  2317. cul_sh2_stamp.tm_hour,
  2318. cul_sh2_stamp.tm_min,
  2319. cul_sh2_stamp.tm_sec,
  2320. hour,
  2321. min,
  2322. sec,
  2323. device_status.run_sta);
  2324. }
  2325. time_period = 0;
  2326. }
  2327. // 如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2328. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) < 0)
  2329. {
  2330. time_period = 1;
  2331. }
  2332. else if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2333. {
  2334. time_period = 2;
  2335. }
  2336. else if (difftime(mktime(&now_stamp), mktime(&cul_sh3_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh3_stamp)) < 0)
  2337. {
  2338. time_period = 3;
  2339. }
  2340. // if (chk_log_time)
  2341. // rt_kprintf("time_period > %d %X %X\n", time_period, difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)), difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)));
  2342. return time_period;
  2343. }
  2344. /**
  2345. * @brief
  2346. *
  2347. * @param mot 电机选择
  2348. * @param limit1 限位1选择
  2349. * @param limit2 限位2选择(不需要的话填0)
  2350. * @param mct 电机操作选择
  2351. * @return Res_def 0-成功
  2352. */
  2353. Res_def Mtor_Control(Motor_type mot, Limit_type limit1, Limit_type limit2, Motor_Ctl_type mct, uint16_t timeout)
  2354. {
  2355. uint8_t res = res_OK;
  2356. uint16_t Motor_L, Motor_R, Lim1, Lim2 = LIMIT_0;
  2357. uint16_t wait_out = 0;
  2358. uint8_t Check_EN = 0;
  2359. if (JD_Check_Test_Sta(tt_auto) == res_OK)
  2360. {
  2361. Check_EN = 2;
  2362. }
  2363. switch (mot) // 电机选型
  2364. {
  2365. case mot_ZP:
  2366. Motor_L = TURN_ZP_L;
  2367. Motor_R = TURN_ZP_R;
  2368. break;
  2369. case mot_CM:
  2370. Motor_L = TURN_CM_L;
  2371. Motor_R = TURN_CM_R;
  2372. break;
  2373. default:
  2374. LOG_W("illegal Motor_type %d", mot);
  2375. return res_Verifi;
  2376. break;
  2377. }
  2378. switch (limit1) // 限位选型1
  2379. {
  2380. case lt_Angle_0:
  2381. Lim1 = LIMIT_0;
  2382. break;
  2383. case lt_Angle_90:
  2384. Lim1 = LIMIT_90;
  2385. break;
  2386. default:
  2387. LOG_W("illegal limit1_type %d", limit1);
  2388. return res_Verifi;
  2389. break;
  2390. }
  2391. if (limit2 != 0)
  2392. {
  2393. switch (limit2) // 限位选型2
  2394. {
  2395. case lt_Angle_0:
  2396. Lim2 = LIMIT_0;
  2397. rt_pin_mode(Lim2, PIN_MODE_INPUT);
  2398. break;
  2399. case lt_Angle_90:
  2400. Lim2 = LIMIT_90;
  2401. rt_pin_mode(Lim2, PIN_MODE_INPUT);
  2402. break;
  2403. default:
  2404. LOG_W("illegal limit2_type %d", limit2);
  2405. return res_Verifi;
  2406. break;
  2407. }
  2408. }
  2409. rt_pin_mode(Motor_L, PIN_MODE_OUTPUT);
  2410. rt_pin_mode(Motor_R, PIN_MODE_OUTPUT);
  2411. rt_pin_mode(Lim1, PIN_MODE_INPUT);
  2412. // 走出限位 天牛没有限位 不需要走出了
  2413. // while ((rt_pin_read(Lim1) == 0) && (wait_out * 1000 < timeout))
  2414. // while ((wait_out * 1000 < timeout))
  2415. // {
  2416. // switch (mct)
  2417. // {
  2418. // case mct_Foreward:
  2419. // rt_pin_write(Motor_R, ON);
  2420. // rt_pin_write(Motor_L, OFF);
  2421. // break;
  2422. // case mct_Reversal:
  2423. // rt_pin_write(Motor_R, OFF);
  2424. // rt_pin_write(Motor_L, ON);
  2425. // break;
  2426. // case mct_Stop:
  2427. // rt_pin_write(Motor_R, OFF);
  2428. // rt_pin_write(Motor_L, OFF);
  2429. // break;
  2430. // default:
  2431. // LOG_W("illegal Motor_Ctl_type %d", mct);
  2432. // return res_Verifi;
  2433. // break;
  2434. // }
  2435. // wait_out++;
  2436. // rt_thread_mdelay(1000);
  2437. // rt_kprintf("wait exit limit %d/%d \n", wait_out, timeout / 1000);
  2438. // }
  2439. // 参数校验完成 开始操作
  2440. switch (mct)
  2441. {
  2442. case mct_Foreward:
  2443. rt_pin_write(Motor_R, ON);
  2444. rt_pin_write(Motor_L, OFF);
  2445. break;
  2446. case mct_Reversal:
  2447. rt_pin_write(Motor_R, OFF);
  2448. rt_pin_write(Motor_L, ON);
  2449. break;
  2450. case mct_Stop:
  2451. rt_pin_write(Motor_R, OFF);
  2452. rt_pin_write(Motor_L, OFF);
  2453. break;
  2454. default:
  2455. LOG_W("illegal Motor_Ctl_type %d", mct);
  2456. return res_Verifi;
  2457. break;
  2458. }
  2459. if (mct == mct_Stop)
  2460. {
  2461. rt_thread_mdelay(10);
  2462. return res;
  2463. }
  2464. res = res_Timeout;
  2465. while ((wait_out * 100 < timeout) && (limit2 == 0))
  2466. {
  2467. switch (mct)
  2468. {
  2469. case mct_Foreward:
  2470. rt_pin_write(Motor_R, ON);
  2471. rt_pin_write(Motor_L, OFF);
  2472. break;
  2473. case mct_Reversal:
  2474. rt_pin_write(Motor_R, OFF);
  2475. rt_pin_write(Motor_L, ON);
  2476. break;
  2477. case mct_Stop:
  2478. rt_pin_write(Motor_R, OFF);
  2479. rt_pin_write(Motor_L, OFF);
  2480. break;
  2481. default:
  2482. LOG_W("illegal Motor_Ctl_type %d", mct);
  2483. return res_Verifi;
  2484. break;
  2485. }
  2486. // if (rt_pin_read(Lim1) == 0)
  2487. if (0)
  2488. {
  2489. // LOG_W("lim1 TIG");
  2490. res = res_OK;
  2491. break;
  2492. }
  2493. wait_out++;
  2494. rt_thread_mdelay(100);
  2495. if ((Check_EN == 2) && (JD_Check_Test_Sta(tt_auto) != res_OK))
  2496. {
  2497. rt_kprintf("tt_tape Break 2\n");
  2498. break;
  2499. }
  2500. }
  2501. while ((wait_out * 100 < timeout) && (limit2 != 0))
  2502. {
  2503. switch (mct)
  2504. {
  2505. case mct_Foreward:
  2506. rt_pin_write(Motor_R, ON);
  2507. rt_pin_write(Motor_L, OFF);
  2508. break;
  2509. case mct_Reversal:
  2510. rt_pin_write(Motor_R, OFF);
  2511. rt_pin_write(Motor_L, ON);
  2512. break;
  2513. case mct_Stop:
  2514. rt_pin_write(Motor_R, OFF);
  2515. rt_pin_write(Motor_L, OFF);
  2516. break;
  2517. default:
  2518. LOG_W("illegal Motor_Ctl_type %d", mct);
  2519. return res_Verifi;
  2520. break;
  2521. }
  2522. // if (rt_pin_read(Lim1) == 0 || rt_pin_read(Lim2) == 0)
  2523. if (0)
  2524. {
  2525. // LOG_W("lim1 or 2 TIG");
  2526. res = res_OK;
  2527. break;
  2528. }
  2529. wait_out++;
  2530. rt_thread_mdelay(100);
  2531. if ((Check_EN == 2) && (JD_Check_Test_Sta(tt_auto) != res_OK))
  2532. {
  2533. rt_kprintf("tt_tape Break 2\n");
  2534. break;
  2535. }
  2536. }
  2537. rt_pin_write(Motor_R, OFF);
  2538. rt_pin_write(Motor_L, OFF);
  2539. if (wait_out * 100 >= timeout && Check_EN == 0)
  2540. {
  2541. // Report_Limit_Error(le_limit_tape);
  2542. // rt_kprintf("胶带限位失效啦\n");
  2543. }
  2544. return res;
  2545. }
  2546. /**
  2547. * @brief 控制转盘电机
  2548. *
  2549. * @param mct 控制操作
  2550. * @return Res_def 执行结果
  2551. */
  2552. Res_def Mtor_Control_ZP(Motor_Ctl_type mct)
  2553. {
  2554. static char free = OFF;
  2555. static Motor_Ctl_type Mtor_sta = mct_Foreward;
  2556. char cnt = 0;
  2557. while (cnt < 30 && free == ON)
  2558. {
  2559. rt_kprintf("waitting for Mtor_Control_ZP free %d\n", cnt);
  2560. rt_thread_mdelay(1000);
  2561. cnt++;
  2562. }
  2563. free = ON;
  2564. if (mct != mct_auto)
  2565. {
  2566. Mtor_sta = mct;
  2567. }
  2568. else
  2569. {
  2570. if (Mtor_sta == mct_Foreward)
  2571. {
  2572. Mtor_sta = mct_Reversal;
  2573. }
  2574. else
  2575. {
  2576. Mtor_sta = mct_Foreward;
  2577. }
  2578. }
  2579. if (Mtor_sta == mct_Foreward)
  2580. {
  2581. LOG_I("Mtor_Control_ZP Start mct_Foreward\n");
  2582. }
  2583. else
  2584. {
  2585. LOG_I("Mtor_Control_ZP Start mct_Reversal\n");
  2586. }
  2587. uint8_t res = Mtor_Control(mot_ZP, lt_Angle_0, 0, Mtor_sta, 15000);
  2588. LOG_I("Mtor_Control Finsh %d - %d\n", mct, res);
  2589. free = OFF;
  2590. return res;
  2591. }
  2592. /**
  2593. * @brief 控制仓门电机
  2594. *
  2595. * @param mct 控制操作
  2596. * @return Res_def 执行结果
  2597. */
  2598. Res_def Mtor_Control_CM(Motor_Ctl_type mct)
  2599. {
  2600. LOG_I("Mtor_Control_CM Start\n");
  2601. uint8_t res = Mtor_Control(mot_CM, lt_Angle_0, 0, mct, 27000);
  2602. LOG_I("Mtor_Control Finsh %d - %d\n", mct, res);
  2603. return res;
  2604. }
  2605. /**
  2606. * @brief 获取当前时间
  2607. *
  2608. * @param now_stamp 存放时间戳的指针
  2609. */
  2610. void Get_now_time(struct tm *now_stamp)
  2611. {
  2612. time_t now;
  2613. time(&now);
  2614. *now_stamp = *localtime(&now);
  2615. }
  2616. /**
  2617. * @brief 检查是否处于工作时间段
  2618. *
  2619. * @param coll_ch 时间段通道
  2620. * @return Temporal_list 检查结果
  2621. */
  2622. Temporal_list Check_Work_time_period(uint8_t coll_ch)
  2623. {
  2624. static struct tm now_stamp = {0}; // 现在
  2625. static struct tm cul_s_stamp = {0}; // 开始
  2626. static struct tm cul_e_stamp = {0}; // 结束
  2627. Get_now_time(&now_stamp);
  2628. Get_now_time(&cul_s_stamp);
  2629. Get_now_time(&cul_e_stamp);
  2630. switch (coll_ch)
  2631. {
  2632. case 1:
  2633. cul_s_stamp.tm_hour = device_status.collecting_sh1;
  2634. cul_s_stamp.tm_min = 0 + test_min;
  2635. cul_s_stamp.tm_sec = 0;
  2636. cul_e_stamp.tm_hour = device_status.collecting_eh1;
  2637. cul_e_stamp.tm_min = 0 + test_min;
  2638. cul_e_stamp.tm_sec = 0;
  2639. break;
  2640. case 2:
  2641. cul_s_stamp.tm_hour = device_status.collecting_sh2;
  2642. cul_s_stamp.tm_min = 0 + test_min;
  2643. cul_s_stamp.tm_sec = 0;
  2644. cul_e_stamp.tm_hour = device_status.collecting_eh2;
  2645. cul_e_stamp.tm_min = 0 + test_min;
  2646. cul_e_stamp.tm_sec = 0;
  2647. // rt_kprintf("cul_s_stamp %d cul_e_stamp %d\n", cul_s_stamp.tm_hour, cul_e_stamp.tm_hour);
  2648. if (cul_s_stamp.tm_hour == cul_e_stamp.tm_hour) // 检查第二时间段是否有效
  2649. {
  2650. return tl_Error;
  2651. }
  2652. break;
  2653. default:
  2654. return tl_Verifi;
  2655. break;
  2656. }
  2657. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) >= 0 && // 大于开始时间
  2658. difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) < 0) // 小于结束时间
  2659. {
  2660. return tl_Center;
  2661. }
  2662. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) < 0) // 小于开始时间
  2663. {
  2664. return tl_Front;
  2665. }
  2666. if (difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) >= 0) // 大于结束时间
  2667. {
  2668. // rt_kprintf("now %02d:%02d:%02d start %02d:%02d:%02d end %02d:%02d:%02d\n" ,
  2669. // now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec,
  2670. // cul_s_stamp.tm_hour,cul_s_stamp.tm_min,cul_s_stamp.tm_sec,
  2671. // cul_e_stamp.tm_hour,cul_e_stamp.tm_min,cul_e_stamp.tm_sec
  2672. // );
  2673. return tl_Back;
  2674. }
  2675. return tl_Verifi;
  2676. }
  2677. /**
  2678. * @brief 检查是否处于工作时间段 跨0点模式
  2679. *
  2680. * @param coll_ch 时间段通道
  2681. * @return Temporal_list 检查结果
  2682. */
  2683. Temporal_list Check_Work_time_period_0oclock(void)
  2684. {
  2685. static struct tm now_stamp = {0}; // 现在
  2686. static struct tm cul_s_stamp = {0}; // 开始
  2687. static struct tm cul_e_stamp = {0}; // 结束
  2688. Get_now_time(&now_stamp);
  2689. Get_now_time(&cul_s_stamp);
  2690. Get_now_time(&cul_e_stamp);
  2691. /** STEP1:get cheange value
  2692. *
  2693. */
  2694. uint8_t cv_start = 0, cv_end = 0;
  2695. cv_start = device_status.collecting_sh1 + 24;
  2696. cv_end = device_status.collecting_eh1 + 48;
  2697. /** STEP2: make zero
  2698. *
  2699. */
  2700. cv_end -= cv_start;
  2701. /** SETP3: get Dvalue
  2702. *
  2703. */
  2704. if (now_stamp.tm_hour >= device_status.collecting_eh1 && now_stamp.tm_hour < device_status.collecting_sh1) // not yet
  2705. {
  2706. // rt_kprintf("未到定时区间\n");
  2707. return tl_Front;
  2708. }
  2709. else if (now_stamp.tm_hour >= device_status.collecting_sh1 && now_stamp.tm_hour < 24) // Phase 1
  2710. {
  2711. // rt_kprintf("阶段1\n");
  2712. now_stamp.tm_hour += 24;
  2713. now_stamp.tm_hour -= cv_start;
  2714. }
  2715. else
  2716. {
  2717. // rt_kprintf("阶段2\n");
  2718. now_stamp.tm_hour += 48;
  2719. now_stamp.tm_hour -= cv_start;
  2720. }
  2721. // rt_kprintf("当前时间为 %d 设定时间为 0 ~ %d\n" ,now_stamp.tm_hour ,cv_end);
  2722. cul_s_stamp.tm_hour = 0;
  2723. cul_s_stamp.tm_min = 0 + test_min;
  2724. cul_s_stamp.tm_sec = 0;
  2725. cul_e_stamp.tm_hour = cv_end;
  2726. cul_e_stamp.tm_min = 0 + test_min;
  2727. cul_e_stamp.tm_sec = 0;
  2728. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) >= 0 && // 大于开始时间
  2729. difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) < 0) // 小于结束时间
  2730. {
  2731. return tl_Center;
  2732. }
  2733. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) < 0) // 小于开始时间
  2734. {
  2735. return tl_Front;
  2736. }
  2737. if (difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) >= 0) // 大于结束时间
  2738. {
  2739. // rt_kprintf("now %02d:%02d:%02d start %02d:%02d:%02d end %02d:%02d:%02d\n" ,
  2740. // now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec,
  2741. // cul_s_stamp.tm_hour,cul_s_stamp.tm_min,cul_s_stamp.tm_sec,
  2742. // cul_e_stamp.tm_hour,cul_e_stamp.tm_min,cul_e_stamp.tm_sec
  2743. // );
  2744. return tl_Back;
  2745. }
  2746. return tl_Verifi;
  2747. }
  2748. /**
  2749. * @brief 写工作运行状态
  2750. *
  2751. * @param sta 要写入的值
  2752. */
  2753. static void Set_run_sta(run_sta_list sta)
  2754. {
  2755. device_status.run_sta = sta;
  2756. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  2757. uint8_t check = 0;
  2758. ef_get_env_blob("run_sta", &check, sizeof(device_status.run_sta), NULL);
  2759. if (check != sta)
  2760. {
  2761. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  2762. }
  2763. rt_kprintf("Set_run_sta > %d \n", check);
  2764. }
  2765. void Set_runsta_reset(void)
  2766. {
  2767. Recode_e_code(e_set_rs_RESET, 1, "工作流程被复位");
  2768. Set_run_sta(rs_RESET);
  2769. }
  2770. MSH_CMD_EXPORT(Set_runsta_reset, Set_runsta_reset);
  2771. /**
  2772. * @brief 读工作运行状态
  2773. *
  2774. * @return run_sta_list 读出的值
  2775. */
  2776. static run_sta_list Get_run_sta(void)
  2777. {
  2778. ef_get_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta), NULL);
  2779. return device_status.run_sta;
  2780. }
  2781. /**
  2782. * @brief 获取剩余工作时间
  2783. *
  2784. * @param coll_ch 工作时间段通道号
  2785. * @return uint8_t 百分比的值
  2786. */
  2787. static int8_t Get_remainder_time(uint8_t coll_ch)
  2788. {
  2789. static struct tm now_stamp = {0}; // 现在
  2790. static struct tm cul_s_stamp = {0}; // 开始
  2791. static struct tm cul_e_stamp = {0}; // 结束
  2792. uint32_t dividend = 0, divisor = 0, quotient = 0;
  2793. int8_t result = 0;
  2794. Get_now_time(&now_stamp);
  2795. Get_now_time(&cul_s_stamp);
  2796. Get_now_time(&cul_e_stamp);
  2797. switch (coll_ch)
  2798. {
  2799. case 1:
  2800. cul_s_stamp.tm_hour = device_status.collecting_sh1;
  2801. cul_s_stamp.tm_min = 0 + test_min;
  2802. cul_s_stamp.tm_sec = 0;
  2803. cul_e_stamp.tm_hour = device_status.collecting_eh1;
  2804. cul_e_stamp.tm_min = 0 + test_min;
  2805. cul_e_stamp.tm_sec = 0;
  2806. break;
  2807. case 2:
  2808. cul_s_stamp.tm_hour = device_status.collecting_sh2;
  2809. cul_s_stamp.tm_min = 0 + test_min;
  2810. cul_s_stamp.tm_sec = 0;
  2811. cul_e_stamp.tm_hour = device_status.collecting_eh2;
  2812. cul_e_stamp.tm_min = 0 + test_min;
  2813. cul_e_stamp.tm_sec = 0;
  2814. if (cul_s_stamp.tm_hour == cul_e_stamp.tm_hour) // 检查第二时间段是否有效
  2815. {
  2816. return -1;
  2817. }
  2818. break;
  2819. default:
  2820. return -1;
  2821. break;
  2822. }
  2823. dividend = (cul_e_stamp.tm_hour * 3600 + cul_e_stamp.tm_min * 60 + cul_e_stamp.tm_sec) - (cul_s_stamp.tm_hour * 3600 + cul_s_stamp.tm_min * 60 + cul_s_stamp.tm_sec);
  2824. divisor = (cul_e_stamp.tm_hour * 3600 + cul_e_stamp.tm_min * 60 + cul_e_stamp.tm_sec) - (now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec);
  2825. divisor *= 100;
  2826. quotient = divisor / dividend;
  2827. result = abs(100 - quotient);
  2828. return result;
  2829. }
  2830. /*********************↓ 胶带结构的孢子仪主工作流程 ↓************************/
  2831. /**
  2832. * @brief 检查工作事件
  2833. *
  2834. * @return Temporal_list
  2835. */
  2836. Temporal_list Check_WorkDay(void)
  2837. {
  2838. work_day_def work_day = {0};
  2839. if (ef_get_env_blob("work_day", &work_day, sizeof(work_day), NULL) == 0)
  2840. {
  2841. return res_Verifi;
  2842. }
  2843. else if (work_day.year == 0 && work_day.month == 0 && work_day.day == 0)
  2844. {
  2845. // rt_kprintf("work_day %d.%d.%d \n", work_day.year, work_day.month, work_day.day);
  2846. return tl_Error;
  2847. }
  2848. static struct tm now_stamp = {0}; // 现在
  2849. Get_now_time(&now_stamp);
  2850. now_stamp.tm_mon += 1;
  2851. // 时间已过期
  2852. if (work_day.year > now_stamp.tm_year)
  2853. {
  2854. return tl_Back;
  2855. }
  2856. else if (work_day.month > now_stamp.tm_mon)
  2857. {
  2858. return tl_Back;
  2859. }
  2860. else if (work_day.day > now_stamp.tm_mday)
  2861. {
  2862. return tl_Back;
  2863. }
  2864. if (work_day.year == now_stamp.tm_year &&
  2865. work_day.month == now_stamp.tm_mon &&
  2866. work_day.day == now_stamp.tm_mday)
  2867. {
  2868. return tl_Center;
  2869. }
  2870. if (work_day.year < now_stamp.tm_year)
  2871. {
  2872. return tl_Front;
  2873. }
  2874. else if (work_day.month < now_stamp.tm_mon)
  2875. {
  2876. return tl_Front;
  2877. }
  2878. else if (work_day.day < now_stamp.tm_mday)
  2879. {
  2880. return tl_Front;
  2881. }
  2882. return res_Verifi;
  2883. }
  2884. void Recode_WorkDay(void)
  2885. {
  2886. work_day_def work_day = {0};
  2887. static struct tm now_stamp = {0}; // 现在
  2888. Get_now_time(&now_stamp);
  2889. work_day.year = now_stamp.tm_year;
  2890. work_day.month = now_stamp.tm_mon + 1;
  2891. work_day.day = now_stamp.tm_mday;
  2892. ef_set_env_blob("work_day", &work_day, sizeof(work_day));
  2893. LOG_E("Recode_WorkDay > %d.%02d.%02d\n", work_day.year % 100, work_day.month, work_day.day);
  2894. char buf[100] = {0};
  2895. sprintf(buf, "今日(%d)工作结束", now_stamp.tm_mday);
  2896. Recode_e_code(e_ReWorkDay, now_stamp.tm_mday, buf); // 工作结束
  2897. }
  2898. MSH_CMD_EXPORT(Recode_WorkDay, Recode_WorkDay);
  2899. void Clear_WorkDay(void)
  2900. {
  2901. work_day_def work_day = {0};
  2902. rt_kprintf("WorkDay SET %d \n", ef_set_env_blob("work_day", &work_day, sizeof(work_day)));
  2903. }
  2904. uint8_t ws_stop_waitA = 0;
  2905. uint8_t ws_stop_waitB = 0;
  2906. /*********************↑ 胶带结构的孢子仪主工作流程 ↑************************/
  2907. /**
  2908. * @brief 孢子仪工作线程V2
  2909. *
  2910. */
  2911. void WorkTask_BZYV2(void)
  2912. {
  2913. // 打开采集风机
  2914. ctr_Collect_Fan(ON);
  2915. // 打开胶带步进电机
  2916. // 等待采集结束
  2917. // 拍照
  2918. // 打开相机供电
  2919. // 打开补光灯
  2920. // 滑台归位
  2921. // 滑台启动
  2922. }
  2923. // 工作任务流程s
  2924. void WorkTask(void)
  2925. {
  2926. static int clt_time = 0; // 收集时间差秒数
  2927. static int heat_time = 0;
  2928. uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  2929. static int16_t log_en_cnt = -1;
  2930. if (chk_log_time)
  2931. {
  2932. LOG_I("工作状态:%d", device_status.work_status);
  2933. LOG_I("开关机状态:%d", device_status.ds);
  2934. }
  2935. 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)
  2936. {
  2937. device_status.work_done = 0;
  2938. if (clt_time == 0)
  2939. {
  2940. #if 0
  2941. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2942. {
  2943. LOG_W("相机电流不足!");
  2944. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2945. DTU_Write("%s",ctr_hk_p_warn);
  2946. rt_free(ctr_hk_p_warn);
  2947. }
  2948. #endif
  2949. #if LAMP_EN == 0
  2950. ctr_Fan_PUT(ON);
  2951. #endif
  2952. DTU_Write("workstart.mp3");
  2953. }
  2954. // 1.打开灯管
  2955. #if LAMP_EN == 1
  2956. if (device_status.lamp_status == 1)
  2957. {
  2958. ctr_lamp(ON, 1);
  2959. ctr_lamp(OFF, 2);
  2960. ctr_lamp(OFF, 3);
  2961. }
  2962. else if (device_status.lamp1_status == 1)
  2963. {
  2964. ctr_lamp(OFF, 1);
  2965. ctr_lamp(ON, 2);
  2966. ctr_lamp(OFF, 3);
  2967. }
  2968. else if (device_status.lamp2_status == 1)
  2969. {
  2970. ctr_lamp(OFF, 1);
  2971. ctr_lamp(OFF, 2);
  2972. ctr_lamp(ON, 3);
  2973. }
  2974. #else
  2975. ctr_lamp(ON, 1);
  2976. #endif
  2977. LOG_I("打开灯管!!!");
  2978. #if 0
  2979. rt_thread_delay(2000);
  2980. if(atoi(device_status.power)<450)
  2981. {
  2982. LOG_I("Current is:%d",atoi(device_status.power));
  2983. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  2984. {
  2985. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  2986. DTU_Write("%s",lamp_err_warn);
  2987. rt_free(lamp_err_warn);
  2988. }
  2989. }
  2990. #endif
  2991. if (device_status.version_type != 5)
  2992. {
  2993. if (clt_time == 0)
  2994. {
  2995. rt_thread_delay(5000); // 待灯管启动再往下执行,否则会触发电机过流
  2996. // 2.打开上仓门
  2997. rt_kprintf("打开上仓门!!!\n");
  2998. device_status.ctr_up_motor = 1;
  2999. }
  3000. // 3.收集
  3001. clt_time = device_status.clt_time * 60;
  3002. while (clt_time != 1)
  3003. {
  3004. #if LAMP_EN == 0
  3005. ctr_Fan_IN(ON);
  3006. #endif
  3007. clt_time--;
  3008. rt_thread_delay(1000);
  3009. #if LAMP_EN == 0
  3010. if (clt_time % 30 == 0)
  3011. ctr_Fan_PUT(OFF);
  3012. #endif
  3013. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n", device_status.clt_time, clt_time);
  3014. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"", device_status.clt_time, clt_time);
  3015. // 如果触发异常
  3016. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  3017. {
  3018. ctr_lamp(OFF, 1);
  3019. #if LAMP_EN == 1
  3020. ctr_lamp(OFF, 2);
  3021. ctr_lamp(OFF, 3);
  3022. #endif
  3023. #if LAMP_EN == 0
  3024. ctr_Fan_IN(OFF);
  3025. #endif
  3026. rt_kprintf("异常退出!!!\n");
  3027. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3028. break;
  3029. }
  3030. if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  3031. {
  3032. ctr_lamp(OFF, 1);
  3033. #if LAMP_EN == 1
  3034. ctr_lamp(OFF, 2);
  3035. ctr_lamp(OFF, 3);
  3036. #endif
  3037. #if LAMP_EN == 0
  3038. ctr_Fan_IN(OFF);
  3039. #endif
  3040. rt_kprintf("异常退出!!!\n");
  3041. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3042. break;
  3043. }
  3044. }
  3045. clt_time = 1;
  3046. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  3047. {
  3048. ctr_lamp(OFF, 1);
  3049. #if LAMP_EN == 1
  3050. ctr_lamp(OFF, 2);
  3051. ctr_lamp(OFF, 3);
  3052. #endif
  3053. #if LAMP_EN == 0
  3054. ctr_Fan_IN(OFF);
  3055. #endif
  3056. rt_kprintf("异常退出!!!\n");
  3057. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3058. break;
  3059. }
  3060. if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  3061. {
  3062. ctr_lamp(OFF, 1);
  3063. #if LAMP_EN == 1
  3064. ctr_lamp(OFF, 2);
  3065. ctr_lamp(OFF, 3);
  3066. #endif
  3067. #if LAMP_EN == 0
  3068. ctr_Fan_IN(OFF);
  3069. #endif
  3070. rt_kprintf("异常退出!!!\n");
  3071. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3072. break;
  3073. }
  3074. #if LAMP_EN == 0
  3075. ctr_Fan_IN(OFF);
  3076. #endif
  3077. // 4.关闭上仓门
  3078. device_status.ctr_up_motor = 2;
  3079. rt_kprintf("关闭上仓门!!!\n");
  3080. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  3081. while (device_status.ctr_up_motor != 0)
  3082. {
  3083. rt_thread_delay(100);
  3084. LOG_I("等待上仓门关闭!!!");
  3085. }
  3086. // 5.加热
  3087. if (device_status.heat_sw == 0)
  3088. {
  3089. calorstat(device_status.heattime * 60);
  3090. }
  3091. else
  3092. {
  3093. heat_time = device_status.heattime * 60;
  3094. while (heat_time != 0)
  3095. {
  3096. heat_time--;
  3097. LOG_I("正在加热状态!!!");
  3098. rt_thread_delay(1000);
  3099. }
  3100. }
  3101. // 6.打开下仓门
  3102. if (device_status.rain_status == 0)
  3103. {
  3104. device_status.ctr_down_motor = 1;
  3105. }
  3106. else
  3107. {
  3108. device_status.ctr_down_motor = 2;
  3109. device_status.ctr_up_motor = 2;
  3110. }
  3111. // 待下仓门完全打开
  3112. while (device_status.ctr_down_motor != 0)
  3113. {
  3114. rt_thread_delay(100);
  3115. rt_kprintf("等待下仓门打开!!!\n");
  3116. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  3117. }
  3118. rt_thread_delay(3000);
  3119. // 关闭下仓门
  3120. device_status.ctr_down_motor = 2;
  3121. while (device_status.ctr_down_motor != 0)
  3122. {
  3123. rt_thread_delay(100);
  3124. rt_kprintf("等待下仓门关闭!!!\n");
  3125. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  3126. }
  3127. // 2022-04-12add:
  3128. LOG_I("打开上仓门!!!");
  3129. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  3130. device_status.ctr_up_motor = 1;
  3131. }
  3132. else
  3133. {
  3134. // 打开高压
  3135. ctr_Heat(ON);
  3136. // 打开补光
  3137. ctr_led(ON);
  3138. LOG_I("打开补光!!");
  3139. rt_pin_mode(CLEAR_L_LIMIT, PIN_MODE_INPUT);
  3140. // 2.翻转清虫
  3141. while (rt_pin_read(CLEAR_L_LIMIT) == 1)
  3142. rt_thread_delay(20);
  3143. rt_thread_delay(500);
  3144. Hk_Take_io(); // 触发海康相机拍照
  3145. rt_thread_delay(500);
  3146. ctr_turnv5(); // 摆动清虫
  3147. LOG_I("摆动清虫!");
  3148. }
  3149. }
  3150. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1 || device_status.ds == 0)
  3151. {
  3152. if (chk_log_time)
  3153. {
  3154. rt_kprintf("-------------------not work--------------------\n");
  3155. }
  3156. device_status.work_status = 0;
  3157. }
  3158. // 工作结束
  3159. if (device_status.work_done == 0 && device_status.work_status == 0)
  3160. {
  3161. DTU_Write("workdone");
  3162. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  3163. ResetInit();
  3164. }
  3165. if (chk_log_time)
  3166. {
  3167. rt_kprintf(" ***not work-device status:%d\n", device_status.ds);
  3168. }
  3169. }
  3170. /* 入口函数-工作状态检测 */
  3171. /**
  3172. * @brief 工作状态检测线程服务函数
  3173. *
  3174. * @param parameter -
  3175. */
  3176. static void Work_status_thread_entry(void *parameter)
  3177. {
  3178. static uint16_t sun_status_num = 120;
  3179. // 如果是光控定时,进行光控监测的消抖
  3180. if (device_status.timeSwitch == 0 && device_status.sun_status == 0)
  3181. {
  3182. rt_kprintf("光控定时!!!\n");
  3183. while (sun_status_num != 0)
  3184. {
  3185. sun_status_num--;
  3186. rt_thread_delay(1000);
  3187. rt_kprintf("开机光控检测:%d\n", sun_status_num);
  3188. }
  3189. device_status.light_over_tim = device_status.sunBar * 3600;
  3190. device_status.test_flag = 0;
  3191. }
  3192. while (1)
  3193. {
  3194. WorkCheck();
  3195. }
  3196. }
  3197. extern int RayDualTriggerInit(void);
  3198. extern void KeyThreadStart(void);
  3199. /*入口函数-工作流程*/
  3200. /**
  3201. * @brief 工作流程线程服务函数
  3202. *
  3203. * @param parameter -
  3204. */
  3205. static void Work_Loop_thread_entry(void *parameter)
  3206. {
  3207. // uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  3208. // static int16_t log_en_cnt = -1;
  3209. rt_thread_delay(1000);
  3210. ResetInit();
  3211. RayDualTriggerInit();
  3212. // Mtor_Control_ZP(mct_Reversal); // 开始工作前给翻版复位 250910 修改开机后不再主动进行翻板复位
  3213. // Mtor_Control_ZP(mct_Foreward); // 开始工作前给翻版复位
  3214. if (device_status.rain_status == 0)
  3215. Mtor_Control_CM(mct_Reversal); // 开始工作前给仓门复位
  3216. // rt_kprintf(" dtype = %d vtype = %d \n\n", device_status.dtype, device_status.version_type);
  3217. // rt_kprintf("Version : %s %s\n", VERSION_BZY , device_status.driver_compile);
  3218. UpLoad_Data(0);
  3219. while (1)
  3220. {
  3221. rt_thread_mdelay(1000);
  3222. WorkTask_TN();
  3223. }
  3224. }
  3225. /*入口函数清扫*/
  3226. /**
  3227. * @brief 控制清扫线程服务函数
  3228. *
  3229. * @param parameter -
  3230. */
  3231. static void Control_Clear_thread_entry(void *parameter)
  3232. {
  3233. while (1)
  3234. {
  3235. #if ROBOT_EN == 1
  3236. ctr_Motor_Robot(ON);
  3237. rt_thread_delay(100);
  3238. #else
  3239. #if Color_EN == 1
  3240. /*复位*/
  3241. if (Read_Color_Key(KEY_RESET) == 1)
  3242. {
  3243. ctr_Color_board(RESET, 1200);
  3244. rt_thread_delay(5000);
  3245. }
  3246. #else
  3247. if (device_status.clear_status == 1)
  3248. {
  3249. if (ctr_clear(1200) == 0)
  3250. ctr_TG_Clear(400);
  3251. }
  3252. rt_thread_delay(100);
  3253. #endif
  3254. #endif
  3255. }
  3256. }
  3257. /*入口函数加热*/
  3258. /**
  3259. * @brief 控制加热线程服务函数
  3260. *
  3261. * @param parameter -
  3262. */
  3263. static void Control_Heat_thread_entry(void *parameter)
  3264. {
  3265. uint16_t var_clear = 0;
  3266. #if ROBOT_EN == 1
  3267. static uint8_t key_on = 0;
  3268. #else
  3269. #if Color_EN == 0
  3270. while (device_status.work_status == 0)
  3271. {
  3272. rt_thread_delay(1000);
  3273. }
  3274. #endif
  3275. #endif
  3276. while (1)
  3277. {
  3278. #if ROBOT_EN == 1
  3279. // 磁悬浮供电
  3280. // 监测磁悬浮限位位置`
  3281. LOG_D("Read 磁悬浮:%d", device_status.heattem);
  3282. // 等待10S
  3283. if (device_status.heattem < 30 && Read_Real_Voltage() / 100 > 12) // 磁悬浮处于关闭状态
  3284. {
  3285. key_on = 0;
  3286. // 发送触摸按钮信号
  3287. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  3288. rt_pin_write(EMS_Dir, 0);
  3289. rt_thread_delay(50);
  3290. rt_pin_write(EMS_Dir, 1);
  3291. }
  3292. else if (device_status.heattem < 30 && Read_Real_Voltage() / 100 < 12)
  3293. {
  3294. ctr_router(OFF);
  3295. }
  3296. if (device_status.heattem > 40)
  3297. {
  3298. key_on = 1;
  3299. rt_thread_delay(1000);
  3300. LOG_D("Read 磁悬浮已打开:%d", device_status.heattem);
  3301. }
  3302. // 磁悬浮供电
  3303. // 监测磁悬浮限位位置
  3304. // 根据位置确定是否 按下按钮+
  3305. if (Read_Real_Voltage() / 100 < 12 && key_on == 1)
  3306. {
  3307. key_on = 0;
  3308. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  3309. rt_pin_write(EMS_Dir, 0);
  3310. rt_thread_delay(50);
  3311. rt_pin_write(EMS_Dir, 1);
  3312. }
  3313. #else
  3314. #if Color_EN == 1
  3315. /*黄板归位*/
  3316. if (Read_Color_Key(KEY_BACK) == 1)
  3317. {
  3318. LOG_W("黄板归位!");
  3319. ctr_Color_board(CLOSE, 300);
  3320. rt_thread_delay(5000);
  3321. }
  3322. #else
  3323. if (device_status.work_status == 1 && device_status.heat_sw == 1)
  3324. {
  3325. ctr_lamp(ON, 1);
  3326. rt_thread_delay(10000);
  3327. calorstat(device_status.heattime * 60);
  3328. }
  3329. else
  3330. {
  3331. LOG_I("Not calorstat!");
  3332. }
  3333. if (device_status.work_status == 1 && device_status.version_type == 5)
  3334. {
  3335. var_clear++;
  3336. if (var_clear == 300)
  3337. {
  3338. var_clear = 0;
  3339. ctr_cangmen_down(ON);
  3340. rt_thread_mdelay(5000);
  3341. ctr_cangmen_down(OFF);
  3342. rt_thread_mdelay(500);
  3343. ctr_cangmen_down(ON);
  3344. rt_thread_mdelay(5000);
  3345. ctr_cangmen_down(OFF);
  3346. }
  3347. }
  3348. // //吸虫塔补光灯测试
  3349. // while(device_status.vol!=200)
  3350. // {
  3351. // if(var_led==0) ctr_led(ON);
  3352. // var_led++;
  3353. // if(var_led>10)
  3354. // {
  3355. // ctr_led(OFF);
  3356. // var_ledoff++;
  3357. // if(var_ledoff>10)
  3358. // {
  3359. // var_led = 0;
  3360. // var_ledoff = 0;
  3361. // ctr_led(ON);
  3362. // }
  3363. // }
  3364. // rt_thread_delay(1000);
  3365. // }
  3366. rt_thread_delay(1000);
  3367. #endif
  3368. #endif
  3369. }
  3370. }
  3371. /*清扫线程*/
  3372. /**
  3373. * @brief 创建用于控制清扫操作的线程
  3374. *
  3375. * @return int -
  3376. */
  3377. int Control_clear(void)
  3378. {
  3379. /* 创建线程*/
  3380. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  3381. Control_Clear_thread_entry, RT_NULL,
  3382. 2048, // 2048
  3383. 24, // 23
  3384. WORK_CHECK_THREAD_TIMESLICE); // 5
  3385. if (tid_ctr != RT_NULL)
  3386. {
  3387. rt_thread_startup(tid_ctr);
  3388. }
  3389. return 0;
  3390. }
  3391. /*恒温加热线程*/
  3392. /**
  3393. * @brief 创建用于恒温加热操作的线程
  3394. *
  3395. * @return int -
  3396. */
  3397. int Control_heat(void)
  3398. {
  3399. /* 创建线程*/
  3400. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  3401. Control_Heat_thread_entry, RT_NULL,
  3402. 1024, // 2048
  3403. 25, // 23
  3404. WORK_CHECK_THREAD_TIMESLICE); // 5
  3405. if (tid_heat != RT_NULL)
  3406. {
  3407. rt_thread_startup(tid_heat);
  3408. }
  3409. return 0;
  3410. }
  3411. /* 创建线程 -工作状态检测*/
  3412. int Workstatus_check(void)
  3413. {
  3414. /* 创建线程*/
  3415. tid_wkstc = rt_thread_create("work_status_check_thread",
  3416. Work_status_thread_entry, RT_NULL,
  3417. WORK_CHECK_THREAD_STACK_SIZE, // 1024
  3418. WORK_CHECK_THREAD_PRIORITY, // 23
  3419. WORK_CHECK_THREAD_TIMESLICE); // 5
  3420. if (tid_wkstc != RT_NULL)
  3421. {
  3422. rt_thread_startup(tid_wkstc);
  3423. }
  3424. return 0;
  3425. }
  3426. /*工作流程线程*/
  3427. int Work_Loop_Task(void)
  3428. {
  3429. /* 创建线程*/
  3430. tid_workloop = rt_thread_create("work_loop_thread",
  3431. Work_Loop_thread_entry, RT_NULL,
  3432. WORK_LOOP_THREAD_STACK_SIZE, // 2048
  3433. WORK_LOOP_THREAD_PRIORITY, // 12
  3434. WORK_LOOP_THREAD_TIMESLICE); // 5
  3435. #if Color_EN == 0
  3436. if (tid_workloop != RT_NULL)
  3437. {
  3438. rt_thread_startup(tid_workloop);
  3439. }
  3440. #endif
  3441. return 0;
  3442. }
  3443. MSH_CMD_EXPORT(HardWare_Check, HardWare_Check<ON | OFF>);
  3444. MSH_CMD_EXPORT(ctr_lamp, ctr_lamp<ON | OFF>);
  3445. MSH_CMD_EXPORT(ctr_clear, ctr_clear<ON | OFF>);
  3446. MSH_CMD_EXPORT(ctr_led, sample : ctr_led<ON | OFF>);
  3447. /** 复位RTU
  3448. *
  3449. */
  3450. static void UART2_Enter_LowPower(void)
  3451. {
  3452. rt_pin_write(RTU_DTU, OFF);
  3453. /* 1. 关闭串口时钟 */
  3454. __HAL_RCC_USART2_CLK_DISABLE();
  3455. /* 2. 设置TX/RX引脚为模拟输入(高阻态) */
  3456. rt_pin_mode(GET_PIN(A, 2), PIN_MODE_INPUT); // TX引脚(PA2)
  3457. rt_pin_mode(GET_PIN(A, 3), PIN_MODE_INPUT); // RX引脚(PA3)
  3458. for (uint8_t a = 0; a < 30; a++)
  3459. {
  3460. rt_kprintf("UART2_Enter_LowPower! %d / %d\n", a + 1, 30);
  3461. rt_thread_mdelay(60000);
  3462. }
  3463. Recode_e_code(e_rtu_fail, 0, "rtu无法正常通信");
  3464. rt_hw_cpu_reset();
  3465. }
  3466. extern char stop_getinfo;
  3467. extern char errcnt_getinfo;
  3468. Res_def SendReadInfo(char c)
  3469. {
  3470. char wait_dtuwrite = 0;
  3471. while (wait_dtuwrite < 150 && stop_getinfo)
  3472. {
  3473. rt_thread_mdelay(100);
  3474. wait_dtuwrite++;
  3475. // rt_kprintf("stop_getinfo\n");
  3476. }
  3477. static uint32_t tim_getinfo = 0;
  3478. if (tim_getinfo == 0)
  3479. {
  3480. recode_timestampe_NEW(&tim_getinfo);
  3481. }
  3482. if (math_timestampe_NEW(&tim_getinfo) < 2)
  3483. {
  3484. // LOG_I("exit getinfo %d",math_timestampe_NEW(&tim_getinfo));
  3485. return res_Error;
  3486. }
  3487. char readinfo[] = "{\"uart\":\"getinfo\"}";
  3488. DTU_Write("%s", readinfo);
  3489. LOG_I("SendReadInfo (%c) > %s\n", c, readinfo);
  3490. recode_timestampe_NEW(&tim_getinfo);
  3491. errcnt_getinfo++;
  3492. if (errcnt_getinfo >= 30)
  3493. {
  3494. UART2_Enter_LowPower();
  3495. }
  3496. return res_ok;
  3497. }