| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733 |
- #include <control.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
- * 2025-06-26 RTC闹钟使用定时开启和关闭路由器供电
- */
- #include "beep.h"
- #include "hmi.h"
- #include "easyflash.h"
- #include "stdio.h"
- #include "sound.h"
- #include "dtu_uart2.h"
- #include "sht30_collect.h"
- #include <user_mb_app.h>
- #include <selfcheck.h>
- #ifdef RT_USING_FINSH
- #include <shell.h> /* finsh_set_device(): uart_stop_for_ota 要把 finsh 摘回 uart1 */
- #endif
- #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"
- #include "param_mirror.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 //时间片
- #define PWM_EN 0 //0关闭,1-打开
- //设备状态表结构体
- /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */
- /* system_status 已收敛为全工程唯一定义(在 sys_monitor.c), 见 sys_monitor.h 的 extern 声明 */
- 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;
- rt_thread_t tid_485_play = RT_NULL;
- extern rt_device_t dtuserial; /*串口设备句柄*/
- rt_event_t event_485_play;
- extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
- /***************************************************************************/
- int thread_delete_workloop(void)
- {
- rt_err_t result;
- if(tid_workloop!=RT_NULL)
- {
- result = rt_thread_delete(tid_workloop);
- }
- return result;
- }
- int thread_restart_workloop(void)
- {
- rt_err_t result;
- result = Work_Loop_Task();
- return result;
- }
- /***************************************************************************/
- /* ==================== OTA 升级前的线程清理 ==================== */
- #define OTA_THREAD_MAX 24
- /* 保留名单: 这几类线程删掉反而会让升级失败 */
- static const char *g_ota_keep[] =
- {
- "syswatch", /* 喂狗线程: 删了 5s 后硬件看门狗就复位, 升级必中断 */
- "tshell", /* 控制台 */
- "tidle",
- "main",
- };
- /* 需要停掉的业务线程前缀(写完整名即可, 比较时自动按 RT_NAME_MAX-1 截断)。
- *
- * 为什么用"前缀匹配 + 遍历"而不是 rt_thread_find(全名):
- * rtconfig.h 里 RT_NAME_MAX = 8, 线程名被内核截断到 7 个字符 ——
- * "Control_Clear_thread_entry" / "Control_Check_thread_entry" 都变成 "Control";
- * "DTU_Uart_thread_send" / "DTU_Uart_thread_recv" 都截成 "DTU_Uar",
- * 而 recv 正是执行升级的当前线程, 用 rt_thread_find 有可能把调用者自己删掉。
- * 所以改成遍历线程对象链表, 并显式跳过 rt_thread_self()。 */
- static const char *g_ota_stop[] =
- {
- "work_loop_thread", /* 工作流程, LOG_I/rt_kprintf 密集 */
- "work_status_check_thread", /* WorkCheck, 每 10s 一批日志 */
- "Control_Clear_thread_entry", /* 清扫, 会驱动电机 */
- "Control_Check_thread_entry", /* 恒温, LOG_I("process") 每秒 */
- "thread_485play", /* 485 播报 */
- "adc_thread", /* ADC 采集, 每 5s 打 3 行 */
- "Power Check thread", /* 每秒读电流/电压 */
- "Hmi_Fresh_thread", /* 屏幕刷新 */
- "Ctr motor", /* 仓门/光源, 带 rt_kprintf */
- "sys_monitor", /* CPU/统计 */
- "param_sv", /* 参数落盘: 会擦 flash(4KB≈100ms) 抢 CPU */
- "DTU_Uart_thread_send", /* DTU 发送 (recv 是自身, 会被跳过) */
- "Uart5_thread_recv", /* 485 透传 */
- "md_m_poll", /* modbus 轮询 */
- "md_m_send", /* modbus 发送 */
- "usage", /* cpu_usage 临时线程 */
- };
- /* 名字前缀比较: 内核只存了前 RT_NAME_MAX-1 个字符, 所以最多比这么多 */
- static int ota_name_match(const char *thread_name, const char *prefix)
- {
- rt_size_t n;
- if (thread_name == RT_NULL || prefix == RT_NULL || thread_name[0] == '\0')
- {
- return 0;
- }
- n = rt_strlen(prefix);
- if (n > RT_NAME_MAX - 1)
- {
- n = RT_NAME_MAX - 1;
- }
- if (n == 0)
- {
- return 0;
- }
- return (rt_strncmp(thread_name, prefix, n) == 0) ? 1 : 0;
- }
- static int ota_in_list(const char *thread_name, const char **list, int cnt)
- {
- int i;
- for (i = 0; i < cnt; i++)
- {
- if (ota_name_match(thread_name, list[i]))
- {
- return 1;
- }
- }
- return 0;
- }
- /**
- * OTA 升级前把所有业务线程停掉, 只留 tshell / idle / syswatch。
- *
- * 为什么必须停:
- * 1) ymodem_ota() 会把 console 切到 uart2(ymodem_ota.c:117), 之后**任何线程**
- * 的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染 ymodem 字节流 -> 校验失败;
- * 2) adc / Power Check 每秒读 ADC, param_sv 会擦 flash(4KB 约 100ms),
- * 抢 CPU 和外设会让 ymodem 丢字节;
- * 3) Control_Clear / Control_Check 会驱动电机, 升级过程中不该动机械部件。
- *
- * 顺序: ① adc_thread 优雅退出(它可能正持有 ADC 设备锁)
- * ② 置 RAM 内退出请求, 让带 `update_flag==1 break` 的线程自己退
- * ③ 其余按前缀强删(先收集再删, 不能边遍历边删)
- *
- * @return 实际删除的线程数; 负值为错误
- */
- int thread_stop_all_for_ota(void)
- {
- struct rt_object_information *info;
- rt_list_t *node;
- rt_thread_t victims[OTA_THREAD_MAX];
- rt_thread_t self;
- int nv = 0, i, done = 0;
- info = rt_object_get_information(RT_Object_Class_Thread);
- if (info == RT_NULL)
- {
- LOG_E("[ota] thread object information NOT found");
- return -RT_ERROR;
- }
- /* ① adc_thread 必须优雅退出: 直接 rt_thread_delete 若刚好删在
- rt_adc_enable/read 中间, 它持有的 ADC 设备锁永远不会释放 */
- thread_delete_adc();
- /* ② 置 RAM 内退出请求。Control_Clear / Control_Check / thread_485play
- 三处都有 `if(device_status.update_flag==1) break;`。
- ★ 只改 RAM, 绝不写 flash / 参数快照 —— 一旦把 1 落盘, 下次开机会被
- hmi.c 的 up_flag_* 裁决判成"有升级请求", 而带 break 的线程就会
- 一开机直接退出(设备不清扫、不恒温)。
- 因为全程不落盘, ymodem 结束复位后这个 1 自然消失, 不需要额外清理。 */
- device_status.update_flag = 1;
- rt_thread_mdelay(300);
- /* ③ 收集待停线程。必须"先收集后删除": rt_thread_delete 会摘掉链表节点,
- 边遍历边删会把 object_list 走坏 */
- self = rt_thread_self();
- rt_enter_critical();
- rt_list_for_each(node, &info->object_list)
- {
- rt_thread_t t = rt_list_entry(node, struct rt_thread, list);
- if (t == self) /* 不能删自己 */
- {
- continue;
- }
- if (ota_in_list(t->name, g_ota_keep, sizeof(g_ota_keep) / sizeof(g_ota_keep[0])))
- {
- continue;
- }
- if (!ota_in_list(t->name, g_ota_stop, sizeof(g_ota_stop) / sizeof(g_ota_stop[0])))
- {
- continue; /* 不在名单里, 不动 */
- }
- if (nv < OTA_THREAD_MAX)
- {
- victims[nv++] = t;
- }
- }
- rt_exit_critical();
- /* ④ 逐个删除 */
- for (i = 0; i < nv; i++)
- {
- if (rt_thread_delete(victims[i]) == RT_EOK)
- {
- done++;
- }
- }
- LOG_I("[ota] stopped %d of %d candidate thread(s)", done, nv);
- return done;
- }
- /**
- * @brief 把 double 定点格式化成 "整数.两位小数" 字符串(不依赖 libc 浮点 printf)。
- *
- * 🔴 为什么必须有这个函数:
- * RT-Thread 的 rt_snprintf() 只是 rt_vsnprintf() 的薄封装, 而 rt_vsnprintf
- * (rt-thread/src/kservice.c:886) 的转换字符 switch 只有:
- * case 'c' / 's' / 'd' / 'i' / 'o' / 'u' / 'x' / 'X' / '%'
- * **没有 case 'f' / 'e' / 'g'**。rtconfig.h 也没打开 RT_USING_LIBC_FLOAT_PRINT。
- * 于是 "%.2f" 会一路走到 default: 分支(kservice.c:1090), 该分支的行为是
- * 把 '%' 和当前字符原样写进缓冲区, 然后 continue;
- * 结果就是缓冲区里得到字面量 **"%f"**(小数点 ".2" 也会被当普通字符写出去),
- * 而且**不消费对应的 double 可变参数** —— 后面若还有格式符, 参数会整体错位。
- * 这就是 2026-09-28 平台侧电流/电压显示成 "%f" 的根因。
- *
- * 也不能简单换回标准库 sprintf("%f"): newlib 的 float printf 默认不使能,
- * 要额外链入 _printf_float(体积大), 且原来 power[8] 装 "25000.00" 会越界。
- *
- * 这里的做法: 纯整数定点运算, 只用 %ld/%02ld, 任何 printf 实现都支持。
- * 代价是没有完整的 dtoa 舍入, 但先加 0.5 再强转已经等价于四舍五入到 0.01,
- * 电流电压这类量只要两位小数, 精度完全够。
- *
- * @param buf 输出缓冲(必须是数组, 不能是指针)
- * @param size 缓冲大小
- * @param val 待格式化的值
- * @param prec 小数位数, 内部钳到 0~2
- */
- void fmt_fixed2(char *buf, rt_size_t size, double val, int prec)
- {
- /* 上限钳位: 防止 val*100 溢出 long。20000.00 是电流/功率物理上够不到的上限。 */
- const double MAX_VAL = 20000.0;
- long scaled; /* val 放大 100 倍后的定点整数 */
- long ip, fp; /* 整数部分 / 小数部分 */
- int neg = 0;
- if (buf == RT_NULL || size == 0)
- {
- return;
- }
- if (prec < 0) { prec = 0; }
- if (prec > 2) { prec = 2; }
- if (val < 0.0)
- {
- neg = 1;
- val = -val;
- }
- if (val > MAX_VAL)
- {
- val = MAX_VAL;
- }
- /* 先统一放大 100 倍并四舍五入(加 0.5 后强转即等价于四舍五入到 0.01)。
- 这样 prec=0/1/2 三个分支都能直接取用, 不会出现 prec=1 时把 12.99
- 截断成 12.9 的问题 —— 那一档要的是四舍五入后的 13.0。 */
- scaled = (long)(val * 100.0 + 0.5);
- ip = scaled / 100;
- fp = scaled % 100;
- if (prec == 0)
- {
- /* 四舍五入到整数: fp >= 50 则进位 */
- if (fp >= 50)
- {
- ip += 1;
- }
- rt_snprintf(buf, size, "%s%ld", neg ? "-" : "", ip);
- }
- else if (prec == 1)
- {
- /* 四舍五入到 0.1: fp 的个位 >= 5 则进位到十分位 */
- if (fp % 10 >= 5)
- {
- fp += 10;
- if (fp >= 100)
- {
- fp -= 100;
- ip += 1;
- }
- }
- rt_snprintf(buf, size, "%s%ld.%ld", neg ? "-" : "", ip, fp / 10);
- }
- else
- {
- rt_snprintf(buf, size, "%s%ld.%02ld", neg ? "-" : "", ip, fp);
- }
- }
- /**
- * @brief 把一个串口设备彻底关掉(解引用到 ref_count==0), 用于 OTA 前收尾。
- *
- * 为什么不能只 rt_device_close() 一次:
- * rt_device_close() 只有在 ref_count 减到 0 时才会真正调用驱动的 close,
- * 否则只减计数就返回 RT_EOK。而 uart4/uart5 的引用不止一处:
- * uart4 → HMI_uart4_init() 开 1 次, 若现场跑过 Debug_log_ON(),
- * rt_console_set_device("uart4") + finsh_set_device("uart4")
- * 又各开 1 次(共 3 次);
- * uart5 → DTU_uart5_init() 开 1 次, UART_485_INIT() 再开 1 次,
- * freemodbus 的 xMBMasterPortSerialInit() 还会再开 1 次。
- * 所以必须循环 close 到 ref_count 归零, 否则设备仍在收发、ISR 仍会触发。
- *
- * @param dev 设备句柄, 可为 RT_NULL
- * @param name 名字, 仅用于日志
- */
- static void ota_close_uart(rt_device_t dev, const char *name)
- {
- rt_uint8_t guard = 8;
- if (dev == RT_NULL)
- {
- return;
- }
- /* ① 先摘掉接收回调。close() 内部也会清, 但清回调后到 close 之间
- 还有窗口期, 此刻若有数据到达, 回调里会 release 已经没人等的
- 信号量 / 往已删线程的消息队列里塞消息。 */
- rt_device_set_rx_indicate(dev, RT_NULL);
- /* ② 循环 close 到引用计数归零。guard 兜底, 防止内核行为异常时空转。 */
- while (dev->ref_count > 0 && guard-- > 0)
- {
- if (rt_device_close(dev) != RT_EOK)
- {
- break;
- }
- }
- LOG_I("[ota] %s closed, ref_count=%d", name, (int)dev->ref_count);
- }
- /**
- * @brief OTA 升级前关闭串口4(HMI屏) 和串口5(485)。
- *
- * 为什么要关:
- * 1) ymodem_ota() 内部会 rt_console_set_device("uart2"), 之后 console 指向
- * uart2。此时 uart4/uart5 若还在工作, 它们的 ISR、以及仍可能残留的
- * 485 播报/屏幕刷新, 都会和 ymodem 抢 CPU 与中断, 导致传输丢字节;
- * 2) 485 是**半双工**总线, 收发器被驱动着去收 ymodem 期间还挂着一个
- * 从机在等应答, 升级完复位后从机状态与 MCU 不同步, 现场表现为
- * "升级完 485 读不到数据";
- * 3) 屏幕在升级过程中继续刷新, 白白占用带宽, 且部分屏会因长时间收不到
- * 指令而重新初始化自身。
- *
- * ★ 本函数必须在 thread_stop_all_for_ota() **之后**调用:
- * 先停线程(没人再发数据) → 再关设备。反过来的话, 关设备的瞬间还有
- * 线程在 HMI_WRITE/Uart5_Write, 会写到已被 disable 的 fifo 上。
- *
- * @return 关闭的串口个数
- */
- int uart_stop_for_ota(void)
- {
- int n = 0;
- /* 置标志必须**先于**关设备: 中间还有别的线程可能正在 HMI_WRITE,
- 看到标志后直接 return, 不会碰到正在被拆掉的 fifo。 */
- g_hmi_port_closed = 1;
- /* ---- 串口4: HMI 屏 ----
- 先把 console / finsh 摘回 uart1, 否则它们各自持有 uart4 的一次 open
- (rt_console_set_device / finsh_set_device 内部都是 open 新的 + close 旧的),
- 引用计数降不到 0, 设备等于没关; 而且 tshell 正阻塞在 uart4 的 read 上,
- 设备被关后它会一直读到 0。 */
- if (rt_console_get_device() != RT_NULL &&
- rt_strcmp(rt_console_get_device()->parent.name, UART_NAME) == 0)
- {
- rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
- }
- #if defined(RT_USING_FINSH) && !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
- finsh_set_device(RT_CONSOLE_DEVICE_NAME);
- #endif
- if (serial != RT_NULL)
- {
- ota_close_uart(serial, UART_NAME);
- n++;
- }
- /* ---- 串口5: 485 ---- */
- if (serial_485 != RT_NULL)
- {
- ota_close_uart(serial_485, "uart5");
- n++;
- }
- /* 把句柄置 NULL: 让上层即使漏判 g_hmi_port_closed 也不会往已废设备写。
- ⚠️ 置 NULL 之后 ymodem 失败返回也不会有任何 HMI/485 输出, 这是预期的
- ——升级流程本来就要求这段时间对外静默。 */
- serial = RT_NULL;
- serial_485 = RT_NULL;
- LOG_I("[ota] uart4/uart5 stopped (%d)", n);
- return n;
- }
- /****************************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);
- }
- /*************************吸虫塔*********************************/
- //吸虫电机
- 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
- *************************************************/
- 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);
- #if PCB_V1 == 1
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- #else
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- #endif
- }
- if(status_up==CLOSE)
- {
- device_status.up_status = 0;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- #if PCB_V1 == 1
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- #else
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- #endif
- }
- 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;
- }
- //震动电机
- uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
- {
- 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)
- {
- for(uint8_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);
- }
- if(device_status.dtype==7)
- HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
- return status_shake;
- }
- //补光
- uint8_t ctr_led(uint8_t status_led)
- {
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- if(status_led==ON)
- {
- rt_pin_write(LED_SW,ON);
- HMI_WRITE("sysTest.switchFill.val=%d",ON);
- }
- else if(status_led==OFF){
- rt_pin_write(LED_SW,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);
- /* ★ 原来 DTU_Write(turn_warn) 把 JSON 当格式串(含 '%' 就会被解析) -> 走 dtu_write_json */
- dtu_write_json(makeJson_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));
- /* 转盘位置变了, 同步进参数镜像(掉电后能恢复到正确仓位) */
- param_mirror_sync();
- }
- 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");
- }
- /*
- *
- * */
- void ctr_Motor_Driver_Power(uint8_t status_ws)
- {
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- rt_pin_write(SHAKE_SW, status_ws);
- }
- /*************************************************
- 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);
- 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 --;
- }
- beep_set(0, 0); //200kzh - 50%
- rt_thread_delay(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)
- {
- LOG_W("转仓执行!");
- 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)
- {
- ctr_Collect_Fan(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);
- ctr_Collect_Fan(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);
- ctr_Collect_Fan(OFF);
- break;
- }
- else {
- break;
- }
- }
- ctr_Collect_Fan(OFF);
- turn_timeout = 0;
- HMI_WRITE("cangWei.rotateFlag.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);
- //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- if(status_router==ON)
- {
- rt_pin_write(ROUTER,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- }
- }
- //清扫-推杆电机
- 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,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- #if QINGSAO_IO == 1
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- #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,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- #if QINGSAO_IO == 1
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- #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 (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);
- }
- }
- // 冷却完成,执行开机
- 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)
- {
- /* 2. 【关键】关闭串口设备。
- * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
- * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
- */
- rt_device_close(dtuserial);
- /* 可选:如果 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);
- /* 3. 【关键】重新打开串口设备。
- * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为
- * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。
- */
- GPIO_InitTypeDef gpioInitStruct;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- //PA2 TXD
- gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式
- gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9
- gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率
- HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
- //PA3 RXD
- gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式
- gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10
- gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率
- HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
- #ifdef RT_USING_SERIAL_V2
- rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
- if(uart_res!=RT_EOK)
- {
- LOG_E("%s打开失败!",RTU_UART_NAME);
- }else{
- LOG_I("%s Open Success!",RTU_UART_NAME);
- }
- #endif
- #ifdef RT_USING_SERIAL_V1
- rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
- #endif
- /* 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)
- {
- device_status.C01 &= ~(1<<1); //加热 正常
- 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;
- }
- }
- delay_heat = 60;
- //跳出FOR循环
- n = n + 60;
- }
- if(device_status.heattem>device_status.run_sta + 1){
- device_status.heat_tem_max = device_status.heattem;
- }
- 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) //加热5分钟温度低于50,判定加热故障
- {
- if(device_status.heattem<50) {
- device_status.C01 |= (1 << 1); // 第1位变为1,加热故障
- }else {
- device_status.C01 &= ~(1<<1); //加热 正常
- }
- }
- }
- /* 屏幕 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.work_mode==1){
- LOG_W("切换模式,退出加热!");
- 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 HK_TakePhoto(void)
- {
- //打开补光灯
- //DTU_Write("takephoto.mp3");
- rt_thread_delay(10000);
- rt_timer_start(timer_takephoto);
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- //吸虫塔
- if(device_status.version_type!=6)
- {
- ctr_led(ON);
- if(device_status.version_type==48)
- {
- rt_thread_delay(1000*60);
- }
- else {
- rt_thread_delay(1000*15);
- }
- }
- //触发海康相机拍照
- 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;
- ctr_lamp(OFF,1);
- }
- 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.version_type!=6&&device_status.version_type!=7)
- {
- device_status.work_done = 1;
- //1.打开路由器供电
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- ctr_RTU_DTU(ON);
- //3.关闭镇流器
- ctr_lamp(OFF,1);
- //4.关闭震动电机
- ctr_shake(OFF, 0);
- //5.关闭加热
- ctr_Heat(OFF);
- //6.关闭补光灯
- if(device_status.photo_ws==0)
- ctr_led(OFF);
- 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(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
- {
- if(device_status.photo_ws==0) device_status.clear_status=1;
- while(device_status.clear_status==1){
- rt_thread_delay(1000);
- }
- #if JINGJIKUAN == 1
- //再执行4次清扫
- for(int i=0;i<4;i++)
- {
- if(device_status.photo_ws==0) device_status.clear_status=1;
- while(device_status.clear_status==1){
- rt_thread_delay(1000);
- }
- }
- #endif
- }
- }
- else {
- if(device_status.power_status==1)
- {
- //1.打开路由器供电
- #if MATCH_DEVICE ==0
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- #endif
- ctr_RTU_DTU(ON);
- }
- }
- }
- /*三色光源*/
- void Led_Check(uint8_t tp)
- {
- if(tp==3)
- {
- if(device_status.light_over_tim>2*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<1*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- else if (tp==6) {
- if(device_status.light_over_tim>4*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- else if (tp==9) {
- if(device_status.light_over_tim>6*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<3*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- }
- void WorkCheck(void)
- {
- static rt_uint32_t light_timeout = 3600;
- 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("***********时控*****************");
- time_t now;
- time(&now);
- struct tm now_tm = *localtime(&now);
- if(device_status.startBar == device_status.stopBar)
- {
- // 开始结束时间相同则24小时模式
- device_status.work_status = 1;
- rt_thread_delay(10000);
- 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);
- continue;
- }
- 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 (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);
- rt_thread_delay(10000);
- }
- else // 光控模式
- {
- LOG_I("光控定时模式!");
- if(device_status.sun_status == 0 && 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 = 0;
- light_timeout = 3600; // 重置防止负数
- device_status.light_over_tim = device_status.sunBar * 3600;
- }
- }
- }
- rt_thread_delay(1000);
- }
- // 异常状态处理
- device_status.work_status = 0;
- rt_thread_delay(1000);
- }
- //工作任务流程s
- /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ====================
- * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。
- * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(85s) + 仓门动作预留(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;
- }
- void WorkTask(void)
- {
- static int clt_time = 0; //收集时间差秒数
- static int heat_time = 0;
- LOG_I("工作状态:%d",device_status.work_status);
- LOG_I("开关机状态:%d",device_status.ds);
- while(device_status.clear_status==1){
- LOG_I("等待清扫执行完成....");
- rt_thread_delay(1000);
- }
- 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)
- {
- if(device_status.work_mode==1){
- break;
- }
- device_status.work_done = 0;
- /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长,
- * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。 */
- Work_Progress_Start();
- if(clt_time==0)
- {
- DTU_Write("workstart.mp3");
- if(device_status.debug_ws==1)
- {
- time_t stamp=time(RT_NULL);
- static struct tm tm;
- gmtime_r(&stamp, &tm);
- rt_sprintf(device_status.stamp,"%04d%02d%02d%02d%02d%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
- dtu_write_json(makeJson_Warn("Work start!", device_status.stamp));
- }
- }
- ctr_lamp(ON,1);
- LOG_I("打开灯管!!!");
- 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;
- while(clt_time!=1)
- {
- clt_time --;
- device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */
- rt_thread_delay(1000);
- rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
- if(device_status.work_mode==1){
- break;
- }
- //如果触发异常
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF,1);
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- }
- 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 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- //4.关闭上仓门
- device_status.ctr_up_motor = 2;
- rt_kprintf("关闭上仓门!!!\n");
- while(device_status.ctr_up_motor!=0)
- {
- rt_thread_delay(100);
- LOG_I("等待上仓门关闭!!!");
- }
- //5.加热
- if(device_status.heat_sw==0)
- {
- calorstat(device_status.heattime*60);
- }
- else {
- heat_time = device_status.heattime * 60;
- while(heat_time!=0)
- {
- if(device_status.work_mode==1)
- {
- break;
- }
- 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;
- #if TG_MODULE == 0
- //6.打开下仓门
- if(device_status.rain_status==0)
- {
- 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");
- }
- rt_thread_delay(3000);
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- }
- //2022-04-12add:
- LOG_I("打开上仓门!!!");
- device_status.ctr_up_motor = 1;
- #else
- //打开下仓门
- ctr_cangmen_down(OPEN);
- uint16_t timeout_down_open = 0;
- while(1)
- {
- timeout_down_open++;
- if(rt_pin_read(CTR_UPC_LIMIT)==1||timeout_down_open>20*20) break;
- rt_thread_delay(50);
- }
- ctr_cangmen_down(OFF);
- rt_thread_delay(1000);
- #endif
- if(device_status.work_mode==1){
- break;
- }
- if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
- {
- //震动
- ctr_shake(ON, device_status.shake_sec);
- //补光&&拍照
- LOG_I("拍照!!!");
- device_status.photo_ws = 1;
- }
- #if TG_MODULE == 1
- while(device_status.photo_ws == 1)
- {
- rt_thread_delay(1000);
- LOG_I("等待拍照完成...");
- }
- rt_thread_delay(1000);
- while(device_status.clear_status==1)
- {
- rt_thread_delay(1000);
- LOG_I("等待清扫完成...");
- }
- ctr_cangmen_down(OPEN);
- //待下仓门完全打开
- uint8_t i = 0;
- while(1)
- {
- i++;
- rt_thread_delay(100);
- rt_kprintf("等待下仓门打开!!!\n");
- if(i>100) {
- ctr_cangmen_down(OFF);
- break;
- }
- }
- rt_thread_delay(2000);
- ctr_shake(ON, device_status.shake_sec);
- device_status.photo_ws = 1;
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- //待下仓门完全关闭
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- }
- device_status.ctr_up_motor = 1; //打开上仓门
- #endif
- }
- }
- if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
- {
- LOG_I("-------------------not work--------------------");
- device_status.work_status = 0;
- }
- //工作结束
- if(device_status.work_done==0&&device_status.work_status==0)
- {
- DTU_Write("workdone");
- ResetInit();
- clt_time = 0;
- }
- rt_kprintf("***not work-device status:%d\n",device_status.ds);
- }
- #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
- void slidelock()
- {
- ctr_Motor_Driver_Power(ON); //电机驱动供电
- rt_thread_delay(10);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
- rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
- }
- /* 入口函数-工作状态检测 */
- 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);
- }
- device_status.light_over_tim = device_status.sunBar * 3600;
- device_status.test_flag = 0;
- }
- while(1)
- {
- WorkCheck();
- }
- }
- //指令
- //
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- rt_thread_delay(1000);
- ResetInit();
- device_status.light_over_tim = device_status.sunBar * 3600;
- rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
- static struct tm tm_tg_run;
- static uint8_t alarm_h,alarm_m,alarm_s;
- while(1)
- {
- if(device_status.work_mode==0)
- {
- WorkTask();
- }else{
- if(device_status.work_status==1){
- ctr_lamp(ON, 1);
- }else{
- ctr_lamp(OFF, 1);
- }
- char buf[7] = {0};
- /* ★ 原来用 "%-6s": 6 是"最小宽度"不是截断宽度, alarm_tg 是 char[12],
- 超过 6 字符时会完整写出 12+1 字节到 7 字节的 buf -> 栈溢出 6 字节。
- 必须写成 "%-6.6s"(精度 .6 才是截断)。 */
- rt_snprintf(buf, sizeof(buf), "%-6.6s", device_status.alarm_tg);
- // 截取 时: 前2位
- alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0');
- // 截取 分: 中间2位
- alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0');
- // 截取 秒: 后2位
- alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0');
- device_status.ctr_up_motor = 2; //关闭上仓门
- device_status.ctr_down_motor = 2; //关闭下仓门
- LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s);
- //判断时间
- time_t time_run_tg;
- time(&time_run_tg);
- tm_tg_run =*localtime(&time_run_tg);
- LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec);
- if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s))
- {
- LOG_W("预制执行 开始!");
- dtu_write_json(makeJson_Warn("执行完成", "预制模式"));
- rt_thread_delay(2000);
- //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1}
- cJSON *params = cJSON_CreateObject(); // params 子对象
- cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
- cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
- /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串 -> 走 dtu_write_json */
- dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
- rt_thread_delay(20000);
- }
- }
- rt_thread_delay(1000);
- }
- }
- /*入口函数清扫*/
- static void Control_Clear_thread_entry(void *parameter)
- {
- while(1)
- {
- if(device_status.update_flag==1) break;
- if(device_status.clear_status==1)
- {
- if(device_status.version_type==41){
- ctr_TG_Clear(300);
- }else {
- ctr_TG_Clear(400); //20260924-350修改为400
- }
- system_status.cleaning_count +=1; //清扫次数
- }
- if(device_status.clear_status==2)
- {
- turnzp();
- device_status.clear_status = 0;
- }
- rt_thread_delay(100);
- }
- }
- /*入口函数,自检*/
- static void Control_Check_thread_entry(void *parameter)
- {
- static int total_seconds = 200;
- static int current_second = 0;
- static int process = 0;
- device_status.C01 = 0;
- rt_thread_delay(10000);
- while(1)
- {
- if(device_status.update_flag==1) break;
- if(device_status.work_status==1&&device_status.heat_sw==1)
- {
- calorstat(device_status.heattime*60);
- rt_thread_delay(10000);
- }
- else
- {
- if(device_status.selfcheck_ws==2)
- {
- while(current_second <= total_seconds)
- {
- process = (current_second * 100) / total_seconds;
- device_status.process = process / 100.0;
- LOG_I("process:%d",process);
- HMI_WRITE("selfcheck.s106.val=%d",process);
- rt_thread_delay(1000);
- current_second++;
- if(device_status.selfcheck_ws==0)
- {
- HMI_WRITE("selfcheck.s106.val=100");
- break;
- }
- }
- HMI_WRITE("selfcheck.s106.val=100");
- }else {
- process = 0;
- current_second = 0;
- #if 0
- if(device_status.rain_status==1)
- {
- LOG_E("雨控!");
- rt_thread_delay(10000);
- }
- //根据故障码 亮指示灯
- //灯管故障
- if(device_status.C01 & (1 << 0))
- {
- LOG_E("灯管故障!");
- //发送红灯常亮信号
- write_single_holding_register(0x01,0x00C2,0x0011,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //加热故障
- if(device_status.C01 & (1 << 1))
- {
- LOG_E("加热故障!");
- //发送红灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0021,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //相机拍照故障
- if(device_status.C01 & (1 << 2))
- {
- LOG_E("拍照故障!");
- //发送红灯快闪信号
- write_single_holding_register(0x01,0x00C2,0x0031,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //补光灯故障
- if(device_status.C01 & (1 << 3))
- {
- LOG_E("补光灯故障!");
- //发送绿灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0023,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //上仓门故障
- if(device_status.C01 & (1 << 8))
- {
- LOG_E("上仓门故障!");
- //发送黄灯常亮信号
- write_single_holding_register(0x01,0x00C2,0x0012,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //下仓门故障
- if(device_status.C01 & (1 << 9))
- {
- LOG_E("下仓门故障!");
- //发送黄灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0022,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //清扫故障
- if(device_status.C01 & (1 << 10))
- {
- LOG_E("清扫故障!");
- //发送黄灯爆闪信号
- write_single_holding_register(0x01,0x00C2,0x0032,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //通信故障
- if(device_status.C01 & (1 << 13))
- {
- LOG_E("通信故障!");
- //发送绿灯爆闪信号
- write_single_holding_register(0x01,0x00C2,0x0033,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- if(device_status.C01==0)
- {
- LOG_W("无故障!");
- //发送绿灯常亮信号
- //write_single_holding_register(0x01,0x00C2,0x0013,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- #endif
- }
- }
- rt_thread_delay(1000);
- }
- }
- static void thread_485_play_entry(void *param)
- {
- static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
- rt_uint32_t e;
- // 创建事件控制块
- event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
- if (event_485_play == RT_NULL)
- {
- LOG_E("Event create failed!");
- return;
- }
- while(1)
- {
- if(device_status.update_flag==1) break;
- //等待事件
- if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
- {
- LOG_I("Interrupt occurred and processed.");
- _485_play(sound105, 4);
- //发送串口消息,通知JETSON计算卡进行拍照
- DTU_Write("{\"guangshan\":\"on\"}");
- }
- }
- }
- /*清扫线程*/
- 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_Check(void)
- {
- /* 创建线程*/
- tid_heat = rt_thread_create("Control_Check_thread_entry",
- Control_Check_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 thread_485_play(void)
- {
- tid_485_play = rt_thread_create("thread_485play",
- thread_485_play_entry, RT_NULL,
- 1024, //1024
- 26, //23
- 5); //5
- if(tid_485_play!=RT_NULL)
- {
- rt_thread_startup(tid_485_play);
- }
- else {
- LOG_E("thread_485_play start failed!");
- }
- 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)
- {
- rt_err_t result_loop = 0;
- /* 创建线程*/
- 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(tid_workloop!=RT_NULL)
- {
- result_loop = rt_thread_startup(tid_workloop);
- }
- return result_loop;
- }
- #ifdef RT_USING_ALARM
- void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp)
- {
- LOG_W("alarm_callback_router_on.\n");
- ctr_router(ON);
- }
- void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp)
- {
- LOG_W("alarm_callback_router_off.\n");
- ctr_router(OFF);
- }
- void alarm_app_router(void)
- {
- struct rt_alarm_setup setup;
- struct rt_alarm * alarm_router_on = RT_NULL;
- if (alarm_router_on != RT_NULL)
- return;
- setup.flag = RT_ALARM_DAILY;
- setup.wktime.tm_hour = 23;
- setup.wktime.tm_min = 0;
- setup.wktime.tm_sec = 0;
- alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup);
- if(RT_NULL != alarm_router_on)
- {
- LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec);
- rt_alarm_start(alarm_router_on);
- }
- }
- #endif
- MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
- MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
- MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
- MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
- MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
- //INIT_APP_EXPORT(Control_heat);
|