control.c 68 KB

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