control.c 83 KB

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