control.c 87 KB

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