control.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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. *
  590. * */
  591. void ctr_Motor_Driver_Power(uint8_t status_ws)
  592. {
  593. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  594. rt_pin_write(SHAKE_SW, status_ws);
  595. }
  596. /*************************************************
  597. Function: ctr_Motor_Driver
  598. Description: 步进电机驱动
  599. Calls:
  600. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  601. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  602. Input: 50000000
  603. Output: NULL
  604. Return: shake_sec
  605. Others: NULL
  606. *************************************************/
  607. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  608. {
  609. beep_init();
  610. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  611. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  612. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  613. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  614. if(EN==0)
  615. {
  616. rt_pin_write(Motor_Power,0);
  617. }
  618. else{
  619. rt_pin_write(Motor_EN, EN);
  620. }
  621. rt_pin_write(Motor_DIR, DIR);
  622. beep_set(pwm, pwm/2); //200kzh - 50%
  623. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  624. {
  625. if(time_out==0)
  626. {
  627. break;
  628. }
  629. rt_thread_delay(50);
  630. time_out --;
  631. }
  632. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  633. {
  634. if(time_out==0)
  635. {
  636. break;
  637. }
  638. rt_thread_delay(50);
  639. time_out --;
  640. }
  641. //rt_pin_write(Motor_EN, OFF);
  642. //if(device_status.autotakepic==0)
  643. //ctr_lamp(OFF, 1);
  644. beep_set(0, 0); //200kzh - 50%
  645. rt_thread_delay(50);
  646. }
  647. /*************************************************
  648. Function: ctr_Petroleum_jelly
  649. Description: 涂凡士林
  650. Calls:
  651. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  652. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  653. Input: NULL
  654. Output: NULL
  655. Return: NULL
  656. Others: NULL
  657. *************************************************/
  658. void ctr_Petroleum_jelly(rt_uint32_t delay)
  659. {
  660. beep_init();
  661. pwm_on(2,50000);
  662. rt_thread_delay(delay*1000);
  663. pwm_off(2,0);
  664. rt_thread_delay(1000);
  665. pwm_on(3, 50000);
  666. rt_thread_delay(delay*1000);
  667. pwm_off(3,0);
  668. device_status.fsl_sw = 0;
  669. }
  670. //1.0转仓
  671. void turnzp(void)
  672. {
  673. static uint16_t turn_timeout = 0;
  674. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  675. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  676. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  677. // rt_pin_write(CLEAR_R,OFF);
  678. // rt_pin_write(CLEAR_L,OFF);
  679. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  680. {
  681. rt_thread_delay(20);
  682. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  683. {
  684. // rt_pin_write(CLEAR_R,ON);
  685. // rt_pin_write(CLEAR_L,OFF);
  686. Motor_Juandai(ON);
  687. rt_thread_delay(50);
  688. turn_timeout++;
  689. if(turn_timeout==600)
  690. {
  691. LOG_E("检测不到限位,电机强制停止");
  692. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  693. DTU_Write("%s",ctr_zp_limit_warn);
  694. rt_free(ctr_zp_limit_warn);
  695. break;
  696. }
  697. }
  698. break;
  699. }
  700. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  701. {
  702. rt_thread_delay(20);
  703. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  704. {
  705. // rt_pin_write(CLEAR_R,ON);
  706. // rt_pin_write(CLEAR_L,OFF);
  707. Motor_Juandai(ON);
  708. rt_thread_delay(50);
  709. turn_timeout++;
  710. if(turn_timeout==600)
  711. {
  712. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  713. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  714. DTU_Write("%s",ctr_zp_limit_warn);
  715. rt_free(ctr_zp_limit_warn);
  716. break;
  717. }
  718. }
  719. break;
  720. }
  721. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  722. {
  723. rt_thread_delay(20);
  724. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  725. {
  726. // rt_pin_write(CLEAR_R,OFF);
  727. // rt_pin_write(CLEAR_L,OFF);
  728. Motor_Juandai(OFF);
  729. break;
  730. }
  731. else {
  732. break;
  733. }
  734. }
  735. Motor_Juandai(OFF);
  736. // rt_pin_write(CLEAR_R,OFF);
  737. // rt_pin_write(CLEAR_L,OFF);
  738. turn_timeout = 0;
  739. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  740. HMI_WRITE("sysTest.switchClear.val=%d",0);
  741. rt_thread_delay(1000);
  742. }
  743. //清扫-常闭型限位开关
  744. //新结构使用推杆电机做清扫
  745. uint8_t ctr_clear(uint16_t status_clear)
  746. {
  747. static uint16_t clear_num = 0;
  748. #if PWM_EN == 0
  749. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  750. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  751. #endif
  752. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  753. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  754. /*************************************************************外接清扫
  755. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  756. rt_pin_write(POWER_CLEAR,ON);
  757. rt_thread_delay(1000*100);
  758. rt_pin_write(POWER_CLEAR,OFF);
  759. ************************************************************/
  760. //停止
  761. #if PWM_EN == 0
  762. rt_pin_write(CLEAR_R,OFF);
  763. rt_pin_write(CLEAR_L,OFF);
  764. #else
  765. Motor_Qingsao_R_PWM(OFF);
  766. Motor_Qingsao_L_PWM(OFF);
  767. #endif
  768. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  769. rt_thread_delay(100);
  770. //如果清扫位置在中间
  771. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  772. {
  773. #if PWM_EN == 0
  774. rt_pin_write(CLEAR_R,ON);
  775. rt_pin_write(CLEAR_L,OFF);
  776. #else
  777. Motor_Qingsao_R_PWM(ON);
  778. Motor_Qingsao_L_PWM(OFF);
  779. #endif
  780. rt_thread_delay(50);
  781. clear_num++;
  782. //已到达检测位置
  783. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  784. {
  785. #if PWM_EN == 0
  786. rt_pin_write(CLEAR_R,ON);
  787. rt_pin_write(CLEAR_L,ON);
  788. #else
  789. Motor_Qingsao_R_PWM(OFF);
  790. Motor_Qingsao_L_PWM(OFF);
  791. #endif
  792. rt_thread_delay(2000);
  793. break;
  794. }
  795. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  796. {
  797. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  798. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  799. #if PWM_EN == 0
  800. rt_pin_write(CLEAR_R,ON);
  801. rt_pin_write(CLEAR_L,ON);
  802. #else
  803. Motor_Qingsao_R_PWM(OFF);
  804. Motor_Qingsao_L_PWM(OFF);
  805. #endif
  806. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  807. if(Read_Real_Voltage()/100<12)
  808. {
  809. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  810. HMI_WRITE("sysTest.switchClear.val=%d",0);
  811. device_status.clear_status = 0;
  812. return 0;
  813. }
  814. break;
  815. }
  816. }
  817. clear_num = 0;
  818. //如果清扫位置在左侧
  819. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  820. {
  821. #if PWM_EN == 0
  822. rt_pin_write(CLEAR_R,ON);
  823. rt_pin_write(CLEAR_L,OFF);
  824. #else
  825. Motor_Qingsao_R_PWM(ON);
  826. Motor_Qingsao_L_PWM(OFF);
  827. #endif
  828. rt_thread_delay(50);
  829. clear_num++;
  830. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  831. {
  832. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  833. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  834. #if PWM_EN == 0
  835. rt_pin_write(CLEAR_R,ON);
  836. rt_pin_write(CLEAR_L,ON);
  837. #else
  838. Motor_Qingsao_R_PWM(OFF);
  839. Motor_Qingsao_L_PWM(OFF);
  840. #endif
  841. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  842. if(Read_Real_Voltage()/100<12)
  843. {
  844. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  845. HMI_WRITE("sysTest.switchClear.val=%d",0);
  846. device_status.clear_status = 0;
  847. return 0;
  848. }
  849. break;
  850. }
  851. }
  852. clear_num = 0;
  853. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  854. {
  855. #if PWM_EN == 0
  856. rt_pin_write(CLEAR_R,ON);
  857. rt_pin_write(CLEAR_L,ON);
  858. #else
  859. Motor_Qingsao_R_PWM(OFF);
  860. Motor_Qingsao_L_PWM(OFF);
  861. #endif
  862. if(Read_Real_Voltage()/100<12)
  863. {
  864. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  865. HMI_WRITE("sysTest.switchClear.val=%d",0);
  866. device_status.clear_status = 0;
  867. return 0;
  868. }
  869. rt_thread_delay(2000);
  870. break;
  871. }
  872. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  873. {
  874. #if PWM_EN == 0
  875. rt_pin_write(CLEAR_R,OFF);
  876. rt_pin_write(CLEAR_L,ON);
  877. #else
  878. Motor_Qingsao_R_PWM(OFF);
  879. Motor_Qingsao_L_PWM(ON);
  880. #endif
  881. rt_thread_delay(50);
  882. clear_num++;
  883. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  884. {
  885. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  886. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  887. //DTU_Write("%s",Clear_ERR);
  888. #if PWM_EN == 0
  889. rt_pin_write(CLEAR_R,ON);
  890. rt_pin_write(CLEAR_L,ON);
  891. #else
  892. Motor_Qingsao_R_PWM(OFF);
  893. Motor_Qingsao_L_PWM(OFF);
  894. #endif
  895. rt_free(Clear_ERR);
  896. if(Read_Real_Voltage()/100<12)
  897. {
  898. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  899. HMI_WRITE("sysTest.switchClear.val=%d",0);
  900. device_status.clear_status = 0;
  901. return 0;
  902. }
  903. break;
  904. }
  905. }
  906. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  907. {
  908. #if PWM_EN == 0
  909. rt_pin_write(CLEAR_R,ON);
  910. rt_pin_write(CLEAR_L,ON);
  911. #else
  912. Motor_Qingsao_R_PWM(OFF);
  913. Motor_Qingsao_L_PWM(OFF);
  914. #endif
  915. rt_thread_delay(2000);
  916. break;
  917. }
  918. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  919. {
  920. #if PWM_EN == 0
  921. rt_pin_write(CLEAR_R,ON);
  922. rt_pin_write(CLEAR_L,ON);
  923. #else
  924. Motor_Qingsao_R_PWM(OFF);
  925. Motor_Qingsao_L_PWM(OFF);
  926. #endif
  927. if(device_status.version_type==41)
  928. {
  929. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  930. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  931. //DTU_Write("%s",Clear_ERR);
  932. rt_free(Clear_ERR);
  933. }
  934. return 0;
  935. }
  936. clear_num = 0;
  937. rt_thread_delay(2000);
  938. #if PWM_EN == 0
  939. rt_pin_write(CLEAR_R,ON);
  940. rt_pin_write(CLEAR_L,ON);
  941. #else
  942. Motor_Qingsao_R_PWM(OFF);
  943. Motor_Qingsao_L_PWM(OFF);
  944. #endif
  945. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  946. HMI_WRITE("sysTest.switchClear.val=%d",0);
  947. device_status.clear_status = 0;
  948. return 1;
  949. }
  950. //路由器供电
  951. void ctr_router(uint8_t status_router)
  952. {
  953. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  954. if(device_status.version_type==6)
  955. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  956. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  957. if(status_router==ON)
  958. {
  959. rt_pin_write(ROUTER,ON);
  960. //
  961. #if LAMP_EN == 0
  962. if(device_status.version_type==6)
  963. rt_pin_write(ROUTER_P,ON);
  964. #endif
  965. }
  966. else {
  967. rt_pin_write(ROUTER,OFF);
  968. //
  969. #if LAMP_EN == 0
  970. if(device_status.version_type==6)
  971. rt_pin_write(ROUTER_P,OFF);
  972. #endif
  973. }
  974. }
  975. //清扫-推杆电机
  976. void ctr_TG_Clear(uint16_t clear_timeout)
  977. {
  978. #if 0
  979. int current_clear[200];
  980. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  981. LOG_I("清扫电机待机:%d",Electric_current);
  982. #endif
  983. static uint16_t clear_count = 0;
  984. clear_count = clear_timeout;
  985. #if PWM_EN == 0
  986. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  987. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  988. #endif
  989. while(clear_count!=0)
  990. {
  991. #if PWM_EN == 0
  992. rt_pin_write(CLEAR_R,ON);
  993. rt_pin_write(CLEAR_L,OFF);
  994. #else
  995. Motor_Qingsao_R_PWM(ON);
  996. Motor_Qingsao_L_PWM(OFF);
  997. #endif
  998. clear_count--;
  999. rt_thread_delay(100);
  1000. }
  1001. #if PWM_EN == 0
  1002. rt_pin_write(CLEAR_R,ON);
  1003. rt_pin_write(CLEAR_L,ON);
  1004. #else
  1005. Motor_Qingsao_R_PWM(OFF);
  1006. Motor_Qingsao_L_PWM(OFF);
  1007. #endif
  1008. rt_thread_delay(3000);
  1009. clear_count = 0;
  1010. while(clear_timeout!=clear_count)
  1011. {
  1012. #if PWM_EN == 0
  1013. rt_pin_write(CLEAR_R,OFF);
  1014. rt_pin_write(CLEAR_L,ON);
  1015. #else
  1016. Motor_Qingsao_R_PWM(OFF);
  1017. Motor_Qingsao_L_PWM(ON);
  1018. #endif
  1019. // if(clear_count<200)
  1020. // current_clear[clear_count] = atoi(device_status.power);
  1021. clear_count++;
  1022. rt_thread_delay(100);
  1023. }
  1024. #if PWM_EN == 0
  1025. rt_pin_write(CLEAR_R,ON);
  1026. rt_pin_write(CLEAR_L,ON);
  1027. #else
  1028. Motor_Qingsao_R_PWM(OFF);
  1029. Motor_Qingsao_L_PWM(OFF);
  1030. #endif
  1031. #if 0
  1032. bubblesort(current_clear,200);
  1033. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1034. {
  1035. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1036. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1037. DTU_Write("%s",ctr_clear_warn);
  1038. rt_free(ctr_clear_warn);
  1039. }
  1040. #endif
  1041. rt_thread_delay(2000);
  1042. device_status.clear_status = 0;
  1043. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1044. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1045. }
  1046. //DTU,RTU供电
  1047. void ctr_RTU_DTU(uint8_t status_rtu)
  1048. {
  1049. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1050. if(status_rtu==ON)
  1051. {
  1052. LOG_W("RTU电源启动!!!");
  1053. rt_pin_write(RTU_DTU,ON);
  1054. }
  1055. else
  1056. {
  1057. LOG_W("RTU电源关闭!!!");
  1058. rt_pin_write(RTU_DTU,OFF);
  1059. }
  1060. }
  1061. //海康拍照
  1062. void Hk_Take_io(void)
  1063. {
  1064. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1065. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1066. rt_pin_write(HK_TAKE, 1);
  1067. rt_pin_write(HK_IO, 0);
  1068. if(device_status.dtype==3)
  1069. Beep_Tip(1000);
  1070. if(device_status.dtype==7)
  1071. rt_thread_mdelay(1000);
  1072. rt_pin_write(HK_TAKE, 0);
  1073. rt_pin_write(HK_IO, 1);
  1074. }
  1075. void calorstat(uint16_t heat_time)
  1076. {
  1077. //uint8_t heat_up_cnt = 0;
  1078. uint16_t n = 0;
  1079. uint16_t delay_heat = 60;
  1080. FlagStatus heat_flg = RESET;
  1081. device_status.heat_time_min = 0;
  1082. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1083. ctr_Heat(ON);
  1084. // UpLoad_Data(); //开始加热时的第一条数据
  1085. //恒温模式
  1086. for(n=0;n < heat_time;n++)
  1087. {
  1088. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1089. {
  1090. heat_flg = SET;
  1091. ctr_Heat(OFF);
  1092. while(delay_heat!=0)
  1093. {
  1094. delay_heat --;
  1095. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1096. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1097. rt_thread_delay(1000);
  1098. if(device_status.heattem>device_status.run_sta + 1){
  1099. device_status.run_sta = device_status.heattem;
  1100. }
  1101. }
  1102. delay_heat = 60;
  1103. //跳出FOR循环
  1104. n = n + 60;
  1105. }
  1106. if(device_status.heattem>device_status.run_sta + 1){
  1107. device_status.run_sta = device_status.heattem;
  1108. }
  1109. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1110. {
  1111. //device_status.run_sta = device_status.heattem;
  1112. ctr_Heat(ON);
  1113. }
  1114. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1115. {
  1116. // if(heat_up_cnt==0)
  1117. // {
  1118. // UpLoad_Data(); //加热到设定温度的数据
  1119. // heat_up_cnt = 1;
  1120. // }
  1121. //device_status.run_sta = device_status.heattem;
  1122. ctr_Heat(OFF);
  1123. }
  1124. if((n % 60)==0)
  1125. {
  1126. device_status.heat_time_min = device_status.heat_time_min + 1;
  1127. // if(device_status.heat_time_min % 5 == 0)
  1128. // {
  1129. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1130. // }
  1131. }
  1132. //如果触发异常退出加热
  1133. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1134. {
  1135. rt_thread_delay(1000);
  1136. if(device_status.bat_status == 1)
  1137. {
  1138. rt_kprintf("============================触发异常退出加热==========================");
  1139. ctr_lamp(OFF,1);
  1140. ctr_Heat(OFF);
  1141. break;
  1142. }
  1143. }
  1144. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1145. {
  1146. ctr_Heat(OFF);
  1147. rt_kprintf("异常退出!!!\n");
  1148. break;
  1149. }
  1150. rt_thread_delay(1000);
  1151. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1152. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1153. }
  1154. ctr_Heat(OFF);
  1155. //UpLoad_Data(); //加热结束的数据
  1156. }
  1157. //初始化所有控制驱动,关闭
  1158. void HardWare_Init(uint8_t boot)
  1159. {
  1160. Beep_Tip(200);
  1161. //关闭上仓门
  1162. ctr_cangmen_up(OFF);
  1163. if(device_status.dtype==3){
  1164. //关闭下仓门电机
  1165. ctr_cangmen_down(OFF);
  1166. }
  1167. //关闭镇流器
  1168. if(device_status.autotakepic==0)
  1169. ctr_lamp(OFF,1);
  1170. //关闭震动电机
  1171. ctr_shake(OFF,0);
  1172. //关闭加热
  1173. ctr_Heat(OFF);
  1174. //关闭补光灯
  1175. ctr_led(OFF);
  1176. //关闭12V供电(海康相机,RTU)
  1177. ctr_photo(OFF);
  1178. //关闭清扫电机驱动
  1179. ctr_clear(OFF);
  1180. }
  1181. //拍照
  1182. void HK_TakePhoto(void)
  1183. {
  1184. //打开补光灯
  1185. DTU_Write("takephoto.mp3");
  1186. rt_thread_delay(10000);
  1187. char *takephotojson = makeJson_takephoto();
  1188. DTU_Write("%s",takephotojson);
  1189. rt_free(takephotojson);
  1190. //吸虫塔
  1191. if(device_status.version_type!=6)
  1192. {
  1193. ctr_led(ON);
  1194. if(device_status.version_type==48)
  1195. {
  1196. rt_thread_delay(1000*60);
  1197. }
  1198. else {
  1199. rt_thread_delay(1000*15);
  1200. }
  1201. }
  1202. //触发海康相机拍照
  1203. Hk_Take_io();
  1204. if(device_status.version_type!=6)
  1205. {
  1206. rt_thread_delay(1000*45);
  1207. }
  1208. else {
  1209. ctr_led(ON);
  1210. rt_thread_delay(1000*19);
  1211. }
  1212. ctr_led(OFF);
  1213. }
  1214. //光控测试任务
  1215. void SunTest(void)
  1216. {
  1217. rt_uint16_t ui_timeout_l = 0;
  1218. //未检测到光照,一秒扫描等待,超时30S
  1219. while(Read_Light_Voltage()<500)
  1220. {
  1221. ui_timeout_l ++;
  1222. rt_thread_delay(1000);
  1223. if(ui_timeout_l==30) break;
  1224. }
  1225. rt_thread_delay(3000);
  1226. if(Read_Light_Voltage()<500)
  1227. {
  1228. device_status.sun_status = 0;//光控状态
  1229. }
  1230. if(Read_Light_Voltage()>500)
  1231. {
  1232. rt_thread_delay(1000);
  1233. }
  1234. rt_thread_delay(3000);
  1235. if(Read_Light_Voltage()>500)
  1236. {
  1237. device_status.sun_status = 1;
  1238. ctr_lamp(OFF,1);
  1239. }
  1240. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1241. device_status.sun_test = 0;
  1242. device_status.work_status=0;
  1243. }
  1244. /*上电初始化*/
  1245. void ResetInit(void)
  1246. {
  1247. LOG_I("ResetInit...");
  1248. if(device_status.sleep_mode==0)
  1249. {
  1250. //1.打开路由器供电
  1251. ctr_router(ON);
  1252. //2. 打开RTU供电
  1253. ctr_RTU_DTU(ON);
  1254. }
  1255. //3.打开光栅供电
  1256. ctr_photo(ON);
  1257. //5.关闭加热
  1258. //6.关闭补光灯
  1259. ctr_led(OFF);
  1260. }
  1261. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1262. void HardWare_Check(uint8_t boot)
  1263. {
  1264. if(boot==0&&device_status.version_type!=6)
  1265. {
  1266. //停止所有动作 -完成
  1267. HardWare_Init(boot);
  1268. }
  1269. else {
  1270. //停止所有动作
  1271. //读取记录待机功耗(实测大概70mA)
  1272. //(需减去屏幕的功耗大概120mA)
  1273. //动作上仓门,读取功率值保存
  1274. //动作下仓门,读取功率值保存
  1275. //动作清扫,读取功率值保存
  1276. //打开镇流器,读取功率值保存
  1277. //打开海康相机供电,读取功率值保存
  1278. //打开补光灯,读取功率值保存
  1279. //打开路由器/RTU供电,读取功率值保存
  1280. //打开加热,读取功率值保存
  1281. }
  1282. }
  1283. void WorkCheck(void)
  1284. {
  1285. static rt_uint32_t light_timeout = 3600;
  1286. while(device_status.ds == 1 &&
  1287. device_status.rain_status == 0 &&
  1288. device_status.temp_status == 0 &&
  1289. device_status.bat_status == 0)
  1290. {
  1291. if(device_status.timeSwitch == 1) // 时控模式
  1292. {
  1293. LOG_I("***********时控*****************");
  1294. if(device_status.startBar == device_status.stopBar)
  1295. {
  1296. // 开始结束时间相同则24小时模式
  1297. device_status.work_status = 1;
  1298. rt_thread_delay(10000);
  1299. continue;
  1300. }
  1301. time_t now;
  1302. time(&now);
  1303. struct tm now_tm = *localtime(&now);
  1304. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1305. rt_uint32_t start_sec = device_status.startBar * 3600;
  1306. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1307. // 修复跨天判断逻辑
  1308. if (start_sec < end_sec)
  1309. {
  1310. // 不跨天模式
  1311. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1312. }
  1313. else
  1314. {
  1315. // 跨天模式
  1316. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1317. }
  1318. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1319. device_status.work_status ? "工作" : "待机",
  1320. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1321. device_status.startBar, device_status.stopBar);
  1322. rt_thread_delay(10000);
  1323. }
  1324. else // 光控模式
  1325. {
  1326. LOG_I("光控定时模式!");
  1327. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1328. {
  1329. device_status.light_over_tim--;
  1330. light_timeout = 3600; // 重置超时计数器
  1331. device_status.work_status = 1;
  1332. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1333. }
  1334. else
  1335. {
  1336. // 仅在白天递减超时计数器
  1337. if(device_status.sun_status == 1 && light_timeout > 0) {
  1338. light_timeout--;
  1339. }
  1340. device_status.work_status = 0;
  1341. //没有在测试并且没有在自检
  1342. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1343. {
  1344. ctr_lamp(OFF,1);
  1345. }
  1346. // 连续1小时白天后重置
  1347. if(light_timeout == 0) {
  1348. device_status.light_over_tim = 0;
  1349. light_timeout = 3600; // 重置防止负数
  1350. }
  1351. // 白天且倒计时结束:重置夜晚时长
  1352. if(device_status.light_over_tim == 0 &&
  1353. device_status.sun_status == 1)
  1354. {
  1355. device_status.light_over_tim = device_status.sunBar * 3600;
  1356. }
  1357. }
  1358. }
  1359. rt_thread_delay(1000);
  1360. }
  1361. // 异常状态处理
  1362. device_status.work_status = 0;
  1363. rt_thread_delay(1000);
  1364. }
  1365. #if 0
  1366. void WorkCheck(void)
  1367. {
  1368. static rt_uint32_t light_timeout = 3600;
  1369. while(device_status.ds == 1 &&
  1370. device_status.rain_status == 0 &&
  1371. device_status.temp_status == 0 &&
  1372. device_status.bat_status == 0)
  1373. {
  1374. if(device_status.timeSwitch == 1) // 时控模式
  1375. {
  1376. LOG_I("***********时控*****************");
  1377. if(device_status.startBar == device_status.stopBar &&
  1378. device_status.startBar == 0)
  1379. {
  1380. // 24小时模式处理
  1381. device_status.work_status = 1;
  1382. rt_thread_delay(10000);
  1383. continue;
  1384. }
  1385. time_t now;
  1386. time(&now);
  1387. struct tm now_tm = *localtime(&now);
  1388. struct tm start_tm = *localtime(&now);
  1389. struct tm end_tm = *localtime(&now);
  1390. // 设置开始时间
  1391. now_tm.tm_hour = now_tm.tm_hour-8;
  1392. time_t now_time = mktime(&now_tm);
  1393. // 设置开始时间
  1394. start_tm.tm_hour = device_status.startBar-8;
  1395. start_tm.tm_min = 0;
  1396. start_tm.tm_sec = 0;
  1397. time_t start_time = mktime(&start_tm);
  1398. // 设置结束时间(自动处理跨天)
  1399. end_tm.tm_hour = device_status.stopBar-8;
  1400. end_tm.tm_min = 0;
  1401. end_tm.tm_sec = 0;
  1402. if(device_status.startBar >= device_status.stopBar) {
  1403. end_tm.tm_mday += 1; // 结束时间+1天
  1404. }
  1405. time_t end_time = mktime(&end_tm);
  1406. // 工作时间判断
  1407. if(now_time >= start_time && now_time < end_time)
  1408. {
  1409. device_status.work_status = 1;
  1410. LOG_I("时控工作模式:现在:%d- 开始:%d-结束:%d",now_time,start_time,end_time);
  1411. rt_thread_delay(10000);
  1412. }
  1413. else
  1414. {
  1415. device_status.work_status = 0;
  1416. LOG_I("时控待机模式:现在:%d- 开始:%d-结束:%d",now_time,start_time,end_time);
  1417. rt_thread_delay(10000);
  1418. }
  1419. }
  1420. else // 光控模式
  1421. {
  1422. LOG_I("光控定时模式!");
  1423. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1424. {
  1425. device_status.light_over_tim--;
  1426. light_timeout = 3600; // 重置超时计数器
  1427. device_status.work_status = 1;
  1428. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1429. }
  1430. else
  1431. {
  1432. // 仅在白天递减超时计数器
  1433. if(device_status.sun_status == 1 && light_timeout > 0) {
  1434. light_timeout--;
  1435. }
  1436. device_status.work_status = 0;
  1437. //没有在测试并且没有在自检
  1438. if(device_status.test_flag==0&&device_status.selfcheck_ws==0)
  1439. {
  1440. ctr_lamp(OFF,1);
  1441. }
  1442. // 连续1小时白天后重置
  1443. if(light_timeout == 0) {
  1444. device_status.light_over_tim = 0;
  1445. light_timeout = 3600; // 重置防止负数
  1446. }
  1447. // 白天且倒计时结束:重置夜晚时长
  1448. if(device_status.light_over_tim == 0 &&
  1449. device_status.sun_status == 1)
  1450. {
  1451. device_status.light_over_tim = device_status.sunBar * 3600;
  1452. }
  1453. }
  1454. }
  1455. rt_thread_delay(1000);
  1456. }
  1457. // 异常状态处理
  1458. device_status.work_status = 0;
  1459. rt_thread_delay(1000);
  1460. }
  1461. #endif
  1462. #if 0
  1463. //工作状态检测
  1464. void WorkCheck(void)
  1465. {
  1466. static rt_uint32_t light_timeout = 3600;
  1467. //雨控检测,温控检测,电池电压检测
  1468. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1469. {
  1470. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1471. if(device_status.timeSwitch==1)
  1472. {
  1473. LOG_I("***********时控*****************");
  1474. if(device_status.startBar==device_status.stopBar&&device_status.startBar==0){
  1475. device_status.work_status = 1;
  1476. LOG_I("24 hours work...");
  1477. #if LAMP_EN == 1
  1478. device_status.lamp_status = 1;
  1479. device_status.lamp1_status = 0;
  1480. device_status.lamp2_status = 0;
  1481. if(device_status.sun_status==1){
  1482. ctr_lamp(OFF, 1);
  1483. ctr_lamp(OFF, 2);
  1484. ctr_lamp(OFF, 3);
  1485. }
  1486. #endif
  1487. }
  1488. //如果开始和结束是同一天
  1489. if(device_status.startBar<device_status.stopBar)
  1490. {
  1491. LOG_I("开始和结束是同一天");
  1492. time_t now;
  1493. time(&now);
  1494. static struct tm nowtime; //现在
  1495. static struct tm starttime; //开始
  1496. static struct tm endtime; //结束
  1497. //获取当前时间戳
  1498. nowtime =*localtime(&now);
  1499. nowtime.tm_hour = nowtime.tm_hour-8;
  1500. //获取定时开始的时间戳
  1501. starttime = *localtime(&now);
  1502. starttime.tm_hour = device_status.startBar-8;
  1503. starttime.tm_min = 0;
  1504. starttime.tm_sec = 0;
  1505. //获取定时结束的时间戳
  1506. endtime = *localtime(&now);
  1507. endtime.tm_hour = device_status.stopBar-8;
  1508. endtime.tm_min = 0;
  1509. endtime.tm_sec = 0;
  1510. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1511. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1512. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1513. {
  1514. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1515. device_status.work_status = 1;
  1516. }
  1517. //如果当前时间小于开始时间
  1518. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1519. {
  1520. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1521. device_status.work_status = 0;
  1522. }
  1523. //当前时间大于结束时间
  1524. else {
  1525. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1526. device_status.work_status = 0;
  1527. }
  1528. #if LAMP_EN == 1
  1529. //如果当前时间超过20点,未到21点
  1530. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1531. {
  1532. LOG_I("3led:20~21");
  1533. device_status.lamp_status = 1;
  1534. device_status.lamp1_status = 0;
  1535. device_status.lamp2_status = 0;
  1536. }
  1537. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1538. LOG_I("3led:21~22");
  1539. device_status.lamp1_status = 1;
  1540. device_status.lamp_status = 0;
  1541. device_status.lamp2_status = 0;
  1542. }
  1543. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1544. LOG_I("3led:22~23");
  1545. device_status.lamp2_status = 1;
  1546. device_status.lamp_status = 0;
  1547. device_status.lamp1_status =0;
  1548. }
  1549. #endif
  1550. }
  1551. //如果结束时间是第二天
  1552. else if (device_status.startBar>device_status.stopBar) {
  1553. time_t now;
  1554. time(&now);
  1555. static struct tm nowtime; //现在
  1556. static struct tm starttime; //开始
  1557. static struct tm endtime; //结束
  1558. //获取当前时间戳
  1559. nowtime =*localtime(&now);
  1560. nowtime.tm_hour = nowtime.tm_hour-8;
  1561. //获取定时开始的时间戳
  1562. starttime = *localtime(&now);
  1563. starttime.tm_hour = device_status.startBar-8;
  1564. starttime.tm_min = 0;
  1565. starttime.tm_sec = 0;
  1566. //获取定时结束的时间戳
  1567. endtime = *localtime(&now);
  1568. endtime.tm_hour = device_status.stopBar-8;
  1569. endtime.tm_min = 0;
  1570. endtime.tm_sec = 0;
  1571. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1572. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1573. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1574. {
  1575. //获取定时结束的时间戳
  1576. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1577. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1578. endtime.tm_min = 0;
  1579. endtime.tm_sec = 0;
  1580. device_status.work_status = 1;
  1581. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1582. }
  1583. //当前时间小于开始时间
  1584. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1585. {
  1586. //获取定时结束的时间戳
  1587. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1588. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1589. endtime.tm_min = 0;
  1590. endtime.tm_sec = 0;
  1591. //如果当前时间戳小于结束时间戳
  1592. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1593. {
  1594. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1595. device_status.work_status = 1;
  1596. }
  1597. else
  1598. {
  1599. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1600. device_status.work_status = 0; //不在工作时间,待机
  1601. }
  1602. }
  1603. }
  1604. }
  1605. else {
  1606. LOG_I("光控定时模式!");
  1607. /*
  1608. time_t now;
  1609. time(&now);
  1610. static struct tm nowstamp;
  1611. nowstamp =*localtime(&now);
  1612. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1613. static rt_uint32_t nowstamp_num = 0;
  1614. nowstamp_num = mktime(&nowstamp);
  1615. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1616. */
  1617. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1618. {
  1619. device_status.light_over_tim--;
  1620. light_timeout = 3600;
  1621. #if LAMP_EN == 1
  1622. Led_Check(device_status.sunBar);
  1623. #else
  1624. device_status.work_status = 1;;
  1625. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1626. #endif
  1627. }
  1628. else
  1629. {
  1630. light_timeout --;
  1631. //没有在测试并且没有在自检
  1632. if(device_status.test_flag==0&&device_status.selfcheck_ws==0)
  1633. {
  1634. ctr_lamp(OFF,1);
  1635. #if LAMP_EN == 1
  1636. ctr_lamp(OFF,2);
  1637. ctr_lamp(OFF,3);
  1638. #endif
  1639. }
  1640. device_status.work_status = 0;
  1641. if(light_timeout==0) device_status.light_over_tim = 0; //持续1小时白天,清除光控计时,20250519
  1642. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1643. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1644. }
  1645. }
  1646. rt_thread_delay(1000);
  1647. }
  1648. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1649. device_status.work_status = 0;
  1650. rt_thread_delay(1000);
  1651. }
  1652. #endif
  1653. static uint8_t sleep_cnt = 0;
  1654. //拍照性诱工作任务流程
  1655. void WorkTask_XYV3(void)
  1656. {
  1657. int cal_n = device_status.cul_time * 60;
  1658. LOG_I("工作状态:%d",device_status.work_status);
  1659. LOG_I("开关机状态:%d",device_status.ds);
  1660. while(device_status.work_status==1&&device_status.bat_status==0&&device_status.ds==1&&device_status.temp_status==0)
  1661. {
  1662. //高压板通电
  1663. ctr_lamp(ON, 1); //清扫-DC12v电机正反转
  1664. //光栅传感器通电
  1665. ctr_photo(ON);
  1666. //拍照(?触发条件)计数?没有虫呢?定时
  1667. while(1){
  1668. cal_n--;
  1669. LOG_I("诱虫检测计数剩余时间:%d S",cal_n);
  1670. if(cal_n==0){
  1671. //自动进入省电模式
  1672. device_status.sleep_mode = 1;
  1673. //发送拍照计数指令
  1674. device_status.photo_ws = 2;
  1675. cal_n = device_status.cul_time * 60;
  1676. }
  1677. if(device_status.sleep_mode>0){
  1678. device_status.cul_time = 120;
  1679. if(cal_n==60){
  1680. ctr_router(ON);
  1681. ctr_RTU_DTU(ON);
  1682. }
  1683. if(cal_n==7000){
  1684. ctr_router(OFF);
  1685. ctr_RTU_DTU(OFF);
  1686. }
  1687. }
  1688. if(device_status.sleep_mode==10)
  1689. {
  1690. LOG_I("倒计时关闭联网模块电源:%d",sleep_cnt);
  1691. sleep_cnt++;
  1692. if(sleep_cnt>=60)
  1693. {
  1694. sleep_cnt = 0;
  1695. ctr_router(OFF);
  1696. ctr_RTU_DTU(OFF);
  1697. device_status.sleep_mode = 1;
  1698. }
  1699. }
  1700. if(device_status.temp_status==1 || device_status.bat_status==1 ||device_status.work_status==0) break;
  1701. rt_thread_delay(1000);
  1702. }
  1703. }
  1704. ctr_lamp(OFF, 1);
  1705. ctr_photo(OFF);
  1706. }
  1707. /* 入口函数-工作状态检测 */
  1708. static void Work_status_thread_entry(void *parameter)
  1709. {
  1710. static uint16_t sun_status_num = 120;
  1711. //如果是光控定时,进行光控监测的消抖
  1712. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1713. {
  1714. rt_kprintf("光控定时!!!\n");
  1715. while(sun_status_num!=0)
  1716. {
  1717. sun_status_num--;
  1718. rt_thread_delay(1000);
  1719. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1720. }
  1721. device_status.light_over_tim = device_status.sunBar * 3600;
  1722. device_status.test_flag = 0;
  1723. }
  1724. while(1)
  1725. {
  1726. WorkCheck();
  1727. }
  1728. }
  1729. /*入口函数-工作流程*/
  1730. static void Work_Loop_thread_entry(void *parameter)
  1731. {
  1732. rt_thread_delay(1000);
  1733. ResetInit();
  1734. while(1)
  1735. {
  1736. WorkTask_XYV3();
  1737. rt_thread_delay(1000);
  1738. }
  1739. }
  1740. /*入口函数清扫*/
  1741. static void Control_Clear_thread_entry(void *parameter)
  1742. {
  1743. static int selfcheck_cnt = 0;
  1744. while(1)
  1745. {
  1746. if(device_status.selfcheck_ws==1){
  1747. selfcheck_cnt++;
  1748. }else{
  1749. selfcheck_cnt = 0;
  1750. }
  1751. if(selfcheck_cnt>1200) //超过3分钟
  1752. {
  1753. device_status.selfcheck_ws = 0;
  1754. device_status.camera_sta = 1; //相机异常
  1755. //上报自检结果
  1756. //上报自检结果
  1757. char *json_temp = makeJson_selfCheck_(1);
  1758. DTU_Write("%s",json_temp);
  1759. free(json_temp);
  1760. }
  1761. if(device_status.clear_status==10){
  1762. //ctr_turn(90,0);
  1763. Motor_shake(1,800);
  1764. device_status.clear_status = 0;
  1765. }
  1766. rt_thread_delay(100);
  1767. }
  1768. }
  1769. /*入口函数加热*/
  1770. //static void Control_Heat_thread_entry(void *parameter)
  1771. //{
  1772. // rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1773. // rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1774. // static int GaoyYa_cnt,GuangShan_cnt = 500; //10s
  1775. // bool GaoYa = false;
  1776. // bool GuangShan = false;
  1777. // while(1)
  1778. // {
  1779. //
  1780. // if(rt_pin_read(SIGNAL_GaoYa)==0)
  1781. // {
  1782. // LOG_I("检测到高压计数!");
  1783. // GaoYa = true;
  1784. // }
  1785. //
  1786. // if(rt_pin_read(SIGNAL_GuangShan)==0)
  1787. // {
  1788. // LOG_I("检测到光栅计数!");
  1789. // GuangShan = true;
  1790. // }
  1791. //
  1792. // if(GaoYa)
  1793. // {
  1794. // GaoyYa_cnt --;
  1795. // if(GaoyYa_cnt==0)
  1796. // {
  1797. // GaoyYa_cnt = 500;
  1798. // GaoYa = false;
  1799. // }
  1800. // }
  1801. //
  1802. // if(GuangShan)
  1803. // {
  1804. // GuangShan_cnt--;
  1805. // if(GuangShan_cnt==0)
  1806. // {
  1807. // GuangShan_cnt = 500;
  1808. // GuangShan = false;
  1809. // }
  1810. // }
  1811. //
  1812. // if(GaoYa&&GuangShan)
  1813. // {
  1814. // device_status.all_ycct += 1; //计数值增加
  1815. // //拍照
  1816. // if(device_status.all_ycct>=1){
  1817. // //device_status.photo_ws = 1;
  1818. // }
  1819. // GaoYa = false;
  1820. // GuangShan = false;
  1821. // GaoyYa_cnt = 500;
  1822. // GuangShan_cnt = 500;
  1823. // rt_thread_delay(1000);
  1824. // }
  1825. // rt_thread_delay(20);
  1826. // }
  1827. //}
  1828. static void Control_Heat_thread_entry(void *parameter) {
  1829. rt_pin_mode(SIGNAL_GaoYa, PIN_MODE_INPUT);
  1830. rt_pin_mode(SIGNAL_GuangShan, PIN_MODE_INPUT);
  1831. // 修正变量名和初始化
  1832. static int GaoYa_cnt = 500, GuangShan_cnt = 500;
  1833. static bool GaoYa = false, GuangShan = false;
  1834. static int highPressureDuration = 0;
  1835. while (1) {
  1836. // 高压信号去抖动
  1837. if (rt_pin_read(SIGNAL_GaoYa) == 0) {
  1838. LOG_I("高压信号稳定触发");
  1839. GaoYa = true;
  1840. highPressureDuration ++;
  1841. }else{
  1842. highPressureDuration = 0;
  1843. }
  1844. // 光栅信号去抖动(类似逻辑)
  1845. if(rt_pin_read(SIGNAL_GuangShan)==0)
  1846. {
  1847. LOG_I("检测到光栅计数!");
  1848. GuangShan = true;
  1849. }
  1850. /* 高压持续时间监测 */
  1851. if(GaoYa)
  1852. {
  1853. // 持续时间累计(每次循环+1,20ms/次)
  1854. if(highPressureDuration >= 50)
  1855. {
  1856. LOG_E("高压持续超过5秒!");
  1857. // 执行关闭高压操作(示例函数)
  1858. ctr_lamp(OFF, 1);
  1859. // 可选:自动复位保护
  1860. rt_thread_mdelay(10000); // 等待2秒
  1861. ctr_lamp(ON, 1);
  1862. highPressureDuration = 0;
  1863. }
  1864. }
  1865. // 计数器逻辑
  1866. if (GaoYa) {
  1867. if (--GaoYa_cnt <= 0) {
  1868. GaoYa = false;
  1869. GaoYa_cnt = 500;
  1870. }
  1871. }
  1872. if (GuangShan) {
  1873. if (--GuangShan_cnt <= 0) {
  1874. GuangShan = false;
  1875. GuangShan_cnt = 500;
  1876. }
  1877. }
  1878. // 触发拍照逻辑
  1879. if (GaoYa && GuangShan) {
  1880. device_status.all_ycct++;
  1881. GaoYa = GuangShan = false;
  1882. GaoYa_cnt = GuangShan_cnt = 500;
  1883. }
  1884. rt_thread_delay(20); // 保持20ms周期
  1885. }
  1886. }
  1887. /*清扫线程*/
  1888. int Control_clear(void)
  1889. {
  1890. /* 创建线程*/
  1891. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1892. Control_Clear_thread_entry, RT_NULL,
  1893. 2048, //2048
  1894. 24, //23
  1895. WORK_CHECK_THREAD_TIMESLICE); //5
  1896. if(tid_ctr!=RT_NULL)
  1897. {
  1898. rt_thread_startup(tid_ctr);
  1899. }
  1900. return 0;
  1901. }
  1902. /*恒温加热线程*/
  1903. int Control_heat(void)
  1904. {
  1905. /* 创建线程*/
  1906. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  1907. Control_Heat_thread_entry, RT_NULL,
  1908. 1024, //2048
  1909. 25, //23
  1910. WORK_CHECK_THREAD_TIMESLICE); //5
  1911. if(tid_heat!=RT_NULL)
  1912. {
  1913. rt_thread_startup(tid_heat);
  1914. }
  1915. return 0;
  1916. }
  1917. /* 创建线程 -工作状态检测*/
  1918. int Workstatus_check(void)
  1919. {
  1920. /* 创建线程*/
  1921. tid_wkstc = rt_thread_create("work_status_check_thread",
  1922. Work_status_thread_entry, RT_NULL,
  1923. WORK_CHECK_THREAD_STACK_SIZE, //1024
  1924. WORK_CHECK_THREAD_PRIORITY, //23
  1925. WORK_CHECK_THREAD_TIMESLICE); //5
  1926. if(tid_wkstc!=RT_NULL)
  1927. {
  1928. rt_thread_startup(tid_wkstc);
  1929. }
  1930. return 0;
  1931. }
  1932. /*工作流程线程*/
  1933. int Work_Loop_Task(void)
  1934. {
  1935. rt_err_t result_loop = 0;
  1936. /* 创建线程*/
  1937. tid_workloop = rt_thread_create("work_loop_thread",
  1938. Work_Loop_thread_entry, RT_NULL,
  1939. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1940. WORK_LOOP_THREAD_PRIORITY, //12
  1941. WORK_LOOP_THREAD_TIMESLICE); //5
  1942. if(tid_workloop!=RT_NULL)
  1943. {
  1944. result_loop = rt_thread_startup(tid_workloop);
  1945. }
  1946. return result_loop;
  1947. }
  1948. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  1949. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1950. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  1951. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1952. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  1953. //INIT_APP_EXPORT(Control_heat);