| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324 |
- #include <control.h>
- #include <stdint.h>
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-30 Administrator the first version
- */
- #include "beep.h"
- #include "hmi.h"
- #include "easyflash.h"
- #include "stdio.h"
- #include "sound.h"
- #include "dtu_uart2.h"
- #include "sht30_collect.h"
- #include "string.h"
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "control"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- #include "sys_monitor.h"
- //工作检测
- #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
- #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
- #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
- //工作流程
- #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
- #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
- #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
- //设备状态表结构体
- Device_Status_typedef device_status;
- System_stats_t system_status;
- rt_device_t dtuserial; /*串口设备句柄*/
- rt_thread_t tid_workloop = RT_NULL;
- rt_thread_t tid_wkstc = RT_NULL;
- rt_thread_t tid_ctr = RT_NULL;
- rt_thread_t tid_heat = RT_NULL;
- /* 快速停止所有功率输出(全部为无延时写引脚,供删除线程/升级等紧急场景使用) */
- static void Stop_Outputs_Fast(void)
- {
- /* 上仓门:双高电平=刹车停止(与 ctr_cangmen_up(OFF) 相同,只是不等待 1s) */
- rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
- rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
- rt_pin_write(Collect_fan_R, ON);
- rt_pin_write(Collect_fan_L, ON);
- /* 下仓门 */
- rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
- rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
- rt_pin_write(C_DOWN_R, ON);
- rt_pin_write(C_DOWN_L, ON);
- /* 清扫推杆/卷带电机 */
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- rt_pin_write(CLEAR_R, ON);
- rt_pin_write(CLEAR_L, ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- /* 加热/震动/灯管/补光 */
- rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Heat_Pin, OFF);
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- rt_pin_write(SHAKE_SW, OFF);
- rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
- rt_pin_write(LAMP_SW, OFF);
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- rt_pin_write(LED_SW, OFF);
- }
- /* 判断线程当前是否挂在某个 IPC 对象(互斥量/信号量/消息队列等)的等待链表上。
- *
- * 背景:本内核版本(4.0.2)的 rt_thread_delete() 对"正阻塞在 IPC 对象上"的线程,
- * 会把同一个 tlist 节点同时挂进 IPC 等待链表和 rt_thread_defunct 链表;
- * 空闲线程回收 TCB(含栈)后,IPC 等待链表仍指向已释放内存,之后 rt_mutex_release()
- * 会向野指针写入并把"已释放线程"插入就绪链表,破坏内核对象链表。
- * 现场表现为:栈溢出误报(线程名乱码) + list_thread() 精确总线错误(BFAR 为垃圾地址)。
- *
- * 判据:挂起态 且 线程定时器未激活。
- * - rt_thread_delay / 带超时的 IPC 等待 都会启动线程定时器;
- * - RT_WAITING_FOREVER 的 IPC 等待不会启动定时器。
- * 注意:若将来在这些线程中引入"带超时的 IPC 等待",该判据会失效,需改用其它同步方式。 */
- static rt_bool_t thread_in_ipc_wait(rt_thread_t t)
- {
- if (t == RT_NULL)
- {
- return RT_FALSE;
- }
- if ((t->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
- {
- return RT_FALSE; /* 就绪/运行/关闭态:不在 IPC 等待链表上 */
- }
- if (t->thread_timer.parent.flag & RT_TIMER_FLAG_ACTIVATED)
- {
- return RT_FALSE; /* 延时中或带超时的等待:定时器在跑 */
- }
- return RT_TRUE; /* 无超时的 IPC 等待 */
- }
- /* 在关中断的临界区内"判定 + 删除",保证判定与删除之间不会被抢占
- * (否则线程可能恰好在这两步之间进入 IPC 等待)。
- * 返回 0=已删除或无需删除;-1=线程正处于 IPC 等待,本次不删除。 */
- int thread_delete_safe(rt_thread_t *ptid)
- {
- rt_base_t level;
- int ret = 0;
- if (ptid == RT_NULL || *ptid == RT_NULL)
- {
- return 0;
- }
- level = rt_hw_interrupt_disable();
- if (thread_in_ipc_wait(*ptid))
- {
- ret = -1;
- }
- else
- {
- rt_thread_delete(*ptid);
- *ptid = RT_NULL; /* 句柄置空,防止悬挂句柄/二次删除 */
- }
- rt_hw_interrupt_enable(level);
- return ret;
- }
- int thread_delete_workloop(void)
- {
- int32_t try_cnt;
- int left;
- LOG_W("thread_delete_workloop.");
- /* 先停掉正在执行的动作输出并清除中继标志,避免线程被删除后电机/加热仍处于导通状态 */
- device_status.ctr_up_motor = 0;
- device_status.ctr_down_motor = 0;
- device_status.clear_status = 0;
- Stop_Outputs_Fast();
- /* 安全删除 4 个线程:若某个线程此刻正挂在 IPC 等待链表上则先不删除,
- * 稍后重试(最多 60 x 50ms ≈ 3s)。仍无法安全删除的线程保留运行并告警,
- * 宁可不删也不能破坏内核对象链表。 */
- for (try_cnt = 0; try_cnt < 60; try_cnt++)
- {
- left = (thread_delete_safe(&tid_workloop) != 0) ? 1 : 0;
- left += (thread_delete_safe(&tid_wkstc) != 0) ? 1 : 0;
- left += (thread_delete_safe(&tid_heat) != 0) ? 1 : 0;
- left += (thread_delete_safe(&tid_ctr) != 0) ? 1 : 0;
- if (left == 0)
- {
- break;
- }
- rt_thread_mdelay(50);
- }
- if (left != 0)
- {
- LOG_E("thread_delete_workloop: %d thread(s) still blocked on IPC, skip deleting!", left);
- }
- return 0;
- }
- /* 恢复所有被 thread_delete_workloop() 删除的线程。
- * 注意:必须按开机时的创建条件恢复全部 4 个线程(原来只恢复 workloop,
- * 导致自检后 work_status 判定/清扫/加热线程永久缺失,工作流程卡死)。 */
- int thread_restart_workloop(void)
- {
- LOG_W("thread_restart_workloop.");
- if(device_status.dtype!=7&&device_status.version_type!=30 && tid_wkstc==RT_NULL)
- {
- Workstatus_check(); /* 工作状态判定线程 */
- }
- if(device_status.version_type!=30 && tid_workloop==RT_NULL)
- {
- Work_Loop_Task(); /* 工作流程线程 */
- }
- if(device_status.dtype!=7)
- {
- if(tid_ctr==RT_NULL)
- {
- Control_clear(); /* 清扫线程 */
- }
- if(tid_heat==RT_NULL)
- {
- Control_heat(); /* 恒温加热线程 */
- }
- }
- return 0;
- }
- /**************************1.0定制2000W*************************************/
- uint8_t ctr_Fan_PUT(uint8_t status_ws)
- {
- rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(FAN_P,ON);
- }
- else {
- rt_pin_write(FAN_P, OFF);
- }
- return 0;
- }
- uint8_t ctr_Fan_IN(uint8_t status_ws)
- {
- rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(FAN_I,ON);
- }
- else {
- rt_pin_write(FAN_I, OFF);
- }
- return 0;
- }
- /****************************定制色诱*************************************/
- /**/
- #if Color_EN == 1
- uint8_t Read_Color_Key(rt_base_t key_pin)
- {
- rt_uint32_t sta_s = 0;
- rt_pin_mode(key_pin, PIN_MODE_INPUT);
- while(rt_pin_read(key_pin)==1)
- {
- sta_s++;
- /*持续的*/
- rt_thread_delay(50);
- }
- while(rt_pin_read(key_pin)==0)
- {
- rt_thread_delay(50);
- if(sta_s>0) break;
- }
- LOG_W("检测到IO0翻转!!!");
- return 1;
- }
- /*读限位开关状态*/
- uint8_t Read_Color_Limit(rt_base_t pin_limit)
- {
- uint8_t l_sta = 0;
- rt_pin_mode(pin_limit, PIN_MODE_INPUT);
- l_sta = rt_pin_read(pin_limit);
- rt_thread_delay(20);
- l_sta = rt_pin_read(pin_limit);
- return l_sta;
- }
- /*色诱板*/
- int ctr_Color_board(uint8_t forward,uint16_t time_board)
- {
- if(forward==CLOSE) //归位
- {
- if(device_status.cul_time>0)
- device_status.cul_time--;
- ctr_cangmen_down(OFF);
- ctr_cangmen_up(OFF);
- rt_thread_delay(1000);
- // if(device_status.cul_time>7) device_status.cul_time=7;
- // if(device_status.cul_time!=7)
- // {
- // uint8_t ct = device_status.cul_time;
- // while(1)
- // {
- // Beep_Tip(100);
- // rt_thread_delay(100);
- // ct--;
- // if(ct==0) break;
- // }
- // //return device_status.cul_time;
- // }
- if(Read_Color_Limit(LIMIT_RESET)==0)
- {
- device_status.cul_time = 0;
- LOG_W("已在初始位置!");
- Beep_Tip(1000);
- return 0;
- //黄板已经在复位状态,直接返回0;
- }
- ctr_cangmen_up(CLOSE);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
- // {
- // LOG_W("第七天!!!");
- // ctr_cangmen_down(CLOSE);
- // rt_thread_delay(500);
- // }
- LOG_W("先打开运行到限位的右侧!");
- while(Read_Color_Limit(LIMIT_BOARD)==0)
- {
- LOG_W("归位:到达限位中间");
- ctr_cangmen_down(CLOSE);
- if(Read_Color_Limit(LIMIT_RESET)==0)
- {
- ctr_cangmen_down(OFF);
- break;
- }
- }
- //先打开运行到限位的右侧
- while(Read_Color_Limit(LIMIT_BOARD)==1)
- {
- LOG_W("归位:运行!");
- ctr_cangmen_down(CLOSE);
- if(Read_Color_Limit(LIMIT_RESET)==0)
- {
- ctr_cangmen_down(OFF);
- break;
- }
- }
- while(Read_Color_Limit(LIMIT_BOARD)==0) //到达限位中间 继续运行
- {
- LOG_W("归位:到达限位中间");
- ctr_cangmen_down(CLOSE);
- if(Read_Color_Limit(LIMIT_RESET)==0)
- {
- ctr_cangmen_down(OFF);
- break;
- }
- }
- if(Read_Color_Limit(LIMIT_RESET)==0) return 0;
- LOG_W("到达限位左侧!");
- //停止电机
- ctr_cangmen_down(OFF);
- rt_thread_delay(2000);
- //将推杆电机归位
- LOG_D("*推杆电机归位*");
- /*推杆电机伸出*/
- ctr_cangmen_up(OPEN);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- return 2;
- }
- else if(forward==OPEN)
- {
- ctr_cangmen_down(OFF);
- ctr_cangmen_up(OFF);
- rt_thread_delay(1000);
- if(device_status.cul_time==7)
- {
- Beep_Tip(5000);
- return 7;
- }
- LOG_W("打开推杆电机!");
- //2023-04-18:add
- //if(Read_Color_Limit(LIMIT_BOARD)==0)
- {
- LOG_D("*推杆电机归位*");
- /*推杆电机归位*/
- ctr_cangmen_up(CLOSE);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- }
- if(device_status.cul_time<7)
- device_status.cul_time++;
- while(Read_Color_Limit(LIMIT_BOARD)==0)
- {
- ctr_cangmen_down(forward);
- rt_thread_delay(50);
- }
- while(Read_Color_Limit(LIMIT_BOARD)==1)
- {
- time_board--;
- if(time_board==0) break;
- ctr_cangmen_down(forward);
- rt_thread_delay(50);
- }
- ctr_cangmen_down(OFF);
- rt_thread_delay(1000);
- ctr_cangmen_up(OPEN);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
- return OPEN;
- }
- else if (forward==RESET) {
- ctr_cangmen_up(OFF);
- ctr_cangmen_down(OFF);
- if(Read_Color_Limit(LIMIT_RESET)==0)
- {
- device_status.cul_time = 0;
- //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
- LOG_W("已在初始位置!");
- Beep_Tip(1000);
- ctr_cangmen_up(CLOSE);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- device_status.cul_time = 0;
- return 0;
- //黄板已经在复位状态,直接返回0;
- }
- //if(Read_Color_Limit(LIMIT_BOARD)==0)
- {
- LOG_D("*推杆电机归位*");
- /*推杆电机归位*/
- ctr_cangmen_up(CLOSE);
- rt_thread_delay(20000);
- ctr_cangmen_up(OFF);
- }
- while(Read_Color_Limit(LIMIT_RESET)==1)
- {
- time_board--;
- if(time_board==0) break;
- ctr_cangmen_down(CLOSE);
- rt_thread_delay(20);
- }
- device_status.cul_time = 0;
- ctr_cangmen_down(OFF);
- }
- // device_status.cul_time = 0;
- //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
- return RESET; //复位返回0
- }
- #endif
- /****************************5.0***********************************/
- void ctr_turnv5(void)
- {
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- // 转出
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //转入
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- }
- /**************************机器人********************************/
- void ctr_Motor_Robot(uint8_t status_ws)
- {
- unsigned int close_timeout = 0;
- unsigned int open_timeout = 0;
- rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
- rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
- rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
- rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
- rt_pin_write(Motor_Power,OFF);
- rt_pin_write(Motor_Direction_Ctr,ON);
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- if(status_ws == ON)
- {
- //检测按键状态
- while(rt_pin_read(Motor_Direction_Button)==1)
- {
- if(rt_pin_read(LIMIT_CLOSE)==1)
- {
- rt_pin_write(Motor_Direction_Ctr,ON);
- close_timeout++;
- //打开电机驱动器电源,默认关闭
- rt_pin_write(Motor_Power,ON);
- if(close_timeout==2)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_pin_write(Motor_Power,OFF);
- rt_thread_delay(20);
- rt_pin_write(Motor_Power,ON);
- }
- rt_thread_delay(20);
- if(close_timeout>500)
- {
- close_timeout = 600;
- rt_pin_write(Motor_Power,OFF);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- }
- }
- else
- {
- close_timeout = 0;
- rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- }
- rt_thread_delay(20);
- }
- rt_pin_write(Motor_Power,OFF);
- rt_thread_delay(1000*2);
- while(rt_pin_read(Motor_Direction_Button)==0)
- {
- if(rt_pin_read(LIMIT_OPEN)==1)
- {
- open_timeout ++;
- //打开电机驱动器电源,默认关闭
- if(open_timeout==2){
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- }
- rt_pin_write(Motor_Power,ON);
- rt_pin_write(Motor_Direction_Ctr,OFF);
- if(open_timeout>500)
- {
- open_timeout = 600;
- rt_pin_write(Motor_Power,OFF);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- }
- rt_thread_delay(20);
- }
- if(rt_pin_read(LIMIT_OPEN)==0)
- {
- open_timeout = 0;
- rt_pin_write(Motor_Power,OFF);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- }
- rt_thread_delay(20);
- close_timeout = 0;
- }
- open_timeout = 0;
- close_timeout = 0;
- }
- else {
- rt_pin_write(Motor_Power,OFF);
- }
- }
- /*************************吸虫塔*********************************/
- //吸虫电机
- uint8_t ctr_Motor_xichong(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- uint8_t ctr_Camera_Power(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- //吸附电机
- uint8_t ctr_Motor_xifu(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR2_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR2_CTR_PIN, OFF);
- }
- return 0;
- }
- //清扫-卷带电机
- void ctr_JD_Clear(uint8_t shake_sw)
- {
- rt_uint16_t turn_timeout = 0;
- rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
- ctr_cangmen_up(OFF); //清扫传送带的电机
- if(shake_sw==ON)
- {
- ctr_Heat(ON); //清洁风机
- Motor_shake(ON, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- else {
- Motor_shake(OFF, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- while(rt_pin_read(MOTOR_C_LIMIT)==0)
- {
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- turn_timeout = 0;
- while(rt_pin_read(MOTOR_C_LIMIT)==1)
- {
- Motor_Juandai(ON);
- turn_timeout++;
- rt_thread_delay(50);
- if(turn_timeout==1000) break;
- }
- Motor_shake(OFF, 1);
- Motor_Juandai(OFF);
- ctr_cangmen_up(OFF); //关闭清扫
- ctr_Heat(OFF);
- }
- //工作任务流程
- void WorkTask_XCT(void)
- {
- while(device_status.work_status==1&&device_status.ds==1)
- {
- ctr_Motor_xifu(ON);
- //打开相机供电
- ctr_Camera_Power(ON);
- //ctr_Heat(ON); //清洁风机
- uint32_t photo_fre = 0;
- photo_fre = device_status.dattim * 60;
- while(photo_fre!=0)
- {
- photo_fre --;
- rt_thread_delay(1000);
- LOG_I("收集中,等待开始拍照:%d",photo_fre);
- }
- //卷带
- ctr_JD_Clear(OFF);
- //拍照
- HK_TakePhoto();
- //关闭吸附风机
- ctr_Motor_xifu(OFF);
- //关闭相机供电
- //ctr_Heat(OFF);
- ctr_Camera_Power(OFF);
- //卷带
- ctr_JD_Clear(ON);
- // rt_thread_delay(1000);
- // ctr_JD_Clear(ON);
- //震动
- Motor_shake(ON, device_status.shake_sec);
- }
- //关闭风机
- ctr_Motor_xichong(OFF);
- ctr_Motor_xifu(OFF);
- LOG_I("不在工作时间,未工作!!!");
- }
- /**********************************************************************/
- /*光控*/
- uint8_t In_Light_status(void)
- {
- rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
- return rt_pin_read(LIGHT_PIN);
- }
- /*雨控*/
- uint8_t In_Rain_status(void)
- {
- rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
- return rt_pin_read(RAIN_PIN);
- }
- /*加热*/
- uint8_t ctr_Heat(uint8_t status_heat)
- {
- rt_bool_t is_currently_on;
- rt_tick_t now;
- now = rt_tick_get();
- is_currently_on = (system_status.heat_start_time != 0);
- device_status.heat_status = status_heat;
- rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Heat_Pin,status_heat);
- if(status_heat==ON){
- // 已经开着 → 不重复计时
- if (is_currently_on) {
- return ON;
- }
- // 关 → 开,记录起始时间
- system_status.heat_start_time = now;
- }else{
- // 已经关着 → 不重复结算
- if (!is_currently_on) {
- return OFF;
- }
- // 开 → 关,结算运行时间
- system_status.heater_running_time +=
- (now - system_status.heat_start_time);
- system_status.heat_start_time = 0;
- }
- return status_heat;
- }
- /*制冷*/
- uint8_t ctr_Cold(uint8_t status_cold)
- {
- device_status.cold_status = status_cold;
- rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Cold_Pin,status_cold);
- return status_cold;
- }
- /*相机供电*/
- void ctr_photo(uint8_t status_photo)
- {
- rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
- rt_pin_write(PHOTO,status_photo);
- }
- /*************************************************
- Function: ctr_Cool_Fan
- Description: 散热风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- uint8_t ctr_Cool_Fan(uint8_t status_cool)
- {
- device_status.cool_fan_status = status_cool;
- rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
- rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
- if(status_cool==OFF)
- {
- rt_pin_write(Cool_Fan_L,OFF);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- if(status_cool==ON)
- {
- rt_pin_write(Cool_Fan_L,ON);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- return device_status.cool_fan_status;
- }
- /*************************************************
- Function: ctr_Collect_Fan
- Description: 采集风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- void ctr_develop(void)
- {
- rt_kprintf("===============开始培养=================:%d\n",device_status.cul_time);
- //已经培养的时间-分钟
- device_status.stay_time = 0;
- rt_uint32_t ii = 0;
- while(device_status.develop_flag==1)
- {
- float stayedtime = 0.01;
- char stayed[10];
- stayedtime = (float)device_status.stay_time / 60.0;
- /* 浮点用 libc snprintf,并放大缓冲区:原 stayed[5] 在培养时间≥10小时("10.00")时会越界 */
- snprintf(stayed, sizeof(stayed), "%0.02f", stayedtime);
- HMI_WRITE("workSta.staytime.txt=\"%s\"",stayed);
- ii++;
- LOG_I("developing...................................%d\n",ii);
- LOG_I("============================设定的培养温度:%d\n",device_status.heatvalue);
- LOG_I("============================培养仓实时温度:%d\n",device_status.pre_temp);
- if(device_status.pre_temp<(device_status.heatvalue-5))
- {
- //未制冷
- if(device_status.cold_status==0)
- {
- ctr_Heat(ON);
- }
- }
- else
- {
- ctr_Heat(OFF);
- }
- LOG_I("============================制冷开关:%d\n",device_status.cold_sw);
- if(device_status.cold_sw==1)
- {
- if(device_status.pre_temp>(device_status.heatvalue-10))
- {
- if(device_status.heat_status==0)
- {
- ctr_Cold(ON);
- }
- }
- else
- {
- ctr_Cold(OFF);
- }
- }
- if(ii%60==0)
- {
- device_status.stay_time++;
- if(device_status.stay_time / 60 == device_status.cul_time) device_status.develop_flag = 0;
- LOG_I("developed...................................%d分钟\n",device_status.stay_time);
- }
- if(device_status.box_temp>35)
- {
- ctr_Cool_Fan(ON);
- }
- else if (device_status.box_temp<30)
- {
- ctr_Cool_Fan(OFF);
- }
- rt_thread_delay(1000);
- }
- ctr_Cold(OFF);
- ctr_Heat(OFF);
- LOG_I("=========!!!培养完成!!!==========");
- }
- uint8_t ctr_Collect_Fan(uint8_t status_collect)
- {
- device_status.collect_fan_status = status_collect;
- rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
- if(status_collect==OFF)
- {
- rt_pin_write(Collect_Fan,OFF);
- }
- if(status_collect==ON)
- {
- rt_pin_write(Collect_Fan,ON);
- }
- return device_status.collect_fan_status;
- }
- /*************************************************
- Function: ctr_develop
- Description: 培养载玻片孢子
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: v=0(复位) v=90转90度
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- /*上仓门*/
- uint8_t ctr_cangmen_up(uint8_t status_up)
- {
- rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
- rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
- if(status_up==OFF)
- {
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,ON);
- device_status.ctr_up_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_up==OPEN)
- {
- device_status.up_status = 1;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- }
- if(status_up==CLOSE)
- {
- device_status.up_status = 0;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- }
- return device_status.up_status;
- }
- //下仓门
- uint8_t ctr_cangmen_down(uint8_t status_down)
- {
- rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
- rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
- if(status_down==OFF)
- {
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,ON);
- device_status.ctr_down_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_down==OPEN)
- {
- device_status.down_status = 1;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,OFF);
- }
- if(status_down==CLOSE)
- {
- device_status.down_status = 0;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,ON);
- }
- return device_status.down_status;
- }
- //镇流器
- uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
- {
- rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
- rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
- rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
- rt_bool_t is_currently_on;
- rt_tick_t now;
- now = rt_tick_get();
- is_currently_on = (system_status.lamp_start_time != 0);
- if (status_lamp == ON)
- {
- // ====== 开灯 ======
- switch (lamp_num) {
- case 1:
- rt_pin_write(LAMP_SW, ON);
- device_status.lamp_status = ON;
- break;
- case 2:
- rt_pin_write(LAMP_1_SW, ON);
- device_status.lamp1_status = ON;
- break;
- case 3:
- rt_pin_write(LAMP_2_SW, ON);
- device_status.lamp2_status = ON;
- break;
- default:
- break;
- }
- // 已经开着 → 不重复计时
- if (is_currently_on) {
- return ON;
- }
- // 关 → 开,记录起始时间
- system_status.lamp_start_time = now;
- }
- else
- {
- // ====== 关灯 ======
- switch (lamp_num) {
- case 1:
- rt_pin_write(LAMP_SW, OFF);
- device_status.lamp_status = OFF;
- break;
- case 2:
- rt_pin_write(LAMP_1_SW, OFF);
- device_status.lamp1_status = OFF;
- break;
- case 3:
- rt_pin_write(LAMP_2_SW, OFF);
- device_status.lamp2_status = OFF;
- break;
- default:
- break;
- }
- // 已经关着 → 不重复结算
- if (!is_currently_on) {
- return OFF;
- }
- // 开 → 关,结算运行时间
- system_status.lamp_running_time +=
- (now - system_status.lamp_start_time);
- system_status.lamp_start_time = 0;
- }
- HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
- return device_status.lamp_status;
- }
- #if LAMP_EN == 1
- /* ==================== 三灯管轮换(李新定制) ====================
- * 工作开始后 3 只灯管依次循环点亮,每只连续工作 LAMP_ROTATE_SEC(默认1小时),
- * 顺序:灯管1 -> 灯管2 -> 灯管3 -> 灯管1 ... 循环,与屏幕/串口2配置的工作时长无关。
- * 用 tick 计时,调用频率不影响轮换周期。 */
- static rt_tick_t s_lamp_cycle_tick = 0; //轮换起始 tick,0-未启动
- static uint8_t s_lamp_cycle_active = 0; //当前点亮的灯管号(1~3),0-全灭
- /* 工作开始:从当前时刻重新开始轮换 */
- void Lamp_Cycle_Start(void)
- {
- s_lamp_cycle_tick = rt_tick_get();
- s_lamp_cycle_active = 0;
- }
- /* 关闭全部灯管并结束轮换 */
- void Lamp_Cycle_Off(void)
- {
- /* 已经全灭则直接返回,避免每秒重复操作镇流器/刷屏 */
- if(s_lamp_cycle_tick == 0 && s_lamp_cycle_active == 0 &&
- device_status.lamp_status == OFF &&
- device_status.lamp1_status == OFF &&
- device_status.lamp2_status == OFF)
- {
- return;
- }
- s_lamp_cycle_tick = 0;
- s_lamp_cycle_active = 0;
- ctr_lamp(OFF,1);
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- }
- /* 轮换刷新:需要在工作态每秒调用一次 */
- void Lamp_Cycle_Update(void)
- {
- uint8_t act;
- uint32_t elapsed;
- rt_tick_t now = rt_tick_get();
- /* 手动测试/出厂自检期间不干预灯管状态,避免与测试指令互相抢占 */
- if(device_status.test_flag != 0 || device_status.selfcheck_ws != 0)
- {
- return;
- }
- if(s_lamp_cycle_tick == 0)
- {
- s_lamp_cycle_tick = now;
- }
- elapsed = (uint32_t)((now - s_lamp_cycle_tick) / RT_TICK_PER_SECOND);
- /* 每 LAMP_ROTATE_SEC 换一只,1->2->3 循环 */
- act = (uint8_t)((elapsed / LAMP_ROTATE_SEC) % 3) + 1;
- if(act == s_lamp_cycle_active)
- {
- /* 未到切换点:确认三路状态仍与轮换结果一致(测试/自检指令可能改动过),
- * 一致则直接返回,避免每秒重复操作镇流器 */
- if((act == 1 && device_status.lamp_status == ON && device_status.lamp1_status == OFF && device_status.lamp2_status == OFF) ||
- (act == 2 && device_status.lamp_status == OFF && device_status.lamp1_status == ON && device_status.lamp2_status == OFF) ||
- (act == 3 && device_status.lamp_status == OFF && device_status.lamp1_status == OFF && device_status.lamp2_status == ON))
- {
- return;
- }
- }
- /* 先全灭再点亮,避免多只镇流器同时冲击电源 */
- ctr_lamp(OFF,1);
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- ctr_lamp(ON,act);
- s_lamp_cycle_active = act;
- LOG_I("三灯管轮换:光源%d 已工作%lu秒", act, elapsed);
- }
- #endif
- //震动电机
- uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
- {
- /* 防御:震动时间异常(参数损坏/非法下发)时钳位,避免超长时间阻塞工作流程 */
- if(shake_sec > 1000)
- {
- LOG_W("shake_sec(%d) too large, clamp to 1000", shake_sec);
- shake_sec = 1000;
- }
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- #if 0
- rt_pin_write(SHAKE_SW,ON);
- return 0;
- #endif
- if(status_shake==ON&&device_status.shake_ws==1)
- {
- if(device_status.dtype==3)
- {
- /* 注意:循环变量必须与 shake_sec 同为 16 位,原 uint8_t 在 shake_sec>=256 时会溢出成死循环 */
- for(uint16_t shake_i = 0;shake_i<shake_sec;shake_i++)
- {
- rt_pin_write(SHAKE_SW,ON);
- rt_thread_delay(100);
- rt_pin_write(SHAKE_SW,OFF);
- rt_thread_delay(1000);
- }
- }
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- system_status.vibration_count += 1;
- }
- else
- {
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- }
- return status_shake;
- }
- //补光
- uint8_t ctr_led(uint8_t status_led)
- {
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- if(device_status.version_type==6)
- rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
- if(status_led==ON)
- {
- rt_pin_write(LED_SW,ON);
- if(device_status.version_type==6)
- rt_pin_write(LED_P,ON);
- HMI_WRITE("sysTest.switchFill.val=%d",ON);
- }
- else if(status_led==OFF){
- rt_pin_write(LED_SW,OFF);
- if(device_status.version_type==6)
- rt_pin_write(LED_P,OFF);
- HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
- HMI_WRITE("sysTest.switchFill.val=%d",OFF);
- }
- return status_led;
- }
- /*************************************************
- Function: ctr_turn
- Description: 转盘驱动
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: v=0(复位) v=90转90度
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- void ctr_turn(uint8_t v,uint8_t sta)
- {
- DTU_Write("turn.mp3");
- LOG_I("================v===============:%d",v);
- rt_uint16_t turn_cnt = 0;
- rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
- rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
- rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
- //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
- while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- turn_cnt ++;
- rt_thread_delay(100);
- if(turn_cnt==1000)
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
- DTU_Write("%s",turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- turn_cnt = 0;
- while(v==0&&rt_pin_read(LIMIT_0)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(ON,0);
- rt_thread_delay(200); //10 to 100
- turn_cnt ++;
- if(turn_cnt==300) //6000 to 600
- {
- turn_cnt = 0;
- // rt_pin_write(TURN_ZP_R, OFF);
- // rt_pin_write(TURN_ZP_L, OFF);
- // char *turn_warn =makeJson_Warn("超时", "转盘复位");
- // DTU_Write("%s",turn_warn);
- // rt_free(turn_warn);
- break;
- }
- }
- while(v==0&&rt_pin_read(LIMIT_0)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- rt_thread_delay(50); //10 to 50
- turn_cnt ++;
- if(turn_cnt==10) //6000 to 1200
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("超时", "转盘复位");
- DTU_Write("%s",turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- turn_cnt = 0;
- while(v==90&&rt_pin_read(LIMIT_90)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- rt_thread_delay(200);
- turn_cnt ++;
- if(turn_cnt==150)
- {
- turn_cnt = 0;
- // rt_pin_write(TURN_ZP_R, OFF);
- // rt_pin_write(TURN_ZP_L, OFF);
- // char *turn_warn =makeJson_Warn("超时", "转盘");
- // DTU_Write(turn_warn);
- // rt_free(turn_warn);
- break;
- }
- }
- while(v==90&&rt_pin_read(LIMIT_90)==1)
- {
- // rt_pin_write(TURN_ZP_R, ON);
- // rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(ON,0);
- rt_thread_delay(50);
- turn_cnt ++;
- if(turn_cnt==10)
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("超时", "转盘");
- DTU_Write("%s",turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- if(sta!=0)
- {
- //保存位置
- device_status.run_sta = sta;
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
- }
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(OFF,0);
- HMI_WRITE("cangWei.rotateFlag.val=0");
- }
- /*************************************************
- Function: ctr_Motor_Driver
- Description: 步进电机驱动
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: 50000000
- Output: NULL
- Return: shake_sec
- Others: NULL
- *************************************************/
- void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
- {
- beep_init();
- rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
- rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
- if(EN==0)
- {
- rt_pin_write(Motor_Power,0);
- }
- else{
- rt_pin_write(Motor_EN, EN);
- }
- rt_pin_write(Motor_DIR, DIR);
- beep_set(pwm, pwm/2); //200kzh - 50%
- while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- rt_pin_write(Motor_EN, OFF);
- #if LAMP_EN == 0
- if(device_status.autotakepic==0)
- ctr_lamp(OFF, 1);
- #endif
- /* LAMP_EN==1:灯管由 Lamp_Cycle 轮换管理,电机动作结束后不再并入关灯动作,
- * 避免每轮电机动作都把工作中灯管熄灭再重新起弧 */
- beep_set(0, 0); //200kzh - 50%
- }
- /*************************************************
- Function: ctr_Petroleum_jelly
- Description: 涂凡士林
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: NULL
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- void ctr_Petroleum_jelly(rt_uint32_t delay)
- {
- beep_init();
- pwm_on(2,50000);
- rt_thread_delay(delay*1000);
- pwm_off(2,0);
- rt_thread_delay(1000);
- pwm_on(3, 50000);
- rt_thread_delay(delay*1000);
- pwm_off(3,0);
- device_status.fsl_sw = 0;
- }
- //1.0转仓
- void turnzp(void)
- {
- static uint16_t turn_timeout = 0;
- //rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- //rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- // rt_pin_write(CLEAR_R,OFF);
- // rt_pin_write(CLEAR_L,OFF);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- // rt_pin_write(CLEAR_R,ON);
- // rt_pin_write(CLEAR_L,OFF);
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到限位,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- break;
- }
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- // rt_pin_write(CLEAR_R,ON);
- // rt_pin_write(CLEAR_L,OFF);
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- break;
- }
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_thread_delay(20);
- if(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- // rt_pin_write(CLEAR_R,OFF);
- // rt_pin_write(CLEAR_L,OFF);
- Motor_Juandai(OFF);
- break;
- }
- else {
- break;
- }
- }
- Motor_Juandai(OFF);
- // rt_pin_write(CLEAR_R,OFF);
- // rt_pin_write(CLEAR_L,OFF);
- turn_timeout = 0;
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- rt_thread_delay(1000);
- }
- //清扫-常闭型限位开关
- //新结构使用推杆电机做清扫
- uint8_t ctr_clear(uint16_t status_clear)
- {
- static uint16_t clear_num = 0;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
- /*************************************************************外接清扫
- rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- rt_pin_write(POWER_CLEAR,ON);
- rt_thread_delay(1000*100);
- rt_pin_write(POWER_CLEAR,OFF);
- ************************************************************/
- //停止
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
- rt_thread_delay(100);
- //如果清扫位置在中间
- while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(50);
- clear_num++;
- //已到达检测位置
- if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(2000);
- break;
- }
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- //如果清扫位置在左侧
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
- //DTU_Write("%s",Clear_ERR);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(Clear_ERR);
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(device_status.version_type==41)
- {
- rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
- //DTU_Write("%s",Clear_ERR);
- rt_free(Clear_ERR);
- }
- return 0;
- }
- clear_num = 0;
- rt_thread_delay(2000);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 1;
- }
- //路由器供电
- void ctr_router(uint8_t status_router)
- {
- rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
- if(device_status.version_type==6)
- rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
- //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- if(status_router==ON)
- {
- rt_pin_write(ROUTER,ON);
- //
- #if LAMP_EN == 0
- if(device_status.version_type==6)
- rt_pin_write(ROUTER_P,ON);
- #endif
- }
- else {
- rt_pin_write(ROUTER,OFF);
- //
- #if LAMP_EN == 0
- if(device_status.version_type==6)
- rt_pin_write(ROUTER_P,OFF);
- #endif
- }
- }
- //清扫-推杆电机
- void ctr_TG_Clear(uint16_t clear_timeout)
- {
- #if 0
- int current_clear[200];
- int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
- LOG_I("清扫电机待机:%d",Electric_current);
- #endif
- static uint16_t clear_count = 0;
- /* 屏幕 workSta.t6「清扫结束剩余时间」用的尾段常量:
- * 全程 = 正向推杆(clear_timeout*100ms) + 3s 停顿 + 反向复位(clear_timeout*100ms) + 2s 收尾 */
- uint16_t clear_left_tail = (uint16_t)(clear_timeout / 10) + 3 + 2;
- clear_count = clear_timeout;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- while(clear_count!=0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- clear_count--;
- device_status.clear_left = (uint16_t)(clear_count / 10) + clear_left_tail; /* t6 剩余清扫时间(s) */
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(3000);
- clear_count = 0;
- while(clear_timeout!=clear_count)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- // if(clear_count<200)
- // current_clear[clear_count] = atoi(device_status.power);
- clear_count++;
- device_status.clear_left = (uint16_t)((clear_timeout - clear_count) / 10) + 2; /* t6 反向复位剩余(s) */
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- #if 0
- bubblesort(current_clear,200);
- if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
- {
- LOG_E("清扫电机电流不足:%d!",current_clear[199]);
- char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
- DTU_Write("%s",ctr_clear_warn);
- rt_free(ctr_clear_warn);
- }
- #endif
- rt_thread_delay(2000);
- device_status.clear_status = 0;
- device_status.clear_left = 0; /* 清扫结束,倒计时清零 */
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- }
- // 全局静态变量,保存上次关闭的 tick
- static rt_uint32_t s_last_off_tick = 0;
- void ctr_RTU_DTU(uint8_t status_rtu)
- {
- // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- if (status_rtu == ON)
- {
- // 开机逻辑:必须满足冷却时间才能开机
- rt_uint32_t current_tick = rt_tick_get();
- rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
- #if 1
- if (s_last_off_tick != 0)
- {
- rt_uint32_t diff_tick = current_tick - s_last_off_tick;
- if (diff_tick < cool_ticks)
- {
- // 未冷却完成,计算剩余时间
- rt_uint32_t wait_ticks = cool_ticks - diff_tick;
- rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
- LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
- // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
- rt_thread_delay(wait_ticks);
- }
- }
- #endif
- // 冷却完成,执行开机
- rt_pin_write(RTU_DTU, ON);
- s_last_off_tick = 0; // 清除关机时间
- LOG_W("RTU电源启动!!!");
- }
- else
- {
- // 关机逻辑:记录关机时刻tick
- rt_pin_write(RTU_DTU, OFF);
- s_last_off_tick = rt_tick_get();
- LOG_W("RTU电源关闭:%d",s_last_off_tick);
- }
- }
- /**
- * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
- */
- void RTU_Power_Off_Safe(void)
- {
- if(dtuserial == RT_NULL)
- {
- LOG_E("RTU_Power_Off_Safe: dtuserial is NULL!");
- return;
- }
- /* 1. 【关键】先把 USART2 外设置入复位态:
- * 停止接收和 DMA 请求,避免关闭串口、释放接收缓冲区后 DMA 仍继续写入该内存。
- */
- __HAL_RCC_USART2_FORCE_RESET();
- rt_thread_mdelay(1);
- __HAL_RCC_USART2_RELEASE_RESET();
- /* 2. 关闭串口设备(会释放接收缓冲区并关中断)。
- */
- rt_device_close(dtuserial);
- dtuserial->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
- /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
- * extern UART_HandleTypeDef huart2;
- * HAL_UART_DeInit(&huart2);
- */
- /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
- * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
- */
- rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
- rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
- /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
- rt_thread_mdelay(50);
- /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
- LOG_I("RTU 12V Power OFF safely.\n");
- }
- /**
- * @brief 恢复 RTU 12V 电源并重新初始化串口
- */
- void RTU_Power_On_Restore(void)
- {
- rt_thread_mdelay(5000);
- /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
- rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
- LOG_I("RTU 12V Power ON.\n");
- /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
- * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
- */
- rt_thread_mdelay(5000);
- __HAL_RCC_USART2_FORCE_RESET();
- rt_thread_mdelay(1);
- __HAL_RCC_USART2_RELEASE_RESET();
- rt_err_t res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);
- if(res!=RT_EOK)
- {
- LOG_E("uart2打开失败!");
- }else{
- LOG_I("uart2 Open Success!");
- }
- /* 4. 【关键】重新注册接收回调
- * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
- * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
- * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
- */
- dtu_uart2_reinit_rx();
- }
- /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
- void cmd_rtu_restart(int argc, char** argv)
- {
- rt_kprintf("Starting RTU safe restart sequence...\n");
- // 1. 安全断电
- RTU_Power_Off_Safe();
- // 2. 保持断电 30 秒 (确保电容彻底放电)
- rt_kprintf("Waiting 30s for capacitor discharge...\n");
- rt_thread_mdelay(30000);
- // 3. 恢复供电并重建串口
- RTU_Power_On_Restore();
- rt_kprintf("RTU restart sequence completed.\n");
- }
- //海康拍照
- void Hk_Take_io(void)
- {
- rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
- rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
- rt_pin_write(HK_TAKE, 1);
- rt_pin_write(HK_IO, 0);
- if(device_status.dtype==3)
- Beep_Tip(1000);
- if(device_status.dtype==7)
- rt_thread_mdelay(1000);
- rt_pin_write(HK_TAKE, 0);
- rt_pin_write(HK_IO, 1);
- }
- void calorstat(uint16_t heat_time)
- {
- //uint8_t heat_up_cnt = 0;
- uint16_t n = 0;
- uint16_t delay_heat = 60;
- FlagStatus heat_flg = RESET;
- device_status.heat_time_min = 0;
- device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
- ctr_Heat(ON);
- // UpLoad_Data(); //开始加热时的第一条数据
- //恒温模式
- for(n=0;n < heat_time;n++)
- {
- if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
- {
- heat_flg = SET;
- ctr_Heat(OFF);
- while(delay_heat!=0)
- {
- delay_heat --;
- rt_kprintf("正在恒温加热!!!:%d",delay_heat);
- HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
- rt_thread_delay(1000);
- if(device_status.heattem>device_status.heat_tem_max + 1){
- device_status.heat_tem_max = device_status.heattem;
- }
- //如果触发异常退出加热
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- rt_thread_delay(1000);
- if(device_status.bat_status == 1)
- {
- LOG_W("============================触发异常退出加热==========================");
- ctr_lamp(OFF,1);
- ctr_Heat(OFF);
- break;
- }
- }
- }
- delay_heat = 60;
- //跳出FOR循环
- n = n + 60;
- }
- if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
- {
- device_status.heat_tem_max = device_status.heattem;
- ctr_Heat(ON);
- }
- else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
- {
- // if(heat_up_cnt==0)
- // {
- // UpLoad_Data(); //加热到设定温度的数据
- // heat_up_cnt = 1;
- // }
- device_status.heat_tem_max = device_status.heattem;
- ctr_Heat(OFF);
- }
- if((n % 60)==0)
- {
- device_status.heat_time_min = device_status.heat_time_min + 1;
- // if(device_status.heat_time_min % 5 == 0)
- // {
- // UpLoad_Data(); //从加热开始每5分钟上传的数据
- // }
- }
- /* 屏幕 workSta.t1:剩余加热时间(秒)。恒温等待阶段 n 未推进,剩余时间按同一口径显示 */
- device_status.work_heat_left = (n < heat_time) ? (uint16_t)(heat_time - n) : 0;
- //如果触发异常退出加热
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- rt_thread_delay(1000);
- if(device_status.bat_status == 1)
- {
- rt_kprintf("============================触发异常退出加热==========================");
- ctr_lamp(OFF,1);
- ctr_Heat(OFF);
- break;
- }
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_Heat(OFF);
- rt_kprintf("异常退出!!!\n");
- break;
- }
- rt_thread_delay(1000);
- rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
- HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
- }
- device_status.work_heat_left = 0; /* 加热结束,倒计时清零 */
- ctr_Heat(OFF);
- //UpLoad_Data(); //加热结束的数据
- }
- //初始化所有控制驱动,关闭
- void HardWare_Init(uint8_t boot)
- {
- Beep_Tip(200);
- //关闭上仓门
- ctr_cangmen_up(OFF);
- if(device_status.dtype==3){
- //关闭下仓门电机
- ctr_cangmen_down(OFF);
- }
- //关闭镇流器
- #if LAMP_EN == 1
- Lamp_Cycle_Off(); //上电/初始化:关闭3只灯管并复位轮换
- #else
- if(device_status.autotakepic==0)
- ctr_lamp(OFF,1);
- #endif
- //关闭震动电机
- ctr_shake(OFF,0);
- //关闭加热
- ctr_Heat(OFF);
- //关闭补光灯
- ctr_led(OFF);
- //关闭12V供电(海康相机,RTU)
- //ctr_photo(OFF);
- }
- //拍照
- void HK_TakePhoto(void)
- {
- //打开补光灯
- ctr_led(ON);
- //播放语音
- DTU_Write("takephoto.mp3");
- rt_thread_delay(1000);
- //吸虫塔,水稻
- if(device_status.version_type!=6&&device_status.version_type!=4)
- {
- rt_thread_delay(1000*15);
- }
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- //触发海康相机拍照
- Hk_Take_io();
- if(device_status.version_type!=6)
- {
- rt_thread_delay(1000*45);
- }
- else {
- ctr_led(ON);
- rt_thread_delay(1000*19);
- }
- ctr_led(OFF);
- }
- //光控测试任务
- void SunTest(void)
- {
- rt_uint16_t ui_timeout_l = 0;
- //未检测到光照,一秒扫描等待,超时30S
- while(Read_Light_Voltage()<500)
- {
- ui_timeout_l ++;
- rt_thread_delay(1000);
- if(ui_timeout_l==30) break;
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()<500)
- {
- device_status.sun_status = 0;//光控状态
- }
- if(Read_Light_Voltage()>500)
- {
- rt_thread_delay(1000);
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()>500)
- {
- device_status.sun_status = 1;
- #if LAMP_EN == 1
- Lamp_Cycle_Off(); //天亮:关闭3只灯管并复位轮换
- #else
- ctr_lamp(OFF,1);
- #endif
- }
- HMI_WRITE("sysTest.switchSun.val=%d",0);
- device_status.sun_test = 0;
- device_status.work_status=0;
- }
- /*上电初始化*/
- void ResetInit(void)
- {
- /* 复位时清掉屏幕 workSta 页的实时倒计时/进度(t1~t6),避免残留旧值 */
- device_status.work_heat_left = 0;
- device_status.work_clt_left = 0;
- device_status.work_door_left = 0;
- device_status.work_progress = 0;
- device_status.photo_left = 0;
- device_status.clear_left = 0;
- device_status.work_start_tick = 0;
- device_status.work_total_sec = 0;
- if(device_status.dtype==7)
- {
- ctr_Motor_Driver(0, 0, 0, 0);
- //关闭电磁阀
- ctr_shake(OFF, 0);
- //关闭加热
- ctr_Heat(OFF);
- //关闭制冷
- ctr_Cold(OFF);
- //关闭补光灯
- ctr_led(OFF);
- //转盘复位
- if(device_status.run_sta==0)
- ctr_turn(0, 0);
- }
- if(device_status.version_type!=6&&device_status.version_type!=7)
- {
- device_status.work_done = 1;
- //1.打开路由器供电
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- //3.关闭镇流器
- #if LAMP_EN == 1
- Lamp_Cycle_Off(); //关闭3只灯管并复位轮换计时
- #else
- ctr_lamp(OFF,1);
- #endif
- //4.关闭震动电机
- ctr_shake(OFF, 0);
- //5.关闭加热
- ctr_Heat(OFF);
- //6.关闭补光灯
- if(device_status.photo_ws == 0) ctr_led(OFF); //2026-10-13 解决工作状态触发雨控,如果在拍照会导致黑照片;
- if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
- {
- LOG_W("欠压!");
- return;
- }
- //7.关闭上仓门
- device_status.ctr_up_motor = 2;
- // while(device_status.ctr_up_motor!=0)
- // {
- // LOG_I("关闭上仓门...");
- // rt_thread_delay(100);
- // }
- //8.关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- LOG_I("关闭下仓门....");
- rt_thread_delay(100);
- }
- //9.清扫
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- LOG_W("异常退出!!!\n");
- return;
- }
- #if 0//LAMP_EN == 0
- ctr_Fan_PUT(OFF);
- ctr_Fan_IN(OFF);
- #endif
- if(device_status.version_type==4||
- device_status.version_type==41||
- device_status.version_type==42||
- device_status.version_type==43||
- device_status.version_type==12)
- {
- if(device_status.photo_ws==0){ //2026-06-05 修复工作结束后最后一张照片拍到清扫电机问题;
- device_status.clear_status=1;
- }
- /* 等待清扫完成,最长 120s:清扫线程异常/被删除时可强制放行,避免工作流程永久卡死 */
- {
- uint16_t clear_wait = 0;
- while(device_status.clear_status==1){
- rt_thread_delay(1000);
- if(++clear_wait>=120){
- LOG_E("wait clear timeout, force clear_status=0");
- device_status.clear_status = 0;
- break;
- }
- }
- }
- #if DZ_260917_1 == 1
- //再执行4次清扫
- for(int i=0;i<4;i++)
- {
- if(device_status.photo_ws==0) device_status.clear_status=1;
- {
- uint16_t clear_wait2 = 0;
- while(device_status.clear_status==1){
- rt_thread_delay(1000);
- if(++clear_wait2>=120){
- LOG_E("wait clear timeout(loop %d), force clear_status=0", i);
- device_status.clear_status = 0;
- break;
- }
- }
- }
- }
- #endif
- }
- #if DZ_260519 == 1
- device_status.clear_status = 2;
- #endif
- #if LAMP_EN == 0
- //排虫
- if(device_status.handle_bugger==0){
- device_status.run_sta = 111; //排虫
- }else{
- device_status.run_sta = 112; //收虫
- }
- #endif
- }
- else {
- //1.打开路由器供电
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- //5.关闭加热
- //6.关闭补光灯
- if(device_status.photo_ws==0){
- ctr_led(OFF);
- }
- }
- }
- //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
- void HardWare_Check(uint8_t boot)
- {
- if(boot==0&&device_status.version_type!=6)
- {
- //停止所有动作 -完成
- HardWare_Init(boot);
- }
- else {
- //停止所有动作
- //读取记录待机功耗(实测大概70mA)
- //(需减去屏幕的功耗大概120mA)
- //动作上仓门,读取功率值保存
- //动作下仓门,读取功率值保存
- //动作清扫,读取功率值保存
- //打开镇流器,读取功率值保存
- //打开海康相机供电,读取功率值保存
- //打开补光灯,读取功率值保存
- //打开路由器/RTU供电,读取功率值保存
- //打开加热,读取功率值保存
- }
- }
- static void handle_rain(void)
- {
- //执行预处理
- //打开上下仓门
- device_status.ctr_up_motor = 1;
- device_status.ctr_down_motor = 1;
- //等待5分钟
- for(int i=0;i<300;i++)
- {
- rt_thread_delay(1000);
- LOG_I("等待5分钟...%d",i);
- }
- //清扫10次
- for(int i=0;i<10;i++)
- {
- device_status.clear_status = 1;
- while (device_status.clear_status==1) {
- rt_thread_delay(10000);
- LOG_I("清扫中...%d",i);
- }
- }
- //关闭上下仓门
- device_status.ctr_up_motor = 2;
- device_status.ctr_down_motor = 2;
- }
- static uint8_t prework_state = 0;
- static uint32_t prework_timer = 0;
- static uint8_t clean_count = 0;
- void WorkCheck(void)
- {
- #if LAMP_EN == 0
- static rt_uint32_t light_timeout = 3600;
- #endif
- while(device_status.ds == 1 &&
- device_status.rain_status == 0 &&
- device_status.temp_status == 0 &&
- device_status.bat_status == 0)
- {
- if(device_status.timeSwitch == 1) // 时控模式
- {
- LOG_I("***********时控*****************");
- if(device_status.startBar == device_status.stopBar)
- {
- // 开始结束时间相同则24小时模式
- device_status.work_status = 1;
- #if LAMP_EN == 1
- Lamp_Cycle_Update(); //三灯管轮换
- #endif
- rt_thread_delay(10000);
- continue;
- }
- time_t now;
- time(&now);
- struct tm now_tm = *localtime(&now);
- rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
- rt_uint32_t start_sec = device_status.startBar * 3600;
- rt_uint32_t end_sec = device_status.stopBar * 3600;
- #if DZ_260623_2 == 1
- //判断当前时间是否是开始时间的前30分钟
- if(curr_sec >= start_sec - 1800 && prework_state == 0)
- {
- prework_state = 1;
- }
- // 预处理状态机
- switch(prework_state) {
- case 1: // 打开上下仓门
- device_status.ctr_up_motor = 1;
- device_status.ctr_down_motor = 1;
- // 仓门已打开,进入等待阶段
- prework_state = 2;
- prework_timer = 5 * 60; // 5分钟(1000ms为单位)
- break;
- case 2: // 等待5分钟
- prework_timer--;
- if(prework_timer == 0) {
- prework_state = 3;
- clean_count = 0;
- LOG_I("DZ260623_2: 等待5分钟结束,开始清扫");
- }
- break;
- case 3: // 清扫10次
- if(device_status.clear_status == 0) {
- device_status.clear_status = 1;
- clean_count++;
- LOG_I("DZ260623_2: 第%d次清扫", clean_count);
- }
- if(clean_count >= 10) {
- prework_state = 4;
- }
- break;
- case 4: // 关闭上下仓门
- device_status.ctr_up_motor = 2;
- device_status.ctr_down_motor = 2;
- prework_state = 0; // 完成
- LOG_I("DZ260623_2: 预处理完成");
- break;
- }
- #endif
- // 修复跨天判断逻辑
- if (start_sec < end_sec)
- {
- // 不跨天模式
- device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
- }
- else
- {
- // 跨天模式
- device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
- }
- LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
- device_status.work_status ? "工作" : "待机",
- now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
- device_status.startBar, device_status.stopBar);
- #if LAMP_EN == 1
- if(device_status.work_status == 1)
- {
- Lamp_Cycle_Update(); //三灯管轮换
- }
- else if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
- {
- Lamp_Cycle_Off(); //待机:全部灯管关闭
- }
- #endif
- rt_thread_delay(10000);
- }
- else // 光控模式
- {
- LOG_I("光控定时模式!");
- #if LAMP_EN == 1
- /* 三灯管定制:工作时长不再受屏幕/串口2下发的 sunBar 限制,
- * 天黑(光控)即工作,天亮即停;工作期间由 Lamp_Cycle_Update 按
- * LAMP_ROTATE_SEC(默认1小时)依次轮换3只灯管。 */
- if(device_status.sun_status == 0)
- {
- device_status.work_status = 1;
- Lamp_Cycle_Update(); //按 LAMP_ROTATE_SEC 轮换光源1/2/3
- }
- else
- {
- device_status.work_status = 0;
- //没有在测试并且没有在自检
- if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
- {
- Lamp_Cycle_Off();
- }
- }
- #else
- if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
- {
- // 夜晚且有剩余时间:工作
- if(device_status.light_over_tim > 0) {
- device_status.light_over_tim--;
- }
- light_timeout = 3600; // 重置超时计数器
- device_status.work_status = 1;
- LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
- }
- else
- {
- // 仅在白天递减超时计数器
- if(device_status.sun_status == 1 && light_timeout > 0) {
- light_timeout--;
- }
- device_status.work_status = 0;
- //没有在测试并且没有在自检
- if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
- {
- ctr_lamp(OFF,1);
- }
- // 连续1小时白天后重置
- if(light_timeout == 0) {
- device_status.light_over_tim = device_status.sunBar * 3600;
- light_timeout = 3600; // 重置防止负数
- }
- }
- #endif
- }
- rt_thread_delay(1000);
- }
- // 异常状态处理while之外
- device_status.work_status = 0;
- prework_state = 0;
- #if LAMP_EN == 1
- Lamp_Cycle_Off(); //退出工作(雨控/欠压/高温/天黑等):全部灯管关闭
- #endif
- rt_thread_delay(1000);
- }
- void WorkTask_BZY(void)
- {
- if(device_status.ds==1)
- {
- //时间段判断
- //先判断第一个时间段
- time_t now;
- time(&now);
- static struct tm now_stamp; //现在
- static struct tm cul_sh1_stamp; //开始1
- static struct tm cul_eh1_stamp; //结束1
- static struct tm cul_sh2_stamp; //开始2
- static struct tm cul_eh2_stamp; //结束2
- static struct tm cul_sh3_stamp; //开始3
- static struct tm cul_eh3_stamp; //结束3
- now_stamp = *localtime(&now);
- //获取定时开始的时间戳
- cul_sh1_stamp = *localtime(&now);
- cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
- cul_sh1_stamp.tm_min = 0;
- cul_sh1_stamp.tm_sec = 0;
- cul_sh2_stamp = *localtime(&now);
- cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
- cul_sh2_stamp.tm_min = 0;
- cul_sh2_stamp.tm_sec = 0;
- cul_sh3_stamp = *localtime(&now);
- cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
- cul_sh3_stamp.tm_min = 0;
- cul_sh3_stamp.tm_sec = 0;
- //获取定时结束的时间戳
- cul_eh1_stamp = *localtime(&now);
- cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
- cul_eh1_stamp.tm_min = 0;
- cul_eh1_stamp.tm_sec = 0;
- cul_eh2_stamp = *localtime(&now);
- cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
- cul_eh2_stamp.tm_min = 0;
- cul_eh2_stamp.tm_sec = 0;
- cul_eh3_stamp = *localtime(&now);
- cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
- cul_eh3_stamp.tm_min = 0;
- cul_eh3_stamp.tm_sec = 0;
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- //此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
- //当前时间戳小于第一段开始时间戳
- if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
- {
- time_t now;
- time(&now);
- cul_sh1_stamp = *localtime(&now);
- cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
- cul_sh1_stamp.tm_min = 0;
- cul_sh1_stamp.tm_sec = 0;
- now_stamp = *localtime(&now);
- 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);
- 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);
- LOG_D("run_sta:%d",device_status.run_sta);
- }
- //当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
- if(difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0&&difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
- {
- time_t now;
- time(&now);
- cul_sh2_stamp = *localtime(&now);
- cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
- cul_sh2_stamp.tm_min = 0;
- cul_sh2_stamp.tm_sec = 0;
- now_stamp = *localtime(&now);
- 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);
- 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);
- LOG_D("run_sta:%d",device_status.run_sta);
- }
- //如果当前 时间戳大于开始时间戳并且小于 结束时间戳
- if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0&&difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
- {
- //转盘到涂凡士林位置
- if(device_status.run_sta==0)
- {
- LOG_I("start to collect!!!");
- DTU_Write("collect.mp3");
- ctr_turn(90,4);
- rt_thread_delay(2000);
- //涂凡士林
- ctr_Petroleum_jelly(10);
- rt_thread_delay(5000);
- }
- /*转盘到采集位置*/
- if(device_status.run_sta==4)
- ctr_turn(90,1);
- //只有开始和结束时刻不同的时间段,才有效
- if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)||(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
- {
- if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)&& difftime(mktime(&now_stamp),mktime(&cul_sh1_stamp)) > 0)
- {
- device_status.work_status = 1;
- ctr_Collect_Fan(ON);
- //当前时间戳小于第一段结束时间戳
- while(difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp))>0)
- {
- time(&now);
- now_stamp = *localtime(&now);
- cul_eh1_stamp = *localtime(&now);
- cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
- cul_eh1_stamp.tm_min = 0;
- cul_eh1_stamp.tm_sec = 0;
- if(device_status.run_sta!=1) break;
- LOG_I("1===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh1_stamp));
- rt_thread_mdelay(10000);
- }
- }
- else {
- device_status.work_status=0;
- ctr_Collect_Fan(OFF);
- LOG_I("未到收集1时间!");
- }
- if(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp),mktime(&cul_sh2_stamp)) > 0)
- {
- device_status.work_status = 1;
- ctr_Collect_Fan(ON);
- while(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp))>0)
- {
- time(&now);
- now_stamp = *localtime(&now);
- cul_eh2_stamp = *localtime(&now);
- cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
- cul_eh2_stamp.tm_min = 0;
- cul_eh2_stamp.tm_sec = 0;
- if(device_status.run_sta!=1) break;
- LOG_I("2===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh2_stamp));
- rt_thread_mdelay(10000);
- }
- }
- else {
- device_status.work_status=0;
- ctr_Collect_Fan(OFF);
- LOG_I("未到收集2时间!");
- }
- }
- else
- {
- LOG_I("this collect time is 0s,The wait time cannot be negative!");
- }
- }
- if(device_status.run_sta==2)
- {
- ctr_Collect_Fan(OFF);
- device_status.develop_flag = 1;
- device_status.work_status = 2;
- ctr_develop(); //培养
- ctr_turn(90,3);
- }
- if(device_status.run_sta==3)
- {
- ctr_Collect_Fan(OFF);
- device_status.work_status = 3;
- device_status.autotakepic=1; //对焦拍照
- }
- //在培养之前再次确认时间戳
- time(&now);
- now_stamp = *localtime(&now);
- cul_eh2_stamp = *localtime(&now);
- cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
- cul_eh2_stamp.tm_min = 0;
- cul_eh2_stamp.tm_sec = 0;
- //当前时间戳大于收集结束时间戳并且run_sta==1
- if(device_status.run_sta==1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
- {
- ctr_Collect_Fan(OFF);
- LOG_I("===============collect ok,turn to drop!!!");
- device_status.develop_flag=1;
- device_status.work_status=2; //现在是培养
- DTU_Write("develop.mp3");
- if(device_status.run_sta==1)
- ctr_turn(90,2); /*转盘到培养位置*/
- rt_thread_delay(1000);
- ctr_shake(ON,device_status.shake_sec);
- rt_thread_delay(1000);
- ctr_develop(); //培养
- if(device_status.run_sta==2)
- ctr_turn(90,3); /*转盘到拍照位置*/
- rt_thread_delay(1000);
- device_status.work_status=3; //现在是拍照
- device_status.autotakepic=1; //对焦拍照
- while(device_status.autotakepic==0)
- {
- rt_thread_delay(1000);
- LOG_I("正在对焦拍照............");
- }
- device_status.work_status = 0; //待机
- DTU_Write("wait.mp3");
- LOG_I("=====================work finished==================");
- }
- rt_thread_delay(1000);
- }
- device_status.work_status = 0; //待机
- LOG_W("==================!!!设备已待机!!!===================");
- }
- void WorkTask_BZYV2(void)
- {
- //打开采集风机
- ctr_Collect_Fan(ON);
- //打开胶带步进电机
- //等待采集结束
- //拍照
- //打开相机供电
- //打开补光灯
- //滑台归位
- //滑台启动
- }
- /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ====================
- * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。
- * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(105s) + 仓门动作预留(40s)。
- * 实际存在仓门寻位、恒温等待、异常提前退出等偏差,故进度上限钳到 99%,
- * 与 t1~t3/t5/t6 的实时倒计时配合显示即可反映真实过程;非工作态返回 0。 */
- void Work_Progress_Start(void)
- {
- uint32_t total;
- total = (uint32_t)device_status.clt_time * 60
- + (uint32_t)device_status.heattime * 60
- + WORK_PHOTO_SEC + WORK_CLEAR_SEC + WORK_DOOR_SEC;
- if (total == 0) total = 1;
- if (total > 0xFFFF) total = 0xFFFF; /* work_total_sec 为 uint16_t */
- device_status.work_total_sec = (uint16_t)total;
- device_status.work_start_tick = rt_tick_get();
- device_status.work_progress = 0;
- /* 新的一轮工作开始,清掉上一轮的实时倒计时 */
- device_status.work_heat_left = 0;
- device_status.work_clt_left = 0;
- device_status.work_door_left = 0;
- device_status.photo_left = 0;
- device_status.clear_left = 0;
- }
- uint8_t Work_Progress_Get(void)
- {
- uint32_t elapsed;
- uint32_t total = device_status.work_total_sec;
- if (device_status.work_status != 1 || device_status.work_start_tick == 0 || total == 0)
- {
- return 0;
- }
- elapsed = (rt_tick_get() - device_status.work_start_tick) / RT_TICK_PER_SECOND;
- device_status.work_progress = (uint8_t)((elapsed >= total) ? 99 : (elapsed * 100 / total));
- return device_status.work_progress;
- }
- //工作任务流程s
- void WorkTask(void)
- {
- static int clt_time = 0; //收集时间差秒数
- static int heat_time = 0;
- static uint8_t workloop = 0;
- LOG_I("工作状态:%d",device_status.work_status);
- LOG_I("开关机状态:%d",device_status.ds);
- 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)
- {
- workloop++;
- device_status.work_done = 0;
- /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长,
- * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。放在本轮循环体开头,
- * 保证 24 小时/时控模式下连续多轮工作时进度与倒计时每轮都重新开始。 */
- Work_Progress_Start();
- if(clt_time==0)
- {
- #if 0
- if(device_status.version_type==41&&atoi(device_status.power)<350)
- {
- LOG_W("相机电流不足!");
- char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
- DTU_Write("%s",ctr_hk_p_warn);
- rt_free(ctr_hk_p_warn);
- }
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_PUT(ON);
- #endif
- DTU_Write("workstart.mp3");
- }
- //1.打开灯管
- #if LAMP_EN == 1
- /* 三灯管定制:首次进入工作流程时启动轮换计时(此后跨轮次保持相位),
- * 由 Lamp_Cycle_Update 按 LAMP_ROTATE_SEC 依次点亮 光源1/2/3 */
- if(s_lamp_cycle_tick == 0)
- {
- Lamp_Cycle_Start();
- }
- Lamp_Cycle_Update();
- #else
- ctr_lamp(ON,1);
- if(device_status.version_type==12)
- {
- ctr_Heat(ON);
- }
- #endif
- LOG_I("打开灯管!!!");
- #if 0
- rt_thread_delay(2000);
- if(atoi(device_status.power)<450)
- {
- LOG_I("Current is:%d",atoi(device_status.power));
- if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
- {
- char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
- DTU_Write("%s",lamp_err_warn);
- rt_free(lamp_err_warn);
- }
- }
- #endif
- if(device_status.version_type!=5)
- {
- rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
- //2.打开上仓门
- LOG_I("打开上仓门!!!");
- device_status.ctr_up_motor = 1;
- //3.收集
- clt_time = device_status.clt_time * 60;
- /* 防御:参数异常(clt_time<=0)时不再进入无限倒计时(原实现 while(clt_time!=1) 会一直递减为负数) */
- if(clt_time <= 0)
- {
- LOG_E("invalid clt_time(%d), skip collecting!", device_status.clt_time);
- }
- while(clt_time>1)
- {
- device_status.collt_status = 1;
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(ON);
- #endif
- clt_time --;
- device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */
- rt_thread_delay(1000);
- #if 0//LAMP_EN == 0
- if(clt_time%30==0)
- ctr_Fan_PUT(OFF);
- #endif
- rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
- HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"",device_status.clt_time,clt_time);
- //如果触发异常
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- HMI_WRITE("workSta.g0.txt=\"异常退出\"");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- HMI_WRITE("workSta.g0.txt=\"异常退出\"");
- break;
- }
- }
- device_status.collt_status = 0;
- clt_time = 1;
- device_status.work_clt_left = 0; /* 收集结束,倒计时清零 */
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- HMI_WRITE("workSta.g0.txt=\"异常退出\"");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- HMI_WRITE("workSta.g0.txt=\"异常退出\"");
- break;
- }
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- //4.关闭上仓门
- device_status.ctr_up_motor = 2;
- rt_kprintf("关闭上仓门!!!\n");
- HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
- while(device_status.ctr_up_motor!=0)
- {
- rt_thread_delay(100);
- LOG_I("等待上仓门关闭!!!");
- }
- //5.加热
- if(device_status.heat_sw==0&&device_status.version_type!=12)
- {
- calorstat(device_status.heattime*60);
- }
- else {
- heat_time = device_status.heattime * 60;
- while(heat_time!=0)
- {
- heat_time--;
- device_status.work_heat_left = (uint16_t)heat_time; /* 屏幕 workSta.t1 剩余加热时间(s) */
- LOG_I("正在加热状态!!!");
- rt_thread_delay(1000);
- }
- }
- device_status.work_heat_left = 0;
- //6.打开下仓门
- if(device_status.rain_status==0)
- {
- uint16_t cnt_photo = 0;
- while(device_status.photo_ws==1) //如果正在拍照,要等拍照结束
- {
- rt_thread_delay(1000);
- cnt_photo++;
- if(cnt_photo>=60) break;
- }
- device_status.ctr_down_motor =1;
- }
- else {
- device_status.ctr_down_motor = 2;
- device_status.ctr_up_motor = 2;
- }
- //待下仓门完全打开
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门打开!!!\n");
- HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
- }
- rt_thread_delay(3000);
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
- }
- //2022-04-12add:
- LOG_I("打开上仓门!!!");
- HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
- if(device_status.rain_status==0)
- {
- device_status.ctr_up_motor = 1;
- }
- if(device_status.version_type==4||
- device_status.version_type==41||
- device_status.version_type==42||
- device_status.version_type==43||
- device_status.version_type==12)
- {
- //震动
- ctr_shake(ON, device_status.shake_sec);
- device_status.photo_ws = 1;
- }
- #if DZ_260519 == 1
- if(workloop==3&&device_status.clt_time==10&&device_status.heattime==10){
- workloop = 0;
- device_status.clear_status = 2;
- }
- #endif
- }
- }
- if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
- {
- rt_kprintf("-------------------not work--------------------\n");
- device_status.work_status = 0;
- }
- //工作结束
- if(device_status.work_done==0&&device_status.work_status==0)
- {
- DTU_Write("workdone");
- HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
- ResetInit();
- clt_time = 0;
- upload_sys_monitor_info();
- }
- rt_kprintf("***not work-device status:%d\n",device_status.ds);
- }
- /* 入口函数-工作状态检测 */
- static void Work_status_thread_entry(void *parameter)
- {
- static uint16_t sun_status_num = 120;
- //如果是光控定时,进行光控监测的消抖
- if(device_status.timeSwitch==0&&device_status.sun_status==0)
- {
- rt_kprintf("光控定时!!!\n");
- while(sun_status_num!=0)
- {
- sun_status_num--;
- rt_thread_delay(1000);
- rt_kprintf("开机光控检测:%d\n",sun_status_num);
- }
- #if LAMP_EN == 0
- device_status.light_over_tim = device_status.sunBar * 3600;
- #endif
- device_status.test_flag = 0;
- }
- while(1)
- {
- WorkCheck();
- }
- }
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- rt_thread_delay(1000);
- ResetInit();
- while(1)
- {
- if(device_status.dtype==7)
- {
- WorkTask_BZY();
- }
- if(device_status.version_type!=6&&device_status.dtype==3)
- {
- WorkTask();
- }
- if(device_status.version_type==6)
- {
- WorkTask_XCT();
- }
- rt_thread_delay(1000);
- }
- }
- /*入口函数清扫*/
- static void Control_Clear_thread_entry(void *parameter)
- {
- static int selfcheck_cnt = 0;
- float voltage_f = 0.0;
- while(1)
- {
- #if ROBOT_EN ==1
- ctr_Motor_Robot(ON);
- rt_thread_delay(100);
- #else
- #if Color_EN ==1
- /*复位*/
- if(Read_Color_Key(KEY_RESET)==1)
- {
- ctr_Color_board(RESET, 1200);
- rt_thread_delay(5000);
- }
- #else
- if(device_status.selfcheck_ws==1){
- selfcheck_cnt ++;
- if(selfcheck_cnt==1){
- ctr_lamp(ON,1);
- rt_thread_delay(1000);
- device_status.ctr_up_motor = 1;
- device_status.ctr_down_motor = 1;
- //记录当前电压
- voltage_f = atof(device_status.voltage);
- rt_thread_delay(1500);
- //开启加热
- ctr_Heat(ON);
- }
- if(selfcheck_cnt==50){
- double voltage_n = atof(device_status.voltage);
- //判断当前电压和加热前的电压差
- if(voltage_f-voltage_n<0.5){
- device_status.heat_sta = 1; //加热异常
- //上报预警,加热故障
- char *heat_warn = makeJson_Warn("加热报警", "heat-error");
- DTU_Write("%s", heat_warn);
- rt_free(heat_warn);
- }else{
- device_status.heat_sta = 0;
- }
- rt_thread_delay(1000);
- //关闭加热
- ctr_Heat(OFF);
- //振动
- ctr_shake(ON, 1);
- //device_status.clear_status = 1;
- ctr_lamp(OFF, 1);
- }
- if(selfcheck_cnt>1800){ //超过3分钟
- device_status.selfcheck_ws = 0;
- device_status.ctr_up_motor = 0;
- device_status.ctr_down_motor = 0;
- //
- device_status.camsta = 1; //相机异常
- device_status.led_sta = 1;
- //上报自检结果
- //上报自检结果
- char *json_temp = makeJson_selfCheck_(1);
- DTU_Write("%s",json_temp);
- free(json_temp);
- }
- }
- if(device_status.selfcheck_ws==0){
- selfcheck_cnt = 0;
- }
- if(device_status.clear_status==1)
- {
- ///if(ctr_clear(1200)==0)
- ctr_TG_Clear(500); //2025-1021,由400修改为500,解决金朵推杆慢导致行程走不完
- system_status.cleaning_count +=1; //清扫次数
- }
- if(device_status.clear_status==2){
- turnzp();
- device_status.clear_status=0;
- }
- rt_thread_delay(100);
- #endif
- #endif
- }
- }
- /*入口函数加热*/
- static void Control_Heat_thread_entry(void *parameter)
- {
- while(1)
- {
- if(device_status.work_status==1&&device_status.heat_sw==1)
- {
- calorstat(device_status.heattime*60);
- rt_thread_delay(10000);
- }
- rt_thread_delay(1000);
- }
- }
- /*清扫线程*/
- int Control_clear(void)
- {
- /* 创建线程*/
- tid_ctr = rt_thread_create("Control_Clear_thread_entry",
- Control_Clear_thread_entry, RT_NULL,
- 2048, //2048
- 24, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_ctr!=RT_NULL)
- {
- rt_thread_startup(tid_ctr);
- }
- return 0;
- }
- /*恒温加热线程*/
- int Control_heat(void)
- {
- /* 创建线程*/
- tid_heat = rt_thread_create("Control_Heat_thread_entry",
- Control_Heat_thread_entry, RT_NULL,
- 1024, //2048
- 25, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_heat!=RT_NULL)
- {
- rt_thread_startup(tid_heat);
- }
- return 0;
- }
- /* 创建线程 -工作状态检测*/
- int Workstatus_check(void)
- {
- /* 创建线程*/
- tid_wkstc = rt_thread_create("work_status_check_thread",
- Work_status_thread_entry, RT_NULL,
- WORK_CHECK_THREAD_STACK_SIZE, //1024
- WORK_CHECK_THREAD_PRIORITY, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_wkstc!=RT_NULL)
- {
- rt_thread_startup(tid_wkstc);
- }
- return 0;
- }
- /*工作流程线程*/
- int Work_Loop_Task(void)
- {
- /* 创建线程*/
- tid_workloop = rt_thread_create("work_loop_thread",
- Work_Loop_thread_entry, RT_NULL,
- WORK_LOOP_THREAD_STACK_SIZE, //2048
- WORK_LOOP_THREAD_PRIORITY, //12
- WORK_LOOP_THREAD_TIMESLICE); //5
- #if Color_EN == 0
- if(tid_workloop!=RT_NULL)
- {
- rt_thread_startup(tid_workloop);
- }
- #endif
- return 0;
- }
- /* 注意:以下 3 个函数签名与 FinSH 的 (int argc, char **argv) 不匹配,
- * 直接导出会导致串口执行时参数为 argc/argv 垃圾值(命令无效果或随机动作),
- * 已统一替换为下方的 ctr_test 命令,此处不再导出。 */
- //MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
- //MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
- //MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
- // RTU/DTU 电源控制 shell 命令
- static void ctr_rtu_dtu(int argc, char** argv)
- {
- if(argc != 2)
- {
- rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
- rt_kprintf(" ON : RTU/DTU Power ON\n");
- rt_kprintf(" OFF : RTU/DTU Power OFF\n");
- rt_kprintf(" 0 : ON-OFF Pulse (10ms interval)\n");
- return;
- }
-
- if(strcmp(argv[1], "ON") == 0 || strcmp(argv[1], "on") == 0)
- {
- ctr_RTU_DTU(ON);
- rt_kprintf("RTU/DTU Power ON\n");
- }
- else if(strcmp(argv[1], "OFF") == 0 || strcmp(argv[1], "off") == 0)
- {
- ctr_RTU_DTU(OFF);
- rt_kprintf("RTU/DTU Power OFF\n");
- }
- else if(strcmp(argv[1], "0") == 0)
- {
- rt_kprintf("RTU/DTU ON-OFF Pulse (10ms interval)\n");
- ctr_RTU_DTU(OFF);
- rt_thread_mdelay(1);
- ctr_RTU_DTU(ON);
- rt_kprintf("Pulse complete\n");
- }
- else
- {
- rt_kprintf("Invalid parameter: %s\n", argv[1]);
- rt_kprintf("Usage: ctr_rtu_dtu <ON|OFF|0>\n");
- }
- }
- /* ==================== 测试/调试用命令行接口 ====================
- * 背景:原代码将控制函数直接 MSH_CMD_EXPORT(ctr_lamp / ctr_led / ctr_clear / HardWare_Check),
- * 但这些函数的签名是 (uint8_t[,uint8_t]),与 FinSH 的 (int argc, char **argv) 不匹配,
- * 从串口执行时参数实为 argc/argv 的垃圾值 —— 命令要么无任何效果(实测 ctr_led on 不生效)、
- * 要么以随机参数动作。现统一改为 argv 形式的 ctr_test,并移除上述有风险的直接导出。
- */
- static int ctr_test(int argc, char **argv)
- {
- const char *item;
- const char *act;
- if (argc < 3)
- {
- rt_kprintf("usage: ctr_test <item> <act> [val]\n"
- " lamp <1|2|3> <on|off> | led <on|off> | clear <on|off>\n"
- " shake <on|off> [sec] | heat <on|off> | cold <on|off>\n"
- " up <on|off> | down <on|off> | hwinit | hwcheck\n");
- return -1;
- }
- item = argv[1];
- act = argv[2];
- if (strcmp(item, "lamp") == 0)
- {
- int num = atoi(act);
- const char *onoff = (argc >= 4) ? argv[3] : "off";
- ctr_lamp((strcmp(onoff, "on") == 0) ? ON : OFF, (uint8_t)num);
- rt_kprintf("[ctr_test] lamp%d %s\n", num, onoff);
- }
- else if (strcmp(item, "led") == 0)
- {
- ctr_led((strcmp(act, "on") == 0) ? ON : OFF);
- rt_kprintf("[ctr_test] led %s\n", act);
- }
- else if (strcmp(item, "clear") == 0)
- {
- /* 置标志位由清扫线程执行(非阻塞),off 用于停止 */
- device_status.clear_status = (strcmp(act, "on") == 0) ? 1 : 0;
- rt_kprintf("[ctr_test] clear %s (clear_status=%d)\n", act, device_status.clear_status);
- }
- else if (strcmp(item, "shake") == 0)
- {
- uint16_t sec = (argc >= 4) ? (uint16_t)atoi(argv[3]) : 0;
- ctr_shake((strcmp(act, "on") == 0) ? ON : OFF, sec);
- rt_kprintf("[ctr_test] shake %s %d\n", act, sec);
- }
- else if (strcmp(item, "heat") == 0)
- {
- ctr_Heat((strcmp(act, "on") == 0) ? ON : OFF);
- rt_kprintf("[ctr_test] heat %s\n", act);
- }
- else if (strcmp(item, "cold") == 0)
- {
- ctr_Cold((strcmp(act, "on") == 0) ? ON : OFF);
- rt_kprintf("[ctr_test] cold %s\n", act);
- }
- else if (strcmp(item, "up") == 0)
- {
- device_status.ctr_up_motor = (strcmp(act, "on") == 0) ? 1 : 2;
- rt_kprintf("[ctr_test] up %s\n", act);
- }
- else if (strcmp(item, "down") == 0)
- {
- device_status.ctr_down_motor = (strcmp(act, "on") == 0) ? 1 : 2;
- rt_kprintf("[ctr_test] down %s\n", act);
- }
- else if (strcmp(item, "hwinit") == 0)
- {
- HardWare_Init(0);
- rt_kprintf("[ctr_test] HardWare_Init done\n");
- }
- else if (strcmp(item, "hwcheck") == 0)
- {
- HardWare_Check(1);
- rt_kprintf("[ctr_test] HardWare_Check(manual) done\n");
- }
- else
- {
- rt_kprintf("[ctr_test] unknown item: %s\n", item);
- return -1;
- }
- return 0;
- }
- MSH_CMD_EXPORT(ctr_test, ctr_test <item> <act> [val] : 外设测试命令(lamp/led/clear/shake/heat/cold/up/down));
- /* 串口注入命令:配合 TX-RX 自环回,可从控制台向 MCU 注入一路串口数据。
- * - dtu_send:向 uart2 发送字符串(环回后等于"RTU 下发报文",可下发 JSON-RPC)
- * - hmi_send:向 uart4 发送字符串(环回后等于"屏幕下发指令")
- * 注意:字符串不要包含空格(FinSH 以空格分参)。 */
- static int dtu_send(int argc, char **argv)
- {
- if (argc < 2)
- {
- rt_kprintf("usage: dtu_send <string> 例: dtu_send {\"cmd\":\"read\"}\n");
- return -1;
- }
- if (dtuserial == RT_NULL)
- {
- rt_kprintf("[dtu_send] uart2 not ready\n");
- return -1;
- }
- rt_kprintf("[dtu_send] %s\n", argv[1]);
- rt_device_write(dtuserial, 0, argv[1], rt_strlen(argv[1]));
- return 0;
- }
- MSH_CMD_EXPORT(dtu_send, uart2 发送字符串(自环回时用于向MCU注入报文));
- static int hmi_send(int argc, char **argv)
- {
- if (argc < 2)
- {
- rt_kprintf("usage: hmi_send <string> 例: hmi_send getFullData\n");
- return -1;
- }
- if (serial == RT_NULL)
- {
- rt_kprintf("[hmi_send] uart4 not ready\n");
- return -1;
- }
- rt_kprintf("[hmi_send] %s\n", argv[1]);
- rt_device_write(serial, 0, argv[1], rt_strlen(argv[1]));
- return 0;
- }
- MSH_CMD_EXPORT(hmi_send, uart4 发送字符串(自环回时用于向MCU注入屏幕指令));
- MSH_CMD_EXPORT(ctr_rtu_dtu,ctr_rtu_dtu<ON|OFF>);
- MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
|