control.c 90 KB

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