control.c 92 KB

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