control.c 86 KB

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