control.c 79 KB

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