control.c 92 KB

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