control.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279
  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. //震动电机
  946. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  947. {
  948. /* 防御:震动时间异常(参数损坏/非法下发)时钳位,避免超长时间阻塞工作流程 */
  949. if(shake_sec > 1000)
  950. {
  951. LOG_W("shake_sec(%d) too large, clamp to 1000", shake_sec);
  952. shake_sec = 1000;
  953. }
  954. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  955. #if 0
  956. rt_pin_write(SHAKE_SW,ON);
  957. return 0;
  958. #endif
  959. if(status_shake==ON&&device_status.shake_ws==1)
  960. {
  961. if(device_status.dtype==3)
  962. {
  963. /* 注意:循环变量必须与 shake_sec 同为 16 位,原 uint8_t 在 shake_sec>=256 时会溢出成死循环 */
  964. for(uint16_t shake_i = 0;shake_i<shake_sec;shake_i++)
  965. {
  966. rt_pin_write(SHAKE_SW,ON);
  967. rt_thread_delay(100);
  968. rt_pin_write(SHAKE_SW,OFF);
  969. rt_thread_delay(1000);
  970. }
  971. }
  972. rt_pin_write(SHAKE_SW,OFF);
  973. HMI_WRITE("cangWei.switchShake.val=%d",0);
  974. system_status.vibration_count += 1;
  975. }
  976. else
  977. {
  978. rt_pin_write(SHAKE_SW,OFF);
  979. HMI_WRITE("cangWei.switchShake.val=%d",0);
  980. }
  981. return status_shake;
  982. }
  983. //补光
  984. uint8_t ctr_led(uint8_t status_led)
  985. {
  986. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  987. if(device_status.version_type==6)
  988. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  989. if(status_led==ON)
  990. {
  991. rt_pin_write(LED_SW,ON);
  992. if(device_status.version_type==6)
  993. rt_pin_write(LED_P,ON);
  994. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  995. }
  996. else if(status_led==OFF){
  997. rt_pin_write(LED_SW,OFF);
  998. if(device_status.version_type==6)
  999. rt_pin_write(LED_P,OFF);
  1000. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  1001. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  1002. }
  1003. return status_led;
  1004. }
  1005. /*************************************************
  1006. Function: ctr_turn
  1007. Description: 转盘驱动
  1008. Calls:
  1009. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1010. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1011. Input: v=0(复位) v=90转90度
  1012. Output: NULL
  1013. Return: NULL
  1014. Others: NULL
  1015. *************************************************/
  1016. void ctr_turn(uint8_t v,uint8_t sta)
  1017. {
  1018. DTU_Write("turn.mp3");
  1019. LOG_I("================v===============:%d",v);
  1020. rt_uint16_t turn_cnt = 0;
  1021. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  1022. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  1023. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  1024. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  1025. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  1026. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  1027. {
  1028. rt_pin_write(TURN_ZP_R, ON);
  1029. rt_pin_write(TURN_ZP_L, OFF);
  1030. //Motor_shake(ON,0);
  1031. turn_cnt ++;
  1032. rt_thread_delay(100);
  1033. if(turn_cnt==1000)
  1034. {
  1035. turn_cnt = 0;
  1036. rt_pin_write(TURN_ZP_R, OFF);
  1037. rt_pin_write(TURN_ZP_L, OFF);
  1038. //Motor_shake(OFF,0);
  1039. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  1040. DTU_Write("%s",turn_warn);
  1041. rt_free(turn_warn);
  1042. break;
  1043. }
  1044. }
  1045. turn_cnt = 0;
  1046. while(v==0&&rt_pin_read(LIMIT_0)==1)
  1047. {
  1048. rt_pin_write(TURN_ZP_R, ON);
  1049. rt_pin_write(TURN_ZP_L, OFF);
  1050. Motor_shake(ON,0);
  1051. rt_thread_delay(200); //10 to 100
  1052. turn_cnt ++;
  1053. if(turn_cnt==300) //6000 to 600
  1054. {
  1055. turn_cnt = 0;
  1056. // rt_pin_write(TURN_ZP_R, OFF);
  1057. // rt_pin_write(TURN_ZP_L, OFF);
  1058. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  1059. // DTU_Write("%s",turn_warn);
  1060. // rt_free(turn_warn);
  1061. break;
  1062. }
  1063. }
  1064. while(v==0&&rt_pin_read(LIMIT_0)==1)
  1065. {
  1066. rt_pin_write(TURN_ZP_R, ON);
  1067. rt_pin_write(TURN_ZP_L, OFF);
  1068. //Motor_shake(ON,0);
  1069. rt_thread_delay(50); //10 to 50
  1070. turn_cnt ++;
  1071. if(turn_cnt==10) //6000 to 1200
  1072. {
  1073. turn_cnt = 0;
  1074. rt_pin_write(TURN_ZP_R, OFF);
  1075. rt_pin_write(TURN_ZP_L, OFF);
  1076. //Motor_shake(OFF,0);
  1077. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  1078. DTU_Write("%s",turn_warn);
  1079. rt_free(turn_warn);
  1080. break;
  1081. }
  1082. }
  1083. turn_cnt = 0;
  1084. while(v==90&&rt_pin_read(LIMIT_90)==1)
  1085. {
  1086. rt_pin_write(TURN_ZP_R, ON);
  1087. rt_pin_write(TURN_ZP_L, OFF);
  1088. //Motor_shake(ON,0);
  1089. rt_thread_delay(200);
  1090. turn_cnt ++;
  1091. if(turn_cnt==150)
  1092. {
  1093. turn_cnt = 0;
  1094. // rt_pin_write(TURN_ZP_R, OFF);
  1095. // rt_pin_write(TURN_ZP_L, OFF);
  1096. // char *turn_warn =makeJson_Warn("超时", "转盘");
  1097. // DTU_Write(turn_warn);
  1098. // rt_free(turn_warn);
  1099. break;
  1100. }
  1101. }
  1102. while(v==90&&rt_pin_read(LIMIT_90)==1)
  1103. {
  1104. // rt_pin_write(TURN_ZP_R, ON);
  1105. // rt_pin_write(TURN_ZP_L, OFF);
  1106. Motor_shake(ON,0);
  1107. rt_thread_delay(50);
  1108. turn_cnt ++;
  1109. if(turn_cnt==10)
  1110. {
  1111. turn_cnt = 0;
  1112. rt_pin_write(TURN_ZP_R, OFF);
  1113. rt_pin_write(TURN_ZP_L, OFF);
  1114. //Motor_shake(OFF,0);
  1115. char *turn_warn =makeJson_Warn("超时", "转盘");
  1116. DTU_Write(turn_warn);
  1117. rt_free(turn_warn);
  1118. break;
  1119. }
  1120. }
  1121. if(sta!=0)
  1122. {
  1123. //保存位置
  1124. device_status.run_sta = sta;
  1125. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  1126. }
  1127. turn_cnt = 0;
  1128. rt_pin_write(TURN_ZP_R, OFF);
  1129. rt_pin_write(TURN_ZP_L, OFF);
  1130. Motor_shake(OFF,0);
  1131. HMI_WRITE("cangWei.rotateFlag.val=0");
  1132. }
  1133. /*************************************************
  1134. Function: ctr_Motor_Driver
  1135. Description: 步进电机驱动
  1136. Calls:
  1137. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1138. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1139. Input: 50000000
  1140. Output: NULL
  1141. Return: shake_sec
  1142. Others: NULL
  1143. *************************************************/
  1144. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  1145. {
  1146. beep_init();
  1147. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  1148. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  1149. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  1150. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  1151. if(EN==0)
  1152. {
  1153. rt_pin_write(Motor_Power,0);
  1154. }
  1155. else{
  1156. rt_pin_write(Motor_EN, EN);
  1157. }
  1158. rt_pin_write(Motor_DIR, DIR);
  1159. beep_set(pwm, pwm/2); //200kzh - 50%
  1160. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  1161. {
  1162. if(time_out==0)
  1163. {
  1164. break;
  1165. }
  1166. rt_thread_delay(50);
  1167. time_out --;
  1168. }
  1169. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  1170. {
  1171. if(time_out==0)
  1172. {
  1173. break;
  1174. }
  1175. rt_thread_delay(50);
  1176. time_out --;
  1177. }
  1178. rt_pin_write(Motor_EN, OFF);
  1179. if(device_status.autotakepic==0)
  1180. ctr_lamp(OFF, 1);
  1181. beep_set(0, 0); //200kzh - 50%
  1182. }
  1183. /*************************************************
  1184. Function: ctr_Petroleum_jelly
  1185. Description: 涂凡士林
  1186. Calls:
  1187. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1188. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1189. Input: NULL
  1190. Output: NULL
  1191. Return: NULL
  1192. Others: NULL
  1193. *************************************************/
  1194. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1195. {
  1196. beep_init();
  1197. pwm_on(2,50000);
  1198. rt_thread_delay(delay*1000);
  1199. pwm_off(2,0);
  1200. rt_thread_delay(1000);
  1201. pwm_on(3, 50000);
  1202. rt_thread_delay(delay*1000);
  1203. pwm_off(3,0);
  1204. device_status.fsl_sw = 0;
  1205. }
  1206. //1.0转仓
  1207. void turnzp(void)
  1208. {
  1209. static uint16_t turn_timeout = 0;
  1210. //rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1211. //rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1212. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1213. // rt_pin_write(CLEAR_R,OFF);
  1214. // rt_pin_write(CLEAR_L,OFF);
  1215. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1216. {
  1217. rt_thread_delay(20);
  1218. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1219. {
  1220. // rt_pin_write(CLEAR_R,ON);
  1221. // rt_pin_write(CLEAR_L,OFF);
  1222. Motor_Juandai(ON);
  1223. rt_thread_delay(50);
  1224. turn_timeout++;
  1225. if(turn_timeout==600)
  1226. {
  1227. LOG_E("检测不到限位,电机强制停止");
  1228. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1229. DTU_Write("%s",ctr_zp_limit_warn);
  1230. rt_free(ctr_zp_limit_warn);
  1231. break;
  1232. }
  1233. }
  1234. break;
  1235. }
  1236. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1237. {
  1238. rt_thread_delay(20);
  1239. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1240. {
  1241. // rt_pin_write(CLEAR_R,ON);
  1242. // rt_pin_write(CLEAR_L,OFF);
  1243. Motor_Juandai(ON);
  1244. rt_thread_delay(50);
  1245. turn_timeout++;
  1246. if(turn_timeout==600)
  1247. {
  1248. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1249. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1250. DTU_Write("%s",ctr_zp_limit_warn);
  1251. rt_free(ctr_zp_limit_warn);
  1252. break;
  1253. }
  1254. }
  1255. break;
  1256. }
  1257. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1258. {
  1259. rt_thread_delay(20);
  1260. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1261. {
  1262. // rt_pin_write(CLEAR_R,OFF);
  1263. // rt_pin_write(CLEAR_L,OFF);
  1264. Motor_Juandai(OFF);
  1265. break;
  1266. }
  1267. else {
  1268. break;
  1269. }
  1270. }
  1271. Motor_Juandai(OFF);
  1272. // rt_pin_write(CLEAR_R,OFF);
  1273. // rt_pin_write(CLEAR_L,OFF);
  1274. turn_timeout = 0;
  1275. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1276. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1277. rt_thread_delay(1000);
  1278. }
  1279. //清扫-常闭型限位开关
  1280. //新结构使用推杆电机做清扫
  1281. uint8_t ctr_clear(uint16_t status_clear)
  1282. {
  1283. static uint16_t clear_num = 0;
  1284. #if PWM_EN == 0
  1285. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1286. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1287. #endif
  1288. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1289. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1290. /*************************************************************外接清扫
  1291. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1292. rt_pin_write(POWER_CLEAR,ON);
  1293. rt_thread_delay(1000*100);
  1294. rt_pin_write(POWER_CLEAR,OFF);
  1295. ************************************************************/
  1296. //停止
  1297. #if PWM_EN == 0
  1298. rt_pin_write(CLEAR_R,OFF);
  1299. rt_pin_write(CLEAR_L,OFF);
  1300. #else
  1301. Motor_Qingsao_R_PWM(OFF);
  1302. Motor_Qingsao_L_PWM(OFF);
  1303. #endif
  1304. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1305. rt_thread_delay(100);
  1306. //如果清扫位置在中间
  1307. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1308. {
  1309. #if PWM_EN == 0
  1310. rt_pin_write(CLEAR_R,ON);
  1311. rt_pin_write(CLEAR_L,OFF);
  1312. #else
  1313. Motor_Qingsao_R_PWM(ON);
  1314. Motor_Qingsao_L_PWM(OFF);
  1315. #endif
  1316. rt_thread_delay(50);
  1317. clear_num++;
  1318. //已到达检测位置
  1319. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1320. {
  1321. #if PWM_EN == 0
  1322. rt_pin_write(CLEAR_R,ON);
  1323. rt_pin_write(CLEAR_L,ON);
  1324. #else
  1325. Motor_Qingsao_R_PWM(OFF);
  1326. Motor_Qingsao_L_PWM(OFF);
  1327. #endif
  1328. rt_thread_delay(2000);
  1329. break;
  1330. }
  1331. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1332. {
  1333. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1334. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1335. #if PWM_EN == 0
  1336. rt_pin_write(CLEAR_R,ON);
  1337. rt_pin_write(CLEAR_L,ON);
  1338. #else
  1339. Motor_Qingsao_R_PWM(OFF);
  1340. Motor_Qingsao_L_PWM(OFF);
  1341. #endif
  1342. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1343. if(Read_Real_Voltage()/100<12)
  1344. {
  1345. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1346. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1347. device_status.clear_status = 0;
  1348. return 0;
  1349. }
  1350. break;
  1351. }
  1352. }
  1353. clear_num = 0;
  1354. //如果清扫位置在左侧
  1355. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1356. {
  1357. #if PWM_EN == 0
  1358. rt_pin_write(CLEAR_R,ON);
  1359. rt_pin_write(CLEAR_L,OFF);
  1360. #else
  1361. Motor_Qingsao_R_PWM(ON);
  1362. Motor_Qingsao_L_PWM(OFF);
  1363. #endif
  1364. rt_thread_delay(50);
  1365. clear_num++;
  1366. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1367. {
  1368. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1369. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1370. #if PWM_EN == 0
  1371. rt_pin_write(CLEAR_R,ON);
  1372. rt_pin_write(CLEAR_L,ON);
  1373. #else
  1374. Motor_Qingsao_R_PWM(OFF);
  1375. Motor_Qingsao_L_PWM(OFF);
  1376. #endif
  1377. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1378. if(Read_Real_Voltage()/100<12)
  1379. {
  1380. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1381. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1382. device_status.clear_status = 0;
  1383. return 0;
  1384. }
  1385. break;
  1386. }
  1387. }
  1388. clear_num = 0;
  1389. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1390. {
  1391. #if PWM_EN == 0
  1392. rt_pin_write(CLEAR_R,ON);
  1393. rt_pin_write(CLEAR_L,ON);
  1394. #else
  1395. Motor_Qingsao_R_PWM(OFF);
  1396. Motor_Qingsao_L_PWM(OFF);
  1397. #endif
  1398. if(Read_Real_Voltage()/100<12)
  1399. {
  1400. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1401. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1402. device_status.clear_status = 0;
  1403. return 0;
  1404. }
  1405. rt_thread_delay(2000);
  1406. break;
  1407. }
  1408. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1409. {
  1410. #if PWM_EN == 0
  1411. rt_pin_write(CLEAR_R,OFF);
  1412. rt_pin_write(CLEAR_L,ON);
  1413. #else
  1414. Motor_Qingsao_R_PWM(OFF);
  1415. Motor_Qingsao_L_PWM(ON);
  1416. #endif
  1417. rt_thread_delay(50);
  1418. clear_num++;
  1419. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1420. {
  1421. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1422. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1423. //DTU_Write("%s",Clear_ERR);
  1424. #if PWM_EN == 0
  1425. rt_pin_write(CLEAR_R,ON);
  1426. rt_pin_write(CLEAR_L,ON);
  1427. #else
  1428. Motor_Qingsao_R_PWM(OFF);
  1429. Motor_Qingsao_L_PWM(OFF);
  1430. #endif
  1431. rt_free(Clear_ERR);
  1432. if(Read_Real_Voltage()/100<12)
  1433. {
  1434. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1435. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1436. device_status.clear_status = 0;
  1437. return 0;
  1438. }
  1439. break;
  1440. }
  1441. }
  1442. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1443. {
  1444. #if PWM_EN == 0
  1445. rt_pin_write(CLEAR_R,ON);
  1446. rt_pin_write(CLEAR_L,ON);
  1447. #else
  1448. Motor_Qingsao_R_PWM(OFF);
  1449. Motor_Qingsao_L_PWM(OFF);
  1450. #endif
  1451. rt_thread_delay(2000);
  1452. break;
  1453. }
  1454. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1455. {
  1456. #if PWM_EN == 0
  1457. rt_pin_write(CLEAR_R,ON);
  1458. rt_pin_write(CLEAR_L,ON);
  1459. #else
  1460. Motor_Qingsao_R_PWM(OFF);
  1461. Motor_Qingsao_L_PWM(OFF);
  1462. #endif
  1463. if(device_status.version_type==41)
  1464. {
  1465. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1466. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1467. //DTU_Write("%s",Clear_ERR);
  1468. rt_free(Clear_ERR);
  1469. }
  1470. return 0;
  1471. }
  1472. clear_num = 0;
  1473. rt_thread_delay(2000);
  1474. #if PWM_EN == 0
  1475. rt_pin_write(CLEAR_R,ON);
  1476. rt_pin_write(CLEAR_L,ON);
  1477. #else
  1478. Motor_Qingsao_R_PWM(OFF);
  1479. Motor_Qingsao_L_PWM(OFF);
  1480. #endif
  1481. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1482. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1483. device_status.clear_status = 0;
  1484. return 1;
  1485. }
  1486. //路由器供电
  1487. void ctr_router(uint8_t status_router)
  1488. {
  1489. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1490. if(device_status.version_type==6)
  1491. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1492. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1493. if(status_router==ON)
  1494. {
  1495. rt_pin_write(ROUTER,ON);
  1496. //
  1497. #if LAMP_EN == 0
  1498. if(device_status.version_type==6)
  1499. rt_pin_write(ROUTER_P,ON);
  1500. #endif
  1501. }
  1502. else {
  1503. rt_pin_write(ROUTER,OFF);
  1504. //
  1505. #if LAMP_EN == 0
  1506. if(device_status.version_type==6)
  1507. rt_pin_write(ROUTER_P,OFF);
  1508. #endif
  1509. }
  1510. }
  1511. //清扫-推杆电机
  1512. void ctr_TG_Clear(uint16_t clear_timeout)
  1513. {
  1514. #if 0
  1515. int current_clear[200];
  1516. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1517. LOG_I("清扫电机待机:%d",Electric_current);
  1518. #endif
  1519. static uint16_t clear_count = 0;
  1520. /* 屏幕 workSta.t6「清扫结束剩余时间」用的尾段常量:
  1521. * 全程 = 正向推杆(clear_timeout*100ms) + 3s 停顿 + 反向复位(clear_timeout*100ms) + 2s 收尾 */
  1522. uint16_t clear_left_tail = (uint16_t)(clear_timeout / 10) + 3 + 2;
  1523. clear_count = clear_timeout;
  1524. #if PWM_EN == 0
  1525. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1526. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1527. #endif
  1528. while(clear_count!=0)
  1529. {
  1530. #if PWM_EN == 0
  1531. rt_pin_write(CLEAR_R,ON);
  1532. rt_pin_write(CLEAR_L,OFF);
  1533. #else
  1534. Motor_Qingsao_R_PWM(ON);
  1535. Motor_Qingsao_L_PWM(OFF);
  1536. #endif
  1537. clear_count--;
  1538. device_status.clear_left = (uint16_t)(clear_count / 10) + clear_left_tail; /* t6 剩余清扫时间(s) */
  1539. rt_thread_delay(100);
  1540. }
  1541. #if PWM_EN == 0
  1542. rt_pin_write(CLEAR_R,ON);
  1543. rt_pin_write(CLEAR_L,ON);
  1544. #else
  1545. Motor_Qingsao_R_PWM(OFF);
  1546. Motor_Qingsao_L_PWM(OFF);
  1547. #endif
  1548. rt_thread_delay(3000);
  1549. clear_count = 0;
  1550. while(clear_timeout!=clear_count)
  1551. {
  1552. #if PWM_EN == 0
  1553. rt_pin_write(CLEAR_R,OFF);
  1554. rt_pin_write(CLEAR_L,ON);
  1555. #else
  1556. Motor_Qingsao_R_PWM(OFF);
  1557. Motor_Qingsao_L_PWM(ON);
  1558. #endif
  1559. // if(clear_count<200)
  1560. // current_clear[clear_count] = atoi(device_status.power);
  1561. clear_count++;
  1562. device_status.clear_left = (uint16_t)((clear_timeout - clear_count) / 10) + 2; /* t6 反向复位剩余(s) */
  1563. rt_thread_delay(100);
  1564. }
  1565. #if PWM_EN == 0
  1566. rt_pin_write(CLEAR_R,ON);
  1567. rt_pin_write(CLEAR_L,ON);
  1568. #else
  1569. Motor_Qingsao_R_PWM(OFF);
  1570. Motor_Qingsao_L_PWM(OFF);
  1571. #endif
  1572. #if 0
  1573. bubblesort(current_clear,200);
  1574. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1575. {
  1576. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1577. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1578. DTU_Write("%s",ctr_clear_warn);
  1579. rt_free(ctr_clear_warn);
  1580. }
  1581. #endif
  1582. rt_thread_delay(2000);
  1583. device_status.clear_status = 0;
  1584. device_status.clear_left = 0; /* 清扫结束,倒计时清零 */
  1585. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1586. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1587. }
  1588. // 全局静态变量,保存上次关闭的 tick
  1589. static rt_uint32_t s_last_off_tick = 0;
  1590. void ctr_RTU_DTU(uint8_t status_rtu)
  1591. {
  1592. // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
  1593. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1594. if (status_rtu == ON)
  1595. {
  1596. // 开机逻辑:必须满足冷却时间才能开机
  1597. rt_uint32_t current_tick = rt_tick_get();
  1598. rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
  1599. #if 1
  1600. if (s_last_off_tick != 0)
  1601. {
  1602. rt_uint32_t diff_tick = current_tick - s_last_off_tick;
  1603. if (diff_tick < cool_ticks)
  1604. {
  1605. // 未冷却完成,计算剩余时间
  1606. rt_uint32_t wait_ticks = cool_ticks - diff_tick;
  1607. rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
  1608. LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
  1609. // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
  1610. rt_thread_delay(wait_ticks);
  1611. }
  1612. }
  1613. #endif
  1614. // 冷却完成,执行开机
  1615. rt_pin_write(RTU_DTU, ON);
  1616. s_last_off_tick = 0; // 清除关机时间
  1617. LOG_W("RTU电源启动!!!");
  1618. }
  1619. else
  1620. {
  1621. // 关机逻辑:记录关机时刻tick
  1622. rt_pin_write(RTU_DTU, OFF);
  1623. s_last_off_tick = rt_tick_get();
  1624. LOG_W("RTU电源关闭:%d",s_last_off_tick);
  1625. }
  1626. }
  1627. /**
  1628. * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
  1629. */
  1630. void RTU_Power_Off_Safe(void)
  1631. {
  1632. if(dtuserial == RT_NULL)
  1633. {
  1634. LOG_E("RTU_Power_Off_Safe: dtuserial is NULL!");
  1635. return;
  1636. }
  1637. /* 1. 【关键】先把 USART2 外设置入复位态:
  1638. * 停止接收和 DMA 请求,避免关闭串口、释放接收缓冲区后 DMA 仍继续写入该内存。
  1639. */
  1640. __HAL_RCC_USART2_FORCE_RESET();
  1641. rt_thread_mdelay(1);
  1642. __HAL_RCC_USART2_RELEASE_RESET();
  1643. /* 2. 关闭串口设备(会释放接收缓冲区并关中断)。
  1644. */
  1645. rt_device_close(dtuserial);
  1646. dtuserial->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  1647. /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
  1648. * extern UART_HandleTypeDef huart2;
  1649. * HAL_UART_DeInit(&huart2);
  1650. */
  1651. /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
  1652. * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
  1653. */
  1654. rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
  1655. rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
  1656. /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
  1657. rt_thread_mdelay(50);
  1658. /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
  1659. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1660. rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
  1661. LOG_I("RTU 12V Power OFF safely.\n");
  1662. }
  1663. /**
  1664. * @brief 恢复 RTU 12V 电源并重新初始化串口
  1665. */
  1666. void RTU_Power_On_Restore(void)
  1667. {
  1668. rt_thread_mdelay(5000);
  1669. /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
  1670. rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
  1671. LOG_I("RTU 12V Power ON.\n");
  1672. /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
  1673. * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
  1674. */
  1675. rt_thread_mdelay(5000);
  1676. __HAL_RCC_USART2_FORCE_RESET();
  1677. rt_thread_mdelay(1);
  1678. __HAL_RCC_USART2_RELEASE_RESET();
  1679. rt_err_t res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
  1680. if(res!=RT_EOK)
  1681. {
  1682. LOG_E("uart2打开失败!");
  1683. }else{
  1684. LOG_I("uart2 Open Success!");
  1685. }
  1686. /* 4. 【关键】重新注册接收回调
  1687. * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
  1688. * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
  1689. * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
  1690. */
  1691. dtu_uart2_reinit_rx();
  1692. }
  1693. /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
  1694. void cmd_rtu_restart(int argc, char** argv)
  1695. {
  1696. rt_kprintf("Starting RTU safe restart sequence...\n");
  1697. // 1. 安全断电
  1698. RTU_Power_Off_Safe();
  1699. // 2. 保持断电 30 秒 (确保电容彻底放电)
  1700. rt_kprintf("Waiting 30s for capacitor discharge...\n");
  1701. rt_thread_mdelay(30000);
  1702. // 3. 恢复供电并重建串口
  1703. RTU_Power_On_Restore();
  1704. rt_kprintf("RTU restart sequence completed.\n");
  1705. }
  1706. //海康拍照
  1707. void Hk_Take_io(void)
  1708. {
  1709. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1710. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1711. rt_pin_write(HK_TAKE, 1);
  1712. rt_pin_write(HK_IO, 0);
  1713. if(device_status.dtype==3)
  1714. Beep_Tip(1000);
  1715. if(device_status.dtype==7)
  1716. rt_thread_mdelay(1000);
  1717. rt_pin_write(HK_TAKE, 0);
  1718. rt_pin_write(HK_IO, 1);
  1719. }
  1720. void calorstat(uint16_t heat_time)
  1721. {
  1722. //uint8_t heat_up_cnt = 0;
  1723. uint16_t n = 0;
  1724. uint16_t delay_heat = 60;
  1725. FlagStatus heat_flg = RESET;
  1726. device_status.heat_time_min = 0;
  1727. device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
  1728. ctr_Heat(ON);
  1729. // UpLoad_Data(); //开始加热时的第一条数据
  1730. //恒温模式
  1731. for(n=0;n < heat_time;n++)
  1732. {
  1733. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1734. {
  1735. heat_flg = SET;
  1736. ctr_Heat(OFF);
  1737. while(delay_heat!=0)
  1738. {
  1739. delay_heat --;
  1740. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1741. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1742. rt_thread_delay(1000);
  1743. if(device_status.heattem>device_status.heat_tem_max + 1){
  1744. device_status.heat_tem_max = device_status.heattem;
  1745. }
  1746. //如果触发异常退出加热
  1747. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1748. {
  1749. rt_thread_delay(1000);
  1750. if(device_status.bat_status == 1)
  1751. {
  1752. LOG_W("============================触发异常退出加热==========================");
  1753. ctr_lamp(OFF,1);
  1754. ctr_Heat(OFF);
  1755. break;
  1756. }
  1757. }
  1758. }
  1759. delay_heat = 60;
  1760. //跳出FOR循环
  1761. n = n + 60;
  1762. }
  1763. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1764. {
  1765. device_status.heat_tem_max = device_status.heattem;
  1766. ctr_Heat(ON);
  1767. }
  1768. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1769. {
  1770. // if(heat_up_cnt==0)
  1771. // {
  1772. // UpLoad_Data(); //加热到设定温度的数据
  1773. // heat_up_cnt = 1;
  1774. // }
  1775. device_status.heat_tem_max = device_status.heattem;
  1776. ctr_Heat(OFF);
  1777. }
  1778. if((n % 60)==0)
  1779. {
  1780. device_status.heat_time_min = device_status.heat_time_min + 1;
  1781. // if(device_status.heat_time_min % 5 == 0)
  1782. // {
  1783. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1784. // }
  1785. }
  1786. /* 屏幕 workSta.t1:剩余加热时间(秒)。恒温等待阶段 n 未推进,剩余时间按同一口径显示 */
  1787. device_status.work_heat_left = (n < heat_time) ? (uint16_t)(heat_time - n) : 0;
  1788. //如果触发异常退出加热
  1789. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1790. {
  1791. rt_thread_delay(1000);
  1792. if(device_status.bat_status == 1)
  1793. {
  1794. rt_kprintf("============================触发异常退出加热==========================");
  1795. ctr_lamp(OFF,1);
  1796. ctr_Heat(OFF);
  1797. break;
  1798. }
  1799. }
  1800. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1801. {
  1802. ctr_Heat(OFF);
  1803. rt_kprintf("异常退出!!!\n");
  1804. break;
  1805. }
  1806. rt_thread_delay(1000);
  1807. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1808. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1809. }
  1810. device_status.work_heat_left = 0; /* 加热结束,倒计时清零 */
  1811. ctr_Heat(OFF);
  1812. //UpLoad_Data(); //加热结束的数据
  1813. }
  1814. //初始化所有控制驱动,关闭
  1815. void HardWare_Init(uint8_t boot)
  1816. {
  1817. Beep_Tip(200);
  1818. //关闭上仓门
  1819. ctr_cangmen_up(OFF);
  1820. if(device_status.dtype==3){
  1821. //关闭下仓门电机
  1822. ctr_cangmen_down(OFF);
  1823. }
  1824. //关闭镇流器
  1825. if(device_status.autotakepic==0)
  1826. ctr_lamp(OFF,1);
  1827. //关闭震动电机
  1828. ctr_shake(OFF,0);
  1829. //关闭加热
  1830. ctr_Heat(OFF);
  1831. //关闭补光灯
  1832. ctr_led(OFF);
  1833. //关闭12V供电(海康相机,RTU)
  1834. //ctr_photo(OFF);
  1835. }
  1836. //拍照
  1837. void HK_TakePhoto(void)
  1838. {
  1839. //打开补光灯
  1840. ctr_led(ON);
  1841. //播放语音
  1842. DTU_Write("takephoto.mp3");
  1843. rt_thread_delay(1000);
  1844. //吸虫塔,水稻
  1845. if(device_status.version_type!=6&&device_status.version_type!=4)
  1846. {
  1847. rt_thread_delay(1000*15);
  1848. }
  1849. char *takephotojson = makeJson_takephoto();
  1850. DTU_Write("%s",takephotojson);
  1851. rt_free(takephotojson);
  1852. //触发海康相机拍照
  1853. Hk_Take_io();
  1854. if(device_status.version_type!=6)
  1855. {
  1856. rt_thread_delay(1000*45);
  1857. }
  1858. else {
  1859. ctr_led(ON);
  1860. rt_thread_delay(1000*19);
  1861. }
  1862. ctr_led(OFF);
  1863. }
  1864. //光控测试任务
  1865. void SunTest(void)
  1866. {
  1867. rt_uint16_t ui_timeout_l = 0;
  1868. //未检测到光照,一秒扫描等待,超时30S
  1869. while(Read_Light_Voltage()<500)
  1870. {
  1871. ui_timeout_l ++;
  1872. rt_thread_delay(1000);
  1873. if(ui_timeout_l==30) break;
  1874. }
  1875. rt_thread_delay(3000);
  1876. if(Read_Light_Voltage()<500)
  1877. {
  1878. device_status.sun_status = 0;//光控状态
  1879. }
  1880. if(Read_Light_Voltage()>500)
  1881. {
  1882. rt_thread_delay(1000);
  1883. }
  1884. rt_thread_delay(3000);
  1885. if(Read_Light_Voltage()>500)
  1886. {
  1887. device_status.sun_status = 1;
  1888. ctr_lamp(OFF,1);
  1889. }
  1890. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1891. device_status.sun_test = 0;
  1892. device_status.work_status=0;
  1893. }
  1894. /*上电初始化*/
  1895. void ResetInit(void)
  1896. {
  1897. /* 复位时清掉屏幕 workSta 页的实时倒计时/进度(t1~t6),避免残留旧值 */
  1898. device_status.work_heat_left = 0;
  1899. device_status.work_clt_left = 0;
  1900. device_status.work_door_left = 0;
  1901. device_status.work_progress = 0;
  1902. device_status.photo_left = 0;
  1903. device_status.clear_left = 0;
  1904. device_status.work_start_tick = 0;
  1905. device_status.work_total_sec = 0;
  1906. if(device_status.dtype==7)
  1907. {
  1908. ctr_Motor_Driver(0, 0, 0, 0);
  1909. //关闭电磁阀
  1910. ctr_shake(OFF, 0);
  1911. //关闭加热
  1912. ctr_Heat(OFF);
  1913. //关闭制冷
  1914. ctr_Cold(OFF);
  1915. //关闭补光灯
  1916. ctr_led(OFF);
  1917. //转盘复位
  1918. if(device_status.run_sta==0)
  1919. ctr_turn(0, 0);
  1920. }
  1921. if(device_status.version_type!=6&&device_status.version_type!=7)
  1922. {
  1923. device_status.work_done = 1;
  1924. //1.打开路由器供电
  1925. ctr_router(ON);
  1926. //2.打开相机供电
  1927. ctr_photo(ON);
  1928. //3.关闭镇流器
  1929. ctr_lamp(OFF,1);
  1930. #if LAMP_EN == 1
  1931. ctr_lamp(OFF,2);
  1932. ctr_lamp(OFF,3);
  1933. #endif
  1934. //4.关闭震动电机
  1935. ctr_shake(OFF, 0);
  1936. //5.关闭加热
  1937. ctr_Heat(OFF);
  1938. //6.关闭补光灯
  1939. ctr_led(OFF);
  1940. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1941. {
  1942. LOG_W("欠压!");
  1943. return;
  1944. }
  1945. //7.关闭上仓门
  1946. device_status.ctr_up_motor = 2;
  1947. // while(device_status.ctr_up_motor!=0)
  1948. // {
  1949. // LOG_I("关闭上仓门...");
  1950. // rt_thread_delay(100);
  1951. // }
  1952. //8.关闭下仓门
  1953. device_status.ctr_down_motor = 2;
  1954. while(device_status.ctr_down_motor!=0)
  1955. {
  1956. LOG_I("关闭下仓门....");
  1957. rt_thread_delay(100);
  1958. }
  1959. //9.清扫
  1960. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1961. {
  1962. LOG_W("异常退出!!!\n");
  1963. return;
  1964. }
  1965. #if 0//LAMP_EN == 0
  1966. ctr_Fan_PUT(OFF);
  1967. ctr_Fan_IN(OFF);
  1968. #endif
  1969. if(device_status.version_type==4||
  1970. device_status.version_type==41||
  1971. device_status.version_type==42||
  1972. device_status.version_type==43||
  1973. device_status.version_type==12)
  1974. {
  1975. if(device_status.photo_ws==0){ //2026-06-05 修复工作结束后最后一张照片拍到清扫电机问题;
  1976. device_status.clear_status=1;
  1977. }
  1978. /* 等待清扫完成,最长 120s:清扫线程异常/被删除时可强制放行,避免工作流程永久卡死 */
  1979. {
  1980. uint16_t clear_wait = 0;
  1981. while(device_status.clear_status==1){
  1982. rt_thread_delay(1000);
  1983. if(++clear_wait>=120){
  1984. LOG_E("wait clear timeout, force clear_status=0");
  1985. device_status.clear_status = 0;
  1986. break;
  1987. }
  1988. }
  1989. }
  1990. #if DZ_260917_1 == 1
  1991. //再执行4次清扫
  1992. for(int i=0;i<4;i++)
  1993. {
  1994. if(device_status.photo_ws==0) device_status.clear_status=1;
  1995. {
  1996. uint16_t clear_wait2 = 0;
  1997. while(device_status.clear_status==1){
  1998. rt_thread_delay(1000);
  1999. if(++clear_wait2>=120){
  2000. LOG_E("wait clear timeout(loop %d), force clear_status=0", i);
  2001. device_status.clear_status = 0;
  2002. break;
  2003. }
  2004. }
  2005. }
  2006. }
  2007. #endif
  2008. }
  2009. #if DZ_260519 == 1
  2010. device_status.clear_status = 2;
  2011. #endif
  2012. #if LAMP_EN == 0
  2013. //排虫
  2014. if(device_status.handle_bugger==0){
  2015. device_status.run_sta = 111; //排虫
  2016. }else{
  2017. device_status.run_sta = 112; //收虫
  2018. }
  2019. #endif
  2020. }
  2021. else {
  2022. //1.打开路由器供电
  2023. ctr_router(ON);
  2024. //2.打开相机供电
  2025. ctr_photo(ON);
  2026. //5.关闭加热
  2027. //6.关闭补光灯
  2028. if(device_status.photo_ws==0){
  2029. ctr_led(OFF);
  2030. }
  2031. }
  2032. }
  2033. /*三色光源*/
  2034. void Led_Check(uint8_t tp)
  2035. {
  2036. if(tp==3)
  2037. {
  2038. if(device_status.light_over_tim>2*3600)
  2039. {
  2040. device_status.work_status = 1;
  2041. device_status.lamp_status = 1;
  2042. device_status.lamp1_status = 0;
  2043. device_status.lamp2_status = 0;
  2044. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  2045. }
  2046. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  2047. device_status.work_status = 1;
  2048. device_status.lamp_status = 0;
  2049. device_status.lamp1_status = 1;
  2050. device_status.lamp2_status = 0;
  2051. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  2052. }
  2053. else if (device_status.light_over_tim<1*3600) {
  2054. device_status.work_status = 1;
  2055. device_status.lamp_status = 0;
  2056. device_status.lamp1_status = 0;
  2057. device_status.lamp2_status = 1;
  2058. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  2059. }
  2060. }
  2061. else if (tp==6) {
  2062. if(device_status.light_over_tim>4*3600)
  2063. {
  2064. device_status.work_status = 1;
  2065. device_status.lamp_status = 1;
  2066. device_status.lamp1_status = 0;
  2067. device_status.lamp2_status = 0;
  2068. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  2069. }
  2070. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  2071. device_status.work_status = 1;
  2072. device_status.lamp_status = 0;
  2073. device_status.lamp1_status = 1;
  2074. device_status.lamp2_status = 0;
  2075. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  2076. }
  2077. else if (device_status.light_over_tim<7200) {
  2078. device_status.work_status = 1;
  2079. device_status.lamp_status = 0;
  2080. device_status.lamp1_status = 0;
  2081. device_status.lamp2_status = 1;
  2082. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  2083. }
  2084. }
  2085. else if (tp==9) {
  2086. if(device_status.light_over_tim>6*3600)
  2087. {
  2088. device_status.work_status = 1;
  2089. device_status.lamp_status = 1;
  2090. device_status.lamp1_status = 0;
  2091. device_status.lamp2_status = 0;
  2092. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  2093. }
  2094. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  2095. device_status.work_status = 1;
  2096. device_status.lamp_status = 0;
  2097. device_status.lamp1_status = 1;
  2098. device_status.lamp2_status = 0;
  2099. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  2100. }
  2101. else if (device_status.light_over_tim<3*3600) {
  2102. device_status.work_status = 1;
  2103. device_status.lamp_status = 0;
  2104. device_status.lamp1_status = 0;
  2105. device_status.lamp2_status = 1;
  2106. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  2107. }
  2108. }
  2109. }
  2110. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  2111. void HardWare_Check(uint8_t boot)
  2112. {
  2113. if(boot==0&&device_status.version_type!=6)
  2114. {
  2115. //停止所有动作 -完成
  2116. HardWare_Init(boot);
  2117. }
  2118. else {
  2119. //停止所有动作
  2120. //读取记录待机功耗(实测大概70mA)
  2121. //(需减去屏幕的功耗大概120mA)
  2122. //动作上仓门,读取功率值保存
  2123. //动作下仓门,读取功率值保存
  2124. //动作清扫,读取功率值保存
  2125. //打开镇流器,读取功率值保存
  2126. //打开海康相机供电,读取功率值保存
  2127. //打开补光灯,读取功率值保存
  2128. //打开路由器/RTU供电,读取功率值保存
  2129. //打开加热,读取功率值保存
  2130. }
  2131. }
  2132. static void handle_rain(void)
  2133. {
  2134. //执行预处理
  2135. //打开上下仓门
  2136. device_status.ctr_up_motor = 1;
  2137. device_status.ctr_down_motor = 1;
  2138. //等待5分钟
  2139. for(int i=0;i<300;i++)
  2140. {
  2141. rt_thread_delay(1000);
  2142. LOG_I("等待5分钟...%d",i);
  2143. }
  2144. //清扫10次
  2145. for(int i=0;i<10;i++)
  2146. {
  2147. device_status.clear_status = 1;
  2148. while (device_status.clear_status==1) {
  2149. rt_thread_delay(10000);
  2150. LOG_I("清扫中...%d",i);
  2151. }
  2152. }
  2153. //关闭上下仓门
  2154. device_status.ctr_up_motor = 2;
  2155. device_status.ctr_down_motor = 2;
  2156. }
  2157. static uint8_t prework_state = 0;
  2158. static uint32_t prework_timer = 0;
  2159. static uint8_t clean_count = 0;
  2160. void WorkCheck(void)
  2161. {
  2162. static rt_uint32_t light_timeout = 3600;
  2163. while(device_status.ds == 1 &&
  2164. device_status.rain_status == 0 &&
  2165. device_status.temp_status == 0 &&
  2166. device_status.bat_status == 0)
  2167. {
  2168. if(device_status.timeSwitch == 1) // 时控模式
  2169. {
  2170. LOG_I("***********时控*****************");
  2171. if(device_status.startBar == device_status.stopBar)
  2172. {
  2173. // 开始结束时间相同则24小时模式
  2174. device_status.work_status = 1;
  2175. rt_thread_delay(10000);
  2176. continue;
  2177. }
  2178. time_t now;
  2179. time(&now);
  2180. struct tm now_tm = *localtime(&now);
  2181. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  2182. rt_uint32_t start_sec = device_status.startBar * 3600;
  2183. rt_uint32_t end_sec = device_status.stopBar * 3600;
  2184. #if DZ_260623_2 == 1
  2185. //判断当前时间是否是开始时间的前30分钟
  2186. if(curr_sec >= start_sec - 1800 && prework_state == 0)
  2187. {
  2188. prework_state = 1;
  2189. }
  2190. // 预处理状态机
  2191. switch(prework_state) {
  2192. case 1: // 打开上下仓门
  2193. device_status.ctr_up_motor = 1;
  2194. device_status.ctr_down_motor = 1;
  2195. // 仓门已打开,进入等待阶段
  2196. prework_state = 2;
  2197. prework_timer = 5 * 60; // 5分钟(1000ms为单位)
  2198. break;
  2199. case 2: // 等待5分钟
  2200. prework_timer--;
  2201. if(prework_timer == 0) {
  2202. prework_state = 3;
  2203. clean_count = 0;
  2204. LOG_I("DZ260623_2: 等待5分钟结束,开始清扫");
  2205. }
  2206. break;
  2207. case 3: // 清扫10次
  2208. if(device_status.clear_status == 0) {
  2209. device_status.clear_status = 1;
  2210. clean_count++;
  2211. LOG_I("DZ260623_2: 第%d次清扫", clean_count);
  2212. }
  2213. if(clean_count >= 10) {
  2214. prework_state = 4;
  2215. }
  2216. break;
  2217. case 4: // 关闭上下仓门
  2218. device_status.ctr_up_motor = 2;
  2219. device_status.ctr_down_motor = 2;
  2220. prework_state = 0; // 完成
  2221. LOG_I("DZ260623_2: 预处理完成");
  2222. break;
  2223. }
  2224. #endif
  2225. // 修复跨天判断逻辑
  2226. if (start_sec < end_sec)
  2227. {
  2228. // 不跨天模式
  2229. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  2230. }
  2231. else
  2232. {
  2233. // 跨天模式
  2234. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  2235. }
  2236. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  2237. device_status.work_status ? "工作" : "待机",
  2238. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  2239. device_status.startBar, device_status.stopBar);
  2240. rt_thread_delay(10000);
  2241. }
  2242. else // 光控模式
  2243. {
  2244. LOG_I("光控定时模式!");
  2245. if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
  2246. {
  2247. // 夜晚且有剩余时间:工作
  2248. if(device_status.light_over_tim > 0) {
  2249. device_status.light_over_tim--;
  2250. }
  2251. light_timeout = 3600; // 重置超时计数器
  2252. device_status.work_status = 1;
  2253. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  2254. }
  2255. else
  2256. {
  2257. // 仅在白天递减超时计数器
  2258. if(device_status.sun_status == 1 && light_timeout > 0) {
  2259. light_timeout--;
  2260. }
  2261. device_status.work_status = 0;
  2262. //没有在测试并且没有在自检
  2263. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  2264. {
  2265. ctr_lamp(OFF,1);
  2266. }
  2267. // 连续1小时白天后重置
  2268. if(light_timeout == 0) {
  2269. device_status.light_over_tim = device_status.sunBar * 3600;
  2270. light_timeout = 3600; // 重置防止负数
  2271. }
  2272. }
  2273. }
  2274. rt_thread_delay(1000);
  2275. }
  2276. // 异常状态处理while之外
  2277. device_status.work_status = 0;
  2278. prework_state = 0;
  2279. rt_thread_delay(1000);
  2280. }
  2281. void WorkTask_BZY(void)
  2282. {
  2283. if(device_status.ds==1)
  2284. {
  2285. //时间段判断
  2286. //先判断第一个时间段
  2287. time_t now;
  2288. time(&now);
  2289. static struct tm now_stamp; //现在
  2290. static struct tm cul_sh1_stamp; //开始1
  2291. static struct tm cul_eh1_stamp; //结束1
  2292. static struct tm cul_sh2_stamp; //开始2
  2293. static struct tm cul_eh2_stamp; //结束2
  2294. static struct tm cul_sh3_stamp; //开始3
  2295. static struct tm cul_eh3_stamp; //结束3
  2296. now_stamp = *localtime(&now);
  2297. //获取定时开始的时间戳
  2298. cul_sh1_stamp = *localtime(&now);
  2299. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2300. cul_sh1_stamp.tm_min = 0;
  2301. cul_sh1_stamp.tm_sec = 0;
  2302. cul_sh2_stamp = *localtime(&now);
  2303. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2304. cul_sh2_stamp.tm_min = 0;
  2305. cul_sh2_stamp.tm_sec = 0;
  2306. cul_sh3_stamp = *localtime(&now);
  2307. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2308. cul_sh3_stamp.tm_min = 0;
  2309. cul_sh3_stamp.tm_sec = 0;
  2310. //获取定时结束的时间戳
  2311. cul_eh1_stamp = *localtime(&now);
  2312. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2313. cul_eh1_stamp.tm_min = 0;
  2314. cul_eh1_stamp.tm_sec = 0;
  2315. cul_eh2_stamp = *localtime(&now);
  2316. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2317. cul_eh2_stamp.tm_min = 0;
  2318. cul_eh2_stamp.tm_sec = 0;
  2319. cul_eh3_stamp = *localtime(&now);
  2320. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2321. cul_eh3_stamp.tm_min = 0;
  2322. cul_eh3_stamp.tm_sec = 0;
  2323. 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);
  2324. 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);
  2325. 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);
  2326. 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);
  2327. 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);
  2328. 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);
  2329. //此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2330. //当前时间戳小于第一段开始时间戳
  2331. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2332. {
  2333. time_t now;
  2334. time(&now);
  2335. cul_sh1_stamp = *localtime(&now);
  2336. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2337. cul_sh1_stamp.tm_min = 0;
  2338. cul_sh1_stamp.tm_sec = 0;
  2339. now_stamp = *localtime(&now);
  2340. 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);
  2341. 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);
  2342. LOG_D("run_sta:%d",device_status.run_sta);
  2343. }
  2344. //当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2345. if(difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0&&difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2346. {
  2347. time_t now;
  2348. time(&now);
  2349. cul_sh2_stamp = *localtime(&now);
  2350. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2351. cul_sh2_stamp.tm_min = 0;
  2352. cul_sh2_stamp.tm_sec = 0;
  2353. now_stamp = *localtime(&now);
  2354. 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);
  2355. 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);
  2356. LOG_D("run_sta:%d",device_status.run_sta);
  2357. }
  2358. //如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2359. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0&&difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2360. {
  2361. //转盘到涂凡士林位置
  2362. if(device_status.run_sta==0)
  2363. {
  2364. LOG_I("start to collect!!!");
  2365. DTU_Write("collect.mp3");
  2366. ctr_turn(90,4);
  2367. rt_thread_delay(2000);
  2368. //涂凡士林
  2369. ctr_Petroleum_jelly(10);
  2370. rt_thread_delay(5000);
  2371. }
  2372. /*转盘到采集位置*/
  2373. if(device_status.run_sta==4)
  2374. ctr_turn(90,1);
  2375. //只有开始和结束时刻不同的时间段,才有效
  2376. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)||(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2377. {
  2378. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)&& difftime(mktime(&now_stamp),mktime(&cul_sh1_stamp)) > 0)
  2379. {
  2380. device_status.work_status = 1;
  2381. ctr_Collect_Fan(ON);
  2382. //当前时间戳小于第一段结束时间戳
  2383. while(difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp))>0)
  2384. {
  2385. time(&now);
  2386. now_stamp = *localtime(&now);
  2387. cul_eh1_stamp = *localtime(&now);
  2388. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2389. cul_eh1_stamp.tm_min = 0;
  2390. cul_eh1_stamp.tm_sec = 0;
  2391. if(device_status.run_sta!=1) break;
  2392. LOG_I("1===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh1_stamp));
  2393. rt_thread_mdelay(10000);
  2394. }
  2395. }
  2396. else {
  2397. device_status.work_status=0;
  2398. ctr_Collect_Fan(OFF);
  2399. LOG_I("未到收集1时间!");
  2400. }
  2401. if(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp),mktime(&cul_sh2_stamp)) > 0)
  2402. {
  2403. device_status.work_status = 1;
  2404. ctr_Collect_Fan(ON);
  2405. while(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp))>0)
  2406. {
  2407. time(&now);
  2408. now_stamp = *localtime(&now);
  2409. cul_eh2_stamp = *localtime(&now);
  2410. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2411. cul_eh2_stamp.tm_min = 0;
  2412. cul_eh2_stamp.tm_sec = 0;
  2413. if(device_status.run_sta!=1) break;
  2414. LOG_I("2===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh2_stamp));
  2415. rt_thread_mdelay(10000);
  2416. }
  2417. }
  2418. else {
  2419. device_status.work_status=0;
  2420. ctr_Collect_Fan(OFF);
  2421. LOG_I("未到收集2时间!");
  2422. }
  2423. }
  2424. else
  2425. {
  2426. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2427. }
  2428. }
  2429. if(device_status.run_sta==2)
  2430. {
  2431. ctr_Collect_Fan(OFF);
  2432. device_status.develop_flag = 1;
  2433. device_status.work_status = 2;
  2434. ctr_develop(); //培养
  2435. ctr_turn(90,3);
  2436. }
  2437. if(device_status.run_sta==3)
  2438. {
  2439. ctr_Collect_Fan(OFF);
  2440. device_status.work_status = 3;
  2441. device_status.autotakepic=1; //对焦拍照
  2442. }
  2443. //在培养之前再次确认时间戳
  2444. time(&now);
  2445. now_stamp = *localtime(&now);
  2446. cul_eh2_stamp = *localtime(&now);
  2447. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2448. cul_eh2_stamp.tm_min = 0;
  2449. cul_eh2_stamp.tm_sec = 0;
  2450. //当前时间戳大于收集结束时间戳并且run_sta==1
  2451. if(device_status.run_sta==1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2452. {
  2453. ctr_Collect_Fan(OFF);
  2454. LOG_I("===============collect ok,turn to drop!!!");
  2455. device_status.develop_flag=1;
  2456. device_status.work_status=2; //现在是培养
  2457. DTU_Write("develop.mp3");
  2458. if(device_status.run_sta==1)
  2459. ctr_turn(90,2); /*转盘到培养位置*/
  2460. rt_thread_delay(1000);
  2461. ctr_shake(ON,device_status.shake_sec);
  2462. rt_thread_delay(1000);
  2463. ctr_develop(); //培养
  2464. if(device_status.run_sta==2)
  2465. ctr_turn(90,3); /*转盘到拍照位置*/
  2466. rt_thread_delay(1000);
  2467. device_status.work_status=3; //现在是拍照
  2468. device_status.autotakepic=1; //对焦拍照
  2469. while(device_status.autotakepic==0)
  2470. {
  2471. rt_thread_delay(1000);
  2472. LOG_I("正在对焦拍照............");
  2473. }
  2474. device_status.work_status = 0; //待机
  2475. DTU_Write("wait.mp3");
  2476. LOG_I("=====================work finished==================");
  2477. }
  2478. rt_thread_delay(1000);
  2479. }
  2480. device_status.work_status = 0; //待机
  2481. LOG_W("==================!!!设备已待机!!!===================");
  2482. }
  2483. void WorkTask_BZYV2(void)
  2484. {
  2485. //打开采集风机
  2486. ctr_Collect_Fan(ON);
  2487. //打开胶带步进电机
  2488. //等待采集结束
  2489. //拍照
  2490. //打开相机供电
  2491. //打开补光灯
  2492. //滑台归位
  2493. //滑台启动
  2494. }
  2495. /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ====================
  2496. * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。
  2497. * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(105s) + 仓门动作预留(40s)。
  2498. * 实际存在仓门寻位、恒温等待、异常提前退出等偏差,故进度上限钳到 99%,
  2499. * 与 t1~t3/t5/t6 的实时倒计时配合显示即可反映真实过程;非工作态返回 0。 */
  2500. void Work_Progress_Start(void)
  2501. {
  2502. uint32_t total;
  2503. total = (uint32_t)device_status.clt_time * 60
  2504. + (uint32_t)device_status.heattime * 60
  2505. + WORK_PHOTO_SEC + WORK_CLEAR_SEC + WORK_DOOR_SEC;
  2506. if (total == 0) total = 1;
  2507. if (total > 0xFFFF) total = 0xFFFF; /* work_total_sec 为 uint16_t */
  2508. device_status.work_total_sec = (uint16_t)total;
  2509. device_status.work_start_tick = rt_tick_get();
  2510. device_status.work_progress = 0;
  2511. /* 新的一轮工作开始,清掉上一轮的实时倒计时 */
  2512. device_status.work_heat_left = 0;
  2513. device_status.work_clt_left = 0;
  2514. device_status.work_door_left = 0;
  2515. device_status.photo_left = 0;
  2516. device_status.clear_left = 0;
  2517. }
  2518. uint8_t Work_Progress_Get(void)
  2519. {
  2520. uint32_t elapsed;
  2521. uint32_t total = device_status.work_total_sec;
  2522. if (device_status.work_status != 1 || device_status.work_start_tick == 0 || total == 0)
  2523. {
  2524. return 0;
  2525. }
  2526. elapsed = (rt_tick_get() - device_status.work_start_tick) / RT_TICK_PER_SECOND;
  2527. device_status.work_progress = (uint8_t)((elapsed >= total) ? 99 : (elapsed * 100 / total));
  2528. return device_status.work_progress;
  2529. }
  2530. //工作任务流程s
  2531. void WorkTask(void)
  2532. {
  2533. static int clt_time = 0; //收集时间差秒数
  2534. static int heat_time = 0;
  2535. static uint8_t workloop = 0;
  2536. LOG_I("工作状态:%d",device_status.work_status);
  2537. LOG_I("开关机状态:%d",device_status.ds);
  2538. 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)
  2539. {
  2540. workloop++;
  2541. device_status.work_done = 0;
  2542. /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长,
  2543. * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。放在本轮循环体开头,
  2544. * 保证 24 小时/时控模式下连续多轮工作时进度与倒计时每轮都重新开始。 */
  2545. Work_Progress_Start();
  2546. if(clt_time==0)
  2547. {
  2548. #if 0
  2549. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2550. {
  2551. LOG_W("相机电流不足!");
  2552. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2553. DTU_Write("%s",ctr_hk_p_warn);
  2554. rt_free(ctr_hk_p_warn);
  2555. }
  2556. #endif
  2557. #if 0//LAMP_EN == 0
  2558. ctr_Fan_PUT(ON);
  2559. #endif
  2560. DTU_Write("workstart.mp3");
  2561. }
  2562. //1.打开灯管
  2563. #if LAMP_EN == 1
  2564. if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
  2565. {
  2566. ctr_lamp(ON,1);
  2567. ctr_lamp(OFF,2);
  2568. ctr_lamp(OFF,3);
  2569. }
  2570. else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
  2571. ctr_lamp(OFF,1);
  2572. ctr_lamp(ON,2);
  2573. ctr_lamp(OFF,3);
  2574. }
  2575. else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
  2576. ctr_lamp(OFF,1);
  2577. ctr_lamp(OFF,2);
  2578. ctr_lamp(ON,3);
  2579. }
  2580. #else
  2581. ctr_lamp(ON,1);
  2582. if(device_status.version_type==12)
  2583. {
  2584. ctr_Heat(ON);
  2585. }
  2586. #endif
  2587. LOG_I("打开灯管!!!");
  2588. #if 0
  2589. rt_thread_delay(2000);
  2590. if(atoi(device_status.power)<450)
  2591. {
  2592. LOG_I("Current is:%d",atoi(device_status.power));
  2593. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  2594. {
  2595. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  2596. DTU_Write("%s",lamp_err_warn);
  2597. rt_free(lamp_err_warn);
  2598. }
  2599. }
  2600. #endif
  2601. if(device_status.version_type!=5)
  2602. {
  2603. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  2604. //2.打开上仓门
  2605. LOG_I("打开上仓门!!!");
  2606. device_status.ctr_up_motor = 1;
  2607. //3.收集
  2608. clt_time = device_status.clt_time * 60;
  2609. /* 防御:参数异常(clt_time<=0)时不再进入无限倒计时(原实现 while(clt_time!=1) 会一直递减为负数) */
  2610. if(clt_time <= 0)
  2611. {
  2612. LOG_E("invalid clt_time(%d), skip collecting!", device_status.clt_time);
  2613. }
  2614. while(clt_time>1)
  2615. {
  2616. device_status.collt_status = 1;
  2617. #if 0//LAMP_EN == 0
  2618. ctr_Fan_IN(ON);
  2619. #endif
  2620. clt_time --;
  2621. device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */
  2622. rt_thread_delay(1000);
  2623. #if 0//LAMP_EN == 0
  2624. if(clt_time%30==0)
  2625. ctr_Fan_PUT(OFF);
  2626. #endif
  2627. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2628. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"",device_status.clt_time,clt_time);
  2629. //如果触发异常
  2630. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2631. {
  2632. ctr_lamp(OFF,1);
  2633. #if LAMP_EN == 1
  2634. ctr_lamp(OFF,2);
  2635. ctr_lamp(OFF,3);
  2636. #endif
  2637. #if 0//LAMP_EN == 0
  2638. ctr_Fan_IN(OFF);
  2639. #endif
  2640. rt_kprintf("异常退出!!!\n");
  2641. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2642. break;
  2643. }
  2644. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2645. {
  2646. ctr_lamp(OFF,1);
  2647. #if LAMP_EN == 1
  2648. ctr_lamp(OFF,2);
  2649. ctr_lamp(OFF,3);
  2650. #endif
  2651. #if 0//LAMP_EN == 0
  2652. ctr_Fan_IN(OFF);
  2653. #endif
  2654. rt_kprintf("异常退出!!!\n");
  2655. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2656. break;
  2657. }
  2658. }
  2659. device_status.collt_status = 0;
  2660. clt_time = 1;
  2661. device_status.work_clt_left = 0; /* 收集结束,倒计时清零 */
  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. #if 0//LAMP_EN == 0
  2691. ctr_Fan_IN(OFF);
  2692. #endif
  2693. //4.关闭上仓门
  2694. device_status.ctr_up_motor = 2;
  2695. rt_kprintf("关闭上仓门!!!\n");
  2696. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  2697. while(device_status.ctr_up_motor!=0)
  2698. {
  2699. rt_thread_delay(100);
  2700. LOG_I("等待上仓门关闭!!!");
  2701. }
  2702. //5.加热
  2703. if(device_status.heat_sw==0&&device_status.version_type!=12)
  2704. {
  2705. calorstat(device_status.heattime*60);
  2706. }
  2707. else {
  2708. heat_time = device_status.heattime * 60;
  2709. while(heat_time!=0)
  2710. {
  2711. heat_time--;
  2712. device_status.work_heat_left = (uint16_t)heat_time; /* 屏幕 workSta.t1 剩余加热时间(s) */
  2713. LOG_I("正在加热状态!!!");
  2714. rt_thread_delay(1000);
  2715. }
  2716. }
  2717. device_status.work_heat_left = 0;
  2718. //6.打开下仓门
  2719. if(device_status.rain_status==0)
  2720. {
  2721. uint16_t cnt_photo = 0;
  2722. while(device_status.photo_ws==1) //如果正在拍照,要等拍照结束
  2723. {
  2724. rt_thread_delay(1000);
  2725. cnt_photo++;
  2726. if(cnt_photo>=60) break;
  2727. }
  2728. device_status.ctr_down_motor =1;
  2729. }
  2730. else {
  2731. device_status.ctr_down_motor = 2;
  2732. device_status.ctr_up_motor = 2;
  2733. }
  2734. //待下仓门完全打开
  2735. while(device_status.ctr_down_motor!=0)
  2736. {
  2737. rt_thread_delay(100);
  2738. rt_kprintf("等待下仓门打开!!!\n");
  2739. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  2740. }
  2741. rt_thread_delay(3000);
  2742. //关闭下仓门
  2743. device_status.ctr_down_motor = 2;
  2744. while(device_status.ctr_down_motor!=0)
  2745. {
  2746. rt_thread_delay(100);
  2747. rt_kprintf("等待下仓门关闭!!!\n");
  2748. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  2749. }
  2750. //2022-04-12add:
  2751. LOG_I("打开上仓门!!!");
  2752. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  2753. if(device_status.rain_status==0)
  2754. {
  2755. device_status.ctr_up_motor = 1;
  2756. }
  2757. if(device_status.version_type==4||
  2758. device_status.version_type==41||
  2759. device_status.version_type==42||
  2760. device_status.version_type==43||
  2761. device_status.version_type==12)
  2762. {
  2763. //震动
  2764. ctr_shake(ON, device_status.shake_sec);
  2765. device_status.photo_ws = 1;
  2766. }
  2767. #if DZ_260519 == 1
  2768. if(workloop==3&&device_status.clt_time==10&&device_status.heattime==10){
  2769. workloop = 0;
  2770. device_status.clear_status = 2;
  2771. }
  2772. #endif
  2773. }
  2774. }
  2775. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2776. {
  2777. rt_kprintf("-------------------not work--------------------\n");
  2778. device_status.work_status = 0;
  2779. }
  2780. //工作结束
  2781. if(device_status.work_done==0&&device_status.work_status==0)
  2782. {
  2783. DTU_Write("workdone");
  2784. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  2785. ResetInit();
  2786. clt_time = 0;
  2787. upload_sys_monitor_info();
  2788. }
  2789. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2790. }
  2791. /* 入口函数-工作状态检测 */
  2792. static void Work_status_thread_entry(void *parameter)
  2793. {
  2794. static uint16_t sun_status_num = 120;
  2795. //如果是光控定时,进行光控监测的消抖
  2796. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2797. {
  2798. rt_kprintf("光控定时!!!\n");
  2799. while(sun_status_num!=0)
  2800. {
  2801. sun_status_num--;
  2802. rt_thread_delay(1000);
  2803. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2804. }
  2805. device_status.light_over_tim = device_status.sunBar * 3600;
  2806. device_status.test_flag = 0;
  2807. }
  2808. while(1)
  2809. {
  2810. WorkCheck();
  2811. }
  2812. }
  2813. /*入口函数-工作流程*/
  2814. static void Work_Loop_thread_entry(void *parameter)
  2815. {
  2816. rt_thread_delay(1000);
  2817. ResetInit();
  2818. while(1)
  2819. {
  2820. if(device_status.dtype==7)
  2821. {
  2822. WorkTask_BZY();
  2823. }
  2824. if(device_status.version_type!=6&&device_status.dtype==3)
  2825. {
  2826. WorkTask();
  2827. }
  2828. if(device_status.version_type==6)
  2829. {
  2830. WorkTask_XCT();
  2831. }
  2832. rt_thread_delay(1000);
  2833. }
  2834. }
  2835. /*入口函数清扫*/
  2836. static void Control_Clear_thread_entry(void *parameter)
  2837. {
  2838. static int selfcheck_cnt = 0;
  2839. float voltage_f = 0.0;
  2840. while(1)
  2841. {
  2842. #if ROBOT_EN ==1
  2843. ctr_Motor_Robot(ON);
  2844. rt_thread_delay(100);
  2845. #else
  2846. #if Color_EN ==1
  2847. /*复位*/
  2848. if(Read_Color_Key(KEY_RESET)==1)
  2849. {
  2850. ctr_Color_board(RESET, 1200);
  2851. rt_thread_delay(5000);
  2852. }
  2853. #else
  2854. if(device_status.selfcheck_ws==1){
  2855. selfcheck_cnt ++;
  2856. if(selfcheck_cnt==1){
  2857. ctr_lamp(ON,1);
  2858. rt_thread_delay(1000);
  2859. device_status.ctr_up_motor = 1;
  2860. device_status.ctr_down_motor = 1;
  2861. //记录当前电压
  2862. voltage_f = atof(device_status.voltage);
  2863. rt_thread_delay(1500);
  2864. //开启加热
  2865. ctr_Heat(ON);
  2866. }
  2867. if(selfcheck_cnt==50){
  2868. double voltage_n = atof(device_status.voltage);
  2869. //判断当前电压和加热前的电压差
  2870. if(voltage_f-voltage_n<0.5){
  2871. device_status.heat_sta = 1; //加热异常
  2872. //上报预警,加热故障
  2873. char *heat_warn = makeJson_Warn("加热报警", "heat-error");
  2874. DTU_Write("%s", heat_warn);
  2875. rt_free(heat_warn);
  2876. }else{
  2877. device_status.heat_sta = 0;
  2878. }
  2879. rt_thread_delay(1000);
  2880. //关闭加热
  2881. ctr_Heat(OFF);
  2882. //振动
  2883. ctr_shake(ON, 1);
  2884. //device_status.clear_status = 1;
  2885. ctr_lamp(OFF, 1);
  2886. }
  2887. if(selfcheck_cnt>1800){ //超过3分钟
  2888. device_status.selfcheck_ws = 0;
  2889. device_status.ctr_up_motor = 0;
  2890. device_status.ctr_down_motor = 0;
  2891. //
  2892. device_status.camsta = 1; //相机异常
  2893. device_status.led_sta = 1;
  2894. //上报自检结果
  2895. //上报自检结果
  2896. char *json_temp = makeJson_selfCheck_(1);
  2897. DTU_Write("%s",json_temp);
  2898. free(json_temp);
  2899. }
  2900. }
  2901. if(device_status.selfcheck_ws==0){
  2902. selfcheck_cnt = 0;
  2903. }
  2904. if(device_status.clear_status==1)
  2905. {
  2906. ///if(ctr_clear(1200)==0)
  2907. ctr_TG_Clear(500); //2025-1021,由400修改为500,解决金朵推杆慢导致行程走不完
  2908. system_status.cleaning_count +=1; //清扫次数
  2909. }
  2910. if(device_status.clear_status==2){
  2911. turnzp();
  2912. device_status.clear_status=0;
  2913. }
  2914. rt_thread_delay(100);
  2915. #endif
  2916. #endif
  2917. }
  2918. }
  2919. /*入口函数加热*/
  2920. static void Control_Heat_thread_entry(void *parameter)
  2921. {
  2922. while(1)
  2923. {
  2924. if(device_status.work_status==1&&device_status.heat_sw==1)
  2925. {
  2926. calorstat(device_status.heattime*60);
  2927. rt_thread_delay(10000);
  2928. }
  2929. rt_thread_delay(1000);
  2930. }
  2931. }
  2932. /*清扫线程*/
  2933. int Control_clear(void)
  2934. {
  2935. /* 创建线程*/
  2936. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2937. Control_Clear_thread_entry, RT_NULL,
  2938. 2048, //2048
  2939. 24, //23
  2940. WORK_CHECK_THREAD_TIMESLICE); //5
  2941. if(tid_ctr!=RT_NULL)
  2942. {
  2943. rt_thread_startup(tid_ctr);
  2944. }
  2945. return 0;
  2946. }
  2947. /*恒温加热线程*/
  2948. int Control_heat(void)
  2949. {
  2950. /* 创建线程*/
  2951. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  2952. Control_Heat_thread_entry, RT_NULL,
  2953. 1024, //2048
  2954. 25, //23
  2955. WORK_CHECK_THREAD_TIMESLICE); //5
  2956. if(tid_heat!=RT_NULL)
  2957. {
  2958. rt_thread_startup(tid_heat);
  2959. }
  2960. return 0;
  2961. }
  2962. /* 创建线程 -工作状态检测*/
  2963. int Workstatus_check(void)
  2964. {
  2965. /* 创建线程*/
  2966. tid_wkstc = rt_thread_create("work_status_check_thread",
  2967. Work_status_thread_entry, RT_NULL,
  2968. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2969. WORK_CHECK_THREAD_PRIORITY, //23
  2970. WORK_CHECK_THREAD_TIMESLICE); //5
  2971. if(tid_wkstc!=RT_NULL)
  2972. {
  2973. rt_thread_startup(tid_wkstc);
  2974. }
  2975. return 0;
  2976. }
  2977. /*工作流程线程*/
  2978. int Work_Loop_Task(void)
  2979. {
  2980. /* 创建线程*/
  2981. tid_workloop = rt_thread_create("work_loop_thread",
  2982. Work_Loop_thread_entry, RT_NULL,
  2983. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2984. WORK_LOOP_THREAD_PRIORITY, //12
  2985. WORK_LOOP_THREAD_TIMESLICE); //5
  2986. #if Color_EN == 0
  2987. if(tid_workloop!=RT_NULL)
  2988. {
  2989. rt_thread_startup(tid_workloop);
  2990. }
  2991. #endif
  2992. return 0;
  2993. }
  2994. /* 注意:以下 3 个函数签名与 FinSH 的 (int argc, char **argv) 不匹配,
  2995. * 直接导出会导致串口执行时参数为 argc/argv 垃圾值(命令无效果或随机动作),
  2996. * 已统一替换为下方的 ctr_test 命令,此处不再导出。 */
  2997. //MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2998. //MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2999. //MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  3000. // RTU/DTU 电源控制 shell 命令
  3001. static void ctr_rtu_dtu(int argc, char** argv)
  3002. {
  3003. if(argc != 2)
  3004. {
  3005. rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
  3006. rt_kprintf(" ON : RTU/DTU Power ON\n");
  3007. rt_kprintf(" OFF : RTU/DTU Power OFF\n");
  3008. rt_kprintf(" 0 : ON-OFF Pulse (10ms interval)\n");
  3009. return;
  3010. }
  3011. if(strcmp(argv[1], "ON") == 0 || strcmp(argv[1], "on") == 0)
  3012. {
  3013. ctr_RTU_DTU(ON);
  3014. rt_kprintf("RTU/DTU Power ON\n");
  3015. }
  3016. else if(strcmp(argv[1], "OFF") == 0 || strcmp(argv[1], "off") == 0)
  3017. {
  3018. ctr_RTU_DTU(OFF);
  3019. rt_kprintf("RTU/DTU Power OFF\n");
  3020. }
  3021. else if(strcmp(argv[1], "0") == 0)
  3022. {
  3023. rt_kprintf("RTU/DTU ON-OFF Pulse (10ms interval)\n");
  3024. ctr_RTU_DTU(OFF);
  3025. rt_thread_mdelay(1);
  3026. ctr_RTU_DTU(ON);
  3027. rt_kprintf("Pulse complete\n");
  3028. }
  3029. else
  3030. {
  3031. rt_kprintf("Invalid parameter: %s\n", argv[1]);
  3032. rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
  3033. }
  3034. }
  3035. /* ==================== 测试/调试用命令行接口 ====================
  3036. * 背景:原代码将控制函数直接 MSH_CMD_EXPORT(ctr_lamp / ctr_led / ctr_clear / HardWare_Check),
  3037. * 但这些函数的签名是 (uint8_t[,uint8_t]),与 FinSH 的 (int argc, char **argv) 不匹配,
  3038. * 从串口执行时参数实为 argc/argv 的垃圾值 —— 命令要么无任何效果(实测 ctr_led on 不生效)、
  3039. * 要么以随机参数动作。现统一改为 argv 形式的 ctr_test,并移除上述有风险的直接导出。
  3040. */
  3041. static int ctr_test(int argc, char **argv)
  3042. {
  3043. const char *item;
  3044. const char *act;
  3045. if (argc < 3)
  3046. {
  3047. rt_kprintf("usage: ctr_test <item> <act> [val]\n"
  3048. " lamp <1|2|3> <on|off> | led <on|off> | clear <on|off>\n"
  3049. " shake <on|off> [sec] | heat <on|off> | cold <on|off>\n"
  3050. " up <on|off> | down <on|off> | hwinit | hwcheck\n");
  3051. return -1;
  3052. }
  3053. item = argv[1];
  3054. act = argv[2];
  3055. if (strcmp(item, "lamp") == 0)
  3056. {
  3057. int num = atoi(act);
  3058. const char *onoff = (argc >= 4) ? argv[3] : "off";
  3059. ctr_lamp((strcmp(onoff, "on") == 0) ? ON : OFF, (uint8_t)num);
  3060. rt_kprintf("[ctr_test] lamp%d %s\n", num, onoff);
  3061. }
  3062. else if (strcmp(item, "led") == 0)
  3063. {
  3064. ctr_led((strcmp(act, "on") == 0) ? ON : OFF);
  3065. rt_kprintf("[ctr_test] led %s\n", act);
  3066. }
  3067. else if (strcmp(item, "clear") == 0)
  3068. {
  3069. /* 置标志位由清扫线程执行(非阻塞),off 用于停止 */
  3070. device_status.clear_status = (strcmp(act, "on") == 0) ? 1 : 0;
  3071. rt_kprintf("[ctr_test] clear %s (clear_status=%d)\n", act, device_status.clear_status);
  3072. }
  3073. else if (strcmp(item, "shake") == 0)
  3074. {
  3075. uint16_t sec = (argc >= 4) ? (uint16_t)atoi(argv[3]) : 0;
  3076. ctr_shake((strcmp(act, "on") == 0) ? ON : OFF, sec);
  3077. rt_kprintf("[ctr_test] shake %s %d\n", act, sec);
  3078. }
  3079. else if (strcmp(item, "heat") == 0)
  3080. {
  3081. ctr_Heat((strcmp(act, "on") == 0) ? ON : OFF);
  3082. rt_kprintf("[ctr_test] heat %s\n", act);
  3083. }
  3084. else if (strcmp(item, "cold") == 0)
  3085. {
  3086. ctr_Cold((strcmp(act, "on") == 0) ? ON : OFF);
  3087. rt_kprintf("[ctr_test] cold %s\n", act);
  3088. }
  3089. else if (strcmp(item, "up") == 0)
  3090. {
  3091. device_status.ctr_up_motor = (strcmp(act, "on") == 0) ? 1 : 2;
  3092. rt_kprintf("[ctr_test] up %s\n", act);
  3093. }
  3094. else if (strcmp(item, "down") == 0)
  3095. {
  3096. device_status.ctr_down_motor = (strcmp(act, "on") == 0) ? 1 : 2;
  3097. rt_kprintf("[ctr_test] down %s\n", act);
  3098. }
  3099. else if (strcmp(item, "hwinit") == 0)
  3100. {
  3101. HardWare_Init(0);
  3102. rt_kprintf("[ctr_test] HardWare_Init done\n");
  3103. }
  3104. else if (strcmp(item, "hwcheck") == 0)
  3105. {
  3106. HardWare_Check(1);
  3107. rt_kprintf("[ctr_test] HardWare_Check(manual) done\n");
  3108. }
  3109. else
  3110. {
  3111. rt_kprintf("[ctr_test] unknown item: %s\n", item);
  3112. return -1;
  3113. }
  3114. return 0;
  3115. }
  3116. MSH_CMD_EXPORT(ctr_test, ctr_test <item> <act> [val] : 外设测试命令(lamp/led/clear/shake/heat/cold/up/down));
  3117. /* 串口注入命令:配合 TX-RX 自环回,可从控制台向 MCU 注入一路串口数据。
  3118. * - dtu_send:向 uart2 发送字符串(环回后等于"RTU 下发报文",可下发 JSON-RPC)
  3119. * - hmi_send:向 uart4 发送字符串(环回后等于"屏幕下发指令")
  3120. * 注意:字符串不要包含空格(FinSH 以空格分参)。 */
  3121. static int dtu_send(int argc, char **argv)
  3122. {
  3123. if (argc < 2)
  3124. {
  3125. rt_kprintf("usage: dtu_send <string> 例: dtu_send {\"cmd\":\"read\"}\n");
  3126. return -1;
  3127. }
  3128. if (dtuserial == RT_NULL)
  3129. {
  3130. rt_kprintf("[dtu_send] uart2 not ready\n");
  3131. return -1;
  3132. }
  3133. rt_kprintf("[dtu_send] %s\n", argv[1]);
  3134. rt_device_write(dtuserial, 0, argv[1], rt_strlen(argv[1]));
  3135. return 0;
  3136. }
  3137. MSH_CMD_EXPORT(dtu_send, uart2 发送字符串(自环回时用于向MCU注入报文));
  3138. static int hmi_send(int argc, char **argv)
  3139. {
  3140. if (argc < 2)
  3141. {
  3142. rt_kprintf("usage: hmi_send <string> 例: hmi_send getFullData\n");
  3143. return -1;
  3144. }
  3145. if (serial == RT_NULL)
  3146. {
  3147. rt_kprintf("[hmi_send] uart4 not ready\n");
  3148. return -1;
  3149. }
  3150. rt_kprintf("[hmi_send] %s\n", argv[1]);
  3151. rt_device_write(serial, 0, argv[1], rt_strlen(argv[1]));
  3152. return 0;
  3153. }
  3154. MSH_CMD_EXPORT(hmi_send, uart4 发送字符串(自环回时用于向MCU注入屏幕指令));
  3155. MSH_CMD_EXPORT(ctr_rtu_dtu,ctr_rtu_dtu<ON|OFF>);
  3156. MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);