control.c 62 KB

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