control.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  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_backup.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 1 //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_mark_dirty();
  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. clear_count = clear_timeout;
  1331. #if PWM_EN == 0
  1332. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1333. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1334. #endif
  1335. while(clear_count!=0)
  1336. {
  1337. #if PWM_EN == 0
  1338. rt_pin_write(CLEAR_R,ON);
  1339. rt_pin_write(CLEAR_L,OFF);
  1340. #else
  1341. #if QINGSAO_IO == 1
  1342. Motor_Qingsao_R_PWM(OFF);
  1343. Motor_Qingsao_L_PWM(ON);
  1344. #else
  1345. Motor_Qingsao_R_PWM(ON);
  1346. Motor_Qingsao_L_PWM(OFF);
  1347. #endif
  1348. #endif
  1349. clear_count--;
  1350. rt_thread_delay(100);
  1351. }
  1352. #if PWM_EN == 0
  1353. rt_pin_write(CLEAR_R,ON);
  1354. rt_pin_write(CLEAR_L,ON);
  1355. #else
  1356. Motor_Qingsao_R_PWM(OFF);
  1357. Motor_Qingsao_L_PWM(OFF);
  1358. #endif
  1359. rt_thread_delay(3000);
  1360. clear_count = 0;
  1361. while(clear_timeout!=clear_count)
  1362. {
  1363. #if PWM_EN == 0
  1364. rt_pin_write(CLEAR_R,OFF);
  1365. rt_pin_write(CLEAR_L,ON);
  1366. #else
  1367. #if QINGSAO_IO == 1
  1368. Motor_Qingsao_R_PWM(ON);
  1369. Motor_Qingsao_L_PWM(OFF);
  1370. #else
  1371. Motor_Qingsao_R_PWM(OFF);
  1372. Motor_Qingsao_L_PWM(ON);
  1373. #endif
  1374. #endif
  1375. // if(clear_count<200)
  1376. // current_clear[clear_count] = atoi(device_status.power);
  1377. clear_count++;
  1378. rt_thread_delay(100);
  1379. }
  1380. #if PWM_EN == 0
  1381. rt_pin_write(CLEAR_R,ON);
  1382. rt_pin_write(CLEAR_L,ON);
  1383. #else
  1384. Motor_Qingsao_R_PWM(OFF);
  1385. Motor_Qingsao_L_PWM(OFF);
  1386. #endif
  1387. #if 0
  1388. bubblesort(current_clear,200);
  1389. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1390. {
  1391. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1392. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1393. DTU_Write("%s",ctr_clear_warn);
  1394. rt_free(ctr_clear_warn);
  1395. }
  1396. #endif
  1397. rt_thread_delay(2000);
  1398. device_status.clear_status = 0;
  1399. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1400. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1401. }
  1402. // 全局静态变量,保存上次关闭的 tick
  1403. static rt_uint32_t s_last_off_tick = 0;
  1404. void ctr_RTU_DTU(uint8_t status_rtu)
  1405. {
  1406. // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
  1407. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1408. if (status_rtu == ON)
  1409. {
  1410. // 开机逻辑:必须满足冷却时间才能开机
  1411. rt_uint32_t current_tick = rt_tick_get();
  1412. rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
  1413. if (s_last_off_tick != 0)
  1414. {
  1415. rt_uint32_t diff_tick = current_tick - s_last_off_tick;
  1416. if (diff_tick < cool_ticks)
  1417. {
  1418. // 未冷却完成,计算剩余时间
  1419. rt_uint32_t wait_ticks = cool_ticks - diff_tick;
  1420. rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
  1421. LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
  1422. // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
  1423. rt_thread_delay(wait_ticks);
  1424. }
  1425. }
  1426. // 冷却完成,执行开机
  1427. rt_pin_write(RTU_DTU, ON);
  1428. s_last_off_tick = 0; // 清除关机时间
  1429. LOG_W("RTU电源启动!!!");
  1430. }
  1431. else
  1432. {
  1433. // 关机逻辑:记录关机时刻tick
  1434. rt_pin_write(RTU_DTU, OFF);
  1435. s_last_off_tick = rt_tick_get();
  1436. LOG_W("RTU电源关闭:%d",s_last_off_tick);
  1437. }
  1438. }
  1439. /**
  1440. * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
  1441. */
  1442. void RTU_Power_Off_Safe(void)
  1443. {
  1444. /* 2. 【关键】关闭串口设备。
  1445. * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
  1446. * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
  1447. */
  1448. rt_device_close(dtuserial);
  1449. /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
  1450. * extern UART_HandleTypeDef huart2;
  1451. * HAL_UART_DeInit(&huart2);
  1452. */
  1453. /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
  1454. * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
  1455. */
  1456. rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
  1457. rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
  1458. /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
  1459. rt_thread_mdelay(50);
  1460. /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
  1461. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1462. rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
  1463. LOG_I("RTU 12V Power OFF safely.\n");
  1464. }
  1465. /**
  1466. * @brief 恢复 RTU 12V 电源并重新初始化串口
  1467. */
  1468. void RTU_Power_On_Restore(void)
  1469. {
  1470. rt_thread_mdelay(5000);
  1471. /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
  1472. rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
  1473. LOG_I("RTU 12V Power ON.\n");
  1474. /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
  1475. * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
  1476. */
  1477. rt_thread_mdelay(5000);
  1478. /* 3. 【关键】重新打开串口设备。
  1479. * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为
  1480. * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。
  1481. */
  1482. GPIO_InitTypeDef gpioInitStruct;
  1483. __HAL_RCC_GPIOA_CLK_ENABLE();
  1484. //PA2 TXD
  1485. gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式
  1486. gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9
  1487. gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率
  1488. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1489. //PA3 RXD
  1490. gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式
  1491. gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10
  1492. gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率
  1493. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1494. #ifdef RT_USING_SERIAL_V2
  1495. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  1496. if(uart_res!=RT_EOK)
  1497. {
  1498. LOG_E("%s打开失败!",RTU_UART_NAME);
  1499. }else{
  1500. LOG_I("%s Open Success!",RTU_UART_NAME);
  1501. }
  1502. #endif
  1503. #ifdef RT_USING_SERIAL_V1
  1504. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  1505. #endif
  1506. /* 4. 【关键】重新注册接收回调
  1507. * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
  1508. * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
  1509. * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
  1510. */
  1511. dtu_uart2_reinit_rx();
  1512. }
  1513. /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
  1514. void cmd_rtu_restart(int argc, char** argv)
  1515. {
  1516. rt_kprintf("Starting RTU safe restart sequence...\n");
  1517. // 1. 安全断电
  1518. RTU_Power_Off_Safe();
  1519. // 2. 保持断电 30 秒 (确保电容彻底放电)
  1520. rt_kprintf("Waiting 30s for capacitor discharge...\n");
  1521. rt_thread_mdelay(30000);
  1522. // 3. 恢复供电并重建串口
  1523. RTU_Power_On_Restore();
  1524. rt_kprintf("RTU restart sequence completed.\n");
  1525. }
  1526. //海康拍照
  1527. void Hk_Take_io(void)
  1528. {
  1529. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1530. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1531. rt_pin_write(HK_TAKE, 1);
  1532. rt_pin_write(HK_IO, 0);
  1533. if(device_status.dtype==3)
  1534. Beep_Tip(1000);
  1535. if(device_status.dtype==7)
  1536. rt_thread_mdelay(1000);
  1537. rt_pin_write(HK_TAKE, 0);
  1538. rt_pin_write(HK_IO, 1);
  1539. }
  1540. void calorstat(uint16_t heat_time)
  1541. {
  1542. //uint8_t heat_up_cnt = 0;
  1543. uint16_t n = 0;
  1544. uint16_t delay_heat = 60;
  1545. FlagStatus heat_flg = RESET;
  1546. device_status.heat_time_min = 0;
  1547. device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
  1548. ctr_Heat(ON);
  1549. // UpLoad_Data(); //开始加热时的第一条数据
  1550. //恒温模式
  1551. for(n=0;n < heat_time;n++)
  1552. {
  1553. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1554. {
  1555. heat_flg = SET;
  1556. ctr_Heat(OFF);
  1557. while(delay_heat!=0)
  1558. {
  1559. device_status.C01 &= ~(1<<1); //加热 正常
  1560. delay_heat --;
  1561. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1562. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1563. rt_thread_delay(1000);
  1564. if(device_status.heattem>device_status.heat_tem_max + 1){
  1565. device_status.heat_tem_max = device_status.heattem;
  1566. }
  1567. }
  1568. delay_heat = 60;
  1569. //跳出FOR循环
  1570. n = n + 60;
  1571. }
  1572. if(device_status.heattem>device_status.run_sta + 1){
  1573. device_status.heat_tem_max = device_status.heattem;
  1574. }
  1575. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1576. {
  1577. device_status.heat_tem_max = device_status.heattem;
  1578. ctr_Heat(ON);
  1579. }
  1580. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1581. {
  1582. // if(heat_up_cnt==0)
  1583. // {
  1584. // UpLoad_Data(); //加热到设定温度的数据
  1585. // heat_up_cnt = 1;
  1586. // }
  1587. device_status.heat_tem_max = device_status.heattem;
  1588. ctr_Heat(OFF);
  1589. }
  1590. if((n % 60)==0)
  1591. {
  1592. device_status.heat_time_min = device_status.heat_time_min + 1;
  1593. if(device_status.heat_time_min % 5 == 0) //加热5分钟温度低于50,判定加热故障
  1594. {
  1595. if(device_status.heattem<50) {
  1596. device_status.C01 |= (1 << 1); // 第1位变为1,加热故障
  1597. }else {
  1598. device_status.C01 &= ~(1<<1); //加热 正常
  1599. }
  1600. }
  1601. }
  1602. //如果触发异常退出加热
  1603. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1604. {
  1605. rt_thread_delay(1000);
  1606. if(device_status.bat_status == 1)
  1607. {
  1608. rt_kprintf("============================触发异常退出加热==========================");
  1609. ctr_lamp(OFF,1);
  1610. ctr_Heat(OFF);
  1611. break;
  1612. }
  1613. }
  1614. if(device_status.work_mode==1){
  1615. LOG_W("切换模式,退出加热!");
  1616. break;
  1617. }
  1618. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1619. {
  1620. ctr_Heat(OFF);
  1621. rt_kprintf("异常退出!!!\n");
  1622. break;
  1623. }
  1624. rt_thread_delay(1000);
  1625. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1626. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1627. }
  1628. ctr_Heat(OFF);
  1629. //UpLoad_Data(); //加热结束的数据
  1630. }
  1631. //拍照
  1632. void HK_TakePhoto(void)
  1633. {
  1634. //打开补光灯
  1635. //DTU_Write("takephoto.mp3");
  1636. rt_thread_delay(10000);
  1637. rt_timer_start(timer_takephoto);
  1638. char *takephotojson = makeJson_takephoto();
  1639. DTU_Write("%s",takephotojson);
  1640. rt_free(takephotojson);
  1641. //吸虫塔
  1642. if(device_status.version_type!=6)
  1643. {
  1644. ctr_led(ON);
  1645. if(device_status.version_type==48)
  1646. {
  1647. rt_thread_delay(1000*60);
  1648. }
  1649. else {
  1650. rt_thread_delay(1000*15);
  1651. }
  1652. }
  1653. //触发海康相机拍照
  1654. Hk_Take_io();
  1655. if(device_status.version_type!=6)
  1656. {
  1657. rt_thread_delay(1000*45);
  1658. }
  1659. else {
  1660. ctr_led(ON);
  1661. rt_thread_delay(1000*19);
  1662. }
  1663. ctr_led(OFF);
  1664. }
  1665. //光控测试任务
  1666. void SunTest(void)
  1667. {
  1668. rt_uint16_t ui_timeout_l = 0;
  1669. //未检测到光照,一秒扫描等待,超时30S
  1670. while(Read_Light_Voltage()<500)
  1671. {
  1672. ui_timeout_l ++;
  1673. rt_thread_delay(1000);
  1674. if(ui_timeout_l==30) break;
  1675. }
  1676. rt_thread_delay(3000);
  1677. if(Read_Light_Voltage()<500)
  1678. {
  1679. device_status.sun_status = 0;//光控状态
  1680. }
  1681. if(Read_Light_Voltage()>500)
  1682. {
  1683. rt_thread_delay(1000);
  1684. }
  1685. rt_thread_delay(3000);
  1686. if(Read_Light_Voltage()>500)
  1687. {
  1688. device_status.sun_status = 1;
  1689. ctr_lamp(OFF,1);
  1690. }
  1691. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1692. device_status.sun_test = 0;
  1693. device_status.work_status=0;
  1694. }
  1695. /*上电初始化*/
  1696. void ResetInit(void)
  1697. {
  1698. if(device_status.version_type!=6&&device_status.version_type!=7)
  1699. {
  1700. device_status.work_done = 1;
  1701. //1.打开路由器供电
  1702. ctr_router(ON);
  1703. //2.打开相机供电
  1704. ctr_photo(ON);
  1705. ctr_RTU_DTU(ON);
  1706. //3.关闭镇流器
  1707. ctr_lamp(OFF,1);
  1708. //4.关闭震动电机
  1709. ctr_shake(OFF, 0);
  1710. //5.关闭加热
  1711. ctr_Heat(OFF);
  1712. //6.关闭补光灯
  1713. if(device_status.photo_ws==0)
  1714. ctr_led(OFF);
  1715. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1716. {
  1717. LOG_W("欠压!");
  1718. return;
  1719. }
  1720. //7.关闭上仓门
  1721. device_status.ctr_up_motor = 2;
  1722. // while(device_status.ctr_up_motor!=0)
  1723. // {
  1724. // LOG_I("关闭上仓门...");
  1725. // rt_thread_delay(100);
  1726. // }
  1727. //8.关闭下仓门
  1728. device_status.ctr_down_motor = 2;
  1729. while(device_status.ctr_down_motor!=0)
  1730. {
  1731. LOG_I("关闭下仓门....");
  1732. rt_thread_delay(100);
  1733. }
  1734. //9.清扫
  1735. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1736. {
  1737. LOG_W("异常退出!!!\n");
  1738. return;
  1739. }
  1740. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  1741. {
  1742. if(device_status.photo_ws==0) device_status.clear_status=1;
  1743. while(device_status.clear_status==1){
  1744. rt_thread_delay(1000);
  1745. }
  1746. #if JINGJIKUAN == 1
  1747. //再执行4次清扫
  1748. for(int i=0;i<4;i++)
  1749. {
  1750. if(device_status.photo_ws==0) device_status.clear_status=1;
  1751. while(device_status.clear_status==1){
  1752. rt_thread_delay(1000);
  1753. }
  1754. }
  1755. #endif
  1756. }
  1757. }
  1758. else {
  1759. if(device_status.power_status==1)
  1760. {
  1761. //1.打开路由器供电
  1762. #if MATCH_DEVICE ==0
  1763. ctr_router(ON);
  1764. //2.打开相机供电
  1765. ctr_photo(ON);
  1766. #endif
  1767. ctr_RTU_DTU(ON);
  1768. }
  1769. }
  1770. }
  1771. /*三色光源*/
  1772. void Led_Check(uint8_t tp)
  1773. {
  1774. if(tp==3)
  1775. {
  1776. if(device_status.light_over_tim>2*3600)
  1777. {
  1778. device_status.work_status = 1;
  1779. device_status.lamp_status = 1;
  1780. device_status.lamp1_status = 0;
  1781. device_status.lamp2_status = 0;
  1782. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1783. }
  1784. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1785. device_status.work_status = 1;
  1786. device_status.lamp_status = 0;
  1787. device_status.lamp1_status = 1;
  1788. device_status.lamp2_status = 0;
  1789. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1790. }
  1791. else if (device_status.light_over_tim<1*3600) {
  1792. device_status.work_status = 1;
  1793. device_status.lamp_status = 0;
  1794. device_status.lamp1_status = 0;
  1795. device_status.lamp2_status = 1;
  1796. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1797. }
  1798. }
  1799. else if (tp==6) {
  1800. if(device_status.light_over_tim>4*3600)
  1801. {
  1802. device_status.work_status = 1;
  1803. device_status.lamp_status = 1;
  1804. device_status.lamp1_status = 0;
  1805. device_status.lamp2_status = 0;
  1806. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1807. }
  1808. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1809. device_status.work_status = 1;
  1810. device_status.lamp_status = 0;
  1811. device_status.lamp1_status = 1;
  1812. device_status.lamp2_status = 0;
  1813. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1814. }
  1815. else if (device_status.light_over_tim<7200) {
  1816. device_status.work_status = 1;
  1817. device_status.lamp_status = 0;
  1818. device_status.lamp1_status = 0;
  1819. device_status.lamp2_status = 1;
  1820. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1821. }
  1822. }
  1823. else if (tp==9) {
  1824. if(device_status.light_over_tim>6*3600)
  1825. {
  1826. device_status.work_status = 1;
  1827. device_status.lamp_status = 1;
  1828. device_status.lamp1_status = 0;
  1829. device_status.lamp2_status = 0;
  1830. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1831. }
  1832. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1833. device_status.work_status = 1;
  1834. device_status.lamp_status = 0;
  1835. device_status.lamp1_status = 1;
  1836. device_status.lamp2_status = 0;
  1837. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1838. }
  1839. else if (device_status.light_over_tim<3*3600) {
  1840. device_status.work_status = 1;
  1841. device_status.lamp_status = 0;
  1842. device_status.lamp1_status = 0;
  1843. device_status.lamp2_status = 1;
  1844. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1845. }
  1846. }
  1847. }
  1848. void WorkCheck(void)
  1849. {
  1850. static rt_uint32_t light_timeout = 3600;
  1851. while(device_status.ds == 1 &&
  1852. device_status.rain_status == 0 &&
  1853. device_status.temp_status == 0 &&
  1854. device_status.bat_status == 0)
  1855. {
  1856. if(device_status.timeSwitch == 1) // 时控模式
  1857. {
  1858. LOG_I("***********时控*****************");
  1859. time_t now;
  1860. time(&now);
  1861. struct tm now_tm = *localtime(&now);
  1862. if(device_status.startBar == device_status.stopBar)
  1863. {
  1864. // 开始结束时间相同则24小时模式
  1865. device_status.work_status = 1;
  1866. rt_thread_delay(10000);
  1867. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1868. device_status.work_status ? "工作" : "待机",
  1869. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1870. device_status.startBar, device_status.stopBar);
  1871. continue;
  1872. }
  1873. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1874. rt_uint32_t start_sec = device_status.startBar * 3600;
  1875. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1876. // 修复跨天判断逻辑
  1877. if (start_sec < end_sec)
  1878. {
  1879. // 不跨天模式
  1880. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1881. }
  1882. else
  1883. {
  1884. // 跨天模式
  1885. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1886. }
  1887. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1888. device_status.work_status ? "工作" : "待机",
  1889. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1890. device_status.startBar, device_status.stopBar);
  1891. rt_thread_delay(10000);
  1892. }
  1893. else // 光控模式
  1894. {
  1895. LOG_I("光控定时模式!");
  1896. if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
  1897. {
  1898. device_status.light_over_tim--;
  1899. light_timeout = 3600; // 重置超时计数器
  1900. device_status.work_status = 1;
  1901. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1902. }
  1903. else
  1904. {
  1905. // 仅在白天递减超时计数器
  1906. if(device_status.sun_status == 1 && light_timeout > 0) {
  1907. light_timeout--;
  1908. }
  1909. device_status.work_status = 0;
  1910. //没有在测试并且没有在自检
  1911. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1912. {
  1913. ctr_lamp(OFF,1);
  1914. }
  1915. // 连续1小时白天后重置
  1916. if(light_timeout == 0) {
  1917. device_status.light_over_tim = 0;
  1918. light_timeout = 3600; // 重置防止负数
  1919. device_status.light_over_tim = device_status.sunBar * 3600;
  1920. }
  1921. }
  1922. }
  1923. rt_thread_delay(1000);
  1924. }
  1925. // 异常状态处理
  1926. device_status.work_status = 0;
  1927. rt_thread_delay(1000);
  1928. }
  1929. //工作任务流程s
  1930. void WorkTask(void)
  1931. {
  1932. static int clt_time = 0; //收集时间差秒数
  1933. static int heat_time = 0;
  1934. LOG_I("工作状态:%d",device_status.work_status);
  1935. LOG_I("开关机状态:%d",device_status.ds);
  1936. while(device_status.clear_status==1){
  1937. LOG_I("等待清扫执行完成....");
  1938. rt_thread_delay(1000);
  1939. }
  1940. 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)
  1941. {
  1942. if(device_status.work_mode==1){
  1943. break;
  1944. }
  1945. device_status.work_done = 0;
  1946. if(clt_time==0)
  1947. {
  1948. DTU_Write("workstart.mp3");
  1949. if(device_status.debug_ws==1)
  1950. {
  1951. time_t stamp=time(RT_NULL);
  1952. static struct tm tm;
  1953. gmtime_r(&stamp, &tm);
  1954. 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);
  1955. dtu_write_json(makeJson_Warn("Work start!", device_status.stamp));
  1956. }
  1957. }
  1958. ctr_lamp(ON,1);
  1959. LOG_I("打开灯管!!!");
  1960. if(device_status.version_type!=5)
  1961. {
  1962. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  1963. //2.打开上仓门
  1964. LOG_I("打开上仓门!!!");
  1965. device_status.ctr_up_motor = 1;
  1966. //3.收集
  1967. clt_time = device_status.clt_time * 60;
  1968. while(clt_time!=1)
  1969. {
  1970. clt_time --;
  1971. rt_thread_delay(1000);
  1972. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  1973. if(device_status.work_mode==1){
  1974. break;
  1975. }
  1976. //如果触发异常
  1977. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1978. {
  1979. ctr_lamp(OFF,1);
  1980. #if 0//LAMP_EN == 0
  1981. ctr_Fan_IN(OFF);
  1982. #endif
  1983. rt_kprintf("异常退出!!!\n");
  1984. break;
  1985. }
  1986. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1987. {
  1988. ctr_lamp(OFF,1);
  1989. #if 0//LAMP_EN == 0
  1990. ctr_Fan_IN(OFF);
  1991. #endif
  1992. rt_kprintf("异常退出!!!\n");
  1993. break;
  1994. }
  1995. }
  1996. clt_time = 1;
  1997. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1998. {
  1999. ctr_lamp(OFF,1);
  2000. #if 0//LAMP_EN == 0
  2001. ctr_Fan_IN(OFF);
  2002. #endif
  2003. rt_kprintf("异常退出!!!\n");
  2004. break;
  2005. }
  2006. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2007. {
  2008. ctr_lamp(OFF,1);
  2009. #if 0//LAMP_EN == 0
  2010. ctr_Fan_IN(OFF);
  2011. #endif
  2012. rt_kprintf("异常退出!!!\n");
  2013. break;
  2014. }
  2015. #if 0//LAMP_EN == 0
  2016. ctr_Fan_IN(OFF);
  2017. #endif
  2018. //4.关闭上仓门
  2019. device_status.ctr_up_motor = 2;
  2020. rt_kprintf("关闭上仓门!!!\n");
  2021. while(device_status.ctr_up_motor!=0)
  2022. {
  2023. rt_thread_delay(100);
  2024. LOG_I("等待上仓门关闭!!!");
  2025. }
  2026. //5.加热
  2027. if(device_status.heat_sw==0)
  2028. {
  2029. calorstat(device_status.heattime*60);
  2030. }
  2031. else {
  2032. heat_time = device_status.heattime * 60;
  2033. while(heat_time!=0)
  2034. {
  2035. if(device_status.work_mode==1)
  2036. {
  2037. break;
  2038. }
  2039. heat_time--;
  2040. LOG_I("正在加热状态!!!");
  2041. rt_thread_delay(1000);
  2042. }
  2043. }
  2044. #if TG_MODULE == 0
  2045. //6.打开下仓门
  2046. if(device_status.rain_status==0)
  2047. {
  2048. device_status.ctr_down_motor =1;
  2049. }
  2050. else {
  2051. device_status.ctr_down_motor = 2;
  2052. device_status.ctr_up_motor = 2;
  2053. }
  2054. //待下仓门完全打开
  2055. while(device_status.ctr_down_motor!=0)
  2056. {
  2057. rt_thread_delay(100);
  2058. rt_kprintf("等待下仓门打开!!!\n");
  2059. }
  2060. rt_thread_delay(3000);
  2061. //关闭下仓门
  2062. device_status.ctr_down_motor = 2;
  2063. while(device_status.ctr_down_motor!=0)
  2064. {
  2065. rt_thread_delay(100);
  2066. rt_kprintf("等待下仓门关闭!!!\n");
  2067. }
  2068. //2022-04-12add:
  2069. LOG_I("打开上仓门!!!");
  2070. device_status.ctr_up_motor = 1;
  2071. #else
  2072. //打开下仓门
  2073. ctr_cangmen_down(OPEN);
  2074. uint16_t timeout_down_open = 0;
  2075. while(1)
  2076. {
  2077. timeout_down_open++;
  2078. if(rt_pin_read(CTR_UPC_LIMIT)==1||timeout_down_open>20*20) break;
  2079. rt_thread_delay(50);
  2080. }
  2081. ctr_cangmen_down(OFF);
  2082. rt_thread_delay(1000);
  2083. #endif
  2084. if(device_status.work_mode==1){
  2085. break;
  2086. }
  2087. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  2088. {
  2089. //震动
  2090. ctr_shake(ON, device_status.shake_sec);
  2091. //补光&&拍照
  2092. LOG_I("拍照!!!");
  2093. device_status.photo_ws = 1;
  2094. }
  2095. #if TG_MODULE == 1
  2096. while(device_status.photo_ws == 1)
  2097. {
  2098. rt_thread_delay(1000);
  2099. LOG_I("等待拍照完成...");
  2100. }
  2101. rt_thread_delay(1000);
  2102. while(device_status.clear_status==1)
  2103. {
  2104. rt_thread_delay(1000);
  2105. LOG_I("等待清扫完成...");
  2106. }
  2107. ctr_cangmen_down(OPEN);
  2108. //待下仓门完全打开
  2109. uint8_t i = 0;
  2110. while(1)
  2111. {
  2112. i++;
  2113. rt_thread_delay(100);
  2114. rt_kprintf("等待下仓门打开!!!\n");
  2115. if(i>100) {
  2116. ctr_cangmen_down(OFF);
  2117. break;
  2118. }
  2119. }
  2120. rt_thread_delay(2000);
  2121. ctr_shake(ON, device_status.shake_sec);
  2122. device_status.photo_ws = 1;
  2123. //关闭下仓门
  2124. device_status.ctr_down_motor = 2;
  2125. //待下仓门完全关闭
  2126. while(device_status.ctr_down_motor!=0)
  2127. {
  2128. rt_thread_delay(100);
  2129. rt_kprintf("等待下仓门关闭!!!\n");
  2130. }
  2131. device_status.ctr_up_motor = 1; //打开上仓门
  2132. #endif
  2133. }
  2134. }
  2135. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2136. {
  2137. LOG_I("-------------------not work--------------------");
  2138. device_status.work_status = 0;
  2139. }
  2140. //工作结束
  2141. if(device_status.work_done==0&&device_status.work_status==0)
  2142. {
  2143. DTU_Write("workdone");
  2144. ResetInit();
  2145. clt_time = 0;
  2146. }
  2147. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2148. }
  2149. #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
  2150. void slidelock()
  2151. {
  2152. ctr_Motor_Driver_Power(ON); //电机驱动供电
  2153. rt_thread_delay(10);
  2154. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2155. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2156. }
  2157. /* 入口函数-工作状态检测 */
  2158. static void Work_status_thread_entry(void *parameter)
  2159. {
  2160. static uint16_t sun_status_num = 120;
  2161. //如果是光控定时,进行光控监测的消抖
  2162. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2163. {
  2164. rt_kprintf("光控定时!!!\n");
  2165. while(sun_status_num!=0)
  2166. {
  2167. sun_status_num--;
  2168. rt_thread_delay(1000);
  2169. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2170. }
  2171. device_status.light_over_tim = device_status.sunBar * 3600;
  2172. device_status.test_flag = 0;
  2173. }
  2174. while(1)
  2175. {
  2176. WorkCheck();
  2177. }
  2178. }
  2179. //指令
  2180. //
  2181. /*入口函数-工作流程*/
  2182. static void Work_Loop_thread_entry(void *parameter)
  2183. {
  2184. rt_thread_delay(1000);
  2185. ResetInit();
  2186. device_status.light_over_tim = device_status.sunBar * 3600;
  2187. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  2188. static struct tm tm_tg_run;
  2189. static uint8_t alarm_h,alarm_m,alarm_s;
  2190. while(1)
  2191. {
  2192. if(device_status.work_mode==0)
  2193. {
  2194. WorkTask();
  2195. }else{
  2196. if(device_status.work_status==1){
  2197. ctr_lamp(ON, 1);
  2198. }else{
  2199. ctr_lamp(OFF, 1);
  2200. }
  2201. char buf[7] = {0};
  2202. /* ★ 原来用 "%-6s": 6 是"最小宽度"不是截断宽度, alarm_tg 是 char[12],
  2203. 超过 6 字符时会完整写出 12+1 字节到 7 字节的 buf -> 栈溢出 6 字节。
  2204. 必须写成 "%-6.6s"(精度 .6 才是截断)。 */
  2205. rt_snprintf(buf, sizeof(buf), "%-6.6s", device_status.alarm_tg);
  2206. // 截取 时: 前2位
  2207. alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0');
  2208. // 截取 分: 中间2位
  2209. alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0');
  2210. // 截取 秒: 后2位
  2211. alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0');
  2212. device_status.ctr_up_motor = 2; //关闭上仓门
  2213. device_status.ctr_down_motor = 2; //关闭下仓门
  2214. LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s);
  2215. //判断时间
  2216. time_t time_run_tg;
  2217. time(&time_run_tg);
  2218. tm_tg_run =*localtime(&time_run_tg);
  2219. LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec);
  2220. if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s))
  2221. {
  2222. LOG_W("预制执行 开始!");
  2223. dtu_write_json(makeJson_Warn("执行完成", "预制模式"));
  2224. rt_thread_delay(2000);
  2225. //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1}
  2226. cJSON *params = cJSON_CreateObject(); // params 子对象
  2227. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  2228. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  2229. /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串 -> 走 dtu_write_json */
  2230. dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
  2231. rt_thread_delay(20000);
  2232. }
  2233. }
  2234. rt_thread_delay(1000);
  2235. }
  2236. }
  2237. /*入口函数清扫*/
  2238. static void Control_Clear_thread_entry(void *parameter)
  2239. {
  2240. while(1)
  2241. {
  2242. if(device_status.update_flag==1) break;
  2243. if(device_status.clear_status==1)
  2244. {
  2245. #if QINGSAO_YUGUAQI == 1
  2246. ctr_clear(300);
  2247. #else
  2248. if(device_status.version_type==41){
  2249. ctr_TG_Clear(300);
  2250. }else {
  2251. ctr_TG_Clear(400); //20260924-350修改为400
  2252. }
  2253. #endif
  2254. system_status.cleaning_count +=1; //清扫次数
  2255. }
  2256. if(device_status.clear_status==2)
  2257. {
  2258. turnzp();
  2259. device_status.clear_status = 0;
  2260. }
  2261. rt_thread_delay(100);
  2262. }
  2263. }
  2264. /*入口函数,自检*/
  2265. static void Control_Check_thread_entry(void *parameter)
  2266. {
  2267. static int total_seconds = 200;
  2268. static int current_second = 0;
  2269. static int process = 0;
  2270. device_status.C01 = 0;
  2271. rt_thread_delay(10000);
  2272. while(1)
  2273. {
  2274. if(device_status.update_flag==1) break;
  2275. if(device_status.work_status==1&&device_status.heat_sw==1)
  2276. {
  2277. calorstat(device_status.heattime*60);
  2278. rt_thread_delay(10000);
  2279. }
  2280. else
  2281. {
  2282. if(device_status.selfcheck_ws==2)
  2283. {
  2284. while(current_second <= total_seconds)
  2285. {
  2286. process = (current_second * 100) / total_seconds;
  2287. device_status.process = process / 100.0;
  2288. LOG_I("process:%d",process);
  2289. HMI_WRITE("selfcheck.s106.val=%d",process);
  2290. rt_thread_delay(1000);
  2291. current_second++;
  2292. if(device_status.selfcheck_ws==0)
  2293. {
  2294. HMI_WRITE("selfcheck.s106.val=100");
  2295. break;
  2296. }
  2297. }
  2298. HMI_WRITE("selfcheck.s106.val=100");
  2299. }else {
  2300. process = 0;
  2301. current_second = 0;
  2302. #if 0
  2303. if(device_status.rain_status==1)
  2304. {
  2305. LOG_E("雨控!");
  2306. rt_thread_delay(10000);
  2307. }
  2308. //根据故障码 亮指示灯
  2309. //灯管故障
  2310. if(device_status.C01 & (1 << 0))
  2311. {
  2312. LOG_E("灯管故障!");
  2313. //发送红灯常亮信号
  2314. write_single_holding_register(0x01,0x00C2,0x0011,1000);
  2315. //延时10S
  2316. rt_thread_delay(10000);
  2317. }
  2318. //加热故障
  2319. if(device_status.C01 & (1 << 1))
  2320. {
  2321. LOG_E("加热故障!");
  2322. //发送红灯慢闪信号
  2323. write_single_holding_register(0x01,0x00C2,0x0021,1000);
  2324. //延时10S
  2325. rt_thread_delay(10000);
  2326. }
  2327. //相机拍照故障
  2328. if(device_status.C01 & (1 << 2))
  2329. {
  2330. LOG_E("拍照故障!");
  2331. //发送红灯快闪信号
  2332. write_single_holding_register(0x01,0x00C2,0x0031,1000);
  2333. //延时10S
  2334. rt_thread_delay(10000);
  2335. }
  2336. //补光灯故障
  2337. if(device_status.C01 & (1 << 3))
  2338. {
  2339. LOG_E("补光灯故障!");
  2340. //发送绿灯慢闪信号
  2341. write_single_holding_register(0x01,0x00C2,0x0023,1000);
  2342. //延时10S
  2343. rt_thread_delay(10000);
  2344. }
  2345. //上仓门故障
  2346. if(device_status.C01 & (1 << 8))
  2347. {
  2348. LOG_E("上仓门故障!");
  2349. //发送黄灯常亮信号
  2350. write_single_holding_register(0x01,0x00C2,0x0012,1000);
  2351. //延时10S
  2352. rt_thread_delay(10000);
  2353. }
  2354. //下仓门故障
  2355. if(device_status.C01 & (1 << 9))
  2356. {
  2357. LOG_E("下仓门故障!");
  2358. //发送黄灯慢闪信号
  2359. write_single_holding_register(0x01,0x00C2,0x0022,1000);
  2360. //延时10S
  2361. rt_thread_delay(10000);
  2362. }
  2363. //清扫故障
  2364. if(device_status.C01 & (1 << 10))
  2365. {
  2366. LOG_E("清扫故障!");
  2367. //发送黄灯爆闪信号
  2368. write_single_holding_register(0x01,0x00C2,0x0032,1000);
  2369. //延时10S
  2370. rt_thread_delay(10000);
  2371. }
  2372. //通信故障
  2373. if(device_status.C01 & (1 << 13))
  2374. {
  2375. LOG_E("通信故障!");
  2376. //发送绿灯爆闪信号
  2377. write_single_holding_register(0x01,0x00C2,0x0033,1000);
  2378. //延时10S
  2379. rt_thread_delay(10000);
  2380. }
  2381. if(device_status.C01==0)
  2382. {
  2383. LOG_W("无故障!");
  2384. //发送绿灯常亮信号
  2385. //write_single_holding_register(0x01,0x00C2,0x0013,1000);
  2386. //延时10S
  2387. rt_thread_delay(10000);
  2388. }
  2389. #endif
  2390. }
  2391. }
  2392. rt_thread_delay(1000);
  2393. }
  2394. }
  2395. static void thread_485_play_entry(void *param)
  2396. {
  2397. static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
  2398. rt_uint32_t e;
  2399. // 创建事件控制块
  2400. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  2401. if (event_485_play == RT_NULL)
  2402. {
  2403. LOG_E("Event create failed!");
  2404. return;
  2405. }
  2406. while(1)
  2407. {
  2408. if(device_status.update_flag==1) break;
  2409. //等待事件
  2410. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  2411. {
  2412. LOG_I("Interrupt occurred and processed.");
  2413. _485_play(sound105, 4);
  2414. //发送串口消息,通知JETSON计算卡进行拍照
  2415. DTU_Write("{\"guangshan\":\"on\"}");
  2416. }
  2417. }
  2418. }
  2419. /*清扫线程*/
  2420. int Control_clear(void)
  2421. {
  2422. /* 创建线程*/
  2423. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2424. Control_Clear_thread_entry, RT_NULL,
  2425. 2048, //2048
  2426. 24, //23
  2427. WORK_CHECK_THREAD_TIMESLICE); //5
  2428. if(tid_ctr!=RT_NULL)
  2429. {
  2430. rt_thread_startup(tid_ctr);
  2431. }
  2432. return 0;
  2433. }
  2434. /*恒温加热线程*/
  2435. int Control_Check(void)
  2436. {
  2437. /* 创建线程*/
  2438. tid_heat = rt_thread_create("Control_Check_thread_entry",
  2439. Control_Check_thread_entry, RT_NULL,
  2440. 1024, //2048
  2441. 25, //23
  2442. WORK_CHECK_THREAD_TIMESLICE); //5
  2443. if(tid_heat!=RT_NULL)
  2444. {
  2445. rt_thread_startup(tid_heat);
  2446. }
  2447. return 0;
  2448. }
  2449. int thread_485_play(void)
  2450. {
  2451. tid_485_play = rt_thread_create("thread_485play",
  2452. thread_485_play_entry, RT_NULL,
  2453. 1024, //1024
  2454. 26, //23
  2455. 5); //5
  2456. if(tid_485_play!=RT_NULL)
  2457. {
  2458. rt_thread_startup(tid_485_play);
  2459. }
  2460. else {
  2461. LOG_E("thread_485_play start failed!");
  2462. }
  2463. return 0;
  2464. }
  2465. /* 创建线程 -工作状态检测*/
  2466. int Workstatus_check(void)
  2467. {
  2468. /* 创建线程*/
  2469. tid_wkstc = rt_thread_create("work_status_check_thread",
  2470. Work_status_thread_entry, RT_NULL,
  2471. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2472. WORK_CHECK_THREAD_PRIORITY, //23
  2473. WORK_CHECK_THREAD_TIMESLICE); //5
  2474. if(tid_wkstc!=RT_NULL)
  2475. {
  2476. rt_thread_startup(tid_wkstc);
  2477. }
  2478. return 0;
  2479. }
  2480. /*工作流程线程*/
  2481. int Work_Loop_Task(void)
  2482. {
  2483. rt_err_t result_loop = 0;
  2484. /* 创建线程*/
  2485. tid_workloop = rt_thread_create("work_loop_thread",
  2486. Work_Loop_thread_entry, RT_NULL,
  2487. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2488. WORK_LOOP_THREAD_PRIORITY, //12
  2489. WORK_LOOP_THREAD_TIMESLICE); //5
  2490. if(tid_workloop!=RT_NULL)
  2491. {
  2492. result_loop = rt_thread_startup(tid_workloop);
  2493. }
  2494. return result_loop;
  2495. }
  2496. #ifdef RT_USING_ALARM
  2497. void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp)
  2498. {
  2499. LOG_W("alarm_callback_router_on.\n");
  2500. ctr_router(ON);
  2501. }
  2502. void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp)
  2503. {
  2504. LOG_W("alarm_callback_router_off.\n");
  2505. ctr_router(OFF);
  2506. }
  2507. void alarm_app_router(void)
  2508. {
  2509. struct rt_alarm_setup setup;
  2510. struct rt_alarm * alarm_router_on = RT_NULL;
  2511. if (alarm_router_on != RT_NULL)
  2512. return;
  2513. setup.flag = RT_ALARM_DAILY;
  2514. setup.wktime.tm_hour = 23;
  2515. setup.wktime.tm_min = 0;
  2516. setup.wktime.tm_sec = 0;
  2517. alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup);
  2518. if(RT_NULL != alarm_router_on)
  2519. {
  2520. LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec);
  2521. rt_alarm_start(alarm_router_on);
  2522. }
  2523. }
  2524. #endif
  2525. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2526. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  2527. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2528. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2529. MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
  2530. //INIT_APP_EXPORT(Control_heat);