control.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703
  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. */
  11. #include "beep.h"
  12. #include "hmi.h"
  13. #include "easyflash.h"
  14. #include "stdio.h"
  15. #include "sound.h"
  16. #include "dtu_uart2.h"
  17. #include "sht30_collect.h"
  18. #define DBG_ENABLE
  19. #define DBG_COLOR
  20. #define DBG_TAG "control"
  21. #define DBG_LEVEL DBG_LOG
  22. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  23. //工作检测
  24. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  25. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  26. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  27. //工作流程
  28. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  29. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  30. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  31. #define PWM_EN 1 //0关闭,1-打开
  32. //设备状态表结构体
  33. Device_Status_typedef device_status;
  34. rt_thread_t tid_workloop = RT_NULL;
  35. rt_thread_t tid_wkstc = RT_NULL;
  36. rt_thread_t tid_ctr = RT_NULL;
  37. rt_thread_t tid_heat = RT_NULL;
  38. /**************************1.0定制2000W*************************************/
  39. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  40. {
  41. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  42. if(status_ws==ON)
  43. {
  44. rt_pin_write(FAN_P,ON);
  45. }
  46. else {
  47. rt_pin_write(FAN_P, OFF);
  48. }
  49. return 0;
  50. }
  51. uint8_t ctr_Fan_IN(uint8_t status_ws)
  52. {
  53. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  54. if(status_ws==ON)
  55. {
  56. rt_pin_write(FAN_I,ON);
  57. }
  58. else {
  59. rt_pin_write(FAN_I, OFF);
  60. }
  61. return 0;
  62. }
  63. /****************************定制色诱*************************************/
  64. /**/
  65. #if Color_EN == 1
  66. uint8_t Read_Color_Key(rt_base_t key_pin)
  67. {
  68. rt_uint32_t sta_s = 0;
  69. rt_pin_mode(key_pin, PIN_MODE_INPUT);
  70. while(rt_pin_read(key_pin)==1)
  71. {
  72. sta_s++;
  73. /*持续的*/
  74. rt_thread_delay(50);
  75. }
  76. while(rt_pin_read(key_pin)==0)
  77. {
  78. rt_thread_delay(50);
  79. if(sta_s>0) break;
  80. }
  81. LOG_W("检测到IO0翻转!!!");
  82. return 1;
  83. }
  84. /*读限位开关状态*/
  85. uint8_t Read_Color_Limit(rt_base_t pin_limit)
  86. {
  87. uint8_t l_sta = 0;
  88. rt_pin_mode(pin_limit, PIN_MODE_INPUT);
  89. l_sta = rt_pin_read(pin_limit);
  90. rt_thread_delay(20);
  91. l_sta = rt_pin_read(pin_limit);
  92. return l_sta;
  93. }
  94. /*色诱板*/
  95. int ctr_Color_board(uint8_t forward,uint16_t time_board)
  96. {
  97. if(forward==CLOSE) //归位
  98. {
  99. if(device_status.cul_time>0)
  100. device_status.cul_time--;
  101. ctr_cangmen_down(OFF);
  102. ctr_cangmen_up(OFF);
  103. rt_thread_delay(1000);
  104. // if(device_status.cul_time>7) device_status.cul_time=7;
  105. // if(device_status.cul_time!=7)
  106. // {
  107. // uint8_t ct = device_status.cul_time;
  108. // while(1)
  109. // {
  110. // Beep_Tip(100);
  111. // rt_thread_delay(100);
  112. // ct--;
  113. // if(ct==0) break;
  114. // }
  115. // //return device_status.cul_time;
  116. // }
  117. if(Read_Color_Limit(LIMIT_RESET)==0)
  118. {
  119. device_status.cul_time = 0;
  120. LOG_W("已在初始位置!");
  121. Beep_Tip(1000);
  122. return 0;
  123. //黄板已经在复位状态,直接返回0;
  124. }
  125. ctr_cangmen_up(CLOSE);
  126. rt_thread_delay(20000);
  127. ctr_cangmen_up(OFF);
  128. // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
  129. // {
  130. // LOG_W("第七天!!!");
  131. // ctr_cangmen_down(CLOSE);
  132. // rt_thread_delay(500);
  133. // }
  134. LOG_W("先打开运行到限位的右侧!");
  135. while(Read_Color_Limit(LIMIT_BOARD)==0)
  136. {
  137. LOG_W("归位:到达限位中间");
  138. ctr_cangmen_down(CLOSE);
  139. if(Read_Color_Limit(LIMIT_RESET)==0)
  140. {
  141. ctr_cangmen_down(OFF);
  142. break;
  143. }
  144. }
  145. //先打开运行到限位的右侧
  146. while(Read_Color_Limit(LIMIT_BOARD)==1)
  147. {
  148. LOG_W("归位:运行!");
  149. ctr_cangmen_down(CLOSE);
  150. if(Read_Color_Limit(LIMIT_RESET)==0)
  151. {
  152. ctr_cangmen_down(OFF);
  153. break;
  154. }
  155. }
  156. while(Read_Color_Limit(LIMIT_BOARD)==0) //到达限位中间 继续运行
  157. {
  158. LOG_W("归位:到达限位中间");
  159. ctr_cangmen_down(CLOSE);
  160. if(Read_Color_Limit(LIMIT_RESET)==0)
  161. {
  162. ctr_cangmen_down(OFF);
  163. break;
  164. }
  165. }
  166. if(Read_Color_Limit(LIMIT_RESET)==0) return 0;
  167. LOG_W("到达限位左侧!");
  168. //停止电机
  169. ctr_cangmen_down(OFF);
  170. rt_thread_delay(2000);
  171. //将推杆电机归位
  172. LOG_D("*推杆电机归位*");
  173. /*推杆电机伸出*/
  174. ctr_cangmen_up(OPEN);
  175. rt_thread_delay(20000);
  176. ctr_cangmen_up(OFF);
  177. return 2;
  178. }
  179. else if(forward==OPEN)
  180. {
  181. ctr_cangmen_down(OFF);
  182. ctr_cangmen_up(OFF);
  183. rt_thread_delay(1000);
  184. if(device_status.cul_time==7)
  185. {
  186. Beep_Tip(5000);
  187. return 7;
  188. }
  189. LOG_W("打开推杆电机!");
  190. //2023-04-18:add
  191. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  192. {
  193. LOG_D("*推杆电机归位*");
  194. /*推杆电机归位*/
  195. ctr_cangmen_up(CLOSE);
  196. rt_thread_delay(20000);
  197. ctr_cangmen_up(OFF);
  198. }
  199. if(device_status.cul_time<7)
  200. device_status.cul_time++;
  201. while(Read_Color_Limit(LIMIT_BOARD)==0)
  202. {
  203. ctr_cangmen_down(forward);
  204. rt_thread_delay(50);
  205. }
  206. while(Read_Color_Limit(LIMIT_BOARD)==1)
  207. {
  208. time_board--;
  209. if(time_board==0) break;
  210. ctr_cangmen_down(forward);
  211. rt_thread_delay(50);
  212. }
  213. ctr_cangmen_down(OFF);
  214. rt_thread_delay(1000);
  215. ctr_cangmen_up(OPEN);
  216. rt_thread_delay(20000);
  217. ctr_cangmen_up(OFF);
  218. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  219. return OPEN;
  220. }
  221. else if (forward==RESET) {
  222. ctr_cangmen_up(OFF);
  223. ctr_cangmen_down(OFF);
  224. if(Read_Color_Limit(LIMIT_RESET)==0)
  225. {
  226. device_status.cul_time = 0;
  227. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  228. LOG_W("已在初始位置!");
  229. Beep_Tip(1000);
  230. ctr_cangmen_up(CLOSE);
  231. rt_thread_delay(20000);
  232. ctr_cangmen_up(OFF);
  233. device_status.cul_time = 0;
  234. return 0;
  235. //黄板已经在复位状态,直接返回0;
  236. }
  237. //if(Read_Color_Limit(LIMIT_BOARD)==0)
  238. {
  239. LOG_D("*推杆电机归位*");
  240. /*推杆电机归位*/
  241. ctr_cangmen_up(CLOSE);
  242. rt_thread_delay(20000);
  243. ctr_cangmen_up(OFF);
  244. }
  245. while(Read_Color_Limit(LIMIT_RESET)==1)
  246. {
  247. time_board--;
  248. if(time_board==0) break;
  249. ctr_cangmen_down(CLOSE);
  250. rt_thread_delay(20);
  251. }
  252. device_status.cul_time = 0;
  253. ctr_cangmen_down(OFF);
  254. }
  255. // device_status.cul_time = 0;
  256. //ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  257. return RESET; //复位返回0
  258. }
  259. #endif
  260. /****************************5.0***********************************/
  261. void ctr_turnv5(void)
  262. {
  263. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  264. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  265. // 转出
  266. rt_pin_write(CLEAR_R,ON);
  267. rt_pin_write(CLEAR_L,OFF);
  268. rt_thread_mdelay(300);
  269. //停止
  270. rt_pin_write(CLEAR_R,OFF);
  271. rt_pin_write(CLEAR_L,OFF);
  272. rt_thread_mdelay(300);
  273. //转入
  274. rt_pin_write(CLEAR_R,OFF);
  275. rt_pin_write(CLEAR_L,ON);
  276. rt_thread_mdelay(300);
  277. //停止
  278. rt_pin_write(CLEAR_R,OFF);
  279. rt_pin_write(CLEAR_L,OFF);
  280. rt_thread_mdelay(300);
  281. }
  282. /**************************机器人********************************/
  283. void ctr_Motor_Robot(uint8_t status_ws)
  284. {
  285. unsigned int close_timeout = 0;
  286. unsigned int open_timeout = 0;
  287. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  288. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  289. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  290. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  291. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  292. rt_pin_write(Motor_Power,OFF);
  293. rt_pin_write(Motor_Direction_Ctr,ON);
  294. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  295. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  296. if(status_ws == ON)
  297. {
  298. //检测按键状态
  299. while(rt_pin_read(Motor_Direction_Button)==1)
  300. {
  301. if(rt_pin_read(LIMIT_CLOSE)==1)
  302. {
  303. rt_pin_write(Motor_Direction_Ctr,ON);
  304. close_timeout++;
  305. //打开电机驱动器电源,默认关闭
  306. rt_pin_write(Motor_Power,ON);
  307. if(close_timeout==2)
  308. {
  309. rt_pin_write(CLEAR_R,ON);
  310. rt_pin_write(CLEAR_L,OFF);
  311. rt_pin_write(Motor_Power,OFF);
  312. rt_thread_delay(20);
  313. rt_pin_write(Motor_Power,ON);
  314. }
  315. rt_thread_delay(20);
  316. if(close_timeout>500)
  317. {
  318. close_timeout = 600;
  319. rt_pin_write(Motor_Power,OFF);
  320. rt_pin_write(CLEAR_R,OFF);
  321. rt_pin_write(CLEAR_L,OFF);
  322. }
  323. }
  324. else
  325. {
  326. close_timeout = 0;
  327. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  328. rt_pin_write(CLEAR_R,OFF);
  329. rt_pin_write(CLEAR_L,OFF);
  330. }
  331. rt_thread_delay(20);
  332. }
  333. rt_pin_write(Motor_Power,OFF);
  334. rt_thread_delay(1000*2);
  335. while(rt_pin_read(Motor_Direction_Button)==0)
  336. {
  337. if(rt_pin_read(LIMIT_OPEN)==1)
  338. {
  339. open_timeout ++;
  340. //打开电机驱动器电源,默认关闭
  341. if(open_timeout==2){
  342. rt_pin_write(CLEAR_R,OFF);
  343. rt_pin_write(CLEAR_L,ON);
  344. }
  345. rt_pin_write(Motor_Power,ON);
  346. rt_pin_write(Motor_Direction_Ctr,OFF);
  347. if(open_timeout>500)
  348. {
  349. open_timeout = 600;
  350. rt_pin_write(Motor_Power,OFF);
  351. rt_pin_write(CLEAR_R,OFF);
  352. rt_pin_write(CLEAR_L,OFF);
  353. }
  354. rt_thread_delay(20);
  355. }
  356. if(rt_pin_read(LIMIT_OPEN)==0)
  357. {
  358. open_timeout = 0;
  359. rt_pin_write(Motor_Power,OFF);
  360. rt_pin_write(CLEAR_R,OFF);
  361. rt_pin_write(CLEAR_L,OFF);
  362. }
  363. rt_thread_delay(20);
  364. close_timeout = 0;
  365. }
  366. open_timeout = 0;
  367. close_timeout = 0;
  368. }
  369. else {
  370. rt_pin_write(Motor_Power,OFF);
  371. }
  372. }
  373. /*************************吸虫塔*********************************/
  374. //吸虫电机
  375. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  376. {
  377. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  378. if(status_ws==ON)
  379. {
  380. rt_pin_write(MOTOR1_CTR_PIN,ON);
  381. }
  382. else {
  383. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  384. }
  385. return 0;
  386. }
  387. uint8_t ctr_Camera_Power(uint8_t status_ws)
  388. {
  389. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  390. if(status_ws==ON)
  391. {
  392. rt_pin_write(MOTOR1_CTR_PIN,ON);
  393. }
  394. else {
  395. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  396. }
  397. return 0;
  398. }
  399. //吸附电机
  400. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  401. {
  402. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  403. if(status_ws==ON)
  404. {
  405. rt_pin_write(MOTOR2_CTR_PIN,ON);
  406. }
  407. else {
  408. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  409. }
  410. return 0;
  411. }
  412. //清扫-卷带电机
  413. void ctr_JD_Clear(uint8_t shake_sw)
  414. {
  415. rt_uint16_t turn_timeout = 0;
  416. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  417. ctr_cangmen_up(OFF); //清扫传送带的电机
  418. if(shake_sw==ON)
  419. {
  420. ctr_Heat(ON); //清洁风机
  421. Motor_shake(ON, 0);
  422. ctr_cangmen_up(OPEN); //打开清扫
  423. }
  424. else {
  425. Motor_shake(OFF, 0);
  426. ctr_cangmen_up(OPEN); //打开清扫
  427. }
  428. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  429. {
  430. Motor_Juandai(ON);
  431. rt_thread_delay(50);
  432. turn_timeout++;
  433. if(turn_timeout==1000) break;
  434. }
  435. turn_timeout = 0;
  436. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  437. {
  438. Motor_Juandai(ON);
  439. turn_timeout++;
  440. rt_thread_delay(50);
  441. if(turn_timeout==1000) break;
  442. }
  443. Motor_shake(OFF, 1);
  444. Motor_Juandai(OFF);
  445. ctr_cangmen_up(OFF); //关闭清扫
  446. ctr_Heat(OFF);
  447. }
  448. //工作任务流程
  449. void WorkTask_XCT(void)
  450. {
  451. while(device_status.work_status==1&&device_status.ds==1)
  452. {
  453. ctr_Motor_xifu(ON);
  454. //打开相机供电
  455. ctr_Camera_Power(ON);
  456. //ctr_Heat(ON); //清洁风机
  457. uint32_t photo_fre = 0;
  458. photo_fre = device_status.dattim * 60;
  459. while(photo_fre!=0)
  460. {
  461. photo_fre --;
  462. rt_thread_delay(1000);
  463. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  464. }
  465. //卷带
  466. ctr_JD_Clear(OFF);
  467. //拍照
  468. HK_TakePhoto();
  469. //关闭吸附风机
  470. ctr_Motor_xifu(OFF);
  471. //关闭相机供电
  472. //ctr_Heat(OFF);
  473. ctr_Camera_Power(OFF);
  474. //卷带
  475. ctr_JD_Clear(ON);
  476. // rt_thread_delay(1000);
  477. // ctr_JD_Clear(ON);
  478. //震动
  479. Motor_shake(ON, device_status.shake_sec);
  480. }
  481. //关闭风机
  482. ctr_Motor_xichong(OFF);
  483. ctr_Motor_xifu(OFF);
  484. LOG_I("不在工作时间,未工作!!!");
  485. }
  486. /**********************************************************************/
  487. /*光控*/
  488. uint8_t In_Light_status(void)
  489. {
  490. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  491. return rt_pin_read(LIGHT_PIN);
  492. }
  493. /*雨控*/
  494. uint8_t In_Rain_status(void)
  495. {
  496. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  497. return rt_pin_read(RAIN_PIN);
  498. }
  499. /*加热*/
  500. uint8_t ctr_Heat(uint8_t status_heat)
  501. {
  502. device_status.heat_status = status_heat;
  503. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  504. rt_pin_write(Heat_Pin,status_heat);
  505. return status_heat;
  506. }
  507. /*制冷*/
  508. uint8_t ctr_Cold(uint8_t status_cold)
  509. {
  510. device_status.cold_status = status_cold;
  511. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  512. rt_pin_write(Cold_Pin,status_cold);
  513. return status_cold;
  514. }
  515. /*相机供电*/
  516. void ctr_photo(uint8_t status_photo)
  517. {
  518. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  519. rt_pin_write(PHOTO,status_photo);
  520. }
  521. /*************************************************
  522. Function: ctr_Cool_Fan
  523. Description: 散热风机的开关
  524. Calls:
  525. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  526. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  527. Input: ON-开,OFF-关
  528. Output: NULL
  529. Return: ON-开,OFF-关
  530. Others: NULL
  531. *************************************************/
  532. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  533. {
  534. device_status.cool_fan_status = status_cool;
  535. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  536. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  537. if(status_cool==OFF)
  538. {
  539. rt_pin_write(Cool_Fan_L,OFF);
  540. rt_pin_write(Cool_Fan_R,OFF);
  541. }
  542. if(status_cool==ON)
  543. {
  544. rt_pin_write(Cool_Fan_L,ON);
  545. rt_pin_write(Cool_Fan_R,OFF);
  546. }
  547. return device_status.cool_fan_status;
  548. }
  549. /*************************************************
  550. Function: ctr_Collect_Fan
  551. Description: 采集风机的开关
  552. Calls:
  553. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  554. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  555. Input: ON-开,OFF-关
  556. Output: NULL
  557. Return: ON-开,OFF-关
  558. Others: NULL
  559. *************************************************/
  560. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  561. {
  562. device_status.collect_fan_status = status_collect;
  563. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  564. if(status_collect==OFF)
  565. {
  566. rt_pin_write(Collect_Fan,OFF);
  567. }
  568. if(status_collect==ON)
  569. {
  570. rt_pin_write(Collect_Fan,ON);
  571. }
  572. return device_status.collect_fan_status;
  573. }
  574. /*************************************************
  575. Function: ctr_develop
  576. Description: 培养载玻片孢子
  577. Calls:
  578. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  579. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  580. Input: v=0(复位) v=90转90度
  581. Output: NULL
  582. Return: NULL
  583. Others: NULL
  584. *************************************************/
  585. void ctr_develop(void)
  586. {
  587. rt_kprintf("===============开始培养=================:%d\n",device_status.cul_time);
  588. //已经培养的时间-分钟
  589. device_status.stay_time = 0;
  590. rt_uint32_t ii = 0;
  591. while(device_status.develop_flag==1)
  592. {
  593. float stayedtime = 0.01;
  594. char stayed[5];
  595. stayedtime = (float)device_status.stay_time / 60.0;
  596. sprintf(stayed,"%0.02f",stayedtime);
  597. HMI_WRITE("workSta.staytime.txt=\"%s\"",stayed);
  598. ii++;
  599. LOG_I("developing...................................%d\n",ii);
  600. LOG_I("============================设定的培养温度:%d\n",device_status.heatvalue);
  601. LOG_I("============================培养仓实时温度:%d\n",device_status.pre_temp);
  602. if(device_status.pre_temp<(device_status.heatvalue-5))
  603. {
  604. //未制冷
  605. if(device_status.cold_status==0)
  606. {
  607. ctr_Heat(ON);
  608. }
  609. }
  610. else
  611. {
  612. ctr_Heat(OFF);
  613. }
  614. LOG_I("============================制冷开关:%d\n",device_status.cold_sw);
  615. if(device_status.cold_sw==1)
  616. {
  617. if(device_status.pre_temp>(device_status.heatvalue-10))
  618. {
  619. if(device_status.heat_status==0)
  620. {
  621. ctr_Cold(ON);
  622. }
  623. }
  624. else
  625. {
  626. ctr_Cold(OFF);
  627. }
  628. }
  629. if(ii%60==0)
  630. {
  631. device_status.stay_time++;
  632. if(device_status.stay_time / 60 == device_status.cul_time) device_status.develop_flag = 0;
  633. LOG_I("developed...................................%d分钟\n",device_status.stay_time);
  634. }
  635. if(device_status.box_temp>35)
  636. {
  637. ctr_Cool_Fan(ON);
  638. }
  639. else if (device_status.box_temp<30)
  640. {
  641. ctr_Cool_Fan(OFF);
  642. }
  643. rt_thread_delay(1000);
  644. }
  645. ctr_Cold(OFF);
  646. ctr_Heat(OFF);
  647. LOG_I("=========!!!培养完成!!!==========");
  648. }
  649. /*上仓门*/
  650. uint8_t ctr_cangmen_up(uint8_t status_up)
  651. {
  652. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  653. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  654. if(status_up==OFF)
  655. {
  656. rt_pin_write(Collect_fan_R,ON);
  657. rt_pin_write(Collect_fan_L,ON);
  658. device_status.ctr_up_motor = 0;
  659. rt_thread_delay(1000);
  660. }
  661. if(status_up==OPEN)
  662. {
  663. device_status.up_status = 1;
  664. rt_pin_write(Collect_fan_R,OFF);
  665. rt_pin_write(Collect_fan_L,OFF);
  666. rt_pin_write(Collect_fan_R,ON);
  667. rt_pin_write(Collect_fan_L,OFF);
  668. }
  669. if(status_up==CLOSE)
  670. {
  671. device_status.up_status = 0;
  672. rt_pin_write(Collect_fan_R,OFF);
  673. rt_pin_write(Collect_fan_L,OFF);
  674. rt_pin_write(Collect_fan_R,OFF);
  675. rt_pin_write(Collect_fan_L,ON);
  676. }
  677. return device_status.up_status;
  678. }
  679. //下仓门
  680. uint8_t ctr_cangmen_down(uint8_t status_down)
  681. {
  682. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  683. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  684. if(status_down==OFF)
  685. {
  686. rt_pin_write(C_DOWN_R,ON);
  687. rt_pin_write(C_DOWN_L,ON);
  688. device_status.ctr_down_motor = 0;
  689. rt_thread_delay(1000);
  690. }
  691. if(status_down==OPEN)
  692. {
  693. device_status.down_status = 1;
  694. rt_pin_write(C_DOWN_R,OFF);
  695. rt_pin_write(C_DOWN_L,OFF);
  696. rt_pin_write(C_DOWN_R,ON);
  697. rt_pin_write(C_DOWN_L,OFF);
  698. }
  699. if(status_down==CLOSE)
  700. {
  701. device_status.down_status = 0;
  702. rt_pin_write(C_DOWN_R,OFF);
  703. rt_pin_write(C_DOWN_L,OFF);
  704. rt_pin_write(C_DOWN_R,OFF);
  705. rt_pin_write(C_DOWN_L,ON);
  706. }
  707. return device_status.down_status;
  708. }
  709. //镇流器
  710. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  711. {
  712. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  713. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  714. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  715. if(status_lamp==ON)
  716. {
  717. switch(lamp_num){
  718. case 1:
  719. rt_pin_write(LAMP_SW,ON);
  720. device_status.lamp_status = status_lamp;
  721. break;
  722. case 2:
  723. rt_pin_write(LAMP_1_SW,ON);
  724. device_status.lamp_status = status_lamp;
  725. break;
  726. case 3:
  727. rt_pin_write(LAMP_2_SW,ON);
  728. device_status.lamp_status = status_lamp;
  729. break;
  730. default:
  731. break;
  732. }
  733. }
  734. else {
  735. switch(lamp_num){
  736. case 1:
  737. rt_pin_write(LAMP_SW,OFF);
  738. device_status.lamp_status = status_lamp;
  739. break;
  740. case 2:
  741. rt_pin_write(LAMP_1_SW,OFF);
  742. device_status.lamp1_status = status_lamp;
  743. break;
  744. case 3:
  745. rt_pin_write(LAMP_2_SW,OFF);
  746. device_status.lamp2_status = status_lamp;
  747. break;
  748. default:
  749. break;
  750. }
  751. }
  752. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  753. return device_status.lamp_status;
  754. }
  755. //震动电机
  756. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  757. {
  758. static rt_uint16_t shake_i = 0;
  759. shake_i = atoi(device_status.power);
  760. LOG_I("震动电机待机电流:%d",shake_i);
  761. if(device_status.dtype==7&&shake_sec!=0)
  762. DTU_Write("drop");
  763. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  764. if(status_shake==ON&&device_status.shake_ws==1)
  765. {
  766. rt_pin_write(SHAKE_SW,ON);
  767. if(device_status.dtype==3)
  768. {
  769. while(shake_sec--)
  770. {
  771. rt_thread_delay(100);
  772. if(shake_sec==20&&device_status.clear_status==0&&device_status.work_status==1&&atoi(device_status.power)-shake_i<100&&shake_i!=0&&shake_i<5000)
  773. {
  774. char * shake_warn = makeJson_Warn("电流不足", "震动电机");
  775. DTU_Write("%s",shake_warn);
  776. rt_free(shake_warn);
  777. }
  778. LOG_D("震动电机:%d",atoi(device_status.power));
  779. }
  780. }
  781. if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
  782. rt_pin_write(SHAKE_SW,OFF);
  783. HMI_WRITE("cangWei.switchShake.val=%d",0);
  784. }
  785. else
  786. {
  787. rt_pin_write(SHAKE_SW,OFF);
  788. HMI_WRITE("cangWei.switchShake.val=%d",0);
  789. }
  790. if(device_status.dtype==7)
  791. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  792. return status_shake;
  793. }
  794. //补光
  795. uint8_t ctr_led(uint8_t status_led)
  796. {
  797. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  798. if(device_status.version_type==6)
  799. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  800. if(status_led==ON)
  801. {
  802. rt_pin_write(LED_SW,ON);
  803. if(device_status.version_type==6)
  804. rt_pin_write(LED_P,ON);
  805. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  806. }
  807. else if(status_led==OFF){
  808. rt_pin_write(LED_SW,OFF);
  809. if(device_status.version_type==6)
  810. rt_pin_write(LED_P,OFF);
  811. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  812. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  813. }
  814. return status_led;
  815. }
  816. /*************************************************
  817. Function: ctr_turn
  818. Description: 转盘驱动
  819. Calls:
  820. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  821. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  822. Input: v=0(复位) v=90转90度
  823. Output: NULL
  824. Return: NULL
  825. Others: NULL
  826. *************************************************/
  827. void ctr_turn(uint8_t v,uint8_t sta)
  828. {
  829. DTU_Write("turn.mp3");
  830. LOG_I("================v===============:%d",v);
  831. rt_uint16_t turn_cnt = 0;
  832. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  833. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  834. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  835. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  836. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  837. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  838. {
  839. rt_pin_write(TURN_ZP_R, ON);
  840. rt_pin_write(TURN_ZP_L, OFF);
  841. //Motor_shake(ON,0);
  842. turn_cnt ++;
  843. rt_thread_delay(100);
  844. if(turn_cnt==1000)
  845. {
  846. turn_cnt = 0;
  847. rt_pin_write(TURN_ZP_R, OFF);
  848. rt_pin_write(TURN_ZP_L, OFF);
  849. //Motor_shake(OFF,0);
  850. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  851. DTU_Write("%s",turn_warn);
  852. rt_free(turn_warn);
  853. break;
  854. }
  855. }
  856. turn_cnt = 0;
  857. while(v==0&&rt_pin_read(LIMIT_0)==1)
  858. {
  859. rt_pin_write(TURN_ZP_R, ON);
  860. rt_pin_write(TURN_ZP_L, OFF);
  861. Motor_shake(ON,0);
  862. rt_thread_delay(200); //10 to 100
  863. turn_cnt ++;
  864. if(turn_cnt==300) //6000 to 600
  865. {
  866. turn_cnt = 0;
  867. // rt_pin_write(TURN_ZP_R, OFF);
  868. // rt_pin_write(TURN_ZP_L, OFF);
  869. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  870. // DTU_Write("%s",turn_warn);
  871. // rt_free(turn_warn);
  872. break;
  873. }
  874. }
  875. while(v==0&&rt_pin_read(LIMIT_0)==1)
  876. {
  877. rt_pin_write(TURN_ZP_R, ON);
  878. rt_pin_write(TURN_ZP_L, OFF);
  879. //Motor_shake(ON,0);
  880. rt_thread_delay(50); //10 to 50
  881. turn_cnt ++;
  882. if(turn_cnt==10) //6000 to 1200
  883. {
  884. turn_cnt = 0;
  885. rt_pin_write(TURN_ZP_R, OFF);
  886. rt_pin_write(TURN_ZP_L, OFF);
  887. //Motor_shake(OFF,0);
  888. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  889. DTU_Write("%s",turn_warn);
  890. rt_free(turn_warn);
  891. break;
  892. }
  893. }
  894. turn_cnt = 0;
  895. while(v==90&&rt_pin_read(LIMIT_90)==1)
  896. {
  897. rt_pin_write(TURN_ZP_R, ON);
  898. rt_pin_write(TURN_ZP_L, OFF);
  899. //Motor_shake(ON,0);
  900. rt_thread_delay(200);
  901. turn_cnt ++;
  902. if(turn_cnt==150)
  903. {
  904. turn_cnt = 0;
  905. // rt_pin_write(TURN_ZP_R, OFF);
  906. // rt_pin_write(TURN_ZP_L, OFF);
  907. // char *turn_warn =makeJson_Warn("超时", "转盘");
  908. // DTU_Write(turn_warn);
  909. // rt_free(turn_warn);
  910. break;
  911. }
  912. }
  913. while(v==90&&rt_pin_read(LIMIT_90)==1)
  914. {
  915. // rt_pin_write(TURN_ZP_R, ON);
  916. // rt_pin_write(TURN_ZP_L, OFF);
  917. Motor_shake(ON,0);
  918. rt_thread_delay(50);
  919. turn_cnt ++;
  920. if(turn_cnt==10)
  921. {
  922. turn_cnt = 0;
  923. rt_pin_write(TURN_ZP_R, OFF);
  924. rt_pin_write(TURN_ZP_L, OFF);
  925. //Motor_shake(OFF,0);
  926. char *turn_warn =makeJson_Warn("超时", "转盘");
  927. DTU_Write(turn_warn);
  928. rt_free(turn_warn);
  929. break;
  930. }
  931. }
  932. if(sta!=0)
  933. {
  934. //保存位置
  935. device_status.run_sta = sta;
  936. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  937. }
  938. turn_cnt = 0;
  939. rt_pin_write(TURN_ZP_R, OFF);
  940. rt_pin_write(TURN_ZP_L, OFF);
  941. Motor_shake(OFF,0);
  942. HMI_WRITE("cangWei.rotateFlag.val=0");
  943. }
  944. /*************************************************
  945. Function: ctr_Motor_Driver
  946. Description: 步进电机驱动
  947. Calls:
  948. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  949. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  950. Input: 50000000
  951. Output: NULL
  952. Return: shake_sec
  953. Others: NULL
  954. *************************************************/
  955. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  956. {
  957. beep_init();
  958. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  959. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  960. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  961. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  962. if(EN==0)
  963. {
  964. rt_pin_write(Motor_Power,0);
  965. }
  966. else{
  967. rt_pin_write(Motor_EN, EN);
  968. }
  969. rt_pin_write(Motor_DIR, DIR);
  970. beep_set(pwm, pwm/2); //200kzh - 50%
  971. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  972. {
  973. if(time_out==0)
  974. {
  975. break;
  976. }
  977. rt_thread_delay(50);
  978. time_out --;
  979. }
  980. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  981. {
  982. if(time_out==0)
  983. {
  984. break;
  985. }
  986. rt_thread_delay(50);
  987. time_out --;
  988. }
  989. rt_pin_write(Motor_EN, OFF);
  990. if(device_status.autotakepic==0)
  991. ctr_lamp(OFF, 1);
  992. beep_set(0, 0); //200kzh - 50%
  993. }
  994. /*************************************************
  995. Function: ctr_Petroleum_jelly
  996. Description: 涂凡士林
  997. Calls:
  998. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  999. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1000. Input: NULL
  1001. Output: NULL
  1002. Return: NULL
  1003. Others: NULL
  1004. *************************************************/
  1005. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1006. {
  1007. beep_init();
  1008. pwm_on(2,50000);
  1009. rt_thread_delay(delay*1000);
  1010. pwm_off(2,0);
  1011. rt_thread_delay(1000);
  1012. pwm_on(3, 50000);
  1013. rt_thread_delay(delay*1000);
  1014. pwm_off(3,0);
  1015. device_status.fsl_sw = 0;
  1016. }
  1017. //1.0转仓
  1018. void turnzp(void)
  1019. {
  1020. static uint16_t turn_timeout = 0;
  1021. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1022. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1023. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1024. // rt_pin_write(CLEAR_R,OFF);
  1025. // rt_pin_write(CLEAR_L,OFF);
  1026. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1027. {
  1028. rt_thread_delay(20);
  1029. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1030. {
  1031. // rt_pin_write(CLEAR_R,ON);
  1032. // rt_pin_write(CLEAR_L,OFF);
  1033. Motor_Juandai(ON);
  1034. rt_thread_delay(50);
  1035. turn_timeout++;
  1036. if(turn_timeout==600)
  1037. {
  1038. LOG_E("检测不到限位,电机强制停止");
  1039. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1040. DTU_Write("%s",ctr_zp_limit_warn);
  1041. rt_free(ctr_zp_limit_warn);
  1042. break;
  1043. }
  1044. }
  1045. break;
  1046. }
  1047. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1048. {
  1049. rt_thread_delay(20);
  1050. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1051. {
  1052. // rt_pin_write(CLEAR_R,ON);
  1053. // rt_pin_write(CLEAR_L,OFF);
  1054. Motor_Juandai(ON);
  1055. rt_thread_delay(50);
  1056. turn_timeout++;
  1057. if(turn_timeout==600)
  1058. {
  1059. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1060. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1061. DTU_Write("%s",ctr_zp_limit_warn);
  1062. rt_free(ctr_zp_limit_warn);
  1063. break;
  1064. }
  1065. }
  1066. break;
  1067. }
  1068. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1069. {
  1070. rt_thread_delay(20);
  1071. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1072. {
  1073. // rt_pin_write(CLEAR_R,OFF);
  1074. // rt_pin_write(CLEAR_L,OFF);
  1075. Motor_Juandai(OFF);
  1076. break;
  1077. }
  1078. else {
  1079. break;
  1080. }
  1081. }
  1082. Motor_Juandai(OFF);
  1083. // rt_pin_write(CLEAR_R,OFF);
  1084. // rt_pin_write(CLEAR_L,OFF);
  1085. turn_timeout = 0;
  1086. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1087. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1088. rt_thread_delay(1000);
  1089. }
  1090. //清扫-常闭型限位开关
  1091. //新结构使用推杆电机做清扫
  1092. uint8_t ctr_clear(uint16_t status_clear)
  1093. {
  1094. static uint16_t clear_num = 0;
  1095. #if PWM_EN == 0
  1096. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1097. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1098. #endif
  1099. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1100. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1101. /*************************************************************外接清扫
  1102. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1103. rt_pin_write(POWER_CLEAR,ON);
  1104. rt_thread_delay(1000*100);
  1105. rt_pin_write(POWER_CLEAR,OFF);
  1106. ************************************************************/
  1107. //停止
  1108. #if PWM_EN == 0
  1109. rt_pin_write(CLEAR_R,OFF);
  1110. rt_pin_write(CLEAR_L,OFF);
  1111. #else
  1112. Motor_Qingsao_R_PWM(OFF);
  1113. Motor_Qingsao_L_PWM(OFF);
  1114. #endif
  1115. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1116. rt_thread_delay(100);
  1117. //如果清扫位置在中间
  1118. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1119. {
  1120. #if PWM_EN == 0
  1121. rt_pin_write(CLEAR_R,ON);
  1122. rt_pin_write(CLEAR_L,OFF);
  1123. #else
  1124. Motor_Qingsao_R_PWM(ON);
  1125. Motor_Qingsao_L_PWM(OFF);
  1126. #endif
  1127. rt_thread_delay(50);
  1128. clear_num++;
  1129. //已到达检测位置
  1130. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1131. {
  1132. #if PWM_EN == 0
  1133. rt_pin_write(CLEAR_R,ON);
  1134. rt_pin_write(CLEAR_L,ON);
  1135. #else
  1136. Motor_Qingsao_R_PWM(OFF);
  1137. Motor_Qingsao_L_PWM(OFF);
  1138. #endif
  1139. rt_thread_delay(2000);
  1140. break;
  1141. }
  1142. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1143. {
  1144. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1145. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1146. #if PWM_EN == 0
  1147. rt_pin_write(CLEAR_R,ON);
  1148. rt_pin_write(CLEAR_L,ON);
  1149. #else
  1150. Motor_Qingsao_R_PWM(OFF);
  1151. Motor_Qingsao_L_PWM(OFF);
  1152. #endif
  1153. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1154. if(Read_Real_Voltage()/100<12)
  1155. {
  1156. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1157. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1158. device_status.clear_status = 0;
  1159. return 0;
  1160. }
  1161. break;
  1162. }
  1163. }
  1164. clear_num = 0;
  1165. //如果清扫位置在左侧
  1166. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1167. {
  1168. #if PWM_EN == 0
  1169. rt_pin_write(CLEAR_R,ON);
  1170. rt_pin_write(CLEAR_L,OFF);
  1171. #else
  1172. Motor_Qingsao_R_PWM(ON);
  1173. Motor_Qingsao_L_PWM(OFF);
  1174. #endif
  1175. rt_thread_delay(50);
  1176. clear_num++;
  1177. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1178. {
  1179. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1180. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1181. #if PWM_EN == 0
  1182. rt_pin_write(CLEAR_R,ON);
  1183. rt_pin_write(CLEAR_L,ON);
  1184. #else
  1185. Motor_Qingsao_R_PWM(OFF);
  1186. Motor_Qingsao_L_PWM(OFF);
  1187. #endif
  1188. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1189. if(Read_Real_Voltage()/100<12)
  1190. {
  1191. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1192. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1193. device_status.clear_status = 0;
  1194. return 0;
  1195. }
  1196. break;
  1197. }
  1198. }
  1199. clear_num = 0;
  1200. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1201. {
  1202. #if PWM_EN == 0
  1203. rt_pin_write(CLEAR_R,ON);
  1204. rt_pin_write(CLEAR_L,ON);
  1205. #else
  1206. Motor_Qingsao_R_PWM(OFF);
  1207. Motor_Qingsao_L_PWM(OFF);
  1208. #endif
  1209. if(Read_Real_Voltage()/100<12)
  1210. {
  1211. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1212. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1213. device_status.clear_status = 0;
  1214. return 0;
  1215. }
  1216. rt_thread_delay(2000);
  1217. break;
  1218. }
  1219. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1220. {
  1221. #if PWM_EN == 0
  1222. rt_pin_write(CLEAR_R,OFF);
  1223. rt_pin_write(CLEAR_L,ON);
  1224. #else
  1225. Motor_Qingsao_R_PWM(OFF);
  1226. Motor_Qingsao_L_PWM(ON);
  1227. #endif
  1228. rt_thread_delay(50);
  1229. clear_num++;
  1230. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1231. {
  1232. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1233. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1234. //DTU_Write("%s",Clear_ERR);
  1235. #if PWM_EN == 0
  1236. rt_pin_write(CLEAR_R,ON);
  1237. rt_pin_write(CLEAR_L,ON);
  1238. #else
  1239. Motor_Qingsao_R_PWM(OFF);
  1240. Motor_Qingsao_L_PWM(OFF);
  1241. #endif
  1242. rt_free(Clear_ERR);
  1243. if(Read_Real_Voltage()/100<12)
  1244. {
  1245. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1246. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1247. device_status.clear_status = 0;
  1248. return 0;
  1249. }
  1250. break;
  1251. }
  1252. }
  1253. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1254. {
  1255. #if PWM_EN == 0
  1256. rt_pin_write(CLEAR_R,ON);
  1257. rt_pin_write(CLEAR_L,ON);
  1258. #else
  1259. Motor_Qingsao_R_PWM(OFF);
  1260. Motor_Qingsao_L_PWM(OFF);
  1261. #endif
  1262. rt_thread_delay(2000);
  1263. break;
  1264. }
  1265. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1266. {
  1267. #if PWM_EN == 0
  1268. rt_pin_write(CLEAR_R,ON);
  1269. rt_pin_write(CLEAR_L,ON);
  1270. #else
  1271. Motor_Qingsao_R_PWM(OFF);
  1272. Motor_Qingsao_L_PWM(OFF);
  1273. #endif
  1274. if(device_status.version_type==41)
  1275. {
  1276. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1277. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1278. //DTU_Write("%s",Clear_ERR);
  1279. rt_free(Clear_ERR);
  1280. }
  1281. return 0;
  1282. }
  1283. clear_num = 0;
  1284. rt_thread_delay(2000);
  1285. #if PWM_EN == 0
  1286. rt_pin_write(CLEAR_R,ON);
  1287. rt_pin_write(CLEAR_L,ON);
  1288. #else
  1289. Motor_Qingsao_R_PWM(OFF);
  1290. Motor_Qingsao_L_PWM(OFF);
  1291. #endif
  1292. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1293. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1294. device_status.clear_status = 0;
  1295. return 1;
  1296. }
  1297. //路由器供电
  1298. void ctr_router(uint8_t status_router)
  1299. {
  1300. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1301. if(device_status.version_type==6)
  1302. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1303. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1304. if(status_router==ON)
  1305. {
  1306. rt_pin_write(ROUTER,ON);
  1307. //
  1308. #if LAMP_EN == 0
  1309. if(device_status.version_type==6)
  1310. rt_pin_write(ROUTER_P,ON);
  1311. #endif
  1312. }
  1313. else {
  1314. rt_pin_write(ROUTER,OFF);
  1315. //
  1316. #if LAMP_EN == 0
  1317. if(device_status.version_type==6)
  1318. rt_pin_write(ROUTER_P,OFF);
  1319. #endif
  1320. }
  1321. }
  1322. //清扫-推杆电机
  1323. void ctr_TG_Clear(uint16_t clear_timeout)
  1324. {
  1325. #if 0
  1326. int current_clear[200];
  1327. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1328. LOG_I("清扫电机待机:%d",Electric_current);
  1329. #endif
  1330. static uint16_t clear_count = 0;
  1331. clear_count = clear_timeout;
  1332. #if PWM_EN == 0
  1333. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1334. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1335. #endif
  1336. while(clear_count!=0)
  1337. {
  1338. #if PWM_EN == 0
  1339. rt_pin_write(CLEAR_R,ON);
  1340. rt_pin_write(CLEAR_L,OFF);
  1341. #else
  1342. Motor_Qingsao_R_PWM(ON);
  1343. Motor_Qingsao_L_PWM(OFF);
  1344. #endif
  1345. clear_count--;
  1346. rt_thread_delay(100);
  1347. }
  1348. #if PWM_EN == 0
  1349. rt_pin_write(CLEAR_R,ON);
  1350. rt_pin_write(CLEAR_L,ON);
  1351. #else
  1352. Motor_Qingsao_R_PWM(OFF);
  1353. Motor_Qingsao_L_PWM(OFF);
  1354. #endif
  1355. rt_thread_delay(3000);
  1356. clear_count = 0;
  1357. while(clear_timeout!=clear_count)
  1358. {
  1359. #if PWM_EN == 0
  1360. rt_pin_write(CLEAR_R,OFF);
  1361. rt_pin_write(CLEAR_L,ON);
  1362. #else
  1363. Motor_Qingsao_R_PWM(OFF);
  1364. Motor_Qingsao_L_PWM(ON);
  1365. #endif
  1366. // if(clear_count<200)
  1367. // current_clear[clear_count] = atoi(device_status.power);
  1368. clear_count++;
  1369. rt_thread_delay(100);
  1370. }
  1371. #if PWM_EN == 0
  1372. rt_pin_write(CLEAR_R,ON);
  1373. rt_pin_write(CLEAR_L,ON);
  1374. #else
  1375. Motor_Qingsao_R_PWM(OFF);
  1376. Motor_Qingsao_L_PWM(OFF);
  1377. #endif
  1378. #if 0
  1379. bubblesort(current_clear,200);
  1380. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1381. {
  1382. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1383. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1384. DTU_Write("%s",ctr_clear_warn);
  1385. rt_free(ctr_clear_warn);
  1386. }
  1387. #endif
  1388. rt_thread_delay(2000);
  1389. device_status.clear_status = 0;
  1390. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1391. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1392. }
  1393. //DTU,RTU供电
  1394. void ctr_RTU_DTU(uint8_t status_rtu)
  1395. {
  1396. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1397. if(status_rtu==ON)
  1398. {
  1399. rt_pin_write(RTU_DTU,ON);
  1400. }
  1401. else
  1402. {
  1403. LOG_W("RTU电源复位!!!");
  1404. rt_pin_write(RTU_DTU,OFF);
  1405. }
  1406. }
  1407. //海康拍照
  1408. void Hk_Take_io(void)
  1409. {
  1410. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1411. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1412. rt_pin_write(HK_TAKE, 1);
  1413. rt_pin_write(HK_IO, 0);
  1414. if(device_status.dtype==3)
  1415. Beep_Tip(1000);
  1416. if(device_status.dtype==7)
  1417. rt_thread_mdelay(1000);
  1418. rt_pin_write(HK_TAKE, 0);
  1419. rt_pin_write(HK_IO, 1);
  1420. }
  1421. void calorstat(uint16_t heat_time)
  1422. {
  1423. //uint8_t heat_up_cnt = 0;
  1424. uint16_t n = 0;
  1425. uint16_t delay_heat = 60;
  1426. FlagStatus heat_flg = RESET;
  1427. device_status.heat_time_min = 0;
  1428. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1429. ctr_Heat(ON);
  1430. // UpLoad_Data(); //开始加热时的第一条数据
  1431. //恒温模式
  1432. for(n=0;n < heat_time;n++)
  1433. {
  1434. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1435. {
  1436. heat_flg = SET;
  1437. ctr_Heat(OFF);
  1438. while(delay_heat!=0)
  1439. {
  1440. delay_heat --;
  1441. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1442. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1443. rt_thread_delay(1000);
  1444. if(device_status.heattem>device_status.run_sta + 1){
  1445. device_status.run_sta = device_status.heattem;
  1446. }
  1447. }
  1448. delay_heat = 60;
  1449. //跳出FOR循环
  1450. n = n + 60;
  1451. }
  1452. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1453. {
  1454. device_status.run_sta = device_status.heattem;
  1455. ctr_Heat(ON);
  1456. }
  1457. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1458. {
  1459. // if(heat_up_cnt==0)
  1460. // {
  1461. // UpLoad_Data(); //加热到设定温度的数据
  1462. // heat_up_cnt = 1;
  1463. // }
  1464. device_status.run_sta = device_status.heattem;
  1465. ctr_Heat(OFF);
  1466. }
  1467. if((n % 60)==0)
  1468. {
  1469. device_status.heat_time_min = device_status.heat_time_min + 1;
  1470. // if(device_status.heat_time_min % 5 == 0)
  1471. // {
  1472. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1473. // }
  1474. }
  1475. //如果触发异常退出加热
  1476. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1477. {
  1478. rt_thread_delay(1000);
  1479. if(device_status.bat_status == 1)
  1480. {
  1481. rt_kprintf("============================触发异常退出加热==========================");
  1482. ctr_lamp(OFF,1);
  1483. ctr_Heat(OFF);
  1484. break;
  1485. }
  1486. }
  1487. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1488. {
  1489. ctr_Heat(OFF);
  1490. rt_kprintf("异常退出!!!\n");
  1491. break;
  1492. }
  1493. rt_thread_delay(1000);
  1494. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1495. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1496. }
  1497. ctr_Heat(OFF);
  1498. //UpLoad_Data(); //加热结束的数据
  1499. }
  1500. //初始化所有控制驱动,关闭
  1501. void HardWare_Init(uint8_t boot)
  1502. {
  1503. Beep_Tip(200);
  1504. //关闭上仓门
  1505. ctr_cangmen_up(OFF);
  1506. if(device_status.dtype==3){
  1507. //关闭下仓门电机
  1508. ctr_cangmen_down(OFF);
  1509. }
  1510. //关闭镇流器
  1511. if(device_status.autotakepic==0)
  1512. ctr_lamp(OFF,1);
  1513. //关闭震动电机
  1514. ctr_shake(OFF,0);
  1515. //关闭加热
  1516. ctr_Heat(OFF);
  1517. //关闭补光灯
  1518. ctr_led(OFF);
  1519. //关闭12V供电(海康相机,RTU)
  1520. ctr_photo(OFF);
  1521. }
  1522. //拍照
  1523. void HK_TakePhoto(void)
  1524. {
  1525. //打开补光灯
  1526. DTU_Write("takephoto.mp3");
  1527. rt_thread_delay(1000);
  1528. char *takephotojson = makeJson_takephoto();
  1529. DTU_Write("%s",takephotojson);
  1530. rt_free(takephotojson);
  1531. //吸虫塔
  1532. if(device_status.version_type!=6)
  1533. {
  1534. ctr_led(ON);
  1535. if(device_status.version_type==48)
  1536. {
  1537. rt_thread_delay(1000*60);
  1538. }
  1539. else {
  1540. rt_thread_delay(1000*15);
  1541. }
  1542. }
  1543. //触发海康相机拍照
  1544. Hk_Take_io();
  1545. if(device_status.version_type!=6)
  1546. {
  1547. rt_thread_delay(1000*45);
  1548. }
  1549. else {
  1550. ctr_led(ON);
  1551. rt_thread_delay(1000*19);
  1552. }
  1553. ctr_led(OFF);
  1554. }
  1555. //光控测试任务
  1556. void SunTest(void)
  1557. {
  1558. rt_uint16_t ui_timeout_l = 0;
  1559. //未检测到光照,一秒扫描等待,超时30S
  1560. while(Read_Light_Voltage()<500)
  1561. {
  1562. ui_timeout_l ++;
  1563. rt_thread_delay(1000);
  1564. if(ui_timeout_l==30) break;
  1565. }
  1566. rt_thread_delay(3000);
  1567. if(Read_Light_Voltage()<500)
  1568. {
  1569. device_status.sun_status = 0;//光控状态
  1570. }
  1571. if(Read_Light_Voltage()>500)
  1572. {
  1573. rt_thread_delay(1000);
  1574. }
  1575. rt_thread_delay(3000);
  1576. if(Read_Light_Voltage()>500)
  1577. {
  1578. device_status.sun_status = 1;
  1579. ctr_lamp(OFF,1);
  1580. }
  1581. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1582. device_status.sun_test = 0;
  1583. device_status.work_status=0;
  1584. }
  1585. /*上电初始化*/
  1586. void ResetInit(void)
  1587. {
  1588. if(device_status.dtype==7)
  1589. {
  1590. ctr_Motor_Driver(0, 0, 0, 0);
  1591. //关闭电磁阀
  1592. ctr_shake(OFF, 0);
  1593. //关闭加热
  1594. ctr_Heat(OFF);
  1595. //关闭制冷
  1596. ctr_Cold(OFF);
  1597. //关闭补光灯
  1598. ctr_led(OFF);
  1599. //转盘复位
  1600. if(device_status.run_sta==0)
  1601. ctr_turn(0, 0);
  1602. }
  1603. if(device_status.version_type!=6&&device_status.version_type!=7)
  1604. {
  1605. device_status.work_done = 1;
  1606. //1.打开路由器供电
  1607. if(device_status.power_status==1)
  1608. {
  1609. ctr_router(ON);
  1610. ctr_RTU_DTU(ON);
  1611. //2.打开相机供电
  1612. ctr_photo(ON);
  1613. }
  1614. //3.关闭镇流器
  1615. ctr_lamp(OFF,1);
  1616. #if LAMP_EN == 1
  1617. ctr_lamp(OFF,2);
  1618. ctr_lamp(OFF,3);
  1619. #endif
  1620. //4.关闭震动电机
  1621. ctr_shake(OFF, 0);
  1622. //5.关闭加热
  1623. ctr_Heat(OFF);
  1624. //6.关闭补光灯
  1625. ctr_led(OFF);
  1626. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1627. {
  1628. LOG_W("欠压!");
  1629. return;
  1630. }
  1631. //7.关闭上仓门
  1632. device_status.ctr_up_motor = 2;
  1633. // while(device_status.ctr_up_motor!=0)
  1634. // {
  1635. // LOG_I("关闭上仓门...");
  1636. // rt_thread_delay(100);
  1637. // }
  1638. //8.关闭下仓门
  1639. device_status.ctr_down_motor = 2;
  1640. while(device_status.ctr_down_motor!=0)
  1641. {
  1642. LOG_I("关闭下仓门....");
  1643. rt_thread_delay(100);
  1644. }
  1645. //9.清扫
  1646. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1647. {
  1648. LOG_W("异常退出!!!\n");
  1649. return;
  1650. }
  1651. #if 0//LAMP_EN == 0
  1652. ctr_Fan_PUT(OFF);
  1653. ctr_Fan_IN(OFF);
  1654. #endif
  1655. if(device_status.version_type==4||device_status.version_type==41)
  1656. {
  1657. device_status.clear_status=1;
  1658. }
  1659. while(device_status.clear_status==1){
  1660. rt_thread_delay(1000);
  1661. }
  1662. //排虫
  1663. if(device_status.handle_bugger==0){
  1664. device_status.run_sta = 111;
  1665. }else{
  1666. device_status.run_sta = 112;
  1667. }
  1668. }
  1669. else {
  1670. if(device_status.power_status==1)
  1671. {
  1672. //1.打开路由器供电
  1673. ctr_router(ON);
  1674. ctr_RTU_DTU(ON);
  1675. //2.打开相机供电
  1676. ctr_photo(ON);
  1677. }
  1678. //5.关闭加热
  1679. //6.关闭补光灯
  1680. ctr_led(OFF);
  1681. }
  1682. }
  1683. /*三色光源*/
  1684. void Led_Check(uint8_t tp)
  1685. {
  1686. if(tp==3)
  1687. {
  1688. if(device_status.light_over_tim>2*3600)
  1689. {
  1690. device_status.work_status = 1;
  1691. device_status.lamp_status = 1;
  1692. device_status.lamp1_status = 0;
  1693. device_status.lamp2_status = 0;
  1694. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1695. }
  1696. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1697. device_status.work_status = 1;
  1698. device_status.lamp_status = 0;
  1699. device_status.lamp1_status = 1;
  1700. device_status.lamp2_status = 0;
  1701. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1702. }
  1703. else if (device_status.light_over_tim<1*3600) {
  1704. device_status.work_status = 1;
  1705. device_status.lamp_status = 0;
  1706. device_status.lamp1_status = 0;
  1707. device_status.lamp2_status = 1;
  1708. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1709. }
  1710. }
  1711. else if (tp==6) {
  1712. if(device_status.light_over_tim>4*3600)
  1713. {
  1714. device_status.work_status = 1;
  1715. device_status.lamp_status = 1;
  1716. device_status.lamp1_status = 0;
  1717. device_status.lamp2_status = 0;
  1718. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1719. }
  1720. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1721. device_status.work_status = 1;
  1722. device_status.lamp_status = 0;
  1723. device_status.lamp1_status = 1;
  1724. device_status.lamp2_status = 0;
  1725. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1726. }
  1727. else if (device_status.light_over_tim<7200) {
  1728. device_status.work_status = 1;
  1729. device_status.lamp_status = 0;
  1730. device_status.lamp1_status = 0;
  1731. device_status.lamp2_status = 1;
  1732. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1733. }
  1734. }
  1735. else if (tp==9) {
  1736. if(device_status.light_over_tim>6*3600)
  1737. {
  1738. device_status.work_status = 1;
  1739. device_status.lamp_status = 1;
  1740. device_status.lamp1_status = 0;
  1741. device_status.lamp2_status = 0;
  1742. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1743. }
  1744. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1745. device_status.work_status = 1;
  1746. device_status.lamp_status = 0;
  1747. device_status.lamp1_status = 1;
  1748. device_status.lamp2_status = 0;
  1749. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1750. }
  1751. else if (device_status.light_over_tim<3*3600) {
  1752. device_status.work_status = 1;
  1753. device_status.lamp_status = 0;
  1754. device_status.lamp1_status = 0;
  1755. device_status.lamp2_status = 1;
  1756. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1757. }
  1758. }
  1759. }
  1760. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1761. void HardWare_Check(uint8_t boot)
  1762. {
  1763. if(boot==0&&device_status.version_type!=6)
  1764. {
  1765. //停止所有动作 -完成
  1766. HardWare_Init(boot);
  1767. }
  1768. else {
  1769. //停止所有动作
  1770. //读取记录待机功耗(实测大概70mA)
  1771. //(需减去屏幕的功耗大概120mA)
  1772. //动作上仓门,读取功率值保存
  1773. //动作下仓门,读取功率值保存
  1774. //动作清扫,读取功率值保存
  1775. //打开镇流器,读取功率值保存
  1776. //打开海康相机供电,读取功率值保存
  1777. //打开补光灯,读取功率值保存
  1778. //打开路由器/RTU供电,读取功率值保存
  1779. //打开加热,读取功率值保存
  1780. }
  1781. }
  1782. //工作状态检测
  1783. void WorkCheck(void)
  1784. {
  1785. //雨控检测,温控检测,电池电压检测
  1786. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1787. {
  1788. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1789. if(device_status.timeSwitch==1)
  1790. {
  1791. LOG_I("***********时控*****************");
  1792. if(device_status.startBar==device_status.stopBar&&device_status.startBar==0){
  1793. device_status.work_status = 1;
  1794. LOG_I("24 hours work...");
  1795. #if LAMP_EN == 1
  1796. device_status.lamp_status = 1;
  1797. device_status.lamp1_status = 0;
  1798. device_status.lamp2_status = 0;
  1799. if(device_status.sun_status==1){
  1800. ctr_lamp(OFF, 1);
  1801. ctr_lamp(OFF, 2);
  1802. ctr_lamp(OFF, 3);
  1803. }
  1804. #endif
  1805. }
  1806. //如果开始和结束是同一天
  1807. if(device_status.startBar<device_status.stopBar)
  1808. {
  1809. LOG_I("开始和结束是同一天");
  1810. time_t now;
  1811. time(&now);
  1812. static struct tm nowtime; //现在
  1813. static struct tm starttime; //开始
  1814. static struct tm endtime; //结束
  1815. //获取当前时间戳
  1816. nowtime =*localtime(&now);
  1817. nowtime.tm_hour = nowtime.tm_hour-8;
  1818. //获取定时开始的时间戳
  1819. starttime = *localtime(&now);
  1820. starttime.tm_hour = device_status.startBar-8;
  1821. starttime.tm_min = 0;
  1822. starttime.tm_sec = 0;
  1823. //获取定时结束的时间戳
  1824. endtime = *localtime(&now);
  1825. endtime.tm_hour = device_status.stopBar-8;
  1826. endtime.tm_min = 0;
  1827. endtime.tm_sec = 0;
  1828. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1829. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1830. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1831. {
  1832. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1833. device_status.work_status = 1;
  1834. }
  1835. //如果当前时间小于开始时间
  1836. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1837. {
  1838. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1839. device_status.work_status = 0;
  1840. }
  1841. //当前时间大于结束时间
  1842. else {
  1843. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1844. device_status.work_status = 0;
  1845. }
  1846. #if LAMP_EN == 1
  1847. //如果当前时间超过20点,未到21点
  1848. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1849. {
  1850. LOG_I("3led:20~21");
  1851. device_status.lamp_status = 1;
  1852. device_status.lamp1_status = 0;
  1853. device_status.lamp2_status = 0;
  1854. }
  1855. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1856. LOG_I("3led:21~22");
  1857. device_status.lamp1_status = 1;
  1858. device_status.lamp_status = 0;
  1859. device_status.lamp2_status = 0;
  1860. }
  1861. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1862. LOG_I("3led:22~23");
  1863. device_status.lamp2_status = 1;
  1864. device_status.lamp_status = 0;
  1865. device_status.lamp1_status =0;
  1866. }
  1867. #endif
  1868. }
  1869. //如果结束时间是第二天
  1870. else if (device_status.startBar>device_status.stopBar) {
  1871. time_t now;
  1872. time(&now);
  1873. static struct tm nowtime; //现在
  1874. static struct tm starttime; //开始
  1875. static struct tm endtime; //结束
  1876. //获取当前时间戳
  1877. nowtime =*localtime(&now);
  1878. nowtime.tm_hour = nowtime.tm_hour-8;
  1879. //获取定时开始的时间戳
  1880. starttime = *localtime(&now);
  1881. starttime.tm_hour = device_status.startBar-8;
  1882. starttime.tm_min = 0;
  1883. starttime.tm_sec = 0;
  1884. //获取定时结束的时间戳
  1885. endtime = *localtime(&now);
  1886. endtime.tm_hour = device_status.stopBar-8;
  1887. endtime.tm_min = 0;
  1888. endtime.tm_sec = 0;
  1889. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1890. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1891. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1892. {
  1893. //获取定时结束的时间戳
  1894. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1895. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1896. endtime.tm_min = 0;
  1897. endtime.tm_sec = 0;
  1898. device_status.work_status = 1;
  1899. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1900. }
  1901. //当前时间小于开始时间
  1902. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1903. {
  1904. //获取定时结束的时间戳
  1905. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1906. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1907. endtime.tm_min = 0;
  1908. endtime.tm_sec = 0;
  1909. //如果当前时间戳小于结束时间戳
  1910. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1911. {
  1912. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1913. device_status.work_status = 1;
  1914. }
  1915. else
  1916. {
  1917. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1918. device_status.work_status = 0; //不在工作时间,待机
  1919. }
  1920. }
  1921. }
  1922. }
  1923. else {
  1924. LOG_I("光控定时模式!");
  1925. /*
  1926. time_t now;
  1927. time(&now);
  1928. static struct tm nowstamp;
  1929. nowstamp =*localtime(&now);
  1930. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1931. static rt_uint32_t nowstamp_num = 0;
  1932. nowstamp_num = mktime(&nowstamp);
  1933. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1934. */
  1935. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1936. {
  1937. device_status.light_over_tim--;
  1938. #if LAMP_EN == 1
  1939. Led_Check(device_status.sunBar);
  1940. #else
  1941. device_status.work_status = 1;;
  1942. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1943. #endif
  1944. }
  1945. else
  1946. {
  1947. if(device_status.test_flag==0)
  1948. {
  1949. ctr_lamp(OFF,1);
  1950. #if LAMP_EN == 1
  1951. ctr_lamp(OFF,2);
  1952. ctr_lamp(OFF,3);
  1953. #endif
  1954. }
  1955. device_status.work_status = 0;
  1956. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1957. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1958. }
  1959. }
  1960. rt_thread_delay(1000);
  1961. }
  1962. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1963. device_status.work_status = 0;
  1964. rt_thread_delay(1000);
  1965. }
  1966. void WorkTask_BZY(void)
  1967. {
  1968. if(device_status.ds==1)
  1969. {
  1970. //时间段判断
  1971. //先判断第一个时间段
  1972. time_t now;
  1973. time(&now);
  1974. static struct tm now_stamp; //现在
  1975. static struct tm cul_sh1_stamp; //开始1
  1976. static struct tm cul_eh1_stamp; //结束1
  1977. static struct tm cul_sh2_stamp; //开始2
  1978. static struct tm cul_eh2_stamp; //结束2
  1979. static struct tm cul_sh3_stamp; //开始3
  1980. static struct tm cul_eh3_stamp; //结束3
  1981. now_stamp = *localtime(&now);
  1982. //获取定时开始的时间戳
  1983. cul_sh1_stamp = *localtime(&now);
  1984. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  1985. cul_sh1_stamp.tm_min = 0;
  1986. cul_sh1_stamp.tm_sec = 0;
  1987. cul_sh2_stamp = *localtime(&now);
  1988. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  1989. cul_sh2_stamp.tm_min = 0;
  1990. cul_sh2_stamp.tm_sec = 0;
  1991. cul_sh3_stamp = *localtime(&now);
  1992. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  1993. cul_sh3_stamp.tm_min = 0;
  1994. cul_sh3_stamp.tm_sec = 0;
  1995. //获取定时结束的时间戳
  1996. cul_eh1_stamp = *localtime(&now);
  1997. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  1998. cul_eh1_stamp.tm_min = 0;
  1999. cul_eh1_stamp.tm_sec = 0;
  2000. cul_eh2_stamp = *localtime(&now);
  2001. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2002. cul_eh2_stamp.tm_min = 0;
  2003. cul_eh2_stamp.tm_sec = 0;
  2004. cul_eh3_stamp = *localtime(&now);
  2005. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2006. cul_eh3_stamp.tm_min = 0;
  2007. cul_eh3_stamp.tm_sec = 0;
  2008. LOG_I("采集时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh1_stamp.tm_year+1900,cul_sh1_stamp.tm_mon+1,cul_sh1_stamp.tm_mday,cul_sh1_stamp.tm_hour,cul_sh1_stamp.tm_min,cul_sh1_stamp.tm_sec);
  2009. LOG_I("采集时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh2_stamp.tm_year+1900,cul_sh2_stamp.tm_mon+1,cul_sh2_stamp.tm_mday,cul_sh2_stamp.tm_hour,cul_sh2_stamp.tm_min,cul_sh2_stamp.tm_sec);
  2010. LOG_I("采集时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh3_stamp.tm_year+1900,cul_sh3_stamp.tm_mon+1,cul_sh3_stamp.tm_mday,cul_sh3_stamp.tm_hour,cul_sh3_stamp.tm_min,cul_sh3_stamp.tm_sec);
  2011. LOG_I("结束时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh1_stamp.tm_year+1900,cul_eh1_stamp.tm_mon+1,cul_eh1_stamp.tm_mday,cul_eh1_stamp.tm_hour,cul_eh1_stamp.tm_min,cul_eh1_stamp.tm_sec);
  2012. LOG_I("结束时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh2_stamp.tm_year+1900,cul_eh2_stamp.tm_mon+1,cul_eh2_stamp.tm_mday,cul_eh2_stamp.tm_hour,cul_eh2_stamp.tm_min,cul_eh2_stamp.tm_sec);
  2013. LOG_I("结束时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_eh3_stamp.tm_year+1900,cul_eh3_stamp.tm_mon+1,cul_eh3_stamp.tm_mday,cul_eh3_stamp.tm_hour,cul_eh3_stamp.tm_min,cul_eh3_stamp.tm_sec);
  2014. //此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2015. //当前时间戳小于第一段开始时间戳
  2016. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2017. {
  2018. time_t now;
  2019. time(&now);
  2020. cul_sh1_stamp = *localtime(&now);
  2021. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2022. cul_sh1_stamp.tm_min = 0;
  2023. cul_sh1_stamp.tm_sec = 0;
  2024. now_stamp = *localtime(&now);
  2025. LOG_I("======收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh1_stamp.tm_year+1900,cul_sh1_stamp.tm_mon+1,cul_sh1_stamp.tm_mday,cul_sh1_stamp.tm_hour,cul_sh1_stamp.tm_min,cul_sh1_stamp.tm_sec);
  2026. LOG_I("======未到收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.",now_stamp.tm_year+1900,now_stamp.tm_mon+1,now_stamp.tm_mday,now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec);
  2027. LOG_D("run_sta:%d",device_status.run_sta);
  2028. }
  2029. //当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2030. if(difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0&&difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2031. {
  2032. time_t now;
  2033. time(&now);
  2034. cul_sh2_stamp = *localtime(&now);
  2035. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2036. cul_sh2_stamp.tm_min = 0;
  2037. cul_sh2_stamp.tm_sec = 0;
  2038. now_stamp = *localtime(&now);
  2039. LOG_I("==============收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n",cul_sh2_stamp.tm_year+1900,cul_sh2_stamp.tm_mon+1,cul_sh2_stamp.tm_mday,cul_sh2_stamp.tm_hour,cul_sh2_stamp.tm_min,cul_sh2_stamp.tm_sec);
  2040. LOG_I("==============未到收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n",now_stamp.tm_year+1900,now_stamp.tm_mon+1,now_stamp.tm_mday,now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec);
  2041. LOG_D("run_sta:%d",device_status.run_sta);
  2042. }
  2043. //如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2044. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0&&difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2045. {
  2046. //转盘到涂凡士林位置
  2047. if(device_status.run_sta==0)
  2048. {
  2049. LOG_I("start to collect!!!");
  2050. DTU_Write("collect.mp3");
  2051. ctr_turn(90,4);
  2052. rt_thread_delay(2000);
  2053. //涂凡士林
  2054. ctr_Petroleum_jelly(10);
  2055. rt_thread_delay(5000);
  2056. }
  2057. /*转盘到采集位置*/
  2058. if(device_status.run_sta==4)
  2059. ctr_turn(90,1);
  2060. //只有开始和结束时刻不同的时间段,才有效
  2061. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)||(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2062. {
  2063. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)&& difftime(mktime(&now_stamp),mktime(&cul_sh1_stamp)) > 0)
  2064. {
  2065. device_status.work_status = 1;
  2066. ctr_Collect_Fan(ON);
  2067. //当前时间戳小于第一段结束时间戳
  2068. while(difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp))>0)
  2069. {
  2070. time(&now);
  2071. now_stamp = *localtime(&now);
  2072. cul_eh1_stamp = *localtime(&now);
  2073. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2074. cul_eh1_stamp.tm_min = 0;
  2075. cul_eh1_stamp.tm_sec = 0;
  2076. if(device_status.run_sta!=1) break;
  2077. LOG_I("1===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh1_stamp));
  2078. rt_thread_mdelay(10000);
  2079. }
  2080. }
  2081. else {
  2082. device_status.work_status=0;
  2083. ctr_Collect_Fan(OFF);
  2084. LOG_I("未到收集1时间!");
  2085. }
  2086. if(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp),mktime(&cul_sh2_stamp)) > 0)
  2087. {
  2088. device_status.work_status = 1;
  2089. ctr_Collect_Fan(ON);
  2090. while(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp))>0)
  2091. {
  2092. time(&now);
  2093. now_stamp = *localtime(&now);
  2094. cul_eh2_stamp = *localtime(&now);
  2095. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2096. cul_eh2_stamp.tm_min = 0;
  2097. cul_eh2_stamp.tm_sec = 0;
  2098. if(device_status.run_sta!=1) break;
  2099. LOG_I("2===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh2_stamp));
  2100. rt_thread_mdelay(10000);
  2101. }
  2102. }
  2103. else {
  2104. device_status.work_status=0;
  2105. ctr_Collect_Fan(OFF);
  2106. LOG_I("未到收集2时间!");
  2107. }
  2108. }
  2109. else
  2110. {
  2111. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2112. }
  2113. }
  2114. if(device_status.run_sta==2)
  2115. {
  2116. ctr_Collect_Fan(OFF);
  2117. device_status.develop_flag = 1;
  2118. device_status.work_status = 2;
  2119. ctr_develop(); //培养
  2120. ctr_turn(90,3);
  2121. }
  2122. if(device_status.run_sta==3)
  2123. {
  2124. ctr_Collect_Fan(OFF);
  2125. device_status.work_status = 3;
  2126. device_status.autotakepic=1; //对焦拍照
  2127. }
  2128. //在培养之前再次确认时间戳
  2129. time(&now);
  2130. now_stamp = *localtime(&now);
  2131. cul_eh2_stamp = *localtime(&now);
  2132. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2133. cul_eh2_stamp.tm_min = 0;
  2134. cul_eh2_stamp.tm_sec = 0;
  2135. //当前时间戳大于收集结束时间戳并且run_sta==1
  2136. if(device_status.run_sta==1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2137. {
  2138. ctr_Collect_Fan(OFF);
  2139. LOG_I("===============collect ok,turn to drop!!!");
  2140. device_status.develop_flag=1;
  2141. device_status.work_status=2; //现在是培养
  2142. DTU_Write("develop.mp3");
  2143. if(device_status.run_sta==1)
  2144. ctr_turn(90,2); /*转盘到培养位置*/
  2145. rt_thread_delay(1000);
  2146. ctr_shake(ON,device_status.shake_sec);
  2147. rt_thread_delay(1000);
  2148. ctr_develop(); //培养
  2149. if(device_status.run_sta==2)
  2150. ctr_turn(90,3); /*转盘到拍照位置*/
  2151. rt_thread_delay(1000);
  2152. device_status.work_status=3; //现在是拍照
  2153. device_status.autotakepic=1; //对焦拍照
  2154. while(device_status.autotakepic==0)
  2155. {
  2156. rt_thread_delay(1000);
  2157. LOG_I("正在对焦拍照............");
  2158. }
  2159. device_status.work_status = 0; //待机
  2160. DTU_Write("wait.mp3");
  2161. LOG_I("=====================work finished==================");
  2162. }
  2163. rt_thread_delay(1000);
  2164. }
  2165. device_status.work_status = 0; //待机
  2166. LOG_W("==================!!!设备已待机!!!===================");
  2167. }
  2168. void WorkTask_BZYV2(void)
  2169. {
  2170. //打开采集风机
  2171. ctr_Collect_Fan(ON);
  2172. //打开胶带步进电机
  2173. //等待采集结束
  2174. //拍照
  2175. //打开相机供电
  2176. //打开补光灯
  2177. //滑台归位
  2178. //滑台启动
  2179. }
  2180. //工作任务流程s
  2181. void WorkTask(void)
  2182. {
  2183. static int clt_time = 0; //收集时间差秒数
  2184. static int heat_time = 0;
  2185. LOG_I("工作状态:%d",device_status.work_status);
  2186. LOG_I("开关机状态:%d",device_status.ds);
  2187. 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)
  2188. {
  2189. device_status.work_done = 0;
  2190. if(clt_time==0)
  2191. {
  2192. #if 0
  2193. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2194. {
  2195. LOG_W("相机电流不足!");
  2196. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2197. DTU_Write("%s",ctr_hk_p_warn);
  2198. rt_free(ctr_hk_p_warn);
  2199. }
  2200. #endif
  2201. #if 0//LAMP_EN == 0
  2202. ctr_Fan_PUT(ON);
  2203. #endif
  2204. DTU_Write("workstart.mp3");
  2205. }
  2206. //1.打开灯管
  2207. #if LAMP_EN == 1
  2208. if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
  2209. {
  2210. ctr_lamp(ON,1);
  2211. ctr_lamp(OFF,2);
  2212. ctr_lamp(OFF,3);
  2213. }
  2214. else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
  2215. ctr_lamp(OFF,1);
  2216. ctr_lamp(ON,2);
  2217. ctr_lamp(OFF,3);
  2218. }
  2219. else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
  2220. ctr_lamp(OFF,1);
  2221. ctr_lamp(OFF,2);
  2222. ctr_lamp(ON,3);
  2223. }
  2224. #else
  2225. ctr_lamp(ON,1);
  2226. #endif
  2227. LOG_I("打开灯管!!!");
  2228. #if 0
  2229. rt_thread_delay(2000);
  2230. if(atoi(device_status.power)<450)
  2231. {
  2232. LOG_I("Current is:%d",atoi(device_status.power));
  2233. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  2234. {
  2235. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  2236. DTU_Write("%s",lamp_err_warn);
  2237. rt_free(lamp_err_warn);
  2238. }
  2239. }
  2240. #endif
  2241. if(device_status.version_type!=5)
  2242. {
  2243. if(clt_time==0)
  2244. {
  2245. rt_thread_delay(5000); //待灯管启动再往下执行,否则会触发电机过流
  2246. //2.打开上仓门
  2247. rt_kprintf("打开上仓门!!!\n");
  2248. device_status.ctr_up_motor = 1;
  2249. }
  2250. //3.收集
  2251. clt_time = device_status.clt_time * 60;
  2252. while(clt_time!=1)
  2253. {
  2254. #if 0//LAMP_EN == 0
  2255. ctr_Fan_IN(ON);
  2256. #endif
  2257. clt_time --;
  2258. rt_thread_delay(1000);
  2259. #if 0//LAMP_EN == 0
  2260. if(clt_time%30==0)
  2261. ctr_Fan_PUT(OFF);
  2262. #endif
  2263. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2264. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"",device_status.clt_time,clt_time);
  2265. //如果触发异常
  2266. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2267. {
  2268. ctr_lamp(OFF,1);
  2269. #if LAMP_EN == 1
  2270. ctr_lamp(OFF,2);
  2271. ctr_lamp(OFF,3);
  2272. #endif
  2273. #if 0//LAMP_EN == 0
  2274. ctr_Fan_IN(OFF);
  2275. #endif
  2276. rt_kprintf("异常退出!!!\n");
  2277. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2278. break;
  2279. }
  2280. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2281. {
  2282. ctr_lamp(OFF,1);
  2283. #if LAMP_EN == 1
  2284. ctr_lamp(OFF,2);
  2285. ctr_lamp(OFF,3);
  2286. #endif
  2287. #if 0//LAMP_EN == 0
  2288. ctr_Fan_IN(OFF);
  2289. #endif
  2290. rt_kprintf("异常退出!!!\n");
  2291. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2292. break;
  2293. }
  2294. }
  2295. clt_time = 1;
  2296. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2297. {
  2298. ctr_lamp(OFF,1);
  2299. #if LAMP_EN == 1
  2300. ctr_lamp(OFF,2);
  2301. ctr_lamp(OFF,3);
  2302. #endif
  2303. #if 0//LAMP_EN == 0
  2304. ctr_Fan_IN(OFF);
  2305. #endif
  2306. rt_kprintf("异常退出!!!\n");
  2307. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2308. break;
  2309. }
  2310. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2311. {
  2312. ctr_lamp(OFF,1);
  2313. #if LAMP_EN == 1
  2314. ctr_lamp(OFF,2);
  2315. ctr_lamp(OFF,3);
  2316. #endif
  2317. #if 0//LAMP_EN == 0
  2318. ctr_Fan_IN(OFF);
  2319. #endif
  2320. rt_kprintf("异常退出!!!\n");
  2321. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2322. break;
  2323. }
  2324. #if 0//LAMP_EN == 0
  2325. ctr_Fan_IN(OFF);
  2326. #endif
  2327. //4.关闭上仓门
  2328. device_status.ctr_up_motor = 2;
  2329. rt_kprintf("关闭上仓门!!!\n");
  2330. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  2331. while(device_status.ctr_up_motor!=0)
  2332. {
  2333. rt_thread_delay(100);
  2334. LOG_I("等待上仓门关闭!!!");
  2335. }
  2336. //5.加热
  2337. if(device_status.heat_sw==0)
  2338. {
  2339. calorstat(device_status.heattime*60);
  2340. }
  2341. else {
  2342. heat_time = device_status.heattime * 60;
  2343. while(heat_time!=0)
  2344. {
  2345. heat_time--;
  2346. LOG_I("正在加热状态!!!");
  2347. rt_thread_delay(1000);
  2348. }
  2349. }
  2350. //6.打开下仓门
  2351. if(device_status.rain_status==0)
  2352. {
  2353. device_status.ctr_down_motor =1;
  2354. }
  2355. else {
  2356. device_status.ctr_down_motor = 2;
  2357. device_status.ctr_up_motor = 2;
  2358. }
  2359. //待下仓门完全打开
  2360. while(device_status.ctr_down_motor!=0)
  2361. {
  2362. rt_thread_delay(100);
  2363. rt_kprintf("等待下仓门打开!!!\n");
  2364. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  2365. }
  2366. rt_thread_delay(3000);
  2367. //关闭下仓门
  2368. device_status.ctr_down_motor = 2;
  2369. while(device_status.ctr_down_motor!=0)
  2370. {
  2371. rt_thread_delay(100);
  2372. rt_kprintf("等待下仓门关闭!!!\n");
  2373. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  2374. }
  2375. //2022-04-12add:
  2376. LOG_I("打开上仓门!!!");
  2377. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  2378. device_status.ctr_up_motor = 1;
  2379. if(device_status.version_type==4||device_status.version_type==41)
  2380. {
  2381. //震动
  2382. ctr_shake(ON, device_status.shake_sec);
  2383. //补光&&拍照
  2384. LOG_I("拍照!!!");
  2385. HMI_WRITE("workSta.g0.txt=\"拍照!\"");
  2386. HK_TakePhoto();
  2387. //清扫
  2388. device_status.clear_status=1;
  2389. //清扫失效则使用震动
  2390. //ctr_shake(ON, 100);
  2391. }
  2392. }
  2393. else {
  2394. //打开高压
  2395. ctr_Heat(ON);
  2396. //打开补光
  2397. ctr_led(ON);
  2398. LOG_I("打开补光!!");
  2399. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  2400. //2.翻转清虫
  2401. while(rt_pin_read(CLEAR_L_LIMIT)==1) rt_thread_delay(20);
  2402. rt_thread_delay(500);
  2403. Hk_Take_io(); //触发海康相机拍照
  2404. rt_thread_delay(500);
  2405. ctr_turnv5(); //摆动清虫
  2406. LOG_I("摆动清虫!");
  2407. }
  2408. }
  2409. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2410. {
  2411. rt_kprintf("-------------------not work--------------------\n");
  2412. device_status.work_status = 0;
  2413. }
  2414. //工作结束
  2415. if(device_status.work_done==0&&device_status.work_status==0)
  2416. {
  2417. DTU_Write("workdone");
  2418. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  2419. if (device_status.version_type==1) {
  2420. turnzp(); //2024-03-21 :开机不再转仓
  2421. }
  2422. ResetInit();
  2423. }
  2424. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2425. }
  2426. /* 入口函数-工作状态检测 */
  2427. static void Work_status_thread_entry(void *parameter)
  2428. {
  2429. static uint16_t sun_status_num = 120;
  2430. //如果是光控定时,进行光控监测的消抖
  2431. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2432. {
  2433. rt_kprintf("光控定时!!!\n");
  2434. while(sun_status_num!=0)
  2435. {
  2436. sun_status_num--;
  2437. rt_thread_delay(1000);
  2438. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2439. }
  2440. device_status.light_over_tim = device_status.sunBar * 3600;
  2441. device_status.test_flag = 0;
  2442. }
  2443. while(1)
  2444. {
  2445. WorkCheck();
  2446. }
  2447. }
  2448. /*入口函数-工作流程*/
  2449. static void Work_Loop_thread_entry(void *parameter)
  2450. {
  2451. rt_thread_delay(1000);
  2452. ResetInit();
  2453. while(1)
  2454. {
  2455. if(device_status.dtype==7)
  2456. {
  2457. WorkTask_BZY();
  2458. }
  2459. if(device_status.version_type!=6&&device_status.dtype==3)
  2460. {
  2461. WorkTask();
  2462. }
  2463. if(device_status.version_type==6)
  2464. {
  2465. WorkTask_XCT();
  2466. }
  2467. rt_thread_delay(1000);
  2468. }
  2469. }
  2470. /*入口函数清扫*/
  2471. static void Control_Clear_thread_entry(void *parameter)
  2472. {
  2473. while(1)
  2474. {
  2475. #if ROBOT_EN ==1
  2476. ctr_Motor_Robot(ON);
  2477. rt_thread_delay(100);
  2478. #else
  2479. #if Color_EN ==1
  2480. /*复位*/
  2481. if(Read_Color_Key(KEY_RESET)==1)
  2482. {
  2483. ctr_Color_board(RESET, 1200);
  2484. rt_thread_delay(5000);
  2485. }
  2486. #else
  2487. if(device_status.clear_status==1)
  2488. {
  2489. if(ctr_clear(1200)==0)
  2490. ctr_TG_Clear(400);
  2491. }
  2492. rt_thread_delay(100);
  2493. #endif
  2494. #endif
  2495. }
  2496. }
  2497. /*入口函数加热*/
  2498. static void Control_Heat_thread_entry(void *parameter)
  2499. {
  2500. uint16_t var_clear = 0;
  2501. #if ROBOT_EN == 1
  2502. static uint8_t key_on = 0;
  2503. #else
  2504. #if Color_EN == 0
  2505. while(device_status.work_status==0)
  2506. {
  2507. rt_thread_delay(1000);
  2508. }
  2509. #endif
  2510. #endif
  2511. while(1)
  2512. {
  2513. #if ROBOT_EN == 1
  2514. //磁悬浮供电
  2515. //监测磁悬浮限位位置`
  2516. LOG_D("Read 磁悬浮:%d",device_status.heattem);
  2517. //等待10S
  2518. if(device_status.heattem<30&&Read_Real_Voltage()/100>12) //磁悬浮处于关闭状态
  2519. {
  2520. key_on = 0;
  2521. //发送触摸按钮信号
  2522. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  2523. rt_pin_write(EMS_Dir, 0);
  2524. rt_thread_delay(50);
  2525. rt_pin_write(EMS_Dir, 1);
  2526. }
  2527. else if (device_status.heattem<30&&Read_Real_Voltage()/100<12) {
  2528. ctr_router(OFF);
  2529. }
  2530. if(device_status.heattem>40)
  2531. {
  2532. key_on = 1;
  2533. rt_thread_delay(1000);
  2534. LOG_D("Read 磁悬浮已打开:%d",device_status.heattem);
  2535. }
  2536. //磁悬浮供电
  2537. //监测磁悬浮限位位置
  2538. //根据位置确定是否 按下按钮+
  2539. if(Read_Real_Voltage()/100<12&&key_on==1)
  2540. {
  2541. key_on = 0;
  2542. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  2543. rt_pin_write(EMS_Dir, 0);
  2544. rt_thread_delay(50);
  2545. rt_pin_write(EMS_Dir, 1);
  2546. }
  2547. #else
  2548. #if Color_EN == 1
  2549. /*黄板归位*/
  2550. if(Read_Color_Key(KEY_BACK)==1)
  2551. {
  2552. LOG_W("黄板归位!");
  2553. ctr_Color_board(CLOSE, 300);
  2554. rt_thread_delay(5000);
  2555. }
  2556. #else
  2557. if(device_status.work_status==1&&device_status.heat_sw==1)
  2558. {
  2559. ctr_lamp(ON, 1);
  2560. rt_thread_delay(10000);
  2561. calorstat(device_status.heattime*60);
  2562. }
  2563. else
  2564. {
  2565. LOG_I("Not calorstat!");
  2566. }
  2567. if(device_status.work_status==1&&device_status.version_type==5)
  2568. {
  2569. var_clear++;
  2570. if(var_clear==300)
  2571. {
  2572. var_clear = 0;
  2573. ctr_cangmen_down(ON);
  2574. rt_thread_mdelay(5000);
  2575. ctr_cangmen_down(OFF);
  2576. rt_thread_mdelay(500);
  2577. ctr_cangmen_down(ON);
  2578. rt_thread_mdelay(5000);
  2579. ctr_cangmen_down(OFF);
  2580. }
  2581. }
  2582. rt_thread_delay(1000);
  2583. #endif
  2584. #endif
  2585. }
  2586. }
  2587. /*清扫线程*/
  2588. int Control_clear(void)
  2589. {
  2590. /* 创建线程*/
  2591. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2592. Control_Clear_thread_entry, RT_NULL,
  2593. 2048, //2048
  2594. 24, //23
  2595. WORK_CHECK_THREAD_TIMESLICE); //5
  2596. if(tid_ctr!=RT_NULL)
  2597. {
  2598. rt_thread_startup(tid_ctr);
  2599. }
  2600. return 0;
  2601. }
  2602. /*恒温加热线程*/
  2603. int Control_heat(void)
  2604. {
  2605. /* 创建线程*/
  2606. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  2607. Control_Heat_thread_entry, RT_NULL,
  2608. 1024, //2048
  2609. 25, //23
  2610. WORK_CHECK_THREAD_TIMESLICE); //5
  2611. if(tid_heat!=RT_NULL)
  2612. {
  2613. rt_thread_startup(tid_heat);
  2614. }
  2615. return 0;
  2616. }
  2617. /* 创建线程 -工作状态检测*/
  2618. int Workstatus_check(void)
  2619. {
  2620. /* 创建线程*/
  2621. tid_wkstc = rt_thread_create("work_status_check_thread",
  2622. Work_status_thread_entry, RT_NULL,
  2623. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2624. WORK_CHECK_THREAD_PRIORITY, //23
  2625. WORK_CHECK_THREAD_TIMESLICE); //5
  2626. if(tid_wkstc!=RT_NULL)
  2627. {
  2628. rt_thread_startup(tid_wkstc);
  2629. }
  2630. return 0;
  2631. }
  2632. /*工作流程线程*/
  2633. int Work_Loop_Task(void)
  2634. {
  2635. /* 创建线程*/
  2636. tid_workloop = rt_thread_create("work_loop_thread",
  2637. Work_Loop_thread_entry, RT_NULL,
  2638. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2639. WORK_LOOP_THREAD_PRIORITY, //12
  2640. WORK_LOOP_THREAD_TIMESLICE); //5
  2641. #if Color_EN == 0
  2642. if(tid_workloop!=RT_NULL)
  2643. {
  2644. rt_thread_startup(tid_workloop);
  2645. }
  2646. #endif
  2647. return 0;
  2648. }
  2649. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2650. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2651. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2652. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);