control.c 77 KB

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