control.c 76 KB

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