control.c 70 KB

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