control.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  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(50);
  844. if(turn_cnt==300)
  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(50); //10 to 100
  863. turn_cnt ++;
  864. if(turn_cnt==1) //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==1) //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(50);
  901. turn_cnt ++;
  902. if(turn_cnt==300)
  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)==0)
  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==1)
  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("sysTest.switchClear.val=%d",0);
  943. //HMI_WRITE("cangWei.rotateFlag.val=0");
  944. }
  945. /*************************************************
  946. Function: ctr_Motor_Driver
  947. Description: 步进电机驱动
  948. Calls:
  949. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  950. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  951. Input: 50000000
  952. Output: NULL
  953. Return: shake_sec
  954. Others: NULL
  955. *************************************************/
  956. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  957. {
  958. beep_init();
  959. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  960. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  961. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  962. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  963. if(EN==0)
  964. {
  965. rt_pin_write(Motor_Power,0);
  966. }
  967. else{
  968. rt_pin_write(Motor_EN, EN);
  969. }
  970. rt_pin_write(Motor_DIR, DIR);
  971. beep_set(pwm, pwm/2); //200kzh - 50%
  972. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  973. {
  974. if(time_out==0)
  975. {
  976. break;
  977. }
  978. rt_thread_delay(50);
  979. time_out --;
  980. }
  981. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  982. {
  983. if(time_out==0)
  984. {
  985. break;
  986. }
  987. rt_thread_delay(50);
  988. time_out --;
  989. }
  990. rt_pin_write(Motor_EN, OFF);
  991. if(device_status.autotakepic==0)
  992. ctr_lamp(OFF, 1);
  993. beep_set(0, 0); //200kzh - 50%
  994. }
  995. /*************************************************
  996. Function: ctr_Petroleum_jelly
  997. Description: 涂凡士林
  998. Calls:
  999. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1000. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1001. Input: NULL
  1002. Output: NULL
  1003. Return: NULL
  1004. Others: NULL
  1005. *************************************************/
  1006. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1007. {
  1008. beep_init();
  1009. pwm_on(2,50000);
  1010. rt_thread_delay(delay*1000);
  1011. pwm_off(2,0);
  1012. rt_thread_delay(1000);
  1013. pwm_on(3, 50000);
  1014. rt_thread_delay(delay*1000);
  1015. pwm_off(3,0);
  1016. device_status.fsl_sw = 0;
  1017. }
  1018. //1.0转仓
  1019. void turnzp(void)
  1020. {
  1021. static uint16_t turn_timeout = 0;
  1022. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1023. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1024. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1025. // rt_pin_write(CLEAR_R,OFF);
  1026. // rt_pin_write(CLEAR_L,OFF);
  1027. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1028. {
  1029. rt_thread_delay(20);
  1030. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1031. {
  1032. // rt_pin_write(CLEAR_R,ON);
  1033. // rt_pin_write(CLEAR_L,OFF);
  1034. Motor_Juandai(ON);
  1035. rt_thread_delay(50);
  1036. turn_timeout++;
  1037. if(turn_timeout==600)
  1038. {
  1039. LOG_E("检测不到限位,电机强制停止");
  1040. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  1041. DTU_Write("%s",ctr_zp_limit_warn);
  1042. rt_free(ctr_zp_limit_warn);
  1043. break;
  1044. }
  1045. }
  1046. break;
  1047. }
  1048. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1049. {
  1050. rt_thread_delay(20);
  1051. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  1052. {
  1053. // rt_pin_write(CLEAR_R,ON);
  1054. // rt_pin_write(CLEAR_L,OFF);
  1055. Motor_Juandai(ON);
  1056. rt_thread_delay(50);
  1057. turn_timeout++;
  1058. if(turn_timeout==600)
  1059. {
  1060. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1061. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  1062. DTU_Write("%s",ctr_zp_limit_warn);
  1063. rt_free(ctr_zp_limit_warn);
  1064. break;
  1065. }
  1066. }
  1067. break;
  1068. }
  1069. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1070. {
  1071. rt_thread_delay(20);
  1072. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  1073. {
  1074. // rt_pin_write(CLEAR_R,OFF);
  1075. // rt_pin_write(CLEAR_L,OFF);
  1076. Motor_Juandai(OFF);
  1077. break;
  1078. }
  1079. else {
  1080. break;
  1081. }
  1082. }
  1083. Motor_Juandai(OFF);
  1084. // rt_pin_write(CLEAR_R,OFF);
  1085. // rt_pin_write(CLEAR_L,OFF);
  1086. turn_timeout = 0;
  1087. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1088. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1089. rt_thread_delay(1000);
  1090. }
  1091. //清扫-常闭型限位开关
  1092. //新结构使用推杆电机做清扫
  1093. uint8_t ctr_clear(uint16_t status_clear)
  1094. {
  1095. static uint16_t clear_num = 0;
  1096. #if PWM_EN == 0
  1097. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1098. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1099. #endif
  1100. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  1101. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1102. /*************************************************************外接清扫
  1103. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1104. rt_pin_write(POWER_CLEAR,ON);
  1105. rt_thread_delay(1000*100);
  1106. rt_pin_write(POWER_CLEAR,OFF);
  1107. ************************************************************/
  1108. //停止
  1109. #if PWM_EN == 0
  1110. rt_pin_write(CLEAR_R,OFF);
  1111. rt_pin_write(CLEAR_L,OFF);
  1112. #else
  1113. Motor_Qingsao_R_PWM(OFF);
  1114. Motor_Qingsao_L_PWM(OFF);
  1115. #endif
  1116. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  1117. rt_thread_delay(100);
  1118. //如果清扫位置在中间
  1119. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  1120. {
  1121. #if PWM_EN == 0
  1122. rt_pin_write(CLEAR_R,ON);
  1123. rt_pin_write(CLEAR_L,OFF);
  1124. #else
  1125. Motor_Qingsao_R_PWM(ON);
  1126. Motor_Qingsao_L_PWM(OFF);
  1127. #endif
  1128. rt_thread_delay(50);
  1129. clear_num++;
  1130. //已到达检测位置
  1131. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1132. {
  1133. #if PWM_EN == 0
  1134. rt_pin_write(CLEAR_R,ON);
  1135. rt_pin_write(CLEAR_L,ON);
  1136. #else
  1137. Motor_Qingsao_R_PWM(OFF);
  1138. Motor_Qingsao_L_PWM(OFF);
  1139. #endif
  1140. rt_thread_delay(2000);
  1141. break;
  1142. }
  1143. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1144. {
  1145. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1146. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1147. #if PWM_EN == 0
  1148. rt_pin_write(CLEAR_R,ON);
  1149. rt_pin_write(CLEAR_L,ON);
  1150. #else
  1151. Motor_Qingsao_R_PWM(OFF);
  1152. Motor_Qingsao_L_PWM(OFF);
  1153. #endif
  1154. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1155. if(Read_Real_Voltage()/100<12)
  1156. {
  1157. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1158. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1159. device_status.clear_status = 0;
  1160. return 0;
  1161. }
  1162. break;
  1163. }
  1164. }
  1165. clear_num = 0;
  1166. //如果清扫位置在左侧
  1167. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  1168. {
  1169. #if PWM_EN == 0
  1170. rt_pin_write(CLEAR_R,ON);
  1171. rt_pin_write(CLEAR_L,OFF);
  1172. #else
  1173. Motor_Qingsao_R_PWM(ON);
  1174. Motor_Qingsao_L_PWM(OFF);
  1175. #endif
  1176. rt_thread_delay(50);
  1177. clear_num++;
  1178. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1179. {
  1180. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1181. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1182. #if PWM_EN == 0
  1183. rt_pin_write(CLEAR_R,ON);
  1184. rt_pin_write(CLEAR_L,ON);
  1185. #else
  1186. Motor_Qingsao_R_PWM(OFF);
  1187. Motor_Qingsao_L_PWM(OFF);
  1188. #endif
  1189. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  1190. if(Read_Real_Voltage()/100<12)
  1191. {
  1192. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1193. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1194. device_status.clear_status = 0;
  1195. return 0;
  1196. }
  1197. break;
  1198. }
  1199. }
  1200. clear_num = 0;
  1201. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  1202. {
  1203. #if PWM_EN == 0
  1204. rt_pin_write(CLEAR_R,ON);
  1205. rt_pin_write(CLEAR_L,ON);
  1206. #else
  1207. Motor_Qingsao_R_PWM(OFF);
  1208. Motor_Qingsao_L_PWM(OFF);
  1209. #endif
  1210. if(Read_Real_Voltage()/100<12)
  1211. {
  1212. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1213. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1214. device_status.clear_status = 0;
  1215. return 0;
  1216. }
  1217. rt_thread_delay(2000);
  1218. break;
  1219. }
  1220. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1221. {
  1222. #if PWM_EN == 0
  1223. rt_pin_write(CLEAR_R,OFF);
  1224. rt_pin_write(CLEAR_L,ON);
  1225. #else
  1226. Motor_Qingsao_R_PWM(OFF);
  1227. Motor_Qingsao_L_PWM(ON);
  1228. #endif
  1229. rt_thread_delay(50);
  1230. clear_num++;
  1231. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1232. {
  1233. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1234. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1235. //DTU_Write("%s",Clear_ERR);
  1236. #if PWM_EN == 0
  1237. rt_pin_write(CLEAR_R,ON);
  1238. rt_pin_write(CLEAR_L,ON);
  1239. #else
  1240. Motor_Qingsao_R_PWM(OFF);
  1241. Motor_Qingsao_L_PWM(OFF);
  1242. #endif
  1243. rt_free(Clear_ERR);
  1244. if(Read_Real_Voltage()/100<12)
  1245. {
  1246. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1247. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1248. device_status.clear_status = 0;
  1249. return 0;
  1250. }
  1251. break;
  1252. }
  1253. }
  1254. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1255. {
  1256. #if PWM_EN == 0
  1257. rt_pin_write(CLEAR_R,ON);
  1258. rt_pin_write(CLEAR_L,ON);
  1259. #else
  1260. Motor_Qingsao_R_PWM(OFF);
  1261. Motor_Qingsao_L_PWM(OFF);
  1262. #endif
  1263. rt_thread_delay(2000);
  1264. break;
  1265. }
  1266. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1267. {
  1268. #if PWM_EN == 0
  1269. rt_pin_write(CLEAR_R,ON);
  1270. rt_pin_write(CLEAR_L,ON);
  1271. #else
  1272. Motor_Qingsao_R_PWM(OFF);
  1273. Motor_Qingsao_L_PWM(OFF);
  1274. #endif
  1275. if(device_status.version_type==41)
  1276. {
  1277. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1278. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1279. //DTU_Write("%s",Clear_ERR);
  1280. rt_free(Clear_ERR);
  1281. }
  1282. return 0;
  1283. }
  1284. clear_num = 0;
  1285. rt_thread_delay(2000);
  1286. #if PWM_EN == 0
  1287. rt_pin_write(CLEAR_R,ON);
  1288. rt_pin_write(CLEAR_L,ON);
  1289. #else
  1290. Motor_Qingsao_R_PWM(OFF);
  1291. Motor_Qingsao_L_PWM(OFF);
  1292. #endif
  1293. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1294. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1295. device_status.clear_status = 0;
  1296. return 1;
  1297. }
  1298. //路由器供电
  1299. void ctr_router(uint8_t status_router)
  1300. {
  1301. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1302. if(device_status.version_type==6)
  1303. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1304. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1305. if(status_router==ON)
  1306. {
  1307. rt_pin_write(ROUTER,ON);
  1308. //
  1309. #if LAMP_EN == 0
  1310. if(device_status.version_type==6)
  1311. rt_pin_write(ROUTER_P,ON);
  1312. #endif
  1313. }
  1314. else {
  1315. rt_pin_write(ROUTER,OFF);
  1316. //
  1317. #if LAMP_EN == 0
  1318. if(device_status.version_type==6)
  1319. rt_pin_write(ROUTER_P,OFF);
  1320. #endif
  1321. }
  1322. }
  1323. //清扫-推杆电机
  1324. void ctr_TG_Clear(uint16_t clear_timeout)
  1325. {
  1326. #if 0
  1327. int current_clear[200];
  1328. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1329. LOG_I("清扫电机待机:%d",Electric_current);
  1330. #endif
  1331. static uint16_t clear_count = 0;
  1332. clear_count = clear_timeout;
  1333. #if PWM_EN == 0
  1334. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1335. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1336. #endif
  1337. while(clear_count!=0)
  1338. {
  1339. #if PWM_EN == 0
  1340. rt_pin_write(CLEAR_R,ON);
  1341. rt_pin_write(CLEAR_L,OFF);
  1342. #else
  1343. Motor_Qingsao_R_PWM(ON);
  1344. Motor_Qingsao_L_PWM(OFF);
  1345. #endif
  1346. clear_count--;
  1347. rt_thread_delay(100);
  1348. }
  1349. #if PWM_EN == 0
  1350. rt_pin_write(CLEAR_R,ON);
  1351. rt_pin_write(CLEAR_L,ON);
  1352. #else
  1353. Motor_Qingsao_R_PWM(OFF);
  1354. Motor_Qingsao_L_PWM(OFF);
  1355. #endif
  1356. rt_thread_delay(3000);
  1357. clear_count = 0;
  1358. while(clear_timeout!=clear_count)
  1359. {
  1360. #if PWM_EN == 0
  1361. rt_pin_write(CLEAR_R,OFF);
  1362. rt_pin_write(CLEAR_L,ON);
  1363. #else
  1364. Motor_Qingsao_R_PWM(OFF);
  1365. Motor_Qingsao_L_PWM(ON);
  1366. #endif
  1367. // if(clear_count<200)
  1368. // current_clear[clear_count] = atoi(device_status.power);
  1369. clear_count++;
  1370. rt_thread_delay(100);
  1371. }
  1372. #if PWM_EN == 0
  1373. rt_pin_write(CLEAR_R,ON);
  1374. rt_pin_write(CLEAR_L,ON);
  1375. #else
  1376. Motor_Qingsao_R_PWM(OFF);
  1377. Motor_Qingsao_L_PWM(OFF);
  1378. #endif
  1379. #if 0
  1380. bubblesort(current_clear,200);
  1381. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1382. {
  1383. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1384. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1385. DTU_Write("%s",ctr_clear_warn);
  1386. rt_free(ctr_clear_warn);
  1387. }
  1388. #endif
  1389. rt_thread_delay(2000);
  1390. device_status.clear_status = 0;
  1391. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1392. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1393. }
  1394. //DTU,RTU供电
  1395. void ctr_RTU_DTU(uint8_t status_rtu)
  1396. {
  1397. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1398. if(status_rtu==ON)
  1399. {
  1400. rt_pin_write(RTU_DTU,ON);
  1401. }
  1402. else
  1403. {
  1404. LOG_W("RTU电源复位!!!");
  1405. rt_pin_write(RTU_DTU,OFF);
  1406. }
  1407. }
  1408. //海康拍照
  1409. void Hk_Take_io(void)
  1410. {
  1411. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1412. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1413. rt_pin_write(HK_TAKE, 1);
  1414. rt_pin_write(HK_IO, 0);
  1415. if(device_status.dtype==3)
  1416. Beep_Tip(1000);
  1417. if(device_status.dtype==7)
  1418. rt_thread_mdelay(1000);
  1419. rt_pin_write(HK_TAKE, 0);
  1420. rt_pin_write(HK_IO, 1);
  1421. }
  1422. void calorstat(uint16_t heat_time)
  1423. {
  1424. //uint8_t heat_up_cnt = 0;
  1425. uint16_t n = 0;
  1426. uint16_t delay_heat = 60;
  1427. FlagStatus heat_flg = RESET;
  1428. device_status.heat_time_min = 0;
  1429. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1430. ctr_Heat(ON);
  1431. // UpLoad_Data(); //开始加热时的第一条数据
  1432. //恒温模式
  1433. for(n=0;n < heat_time;n++)
  1434. {
  1435. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1436. {
  1437. heat_flg = SET;
  1438. ctr_Heat(OFF);
  1439. while(delay_heat!=0)
  1440. {
  1441. delay_heat --;
  1442. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1443. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1444. rt_thread_delay(1000);
  1445. if(device_status.heattem>device_status.run_sta + 1){
  1446. device_status.run_sta = device_status.heattem;
  1447. }
  1448. }
  1449. delay_heat = 60;
  1450. //跳出FOR循环
  1451. n = n + 60;
  1452. }
  1453. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1454. {
  1455. device_status.run_sta = device_status.heattem;
  1456. ctr_Heat(ON);
  1457. }
  1458. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1459. {
  1460. // if(heat_up_cnt==0)
  1461. // {
  1462. // UpLoad_Data(); //加热到设定温度的数据
  1463. // heat_up_cnt = 1;
  1464. // }
  1465. device_status.run_sta = device_status.heattem;
  1466. ctr_Heat(OFF);
  1467. }
  1468. if((n % 60)==0)
  1469. {
  1470. device_status.heat_time_min = device_status.heat_time_min + 1;
  1471. // if(device_status.heat_time_min % 5 == 0)
  1472. // {
  1473. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1474. // }
  1475. }
  1476. //如果触发异常退出加热
  1477. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1478. {
  1479. rt_thread_delay(1000);
  1480. if(device_status.bat_status == 1)
  1481. {
  1482. rt_kprintf("============================触发异常退出加热==========================");
  1483. ctr_lamp(OFF,1);
  1484. ctr_Heat(OFF);
  1485. break;
  1486. }
  1487. }
  1488. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1489. {
  1490. ctr_Heat(OFF);
  1491. rt_kprintf("异常退出!!!\n");
  1492. break;
  1493. }
  1494. rt_thread_delay(1000);
  1495. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1496. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1497. }
  1498. ctr_Heat(OFF);
  1499. //UpLoad_Data(); //加热结束的数据
  1500. }
  1501. //初始化所有控制驱动,关闭
  1502. void HardWare_Init(uint8_t boot)
  1503. {
  1504. Beep_Tip(200);
  1505. //关闭上仓门
  1506. ctr_cangmen_up(OFF);
  1507. if(device_status.dtype==3){
  1508. //关闭下仓门电机
  1509. ctr_cangmen_down(OFF);
  1510. }
  1511. //关闭镇流器
  1512. if(device_status.autotakepic==0)
  1513. ctr_lamp(OFF,1);
  1514. //关闭震动电机
  1515. ctr_shake(OFF,0);
  1516. //关闭加热
  1517. ctr_Heat(OFF);
  1518. //关闭补光灯
  1519. ctr_led(OFF);
  1520. //关闭12V供电(海康相机,RTU)
  1521. ctr_photo(OFF);
  1522. }
  1523. //拍照
  1524. void HK_TakePhoto(void)
  1525. {
  1526. //打开补光灯
  1527. DTU_Write("takephoto.mp3");
  1528. rt_thread_delay(1000);
  1529. char *takephotojson = makeJson_takephoto();
  1530. DTU_Write("%s",takephotojson);
  1531. rt_free(takephotojson);
  1532. //吸虫塔
  1533. if(device_status.version_type!=6&&device_status.version_type!=8)
  1534. {
  1535. ctr_led(ON);
  1536. if(device_status.version_type==48)
  1537. {
  1538. rt_thread_delay(1000*60);
  1539. }
  1540. else {
  1541. rt_thread_delay(1000*15);
  1542. }
  1543. }
  1544. //触发海康相机拍照
  1545. Hk_Take_io();
  1546. if(device_status.version_type!=6)
  1547. {
  1548. rt_thread_delay(1000*45);
  1549. }
  1550. else {
  1551. ctr_led(ON);
  1552. rt_thread_delay(1000*19);
  1553. }
  1554. ctr_led(OFF);
  1555. }
  1556. //光控测试任务
  1557. void SunTest(void)
  1558. {
  1559. rt_uint16_t ui_timeout_l = 0;
  1560. //未检测到光照,一秒扫描等待,超时30S
  1561. while(Read_Light_Voltage()<500)
  1562. {
  1563. ui_timeout_l ++;
  1564. rt_thread_delay(1000);
  1565. if(ui_timeout_l==30) break;
  1566. }
  1567. rt_thread_delay(3000);
  1568. if(Read_Light_Voltage()<500)
  1569. {
  1570. device_status.sun_status = 0;//光控状态
  1571. }
  1572. if(Read_Light_Voltage()>500)
  1573. {
  1574. rt_thread_delay(1000);
  1575. }
  1576. rt_thread_delay(3000);
  1577. if(Read_Light_Voltage()>500)
  1578. {
  1579. device_status.sun_status = 1;
  1580. ctr_lamp(OFF,1);
  1581. }
  1582. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1583. device_status.sun_test = 0;
  1584. device_status.work_status=0;
  1585. }
  1586. /*上电初始化*/
  1587. void ResetInit(void)
  1588. {
  1589. if(device_status.dtype==7)
  1590. {
  1591. ctr_Motor_Driver(0, 0, 0, 0);
  1592. //关闭电磁阀
  1593. ctr_shake(OFF, 0);
  1594. //关闭加热
  1595. ctr_Heat(OFF);
  1596. //关闭制冷
  1597. ctr_Cold(OFF);
  1598. //关闭补光灯
  1599. ctr_led(OFF);
  1600. //转盘复位
  1601. if(device_status.run_sta==0)
  1602. ctr_turn(0, 0);
  1603. }
  1604. if(device_status.version_type!=6&&device_status.version_type!=7&&device_status.version_type!=8)
  1605. {
  1606. device_status.work_done = 1;
  1607. //1.打开路由器供电
  1608. if(device_status.power_status==1)
  1609. {
  1610. ctr_router(ON);
  1611. ctr_RTU_DTU(ON);
  1612. //2.打开相机供电
  1613. ctr_photo(ON);
  1614. }
  1615. //3.关闭镇流器
  1616. ctr_lamp(OFF,1);
  1617. #if LAMP_EN == 1
  1618. ctr_lamp(OFF,2);
  1619. ctr_lamp(OFF,3);
  1620. #endif
  1621. //4.关闭震动电机
  1622. ctr_shake(OFF, 0);
  1623. //5.关闭加热
  1624. ctr_Heat(OFF);
  1625. //6.关闭补光灯
  1626. ctr_led(OFF);
  1627. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1628. {
  1629. LOG_W("欠压!");
  1630. return;
  1631. }
  1632. //7.关闭上仓门
  1633. device_status.ctr_up_motor = 2;
  1634. // while(device_status.ctr_up_motor!=0)
  1635. // {
  1636. // LOG_I("关闭上仓门...");
  1637. // rt_thread_delay(100);
  1638. // }
  1639. //8.关闭下仓门
  1640. device_status.ctr_down_motor = 2;
  1641. while(device_status.ctr_down_motor!=0)
  1642. {
  1643. LOG_I("关闭下仓门....");
  1644. rt_thread_delay(100);
  1645. }
  1646. //9.清扫
  1647. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1648. {
  1649. LOG_W("异常退出!!!\n");
  1650. return;
  1651. }
  1652. #if LAMP_EN == 0
  1653. ctr_Fan_PUT(OFF);
  1654. ctr_Fan_IN(OFF);
  1655. #endif
  1656. if(device_status.version_type==4||device_status.version_type==41)
  1657. {
  1658. device_status.clear_status=1;
  1659. }
  1660. rt_thread_delay(1000);
  1661. }
  1662. else {
  1663. if(device_status.power_status==1)
  1664. {
  1665. //1.打开路由器供电
  1666. ctr_router(ON);
  1667. ctr_RTU_DTU(ON);
  1668. //2.打开相机供电
  1669. ctr_photo(ON);
  1670. }
  1671. //5.关闭加热
  1672. //6.关闭补光灯
  1673. ctr_led(OFF);
  1674. }
  1675. }
  1676. /*三色光源*/
  1677. void Led_Check(uint8_t tp)
  1678. {
  1679. if(tp==3)
  1680. {
  1681. if(device_status.light_over_tim>2*3600)
  1682. {
  1683. device_status.work_status = 1;
  1684. device_status.lamp_status = 1;
  1685. device_status.lamp1_status = 0;
  1686. device_status.lamp2_status = 0;
  1687. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1688. }
  1689. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1690. device_status.work_status = 1;
  1691. device_status.lamp_status = 0;
  1692. device_status.lamp1_status = 1;
  1693. device_status.lamp2_status = 0;
  1694. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1695. }
  1696. else if (device_status.light_over_tim<1*3600) {
  1697. device_status.work_status = 1;
  1698. device_status.lamp_status = 0;
  1699. device_status.lamp1_status = 0;
  1700. device_status.lamp2_status = 1;
  1701. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1702. }
  1703. }
  1704. else if (tp==6) {
  1705. if(device_status.light_over_tim>4*3600)
  1706. {
  1707. device_status.work_status = 1;
  1708. device_status.lamp_status = 1;
  1709. device_status.lamp1_status = 0;
  1710. device_status.lamp2_status = 0;
  1711. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1712. }
  1713. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1714. device_status.work_status = 1;
  1715. device_status.lamp_status = 0;
  1716. device_status.lamp1_status = 1;
  1717. device_status.lamp2_status = 0;
  1718. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1719. }
  1720. else if (device_status.light_over_tim<7200) {
  1721. device_status.work_status = 1;
  1722. device_status.lamp_status = 0;
  1723. device_status.lamp1_status = 0;
  1724. device_status.lamp2_status = 1;
  1725. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1726. }
  1727. }
  1728. else if (tp==9) {
  1729. if(device_status.light_over_tim>6*3600)
  1730. {
  1731. device_status.work_status = 1;
  1732. device_status.lamp_status = 1;
  1733. device_status.lamp1_status = 0;
  1734. device_status.lamp2_status = 0;
  1735. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1736. }
  1737. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1738. device_status.work_status = 1;
  1739. device_status.lamp_status = 0;
  1740. device_status.lamp1_status = 1;
  1741. device_status.lamp2_status = 0;
  1742. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1743. }
  1744. else if (device_status.light_over_tim<3*3600) {
  1745. device_status.work_status = 1;
  1746. device_status.lamp_status = 0;
  1747. device_status.lamp1_status = 0;
  1748. device_status.lamp2_status = 1;
  1749. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1750. }
  1751. }
  1752. }
  1753. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1754. void HardWare_Check(uint8_t boot)
  1755. {
  1756. if(boot==0&&device_status.version_type!=6)
  1757. {
  1758. //停止所有动作 -完成
  1759. HardWare_Init(boot);
  1760. }
  1761. else {
  1762. //停止所有动作
  1763. //读取记录待机功耗(实测大概70mA)
  1764. //(需减去屏幕的功耗大概120mA)
  1765. //动作上仓门,读取功率值保存
  1766. //动作下仓门,读取功率值保存
  1767. //动作清扫,读取功率值保存
  1768. //打开镇流器,读取功率值保存
  1769. //打开海康相机供电,读取功率值保存
  1770. //打开补光灯,读取功率值保存
  1771. //打开路由器/RTU供电,读取功率值保存
  1772. //打开加热,读取功率值保存
  1773. }
  1774. }
  1775. //工作状态检测
  1776. void WorkCheck(void)
  1777. {
  1778. //雨控检测,温控检测,电池电压检测
  1779. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1780. {
  1781. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1782. if(device_status.timeSwitch==1)
  1783. {
  1784. LOG_I("***********时控*****************");
  1785. if(device_status.startBar==device_status.stopBar){
  1786. LOG_I("全天工作!");
  1787. device_status.work_status = 1;
  1788. }
  1789. //如果开始和结束是同一天
  1790. if(device_status.startBar<device_status.stopBar)
  1791. {
  1792. LOG_I("开始和结束是同一天");
  1793. time_t now;
  1794. time(&now);
  1795. static struct tm nowtime; //现在
  1796. static struct tm starttime; //开始
  1797. static struct tm endtime; //结束
  1798. //获取当前时间戳
  1799. nowtime =*localtime(&now);
  1800. nowtime.tm_hour = nowtime.tm_hour-8;
  1801. //获取定时开始的时间戳
  1802. starttime = *localtime(&now);
  1803. starttime.tm_hour = device_status.startBar-8;
  1804. starttime.tm_min = 0;
  1805. starttime.tm_sec = 0;
  1806. //获取定时结束的时间戳
  1807. endtime = *localtime(&now);
  1808. endtime.tm_hour = device_status.stopBar-8;
  1809. endtime.tm_min = 0;
  1810. endtime.tm_sec = 0;
  1811. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1812. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1813. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1814. {
  1815. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1816. device_status.work_status = 1;
  1817. }
  1818. //如果当前时间小于开始时间
  1819. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1820. {
  1821. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1822. device_status.work_status = 0;
  1823. }
  1824. //当前时间大于结束时间
  1825. else {
  1826. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1827. device_status.work_status = 0;
  1828. }
  1829. #if LAMP_EN == 1
  1830. //如果当前时间超过20点,未到21点
  1831. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1832. {
  1833. LOG_I("3led:20~21");
  1834. device_status.lamp_status = 1;
  1835. device_status.lamp1_status = 0;
  1836. device_status.lamp2_status = 0;
  1837. }
  1838. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1839. LOG_I("3led:21~22");
  1840. device_status.lamp1_status = 1;
  1841. device_status.lamp_status = 0;
  1842. device_status.lamp2_status = 0;
  1843. }
  1844. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1845. LOG_I("3led:22~23");
  1846. device_status.lamp2_status = 1;
  1847. device_status.lamp_status = 0;
  1848. device_status.lamp1_status =0;
  1849. }
  1850. #endif
  1851. }
  1852. //如果结束时间是第二天
  1853. else if (device_status.startBar>device_status.stopBar) {
  1854. time_t now;
  1855. time(&now);
  1856. static struct tm nowtime; //现在
  1857. static struct tm starttime; //开始
  1858. static struct tm endtime; //结束
  1859. //获取当前时间戳
  1860. nowtime =*localtime(&now);
  1861. nowtime.tm_hour = nowtime.tm_hour-8;
  1862. //获取定时开始的时间戳
  1863. starttime = *localtime(&now);
  1864. starttime.tm_hour = device_status.startBar-8;
  1865. starttime.tm_min = 0;
  1866. starttime.tm_sec = 0;
  1867. //获取定时结束的时间戳
  1868. endtime = *localtime(&now);
  1869. endtime.tm_hour = device_status.stopBar-8;
  1870. endtime.tm_min = 0;
  1871. endtime.tm_sec = 0;
  1872. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1873. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1874. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1875. {
  1876. //获取定时结束的时间戳
  1877. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1878. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1879. endtime.tm_min = 0;
  1880. endtime.tm_sec = 0;
  1881. device_status.work_status = 1;
  1882. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1883. }
  1884. //当前时间小于开始时间
  1885. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1886. {
  1887. //获取定时结束的时间戳
  1888. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1889. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1890. endtime.tm_min = 0;
  1891. endtime.tm_sec = 0;
  1892. //如果当前时间戳小于结束时间戳
  1893. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1894. {
  1895. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1896. device_status.work_status = 1;
  1897. }
  1898. else
  1899. {
  1900. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1901. device_status.work_status = 0; //不在工作时间,待机
  1902. }
  1903. }
  1904. }
  1905. }
  1906. else {
  1907. LOG_I("光控定时模式!");
  1908. /*
  1909. time_t now;
  1910. time(&now);
  1911. static struct tm nowstamp;
  1912. nowstamp =*localtime(&now);
  1913. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1914. static rt_uint32_t nowstamp_num = 0;
  1915. nowstamp_num = mktime(&nowstamp);
  1916. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1917. */
  1918. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1919. {
  1920. device_status.light_over_tim--;
  1921. #if LAMP_EN == 1
  1922. Led_Check(device_status.sunBar);
  1923. #else
  1924. device_status.work_status = 1;;
  1925. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1926. #endif
  1927. }
  1928. else
  1929. {
  1930. if(device_status.test_flag==0)
  1931. {
  1932. ctr_lamp(OFF,1);
  1933. #if LAMP_EN == 1
  1934. ctr_lamp(OFF,2);
  1935. ctr_lamp(OFF,3);
  1936. #endif
  1937. }
  1938. device_status.work_status = 0;
  1939. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1940. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1941. }
  1942. }
  1943. rt_thread_delay(1000);
  1944. }
  1945. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1946. device_status.work_status = 0;
  1947. rt_thread_delay(1000);
  1948. }
  1949. void WorkTask_BZY(void)
  1950. {
  1951. if(device_status.ds==1)
  1952. {
  1953. //时间段判断
  1954. //先判断第一个时间段
  1955. time_t now;
  1956. time(&now);
  1957. static struct tm now_stamp; //现在
  1958. static struct tm cul_sh1_stamp; //开始1
  1959. static struct tm cul_eh1_stamp; //结束1
  1960. static struct tm cul_sh2_stamp; //开始2
  1961. static struct tm cul_eh2_stamp; //结束2
  1962. static struct tm cul_sh3_stamp; //开始3
  1963. static struct tm cul_eh3_stamp; //结束3
  1964. now_stamp = *localtime(&now);
  1965. //获取定时开始的时间戳
  1966. cul_sh1_stamp = *localtime(&now);
  1967. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  1968. cul_sh1_stamp.tm_min = 0;
  1969. cul_sh1_stamp.tm_sec = 0;
  1970. cul_sh2_stamp = *localtime(&now);
  1971. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  1972. cul_sh2_stamp.tm_min = 0;
  1973. cul_sh2_stamp.tm_sec = 0;
  1974. cul_sh3_stamp = *localtime(&now);
  1975. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  1976. cul_sh3_stamp.tm_min = 0;
  1977. cul_sh3_stamp.tm_sec = 0;
  1978. //获取定时结束的时间戳
  1979. cul_eh1_stamp = *localtime(&now);
  1980. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  1981. cul_eh1_stamp.tm_min = 0;
  1982. cul_eh1_stamp.tm_sec = 0;
  1983. cul_eh2_stamp = *localtime(&now);
  1984. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  1985. cul_eh2_stamp.tm_min = 0;
  1986. cul_eh2_stamp.tm_sec = 0;
  1987. cul_eh3_stamp = *localtime(&now);
  1988. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  1989. cul_eh3_stamp.tm_min = 0;
  1990. cul_eh3_stamp.tm_sec = 0;
  1991. 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);
  1992. 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);
  1993. 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);
  1994. 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);
  1995. 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);
  1996. 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);
  1997. //此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  1998. //当前时间戳小于第一段开始时间戳
  1999. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2000. {
  2001. time_t now;
  2002. time(&now);
  2003. cul_sh1_stamp = *localtime(&now);
  2004. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2005. cul_sh1_stamp.tm_min = 0;
  2006. cul_sh1_stamp.tm_sec = 0;
  2007. now_stamp = *localtime(&now);
  2008. LOG_I("======收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.",cul_sh1_stamp.tm_year+1900,cul_sh1_stamp.tm_mon+1,cul_sh1_stamp.tm_mday,cul_sh1_stamp.tm_hour,cul_sh1_stamp.tm_min,cul_sh1_stamp.tm_sec);
  2009. LOG_I("======未到收集时间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);
  2010. LOG_D("run_sta:%d",device_status.run_sta);
  2011. }
  2012. //当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2013. if(difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0&&difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2014. {
  2015. time_t now;
  2016. time(&now);
  2017. cul_sh2_stamp = *localtime(&now);
  2018. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2019. cul_sh2_stamp.tm_min = 0;
  2020. cul_sh2_stamp.tm_sec = 0;
  2021. now_stamp = *localtime(&now);
  2022. 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);
  2023. 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);
  2024. LOG_D("run_sta:%d",device_status.run_sta);
  2025. }
  2026. //如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2027. if(difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0&&difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2028. {
  2029. //转盘到涂凡士林位置
  2030. if(device_status.run_sta==0)
  2031. {
  2032. LOG_I("start to collect!!!");
  2033. DTU_Write("collect.mp3");
  2034. ctr_turn(90,4);
  2035. rt_thread_delay(2000);
  2036. //涂凡士林
  2037. ctr_Petroleum_jelly(10);
  2038. rt_thread_delay(5000);
  2039. }
  2040. /*转盘到采集位置*/
  2041. if(device_status.run_sta==4)
  2042. ctr_turn(90,1);
  2043. //只有开始和结束时刻不同的时间段,才有效
  2044. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)||(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2045. {
  2046. if((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)&& difftime(mktime(&now_stamp),mktime(&cul_sh1_stamp)) > 0)
  2047. {
  2048. device_status.work_status = 1;
  2049. ctr_Collect_Fan(ON);
  2050. //当前时间戳小于第一段结束时间戳
  2051. while(difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp))>0)
  2052. {
  2053. time(&now);
  2054. now_stamp = *localtime(&now);
  2055. cul_eh1_stamp = *localtime(&now);
  2056. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2057. cul_eh1_stamp.tm_min = 0;
  2058. cul_eh1_stamp.tm_sec = 0;
  2059. if(device_status.run_sta!=1) break;
  2060. LOG_I("1===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh1_stamp));
  2061. rt_thread_mdelay(10000);
  2062. }
  2063. }
  2064. else {
  2065. device_status.work_status=0;
  2066. ctr_Collect_Fan(OFF);
  2067. LOG_I("未到收集1时间!");
  2068. }
  2069. if(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp),mktime(&cul_sh2_stamp)) > 0)
  2070. {
  2071. device_status.work_status = 1;
  2072. ctr_Collect_Fan(ON);
  2073. while(difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp))>0)
  2074. {
  2075. time(&now);
  2076. now_stamp = *localtime(&now);
  2077. cul_eh2_stamp = *localtime(&now);
  2078. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2079. cul_eh2_stamp.tm_min = 0;
  2080. cul_eh2_stamp.tm_sec = 0;
  2081. if(device_status.run_sta!=1) break;
  2082. LOG_I("2===========采集风机启动,正在收集.....%d-%d",mktime(&now_stamp),mktime(&cul_eh2_stamp));
  2083. rt_thread_mdelay(10000);
  2084. }
  2085. }
  2086. else {
  2087. device_status.work_status=0;
  2088. ctr_Collect_Fan(OFF);
  2089. LOG_I("未到收集2时间!");
  2090. }
  2091. }
  2092. else
  2093. {
  2094. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2095. }
  2096. }
  2097. if(device_status.run_sta==2)
  2098. {
  2099. ctr_Collect_Fan(OFF);
  2100. device_status.develop_flag = 1;
  2101. device_status.work_status = 2;
  2102. ctr_develop(); //培养
  2103. ctr_turn(90,3);
  2104. }
  2105. if(device_status.run_sta==3)
  2106. {
  2107. ctr_Collect_Fan(OFF);
  2108. device_status.work_status = 3;
  2109. device_status.autotakepic=1; //对焦拍照
  2110. }
  2111. //在培养之前再次确认时间戳
  2112. time(&now);
  2113. now_stamp = *localtime(&now);
  2114. cul_eh2_stamp = *localtime(&now);
  2115. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2116. cul_eh2_stamp.tm_min = 0;
  2117. cul_eh2_stamp.tm_sec = 0;
  2118. //当前时间戳大于收集结束时间戳并且run_sta==1
  2119. if(device_status.run_sta==1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2120. {
  2121. ctr_Collect_Fan(OFF);
  2122. LOG_I("===============collect ok,turn to drop!!!");
  2123. device_status.develop_flag=1;
  2124. device_status.work_status=2; //现在是培养
  2125. DTU_Write("develop.mp3");
  2126. if(device_status.run_sta==1)
  2127. ctr_turn(90,2); /*转盘到培养位置*/
  2128. rt_thread_delay(1000);
  2129. ctr_shake(ON,device_status.shake_sec);
  2130. rt_thread_delay(1000);
  2131. ctr_develop(); //培养
  2132. if(device_status.run_sta==2)
  2133. ctr_turn(90,3); /*转盘到拍照位置*/
  2134. rt_thread_delay(1000);
  2135. device_status.work_status=3; //现在是拍照
  2136. device_status.autotakepic=1; //对焦拍照
  2137. while(device_status.autotakepic==0)
  2138. {
  2139. rt_thread_delay(1000);
  2140. LOG_I("正在对焦拍照............");
  2141. }
  2142. device_status.work_status = 0; //待机
  2143. DTU_Write("wait.mp3");
  2144. LOG_I("=====================work finished==================");
  2145. }
  2146. rt_thread_delay(1000);
  2147. }
  2148. device_status.work_status = 0; //待机
  2149. LOG_W("==================!!!设备已待机!!!===================");
  2150. }
  2151. //工作任务流程s
  2152. void WorkTask(void)
  2153. {
  2154. static int clt_time = 0; //收集时间差秒数
  2155. static int heat_time = 0;
  2156. LOG_I("工作状态:%d",device_status.work_status);
  2157. LOG_I("开关机状态:%d",device_status.ds);
  2158. 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)
  2159. {
  2160. device_status.work_done = 0;
  2161. if(clt_time==0)
  2162. {
  2163. #if 0
  2164. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2165. {
  2166. LOG_W("相机电流不足!");
  2167. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2168. DTU_Write("%s",ctr_hk_p_warn);
  2169. rt_free(ctr_hk_p_warn);
  2170. }
  2171. #endif
  2172. #if LAMP_EN == 0
  2173. ctr_Fan_PUT(ON);
  2174. #endif
  2175. DTU_Write("workstart.mp3");
  2176. }
  2177. //1.打开灯管
  2178. #if LAMP_EN == 1
  2179. if(device_status.lamp_status==1)
  2180. {
  2181. ctr_lamp(ON,1);
  2182. ctr_lamp(OFF,2);
  2183. ctr_lamp(OFF,3);
  2184. }
  2185. else if (device_status.lamp1_status==1) {
  2186. ctr_lamp(OFF,1);
  2187. ctr_lamp(ON,2);
  2188. ctr_lamp(OFF,3);
  2189. }
  2190. else if (device_status.lamp2_status==1) {
  2191. ctr_lamp(OFF,1);
  2192. ctr_lamp(OFF,2);
  2193. ctr_lamp(ON,3);
  2194. }
  2195. #else
  2196. ctr_lamp(ON,1);
  2197. #endif
  2198. LOG_I("打开灯管!!!");
  2199. #if 0
  2200. rt_thread_delay(2000);
  2201. if(atoi(device_status.power)<450)
  2202. {
  2203. LOG_I("Current is:%d",atoi(device_status.power));
  2204. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  2205. {
  2206. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  2207. DTU_Write("%s",lamp_err_warn);
  2208. rt_free(lamp_err_warn);
  2209. }
  2210. }
  2211. #endif
  2212. if(device_status.version_type!=5)
  2213. {
  2214. if(clt_time==0)
  2215. {
  2216. rt_thread_delay(5000); //待灯管启动再往下执行,否则会触发电机过流
  2217. //2.打开上仓门
  2218. rt_kprintf("打开上仓门!!!\n");
  2219. device_status.ctr_up_motor = 1;
  2220. }
  2221. //3.收集
  2222. clt_time = device_status.clt_time * 60;
  2223. while(clt_time!=1)
  2224. {
  2225. #if LAMP_EN == 0
  2226. ctr_Fan_IN(ON);
  2227. #endif
  2228. clt_time --;
  2229. rt_thread_delay(1000);
  2230. #if LAMP_EN == 0
  2231. if(clt_time%30==0)
  2232. ctr_Fan_PUT(OFF);
  2233. #endif
  2234. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  2235. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"",device_status.clt_time,clt_time);
  2236. //如果触发异常
  2237. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2238. {
  2239. ctr_lamp(OFF,1);
  2240. #if LAMP_EN == 1
  2241. ctr_lamp(OFF,2);
  2242. ctr_lamp(OFF,3);
  2243. #endif
  2244. #if LAMP_EN == 0
  2245. ctr_Fan_IN(OFF);
  2246. #endif
  2247. rt_kprintf("异常退出!!!\n");
  2248. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2249. break;
  2250. }
  2251. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2252. {
  2253. ctr_lamp(OFF,1);
  2254. #if LAMP_EN == 1
  2255. ctr_lamp(OFF,2);
  2256. ctr_lamp(OFF,3);
  2257. #endif
  2258. #if LAMP_EN == 0
  2259. ctr_Fan_IN(OFF);
  2260. #endif
  2261. rt_kprintf("异常退出!!!\n");
  2262. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2263. break;
  2264. }
  2265. }
  2266. clt_time = 1;
  2267. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  2268. {
  2269. ctr_lamp(OFF,1);
  2270. #if LAMP_EN == 1
  2271. ctr_lamp(OFF,2);
  2272. ctr_lamp(OFF,3);
  2273. #endif
  2274. #if LAMP_EN == 0
  2275. ctr_Fan_IN(OFF);
  2276. #endif
  2277. rt_kprintf("异常退出!!!\n");
  2278. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2279. break;
  2280. }
  2281. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  2282. {
  2283. ctr_lamp(OFF,1);
  2284. #if LAMP_EN == 1
  2285. ctr_lamp(OFF,2);
  2286. ctr_lamp(OFF,3);
  2287. #endif
  2288. #if LAMP_EN == 0
  2289. ctr_Fan_IN(OFF);
  2290. #endif
  2291. rt_kprintf("异常退出!!!\n");
  2292. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  2293. break;
  2294. }
  2295. #if LAMP_EN == 0
  2296. ctr_Fan_IN(OFF);
  2297. #endif
  2298. //4.关闭上仓门
  2299. device_status.ctr_up_motor = 2;
  2300. rt_kprintf("关闭上仓门!!!\n");
  2301. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  2302. while(device_status.ctr_up_motor!=0)
  2303. {
  2304. rt_thread_delay(100);
  2305. LOG_I("等待上仓门关闭!!!");
  2306. }
  2307. //5.加热
  2308. if(device_status.heat_sw==0)
  2309. {
  2310. calorstat(device_status.heattime*60);
  2311. }
  2312. else {
  2313. heat_time = device_status.heattime * 60;
  2314. while(heat_time!=0)
  2315. {
  2316. heat_time--;
  2317. LOG_I("正在加热状态!!!");
  2318. rt_thread_delay(1000);
  2319. }
  2320. }
  2321. //6.打开下仓门
  2322. if(device_status.rain_status==0)
  2323. {
  2324. device_status.ctr_down_motor =1;
  2325. }
  2326. else {
  2327. device_status.ctr_down_motor = 2;
  2328. device_status.ctr_up_motor = 2;
  2329. }
  2330. //待下仓门完全打开
  2331. while(device_status.ctr_down_motor!=0)
  2332. {
  2333. rt_thread_delay(100);
  2334. rt_kprintf("等待下仓门打开!!!\n");
  2335. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  2336. }
  2337. rt_thread_delay(3000);
  2338. //关闭下仓门
  2339. device_status.ctr_down_motor = 2;
  2340. while(device_status.ctr_down_motor!=0)
  2341. {
  2342. rt_thread_delay(100);
  2343. rt_kprintf("等待下仓门关闭!!!\n");
  2344. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  2345. }
  2346. //2022-04-12add:
  2347. LOG_I("打开上仓门!!!");
  2348. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  2349. device_status.ctr_up_motor = 1;
  2350. if(device_status.version_type==4||device_status.version_type==41)
  2351. {
  2352. //震动
  2353. ctr_shake(ON, device_status.shake_sec);
  2354. //补光&&拍照
  2355. LOG_I("拍照!!!");
  2356. HMI_WRITE("workSta.g0.txt=\"拍照!\"");
  2357. HK_TakePhoto();
  2358. //清扫
  2359. device_status.clear_status=1;
  2360. //清扫失效则使用震动
  2361. //ctr_shake(ON, 100);
  2362. }
  2363. }
  2364. else {
  2365. //打开高压
  2366. ctr_Heat(ON);
  2367. //打开补光
  2368. ctr_led(ON);
  2369. LOG_I("打开补光!!");
  2370. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  2371. //2.翻转清虫
  2372. while(rt_pin_read(CLEAR_L_LIMIT)==1) rt_thread_delay(20);
  2373. rt_thread_delay(500);
  2374. Hk_Take_io(); //触发海康相机拍照
  2375. rt_thread_delay(500);
  2376. ctr_turnv5(); //摆动清虫
  2377. LOG_I("摆动清虫!");
  2378. }
  2379. }
  2380. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  2381. {
  2382. rt_kprintf("-------------------not work--------------------\n");
  2383. device_status.work_status = 0;
  2384. }
  2385. //工作结束
  2386. if(device_status.work_done==0&&device_status.work_status==0)
  2387. {
  2388. DTU_Write("workdone");
  2389. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  2390. if (device_status.version_type==1) {
  2391. turnzp(); //2024-03-21 :开机不再转仓
  2392. }
  2393. ResetInit();
  2394. }
  2395. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  2396. }
  2397. //拍照性诱工作任务流程
  2398. void WorkTask_XYV3(void)
  2399. {
  2400. int cal_n = device_status.cul_time * 60;
  2401. LOG_I("工作状态:%d",device_status.work_status);
  2402. LOG_I("开关机状态:%d",device_status.ds);
  2403. while(device_status.work_status==1&&device_status.bat_status==0&&device_status.ds==1&&device_status.temp_status==0)
  2404. {
  2405. //高压板通电
  2406. ctr_lamp(ON, 1); //清扫-DC12v电机正反转
  2407. //光栅传感器通电
  2408. ctr_photo(ON);
  2409. //拍照(?触发条件)计数?没有虫呢?定时
  2410. while(1){
  2411. cal_n--;
  2412. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  2413. if(cal_n==0){
  2414. //发送拍照计数指令
  2415. device_status.photo_ws = 2;
  2416. cal_n = device_status.cul_time * 60;
  2417. }
  2418. // LOG_I("当前时间:%d:%d:%d",alarm_takephpoto.tm_hour,alarm_takephpoto.tm_min,alarm_takephpoto.tm_sec);
  2419. rt_thread_delay(1000);
  2420. }
  2421. }
  2422. ctr_lamp(OFF, 1);
  2423. ctr_photo(OFF);
  2424. }
  2425. /* 入口函数-工作状态检测 */
  2426. static void Work_status_thread_entry(void *parameter)
  2427. {
  2428. static uint16_t sun_status_num = 120;
  2429. //如果是光控定时,进行光控监测的消抖
  2430. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2431. {
  2432. rt_kprintf("光控定时!!!\n");
  2433. while(sun_status_num!=0)
  2434. {
  2435. sun_status_num--;
  2436. rt_thread_delay(1000);
  2437. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2438. }
  2439. device_status.light_over_tim = device_status.sunBar * 3600;
  2440. device_status.test_flag = 0;
  2441. }
  2442. while(1)
  2443. {
  2444. WorkCheck();
  2445. }
  2446. }
  2447. /*入口函数-工作流程*/
  2448. static void Work_Loop_thread_entry(void *parameter)
  2449. {
  2450. rt_thread_delay(1000);
  2451. ResetInit();
  2452. while(1)
  2453. {
  2454. if(device_status.dtype==7)
  2455. {
  2456. WorkTask_BZY();
  2457. }
  2458. if(device_status.version_type!=6&&device_status.dtype==3)
  2459. {
  2460. WorkTask();
  2461. }
  2462. if(device_status.version_type==6)
  2463. {
  2464. WorkTask_XCT();
  2465. }
  2466. if(device_status.dtype==8){
  2467. WorkTask_XYV3();
  2468. }
  2469. rt_thread_delay(1000);
  2470. }
  2471. }
  2472. /*入口函数清扫*/
  2473. static void Control_Clear_thread_entry(void *parameter)
  2474. {
  2475. if(device_status.cul_time <=5) device_status.clt_time = 60;
  2476. static int selfcheck_cnt = 0;
  2477. while(1)
  2478. {
  2479. #if ROBOT_EN ==1
  2480. ctr_Motor_Robot(ON);
  2481. rt_thread_delay(100);
  2482. #else
  2483. #if Color_EN ==1
  2484. /*复位*/
  2485. if(Read_Color_Key(KEY_RESET)==1)
  2486. {
  2487. ctr_Color_board(RESET, 1200);
  2488. rt_thread_delay(5000);
  2489. }
  2490. #else
  2491. if(device_status.clear_status==1)
  2492. {
  2493. if(ctr_clear(1200)==0)
  2494. ctr_TG_Clear(400);
  2495. }
  2496. if(device_status.selfcheck_ws==1){
  2497. selfcheck_cnt++;
  2498. }else{
  2499. selfcheck_cnt = 0;
  2500. }
  2501. if(selfcheck_cnt>1200) //超过3分钟
  2502. {
  2503. device_status.selfcheck_ws = 0;
  2504. device_status.camera_sta = 1; //相机异常
  2505. //上报自检结果
  2506. //上报自检结果
  2507. char *json_temp = makeJson_selfCheck_(1);
  2508. DTU_Write("%s",json_temp);
  2509. free(json_temp);
  2510. }
  2511. if(device_status.clear_status==10){
  2512. //ctr_turn(90,0);
  2513. Motor_shake(1,800);
  2514. device_status.clear_status = 0;
  2515. }
  2516. #if 0
  2517. if(device_status.sensor_485_sta==0){
  2518. time_t time_TT;
  2519. time(&time_TT);
  2520. static struct tm alarm_time_TT;
  2521. alarm_time_TT =*localtime(&time_TT);
  2522. LOG_I("当前小时:%d",alarm_time_TT.tm_hour);
  2523. if((alarm_time_TT.tm_hour)>5&&(alarm_time_TT.tm_hour)<19)
  2524. {
  2525. float voltage_sun = atof(device_status.voltage) + alarm_time_TT.tm_hour * 0.1;
  2526. sprintf(device_status.voltage_sun,"%0.1f",(float)voltage_sun);
  2527. }else{
  2528. sprintf(device_status.voltage_sun,"0.0");
  2529. }
  2530. if(alarm_time_TT.tm_hour==1&&alarm_time_TT.tm_min==0&&alarm_time_TT.tm_sec==30){ //记录当前太阳能电池板电压
  2531. ef_set_env_blob("voltage_night", &device_status.voltage_sun, sizeof(device_status.voltage_sun));
  2532. }else if(alarm_time_TT.tm_hour==15&&alarm_time_TT.tm_min==30&&alarm_time_TT.tm_sec==30){
  2533. char voltage_temp[8];
  2534. ef_get_env_blob("voltage_night",&voltage_temp,sizeof(voltage_temp),NULL);
  2535. if(atof(device_status.voltage_sun)-atof(voltage_temp)<0.5){ //电压差小于0.5,则判定充电故障
  2536. }
  2537. }
  2538. }
  2539. #endif
  2540. rt_thread_delay(100);
  2541. #endif
  2542. #endif
  2543. }
  2544. }
  2545. /*入口函数加热*/
  2546. static void IO_Check_thread_entry(void *parameter)
  2547. {
  2548. rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  2549. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  2550. static int GaoyYa_cnt,GuangShan_cnt = 500; //10s
  2551. bool GaoYa = false;
  2552. bool GuangShan = false;
  2553. while(1)
  2554. {
  2555. if(rt_pin_read(SIGNAL_GaoYa)==0)
  2556. {
  2557. LOG_I("检测到高压计数!");
  2558. GaoYa = true;
  2559. }
  2560. if(rt_pin_read(SIGNAL_GuangShan)==0)
  2561. {
  2562. LOG_I("检测到光栅计数!");
  2563. GuangShan = true;
  2564. }
  2565. if(GaoYa)
  2566. {
  2567. GaoyYa_cnt --;
  2568. if(GaoyYa_cnt==0)
  2569. {
  2570. GaoyYa_cnt = 500;
  2571. GaoYa = false;
  2572. }
  2573. }
  2574. if(GuangShan)
  2575. {
  2576. GuangShan_cnt--;
  2577. if(GuangShan_cnt==0)
  2578. {
  2579. GuangShan_cnt = 500;
  2580. GuangShan = false;
  2581. }
  2582. }
  2583. if(GaoYa&&GuangShan)
  2584. {
  2585. device_status.all_ycct += 1; //计数值增加
  2586. //拍照
  2587. if(device_status.all_ycct>=1){
  2588. //device_status.photo_ws = 1;
  2589. }
  2590. GaoYa = false;
  2591. GuangShan = false;
  2592. GaoyYa_cnt = 500;
  2593. GuangShan_cnt = 500;
  2594. rt_thread_delay(1000);
  2595. }
  2596. rt_thread_delay(20);
  2597. }
  2598. }
  2599. /*清扫线程*/
  2600. int Control_clear(void)
  2601. {
  2602. /* 创建线程*/
  2603. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2604. Control_Clear_thread_entry, RT_NULL,
  2605. 2048, //2048
  2606. 24, //23
  2607. WORK_CHECK_THREAD_TIMESLICE); //5
  2608. if(tid_ctr!=RT_NULL)
  2609. {
  2610. rt_thread_startup(tid_ctr);
  2611. }
  2612. return 0;
  2613. }
  2614. /*恒温加热线程*/
  2615. int Control_heat(void)
  2616. {
  2617. /* 创建线程*/
  2618. tid_heat = rt_thread_create("IO_Check_thread_entry",
  2619. IO_Check_thread_entry, RT_NULL,
  2620. 1024, //2048
  2621. 25, //23
  2622. WORK_CHECK_THREAD_TIMESLICE); //5
  2623. if(tid_heat!=RT_NULL)
  2624. {
  2625. rt_thread_startup(tid_heat);
  2626. }
  2627. return 0;
  2628. }
  2629. /* 创建线程 -工作状态检测*/
  2630. int Workstatus_check(void)
  2631. {
  2632. /* 创建线程*/
  2633. tid_wkstc = rt_thread_create("work_status_check_thread",
  2634. Work_status_thread_entry, RT_NULL,
  2635. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2636. WORK_CHECK_THREAD_PRIORITY, //23
  2637. WORK_CHECK_THREAD_TIMESLICE); //5
  2638. if(tid_wkstc!=RT_NULL)
  2639. {
  2640. rt_thread_startup(tid_wkstc);
  2641. }
  2642. return 0;
  2643. }
  2644. /*工作流程线程*/
  2645. int Work_Loop_Task(void)
  2646. {
  2647. /* 创建线程*/
  2648. tid_workloop = rt_thread_create("work_loop_thread",
  2649. Work_Loop_thread_entry, RT_NULL,
  2650. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2651. WORK_LOOP_THREAD_PRIORITY, //12
  2652. WORK_LOOP_THREAD_TIMESLICE); //5
  2653. #if Color_EN == 0
  2654. if(tid_workloop!=RT_NULL)
  2655. {
  2656. rt_thread_startup(tid_workloop);
  2657. }
  2658. #endif
  2659. return 0;
  2660. }
  2661. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2662. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2663. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2664. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);