control.c 64 KB

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