control.c 112 KB

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