control.c 70 KB

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