control.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618
  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. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  389. {
  390. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  391. if(status_ws==ON)
  392. {
  393. rt_pin_write(FAN_P,ON);
  394. }
  395. else {
  396. rt_pin_write(FAN_P, OFF);
  397. }
  398. return 0;
  399. }
  400. uint8_t ctr_Fan_IN(uint8_t status_ws)
  401. {
  402. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  403. if(status_ws==ON)
  404. {
  405. rt_pin_write(FAN_I,ON);
  406. }
  407. else {
  408. rt_pin_write(FAN_I, OFF);
  409. }
  410. return 0;
  411. }
  412. /**********************************************************************/
  413. /*光控*/
  414. uint8_t In_Light_status(void)
  415. {
  416. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  417. return rt_pin_read(LIGHT_PIN);
  418. }
  419. /*雨控*/
  420. uint8_t In_Rain_status(void)
  421. {
  422. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  423. return rt_pin_read(RAIN_PIN);
  424. }
  425. /*加热*/
  426. uint8_t ctr_Heat(uint8_t status_heat)
  427. {
  428. rt_bool_t is_currently_on;
  429. rt_tick_t now;
  430. now = rt_tick_get();
  431. is_currently_on = (system_status.heat_start_time != 0);
  432. device_status.heat_status = status_heat;
  433. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  434. rt_pin_write(Heat_Pin,status_heat);
  435. if(status_heat==ON){
  436. // 已经开着 → 不重复计时
  437. if (is_currently_on) {
  438. return ON;
  439. }
  440. // 关 → 开,记录起始时间
  441. system_status.heat_start_time = now;
  442. }else{
  443. // 已经关着 → 不重复结算
  444. if (!is_currently_on) {
  445. return OFF;
  446. }
  447. // 开 → 关,结算运行时间
  448. system_status.heater_running_time +=
  449. (now - system_status.heat_start_time);
  450. system_status.heat_start_time = 0;
  451. }
  452. return status_heat;
  453. }
  454. /*制冷*/
  455. uint8_t ctr_Cold(uint8_t status_cold)
  456. {
  457. device_status.cold_status = status_cold;
  458. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  459. rt_pin_write(Cold_Pin,status_cold);
  460. return status_cold;
  461. }
  462. /*相机供电*/
  463. void ctr_photo(uint8_t status_photo)
  464. {
  465. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  466. rt_pin_write(PHOTO,status_photo);
  467. }
  468. /*************************************************
  469. Function: ctr_Cool_Fan
  470. Description: 散热风机的开关
  471. Calls:
  472. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  473. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  474. Input: ON-开,OFF-关
  475. Output: NULL
  476. Return: ON-开,OFF-关
  477. Others: NULL
  478. *************************************************/
  479. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  480. {
  481. device_status.cool_fan_status = status_cool;
  482. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  483. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  484. if(status_cool==OFF)
  485. {
  486. rt_pin_write(Cool_Fan_L,OFF);
  487. rt_pin_write(Cool_Fan_R,OFF);
  488. }
  489. if(status_cool==ON)
  490. {
  491. rt_pin_write(Cool_Fan_L,ON);
  492. rt_pin_write(Cool_Fan_R,OFF);
  493. }
  494. return device_status.cool_fan_status;
  495. }
  496. /*************************************************
  497. Function: ctr_Collect_Fan
  498. Description: 采集风机的开关
  499. Calls:
  500. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  501. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  502. Input: ON-开,OFF-关
  503. Output: NULL
  504. Return: ON-开,OFF-关
  505. Others: NULL
  506. *************************************************/
  507. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  508. {
  509. device_status.collect_fan_status = status_collect;
  510. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  511. if(status_collect==OFF)
  512. {
  513. rt_pin_write(Collect_Fan,OFF);
  514. }
  515. if(status_collect==ON)
  516. {
  517. rt_pin_write(Collect_Fan,ON);
  518. }
  519. return device_status.collect_fan_status;
  520. }
  521. /*************************************************
  522. Function: ctr_develop
  523. Description: 培养载玻片孢子
  524. Calls:
  525. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  526. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  527. Input: v=0(复位) v=90转90度
  528. Output: NULL
  529. Return: NULL
  530. Others: NULL
  531. *************************************************/
  532. /*上仓门*/
  533. uint8_t ctr_cangmen_up(uint8_t status_up)
  534. {
  535. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  536. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  537. if(status_up==OFF)
  538. {
  539. rt_pin_write(Collect_fan_R,ON);
  540. rt_pin_write(Collect_fan_L,ON);
  541. device_status.ctr_up_motor = 0;
  542. rt_thread_delay(1000);
  543. }
  544. if(status_up==OPEN)
  545. {
  546. device_status.up_status = 1;
  547. rt_pin_write(Collect_fan_R,OFF);
  548. rt_pin_write(Collect_fan_L,OFF);
  549. #if PCB_V1 == 1
  550. rt_pin_write(Collect_fan_R,ON);
  551. rt_pin_write(Collect_fan_L,OFF);
  552. #else
  553. rt_pin_write(Collect_fan_R,OFF);
  554. rt_pin_write(Collect_fan_L,ON);
  555. #endif
  556. }
  557. if(status_up==CLOSE)
  558. {
  559. device_status.up_status = 0;
  560. rt_pin_write(Collect_fan_R,OFF);
  561. rt_pin_write(Collect_fan_L,OFF);
  562. #if PCB_V1 == 1
  563. rt_pin_write(Collect_fan_R,OFF);
  564. rt_pin_write(Collect_fan_L,ON);
  565. #else
  566. rt_pin_write(Collect_fan_R,ON);
  567. rt_pin_write(Collect_fan_L,OFF);
  568. #endif
  569. }
  570. return device_status.up_status;
  571. }
  572. //下仓门
  573. uint8_t ctr_cangmen_down(uint8_t status_down)
  574. {
  575. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  576. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  577. if(status_down==OFF)
  578. {
  579. rt_pin_write(C_DOWN_R,ON);
  580. rt_pin_write(C_DOWN_L,ON);
  581. device_status.ctr_down_motor = 0;
  582. rt_thread_delay(1000);
  583. }
  584. if(status_down==OPEN)
  585. {
  586. device_status.down_status = 1;
  587. rt_pin_write(C_DOWN_R,OFF);
  588. rt_pin_write(C_DOWN_L,OFF);
  589. rt_pin_write(C_DOWN_R,ON);
  590. rt_pin_write(C_DOWN_L,OFF);
  591. }
  592. if(status_down==CLOSE)
  593. {
  594. device_status.down_status = 0;
  595. rt_pin_write(C_DOWN_R,OFF);
  596. rt_pin_write(C_DOWN_L,OFF);
  597. rt_pin_write(C_DOWN_R,OFF);
  598. rt_pin_write(C_DOWN_L,ON);
  599. }
  600. return device_status.down_status;
  601. }
  602. //镇流器
  603. //镇流器
  604. uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
  605. {
  606. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  607. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  608. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  609. rt_bool_t is_currently_on;
  610. rt_tick_t now;
  611. now = rt_tick_get();
  612. is_currently_on = (system_status.lamp_start_time != 0);
  613. if (status_lamp == ON)
  614. {
  615. // ====== 开灯 ======
  616. switch (lamp_num) {
  617. case 1:
  618. rt_pin_write(LAMP_SW, ON);
  619. device_status.lamp_status = ON;
  620. break;
  621. case 2:
  622. rt_pin_write(LAMP_1_SW, ON);
  623. device_status.lamp1_status = ON;
  624. break;
  625. case 3:
  626. rt_pin_write(LAMP_2_SW, ON);
  627. device_status.lamp2_status = ON;
  628. break;
  629. default:
  630. break;
  631. }
  632. // 已经开着 → 不重复计时
  633. if (is_currently_on) {
  634. return ON;
  635. }
  636. // 关 → 开,记录起始时间
  637. system_status.lamp_start_time = now;
  638. }
  639. else
  640. {
  641. // ====== 关灯 ======
  642. switch (lamp_num) {
  643. case 1:
  644. rt_pin_write(LAMP_SW, OFF);
  645. device_status.lamp_status = OFF;
  646. break;
  647. case 2:
  648. rt_pin_write(LAMP_1_SW, OFF);
  649. device_status.lamp1_status = OFF;
  650. break;
  651. case 3:
  652. rt_pin_write(LAMP_2_SW, OFF);
  653. device_status.lamp2_status = OFF;
  654. break;
  655. default:
  656. break;
  657. }
  658. // 已经关着 → 不重复结算
  659. if (!is_currently_on) {
  660. return OFF;
  661. }
  662. // 开 → 关,结算运行时间
  663. system_status.lamp_running_time +=
  664. (now - system_status.lamp_start_time);
  665. system_status.lamp_start_time = 0;
  666. }
  667. HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
  668. return device_status.lamp_status;
  669. }
  670. //震动电机
  671. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  672. {
  673. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  674. #if 0
  675. rt_pin_write(SHAKE_SW,ON);
  676. return 0;
  677. #endif
  678. if(status_shake==ON&&device_status.shake_ws==1)
  679. {
  680. if(device_status.dtype==3)
  681. {
  682. for(uint8_t shake_i = 0;shake_i<shake_sec;shake_i++)
  683. {
  684. rt_pin_write(SHAKE_SW,ON);
  685. rt_thread_delay(100);
  686. rt_pin_write(SHAKE_SW,OFF);
  687. rt_thread_delay(1000);
  688. }
  689. }
  690. rt_pin_write(SHAKE_SW,OFF);
  691. HMI_WRITE("cangWei.switchShake.val=%d",0);
  692. system_status.vibration_count += 1;
  693. }
  694. else
  695. {
  696. rt_pin_write(SHAKE_SW,OFF);
  697. HMI_WRITE("cangWei.switchShake.val=%d",0);
  698. }
  699. if(device_status.dtype==7)
  700. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  701. return status_shake;
  702. }
  703. //补光
  704. uint8_t ctr_led(uint8_t status_led)
  705. {
  706. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  707. if(status_led==ON)
  708. {
  709. rt_pin_write(LED_SW,ON);
  710. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  711. }
  712. else if(status_led==OFF){
  713. rt_pin_write(LED_SW,OFF);
  714. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  715. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  716. }
  717. return status_led;
  718. }
  719. /*************************************************
  720. Function: ctr_turn
  721. Description: 转盘驱动
  722. Calls:
  723. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  724. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  725. Input: v=0(复位) v=90转90度
  726. Output: NULL
  727. Return: NULL
  728. Others: NULL
  729. *************************************************/
  730. void ctr_turn(uint8_t v,uint8_t sta)
  731. {
  732. DTU_Write("turn.mp3");
  733. LOG_I("================v===============:%d",v);
  734. rt_uint16_t turn_cnt = 0;
  735. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  736. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  737. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  738. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  739. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  740. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  741. {
  742. rt_pin_write(TURN_ZP_R, ON);
  743. rt_pin_write(TURN_ZP_L, OFF);
  744. //Motor_shake(ON,0);
  745. turn_cnt ++;
  746. rt_thread_delay(100);
  747. if(turn_cnt==1000)
  748. {
  749. turn_cnt = 0;
  750. rt_pin_write(TURN_ZP_R, OFF);
  751. rt_pin_write(TURN_ZP_L, OFF);
  752. //Motor_shake(OFF,0);
  753. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  754. DTU_Write("%s",turn_warn);
  755. rt_free(turn_warn);
  756. break;
  757. }
  758. }
  759. turn_cnt = 0;
  760. while(v==0&&rt_pin_read(LIMIT_0)==1)
  761. {
  762. rt_pin_write(TURN_ZP_R, ON);
  763. rt_pin_write(TURN_ZP_L, OFF);
  764. Motor_shake(ON,0);
  765. rt_thread_delay(200); //10 to 100
  766. turn_cnt ++;
  767. if(turn_cnt==300) //6000 to 600
  768. {
  769. turn_cnt = 0;
  770. // rt_pin_write(TURN_ZP_R, OFF);
  771. // rt_pin_write(TURN_ZP_L, OFF);
  772. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  773. // DTU_Write("%s",turn_warn);
  774. // rt_free(turn_warn);
  775. break;
  776. }
  777. }
  778. while(v==0&&rt_pin_read(LIMIT_0)==1)
  779. {
  780. rt_pin_write(TURN_ZP_R, ON);
  781. rt_pin_write(TURN_ZP_L, OFF);
  782. //Motor_shake(ON,0);
  783. rt_thread_delay(50); //10 to 50
  784. turn_cnt ++;
  785. if(turn_cnt==10) //6000 to 1200
  786. {
  787. turn_cnt = 0;
  788. rt_pin_write(TURN_ZP_R, OFF);
  789. rt_pin_write(TURN_ZP_L, OFF);
  790. //Motor_shake(OFF,0);
  791. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  792. DTU_Write("%s",turn_warn);
  793. rt_free(turn_warn);
  794. break;
  795. }
  796. }
  797. turn_cnt = 0;
  798. while(v==90&&rt_pin_read(LIMIT_90)==1)
  799. {
  800. rt_pin_write(TURN_ZP_R, ON);
  801. rt_pin_write(TURN_ZP_L, OFF);
  802. //Motor_shake(ON,0);
  803. rt_thread_delay(200);
  804. turn_cnt ++;
  805. if(turn_cnt==150)
  806. {
  807. turn_cnt = 0;
  808. // rt_pin_write(TURN_ZP_R, OFF);
  809. // rt_pin_write(TURN_ZP_L, OFF);
  810. // char *turn_warn =makeJson_Warn("超时", "转盘");
  811. // DTU_Write(turn_warn);
  812. // rt_free(turn_warn);
  813. break;
  814. }
  815. }
  816. while(v==90&&rt_pin_read(LIMIT_90)==1)
  817. {
  818. // rt_pin_write(TURN_ZP_R, ON);
  819. // rt_pin_write(TURN_ZP_L, OFF);
  820. Motor_shake(ON,0);
  821. rt_thread_delay(50);
  822. turn_cnt ++;
  823. if(turn_cnt==10)
  824. {
  825. turn_cnt = 0;
  826. rt_pin_write(TURN_ZP_R, OFF);
  827. rt_pin_write(TURN_ZP_L, OFF);
  828. //Motor_shake(OFF,0);
  829. /* ★ 原来 DTU_Write(turn_warn) 把 JSON 当格式串(含 '%' 就会被解析) -> 走 dtu_write_json */
  830. dtu_write_json(makeJson_Warn("超时", "转盘"));
  831. break;
  832. }
  833. }
  834. if(sta!=0)
  835. {
  836. //保存位置
  837. device_status.run_sta = sta;
  838. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  839. /* 转盘位置变了, 同步进参数镜像(掉电后能恢复到正确仓位) */
  840. param_mirror_sync();
  841. }
  842. turn_cnt = 0;
  843. rt_pin_write(TURN_ZP_R, OFF);
  844. rt_pin_write(TURN_ZP_L, OFF);
  845. Motor_shake(OFF,0);
  846. HMI_WRITE("cangWei.rotateFlag.val=0");
  847. }
  848. /*
  849. *
  850. * */
  851. void ctr_Motor_Driver_Power(uint8_t status_ws)
  852. {
  853. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  854. rt_pin_write(SHAKE_SW, status_ws);
  855. }
  856. /*************************************************
  857. Function: ctr_Motor_Driver
  858. Description: 步进电机驱动
  859. Calls:
  860. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  861. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  862. Input: 50000000
  863. Output: NULL
  864. Return: shake_sec
  865. Others: NULL
  866. *************************************************/
  867. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  868. {
  869. beep_init();
  870. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  871. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  872. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  873. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  874. rt_pin_write(Motor_EN, EN);
  875. rt_pin_write(Motor_DIR, DIR);
  876. beep_set(pwm, pwm/2); //200kzh - 50%
  877. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  878. {
  879. if(time_out==0)
  880. {
  881. break;
  882. }
  883. rt_thread_delay(50);
  884. time_out --;
  885. }
  886. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  887. {
  888. if(time_out==0)
  889. {
  890. break;
  891. }
  892. rt_thread_delay(50);
  893. time_out --;
  894. }
  895. beep_set(0, 0); //200kzh - 50%
  896. rt_thread_delay(50);
  897. }
  898. /*************************************************
  899. Function: ctr_Petroleum_jelly
  900. Description: 涂凡士林
  901. Calls:
  902. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  903. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  904. Input: NULL
  905. Output: NULL
  906. Return: NULL
  907. Others: NULL
  908. *************************************************/
  909. void ctr_Petroleum_jelly(rt_uint32_t delay)
  910. {
  911. beep_init();
  912. pwm_on(2,50000);
  913. rt_thread_delay(delay*1000);
  914. pwm_off(2,0);
  915. rt_thread_delay(1000);
  916. pwm_on(3, 50000);
  917. rt_thread_delay(delay*1000);
  918. pwm_off(3,0);
  919. device_status.fsl_sw = 0;
  920. }
  921. //1.0转仓
  922. void turnzp(void)
  923. {
  924. LOG_W("转仓执行!");
  925. static uint16_t turn_timeout = 0;
  926. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  927. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  928. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  929. // rt_pin_write(CLEAR_R,OFF);
  930. // rt_pin_write(CLEAR_L,OFF);
  931. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  932. {
  933. rt_thread_delay(20);
  934. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  935. {
  936. ctr_Collect_Fan(ON);
  937. rt_thread_delay(50);
  938. turn_timeout++;
  939. if(turn_timeout==600)
  940. {
  941. LOG_E("检测不到限位,电机强制停止");
  942. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  943. DTU_Write("%s",ctr_zp_limit_warn);
  944. rt_free(ctr_zp_limit_warn);
  945. break;
  946. }
  947. }
  948. break;
  949. }
  950. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  951. {
  952. rt_thread_delay(20);
  953. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  954. {
  955. // rt_pin_write(CLEAR_R,ON);
  956. // rt_pin_write(CLEAR_L,OFF);
  957. ctr_Collect_Fan(ON);
  958. rt_thread_delay(50);
  959. turn_timeout++;
  960. if(turn_timeout==600)
  961. {
  962. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  963. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  964. DTU_Write("%s",ctr_zp_limit_warn);
  965. rt_free(ctr_zp_limit_warn);
  966. break;
  967. }
  968. }
  969. break;
  970. }
  971. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  972. {
  973. rt_thread_delay(20);
  974. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  975. {
  976. // rt_pin_write(CLEAR_R,OFF);
  977. // rt_pin_write(CLEAR_L,OFF);
  978. ctr_Collect_Fan(OFF);
  979. break;
  980. }
  981. else {
  982. break;
  983. }
  984. }
  985. ctr_Collect_Fan(OFF);
  986. turn_timeout = 0;
  987. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  988. rt_thread_delay(1000);
  989. }
  990. //清扫-常闭型限位开关
  991. //新结构使用推杆电机做清扫
  992. uint8_t ctr_clear(uint16_t status_clear)
  993. {
  994. static uint16_t clear_num = 0;
  995. #if PWM_EN == 0
  996. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  997. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  998. #endif
  999. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1000. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1001. /*************************************************************外接清扫
  1002. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1003. rt_pin_write(POWER_CLEAR,ON);
  1004. rt_thread_delay(1000*100);
  1005. rt_pin_write(POWER_CLEAR,OFF);
  1006. ************************************************************/
  1007. //停止
  1008. #if PWM_EN == 0
  1009. rt_pin_write(CLEAR_R,OFF);
  1010. rt_pin_write(CLEAR_L,OFF);
  1011. #else
  1012. Motor_Qingsao_R_PWM(OFF);
  1013. Motor_Qingsao_L_PWM(OFF);
  1014. #endif
  1015. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1016. rt_thread_delay(100);
  1017. //如果清扫位置在中间
  1018. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1019. {
  1020. #if PWM_EN == 0
  1021. rt_pin_write(CLEAR_R,ON);
  1022. rt_pin_write(CLEAR_L,OFF);
  1023. #else
  1024. Motor_Qingsao_R_PWM(ON);
  1025. Motor_Qingsao_L_PWM(OFF);
  1026. #endif
  1027. rt_thread_delay(50);
  1028. clear_num++;
  1029. //已到达检测位置
  1030. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1031. {
  1032. #if PWM_EN == 0
  1033. rt_pin_write(CLEAR_R,ON);
  1034. rt_pin_write(CLEAR_L,ON);
  1035. #else
  1036. Motor_Qingsao_R_PWM(OFF);
  1037. Motor_Qingsao_L_PWM(OFF);
  1038. #endif
  1039. rt_thread_delay(2000);
  1040. break;
  1041. }
  1042. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1043. {
  1044. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1045. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1046. #if PWM_EN == 0
  1047. rt_pin_write(CLEAR_R,ON);
  1048. rt_pin_write(CLEAR_L,ON);
  1049. #else
  1050. Motor_Qingsao_R_PWM(OFF);
  1051. Motor_Qingsao_L_PWM(OFF);
  1052. #endif
  1053. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1054. if(Read_Real_Voltage()/100<12)
  1055. {
  1056. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1057. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1058. device_status.clear_status = 0;
  1059. return 0;
  1060. }
  1061. break;
  1062. }
  1063. }
  1064. clear_num = 0;
  1065. //如果清扫位置在左侧
  1066. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1067. {
  1068. #if PWM_EN == 0
  1069. rt_pin_write(CLEAR_R,ON);
  1070. rt_pin_write(CLEAR_L,OFF);
  1071. #else
  1072. Motor_Qingsao_R_PWM(ON);
  1073. Motor_Qingsao_L_PWM(OFF);
  1074. #endif
  1075. rt_thread_delay(50);
  1076. clear_num++;
  1077. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1078. {
  1079. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1080. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1081. #if PWM_EN == 0
  1082. rt_pin_write(CLEAR_R,ON);
  1083. rt_pin_write(CLEAR_L,ON);
  1084. #else
  1085. Motor_Qingsao_R_PWM(OFF);
  1086. Motor_Qingsao_L_PWM(OFF);
  1087. #endif
  1088. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1089. if(Read_Real_Voltage()/100<12)
  1090. {
  1091. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1092. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1093. device_status.clear_status = 0;
  1094. return 0;
  1095. }
  1096. break;
  1097. }
  1098. }
  1099. clear_num = 0;
  1100. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1101. {
  1102. #if PWM_EN == 0
  1103. rt_pin_write(CLEAR_R,ON);
  1104. rt_pin_write(CLEAR_L,ON);
  1105. #else
  1106. Motor_Qingsao_R_PWM(OFF);
  1107. Motor_Qingsao_L_PWM(OFF);
  1108. #endif
  1109. if(Read_Real_Voltage()/100<12)
  1110. {
  1111. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1112. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1113. device_status.clear_status = 0;
  1114. return 0;
  1115. }
  1116. rt_thread_delay(2000);
  1117. break;
  1118. }
  1119. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1120. {
  1121. #if PWM_EN == 0
  1122. rt_pin_write(CLEAR_R,OFF);
  1123. rt_pin_write(CLEAR_L,ON);
  1124. #else
  1125. Motor_Qingsao_R_PWM(OFF);
  1126. Motor_Qingsao_L_PWM(ON);
  1127. #endif
  1128. rt_thread_delay(50);
  1129. clear_num++;
  1130. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1131. {
  1132. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1133. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1134. //DTU_Write("%s",Clear_ERR);
  1135. #if PWM_EN == 0
  1136. rt_pin_write(CLEAR_R,ON);
  1137. rt_pin_write(CLEAR_L,ON);
  1138. #else
  1139. Motor_Qingsao_R_PWM(OFF);
  1140. Motor_Qingsao_L_PWM(OFF);
  1141. #endif
  1142. rt_free(Clear_ERR);
  1143. if(Read_Real_Voltage()/100<12)
  1144. {
  1145. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1146. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1147. device_status.clear_status = 0;
  1148. return 0;
  1149. }
  1150. break;
  1151. }
  1152. }
  1153. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1154. {
  1155. #if PWM_EN == 0
  1156. rt_pin_write(CLEAR_R,ON);
  1157. rt_pin_write(CLEAR_L,ON);
  1158. #else
  1159. Motor_Qingsao_R_PWM(OFF);
  1160. Motor_Qingsao_L_PWM(OFF);
  1161. #endif
  1162. rt_thread_delay(2000);
  1163. break;
  1164. }
  1165. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1166. {
  1167. #if PWM_EN == 0
  1168. rt_pin_write(CLEAR_R,ON);
  1169. rt_pin_write(CLEAR_L,ON);
  1170. #else
  1171. Motor_Qingsao_R_PWM(OFF);
  1172. Motor_Qingsao_L_PWM(OFF);
  1173. #endif
  1174. if(device_status.version_type==41)
  1175. {
  1176. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1177. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1178. //DTU_Write("%s",Clear_ERR);
  1179. rt_free(Clear_ERR);
  1180. }
  1181. return 0;
  1182. }
  1183. clear_num = 0;
  1184. rt_thread_delay(2000);
  1185. #if PWM_EN == 0
  1186. rt_pin_write(CLEAR_R,ON);
  1187. rt_pin_write(CLEAR_L,ON);
  1188. #else
  1189. Motor_Qingsao_R_PWM(OFF);
  1190. Motor_Qingsao_L_PWM(OFF);
  1191. #endif
  1192. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1193. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1194. device_status.clear_status = 0;
  1195. return 1;
  1196. }
  1197. //路由器供电
  1198. void ctr_router(uint8_t status_router)
  1199. {
  1200. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1201. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1202. if(status_router==ON)
  1203. {
  1204. rt_pin_write(ROUTER,ON);
  1205. }
  1206. else {
  1207. rt_pin_write(ROUTER,OFF);
  1208. }
  1209. }
  1210. //清扫-推杆电机
  1211. void ctr_TG_Clear(uint16_t clear_timeout)
  1212. {
  1213. #if 0
  1214. int current_clear[200];
  1215. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1216. LOG_I("清扫电机待机:%d",Electric_current);
  1217. #endif
  1218. static uint16_t clear_count = 0;
  1219. /* 屏幕 workSta.t6「清扫结束剩余时间」用的尾段常量:
  1220. * 全程 = 正向推杆(clear_timeout*100ms) + 3s 停顿 + 反向复位(clear_timeout*100ms) + 2s 收尾 */
  1221. uint16_t clear_left_tail = (uint16_t)(clear_timeout / 10) + 3 + 2;
  1222. clear_count = clear_timeout;
  1223. #if PWM_EN == 0
  1224. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1225. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1226. #endif
  1227. while(clear_count!=0)
  1228. {
  1229. #if PWM_EN == 0
  1230. rt_pin_write(CLEAR_R,OFF);
  1231. rt_pin_write(CLEAR_L,ON);
  1232. #else
  1233. #if QINGSAO_IO == 1
  1234. Motor_Qingsao_R_PWM(OFF);
  1235. Motor_Qingsao_L_PWM(ON);
  1236. #else
  1237. Motor_Qingsao_R_PWM(ON);
  1238. Motor_Qingsao_L_PWM(OFF);
  1239. #endif
  1240. #endif
  1241. clear_count--;
  1242. device_status.clear_left = (uint16_t)(clear_count / 10) + clear_left_tail; /* t6 剩余清扫时间(s) */
  1243. rt_thread_delay(100);
  1244. }
  1245. #if PWM_EN == 0
  1246. rt_pin_write(CLEAR_R,ON);
  1247. rt_pin_write(CLEAR_L,ON);
  1248. #else
  1249. Motor_Qingsao_R_PWM(OFF);
  1250. Motor_Qingsao_L_PWM(OFF);
  1251. #endif
  1252. rt_thread_delay(3000);
  1253. clear_count = 0;
  1254. while(clear_timeout!=clear_count)
  1255. {
  1256. #if PWM_EN == 0
  1257. rt_pin_write(CLEAR_R,ON);
  1258. rt_pin_write(CLEAR_L,OFF);
  1259. #else
  1260. #if QINGSAO_IO == 1
  1261. Motor_Qingsao_R_PWM(ON);
  1262. Motor_Qingsao_L_PWM(OFF);
  1263. #else
  1264. Motor_Qingsao_R_PWM(OFF);
  1265. Motor_Qingsao_L_PWM(ON);
  1266. #endif
  1267. #endif
  1268. // if(clear_count<200)
  1269. // current_clear[clear_count] = atoi(device_status.power);
  1270. clear_count++;
  1271. device_status.clear_left = (uint16_t)((clear_timeout - clear_count) / 10) + 2; /* t6 反向复位剩余(s) */
  1272. rt_thread_delay(100);
  1273. }
  1274. #if PWM_EN == 0
  1275. rt_pin_write(CLEAR_R,ON);
  1276. rt_pin_write(CLEAR_L,ON);
  1277. #else
  1278. Motor_Qingsao_R_PWM(OFF);
  1279. Motor_Qingsao_L_PWM(OFF);
  1280. #endif
  1281. #if 0
  1282. bubblesort(current_clear,200);
  1283. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1284. {
  1285. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1286. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1287. DTU_Write("%s",ctr_clear_warn);
  1288. rt_free(ctr_clear_warn);
  1289. }
  1290. #endif
  1291. rt_thread_delay(2000);
  1292. device_status.clear_status = 0;
  1293. device_status.clear_left = 0; /* 清扫结束,倒计时清零 */
  1294. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1295. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1296. }
  1297. // 全局静态变量,保存上次关闭的 tick
  1298. static rt_uint32_t s_last_off_tick = 0;
  1299. void ctr_RTU_DTU(uint8_t status_rtu)
  1300. {
  1301. // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
  1302. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1303. if (status_rtu == ON)
  1304. {
  1305. // 开机逻辑:必须满足冷却时间才能开机
  1306. rt_uint32_t current_tick = rt_tick_get();
  1307. rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
  1308. if (s_last_off_tick != 0)
  1309. {
  1310. rt_uint32_t diff_tick = current_tick - s_last_off_tick;
  1311. if (diff_tick < cool_ticks)
  1312. {
  1313. // 未冷却完成,计算剩余时间
  1314. rt_uint32_t wait_ticks = cool_ticks - diff_tick;
  1315. rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
  1316. LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
  1317. // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
  1318. rt_thread_delay(wait_ticks);
  1319. }
  1320. }
  1321. // 冷却完成,执行开机
  1322. rt_pin_write(RTU_DTU, ON);
  1323. s_last_off_tick = 0; // 清除关机时间
  1324. LOG_W("RTU电源启动!!!");
  1325. }
  1326. else
  1327. {
  1328. // 关机逻辑:记录关机时刻tick
  1329. rt_pin_write(RTU_DTU, OFF);
  1330. s_last_off_tick = rt_tick_get();
  1331. LOG_W("RTU电源关闭:%d",s_last_off_tick);
  1332. }
  1333. }
  1334. /**
  1335. * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
  1336. */
  1337. void RTU_Power_Off_Safe(void)
  1338. {
  1339. /* 2. 【关键】关闭串口设备。
  1340. * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
  1341. * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
  1342. */
  1343. rt_device_close(dtuserial);
  1344. /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
  1345. * extern UART_HandleTypeDef huart2;
  1346. * HAL_UART_DeInit(&huart2);
  1347. */
  1348. /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
  1349. * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
  1350. */
  1351. rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
  1352. rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
  1353. /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
  1354. rt_thread_mdelay(50);
  1355. /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
  1356. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1357. rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
  1358. LOG_I("RTU 12V Power OFF safely.\n");
  1359. }
  1360. /**
  1361. * @brief 恢复 RTU 12V 电源并重新初始化串口
  1362. */
  1363. void RTU_Power_On_Restore(void)
  1364. {
  1365. rt_thread_mdelay(5000);
  1366. /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
  1367. rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
  1368. LOG_I("RTU 12V Power ON.\n");
  1369. /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
  1370. * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
  1371. */
  1372. rt_thread_mdelay(5000);
  1373. /* 3. 【关键】重新打开串口设备。
  1374. * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为
  1375. * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。
  1376. */
  1377. GPIO_InitTypeDef gpioInitStruct;
  1378. __HAL_RCC_GPIOA_CLK_ENABLE();
  1379. //PA2 TXD
  1380. gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式
  1381. gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9
  1382. gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率
  1383. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1384. //PA3 RXD
  1385. gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式
  1386. gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10
  1387. gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率
  1388. HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
  1389. #ifdef RT_USING_SERIAL_V2
  1390. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  1391. if(uart_res!=RT_EOK)
  1392. {
  1393. LOG_E("%s打开失败!",RTU_UART_NAME);
  1394. }else{
  1395. LOG_I("%s Open Success!",RTU_UART_NAME);
  1396. }
  1397. #endif
  1398. #ifdef RT_USING_SERIAL_V1
  1399. rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  1400. #endif
  1401. /* 4. 【关键】重新注册接收回调
  1402. * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
  1403. * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
  1404. * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
  1405. */
  1406. dtu_uart2_reinit_rx();
  1407. }
  1408. /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
  1409. void cmd_rtu_restart(int argc, char** argv)
  1410. {
  1411. rt_kprintf("Starting RTU safe restart sequence...\n");
  1412. // 1. 安全断电
  1413. RTU_Power_Off_Safe();
  1414. // 2. 保持断电 30 秒 (确保电容彻底放电)
  1415. rt_kprintf("Waiting 30s for capacitor discharge...\n");
  1416. rt_thread_mdelay(30000);
  1417. // 3. 恢复供电并重建串口
  1418. RTU_Power_On_Restore();
  1419. rt_kprintf("RTU restart sequence completed.\n");
  1420. }
  1421. //海康拍照
  1422. void Hk_Take_io(void)
  1423. {
  1424. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1425. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1426. rt_pin_write(HK_TAKE, 1);
  1427. rt_pin_write(HK_IO, 0);
  1428. if(device_status.dtype==3)
  1429. Beep_Tip(1000);
  1430. if(device_status.dtype==7)
  1431. rt_thread_mdelay(1000);
  1432. rt_pin_write(HK_TAKE, 0);
  1433. rt_pin_write(HK_IO, 1);
  1434. }
  1435. void calorstat(uint16_t heat_time)
  1436. {
  1437. //uint8_t heat_up_cnt = 0;
  1438. uint16_t n = 0;
  1439. uint16_t delay_heat = 60;
  1440. FlagStatus heat_flg = RESET;
  1441. device_status.heat_time_min = 0;
  1442. device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
  1443. ctr_Heat(ON);
  1444. // UpLoad_Data(); //开始加热时的第一条数据
  1445. //恒温模式
  1446. for(n=0;n < heat_time;n++)
  1447. {
  1448. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1449. {
  1450. heat_flg = SET;
  1451. ctr_Heat(OFF);
  1452. while(delay_heat!=0)
  1453. {
  1454. device_status.C01 &= ~(1<<1); //加热 正常
  1455. delay_heat --;
  1456. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1457. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1458. rt_thread_delay(1000);
  1459. if(device_status.heattem>device_status.heat_tem_max + 1){
  1460. device_status.heat_tem_max = device_status.heattem;
  1461. }
  1462. }
  1463. delay_heat = 60;
  1464. //跳出FOR循环
  1465. n = n + 60;
  1466. }
  1467. if(device_status.heattem>device_status.run_sta + 1){
  1468. device_status.heat_tem_max = device_status.heattem;
  1469. }
  1470. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1471. {
  1472. device_status.heat_tem_max = device_status.heattem;
  1473. ctr_Heat(ON);
  1474. }
  1475. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1476. {
  1477. // if(heat_up_cnt==0)
  1478. // {
  1479. // UpLoad_Data(); //加热到设定温度的数据
  1480. // heat_up_cnt = 1;
  1481. // }
  1482. device_status.heat_tem_max = device_status.heattem;
  1483. ctr_Heat(OFF);
  1484. }
  1485. if((n % 60)==0)
  1486. {
  1487. device_status.heat_time_min = device_status.heat_time_min + 1;
  1488. if(device_status.heat_time_min % 5 == 0) //加热5分钟温度低于50,判定加热故障
  1489. {
  1490. if(device_status.heattem<50) {
  1491. device_status.C01 |= (1 << 1); // 第1位变为1,加热故障
  1492. }else {
  1493. device_status.C01 &= ~(1<<1); //加热 正常
  1494. }
  1495. }
  1496. }
  1497. /* 屏幕 workSta.t1:剩余加热时间(秒)。恒温等待阶段 n 未推进,剩余时间按同一口径显示 */
  1498. device_status.work_heat_left = (n < heat_time) ? (uint16_t)(heat_time - n) : 0;
  1499. //如果触发异常退出加热
  1500. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1501. {
  1502. rt_thread_delay(1000);
  1503. if(device_status.bat_status == 1)
  1504. {
  1505. rt_kprintf("============================触发异常退出加热==========================");
  1506. ctr_lamp(OFF,1);
  1507. ctr_Heat(OFF);
  1508. break;
  1509. }
  1510. }
  1511. if(device_status.work_mode==1){
  1512. LOG_W("切换模式,退出加热!");
  1513. break;
  1514. }
  1515. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1516. {
  1517. ctr_Heat(OFF);
  1518. rt_kprintf("异常退出!!!\n");
  1519. break;
  1520. }
  1521. rt_thread_delay(1000);
  1522. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1523. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1524. }
  1525. device_status.work_heat_left = 0; /* 加热结束,倒计时清零 */
  1526. ctr_Heat(OFF);
  1527. //UpLoad_Data(); //加热结束的数据
  1528. }
  1529. //拍照
  1530. void HK_TakePhoto(void)
  1531. {
  1532. //打开补光灯
  1533. //DTU_Write("takephoto.mp3");
  1534. rt_thread_delay(10000);
  1535. rt_timer_start(timer_takephoto);
  1536. char *takephotojson = makeJson_takephoto();
  1537. DTU_Write("%s",takephotojson);
  1538. rt_free(takephotojson);
  1539. //吸虫塔
  1540. if(device_status.version_type!=6)
  1541. {
  1542. ctr_led(ON);
  1543. if(device_status.version_type==48)
  1544. {
  1545. rt_thread_delay(1000*60);
  1546. }
  1547. else {
  1548. rt_thread_delay(1000*15);
  1549. }
  1550. }
  1551. //触发海康相机拍照
  1552. Hk_Take_io();
  1553. if(device_status.version_type!=6)
  1554. {
  1555. rt_thread_delay(1000*45);
  1556. }
  1557. else {
  1558. ctr_led(ON);
  1559. rt_thread_delay(1000*19);
  1560. }
  1561. ctr_led(OFF);
  1562. }
  1563. //光控测试任务
  1564. void SunTest(void)
  1565. {
  1566. rt_uint16_t ui_timeout_l = 0;
  1567. //未检测到光照,一秒扫描等待,超时30S
  1568. while(Read_Light_Voltage()<500)
  1569. {
  1570. ui_timeout_l ++;
  1571. rt_thread_delay(1000);
  1572. if(ui_timeout_l==30) break;
  1573. }
  1574. rt_thread_delay(3000);
  1575. if(Read_Light_Voltage()<500)
  1576. {
  1577. device_status.sun_status = 0;//光控状态
  1578. }
  1579. if(Read_Light_Voltage()>500)
  1580. {
  1581. rt_thread_delay(1000);
  1582. }
  1583. rt_thread_delay(3000);
  1584. if(Read_Light_Voltage()>500)
  1585. {
  1586. device_status.sun_status = 1;
  1587. ctr_lamp(OFF,1);
  1588. }
  1589. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1590. device_status.sun_test = 0;
  1591. device_status.work_status=0;
  1592. }
  1593. /*上电初始化*/
  1594. void ResetInit(void)
  1595. {
  1596. /* 复位时清掉屏幕 workSta 页的实时倒计时/进度(t1~t6),避免残留旧值 */
  1597. device_status.work_heat_left = 0;
  1598. device_status.work_clt_left = 0;
  1599. device_status.work_door_left = 0;
  1600. device_status.work_progress = 0;
  1601. device_status.photo_left = 0;
  1602. device_status.clear_left = 0;
  1603. device_status.work_start_tick = 0;
  1604. device_status.work_total_sec = 0;
  1605. if(device_status.version_type!=6&&device_status.version_type!=7)
  1606. {
  1607. device_status.work_done = 1;
  1608. //1.打开路由器供电
  1609. ctr_router(ON);
  1610. //2.打开相机供电
  1611. ctr_photo(ON);
  1612. ctr_RTU_DTU(ON);
  1613. //3.关闭镇流器
  1614. ctr_lamp(OFF,1);
  1615. //4.关闭震动电机
  1616. ctr_shake(OFF, 0);
  1617. //5.关闭加热
  1618. ctr_Heat(OFF);
  1619. //6.关闭补光灯
  1620. if(device_status.photo_ws==0)
  1621. ctr_led(OFF);
  1622. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1623. {
  1624. LOG_W("欠压!");
  1625. return;
  1626. }
  1627. //7.关闭上仓门
  1628. device_status.ctr_up_motor = 2;
  1629. // while(device_status.ctr_up_motor!=0)
  1630. // {
  1631. // LOG_I("关闭上仓门...");
  1632. // rt_thread_delay(100);
  1633. // }
  1634. //8.关闭下仓门
  1635. device_status.ctr_down_motor = 2;
  1636. while(device_status.ctr_down_motor!=0)
  1637. {
  1638. LOG_I("关闭下仓门....");
  1639. rt_thread_delay(100);
  1640. }
  1641. //9.清扫
  1642. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1643. {
  1644. LOG_W("异常退出!!!\n");
  1645. return;
  1646. }
  1647. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  1648. {
  1649. if(device_status.photo_ws==0) device_status.clear_status=1;
  1650. while(device_status.clear_status==1){
  1651. rt_thread_delay(1000);
  1652. }
  1653. #if JINGJIKUAN == 1
  1654. //再执行4次清扫
  1655. for(int i=0;i<4;i++)
  1656. {
  1657. if(device_status.photo_ws==0) device_status.clear_status=1;
  1658. while(device_status.clear_status==1){
  1659. rt_thread_delay(1000);
  1660. }
  1661. }
  1662. #endif
  1663. }
  1664. }
  1665. else {
  1666. if(device_status.power_status==1)
  1667. {
  1668. //1.打开路由器供电
  1669. #if MATCH_DEVICE ==0
  1670. ctr_router(ON);
  1671. //2.打开相机供电
  1672. ctr_photo(ON);
  1673. #endif
  1674. ctr_RTU_DTU(ON);
  1675. }
  1676. }
  1677. }
  1678. /*三色光源*/
  1679. void Led_Check(uint8_t tp)
  1680. {
  1681. if(tp==3)
  1682. {
  1683. if(device_status.light_over_tim>2*3600)
  1684. {
  1685. device_status.work_status = 1;
  1686. device_status.lamp_status = 1;
  1687. device_status.lamp1_status = 0;
  1688. device_status.lamp2_status = 0;
  1689. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1690. }
  1691. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1692. device_status.work_status = 1;
  1693. device_status.lamp_status = 0;
  1694. device_status.lamp1_status = 1;
  1695. device_status.lamp2_status = 0;
  1696. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1697. }
  1698. else if (device_status.light_over_tim<1*3600) {
  1699. device_status.work_status = 1;
  1700. device_status.lamp_status = 0;
  1701. device_status.lamp1_status = 0;
  1702. device_status.lamp2_status = 1;
  1703. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1704. }
  1705. }
  1706. else if (tp==6) {
  1707. if(device_status.light_over_tim>4*3600)
  1708. {
  1709. device_status.work_status = 1;
  1710. device_status.lamp_status = 1;
  1711. device_status.lamp1_status = 0;
  1712. device_status.lamp2_status = 0;
  1713. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1714. }
  1715. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1716. device_status.work_status = 1;
  1717. device_status.lamp_status = 0;
  1718. device_status.lamp1_status = 1;
  1719. device_status.lamp2_status = 0;
  1720. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1721. }
  1722. else if (device_status.light_over_tim<7200) {
  1723. device_status.work_status = 1;
  1724. device_status.lamp_status = 0;
  1725. device_status.lamp1_status = 0;
  1726. device_status.lamp2_status = 1;
  1727. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1728. }
  1729. }
  1730. else if (tp==9) {
  1731. if(device_status.light_over_tim>6*3600)
  1732. {
  1733. device_status.work_status = 1;
  1734. device_status.lamp_status = 1;
  1735. device_status.lamp1_status = 0;
  1736. device_status.lamp2_status = 0;
  1737. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1738. }
  1739. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1740. device_status.work_status = 1;
  1741. device_status.lamp_status = 0;
  1742. device_status.lamp1_status = 1;
  1743. device_status.lamp2_status = 0;
  1744. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1745. }
  1746. else if (device_status.light_over_tim<3*3600) {
  1747. device_status.work_status = 1;
  1748. device_status.lamp_status = 0;
  1749. device_status.lamp1_status = 0;
  1750. device_status.lamp2_status = 1;
  1751. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1752. }
  1753. }
  1754. }
  1755. void WorkCheck(void)
  1756. {
  1757. static rt_uint32_t light_timeout = 3600;
  1758. while(device_status.ds == 1 &&
  1759. device_status.rain_status == 0 &&
  1760. device_status.temp_status == 0 &&
  1761. device_status.bat_status == 0)
  1762. {
  1763. if(device_status.timeSwitch == 1) // 时控模式
  1764. {
  1765. LOG_I("***********时控*****************");
  1766. time_t now;
  1767. time(&now);
  1768. struct tm now_tm = *localtime(&now);
  1769. if(device_status.startBar == device_status.stopBar)
  1770. {
  1771. // 开始结束时间相同则24小时模式
  1772. device_status.work_status = 1;
  1773. rt_thread_delay(10000);
  1774. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1775. device_status.work_status ? "工作" : "待机",
  1776. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1777. device_status.startBar, device_status.stopBar);
  1778. continue;
  1779. }
  1780. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1781. rt_uint32_t start_sec = device_status.startBar * 3600;
  1782. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1783. // 修复跨天判断逻辑
  1784. if (start_sec < end_sec)
  1785. {
  1786. // 不跨天模式
  1787. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1788. }
  1789. else
  1790. {
  1791. // 跨天模式
  1792. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1793. }
  1794. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1795. device_status.work_status ? "工作" : "待机",
  1796. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1797. device_status.startBar, device_status.stopBar);
  1798. rt_thread_delay(10000);
  1799. }
  1800. else // 光控模式
  1801. {
  1802. LOG_I("光控定时模式!");
  1803. if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
  1804. {
  1805. device_status.light_over_tim--;
  1806. light_timeout = 3600; // 重置超时计数器
  1807. device_status.work_status = 1;
  1808. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1809. }
  1810. else
  1811. {
  1812. // 仅在白天递减超时计数器
  1813. if(device_status.sun_status == 1 && light_timeout > 0) {
  1814. light_timeout--;
  1815. }
  1816. device_status.work_status = 0;
  1817. //没有在测试并且没有在自检
  1818. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1819. {
  1820. ctr_lamp(OFF,1);
  1821. }
  1822. // 连续1小时白天后重置
  1823. if(light_timeout == 0) {
  1824. device_status.light_over_tim = 0;
  1825. light_timeout = 3600; // 重置防止负数
  1826. device_status.light_over_tim = device_status.sunBar * 3600;
  1827. }
  1828. }
  1829. }
  1830. rt_thread_delay(1000);
  1831. }
  1832. // 异常状态处理
  1833. device_status.work_status = 0;
  1834. rt_thread_delay(1000);
  1835. }
  1836. //工作任务流程s
  1837. /* ==================== 工作进度(屏幕 workSta.t4 工作进度百分比) ====================
  1838. * 口径:按时间加权——进度 = 已用时间 / 本轮预计总时长 × 100%。
  1839. * 预计总时长在每轮工作开始时按参数估算:收集 + 加热 + 拍照(60s) + 清扫(85s) + 仓门动作预留(40s)。
  1840. * 实际存在仓门寻位、恒温等待、异常提前退出等偏差,故进度上限钳到 99%,
  1841. * 与 t1~t3/t5/t6 的实时倒计时配合显示即可反映真实过程;非工作态返回 0。 */
  1842. void Work_Progress_Start(void)
  1843. {
  1844. uint32_t total;
  1845. total = (uint32_t)device_status.clt_time * 60
  1846. + (uint32_t)device_status.heattime * 60
  1847. + WORK_PHOTO_SEC + WORK_CLEAR_SEC + WORK_DOOR_SEC;
  1848. if (total == 0) total = 1;
  1849. if (total > 0xFFFF) total = 0xFFFF; /* work_total_sec 为 uint16_t */
  1850. device_status.work_total_sec = (uint16_t)total;
  1851. device_status.work_start_tick = rt_tick_get();
  1852. device_status.work_progress = 0;
  1853. /* 新的一轮工作开始,清掉上一轮的实时倒计时 */
  1854. device_status.work_heat_left = 0;
  1855. device_status.work_clt_left = 0;
  1856. device_status.work_door_left = 0;
  1857. device_status.photo_left = 0;
  1858. device_status.clear_left = 0;
  1859. }
  1860. uint8_t Work_Progress_Get(void)
  1861. {
  1862. uint32_t elapsed;
  1863. uint32_t total = device_status.work_total_sec;
  1864. if (device_status.work_status != 1 || device_status.work_start_tick == 0 || total == 0)
  1865. {
  1866. return 0;
  1867. }
  1868. elapsed = (rt_tick_get() - device_status.work_start_tick) / RT_TICK_PER_SECOND;
  1869. device_status.work_progress = (uint8_t)((elapsed >= total) ? 99 : (elapsed * 100 / total));
  1870. return device_status.work_progress;
  1871. }
  1872. void WorkTask(void)
  1873. {
  1874. static int clt_time = 0; //收集时间差秒数
  1875. static int heat_time = 0;
  1876. LOG_I("工作状态:%d",device_status.work_status);
  1877. LOG_I("开关机状态:%d",device_status.ds);
  1878. while(device_status.clear_status==1){
  1879. LOG_I("等待清扫执行完成....");
  1880. rt_thread_delay(1000);
  1881. }
  1882. 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)
  1883. {
  1884. if(device_status.work_mode==1){
  1885. break;
  1886. }
  1887. device_status.work_done = 0;
  1888. /* 每一轮工作开始(外层 while 每轮一次):记录进度起点与预计总时长,
  1889. * 并清零上一轮的实时倒计时(屏幕 workSta.t1~t6)。 */
  1890. Work_Progress_Start();
  1891. if(clt_time==0)
  1892. {
  1893. DTU_Write("workstart.mp3");
  1894. if(device_status.debug_ws==1)
  1895. {
  1896. time_t stamp=time(RT_NULL);
  1897. static struct tm tm;
  1898. gmtime_r(&stamp, &tm);
  1899. 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);
  1900. dtu_write_json(makeJson_Warn("Work start!", device_status.stamp));
  1901. }
  1902. }
  1903. ctr_lamp(ON,1);
  1904. LOG_I("打开灯管!!!");
  1905. if(device_status.version_type!=5)
  1906. {
  1907. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  1908. //2.打开上仓门
  1909. LOG_I("打开上仓门!!!");
  1910. device_status.ctr_up_motor = 1;
  1911. //3.收集
  1912. clt_time = device_status.clt_time * 60;
  1913. while(clt_time!=1)
  1914. {
  1915. clt_time --;
  1916. device_status.work_clt_left = (uint16_t)clt_time; /* 屏幕 workSta.t2 收集倒计时(s) */
  1917. rt_thread_delay(1000);
  1918. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  1919. if(device_status.work_mode==1){
  1920. break;
  1921. }
  1922. //如果触发异常
  1923. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1924. {
  1925. ctr_lamp(OFF,1);
  1926. #if 0//LAMP_EN == 0
  1927. ctr_Fan_IN(OFF);
  1928. #endif
  1929. rt_kprintf("异常退出!!!\n");
  1930. break;
  1931. }
  1932. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1933. {
  1934. ctr_lamp(OFF,1);
  1935. #if 0//LAMP_EN == 0
  1936. ctr_Fan_IN(OFF);
  1937. #endif
  1938. rt_kprintf("异常退出!!!\n");
  1939. break;
  1940. }
  1941. }
  1942. clt_time = 1;
  1943. device_status.work_clt_left = 0; /* 收集结束,倒计时清零 */
  1944. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1945. {
  1946. ctr_lamp(OFF,1);
  1947. #if 0//LAMP_EN == 0
  1948. ctr_Fan_IN(OFF);
  1949. #endif
  1950. rt_kprintf("异常退出!!!\n");
  1951. break;
  1952. }
  1953. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1954. {
  1955. ctr_lamp(OFF,1);
  1956. #if 0//LAMP_EN == 0
  1957. ctr_Fan_IN(OFF);
  1958. #endif
  1959. rt_kprintf("异常退出!!!\n");
  1960. break;
  1961. }
  1962. #if 0//LAMP_EN == 0
  1963. ctr_Fan_IN(OFF);
  1964. #endif
  1965. //4.关闭上仓门
  1966. device_status.ctr_up_motor = 2;
  1967. rt_kprintf("关闭上仓门!!!\n");
  1968. while(device_status.ctr_up_motor!=0)
  1969. {
  1970. rt_thread_delay(100);
  1971. LOG_I("等待上仓门关闭!!!");
  1972. }
  1973. //5.加热
  1974. if(device_status.heat_sw==0)
  1975. {
  1976. calorstat(device_status.heattime*60);
  1977. }
  1978. else {
  1979. heat_time = device_status.heattime * 60;
  1980. while(heat_time!=0)
  1981. {
  1982. if(device_status.work_mode==1)
  1983. {
  1984. break;
  1985. }
  1986. heat_time--;
  1987. device_status.work_heat_left = (uint16_t)heat_time; /* 屏幕 workSta.t1 剩余加热时间(s) */
  1988. LOG_I("正在加热状态!!!");
  1989. rt_thread_delay(1000);
  1990. }
  1991. }
  1992. device_status.work_heat_left = 0;
  1993. #if TG_MODULE == 0
  1994. //6.打开下仓门
  1995. if(device_status.rain_status==0)
  1996. {
  1997. device_status.ctr_down_motor =1;
  1998. }
  1999. else {
  2000. device_status.ctr_down_motor = 2;
  2001. device_status.ctr_up_motor = 2;
  2002. }
  2003. //待下仓门完全打开
  2004. while(device_status.ctr_down_motor!=0)
  2005. {
  2006. rt_thread_delay(100);
  2007. rt_kprintf("等待下仓门打开!!!\n");
  2008. }
  2009. rt_thread_delay(3000);
  2010. //关闭下仓门
  2011. device_status.ctr_down_motor = 2;
  2012. while(device_status.ctr_down_motor!=0)
  2013. {
  2014. rt_thread_delay(100);
  2015. rt_kprintf("等待下仓门关闭!!!\n");
  2016. }
  2017. //2022-04-12add:
  2018. LOG_I("打开上仓门!!!");
  2019. device_status.ctr_up_motor = 1;
  2020. #else
  2021. //打开下仓门
  2022. ctr_cangmen_down(OPEN);
  2023. uint16_t timeout_down_open = 0;
  2024. while(1)
  2025. {
  2026. timeout_down_open++;
  2027. if(rt_pin_read(CTR_UPC_LIMIT)==1||timeout_down_open>20*20) break;
  2028. rt_thread_delay(50);
  2029. }
  2030. ctr_cangmen_down(OFF);
  2031. rt_thread_delay(1000);
  2032. #endif
  2033. if(device_status.work_mode==1){
  2034. break;
  2035. }
  2036. if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
  2037. {
  2038. //震动
  2039. ctr_shake(ON, device_status.shake_sec);
  2040. //补光&&拍照
  2041. LOG_I("拍照!!!");
  2042. device_status.photo_ws = 1;
  2043. }
  2044. #if TG_MODULE == 1
  2045. while(device_status.photo_ws == 1)
  2046. {
  2047. rt_thread_delay(1000);
  2048. LOG_I("等待拍照完成...");
  2049. }
  2050. rt_thread_delay(1000);
  2051. while(device_status.clear_status==1)
  2052. {
  2053. rt_thread_delay(1000);
  2054. LOG_I("等待清扫完成...");
  2055. }
  2056. ctr_cangmen_down(OPEN);
  2057. //待下仓门完全打开
  2058. uint8_t i = 0;
  2059. while(1)
  2060. {
  2061. i++;
  2062. rt_thread_delay(100);
  2063. rt_kprintf("等待下仓门打开!!!\n");
  2064. if(i>100) {
  2065. ctr_cangmen_down(OFF);
  2066. break;
  2067. }
  2068. }
  2069. rt_thread_delay(2000);
  2070. ctr_shake(ON, device_status.shake_sec);
  2071. device_status.photo_ws = 1;
  2072. //关闭下仓门
  2073. device_status.ctr_down_motor = 2;
  2074. //待下仓门完全关闭
  2075. while(device_status.ctr_down_motor!=0)
  2076. {
  2077. rt_thread_delay(100);
  2078. rt_kprintf("等待下仓门关闭!!!\n");
  2079. }
  2080. device_status.ctr_up_motor = 1; //打开上仓门
  2081. #endif
  2082. }
  2083. }
  2084. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2085. {
  2086. LOG_I("-------------------not work--------------------");
  2087. device_status.work_status = 0;
  2088. }
  2089. //工作结束
  2090. if(device_status.work_done==0&&device_status.work_status==0)
  2091. {
  2092. DTU_Write("workdone");
  2093. ResetInit();
  2094. clt_time = 0;
  2095. }
  2096. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2097. }
  2098. #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
  2099. void slidelock()
  2100. {
  2101. ctr_Motor_Driver_Power(ON); //电机驱动供电
  2102. rt_thread_delay(10);
  2103. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2104. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2105. }
  2106. /* 入口函数-工作状态检测 */
  2107. static void Work_status_thread_entry(void *parameter)
  2108. {
  2109. static uint16_t sun_status_num = 120;
  2110. //如果是光控定时,进行光控监测的消抖
  2111. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2112. {
  2113. rt_kprintf("光控定时!!!\n");
  2114. while(sun_status_num!=0)
  2115. {
  2116. sun_status_num--;
  2117. rt_thread_delay(1000);
  2118. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2119. }
  2120. device_status.light_over_tim = device_status.sunBar * 3600;
  2121. device_status.test_flag = 0;
  2122. }
  2123. while(1)
  2124. {
  2125. WorkCheck();
  2126. }
  2127. }
  2128. //指令
  2129. //
  2130. /*入口函数-工作流程*/
  2131. static void Work_Loop_thread_entry(void *parameter)
  2132. {
  2133. rt_thread_delay(1000);
  2134. ResetInit();
  2135. device_status.light_over_tim = device_status.sunBar * 3600;
  2136. rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
  2137. static struct tm tm_tg_run;
  2138. static uint8_t alarm_h,alarm_m,alarm_s;
  2139. while(1)
  2140. {
  2141. if(device_status.work_mode==0)
  2142. {
  2143. WorkTask();
  2144. }else{
  2145. if(device_status.work_status==1){
  2146. ctr_lamp(ON, 1);
  2147. }else{
  2148. ctr_lamp(OFF, 1);
  2149. }
  2150. char buf[7] = {0};
  2151. /* ★ 原来用 "%-6s": 6 是"最小宽度"不是截断宽度, alarm_tg 是 char[12],
  2152. 超过 6 字符时会完整写出 12+1 字节到 7 字节的 buf -> 栈溢出 6 字节。
  2153. 必须写成 "%-6.6s"(精度 .6 才是截断)。 */
  2154. rt_snprintf(buf, sizeof(buf), "%-6.6s", device_status.alarm_tg);
  2155. // 截取 时: 前2位
  2156. alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0');
  2157. // 截取 分: 中间2位
  2158. alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0');
  2159. // 截取 秒: 后2位
  2160. alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0');
  2161. device_status.ctr_up_motor = 2; //关闭上仓门
  2162. device_status.ctr_down_motor = 2; //关闭下仓门
  2163. LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s);
  2164. //判断时间
  2165. time_t time_run_tg;
  2166. time(&time_run_tg);
  2167. tm_tg_run =*localtime(&time_run_tg);
  2168. LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec);
  2169. if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s))
  2170. {
  2171. LOG_W("预制执行 开始!");
  2172. dtu_write_json(makeJson_Warn("执行完成", "预制模式"));
  2173. rt_thread_delay(2000);
  2174. //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1}
  2175. cJSON *params = cJSON_CreateObject(); // params 子对象
  2176. cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
  2177. cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
  2178. /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串 -> 走 dtu_write_json */
  2179. dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
  2180. rt_thread_delay(20000);
  2181. }
  2182. }
  2183. rt_thread_delay(1000);
  2184. }
  2185. }
  2186. /*入口函数清扫*/
  2187. static void Control_Clear_thread_entry(void *parameter)
  2188. {
  2189. while(1)
  2190. {
  2191. if(device_status.update_flag==1) break;
  2192. if(device_status.clear_status==1)
  2193. {
  2194. if(device_status.version_type==41){
  2195. ctr_TG_Clear(300);
  2196. }else {
  2197. ctr_TG_Clear(400); //20260924-350修改为400
  2198. }
  2199. system_status.cleaning_count +=1; //清扫次数
  2200. }
  2201. if(device_status.clear_status==2)
  2202. {
  2203. turnzp();
  2204. device_status.clear_status = 0;
  2205. }
  2206. rt_thread_delay(100);
  2207. }
  2208. }
  2209. /*入口函数,自检*/
  2210. static void Control_Check_thread_entry(void *parameter)
  2211. {
  2212. static int total_seconds = 200;
  2213. static int current_second = 0;
  2214. static int process = 0;
  2215. device_status.C01 = 0;
  2216. rt_thread_delay(10000);
  2217. while(1)
  2218. {
  2219. if(device_status.update_flag==1) break;
  2220. if(device_status.work_status==1&&device_status.heat_sw==1)
  2221. {
  2222. calorstat(device_status.heattime*60);
  2223. rt_thread_delay(10000);
  2224. }
  2225. else
  2226. {
  2227. if(device_status.selfcheck_ws==2)
  2228. {
  2229. while(current_second <= total_seconds)
  2230. {
  2231. process = (current_second * 100) / total_seconds;
  2232. device_status.process = process / 100.0;
  2233. LOG_I("process:%d",process);
  2234. HMI_WRITE("selfcheck.s106.val=%d",process);
  2235. rt_thread_delay(1000);
  2236. current_second++;
  2237. if(device_status.selfcheck_ws==0)
  2238. {
  2239. HMI_WRITE("selfcheck.s106.val=100");
  2240. break;
  2241. }
  2242. }
  2243. HMI_WRITE("selfcheck.s106.val=100");
  2244. }else {
  2245. process = 0;
  2246. current_second = 0;
  2247. #if 0
  2248. if(device_status.rain_status==1)
  2249. {
  2250. LOG_E("雨控!");
  2251. rt_thread_delay(10000);
  2252. }
  2253. //根据故障码 亮指示灯
  2254. //灯管故障
  2255. if(device_status.C01 & (1 << 0))
  2256. {
  2257. LOG_E("灯管故障!");
  2258. //发送红灯常亮信号
  2259. write_single_holding_register(0x01,0x00C2,0x0011,1000);
  2260. //延时10S
  2261. rt_thread_delay(10000);
  2262. }
  2263. //加热故障
  2264. if(device_status.C01 & (1 << 1))
  2265. {
  2266. LOG_E("加热故障!");
  2267. //发送红灯慢闪信号
  2268. write_single_holding_register(0x01,0x00C2,0x0021,1000);
  2269. //延时10S
  2270. rt_thread_delay(10000);
  2271. }
  2272. //相机拍照故障
  2273. if(device_status.C01 & (1 << 2))
  2274. {
  2275. LOG_E("拍照故障!");
  2276. //发送红灯快闪信号
  2277. write_single_holding_register(0x01,0x00C2,0x0031,1000);
  2278. //延时10S
  2279. rt_thread_delay(10000);
  2280. }
  2281. //补光灯故障
  2282. if(device_status.C01 & (1 << 3))
  2283. {
  2284. LOG_E("补光灯故障!");
  2285. //发送绿灯慢闪信号
  2286. write_single_holding_register(0x01,0x00C2,0x0023,1000);
  2287. //延时10S
  2288. rt_thread_delay(10000);
  2289. }
  2290. //上仓门故障
  2291. if(device_status.C01 & (1 << 8))
  2292. {
  2293. LOG_E("上仓门故障!");
  2294. //发送黄灯常亮信号
  2295. write_single_holding_register(0x01,0x00C2,0x0012,1000);
  2296. //延时10S
  2297. rt_thread_delay(10000);
  2298. }
  2299. //下仓门故障
  2300. if(device_status.C01 & (1 << 9))
  2301. {
  2302. LOG_E("下仓门故障!");
  2303. //发送黄灯慢闪信号
  2304. write_single_holding_register(0x01,0x00C2,0x0022,1000);
  2305. //延时10S
  2306. rt_thread_delay(10000);
  2307. }
  2308. //清扫故障
  2309. if(device_status.C01 & (1 << 10))
  2310. {
  2311. LOG_E("清扫故障!");
  2312. //发送黄灯爆闪信号
  2313. write_single_holding_register(0x01,0x00C2,0x0032,1000);
  2314. //延时10S
  2315. rt_thread_delay(10000);
  2316. }
  2317. //通信故障
  2318. if(device_status.C01 & (1 << 13))
  2319. {
  2320. LOG_E("通信故障!");
  2321. //发送绿灯爆闪信号
  2322. write_single_holding_register(0x01,0x00C2,0x0033,1000);
  2323. //延时10S
  2324. rt_thread_delay(10000);
  2325. }
  2326. if(device_status.C01==0)
  2327. {
  2328. LOG_W("无故障!");
  2329. //发送绿灯常亮信号
  2330. //write_single_holding_register(0x01,0x00C2,0x0013,1000);
  2331. //延时10S
  2332. rt_thread_delay(10000);
  2333. }
  2334. #endif
  2335. }
  2336. }
  2337. rt_thread_delay(1000);
  2338. }
  2339. }
  2340. static void thread_485_play_entry(void *param)
  2341. {
  2342. static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
  2343. rt_uint32_t e;
  2344. // 创建事件控制块
  2345. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  2346. if (event_485_play == RT_NULL)
  2347. {
  2348. LOG_E("Event create failed!");
  2349. return;
  2350. }
  2351. while(1)
  2352. {
  2353. if(device_status.update_flag==1) break;
  2354. //等待事件
  2355. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  2356. {
  2357. LOG_I("Interrupt occurred and processed.");
  2358. _485_play(sound105, 4);
  2359. //发送串口消息,通知JETSON计算卡进行拍照
  2360. DTU_Write("{\"guangshan\":\"on\"}");
  2361. }
  2362. }
  2363. }
  2364. /*清扫线程*/
  2365. int Control_clear(void)
  2366. {
  2367. /* 创建线程*/
  2368. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2369. Control_Clear_thread_entry, RT_NULL,
  2370. 2048, //2048
  2371. 24, //23
  2372. WORK_CHECK_THREAD_TIMESLICE); //5
  2373. if(tid_ctr!=RT_NULL)
  2374. {
  2375. rt_thread_startup(tid_ctr);
  2376. }
  2377. return 0;
  2378. }
  2379. /*恒温加热线程*/
  2380. int Control_Check(void)
  2381. {
  2382. /* 创建线程*/
  2383. tid_heat = rt_thread_create("Control_Check_thread_entry",
  2384. Control_Check_thread_entry, RT_NULL,
  2385. 1024, //2048
  2386. 25, //23
  2387. WORK_CHECK_THREAD_TIMESLICE); //5
  2388. if(tid_heat!=RT_NULL)
  2389. {
  2390. rt_thread_startup(tid_heat);
  2391. }
  2392. return 0;
  2393. }
  2394. int thread_485_play(void)
  2395. {
  2396. tid_485_play = rt_thread_create("thread_485play",
  2397. thread_485_play_entry, RT_NULL,
  2398. 1024, //1024
  2399. 26, //23
  2400. 5); //5
  2401. if(tid_485_play!=RT_NULL)
  2402. {
  2403. rt_thread_startup(tid_485_play);
  2404. }
  2405. else {
  2406. LOG_E("thread_485_play start failed!");
  2407. }
  2408. return 0;
  2409. }
  2410. /* 创建线程 -工作状态检测*/
  2411. int Workstatus_check(void)
  2412. {
  2413. /* 创建线程*/
  2414. tid_wkstc = rt_thread_create("work_status_check_thread",
  2415. Work_status_thread_entry, RT_NULL,
  2416. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2417. WORK_CHECK_THREAD_PRIORITY, //23
  2418. WORK_CHECK_THREAD_TIMESLICE); //5
  2419. if(tid_wkstc!=RT_NULL)
  2420. {
  2421. rt_thread_startup(tid_wkstc);
  2422. }
  2423. return 0;
  2424. }
  2425. /*工作流程线程*/
  2426. int Work_Loop_Task(void)
  2427. {
  2428. rt_err_t result_loop = 0;
  2429. /* 创建线程*/
  2430. tid_workloop = rt_thread_create("work_loop_thread",
  2431. Work_Loop_thread_entry, RT_NULL,
  2432. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2433. WORK_LOOP_THREAD_PRIORITY, //12
  2434. WORK_LOOP_THREAD_TIMESLICE); //5
  2435. if(tid_workloop!=RT_NULL)
  2436. {
  2437. result_loop = rt_thread_startup(tid_workloop);
  2438. }
  2439. return result_loop;
  2440. }
  2441. #ifdef RT_USING_ALARM
  2442. void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp)
  2443. {
  2444. LOG_W("alarm_callback_router_on.\n");
  2445. ctr_router(ON);
  2446. }
  2447. void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp)
  2448. {
  2449. LOG_W("alarm_callback_router_off.\n");
  2450. ctr_router(OFF);
  2451. }
  2452. void alarm_app_router(void)
  2453. {
  2454. struct rt_alarm_setup setup;
  2455. struct rt_alarm * alarm_router_on = RT_NULL;
  2456. if (alarm_router_on != RT_NULL)
  2457. return;
  2458. setup.flag = RT_ALARM_DAILY;
  2459. setup.wktime.tm_hour = 23;
  2460. setup.wktime.tm_min = 0;
  2461. setup.wktime.tm_sec = 0;
  2462. alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup);
  2463. if(RT_NULL != alarm_router_on)
  2464. {
  2465. LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec);
  2466. rt_alarm_start(alarm_router_on);
  2467. }
  2468. }
  2469. #endif
  2470. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2471. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  2472. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2473. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2474. MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
  2475. //INIT_APP_EXPORT(Control_heat);