control.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. #include <control.h>
  2. /*
  3. * Copyright (c) 2006-2020, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-05-30 Administrator the first version
  10. * 2025-06-26 RTC闹钟使用定时开启和关闭路由器供电
  11. */
  12. #include "beep.h"
  13. #include "hmi.h"
  14. #include "easyflash.h"
  15. #include "stdio.h"
  16. #include "sound.h"
  17. #include "dtu_uart2.h"
  18. #include "sht30_collect.h"
  19. #include <user_mb_app.h>
  20. #include <selfcheck.h>
  21. #ifdef RT_USING_FINSH
  22. #include <shell.h> /* finsh_set_device(): uart_stop_for_ota 要把 finsh 摘回 uart1 */
  23. #endif
  24. #define DBG_ENABLE
  25. #define DBG_COLOR
  26. #define DBG_TAG "control"
  27. #define DBG_LEVEL DBG_LOG
  28. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  29. #include "sys_monitor.h"
  30. #include "param_mirror.h"
  31. //工作检测
  32. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  33. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  34. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  35. //工作流程
  36. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  37. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  38. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  39. #define PWM_EN 0 //0关闭,1-打开
  40. //设备状态表结构体
  41. /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */
  42. /* system_status 已收敛为全工程唯一定义(在 sys_monitor.c), 见 sys_monitor.h 的 extern 声明 */
  43. rt_thread_t tid_workloop = RT_NULL;
  44. rt_thread_t tid_wkstc = RT_NULL;
  45. rt_thread_t tid_ctr = RT_NULL;
  46. rt_thread_t tid_heat = RT_NULL;
  47. rt_thread_t tid_485_play = RT_NULL;
  48. extern rt_device_t dtuserial; /*串口设备句柄*/
  49. rt_event_t event_485_play;
  50. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  51. /***************************************************************************/
  52. int thread_delete_workloop(void)
  53. {
  54. rt_err_t result;
  55. if(tid_workloop!=RT_NULL)
  56. {
  57. result = rt_thread_delete(tid_workloop);
  58. }
  59. return result;
  60. }
  61. int thread_restart_workloop(void)
  62. {
  63. rt_err_t result;
  64. result = Work_Loop_Task();
  65. return result;
  66. }
  67. /***************************************************************************/
  68. /* ==================== OTA 升级前的线程清理 ==================== */
  69. #define OTA_THREAD_MAX 24
  70. /* 保留名单: 这几类线程删掉反而会让升级失败 */
  71. static const char *g_ota_keep[] =
  72. {
  73. "syswatch", /* 喂狗线程: 删了 5s 后硬件看门狗就复位, 升级必中断 */
  74. "tshell", /* 控制台 */
  75. "tidle",
  76. "main",
  77. };
  78. /* 需要停掉的业务线程前缀(写完整名即可, 比较时自动按 RT_NAME_MAX-1 截断)。
  79. *
  80. * 为什么用"前缀匹配 + 遍历"而不是 rt_thread_find(全名):
  81. * rtconfig.h 里 RT_NAME_MAX = 8, 线程名被内核截断到 7 个字符 ——
  82. * "Control_Clear_thread_entry" / "Control_Check_thread_entry" 都变成 "Control";
  83. * "DTU_Uart_thread_send" / "DTU_Uart_thread_recv" 都截成 "DTU_Uar",
  84. * 而 recv 正是执行升级的当前线程, 用 rt_thread_find 有可能把调用者自己删掉。
  85. * 所以改成遍历线程对象链表, 并显式跳过 rt_thread_self()。 */
  86. static const char *g_ota_stop[] =
  87. {
  88. "work_loop_thread", /* 工作流程, LOG_I/rt_kprintf 密集 */
  89. "work_status_check_thread", /* WorkCheck, 每 10s 一批日志 */
  90. "Control_Clear_thread_entry", /* 清扫, 会驱动电机 */
  91. "Control_Check_thread_entry", /* 恒温, LOG_I("process") 每秒 */
  92. "thread_485play", /* 485 播报 */
  93. "adc_thread", /* ADC 采集, 每 5s 打 3 行 */
  94. "Power Check thread", /* 每秒读电流/电压 */
  95. "Hmi_Fresh_thread", /* 屏幕刷新 */
  96. "Ctr motor", /* 仓门/光源, 带 rt_kprintf */
  97. "sys_monitor", /* CPU/统计 */
  98. "param_sv", /* 参数落盘: 会擦 flash(4KB≈100ms) 抢 CPU */
  99. "DTU_Uart_thread_send", /* DTU 发送 (recv 是自身, 会被跳过) */
  100. "Uart5_thread_recv", /* 485 透传 */
  101. "md_m_poll", /* modbus 轮询 */
  102. "md_m_send", /* modbus 发送 */
  103. "usage", /* cpu_usage 临时线程 */
  104. };
  105. /* 名字前缀比较: 内核只存了前 RT_NAME_MAX-1 个字符, 所以最多比这么多 */
  106. static int ota_name_match(const char *thread_name, const char *prefix)
  107. {
  108. rt_size_t n;
  109. if (thread_name == RT_NULL || prefix == RT_NULL || thread_name[0] == '\0')
  110. {
  111. return 0;
  112. }
  113. n = rt_strlen(prefix);
  114. if (n > RT_NAME_MAX - 1)
  115. {
  116. n = RT_NAME_MAX - 1;
  117. }
  118. if (n == 0)
  119. {
  120. return 0;
  121. }
  122. return (rt_strncmp(thread_name, prefix, n) == 0) ? 1 : 0;
  123. }
  124. static int ota_in_list(const char *thread_name, const char **list, int cnt)
  125. {
  126. int i;
  127. for (i = 0; i < cnt; i++)
  128. {
  129. if (ota_name_match(thread_name, list[i]))
  130. {
  131. return 1;
  132. }
  133. }
  134. return 0;
  135. }
  136. /**
  137. * OTA 升级前把所有业务线程停掉, 只留 tshell / idle / syswatch。
  138. *
  139. * 为什么必须停:
  140. * 1) ymodem_ota() 会把 console 切到 uart2(ymodem_ota.c:117), 之后**任何线程**
  141. * 的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染 ymodem 字节流 -> 校验失败;
  142. * 2) adc / Power Check 每秒读 ADC, param_sv 会擦 flash(4KB 约 100ms),
  143. * 抢 CPU 和外设会让 ymodem 丢字节;
  144. * 3) Control_Clear / Control_Check 会驱动电机, 升级过程中不该动机械部件。
  145. *
  146. * 顺序: ① adc_thread 优雅退出(它可能正持有 ADC 设备锁)
  147. * ② 置 RAM 内退出请求, 让带 `update_flag==1 break` 的线程自己退
  148. * ③ 其余按前缀强删(先收集再删, 不能边遍历边删)
  149. *
  150. * @return 实际删除的线程数; 负值为错误
  151. */
  152. int thread_stop_all_for_ota(void)
  153. {
  154. struct rt_object_information *info;
  155. rt_list_t *node;
  156. rt_thread_t victims[OTA_THREAD_MAX];
  157. rt_thread_t self;
  158. int nv = 0, i, done = 0;
  159. info = rt_object_get_information(RT_Object_Class_Thread);
  160. if (info == RT_NULL)
  161. {
  162. LOG_E("[ota] thread object information NOT found");
  163. return -RT_ERROR;
  164. }
  165. /* ① adc_thread 必须优雅退出: 直接 rt_thread_delete 若刚好删在
  166. rt_adc_enable/read 中间, 它持有的 ADC 设备锁永远不会释放 */
  167. thread_delete_adc();
  168. /* ② 置 RAM 内退出请求。Control_Clear / Control_Check / thread_485play
  169. 三处都有 `if(device_status.update_flag==1) break;`。
  170. ★ 只改 RAM, 绝不写 flash / 参数快照 —— 一旦把 1 落盘, 下次开机会被
  171. hmi.c 的 up_flag_* 裁决判成"有升级请求", 而带 break 的线程就会
  172. 一开机直接退出(设备不清扫、不恒温)。
  173. 因为全程不落盘, ymodem 结束复位后这个 1 自然消失, 不需要额外清理。 */
  174. device_status.update_flag = 1;
  175. rt_thread_mdelay(300);
  176. /* ③ 收集待停线程。必须"先收集后删除": rt_thread_delete 会摘掉链表节点,
  177. 边遍历边删会把 object_list 走坏 */
  178. self = rt_thread_self();
  179. rt_enter_critical();
  180. rt_list_for_each(node, &info->object_list)
  181. {
  182. rt_thread_t t = rt_list_entry(node, struct rt_thread, list);
  183. if (t == self) /* 不能删自己 */
  184. {
  185. continue;
  186. }
  187. if (ota_in_list(t->name, g_ota_keep, sizeof(g_ota_keep) / sizeof(g_ota_keep[0])))
  188. {
  189. continue;
  190. }
  191. if (!ota_in_list(t->name, g_ota_stop, sizeof(g_ota_stop) / sizeof(g_ota_stop[0])))
  192. {
  193. continue; /* 不在名单里, 不动 */
  194. }
  195. if (nv < OTA_THREAD_MAX)
  196. {
  197. victims[nv++] = t;
  198. }
  199. }
  200. rt_exit_critical();
  201. /* ④ 逐个删除 */
  202. for (i = 0; i < nv; i++)
  203. {
  204. if (rt_thread_delete(victims[i]) == RT_EOK)
  205. {
  206. done++;
  207. }
  208. }
  209. LOG_I("[ota] stopped %d of %d candidate thread(s)", done, nv);
  210. return done;
  211. }
  212. /**
  213. * @brief 把 double 定点格式化成 "整数.两位小数" 字符串(不依赖 libc 浮点 printf)。
  214. *
  215. * 🔴 为什么必须有这个函数:
  216. * RT-Thread 的 rt_snprintf() 只是 rt_vsnprintf() 的薄封装, 而 rt_vsnprintf
  217. * (rt-thread/src/kservice.c:886) 的转换字符 switch 只有:
  218. * case 'c' / 's' / 'd' / 'i' / 'o' / 'u' / 'x' / 'X' / '%'
  219. * **没有 case 'f' / 'e' / 'g'**。rtconfig.h 也没打开 RT_USING_LIBC_FLOAT_PRINT。
  220. * 于是 "%.2f" 会一路走到 default: 分支(kservice.c:1090), 该分支的行为是
  221. * 把 '%' 和当前字符原样写进缓冲区, 然后 continue;
  222. * 结果就是缓冲区里得到字面量 **"%f"**(小数点 ".2" 也会被当普通字符写出去),
  223. * 而且**不消费对应的 double 可变参数** —— 后面若还有格式符, 参数会整体错位。
  224. * 这就是 2026-09-28 平台侧电流/电压显示成 "%f" 的根因。
  225. *
  226. * 也不能简单换回标准库 sprintf("%f"): newlib 的 float printf 默认不使能,
  227. * 要额外链入 _printf_float(体积大), 且原来 power[8] 装 "25000.00" 会越界。
  228. *
  229. * 这里的做法: 纯整数定点运算, 只用 %ld/%02ld, 任何 printf 实现都支持。
  230. * 代价是没有完整的 dtoa 舍入, 但先加 0.5 再强转已经等价于四舍五入到 0.01,
  231. * 电流电压这类量只要两位小数, 精度完全够。
  232. *
  233. * @param buf 输出缓冲(必须是数组, 不能是指针)
  234. * @param size 缓冲大小
  235. * @param val 待格式化的值
  236. * @param prec 小数位数, 内部钳到 0~2
  237. */
  238. void fmt_fixed2(char *buf, rt_size_t size, double val, int prec)
  239. {
  240. /* 上限钳位: 防止 val*100 溢出 long。20000.00 是电流/功率物理上够不到的上限。 */
  241. const double MAX_VAL = 20000.0;
  242. long scaled; /* val 放大 100 倍后的定点整数 */
  243. long ip, fp; /* 整数部分 / 小数部分 */
  244. int neg = 0;
  245. if (buf == RT_NULL || size == 0)
  246. {
  247. return;
  248. }
  249. if (prec < 0) { prec = 0; }
  250. if (prec > 2) { prec = 2; }
  251. if (val < 0.0)
  252. {
  253. neg = 1;
  254. val = -val;
  255. }
  256. if (val > MAX_VAL)
  257. {
  258. val = MAX_VAL;
  259. }
  260. /* 先统一放大 100 倍并四舍五入(加 0.5 后强转即等价于四舍五入到 0.01)。
  261. 这样 prec=0/1/2 三个分支都能直接取用, 不会出现 prec=1 时把 12.99
  262. 截断成 12.9 的问题 —— 那一档要的是四舍五入后的 13.0。 */
  263. scaled = (long)(val * 100.0 + 0.5);
  264. ip = scaled / 100;
  265. fp = scaled % 100;
  266. if (prec == 0)
  267. {
  268. /* 四舍五入到整数: fp >= 50 则进位 */
  269. if (fp >= 50)
  270. {
  271. ip += 1;
  272. }
  273. rt_snprintf(buf, size, "%s%ld", neg ? "-" : "", ip);
  274. }
  275. else if (prec == 1)
  276. {
  277. /* 四舍五入到 0.1: fp 的个位 >= 5 则进位到十分位 */
  278. if (fp % 10 >= 5)
  279. {
  280. fp += 10;
  281. if (fp >= 100)
  282. {
  283. fp -= 100;
  284. ip += 1;
  285. }
  286. }
  287. rt_snprintf(buf, size, "%s%ld.%ld", neg ? "-" : "", ip, fp / 10);
  288. }
  289. else
  290. {
  291. rt_snprintf(buf, size, "%s%ld.%02ld", neg ? "-" : "", ip, fp);
  292. }
  293. }
  294. /**
  295. * @brief 把一个串口设备彻底关掉(解引用到 ref_count==0), 用于 OTA 前收尾。
  296. *
  297. * 为什么不能只 rt_device_close() 一次:
  298. * rt_device_close() 只有在 ref_count 减到 0 时才会真正调用驱动的 close,
  299. * 否则只减计数就返回 RT_EOK。而 uart4/uart5 的引用不止一处:
  300. * uart4 → HMI_uart4_init() 开 1 次, 若现场跑过 Debug_log_ON(),
  301. * rt_console_set_device("uart4") + finsh_set_device("uart4")
  302. * 又各开 1 次(共 3 次);
  303. * uart5 → DTU_uart5_init() 开 1 次, UART_485_INIT() 再开 1 次,
  304. * freemodbus 的 xMBMasterPortSerialInit() 还会再开 1 次。
  305. * 所以必须循环 close 到 ref_count 归零, 否则设备仍在收发、ISR 仍会触发。
  306. *
  307. * @param dev 设备句柄, 可为 RT_NULL
  308. * @param name 名字, 仅用于日志
  309. */
  310. static void ota_close_uart(rt_device_t dev, const char *name)
  311. {
  312. rt_uint8_t guard = 8;
  313. if (dev == RT_NULL)
  314. {
  315. return;
  316. }
  317. /* ① 先摘掉接收回调。close() 内部也会清, 但清回调后到 close 之间
  318. 还有窗口期, 此刻若有数据到达, 回调里会 release 已经没人等的
  319. 信号量 / 往已删线程的消息队列里塞消息。 */
  320. rt_device_set_rx_indicate(dev, RT_NULL);
  321. /* ② 循环 close 到引用计数归零。guard 兜底, 防止内核行为异常时空转。 */
  322. while (dev->ref_count > 0 && guard-- > 0)
  323. {
  324. if (rt_device_close(dev) != RT_EOK)
  325. {
  326. break;
  327. }
  328. }
  329. LOG_I("[ota] %s closed, ref_count=%d", name, (int)dev->ref_count);
  330. }
  331. /**
  332. * @brief OTA 升级前关闭串口4(HMI屏) 和串口5(485)。
  333. *
  334. * 为什么要关:
  335. * 1) ymodem_ota() 内部会 rt_console_set_device("uart2"), 之后 console 指向
  336. * uart2。此时 uart4/uart5 若还在工作, 它们的 ISR、以及仍可能残留的
  337. * 485 播报/屏幕刷新, 都会和 ymodem 抢 CPU 与中断, 导致传输丢字节;
  338. * 2) 485 是**半双工**总线, 收发器被驱动着去收 ymodem 期间还挂着一个
  339. * 从机在等应答, 升级完复位后从机状态与 MCU 不同步, 现场表现为
  340. * "升级完 485 读不到数据";
  341. * 3) 屏幕在升级过程中继续刷新, 白白占用带宽, 且部分屏会因长时间收不到
  342. * 指令而重新初始化自身。
  343. *
  344. * ★ 本函数必须在 thread_stop_all_for_ota() **之后**调用:
  345. * 先停线程(没人再发数据) → 再关设备。反过来的话, 关设备的瞬间还有
  346. * 线程在 HMI_WRITE/Uart5_Write, 会写到已被 disable 的 fifo 上。
  347. *
  348. * @return 关闭的串口个数
  349. */
  350. int uart_stop_for_ota(void)
  351. {
  352. int n = 0;
  353. /* 置标志必须**先于**关设备: 中间还有别的线程可能正在 HMI_WRITE,
  354. 看到标志后直接 return, 不会碰到正在被拆掉的 fifo。 */
  355. g_hmi_port_closed = 1;
  356. /* ---- 串口4: HMI 屏 ----
  357. 先把 console / finsh 摘回 uart1, 否则它们各自持有 uart4 的一次 open
  358. (rt_console_set_device / finsh_set_device 内部都是 open 新的 + close 旧的),
  359. 引用计数降不到 0, 设备等于没关; 而且 tshell 正阻塞在 uart4 的 read 上,
  360. 设备被关后它会一直读到 0。 */
  361. if (rt_console_get_device() != RT_NULL &&
  362. rt_strcmp(rt_console_get_device()->parent.name, UART_NAME) == 0)
  363. {
  364. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  365. }
  366. #if defined(RT_USING_FINSH) && !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  367. finsh_set_device(RT_CONSOLE_DEVICE_NAME);
  368. #endif
  369. if (serial != RT_NULL)
  370. {
  371. ota_close_uart(serial, UART_NAME);
  372. n++;
  373. }
  374. /* ---- 串口5: 485 ---- */
  375. if (serial_485 != RT_NULL)
  376. {
  377. ota_close_uart(serial_485, "uart5");
  378. n++;
  379. }
  380. /* 把句柄置 NULL: 让上层即使漏判 g_hmi_port_closed 也不会往已废设备写。
  381. ⚠️ 置 NULL 之后 ymodem 失败返回也不会有任何 HMI/485 输出, 这是预期的
  382. ——升级流程本来就要求这段时间对外静默。 */
  383. serial = RT_NULL;
  384. serial_485 = RT_NULL;
  385. LOG_I("[ota] uart4/uart5 stopped (%d)", n);
  386. return n;
  387. }
  388. /****************************5.0***********************************/
  389. void ctr_turnv5(void)
  390. {
  391. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  392. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  393. // 转出
  394. rt_pin_write(CLEAR_R,ON);
  395. rt_pin_write(CLEAR_L,OFF);
  396. rt_thread_mdelay(300);
  397. //停止
  398. rt_pin_write(CLEAR_R,OFF);
  399. rt_pin_write(CLEAR_L,OFF);
  400. rt_thread_mdelay(300);
  401. //转入
  402. rt_pin_write(CLEAR_R,OFF);
  403. rt_pin_write(CLEAR_L,ON);
  404. rt_thread_mdelay(300);
  405. //停止
  406. rt_pin_write(CLEAR_R,OFF);
  407. rt_pin_write(CLEAR_L,OFF);
  408. rt_thread_mdelay(300);
  409. }
  410. /*************************吸虫塔*********************************/
  411. //吸虫电机
  412. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  413. {
  414. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  415. if(status_ws==ON)
  416. {
  417. rt_pin_write(MOTOR1_CTR_PIN,ON);
  418. }
  419. else {
  420. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  421. }
  422. return 0;
  423. }
  424. uint8_t ctr_Camera_Power(uint8_t status_ws)
  425. {
  426. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  427. if(status_ws==ON)
  428. {
  429. rt_pin_write(MOTOR1_CTR_PIN,ON);
  430. }
  431. else {
  432. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  433. }
  434. return 0;
  435. }
  436. //吸附电机
  437. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  438. {
  439. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  440. if(status_ws==ON)
  441. {
  442. rt_pin_write(MOTOR2_CTR_PIN,ON);
  443. }
  444. else {
  445. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  446. }
  447. return 0;
  448. }
  449. //清扫-卷带电机
  450. void ctr_JD_Clear(uint8_t shake_sw)
  451. {
  452. rt_uint16_t turn_timeout = 0;
  453. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  454. ctr_cangmen_up(OFF); //清扫传送带的电机
  455. if(shake_sw==ON)
  456. {
  457. ctr_Heat(ON); //清洁风机
  458. Motor_shake(ON, 0);
  459. ctr_cangmen_up(OPEN); //打开清扫
  460. }
  461. else {
  462. Motor_shake(OFF, 0);
  463. ctr_cangmen_up(OPEN); //打开清扫
  464. }
  465. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  466. {
  467. Motor_Juandai(ON);
  468. rt_thread_delay(50);
  469. turn_timeout++;
  470. if(turn_timeout==1000) break;
  471. }
  472. turn_timeout = 0;
  473. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  474. {
  475. Motor_Juandai(ON);
  476. turn_timeout++;
  477. rt_thread_delay(50);
  478. if(turn_timeout==1000) break;
  479. }
  480. Motor_shake(OFF, 1);
  481. Motor_Juandai(OFF);
  482. ctr_cangmen_up(OFF); //关闭清扫
  483. ctr_Heat(OFF);
  484. }
  485. //工作任务流程
  486. void WorkTask_XCT(void)
  487. {
  488. while(device_status.work_status==1&&device_status.ds==1)
  489. {
  490. ctr_Motor_xifu(ON);
  491. //打开相机供电
  492. ctr_Camera_Power(ON);
  493. //ctr_Heat(ON); //清洁风机
  494. uint32_t photo_fre = 0;
  495. photo_fre = device_status.dattim * 60;
  496. while(photo_fre!=0)
  497. {
  498. photo_fre --;
  499. rt_thread_delay(1000);
  500. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  501. }
  502. //卷带
  503. ctr_JD_Clear(OFF);
  504. //拍照
  505. HK_TakePhoto();
  506. //关闭吸附风机
  507. ctr_Motor_xifu(OFF);
  508. //关闭相机供电
  509. //ctr_Heat(OFF);
  510. ctr_Camera_Power(OFF);
  511. //卷带
  512. ctr_JD_Clear(ON);
  513. // rt_thread_delay(1000);
  514. // ctr_JD_Clear(ON);
  515. //震动
  516. Motor_shake(ON, device_status.shake_sec);
  517. }
  518. //关闭风机
  519. ctr_Motor_xichong(OFF);
  520. ctr_Motor_xifu(OFF);
  521. LOG_I("不在工作时间,未工作!!!");
  522. }
  523. /**********************************************************************/
  524. /*光控*/
  525. uint8_t In_Light_status(void)
  526. {
  527. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  528. return rt_pin_read(LIGHT_PIN);
  529. }
  530. /*雨控*/
  531. uint8_t In_Rain_status(void)
  532. {
  533. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  534. return rt_pin_read(RAIN_PIN);
  535. }
  536. /*加热*/
  537. uint8_t ctr_Heat(uint8_t status_heat)
  538. {
  539. rt_bool_t is_currently_on;
  540. rt_tick_t now;
  541. now = rt_tick_get();
  542. is_currently_on = (system_status.heat_start_time != 0);
  543. device_status.heat_status = status_heat;
  544. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  545. rt_pin_write(Heat_Pin,status_heat);
  546. if(status_heat==ON){
  547. // 已经开着 → 不重复计时
  548. if (is_currently_on) {
  549. return ON;
  550. }
  551. // 关 → 开,记录起始时间
  552. system_status.heat_start_time = now;
  553. }else{
  554. // 已经关着 → 不重复结算
  555. if (!is_currently_on) {
  556. return OFF;
  557. }
  558. // 开 → 关,结算运行时间
  559. system_status.heater_running_time +=
  560. (now - system_status.heat_start_time);
  561. system_status.heat_start_time = 0;
  562. }
  563. return status_heat;
  564. }
  565. /*制冷*/
  566. uint8_t ctr_Cold(uint8_t status_cold)
  567. {
  568. device_status.cold_status = status_cold;
  569. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  570. rt_pin_write(Cold_Pin,status_cold);
  571. return status_cold;
  572. }
  573. /*相机供电*/
  574. void ctr_photo(uint8_t status_photo)
  575. {
  576. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  577. rt_pin_write(PHOTO,status_photo);
  578. }
  579. /*************************************************
  580. Function: ctr_Cool_Fan
  581. Description: 散热风机的开关
  582. Calls:
  583. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  584. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  585. Input: ON-开,OFF-关
  586. Output: NULL
  587. Return: ON-开,OFF-关
  588. Others: NULL
  589. *************************************************/
  590. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  591. {
  592. device_status.cool_fan_status = status_cool;
  593. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  594. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  595. if(status_cool==OFF)
  596. {
  597. rt_pin_write(Cool_Fan_L,OFF);
  598. rt_pin_write(Cool_Fan_R,OFF);
  599. }
  600. if(status_cool==ON)
  601. {
  602. rt_pin_write(Cool_Fan_L,ON);
  603. rt_pin_write(Cool_Fan_R,OFF);
  604. }
  605. return device_status.cool_fan_status;
  606. }
  607. /*************************************************
  608. Function: ctr_Collect_Fan
  609. Description: 采集风机的开关
  610. Calls:
  611. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  612. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  613. Input: ON-开,OFF-关
  614. Output: NULL
  615. Return: ON-开,OFF-关
  616. Others: NULL
  617. *************************************************/
  618. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  619. {
  620. device_status.collect_fan_status = status_collect;
  621. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  622. if(status_collect==OFF)
  623. {
  624. rt_pin_write(Collect_Fan,OFF);
  625. }
  626. if(status_collect==ON)
  627. {
  628. rt_pin_write(Collect_Fan,ON);
  629. }
  630. return device_status.collect_fan_status;
  631. }
  632. /*************************************************
  633. Function: ctr_develop
  634. Description: 培养载玻片孢子
  635. Calls:
  636. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  637. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  638. Input: v=0(复位) v=90转90度
  639. Output: NULL
  640. Return: NULL
  641. Others: NULL
  642. *************************************************/
  643. /*上仓门*/
  644. uint8_t ctr_cangmen_up(uint8_t status_up)
  645. {
  646. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  647. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  648. if(status_up==OFF)
  649. {
  650. rt_pin_write(Collect_fan_R,ON);
  651. rt_pin_write(Collect_fan_L,ON);
  652. device_status.ctr_up_motor = 0;
  653. rt_thread_delay(1000);
  654. }
  655. if(status_up==OPEN)
  656. {
  657. device_status.up_status = 1;
  658. rt_pin_write(Collect_fan_R,OFF);
  659. rt_pin_write(Collect_fan_L,OFF);
  660. #if PCB_V1 == 1
  661. rt_pin_write(Collect_fan_R,ON);
  662. rt_pin_write(Collect_fan_L,OFF);
  663. #else
  664. rt_pin_write(Collect_fan_R,OFF);
  665. rt_pin_write(Collect_fan_L,ON);
  666. #endif
  667. }
  668. if(status_up==CLOSE)
  669. {
  670. device_status.up_status = 0;
  671. rt_pin_write(Collect_fan_R,OFF);
  672. rt_pin_write(Collect_fan_L,OFF);
  673. #if PCB_V1 == 1
  674. rt_pin_write(Collect_fan_R,OFF);
  675. rt_pin_write(Collect_fan_L,ON);
  676. #else
  677. rt_pin_write(Collect_fan_R,ON);
  678. rt_pin_write(Collect_fan_L,OFF);
  679. #endif
  680. }
  681. return device_status.up_status;
  682. }
  683. //下仓门
  684. uint8_t ctr_cangmen_down(uint8_t status_down)
  685. {
  686. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  687. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  688. if(status_down==OFF)
  689. {
  690. rt_pin_write(C_DOWN_R,ON);
  691. rt_pin_write(C_DOWN_L,ON);
  692. device_status.ctr_down_motor = 0;
  693. rt_thread_delay(1000);
  694. }
  695. if(status_down==OPEN)
  696. {
  697. device_status.down_status = 1;
  698. rt_pin_write(C_DOWN_R,OFF);
  699. rt_pin_write(C_DOWN_L,OFF);
  700. rt_pin_write(C_DOWN_R,ON);
  701. rt_pin_write(C_DOWN_L,OFF);
  702. }
  703. if(status_down==CLOSE)
  704. {
  705. device_status.down_status = 0;
  706. rt_pin_write(C_DOWN_R,OFF);
  707. rt_pin_write(C_DOWN_L,OFF);
  708. rt_pin_write(C_DOWN_R,OFF);
  709. rt_pin_write(C_DOWN_L,ON);
  710. }
  711. return device_status.down_status;
  712. }
  713. //镇流器
  714. //镇流器
  715. uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
  716. {
  717. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  718. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  719. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  720. rt_bool_t is_currently_on;
  721. rt_tick_t now;
  722. now = rt_tick_get();
  723. is_currently_on = (system_status.lamp_start_time != 0);
  724. if (status_lamp == ON)
  725. {
  726. // ====== 开灯 ======
  727. switch (lamp_num) {
  728. case 1:
  729. rt_pin_write(LAMP_SW, ON);
  730. device_status.lamp_status = ON;
  731. break;
  732. case 2:
  733. rt_pin_write(LAMP_1_SW, ON);
  734. device_status.lamp1_status = ON;
  735. break;
  736. case 3:
  737. rt_pin_write(LAMP_2_SW, ON);
  738. device_status.lamp2_status = ON;
  739. break;
  740. default:
  741. break;
  742. }
  743. // 已经开着 → 不重复计时
  744. if (is_currently_on) {
  745. return ON;
  746. }
  747. // 关 → 开,记录起始时间
  748. system_status.lamp_start_time = now;
  749. }
  750. else
  751. {
  752. // ====== 关灯 ======
  753. switch (lamp_num) {
  754. case 1:
  755. rt_pin_write(LAMP_SW, OFF);
  756. device_status.lamp_status = OFF;
  757. break;
  758. case 2:
  759. rt_pin_write(LAMP_1_SW, OFF);
  760. device_status.lamp1_status = OFF;
  761. break;
  762. case 3:
  763. rt_pin_write(LAMP_2_SW, OFF);
  764. device_status.lamp2_status = OFF;
  765. break;
  766. default:
  767. break;
  768. }
  769. // 已经关着 → 不重复结算
  770. if (!is_currently_on) {
  771. return OFF;
  772. }
  773. // 开 → 关,结算运行时间
  774. system_status.lamp_running_time +=
  775. (now - system_status.lamp_start_time);
  776. system_status.lamp_start_time = 0;
  777. }
  778. HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
  779. return device_status.lamp_status;
  780. }
  781. //震动电机
  782. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  783. {
  784. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  785. #if 0
  786. rt_pin_write(SHAKE_SW,ON);
  787. return 0;
  788. #endif
  789. if(status_shake==ON&&device_status.shake_ws==1)
  790. {
  791. if(device_status.dtype==3)
  792. {
  793. for(uint8_t shake_i = 0;shake_i<shake_sec;shake_i++)
  794. {
  795. rt_pin_write(SHAKE_SW,ON);
  796. rt_thread_delay(100);
  797. rt_pin_write(SHAKE_SW,OFF);
  798. rt_thread_delay(1000);
  799. }
  800. }
  801. rt_pin_write(SHAKE_SW,OFF);
  802. HMI_WRITE("cangWei.switchShake.val=%d",0);
  803. system_status.vibration_count += 1;
  804. }
  805. else
  806. {
  807. rt_pin_write(SHAKE_SW,OFF);
  808. HMI_WRITE("cangWei.switchShake.val=%d",0);
  809. }
  810. if(device_status.dtype==7)
  811. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  812. return status_shake;
  813. }
  814. //补光
  815. uint8_t ctr_led(uint8_t status_led)
  816. {
  817. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  818. if(status_led==ON)
  819. {
  820. rt_pin_write(LED_SW,ON);
  821. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  822. }
  823. else if(status_led==OFF){
  824. rt_pin_write(LED_SW,OFF);
  825. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  826. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  827. }
  828. return status_led;
  829. }
  830. /*************************************************
  831. Function: ctr_turn
  832. Description: 转盘驱动
  833. Calls:
  834. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  835. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  836. Input: v=0(复位) v=90转90度
  837. Output: NULL
  838. Return: NULL
  839. Others: NULL
  840. *************************************************/
  841. void ctr_turn(uint8_t v,uint8_t sta)
  842. {
  843. DTU_Write("turn.mp3");
  844. LOG_I("================v===============:%d",v);
  845. rt_uint16_t turn_cnt = 0;
  846. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  847. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  848. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  849. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  850. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  851. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  852. {
  853. rt_pin_write(TURN_ZP_R, ON);
  854. rt_pin_write(TURN_ZP_L, OFF);
  855. //Motor_shake(ON,0);
  856. turn_cnt ++;
  857. rt_thread_delay(100);
  858. if(turn_cnt==1000)
  859. {
  860. turn_cnt = 0;
  861. rt_pin_write(TURN_ZP_R, OFF);
  862. rt_pin_write(TURN_ZP_L, OFF);
  863. //Motor_shake(OFF,0);
  864. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  865. DTU_Write("%s",turn_warn);
  866. rt_free(turn_warn);
  867. break;
  868. }
  869. }
  870. turn_cnt = 0;
  871. while(v==0&&rt_pin_read(LIMIT_0)==1)
  872. {
  873. rt_pin_write(TURN_ZP_R, ON);
  874. rt_pin_write(TURN_ZP_L, OFF);
  875. Motor_shake(ON,0);
  876. rt_thread_delay(200); //10 to 100
  877. turn_cnt ++;
  878. if(turn_cnt==300) //6000 to 600
  879. {
  880. turn_cnt = 0;
  881. // rt_pin_write(TURN_ZP_R, OFF);
  882. // rt_pin_write(TURN_ZP_L, OFF);
  883. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  884. // DTU_Write("%s",turn_warn);
  885. // rt_free(turn_warn);
  886. break;
  887. }
  888. }
  889. while(v==0&&rt_pin_read(LIMIT_0)==1)
  890. {
  891. rt_pin_write(TURN_ZP_R, ON);
  892. rt_pin_write(TURN_ZP_L, OFF);
  893. //Motor_shake(ON,0);
  894. rt_thread_delay(50); //10 to 50
  895. turn_cnt ++;
  896. if(turn_cnt==10) //6000 to 1200
  897. {
  898. turn_cnt = 0;
  899. rt_pin_write(TURN_ZP_R, OFF);
  900. rt_pin_write(TURN_ZP_L, OFF);
  901. //Motor_shake(OFF,0);
  902. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  903. DTU_Write("%s",turn_warn);
  904. rt_free(turn_warn);
  905. break;
  906. }
  907. }
  908. turn_cnt = 0;
  909. while(v==90&&rt_pin_read(LIMIT_90)==1)
  910. {
  911. rt_pin_write(TURN_ZP_R, ON);
  912. rt_pin_write(TURN_ZP_L, OFF);
  913. //Motor_shake(ON,0);
  914. rt_thread_delay(200);
  915. turn_cnt ++;
  916. if(turn_cnt==150)
  917. {
  918. turn_cnt = 0;
  919. // rt_pin_write(TURN_ZP_R, OFF);
  920. // rt_pin_write(TURN_ZP_L, OFF);
  921. // char *turn_warn =makeJson_Warn("超时", "转盘");
  922. // DTU_Write(turn_warn);
  923. // rt_free(turn_warn);
  924. break;
  925. }
  926. }
  927. while(v==90&&rt_pin_read(LIMIT_90)==1)
  928. {
  929. // rt_pin_write(TURN_ZP_R, ON);
  930. // rt_pin_write(TURN_ZP_L, OFF);
  931. Motor_shake(ON,0);
  932. rt_thread_delay(50);
  933. turn_cnt ++;
  934. if(turn_cnt==10)
  935. {
  936. turn_cnt = 0;
  937. rt_pin_write(TURN_ZP_R, OFF);
  938. rt_pin_write(TURN_ZP_L, OFF);
  939. //Motor_shake(OFF,0);
  940. /* ★ 原来 DTU_Write(turn_warn) 把 JSON 当格式串(含 '%' 就会被解析) -> 走 dtu_write_json */
  941. dtu_write_json(makeJson_Warn("超时", "转盘"));
  942. break;
  943. }
  944. }
  945. if(sta!=0)
  946. {
  947. //保存位置
  948. device_status.run_sta = sta;
  949. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  950. /* 转盘位置变了, 同步进参数镜像(掉电后能恢复到正确仓位) */
  951. param_mirror_sync();
  952. }
  953. turn_cnt = 0;
  954. rt_pin_write(TURN_ZP_R, OFF);
  955. rt_pin_write(TURN_ZP_L, OFF);
  956. Motor_shake(OFF,0);
  957. HMI_WRITE("cangWei.rotateFlag.val=0");
  958. }
  959. /*
  960. *
  961. * */
  962. void ctr_Motor_Driver_Power(uint8_t status_ws)
  963. {
  964. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  965. rt_pin_write(SHAKE_SW, status_ws);
  966. }
  967. /*************************************************
  968. Function: ctr_Motor_Driver
  969. Description: 步进电机驱动
  970. Calls:
  971. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  972. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  973. Input: 50000000
  974. Output: NULL
  975. Return: shake_sec
  976. Others: NULL
  977. *************************************************/
  978. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  979. {
  980. beep_init();
  981. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  982. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  983. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  984. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  985. rt_pin_write(Motor_EN, EN);
  986. rt_pin_write(Motor_DIR, DIR);
  987. beep_set(pwm, pwm/2); //200kzh - 50%
  988. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  989. {
  990. if(time_out==0)
  991. {
  992. break;
  993. }
  994. rt_thread_delay(50);
  995. time_out --;
  996. }
  997. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  998. {
  999. if(time_out==0)
  1000. {
  1001. break;
  1002. }
  1003. rt_thread_delay(50);
  1004. time_out --;
  1005. }
  1006. beep_set(0, 0); //200kzh - 50%
  1007. rt_thread_delay(50);
  1008. }
  1009. /*************************************************
  1010. Function: ctr_Petroleum_jelly
  1011. Description: 涂凡士林
  1012. Calls:
  1013. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1014. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1015. Input: NULL
  1016. Output: NULL
  1017. Return: NULL
  1018. Others: NULL
  1019. *************************************************/
  1020. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1021. {
  1022. beep_init();
  1023. pwm_on(2,50000);
  1024. rt_thread_delay(delay*1000);
  1025. pwm_off(2,0);
  1026. rt_thread_delay(1000);
  1027. pwm_on(3, 50000);
  1028. rt_thread_delay(delay*1000);
  1029. pwm_off(3,0);
  1030. device_status.fsl_sw = 0;
  1031. }
  1032. //1.0转仓
  1033. void turnzp(void)
  1034. {
  1035. LOG_W("转仓执行!");
  1036. static uint16_t turn_timeout = 0;
  1037. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1038. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1039. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1040. // rt_pin_write(CLEAR_R,OFF);
  1041. // rt_pin_write(CLEAR_L,OFF);
  1042. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1043. {
  1044. rt_thread_delay(20);
  1045. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1046. {
  1047. ctr_Collect_Fan(ON);
  1048. rt_thread_delay(50);
  1049. turn_timeout++;
  1050. if(turn_timeout==600)
  1051. {
  1052. LOG_E("检测不到限位,电机强制停止");
  1053. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1054. DTU_Write("%s",ctr_zp_limit_warn);
  1055. rt_free(ctr_zp_limit_warn);
  1056. break;
  1057. }
  1058. }
  1059. break;
  1060. }
  1061. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1062. {
  1063. rt_thread_delay(20);
  1064. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1065. {
  1066. // rt_pin_write(CLEAR_R,ON);
  1067. // rt_pin_write(CLEAR_L,OFF);
  1068. ctr_Collect_Fan(ON);
  1069. rt_thread_delay(50);
  1070. turn_timeout++;
  1071. if(turn_timeout==600)
  1072. {
  1073. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1074. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1075. DTU_Write("%s",ctr_zp_limit_warn);
  1076. rt_free(ctr_zp_limit_warn);
  1077. break;
  1078. }
  1079. }
  1080. break;
  1081. }
  1082. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1083. {
  1084. rt_thread_delay(20);
  1085. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1086. {
  1087. // rt_pin_write(CLEAR_R,OFF);
  1088. // rt_pin_write(CLEAR_L,OFF);
  1089. ctr_Collect_Fan(OFF);
  1090. break;
  1091. }
  1092. else {
  1093. break;
  1094. }
  1095. }
  1096. ctr_Collect_Fan(OFF);
  1097. turn_timeout = 0;
  1098. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1099. rt_thread_delay(1000);
  1100. }
  1101. //清扫-常闭型限位开关
  1102. //新结构使用推杆电机做清扫
  1103. uint8_t ctr_clear(uint16_t status_clear)
  1104. {
  1105. static uint16_t clear_num = 0;
  1106. #if PWM_EN == 0
  1107. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1108. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1109. #endif
  1110. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1111. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1112. /*************************************************************外接清扫
  1113. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1114. rt_pin_write(POWER_CLEAR,ON);
  1115. rt_thread_delay(1000*100);
  1116. rt_pin_write(POWER_CLEAR,OFF);
  1117. ************************************************************/
  1118. //停止
  1119. #if PWM_EN == 0
  1120. rt_pin_write(CLEAR_R,OFF);
  1121. rt_pin_write(CLEAR_L,OFF);
  1122. #else
  1123. Motor_Qingsao_R_PWM(OFF);
  1124. Motor_Qingsao_L_PWM(OFF);
  1125. #endif
  1126. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1127. rt_thread_delay(100);
  1128. //如果清扫位置在中间
  1129. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1130. {
  1131. #if PWM_EN == 0
  1132. rt_pin_write(CLEAR_R,ON);
  1133. rt_pin_write(CLEAR_L,OFF);
  1134. #else
  1135. Motor_Qingsao_R_PWM(ON);
  1136. Motor_Qingsao_L_PWM(OFF);
  1137. #endif
  1138. rt_thread_delay(50);
  1139. clear_num++;
  1140. //已到达检测位置
  1141. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1142. {
  1143. #if PWM_EN == 0
  1144. rt_pin_write(CLEAR_R,ON);
  1145. rt_pin_write(CLEAR_L,ON);
  1146. #else
  1147. Motor_Qingsao_R_PWM(OFF);
  1148. Motor_Qingsao_L_PWM(OFF);
  1149. #endif
  1150. rt_thread_delay(2000);
  1151. break;
  1152. }
  1153. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1154. {
  1155. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1156. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1157. #if PWM_EN == 0
  1158. rt_pin_write(CLEAR_R,ON);
  1159. rt_pin_write(CLEAR_L,ON);
  1160. #else
  1161. Motor_Qingsao_R_PWM(OFF);
  1162. Motor_Qingsao_L_PWM(OFF);
  1163. #endif
  1164. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1165. if(Read_Real_Voltage()/100<12)
  1166. {
  1167. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1168. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1169. device_status.clear_status = 0;
  1170. return 0;
  1171. }
  1172. break;
  1173. }
  1174. }
  1175. clear_num = 0;
  1176. //如果清扫位置在左侧
  1177. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1178. {
  1179. #if PWM_EN == 0
  1180. rt_pin_write(CLEAR_R,ON);
  1181. rt_pin_write(CLEAR_L,OFF);
  1182. #else
  1183. Motor_Qingsao_R_PWM(ON);
  1184. Motor_Qingsao_L_PWM(OFF);
  1185. #endif
  1186. rt_thread_delay(50);
  1187. clear_num++;
  1188. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1189. {
  1190. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1191. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1192. #if PWM_EN == 0
  1193. rt_pin_write(CLEAR_R,ON);
  1194. rt_pin_write(CLEAR_L,ON);
  1195. #else
  1196. Motor_Qingsao_R_PWM(OFF);
  1197. Motor_Qingsao_L_PWM(OFF);
  1198. #endif
  1199. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1200. if(Read_Real_Voltage()/100<12)
  1201. {
  1202. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1203. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1204. device_status.clear_status = 0;
  1205. return 0;
  1206. }
  1207. break;
  1208. }
  1209. }
  1210. clear_num = 0;
  1211. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1212. {
  1213. #if PWM_EN == 0
  1214. rt_pin_write(CLEAR_R,ON);
  1215. rt_pin_write(CLEAR_L,ON);
  1216. #else
  1217. Motor_Qingsao_R_PWM(OFF);
  1218. Motor_Qingsao_L_PWM(OFF);
  1219. #endif
  1220. if(Read_Real_Voltage()/100<12)
  1221. {
  1222. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1223. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1224. device_status.clear_status = 0;
  1225. return 0;
  1226. }
  1227. rt_thread_delay(2000);
  1228. break;
  1229. }
  1230. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1231. {
  1232. #if PWM_EN == 0
  1233. rt_pin_write(CLEAR_R,OFF);
  1234. rt_pin_write(CLEAR_L,ON);
  1235. #else
  1236. Motor_Qingsao_R_PWM(OFF);
  1237. Motor_Qingsao_L_PWM(ON);
  1238. #endif
  1239. rt_thread_delay(50);
  1240. clear_num++;
  1241. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1242. {
  1243. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1244. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1245. //DTU_Write("%s",Clear_ERR);
  1246. #if PWM_EN == 0
  1247. rt_pin_write(CLEAR_R,ON);
  1248. rt_pin_write(CLEAR_L,ON);
  1249. #else
  1250. Motor_Qingsao_R_PWM(OFF);
  1251. Motor_Qingsao_L_PWM(OFF);
  1252. #endif
  1253. rt_free(Clear_ERR);
  1254. if(Read_Real_Voltage()/100<12)
  1255. {
  1256. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1257. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1258. device_status.clear_status = 0;
  1259. return 0;
  1260. }
  1261. break;
  1262. }
  1263. }
  1264. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1265. {
  1266. #if PWM_EN == 0
  1267. rt_pin_write(CLEAR_R,ON);
  1268. rt_pin_write(CLEAR_L,ON);
  1269. #else
  1270. Motor_Qingsao_R_PWM(OFF);
  1271. Motor_Qingsao_L_PWM(OFF);
  1272. #endif
  1273. rt_thread_delay(2000);
  1274. break;
  1275. }
  1276. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1277. {
  1278. #if PWM_EN == 0
  1279. rt_pin_write(CLEAR_R,ON);
  1280. rt_pin_write(CLEAR_L,ON);
  1281. #else
  1282. Motor_Qingsao_R_PWM(OFF);
  1283. Motor_Qingsao_L_PWM(OFF);
  1284. #endif
  1285. if(device_status.version_type==41)
  1286. {
  1287. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1288. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1289. //DTU_Write("%s",Clear_ERR);
  1290. rt_free(Clear_ERR);
  1291. }
  1292. return 0;
  1293. }
  1294. clear_num = 0;
  1295. rt_thread_delay(2000);
  1296. #if PWM_EN == 0
  1297. rt_pin_write(CLEAR_R,ON);
  1298. rt_pin_write(CLEAR_L,ON);
  1299. #else
  1300. Motor_Qingsao_R_PWM(OFF);
  1301. Motor_Qingsao_L_PWM(OFF);
  1302. #endif
  1303. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1304. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1305. device_status.clear_status = 0;
  1306. return 1;
  1307. }
  1308. //路由器供电
  1309. void ctr_router(uint8_t status_router)
  1310. {
  1311. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1312. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1313. if(status_router==ON)
  1314. {
  1315. rt_pin_write(ROUTER,ON);
  1316. }
  1317. else {
  1318. rt_pin_write(ROUTER,OFF);
  1319. }
  1320. }
  1321. //清扫-推杆电机
  1322. void ctr_TG_Clear(uint16_t clear_timeout)
  1323. {
  1324. #if 0
  1325. int current_clear[200];
  1326. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1327. LOG_I("清扫电机待机:%d",Electric_current);
  1328. #endif
  1329. static uint16_t clear_count = 0;
  1330. /* 屏幕 workSta.t6「清扫结束剩余时间」用的尾段常量:
  1331. * 全程 = 正向推杆(clear_timeout*100ms) + 3s 停顿 + 反向复位(clear_timeout*100ms) + 2s 收尾 */
  1332. uint16_t clear_left_tail = (uint16_t)(clear_timeout / 10) + 3 + 2;
  1333. clear_count = clear_timeout;
  1334. #if PWM_EN == 0
  1335. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1336. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1337. #endif
  1338. while(clear_count!=0)
  1339. {
  1340. #if PWM_EN == 0
  1341. rt_pin_write(CLEAR_R,OFF);
  1342. rt_pin_write(CLEAR_L,ON);
  1343. #else
  1344. #if QINGSAO_IO == 1
  1345. Motor_Qingsao_R_PWM(OFF);
  1346. Motor_Qingsao_L_PWM(ON);
  1347. #else
  1348. Motor_Qingsao_R_PWM(ON);
  1349. Motor_Qingsao_L_PWM(OFF);
  1350. #endif
  1351. #endif
  1352. clear_count--;
  1353. device_status.clear_left = (uint16_t)(clear_count / 10) + clear_left_tail; /* t6 剩余清扫时间(s) */
  1354. rt_thread_delay(100);
  1355. }
  1356. #if PWM_EN == 0
  1357. rt_pin_write(CLEAR_R,ON);
  1358. rt_pin_write(CLEAR_L,ON);
  1359. #else
  1360. Motor_Qingsao_R_PWM(OFF);
  1361. Motor_Qingsao_L_PWM(OFF);
  1362. #endif
  1363. rt_thread_delay(3000);
  1364. clear_count = 0;
  1365. while(clear_timeout!=clear_count)
  1366. {
  1367. #if PWM_EN == 0
  1368. rt_pin_write(CLEAR_R,ON);
  1369. rt_pin_write(CLEAR_L,OFF);
  1370. #else
  1371. #if QINGSAO_IO == 1
  1372. Motor_Qingsao_R_PWM(ON);
  1373. Motor_Qingsao_L_PWM(OFF);
  1374. #else
  1375. Motor_Qingsao_R_PWM(OFF);
  1376. Motor_Qingsao_L_PWM(ON);
  1377. #endif
  1378. #endif
  1379. // if(clear_count<200)
  1380. // current_clear[clear_count] = atoi(device_status.power);
  1381. clear_count++;
  1382. device_status.clear_left = (uint16_t)((clear_timeout - clear_count) / 10) + 2; /* t6 反向复位剩余(s) */
  1383. rt_thread_delay(100);
  1384. }
  1385. #if PWM_EN == 0
  1386. rt_pin_write(CLEAR_R,ON);
  1387. rt_pin_write(CLEAR_L,ON);
  1388. #else
  1389. Motor_Qingsao_R_PWM(OFF);
  1390. Motor_Qingsao_L_PWM(OFF);
  1391. #endif
  1392. #if 0
  1393. bubblesort(current_clear,200);
  1394. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1395. {
  1396. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1397. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1398. DTU_Write("%s",ctr_clear_warn);
  1399. rt_free(ctr_clear_warn);
  1400. }
  1401. #endif
  1402. rt_thread_delay(2000);
  1403. device_status.clear_status = 0;
  1404. device_status.clear_left = 0; /* 清扫结束,倒计时清零 */
  1405. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1406. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1407. }
  1408. // 全局静态变量,保存上次关闭的 tick
  1409. static rt_uint32_t s_last_off_tick = 0;
  1410. void ctr_RTU_DTU(uint8_t status_rtu)
  1411. {
  1412. // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
  1413. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1414. if (status_rtu == ON)
  1415. {
  1416. // 开机逻辑:必须满足冷却时间才能开机
  1417. rt_uint32_t current_tick = rt_tick_get();
  1418. rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
  1419. if (s_last_off_tick != 0)
  1420. {
  1421. rt_uint32_t diff_tick = current_tick - s_last_off_tick;
  1422. if (diff_tick < cool_ticks)
  1423. {
  1424. // 未冷却完成,计算剩余时间
  1425. rt_uint32_t wait_ticks = cool_ticks - diff_tick;
  1426. rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
  1427. LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
  1428. // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
  1429. rt_thread_delay(wait_ticks);
  1430. }
  1431. }
  1432. // 冷却完成,执行开机
  1433. rt_pin_write(RTU_DTU, ON);
  1434. s_last_off_tick = 0; // 清除关机时间
  1435. LOG_W("RTU电源启动!!!");
  1436. }
  1437. else
  1438. {
  1439. // 关机逻辑:记录关机时刻tick
  1440. rt_pin_write(RTU_DTU, OFF);
  1441. s_last_off_tick = rt_tick_get();
  1442. LOG_W("RTU电源关闭:%d",s_last_off_tick);
  1443. }
  1444. }
  1445. /**
  1446. * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
  1447. */
  1448. void RTU_Power_Off_Safe(void)
  1449. {
  1450. /* 2. 【关键】关闭串口设备。
  1451. * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
  1452. * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
  1453. */
  1454. rt_device_close(dtuserial);
  1455. /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
  1456. * extern UART_HandleTypeDef huart2;
  1457. * HAL_UART_DeInit(&huart2);
  1458. */
  1459. /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
  1460. * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
  1461. */
  1462. rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
  1463. rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
  1464. /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
  1465. rt_thread_mdelay(50);
  1466. /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
  1467. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1468. rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
  1469. LOG_I("RTU 12V Power OFF safely.\n");
  1470. }
  1471. /**
  1472. * @brief 恢复 RTU 12V 电源并重新初始化串口
  1473. */
  1474. void RTU_Power_On_Restore(void)
  1475. {
  1476. rt_thread_mdelay(5000);
  1477. /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
  1478. rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
  1479. LOG_I("RTU 12V Power ON.\n");
  1480. /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
  1481. * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
  1482. */
  1483. rt_thread_mdelay(5000);
  1484. /* 3. 【关键】重新打开串口设备。
  1485. * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为
  1486. * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。
  1487. */
  1488. GPIO_InitTypeDef gpioInitStruct;
  1489. __HAL_RCC_GPIOA_CLK_ENABLE();
  1490. //PA2 TXD
  1491. gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式
  1492. gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9
  1493. gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率
  1494. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1495. //PA3 RXD
  1496. gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式
  1497. gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10
  1498. gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率
  1499. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1500. #ifdef RT_USING_SERIAL_V2
  1501. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  1502. if(uart_res!=RT_EOK)
  1503. {
  1504. LOG_E("%s打开失败!",RTU_UART_NAME);
  1505. }else{
  1506. LOG_I("%s Open Success!",RTU_UART_NAME);
  1507. }
  1508. #endif
  1509. #ifdef RT_USING_SERIAL_V1
  1510. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  1511. #endif
  1512. /* 4. 【关键】重新注册接收回调
  1513. * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
  1514. * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
  1515. * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
  1516. */
  1517. dtu_uart2_reinit_rx();
  1518. }
  1519. /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
  1520. void cmd_rtu_restart(int argc, char** argv)
  1521. {
  1522. rt_kprintf("Starting RTU safe restart sequence...\n");
  1523. // 1. 安全断电
  1524. RTU_Power_Off_Safe();
  1525. // 2. 保持断电 30 秒 (确保电容彻底放电)
  1526. rt_kprintf("Waiting 30s for capacitor discharge...\n");
  1527. rt_thread_mdelay(30000);
  1528. // 3. 恢复供电并重建串口
  1529. RTU_Power_On_Restore();
  1530. rt_kprintf("RTU restart sequence completed.\n");
  1531. }
  1532. //海康拍照
  1533. void Hk_Take_io(void)
  1534. {
  1535. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1536. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1537. rt_pin_write(HK_TAKE, 1);
  1538. rt_pin_write(HK_IO, 0);
  1539. if(device_status.dtype==3)
  1540. Beep_Tip(1000);
  1541. if(device_status.dtype==7)
  1542. rt_thread_mdelay(1000);
  1543. rt_pin_write(HK_TAKE, 0);
  1544. rt_pin_write(HK_IO, 1);
  1545. }
  1546. void calorstat(uint16_t heat_time)
  1547. {
  1548. //uint8_t heat_up_cnt = 0;
  1549. uint16_t n = 0;
  1550. uint16_t delay_heat = 60;
  1551. FlagStatus heat_flg = RESET;
  1552. device_status.heat_time_min = 0;
  1553. device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
  1554. ctr_Heat(ON);
  1555. // UpLoad_Data(); //开始加热时的第一条数据
  1556. //恒温模式
  1557. for(n=0;n < heat_time;n++)
  1558. {
  1559. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1560. {
  1561. heat_flg = SET;
  1562. ctr_Heat(OFF);
  1563. while(delay_heat!=0)
  1564. {
  1565. device_status.C01 &= ~(1<<1); //加热 正常
  1566. delay_heat --;
  1567. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1568. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1569. rt_thread_delay(1000);
  1570. if(device_status.heattem>device_status.heat_tem_max + 1){
  1571. device_status.heat_tem_max = device_status.heattem;
  1572. }
  1573. }
  1574. delay_heat = 60;
  1575. //跳出FOR循环
  1576. n = n + 60;
  1577. }
  1578. if(device_status.heattem>device_status.run_sta + 1){
  1579. device_status.heat_tem_max = device_status.heattem;
  1580. }
  1581. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1582. {
  1583. device_status.heat_tem_max = device_status.heattem;
  1584. ctr_Heat(ON);
  1585. }
  1586. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1587. {
  1588. // if(heat_up_cnt==0)
  1589. // {
  1590. // UpLoad_Data(); //加热到设定温度的数据
  1591. // heat_up_cnt = 1;
  1592. // }
  1593. device_status.heat_tem_max = device_status.heattem;
  1594. ctr_Heat(OFF);
  1595. }
  1596. if((n % 60)==0)
  1597. {
  1598. device_status.heat_time_min = device_status.heat_time_min + 1;
  1599. if(device_status.heat_time_min % 5 == 0) //加热5分钟温度低于50,判定加热故障
  1600. {
  1601. if(device_status.heattem<50) {
  1602. device_status.C01 |= (1 << 1); // 第1位变为1,加热故障
  1603. }else {
  1604. device_status.C01 &= ~(1<<1); //加热 正常
  1605. }
  1606. }
  1607. }
  1608. /* 屏幕 workSta.t1:剩余加热时间(秒)。恒温等待阶段 n 未推进,剩余时间按同一口径显示 */
  1609. device_status.work_heat_left = (n < heat_time) ? (uint16_t)(heat_time - n) : 0;
  1610. //如果触发异常退出加热
  1611. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1612. {
  1613. rt_thread_delay(1000);
  1614. if(device_status.bat_status == 1)
  1615. {
  1616. rt_kprintf("============================触发异常退出加热==========================");
  1617. ctr_lamp(OFF,1);
  1618. ctr_Heat(OFF);
  1619. break;
  1620. }
  1621. }
  1622. if(device_status.work_mode==1){
  1623. LOG_W("切换模式,退出加热!");
  1624. break;
  1625. }
  1626. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1627. {
  1628. ctr_Heat(OFF);
  1629. rt_kprintf("异常退出!!!\n");
  1630. break;
  1631. }
  1632. rt_thread_delay(1000);
  1633. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1634. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1635. }
  1636. device_status.work_heat_left = 0; /* 加热结束,倒计时清零 */
  1637. ctr_Heat(OFF);
  1638. //UpLoad_Data(); //加热结束的数据
  1639. }
  1640. //拍照
  1641. void HK_TakePhoto(void)
  1642. {
  1643. //打开补光灯
  1644. //DTU_Write("takephoto.mp3");
  1645. rt_thread_delay(10000);
  1646. rt_timer_start(timer_takephoto);
  1647. char *takephotojson = makeJson_takephoto();
  1648. DTU_Write("%s",takephotojson);
  1649. rt_free(takephotojson);
  1650. //吸虫塔
  1651. if(device_status.version_type!=6)
  1652. {
  1653. ctr_led(ON);
  1654. if(device_status.version_type==48)
  1655. {
  1656. rt_thread_delay(1000*60);
  1657. }
  1658. else {
  1659. rt_thread_delay(1000*15);
  1660. }
  1661. }
  1662. //触发海康相机拍照
  1663. Hk_Take_io();
  1664. if(device_status.version_type!=6)
  1665. {
  1666. rt_thread_delay(1000*45);
  1667. }
  1668. else {
  1669. ctr_led(ON);
  1670. rt_thread_delay(1000*19);
  1671. }
  1672. ctr_led(OFF);
  1673. }
  1674. //光控测试任务
  1675. void SunTest(void)
  1676. {
  1677. rt_uint16_t ui_timeout_l = 0;
  1678. //未检测到光照,一秒扫描等待,超时30S
  1679. while(Read_Light_Voltage()<500)
  1680. {
  1681. ui_timeout_l ++;
  1682. rt_thread_delay(1000);
  1683. if(ui_timeout_l==30) break;
  1684. }
  1685. rt_thread_delay(3000);
  1686. if(Read_Light_Voltage()<500)
  1687. {
  1688. device_status.sun_status = 0;//光控状态
  1689. }
  1690. if(Read_Light_Voltage()>500)
  1691. {
  1692. rt_thread_delay(1000);
  1693. }
  1694. rt_thread_delay(3000);
  1695. if(Read_Light_Voltage()>500)
  1696. {
  1697. device_status.sun_status = 1;
  1698. ctr_lamp(OFF,1);
  1699. }
  1700. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1701. device_status.sun_test = 0;
  1702. device_status.work_status=0;
  1703. }
  1704. /*上电初始化*/
  1705. void ResetInit(void)
  1706. {
  1707. /* 复位时清掉屏幕 workSta 页的实时倒计时/进度(t1~t6),避免残留旧值 */
  1708. device_status.work_heat_left = 0;
  1709. device_status.work_clt_left = 0;
  1710. device_status.work_door_left = 0;
  1711. device_status.work_progress = 0;
  1712. device_status.photo_left = 0;
  1713. device_status.clear_left = 0;
  1714. device_status.work_start_tick = 0;
  1715. device_status.work_total_sec = 0;
  1716. if(device_status.version_type!=6&&device_status.version_type!=7)
  1717. {
  1718. device_status.work_done = 1;
  1719. //1.打开路由器供电
  1720. ctr_router(ON);
  1721. //2.打开相机供电
  1722. ctr_photo(ON);
  1723. ctr_RTU_DTU(ON);
  1724. //3.关闭镇流器
  1725. ctr_lamp(OFF,1);
  1726. //4.关闭震动电机
  1727. ctr_shake(OFF, 0);
  1728. //5.关闭加热
  1729. ctr_Heat(OFF);
  1730. //6.关闭补光灯
  1731. if(device_status.photo_ws==0)
  1732. ctr_led(OFF);
  1733. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1734. {
  1735. LOG_W("欠压!");
  1736. return;
  1737. }
  1738. //7.关闭上仓门
  1739. device_status.ctr_up_motor = 2;
  1740. // while(device_status.ctr_up_motor!=0)
  1741. // {
  1742. // LOG_I("关闭上仓门...");
  1743. // rt_thread_delay(100);
  1744. // }
  1745. //8.关闭下仓门
  1746. device_status.ctr_down_motor = 2;
  1747. while(device_status.ctr_down_motor!=0)
  1748. {
  1749. LOG_I("关闭下仓门....");
  1750. rt_thread_delay(100);
  1751. }
  1752. //9.清扫
  1753. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1754. {
  1755. LOG_W("异常退出!!!\n");
  1756. return;
  1757. }
  1758. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  1759. {
  1760. if(device_status.photo_ws==0) device_status.clear_status=1;
  1761. while(device_status.clear_status==1){
  1762. rt_thread_delay(1000);
  1763. }
  1764. #if JINGJIKUAN == 1
  1765. //再执行4次清扫
  1766. for(int i=0;i<4;i++)
  1767. {
  1768. if(device_status.photo_ws==0) device_status.clear_status=1;
  1769. while(device_status.clear_status==1){
  1770. rt_thread_delay(1000);
  1771. }
  1772. }
  1773. #endif
  1774. }
  1775. }
  1776. else {
  1777. if(device_status.power_status==1)
  1778. {
  1779. //1.打开路由器供电
  1780. #if MATCH_DEVICE ==0
  1781. ctr_router(ON);
  1782. //2.打开相机供电
  1783. ctr_photo(ON);
  1784. #endif
  1785. ctr_RTU_DTU(ON);
  1786. }
  1787. }
  1788. }
  1789. /*三色光源*/
  1790. void Led_Check(uint8_t tp)
  1791. {
  1792. if(tp==3)
  1793. {
  1794. if(device_status.light_over_tim>2*3600)
  1795. {
  1796. device_status.work_status = 1;
  1797. device_status.lamp_status = 1;
  1798. device_status.lamp1_status = 0;
  1799. device_status.lamp2_status = 0;
  1800. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1801. }
  1802. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1803. device_status.work_status = 1;
  1804. device_status.lamp_status = 0;
  1805. device_status.lamp1_status = 1;
  1806. device_status.lamp2_status = 0;
  1807. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1808. }
  1809. else if (device_status.light_over_tim<1*3600) {
  1810. device_status.work_status = 1;
  1811. device_status.lamp_status = 0;
  1812. device_status.lamp1_status = 0;
  1813. device_status.lamp2_status = 1;
  1814. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1815. }
  1816. }
  1817. else if (tp==6) {
  1818. if(device_status.light_over_tim>4*3600)
  1819. {
  1820. device_status.work_status = 1;
  1821. device_status.lamp_status = 1;
  1822. device_status.lamp1_status = 0;
  1823. device_status.lamp2_status = 0;
  1824. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1825. }
  1826. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1827. device_status.work_status = 1;
  1828. device_status.lamp_status = 0;
  1829. device_status.lamp1_status = 1;
  1830. device_status.lamp2_status = 0;
  1831. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1832. }
  1833. else if (device_status.light_over_tim<7200) {
  1834. device_status.work_status = 1;
  1835. device_status.lamp_status = 0;
  1836. device_status.lamp1_status = 0;
  1837. device_status.lamp2_status = 1;
  1838. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1839. }
  1840. }
  1841. else if (tp==9) {
  1842. if(device_status.light_over_tim>6*3600)
  1843. {
  1844. device_status.work_status = 1;
  1845. device_status.lamp_status = 1;
  1846. device_status.lamp1_status = 0;
  1847. device_status.lamp2_status = 0;
  1848. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1849. }
  1850. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1851. device_status.work_status = 1;
  1852. device_status.lamp_status = 0;
  1853. device_status.lamp1_status = 1;
  1854. device_status.lamp2_status = 0;
  1855. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1856. }
  1857. else if (device_status.light_over_tim<3*3600) {
  1858. device_status.work_status = 1;
  1859. device_status.lamp_status = 0;
  1860. device_status.lamp1_status = 0;
  1861. device_status.lamp2_status = 1;
  1862. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1863. }
  1864. }
  1865. }
  1866. void WorkCheck(void)
  1867. {
  1868. static rt_uint32_t light_timeout = 3600;
  1869. while(device_status.ds == 1 &&
  1870. device_status.rain_status == 0 &&
  1871. device_status.temp_status == 0 &&
  1872. device_status.bat_status == 0)
  1873. {
  1874. if(device_status.timeSwitch == 1) // 时控模式
  1875. {
  1876. LOG_I("***********时控*****************");
  1877. time_t now;
  1878. time(&now);
  1879. struct tm now_tm = *localtime(&now);
  1880. if(device_status.startBar == device_status.stopBar)
  1881. {
  1882. // 开始结束时间相同则24小时模式
  1883. device_status.work_status = 1;
  1884. rt_thread_delay(10000);
  1885. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1886. device_status.work_status ? "工作" : "待机",
  1887. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1888. device_status.startBar, device_status.stopBar);
  1889. continue;
  1890. }
  1891. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1892. rt_uint32_t start_sec = device_status.startBar * 3600;
  1893. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1894. // 修复跨天判断逻辑
  1895. if (start_sec < end_sec)
  1896. {
  1897. // 不跨天模式
  1898. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1899. }
  1900. else
  1901. {
  1902. // 跨天模式
  1903. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1904. }
  1905. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1906. device_status.work_status ? "工作" : "待机",
  1907. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1908. device_status.startBar, device_status.stopBar);
  1909. rt_thread_delay(10000);
  1910. }
  1911. else // 光控模式
  1912. {
  1913. LOG_I("光控定时模式!");
  1914. if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
  1915. {
  1916. device_status.light_over_tim--;
  1917. light_timeout = 3600; // 重置超时计数器
  1918. device_status.work_status = 1;
  1919. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1920. }
  1921. else
  1922. {
  1923. // 仅在白天递减超时计数器
  1924. if(device_status.sun_status == 1 && light_timeout > 0) {
  1925. light_timeout--;
  1926. }
  1927. device_status.work_status = 0;
  1928. //没有在测试并且没有在自检
  1929. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1930. {
  1931. ctr_lamp(OFF,1);
  1932. }
  1933. // 连续1小时白天后重置
  1934. if(light_timeout == 0) {
  1935. device_status.light_over_tim = 0;
  1936. light_timeout = 3600; // 重置防止负数
  1937. device_status.light_over_tim = device_status.sunBar * 3600;
  1938. }
  1939. }
  1940. }
  1941. rt_thread_delay(1000);
  1942. }
  1943. // 异常状态处理
  1944. device_status.work_status = 0;
  1945. rt_thread_delay(1000);
  1946. }
  1947. //工作任务流程s
  1948. /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ====================
  1949. * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。
  1950. * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(85s) + 仓门动作预留(40s)。
  1951. * 实际存在仓门寻位、恒温等待、异常提前退出等偏差,故进度上限钳到 99%,
  1952. * 与 t1~t3/t5/t6 的实时倒计时配合显示即可反映真实过程;非工作态返回 0。 */
  1953. void Work_Progress_Start(void)
  1954. {
  1955. uint32_t total;
  1956. total = (uint32_t)device_status.clt_time * 60
  1957. + (uint32_t)device_status.heattime * 60
  1958. + WORK_PHOTO_SEC + WORK_CLEAR_SEC + WORK_DOOR_SEC;
  1959. if (total == 0) total = 1;
  1960. if (total > 0xFFFF) total = 0xFFFF; /* work_total_sec 为 uint16_t */
  1961. device_status.work_total_sec = (uint16_t)total;
  1962. device_status.work_start_tick = rt_tick_get();
  1963. device_status.work_progress = 0;
  1964. /* 新的一轮工作开始,清掉上一轮的实时倒计时 */
  1965. device_status.work_heat_left = 0;
  1966. device_status.work_clt_left = 0;
  1967. device_status.work_door_left = 0;
  1968. device_status.photo_left = 0;
  1969. device_status.clear_left = 0;
  1970. }
  1971. uint8_t Work_Progress_Get(void)
  1972. {
  1973. uint32_t elapsed;
  1974. uint32_t total = device_status.work_total_sec;
  1975. if (device_status.work_status != 1 || device_status.work_start_tick == 0 || total == 0)
  1976. {
  1977. return 0;
  1978. }
  1979. elapsed = (rt_tick_get() - device_status.work_start_tick) / RT_TICK_PER_SECOND;
  1980. device_status.work_progress = (uint8_t)((elapsed >= total) ? 99 : (elapsed * 100 / total));
  1981. return device_status.work_progress;
  1982. }
  1983. void WorkTask(void)
  1984. {
  1985. static int clt_time = 0; //收集时间差秒数
  1986. static int heat_time = 0;
  1987. LOG_I("工作状态:%d",device_status.work_status);
  1988. LOG_I("开关机状态:%d",device_status.ds);
  1989. while(device_status.clear_status==1){
  1990. LOG_I("等待清扫执行完成....");
  1991. rt_thread_delay(1000);
  1992. }
  1993. 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)
  1994. {
  1995. if(device_status.work_mode==1){
  1996. break;
  1997. }
  1998. device_status.work_done = 0;
  1999. /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长,
  2000. * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。 */
  2001. Work_Progress_Start();
  2002. if(clt_time==0)
  2003. {
  2004. DTU_Write("workstart.mp3");
  2005. if(device_status.debug_ws==1)
  2006. {
  2007. time_t stamp=time(RT_NULL);
  2008. static struct tm tm;
  2009. gmtime_r(&stamp, &tm);
  2010. 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);
  2011. dtu_write_json(makeJson_Warn("Work start!", device_status.stamp));
  2012. }
  2013. }
  2014. ctr_lamp(ON,1);
  2015. LOG_I("打开灯管!!!");
  2016. if(device_status.version_type!=5)
  2017. {
  2018. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  2019. //2.打开上仓门
  2020. LOG_I("打开上仓门!!!");
  2021. device_status.ctr_up_motor = 1;
  2022. //3.收集
  2023. clt_time = device_status.clt_time * 60;
  2024. while(clt_time!=1)
  2025. {
  2026. clt_time --;
  2027. device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */
  2028. rt_thread_delay(1000);
  2029. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2030. if(device_status.work_mode==1){
  2031. break;
  2032. }
  2033. //如果触发异常
  2034. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2035. {
  2036. ctr_lamp(OFF,1);
  2037. #if 0//LAMP_EN == 0
  2038. ctr_Fan_IN(OFF);
  2039. #endif
  2040. rt_kprintf("异常退出!!!\n");
  2041. break;
  2042. }
  2043. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2044. {
  2045. ctr_lamp(OFF,1);
  2046. #if 0//LAMP_EN == 0
  2047. ctr_Fan_IN(OFF);
  2048. #endif
  2049. rt_kprintf("异常退出!!!\n");
  2050. break;
  2051. }
  2052. }
  2053. clt_time = 1;
  2054. device_status.work_clt_left = 0; /* 收集结束,倒计时清零 */
  2055. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2056. {
  2057. ctr_lamp(OFF,1);
  2058. #if 0//LAMP_EN == 0
  2059. ctr_Fan_IN(OFF);
  2060. #endif
  2061. rt_kprintf("异常退出!!!\n");
  2062. break;
  2063. }
  2064. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2065. {
  2066. ctr_lamp(OFF,1);
  2067. #if 0//LAMP_EN == 0
  2068. ctr_Fan_IN(OFF);
  2069. #endif
  2070. rt_kprintf("异常退出!!!\n");
  2071. break;
  2072. }
  2073. #if 0//LAMP_EN == 0
  2074. ctr_Fan_IN(OFF);
  2075. #endif
  2076. //4.关闭上仓门
  2077. device_status.ctr_up_motor = 2;
  2078. rt_kprintf("关闭上仓门!!!\n");
  2079. while(device_status.ctr_up_motor!=0)
  2080. {
  2081. rt_thread_delay(100);
  2082. LOG_I("等待上仓门关闭!!!");
  2083. }
  2084. //5.加热
  2085. if(device_status.heat_sw==0)
  2086. {
  2087. calorstat(device_status.heattime*60);
  2088. }
  2089. else {
  2090. heat_time = device_status.heattime * 60;
  2091. while(heat_time!=0)
  2092. {
  2093. if(device_status.work_mode==1)
  2094. {
  2095. break;
  2096. }
  2097. heat_time--;
  2098. device_status.work_heat_left = (uint16_t)heat_time; /* 屏幕 workSta.t1 剩余加热时间(s) */
  2099. LOG_I("正在加热状态!!!");
  2100. rt_thread_delay(1000);
  2101. }
  2102. }
  2103. device_status.work_heat_left = 0;
  2104. #if TG_MODULE == 0
  2105. //6.打开下仓门
  2106. if(device_status.rain_status==0)
  2107. {
  2108. device_status.ctr_down_motor =1;
  2109. }
  2110. else {
  2111. device_status.ctr_down_motor = 2;
  2112. device_status.ctr_up_motor = 2;
  2113. }
  2114. //待下仓门完全打开
  2115. while(device_status.ctr_down_motor!=0)
  2116. {
  2117. rt_thread_delay(100);
  2118. rt_kprintf("等待下仓门打开!!!\n");
  2119. }
  2120. rt_thread_delay(3000);
  2121. //关闭下仓门
  2122. device_status.ctr_down_motor = 2;
  2123. while(device_status.ctr_down_motor!=0)
  2124. {
  2125. rt_thread_delay(100);
  2126. rt_kprintf("等待下仓门关闭!!!\n");
  2127. }
  2128. //2022-04-12add:
  2129. LOG_I("打开上仓门!!!");
  2130. device_status.ctr_up_motor = 1;
  2131. #else
  2132. //打开下仓门
  2133. ctr_cangmen_down(OPEN);
  2134. uint16_t timeout_down_open = 0;
  2135. while(1)
  2136. {
  2137. timeout_down_open++;
  2138. if(rt_pin_read(CTR_UPC_LIMIT)==1||timeout_down_open>20*20) break;
  2139. rt_thread_delay(50);
  2140. }
  2141. ctr_cangmen_down(OFF);
  2142. rt_thread_delay(1000);
  2143. #endif
  2144. if(device_status.work_mode==1){
  2145. break;
  2146. }
  2147. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  2148. {
  2149. //震动
  2150. ctr_shake(ON, device_status.shake_sec);
  2151. //补光&&拍照
  2152. LOG_I("拍照!!!");
  2153. device_status.photo_ws = 1;
  2154. }
  2155. #if TG_MODULE == 1
  2156. while(device_status.photo_ws == 1)
  2157. {
  2158. rt_thread_delay(1000);
  2159. LOG_I("等待拍照完成...");
  2160. }
  2161. rt_thread_delay(1000);
  2162. while(device_status.clear_status==1)
  2163. {
  2164. rt_thread_delay(1000);
  2165. LOG_I("等待清扫完成...");
  2166. }
  2167. ctr_cangmen_down(OPEN);
  2168. //待下仓门完全打开
  2169. uint8_t i = 0;
  2170. while(1)
  2171. {
  2172. i++;
  2173. rt_thread_delay(100);
  2174. rt_kprintf("等待下仓门打开!!!\n");
  2175. if(i>100) {
  2176. ctr_cangmen_down(OFF);
  2177. break;
  2178. }
  2179. }
  2180. rt_thread_delay(2000);
  2181. ctr_shake(ON, device_status.shake_sec);
  2182. device_status.photo_ws = 1;
  2183. //关闭下仓门
  2184. device_status.ctr_down_motor = 2;
  2185. //待下仓门完全关闭
  2186. while(device_status.ctr_down_motor!=0)
  2187. {
  2188. rt_thread_delay(100);
  2189. rt_kprintf("等待下仓门关闭!!!\n");
  2190. }
  2191. device_status.ctr_up_motor = 1; //打开上仓门
  2192. #endif
  2193. }
  2194. }
  2195. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2196. {
  2197. LOG_I("-------------------not work--------------------");
  2198. device_status.work_status = 0;
  2199. }
  2200. //工作结束
  2201. if(device_status.work_done==0&&device_status.work_status==0)
  2202. {
  2203. DTU_Write("workdone");
  2204. ResetInit();
  2205. clt_time = 0;
  2206. }
  2207. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2208. }
  2209. #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
  2210. void slidelock()
  2211. {
  2212. ctr_Motor_Driver_Power(ON); //电机驱动供电
  2213. rt_thread_delay(10);
  2214. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2215. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2216. }
  2217. /* 入口函数-工作状态检测 */
  2218. static void Work_status_thread_entry(void *parameter)
  2219. {
  2220. static uint16_t sun_status_num = 120;
  2221. //如果是光控定时,进行光控监测的消抖
  2222. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2223. {
  2224. rt_kprintf("光控定时!!!\n");
  2225. while(sun_status_num!=0)
  2226. {
  2227. sun_status_num--;
  2228. rt_thread_delay(1000);
  2229. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2230. }
  2231. device_status.light_over_tim = device_status.sunBar * 3600;
  2232. device_status.test_flag = 0;
  2233. }
  2234. while(1)
  2235. {
  2236. WorkCheck();
  2237. }
  2238. }
  2239. //指令
  2240. //
  2241. /*入口函数-工作流程*/
  2242. static void Work_Loop_thread_entry(void *parameter)
  2243. {
  2244. rt_thread_delay(1000);
  2245. ResetInit();
  2246. device_status.light_over_tim = device_status.sunBar * 3600;
  2247. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  2248. static struct tm tm_tg_run;
  2249. static uint8_t alarm_h,alarm_m,alarm_s;
  2250. while(1)
  2251. {
  2252. if(device_status.work_mode==0)
  2253. {
  2254. WorkTask();
  2255. }else{
  2256. if(device_status.work_status==1){
  2257. ctr_lamp(ON, 1);
  2258. }else{
  2259. ctr_lamp(OFF, 1);
  2260. }
  2261. char buf[7] = {0};
  2262. /* ★ 原来用 "%-6s": 6 是"最小宽度"不是截断宽度, alarm_tg 是 char[12],
  2263. 超过 6 字符时会完整写出 12+1 字节到 7 字节的 buf -> 栈溢出 6 字节。
  2264. 必须写成 "%-6.6s"(精度 .6 才是截断)。 */
  2265. rt_snprintf(buf, sizeof(buf), "%-6.6s", device_status.alarm_tg);
  2266. // 截取 时: 前2位
  2267. alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0');
  2268. // 截取 分: 中间2位
  2269. alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0');
  2270. // 截取 秒: 后2位
  2271. alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0');
  2272. device_status.ctr_up_motor = 2; //关闭上仓门
  2273. device_status.ctr_down_motor = 2; //关闭下仓门
  2274. LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s);
  2275. //判断时间
  2276. time_t time_run_tg;
  2277. time(&time_run_tg);
  2278. tm_tg_run =*localtime(&time_run_tg);
  2279. LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec);
  2280. if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s))
  2281. {
  2282. LOG_W("预制执行 开始!");
  2283. dtu_write_json(makeJson_Warn("执行完成", "预制模式"));
  2284. rt_thread_delay(2000);
  2285. //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1}
  2286. cJSON *params = cJSON_CreateObject(); // params 子对象
  2287. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  2288. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  2289. /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串 -> 走 dtu_write_json */
  2290. dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
  2291. rt_thread_delay(20000);
  2292. }
  2293. }
  2294. rt_thread_delay(1000);
  2295. }
  2296. }
  2297. /*入口函数清扫*/
  2298. static void Control_Clear_thread_entry(void *parameter)
  2299. {
  2300. while(1)
  2301. {
  2302. if(device_status.update_flag==1) break;
  2303. if(device_status.clear_status==1)
  2304. {
  2305. if(device_status.version_type==41){
  2306. ctr_TG_Clear(300);
  2307. }else {
  2308. ctr_TG_Clear(400); //20260924-350修改为400
  2309. }
  2310. system_status.cleaning_count +=1; //清扫次数
  2311. }
  2312. if(device_status.clear_status==2)
  2313. {
  2314. turnzp();
  2315. device_status.clear_status = 0;
  2316. }
  2317. rt_thread_delay(100);
  2318. }
  2319. }
  2320. /*入口函数,自检*/
  2321. static void Control_Check_thread_entry(void *parameter)
  2322. {
  2323. static int total_seconds = 200;
  2324. static int current_second = 0;
  2325. static int process = 0;
  2326. device_status.C01 = 0;
  2327. rt_thread_delay(10000);
  2328. while(1)
  2329. {
  2330. if(device_status.update_flag==1) break;
  2331. if(device_status.work_status==1&&device_status.heat_sw==1)
  2332. {
  2333. calorstat(device_status.heattime*60);
  2334. rt_thread_delay(10000);
  2335. }
  2336. else
  2337. {
  2338. if(device_status.selfcheck_ws==2)
  2339. {
  2340. while(current_second <= total_seconds)
  2341. {
  2342. process = (current_second * 100) / total_seconds;
  2343. device_status.process = process / 100.0;
  2344. LOG_I("process:%d",process);
  2345. HMI_WRITE("selfcheck.s106.val=%d",process);
  2346. rt_thread_delay(1000);
  2347. current_second++;
  2348. if(device_status.selfcheck_ws==0)
  2349. {
  2350. HMI_WRITE("selfcheck.s106.val=100");
  2351. break;
  2352. }
  2353. }
  2354. HMI_WRITE("selfcheck.s106.val=100");
  2355. }else {
  2356. process = 0;
  2357. current_second = 0;
  2358. #if 0
  2359. if(device_status.rain_status==1)
  2360. {
  2361. LOG_E("雨控!");
  2362. rt_thread_delay(10000);
  2363. }
  2364. //根据故障码 亮指示灯
  2365. //灯管故障
  2366. if(device_status.C01 & (1 << 0))
  2367. {
  2368. LOG_E("灯管故障!");
  2369. //发送红灯常亮信号
  2370. write_single_holding_register(0x01,0x00C2,0x0011,1000);
  2371. //延时10S
  2372. rt_thread_delay(10000);
  2373. }
  2374. //加热故障
  2375. if(device_status.C01 & (1 << 1))
  2376. {
  2377. LOG_E("加热故障!");
  2378. //发送红灯慢闪信号
  2379. write_single_holding_register(0x01,0x00C2,0x0021,1000);
  2380. //延时10S
  2381. rt_thread_delay(10000);
  2382. }
  2383. //相机拍照故障
  2384. if(device_status.C01 & (1 << 2))
  2385. {
  2386. LOG_E("拍照故障!");
  2387. //发送红灯快闪信号
  2388. write_single_holding_register(0x01,0x00C2,0x0031,1000);
  2389. //延时10S
  2390. rt_thread_delay(10000);
  2391. }
  2392. //补光灯故障
  2393. if(device_status.C01 & (1 << 3))
  2394. {
  2395. LOG_E("补光灯故障!");
  2396. //发送绿灯慢闪信号
  2397. write_single_holding_register(0x01,0x00C2,0x0023,1000);
  2398. //延时10S
  2399. rt_thread_delay(10000);
  2400. }
  2401. //上仓门故障
  2402. if(device_status.C01 & (1 << 8))
  2403. {
  2404. LOG_E("上仓门故障!");
  2405. //发送黄灯常亮信号
  2406. write_single_holding_register(0x01,0x00C2,0x0012,1000);
  2407. //延时10S
  2408. rt_thread_delay(10000);
  2409. }
  2410. //下仓门故障
  2411. if(device_status.C01 & (1 << 9))
  2412. {
  2413. LOG_E("下仓门故障!");
  2414. //发送黄灯慢闪信号
  2415. write_single_holding_register(0x01,0x00C2,0x0022,1000);
  2416. //延时10S
  2417. rt_thread_delay(10000);
  2418. }
  2419. //清扫故障
  2420. if(device_status.C01 & (1 << 10))
  2421. {
  2422. LOG_E("清扫故障!");
  2423. //发送黄灯爆闪信号
  2424. write_single_holding_register(0x01,0x00C2,0x0032,1000);
  2425. //延时10S
  2426. rt_thread_delay(10000);
  2427. }
  2428. //通信故障
  2429. if(device_status.C01 & (1 << 13))
  2430. {
  2431. LOG_E("通信故障!");
  2432. //发送绿灯爆闪信号
  2433. write_single_holding_register(0x01,0x00C2,0x0033,1000);
  2434. //延时10S
  2435. rt_thread_delay(10000);
  2436. }
  2437. if(device_status.C01==0)
  2438. {
  2439. LOG_W("无故障!");
  2440. //发送绿灯常亮信号
  2441. //write_single_holding_register(0x01,0x00C2,0x0013,1000);
  2442. //延时10S
  2443. rt_thread_delay(10000);
  2444. }
  2445. #endif
  2446. }
  2447. }
  2448. rt_thread_delay(1000);
  2449. }
  2450. }
  2451. static void thread_485_play_entry(void *param)
  2452. {
  2453. static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
  2454. rt_uint32_t e;
  2455. // 创建事件控制块
  2456. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  2457. if (event_485_play == RT_NULL)
  2458. {
  2459. LOG_E("Event create failed!");
  2460. return;
  2461. }
  2462. while(1)
  2463. {
  2464. if(device_status.update_flag==1) break;
  2465. //等待事件
  2466. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  2467. {
  2468. LOG_I("Interrupt occurred and processed.");
  2469. _485_play(sound105, 4);
  2470. //发送串口消息,通知JETSON计算卡进行拍照
  2471. DTU_Write("{\"guangshan\":\"on\"}");
  2472. }
  2473. }
  2474. }
  2475. /*清扫线程*/
  2476. int Control_clear(void)
  2477. {
  2478. /* 创建线程*/
  2479. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2480. Control_Clear_thread_entry, RT_NULL,
  2481. 2048, //2048
  2482. 24, //23
  2483. WORK_CHECK_THREAD_TIMESLICE); //5
  2484. if(tid_ctr!=RT_NULL)
  2485. {
  2486. rt_thread_startup(tid_ctr);
  2487. }
  2488. return 0;
  2489. }
  2490. /*恒温加热线程*/
  2491. int Control_Check(void)
  2492. {
  2493. /* 创建线程*/
  2494. tid_heat = rt_thread_create("Control_Check_thread_entry",
  2495. Control_Check_thread_entry, RT_NULL,
  2496. 1024, //2048
  2497. 25, //23
  2498. WORK_CHECK_THREAD_TIMESLICE); //5
  2499. if(tid_heat!=RT_NULL)
  2500. {
  2501. rt_thread_startup(tid_heat);
  2502. }
  2503. return 0;
  2504. }
  2505. int thread_485_play(void)
  2506. {
  2507. tid_485_play = rt_thread_create("thread_485play",
  2508. thread_485_play_entry, RT_NULL,
  2509. 1024, //1024
  2510. 26, //23
  2511. 5); //5
  2512. if(tid_485_play!=RT_NULL)
  2513. {
  2514. rt_thread_startup(tid_485_play);
  2515. }
  2516. else {
  2517. LOG_E("thread_485_play start failed!");
  2518. }
  2519. return 0;
  2520. }
  2521. /* 创建线程 -工作状态检测*/
  2522. int Workstatus_check(void)
  2523. {
  2524. /* 创建线程*/
  2525. tid_wkstc = rt_thread_create("work_status_check_thread",
  2526. Work_status_thread_entry, RT_NULL,
  2527. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2528. WORK_CHECK_THREAD_PRIORITY, //23
  2529. WORK_CHECK_THREAD_TIMESLICE); //5
  2530. if(tid_wkstc!=RT_NULL)
  2531. {
  2532. rt_thread_startup(tid_wkstc);
  2533. }
  2534. return 0;
  2535. }
  2536. /*工作流程线程*/
  2537. int Work_Loop_Task(void)
  2538. {
  2539. rt_err_t result_loop = 0;
  2540. /* 创建线程*/
  2541. tid_workloop = rt_thread_create("work_loop_thread",
  2542. Work_Loop_thread_entry, RT_NULL,
  2543. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2544. WORK_LOOP_THREAD_PRIORITY, //12
  2545. WORK_LOOP_THREAD_TIMESLICE); //5
  2546. if(tid_workloop!=RT_NULL)
  2547. {
  2548. result_loop = rt_thread_startup(tid_workloop);
  2549. }
  2550. return result_loop;
  2551. }
  2552. #ifdef RT_USING_ALARM
  2553. void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp)
  2554. {
  2555. LOG_W("alarm_callback_router_on.\n");
  2556. ctr_router(ON);
  2557. }
  2558. void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp)
  2559. {
  2560. LOG_W("alarm_callback_router_off.\n");
  2561. ctr_router(OFF);
  2562. }
  2563. void alarm_app_router(void)
  2564. {
  2565. struct rt_alarm_setup setup;
  2566. struct rt_alarm * alarm_router_on = RT_NULL;
  2567. if (alarm_router_on != RT_NULL)
  2568. return;
  2569. setup.flag = RT_ALARM_DAILY;
  2570. setup.wktime.tm_hour = 23;
  2571. setup.wktime.tm_min = 0;
  2572. setup.wktime.tm_sec = 0;
  2573. alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup);
  2574. if(RT_NULL != alarm_router_on)
  2575. {
  2576. LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec);
  2577. rt_alarm_start(alarm_router_on);
  2578. }
  2579. }
  2580. #endif
  2581. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2582. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  2583. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2584. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2585. MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
  2586. //INIT_APP_EXPORT(Control_heat);