control.c 90 KB

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