control.c 64 KB

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