control.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. //定时器
  37. rt_timer_t timer_shibie = RT_NULL;
  38. rt_timer_t timer_motor = RT_NULL;
  39. //线程
  40. rt_thread_t tid_workloop = RT_NULL;
  41. rt_thread_t tid_heat = RT_NULL;
  42. rt_thread_t tid_485_play = RT_NULL;
  43. rt_event_t event_485_play;
  44. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  45. /***************************************************************************/
  46. int thread_delete_workloop(void)
  47. {
  48. rt_err_t result;
  49. if(tid_workloop!=RT_NULL)
  50. {
  51. result = rt_thread_delete(tid_workloop);
  52. }
  53. return result;
  54. }
  55. int thread_restart_workloop(void)
  56. {
  57. rt_err_t result;
  58. result = Work_Loop_Task();
  59. return result;
  60. }
  61. /**************************1.0定制2000W*************************************/
  62. /****************************5.0***********************************/
  63. void ctr_turnv5(void)
  64. {
  65. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  66. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  67. // 转出
  68. rt_pin_write(CLEAR_R,ON);
  69. rt_pin_write(CLEAR_L,OFF);
  70. rt_thread_mdelay(300);
  71. //停止
  72. rt_pin_write(CLEAR_R,OFF);
  73. rt_pin_write(CLEAR_L,OFF);
  74. rt_thread_mdelay(300);
  75. //转入
  76. rt_pin_write(CLEAR_R,OFF);
  77. rt_pin_write(CLEAR_L,ON);
  78. rt_thread_mdelay(300);
  79. //停止
  80. rt_pin_write(CLEAR_R,OFF);
  81. rt_pin_write(CLEAR_L,OFF);
  82. rt_thread_mdelay(300);
  83. }
  84. /*************************吸虫塔*********************************/
  85. //吸虫电机
  86. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  87. {
  88. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  89. if(status_ws==ON)
  90. {
  91. rt_pin_write(MOTOR1_CTR_PIN,ON);
  92. }
  93. else {
  94. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  95. }
  96. return 0;
  97. }
  98. uint8_t ctr_Camera_Power(uint8_t status_ws)
  99. {
  100. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  101. if(status_ws==ON)
  102. {
  103. rt_pin_write(MOTOR1_CTR_PIN,ON);
  104. }
  105. else {
  106. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  107. }
  108. return 0;
  109. }
  110. //吸附电机
  111. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  112. {
  113. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  114. if(status_ws==ON)
  115. {
  116. rt_pin_write(MOTOR2_CTR_PIN,ON);
  117. }
  118. else {
  119. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  120. }
  121. return 0;
  122. }
  123. //清扫-卷带电机
  124. void ctr_JD_Clear(uint8_t shake_sw)
  125. {
  126. rt_uint16_t turn_timeout = 0;
  127. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  128. ctr_cangmen_up(OFF); //清扫传送带的电机
  129. if(shake_sw==ON)
  130. {
  131. ctr_Heat(ON); //清洁风机
  132. Motor_shake(ON, 0);
  133. ctr_cangmen_up(OPEN); //打开清扫
  134. }
  135. else {
  136. Motor_shake(OFF, 0);
  137. ctr_cangmen_up(OPEN); //打开清扫
  138. }
  139. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  140. {
  141. Motor_Juandai(ON);
  142. rt_thread_delay(50);
  143. turn_timeout++;
  144. if(turn_timeout==1000) break;
  145. }
  146. turn_timeout = 0;
  147. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  148. {
  149. Motor_Juandai(ON);
  150. turn_timeout++;
  151. rt_thread_delay(50);
  152. if(turn_timeout==1000) break;
  153. }
  154. Motor_shake(OFF, 1);
  155. Motor_Juandai(OFF);
  156. ctr_cangmen_up(OFF); //关闭清扫
  157. ctr_Heat(OFF);
  158. }
  159. /**********************************************************************/
  160. /*光控*/
  161. uint8_t In_Light_status(void)
  162. {
  163. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  164. return rt_pin_read(LIGHT_PIN);
  165. }
  166. /*雨控*/
  167. uint8_t In_Rain_status(void)
  168. {
  169. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  170. return rt_pin_read(RAIN_PIN);
  171. }
  172. /*加热*/
  173. uint8_t ctr_Heat(uint8_t status_heat)
  174. {
  175. LOG_I("操作加热:%d",status_heat);
  176. device_status.heat_status = status_heat;
  177. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  178. rt_pin_write(Heat_Pin,status_heat);
  179. return status_heat;
  180. }
  181. /*制冷*/
  182. uint8_t ctr_Cold(uint8_t status_cold)
  183. {
  184. device_status.cold_status = status_cold;
  185. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  186. rt_pin_write(Cold_Pin,status_cold);
  187. return status_cold;
  188. }
  189. /*相机供电*/
  190. void ctr_photo(uint8_t status_photo)
  191. {
  192. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  193. rt_pin_write(PHOTO,status_photo);
  194. }
  195. /*************************************************
  196. Function: ctr_Cool_Fan
  197. Description: 散热风机的开关
  198. Calls:
  199. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  200. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  201. Input: ON-开,OFF-关
  202. Output: NULL
  203. Return: ON-开,OFF-关
  204. Others: NULL
  205. *************************************************/
  206. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  207. {
  208. device_status.cool_fan_status = status_cool;
  209. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  210. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  211. if(status_cool==OFF)
  212. {
  213. rt_pin_write(Cool_Fan_L,OFF);
  214. rt_pin_write(Cool_Fan_R,OFF);
  215. }
  216. if(status_cool==ON)
  217. {
  218. rt_pin_write(Cool_Fan_L,ON);
  219. rt_pin_write(Cool_Fan_R,OFF);
  220. }
  221. return device_status.cool_fan_status;
  222. }
  223. /*************************************************
  224. Function: ctr_Collect_Fan
  225. Description: 采集风机的开关
  226. Calls:
  227. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  228. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  229. Input: ON-开,OFF-关
  230. Output: NULL
  231. Return: ON-开,OFF-关
  232. Others: NULL
  233. *************************************************/
  234. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  235. {
  236. LOG_I("操作风机:%d",status_collect);
  237. device_status.collect_fan_status = status_collect;
  238. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  239. if(status_collect==OFF)
  240. {
  241. rt_pin_write(Collect_Fan,OFF);
  242. }
  243. if(status_collect==ON)
  244. {
  245. rt_pin_write(Collect_Fan,ON);
  246. }
  247. return device_status.collect_fan_status;
  248. }
  249. /*************************************************
  250. Function: ctr_develop
  251. Description: 培养载玻片孢子
  252. Calls:
  253. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  254. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  255. Input: v=0(复位) v=90转90度
  256. Output: NULL
  257. Return: NULL
  258. Others: NULL
  259. *************************************************/
  260. /*上仓门*/
  261. uint8_t ctr_cangmen_up(uint8_t status_up)
  262. {
  263. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  264. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  265. if(status_up==OFF)
  266. {
  267. rt_pin_write(Collect_fan_R,ON);
  268. rt_pin_write(Collect_fan_L,ON);
  269. device_status.ctr_up_motor = 0;
  270. rt_thread_delay(1000);
  271. }
  272. if(status_up==OPEN)
  273. {
  274. device_status.up_status = 1;
  275. rt_pin_write(Collect_fan_R,OFF);
  276. rt_pin_write(Collect_fan_L,OFF);
  277. rt_pin_write(Collect_fan_R,ON);
  278. rt_pin_write(Collect_fan_L,OFF);
  279. }
  280. if(status_up==CLOSE)
  281. {
  282. device_status.up_status = 0;
  283. rt_pin_write(Collect_fan_R,OFF);
  284. rt_pin_write(Collect_fan_L,OFF);
  285. rt_pin_write(Collect_fan_R,OFF);
  286. rt_pin_write(Collect_fan_L,ON);
  287. }
  288. return device_status.up_status;
  289. }
  290. //下仓门
  291. uint8_t ctr_cangmen_down(uint8_t status_down)
  292. {
  293. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  294. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  295. if(status_down==OFF)
  296. {
  297. rt_pin_write(C_DOWN_R,ON);
  298. rt_pin_write(C_DOWN_L,ON);
  299. device_status.ctr_down_motor = 0;
  300. rt_thread_delay(1000);
  301. }
  302. if(status_down==OPEN)
  303. {
  304. device_status.down_status = 1;
  305. rt_pin_write(C_DOWN_R,OFF);
  306. rt_pin_write(C_DOWN_L,OFF);
  307. rt_pin_write(C_DOWN_R,ON);
  308. rt_pin_write(C_DOWN_L,OFF);
  309. }
  310. if(status_down==CLOSE)
  311. {
  312. device_status.down_status = 0;
  313. rt_pin_write(C_DOWN_R,OFF);
  314. rt_pin_write(C_DOWN_L,OFF);
  315. rt_pin_write(C_DOWN_R,OFF);
  316. rt_pin_write(C_DOWN_L,ON);
  317. }
  318. return device_status.down_status;
  319. }
  320. //镇流器
  321. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  322. {
  323. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  324. if(status_lamp==ON)
  325. {
  326. switch(lamp_num){
  327. case 1:
  328. rt_pin_write(LAMP_SW,ON);
  329. device_status.lamp_status = status_lamp;
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. else {
  336. switch(lamp_num){
  337. case 1:
  338. rt_pin_write(LAMP_SW,OFF);
  339. device_status.lamp_status = status_lamp;
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  346. return device_status.lamp_status;
  347. }
  348. //震动电机
  349. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  350. {
  351. static rt_uint16_t shake_i = 0;
  352. shake_i = atoi(device_status.power);
  353. LOG_I("震动电机待机电流:%d",shake_i);
  354. if(device_status.dtype==7&&shake_sec!=0)
  355. DTU_Write("drop");
  356. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  357. #if 0
  358. rt_pin_write(SHAKE_SW,ON);
  359. return 0;
  360. #endif
  361. if(status_shake==ON&&device_status.shake_ws==1)
  362. {
  363. #if 0
  364. for(int8_t s = 0;s++;s<2)
  365. {
  366. rt_pin_write(SHAKE_SW,ON);
  367. rt_thread_delay(100);
  368. rt_pin_write(SHAKE_SW,OFF);
  369. rt_thread_delay(1000);
  370. }
  371. #endif
  372. rt_pin_write(SHAKE_SW,ON);
  373. if(device_status.dtype==3)
  374. {
  375. while(shake_sec--)
  376. {
  377. rt_thread_delay(100);
  378. 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)
  379. {
  380. char * shake_warn = makeJson_Warn("电流不足", "震动电机");
  381. DTU_Write("%s",shake_warn);
  382. rt_free(shake_warn);
  383. }
  384. LOG_D("震动电机:%d",atoi(device_status.power));
  385. }
  386. }
  387. if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
  388. rt_pin_write(SHAKE_SW,OFF);
  389. HMI_WRITE("cangWei.switchShake.val=%d",0);
  390. }
  391. else
  392. {
  393. rt_pin_write(SHAKE_SW,OFF);
  394. HMI_WRITE("cangWei.switchShake.val=%d",0);
  395. }
  396. if(device_status.dtype==7)
  397. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  398. return status_shake;
  399. }
  400. //补光
  401. uint8_t ctr_led(uint8_t status_led)
  402. {
  403. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  404. if(device_status.version_type==6)
  405. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  406. if(status_led==ON)
  407. {
  408. rt_pin_write(LED_SW,ON);
  409. if(device_status.version_type==6)
  410. rt_pin_write(LED_P,ON);
  411. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  412. }
  413. else if(status_led==OFF){
  414. rt_pin_write(LED_SW,OFF);
  415. if(device_status.version_type==6)
  416. rt_pin_write(LED_P,OFF);
  417. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  418. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  419. }
  420. return status_led;
  421. }
  422. /*
  423. *
  424. * */
  425. void ctr_Motor_Driver_Power(uint8_t status_ws)
  426. {
  427. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  428. rt_pin_write(SHAKE_SW, status_ws);
  429. }
  430. /*************************************************
  431. Function: ctr_Motor_Driver
  432. Description: 步进电机驱动
  433. Calls:
  434. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  435. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  436. Input: 50000000
  437. Output: NULL
  438. Return: shake_sec
  439. Others: NULL
  440. *************************************************/
  441. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  442. {
  443. beep_init();
  444. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  445. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  446. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  447. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  448. if(EN==0)
  449. {
  450. rt_pin_write(Motor_EN, EN);
  451. }
  452. rt_pin_write(Motor_DIR, DIR);
  453. beep_set(pwm, pwm/2); //200kzh - 50%
  454. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  455. {
  456. if(time_out==0)
  457. {
  458. break;
  459. }
  460. rt_thread_delay(50);
  461. time_out --;
  462. }
  463. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  464. {
  465. if(time_out==0)
  466. {
  467. break;
  468. }
  469. rt_thread_delay(50);
  470. time_out --;
  471. }
  472. //rt_pin_write(Motor_EN, OFF);
  473. //if(device_status.autotakepic==0)
  474. //ctr_lamp(OFF, 1);
  475. beep_set(0, 0); //200kzh - 50%
  476. rt_thread_delay(50);
  477. }
  478. /*************************************************
  479. Function: ctr_Petroleum_jelly
  480. Description: 涂凡士林
  481. Calls:
  482. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  483. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  484. Input: NULL
  485. Output: NULL
  486. Return: NULL
  487. Others: NULL
  488. *************************************************/
  489. void ctr_Petroleum_jelly(rt_uint32_t delay)
  490. {
  491. beep_init();
  492. pwm_on(2,50000);
  493. rt_thread_delay(delay*1000);
  494. pwm_off(2,0);
  495. rt_thread_delay(1000);
  496. pwm_on(3, 50000);
  497. rt_thread_delay(delay*1000);
  498. pwm_off(3,0);
  499. device_status.fsl_sw = 0;
  500. }
  501. //1.0转仓
  502. void turnzp(void)
  503. {
  504. static uint16_t turn_timeout = 0;
  505. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  506. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  507. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  508. // rt_pin_write(CLEAR_R,OFF);
  509. // rt_pin_write(CLEAR_L,OFF);
  510. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  511. {
  512. rt_thread_delay(20);
  513. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  514. {
  515. // rt_pin_write(CLEAR_R,ON);
  516. // rt_pin_write(CLEAR_L,OFF);
  517. Motor_Juandai(ON);
  518. rt_thread_delay(50);
  519. turn_timeout++;
  520. if(turn_timeout==600)
  521. {
  522. LOG_E("检测不到限位,电机强制停止");
  523. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  524. DTU_Write("%s",ctr_zp_limit_warn);
  525. rt_free(ctr_zp_limit_warn);
  526. break;
  527. }
  528. }
  529. break;
  530. }
  531. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  532. {
  533. rt_thread_delay(20);
  534. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  535. {
  536. // rt_pin_write(CLEAR_R,ON);
  537. // rt_pin_write(CLEAR_L,OFF);
  538. Motor_Juandai(ON);
  539. rt_thread_delay(50);
  540. turn_timeout++;
  541. if(turn_timeout==600)
  542. {
  543. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  544. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  545. DTU_Write("%s",ctr_zp_limit_warn);
  546. rt_free(ctr_zp_limit_warn);
  547. break;
  548. }
  549. }
  550. break;
  551. }
  552. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  553. {
  554. rt_thread_delay(20);
  555. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  556. {
  557. // rt_pin_write(CLEAR_R,OFF);
  558. // rt_pin_write(CLEAR_L,OFF);
  559. Motor_Juandai(OFF);
  560. break;
  561. }
  562. else {
  563. break;
  564. }
  565. }
  566. Motor_Juandai(OFF);
  567. // rt_pin_write(CLEAR_R,OFF);
  568. // rt_pin_write(CLEAR_L,OFF);
  569. turn_timeout = 0;
  570. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  571. HMI_WRITE("sysTest.switchClear.val=%d",0);
  572. rt_thread_delay(1000);
  573. }
  574. //清扫-常闭型限位开关
  575. //新结构使用推杆电机做清扫
  576. uint8_t ctr_clear(uint16_t status_clear)
  577. {
  578. static uint16_t clear_num = 0;
  579. #if PWM_EN == 0
  580. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  581. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  582. #endif
  583. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  584. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  585. /*************************************************************外接清扫
  586. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  587. rt_pin_write(POWER_CLEAR,ON);
  588. rt_thread_delay(1000*100);
  589. rt_pin_write(POWER_CLEAR,OFF);
  590. ************************************************************/
  591. //停止
  592. #if PWM_EN == 0
  593. rt_pin_write(CLEAR_R,OFF);
  594. rt_pin_write(CLEAR_L,OFF);
  595. #else
  596. Motor_Qingsao_R_PWM(OFF);
  597. Motor_Qingsao_L_PWM(OFF);
  598. #endif
  599. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  600. rt_thread_delay(100);
  601. //如果清扫位置在中间
  602. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  603. {
  604. #if PWM_EN == 0
  605. rt_pin_write(CLEAR_R,ON);
  606. rt_pin_write(CLEAR_L,OFF);
  607. #else
  608. Motor_Qingsao_R_PWM(ON);
  609. Motor_Qingsao_L_PWM(OFF);
  610. #endif
  611. rt_thread_delay(50);
  612. clear_num++;
  613. //已到达检测位置
  614. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  615. {
  616. #if PWM_EN == 0
  617. rt_pin_write(CLEAR_R,ON);
  618. rt_pin_write(CLEAR_L,ON);
  619. #else
  620. Motor_Qingsao_R_PWM(OFF);
  621. Motor_Qingsao_L_PWM(OFF);
  622. #endif
  623. rt_thread_delay(2000);
  624. break;
  625. }
  626. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  627. {
  628. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  629. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  630. #if PWM_EN == 0
  631. rt_pin_write(CLEAR_R,ON);
  632. rt_pin_write(CLEAR_L,ON);
  633. #else
  634. Motor_Qingsao_R_PWM(OFF);
  635. Motor_Qingsao_L_PWM(OFF);
  636. #endif
  637. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  638. if(Read_Real_Voltage()/100<12)
  639. {
  640. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  641. HMI_WRITE("sysTest.switchClear.val=%d",0);
  642. device_status.clear_status = 0;
  643. return 0;
  644. }
  645. break;
  646. }
  647. }
  648. clear_num = 0;
  649. //如果清扫位置在左侧
  650. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  651. {
  652. #if PWM_EN == 0
  653. rt_pin_write(CLEAR_R,ON);
  654. rt_pin_write(CLEAR_L,OFF);
  655. #else
  656. Motor_Qingsao_R_PWM(ON);
  657. Motor_Qingsao_L_PWM(OFF);
  658. #endif
  659. rt_thread_delay(50);
  660. clear_num++;
  661. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  662. {
  663. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  664. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  665. #if PWM_EN == 0
  666. rt_pin_write(CLEAR_R,ON);
  667. rt_pin_write(CLEAR_L,ON);
  668. #else
  669. Motor_Qingsao_R_PWM(OFF);
  670. Motor_Qingsao_L_PWM(OFF);
  671. #endif
  672. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  673. if(Read_Real_Voltage()/100<12)
  674. {
  675. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  676. HMI_WRITE("sysTest.switchClear.val=%d",0);
  677. device_status.clear_status = 0;
  678. return 0;
  679. }
  680. break;
  681. }
  682. }
  683. clear_num = 0;
  684. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  685. {
  686. #if PWM_EN == 0
  687. rt_pin_write(CLEAR_R,ON);
  688. rt_pin_write(CLEAR_L,ON);
  689. #else
  690. Motor_Qingsao_R_PWM(OFF);
  691. Motor_Qingsao_L_PWM(OFF);
  692. #endif
  693. if(Read_Real_Voltage()/100<12)
  694. {
  695. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  696. HMI_WRITE("sysTest.switchClear.val=%d",0);
  697. device_status.clear_status = 0;
  698. return 0;
  699. }
  700. rt_thread_delay(2000);
  701. break;
  702. }
  703. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  704. {
  705. #if PWM_EN == 0
  706. rt_pin_write(CLEAR_R,OFF);
  707. rt_pin_write(CLEAR_L,ON);
  708. #else
  709. Motor_Qingsao_R_PWM(OFF);
  710. Motor_Qingsao_L_PWM(ON);
  711. #endif
  712. rt_thread_delay(50);
  713. clear_num++;
  714. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  715. {
  716. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  717. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  718. //DTU_Write("%s",Clear_ERR);
  719. #if PWM_EN == 0
  720. rt_pin_write(CLEAR_R,ON);
  721. rt_pin_write(CLEAR_L,ON);
  722. #else
  723. Motor_Qingsao_R_PWM(OFF);
  724. Motor_Qingsao_L_PWM(OFF);
  725. #endif
  726. rt_free(Clear_ERR);
  727. if(Read_Real_Voltage()/100<12)
  728. {
  729. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  730. HMI_WRITE("sysTest.switchClear.val=%d",0);
  731. device_status.clear_status = 0;
  732. return 0;
  733. }
  734. break;
  735. }
  736. }
  737. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  738. {
  739. #if PWM_EN == 0
  740. rt_pin_write(CLEAR_R,ON);
  741. rt_pin_write(CLEAR_L,ON);
  742. #else
  743. Motor_Qingsao_R_PWM(OFF);
  744. Motor_Qingsao_L_PWM(OFF);
  745. #endif
  746. rt_thread_delay(2000);
  747. break;
  748. }
  749. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  750. {
  751. #if PWM_EN == 0
  752. rt_pin_write(CLEAR_R,ON);
  753. rt_pin_write(CLEAR_L,ON);
  754. #else
  755. Motor_Qingsao_R_PWM(OFF);
  756. Motor_Qingsao_L_PWM(OFF);
  757. #endif
  758. if(device_status.version_type==41)
  759. {
  760. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  761. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  762. //DTU_Write("%s",Clear_ERR);
  763. rt_free(Clear_ERR);
  764. }
  765. return 0;
  766. }
  767. clear_num = 0;
  768. rt_thread_delay(2000);
  769. #if PWM_EN == 0
  770. rt_pin_write(CLEAR_R,ON);
  771. rt_pin_write(CLEAR_L,ON);
  772. #else
  773. Motor_Qingsao_R_PWM(OFF);
  774. Motor_Qingsao_L_PWM(OFF);
  775. #endif
  776. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  777. HMI_WRITE("sysTest.switchClear.val=%d",0);
  778. device_status.clear_status = 0;
  779. return 1;
  780. }
  781. //路由器供电
  782. void ctr_router(uint8_t status_router)
  783. {
  784. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  785. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  786. if(status_router==ON)
  787. {
  788. rt_pin_write(ROUTER,ON);
  789. }
  790. else {
  791. rt_pin_write(ROUTER,OFF);
  792. }
  793. }
  794. //清扫-推杆电机
  795. void ctr_TG_Clear(uint16_t clear_timeout)
  796. {
  797. #if 0
  798. int current_clear[200];
  799. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  800. LOG_I("清扫电机待机:%d",Electric_current);
  801. #endif
  802. static uint16_t clear_count = 0;
  803. clear_count = clear_timeout;
  804. #if PWM_EN == 0
  805. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  806. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  807. #endif
  808. while(clear_count!=0)
  809. {
  810. #if PWM_EN == 0
  811. rt_pin_write(CLEAR_R,ON);
  812. rt_pin_write(CLEAR_L,OFF);
  813. #else
  814. Motor_Qingsao_R_PWM(ON);
  815. Motor_Qingsao_L_PWM(OFF);
  816. #endif
  817. clear_count--;
  818. rt_thread_delay(100);
  819. }
  820. #if PWM_EN == 0
  821. rt_pin_write(CLEAR_R,ON);
  822. rt_pin_write(CLEAR_L,ON);
  823. #else
  824. Motor_Qingsao_R_PWM(OFF);
  825. Motor_Qingsao_L_PWM(OFF);
  826. #endif
  827. rt_thread_delay(3000);
  828. clear_count = 0;
  829. while(clear_timeout!=clear_count)
  830. {
  831. #if PWM_EN == 0
  832. rt_pin_write(CLEAR_R,OFF);
  833. rt_pin_write(CLEAR_L,ON);
  834. #else
  835. Motor_Qingsao_R_PWM(OFF);
  836. Motor_Qingsao_L_PWM(ON);
  837. #endif
  838. // if(clear_count<200)
  839. // current_clear[clear_count] = atoi(device_status.power);
  840. clear_count++;
  841. rt_thread_delay(100);
  842. }
  843. #if PWM_EN == 0
  844. rt_pin_write(CLEAR_R,ON);
  845. rt_pin_write(CLEAR_L,ON);
  846. #else
  847. Motor_Qingsao_R_PWM(OFF);
  848. Motor_Qingsao_L_PWM(OFF);
  849. #endif
  850. #if 0
  851. bubblesort(current_clear,200);
  852. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  853. {
  854. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  855. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  856. DTU_Write("%s",ctr_clear_warn);
  857. rt_free(ctr_clear_warn);
  858. }
  859. #endif
  860. rt_thread_delay(2000);
  861. device_status.clear_status = 0;
  862. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  863. HMI_WRITE("sysTest.switchClear.val=%d",0);
  864. }
  865. //DTU,RTU供电
  866. void ctr_RTU_DTU(uint8_t status_rtu)
  867. {
  868. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  869. if(status_rtu==ON)
  870. {
  871. rt_pin_write(RTU_DTU,ON);
  872. }
  873. else
  874. {
  875. LOG_W("RTU电源复位!!!");
  876. rt_pin_write(RTU_DTU,OFF);
  877. }
  878. }
  879. //海康拍照
  880. void Hk_Take_io(void)
  881. {
  882. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  883. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  884. rt_pin_write(HK_TAKE, 1);
  885. rt_pin_write(HK_IO, 0);
  886. if(device_status.dtype==3)
  887. Beep_Tip(1000);
  888. if(device_status.dtype==7)
  889. rt_thread_mdelay(1000);
  890. rt_pin_write(HK_TAKE, 0);
  891. rt_pin_write(HK_IO, 1);
  892. }
  893. //拍照
  894. void HK_TakePhoto(void)
  895. {
  896. //打开补光灯
  897. //DTU_Write("takephoto.mp3");
  898. rt_thread_delay(10000);
  899. rt_timer_start(timer_takephoto);
  900. char *takephotojson = makeJson_takephoto();
  901. DTU_Write("%s",takephotojson);
  902. rt_free(takephotojson);
  903. //吸虫塔
  904. if(device_status.version_type!=6)
  905. {
  906. ctr_led(ON);
  907. if(device_status.version_type==48)
  908. {
  909. rt_thread_delay(1000*60);
  910. }
  911. else {
  912. rt_thread_delay(1000*15);
  913. }
  914. }
  915. //触发海康相机拍照
  916. Hk_Take_io();
  917. if(device_status.version_type!=6)
  918. {
  919. rt_thread_delay(1000*45);
  920. }
  921. else {
  922. ctr_led(ON);
  923. rt_thread_delay(1000*19);
  924. }
  925. ctr_led(OFF);
  926. }
  927. //光控测试任务
  928. void SunTest(void)
  929. {
  930. rt_uint16_t ui_timeout_l = 0;
  931. //未检测到光照,一秒扫描等待,超时30S
  932. while(Read_Light_Voltage()<500)
  933. {
  934. ui_timeout_l ++;
  935. rt_thread_delay(1000);
  936. if(ui_timeout_l==30) break;
  937. }
  938. rt_thread_delay(3000);
  939. if(Read_Light_Voltage()<500)
  940. {
  941. device_status.sun_status = 0;//光控状态
  942. }
  943. if(Read_Light_Voltage()>500)
  944. {
  945. rt_thread_delay(1000);
  946. }
  947. rt_thread_delay(3000);
  948. if(Read_Light_Voltage()>500)
  949. {
  950. device_status.sun_status = 1;
  951. ctr_lamp(OFF,1);
  952. }
  953. HMI_WRITE("sysTest.switchSun.val=%d",0);
  954. device_status.sun_test = 0;
  955. device_status.work_status=0;
  956. }
  957. /*上电初始化*/
  958. void ResetInit(void)
  959. {
  960. if(device_status.version_type==5)
  961. {
  962. ctr_lamp(OFF, 1); //镇流器
  963. ctr_led(OFF); //补光灯关闭
  964. ctr_Heat(OFF); //风机关闭
  965. ctr_Motor_Driver_Power(ON); //步进电机驱动电源开启
  966. ctr_Motor86_move(0, 500000, 0); //86电机复位
  967. device_status.ctr_up_motor = 2; //推杆电机缩进去(落虫)
  968. ctr_Collect_Fan(OFF);
  969. PWM2_CH3_PB10_Fan_LCTD(OFF);
  970. ctr_Motor_Driver_Power(OFF);
  971. }
  972. if(device_status.power_status==1)
  973. {
  974. //1.打开路由器供电
  975. ctr_router(ON);
  976. ctr_RTU_DTU(ON);
  977. //2.打开相机供电
  978. ctr_photo(ON);
  979. }
  980. }
  981. void WorkCheck(void)
  982. {
  983. static rt_uint32_t light_timeout = 3600;
  984. while(device_status.ds == 1 &&
  985. device_status.rain_status == 0 &&
  986. device_status.temp_status == 0 &&
  987. device_status.bat_status == 0)
  988. {
  989. if(device_status.timeSwitch == 1) // 时控模式
  990. {
  991. LOG_I("***********时控*****************");
  992. time_t now;
  993. time(&now);
  994. struct tm now_tm = *localtime(&now);
  995. if(device_status.startBar == device_status.stopBar)
  996. {
  997. // 开始结束时间相同则24小时模式
  998. device_status.work_status = 1;
  999. rt_thread_delay(10000);
  1000. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1001. device_status.work_status ? "工作" : "待机",
  1002. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1003. device_status.startBar, device_status.stopBar);
  1004. continue;
  1005. }
  1006. rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
  1007. rt_uint32_t start_sec = device_status.startBar * 3600;
  1008. rt_uint32_t end_sec = device_status.stopBar * 3600;
  1009. // 修复跨天判断逻辑
  1010. if (start_sec < end_sec)
  1011. {
  1012. // 不跨天模式
  1013. device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
  1014. }
  1015. else
  1016. {
  1017. // 跨天模式
  1018. device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
  1019. }
  1020. LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
  1021. device_status.work_status ? "工作" : "待机",
  1022. now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
  1023. device_status.startBar, device_status.stopBar);
  1024. rt_thread_delay(10000);
  1025. }
  1026. else // 光控模式
  1027. {
  1028. LOG_I("光控定时模式!");
  1029. if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
  1030. {
  1031. device_status.light_over_tim--;
  1032. light_timeout = 3600; // 重置超时计数器
  1033. device_status.work_status = 1;
  1034. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1035. }
  1036. else
  1037. {
  1038. // 仅在白天递减超时计数器
  1039. if(device_status.sun_status == 1 && light_timeout > 0) {
  1040. light_timeout--;
  1041. }
  1042. device_status.work_status = 0;
  1043. //没有在测试并且没有在自检
  1044. if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
  1045. {
  1046. ctr_lamp(OFF,1);
  1047. }
  1048. // 连续1小时白天后重置
  1049. if(light_timeout == 0) {
  1050. device_status.light_over_tim = 0;
  1051. light_timeout = 3600; // 重置防止负数
  1052. }
  1053. // 白天且倒计时结束:重置夜晚时长
  1054. if(device_status.light_over_tim == 0 &&
  1055. device_status.sun_status == 1)
  1056. {
  1057. device_status.light_over_tim = device_status.sunBar * 3600;
  1058. }
  1059. }
  1060. }
  1061. rt_thread_delay(1000);
  1062. }
  1063. // 异常状态处理
  1064. device_status.work_status = 0;
  1065. rt_thread_delay(1000);
  1066. }
  1067. void guanshan_on(void *args)
  1068. {
  1069. LOG_W("guanshan_on!");
  1070. rt_timer_start(timer_shibie);
  1071. rt_event_send(event_485_play, 0x01);
  1072. }
  1073. void pin_guangshan_bind(void)
  1074. {
  1075. LOG_W("pin_guangshan_bind");
  1076. /* PC14引脚为输入模式 */
  1077. rt_pin_mode(GUANG_SHAN, PIN_MODE_INPUT);
  1078. /* 绑定中断,下降沿模式,回调函数名为guanshan_on */
  1079. rt_pin_attach_irq(GUANG_SHAN, PIN_IRQ_MODE_RISING, guanshan_on, RT_NULL);
  1080. /* 使能中断 */
  1081. rt_pin_irq_enable(GUANG_SHAN, PIN_IRQ_ENABLE);
  1082. }
  1083. void pin_guangshan_unbind(void)
  1084. {
  1085. LOG_D("pin_guangshan_unbind");
  1086. rt_pin_detach_irq(GUANG_SHAN);
  1087. }
  1088. #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
  1089. void slidelock()
  1090. {
  1091. ctr_Motor_Driver_Power(ON); //电机驱动供电
  1092. rt_thread_delay(100);
  1093. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  1094. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  1095. }
  1096. void motor_zero()
  1097. {
  1098. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  1099. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  1100. rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
  1101. rt_pin_mode(CTR_RC_LIMIT, PIN_MODE_OUTPUT); //CW PC6 配置为输出模式
  1102. rt_thread_delay(300);
  1103. rt_thread_delay(300);
  1104. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT); //GET_PIN(B,8)
  1105. rt_pin_write(CTR_RO_LIMIT, OFF); //CCW back配置
  1106. uint32_t j;
  1107. for(j=0;j<3200;j++) //多配置1600个脉冲,保证肯定能归零;
  1108. {
  1109. rt_pin_write(CTR_RC_LIMIT, OFF);
  1110. rt_thread_delay(PULES);
  1111. rt_pin_write(CTR_RC_LIMIT, ON);
  1112. rt_thread_delay(PULES);
  1113. if( rt_pin_read(Motor_Direction_Button)==0)//判断得到限位信号
  1114. {
  1115. rt_thread_delay(5);
  1116. if( rt_pin_read(Motor_Direction_Button)==0)
  1117. {
  1118. slidelock();
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. rt_thread_delay(300);
  1124. }
  1125. uint16_t ctr_Motor86_move(uint32_t xdis,uint32_t pulse,uint8_t dir)
  1126. {
  1127. //rt_kprintf("run here1");
  1128. LOG_W("ctr_Motor86_move:%d-%d-%d",xdis,pulse,dir); //两个脉冲移动0.72度,轴方向上移动2um
  1129. uint32_t limit_tm= 0;
  1130. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  1131. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  1132. rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
  1133. rt_pin_write(CTR_RO_LIMIT, dir); //CCW 置高
  1134. rt_thread_delay(50);
  1135. pwm_motor86_enable(pulse);
  1136. rt_thread_delay(50);
  1137. rt_timer_start(timer_motor);
  1138. //启动定时器
  1139. return xdis;
  1140. }
  1141. /*
  1142. * 灯管
  1143. *
  1144. *
  1145. * */
  1146. void WorkTask_CB5(void)
  1147. {
  1148. static uint8_t guangshan_hand = 0;
  1149. static int work_loop = 0;
  1150. while(device_status.work_status==1&&
  1151. device_status.temp_status==0&&
  1152. device_status.bat_status==0&&
  1153. device_status.rain_status==0&&
  1154. device_status.ds==1)
  1155. {
  1156. work_loop++;
  1157. device_status.work_done = 0;
  1158. //发送串口消息,通知yolo是被及相机采集模块启动,定期将模块的运行状态展示到屏幕上
  1159. if(work_loop%60==0||work_loop==1)
  1160. {
  1161. //推杆电机1打开,导流板到落虫通道
  1162. device_status.ctr_up_motor = 1;
  1163. while(device_status.ctr_up_motor!=0)
  1164. {
  1165. rt_thread_delay(1000);
  1166. LOG_I("等待上仓门打开....");
  1167. }
  1168. //打开86步进电机驱动器供电
  1169. ctr_Motor_Driver_Power(ON);
  1170. //风机启动
  1171. rt_thread_delay(1000);
  1172. //打开灯管
  1173. ctr_lamp(ON,1);
  1174. DTU_Write("{\"cmd\":\"workstart\"}");
  1175. //打开补光灯
  1176. ctr_led(ON);
  1177. rt_thread_delay(1000);
  1178. //光栅启动
  1179. ctr_photo(ON);
  1180. rt_thread_delay(1000);
  1181. //上风机启动
  1182. //PWM2_CH3_PB10_Fan_LCTD(ON);
  1183. ctr_Collect_Fan(ON);
  1184. rt_thread_delay(1000);
  1185. //下风机启动
  1186. ctr_Heat(ON);
  1187. rt_thread_delay(1000);
  1188. //光栅检测启动
  1189. pin_guangshan_bind();
  1190. //电机限位检测启动
  1191. pin_motor_86_bind();
  1192. rt_thread_delay(1000);
  1193. while(rt_pin_read(GUANG_SHAN)==0)
  1194. {
  1195. guangshan_hand++;
  1196. rt_thread_delay(100);
  1197. if(guangshan_hand>5) break;
  1198. }
  1199. if(guangshan_hand>5)
  1200. {
  1201. guangshan_hand = 0;
  1202. device_status.autotakepic = 11;
  1203. }
  1204. }
  1205. rt_thread_delay(1000);
  1206. }
  1207. device_status.work_status = 0;
  1208. //工作结束
  1209. if(device_status.work_done==0&&device_status.work_status==0)
  1210. {
  1211. pin_guangshan_unbind();
  1212. pin_motor_86_unbind();
  1213. //打开推杆电机,益虫放生
  1214. DTU_Write("{\"cmd\":\"workdone\"}");
  1215. ResetInit();
  1216. }
  1217. work_loop = 0;
  1218. while(device_status.work_status==0)
  1219. {
  1220. work_loop++;
  1221. LOG_I("WorkTask_CB5 sleeping......%d",work_loop);
  1222. if(work_loop>120)
  1223. {
  1224. work_loop = 0;
  1225. DTU_Write("{\"cmd\":\"workdone\"}");
  1226. }
  1227. rt_thread_delay(5000);
  1228. }
  1229. }
  1230. /* 入口函数-工作状态检测 */
  1231. static void Work_status_thread_entry(void *parameter)
  1232. {
  1233. static uint16_t sun_status_num = 120;
  1234. //如果是光控定时,进行光控监测的消抖
  1235. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  1236. {
  1237. rt_kprintf("光控定时!!!\n");
  1238. while(sun_status_num!=0)
  1239. {
  1240. sun_status_num--;
  1241. rt_thread_delay(1000);
  1242. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  1243. }
  1244. device_status.light_over_tim = device_status.sunBar * 3600;
  1245. device_status.test_flag = 0;
  1246. }
  1247. while(1)
  1248. {
  1249. WorkCheck();
  1250. }
  1251. }
  1252. /*入口函数-工作流程*/
  1253. static void Work_Loop_thread_entry(void *parameter)
  1254. {
  1255. rt_thread_delay(1000);
  1256. ResetInit();
  1257. while(1)
  1258. {
  1259. WorkTask_CB5();
  1260. rt_thread_delay(1000);
  1261. }
  1262. }
  1263. /*入口函数清扫*/
  1264. static void Control_Clear_thread_entry(void *parameter)
  1265. {
  1266. static uint32_t rain_disable = 0;
  1267. while(1)
  1268. {
  1269. if(device_status.update_flag==1) break;
  1270. if(device_status.clear_status==1)
  1271. {
  1272. if(device_status.version_type==41){
  1273. ctr_TG_Clear(300);
  1274. }else {
  1275. ctr_TG_Clear(350);
  1276. }
  1277. }
  1278. rt_thread_delay(100);
  1279. }
  1280. }
  1281. /*入口函数,自检*/
  1282. static void Control_Check_thread_entry(void *parameter)
  1283. {
  1284. static int total_seconds = 200;
  1285. static int current_second = 0;
  1286. static int process = 0;
  1287. #if MATCH_DEVICE == 1
  1288. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  1289. rt_pin_write(LED_P,ON);
  1290. #endif
  1291. device_status.C01 = 0;
  1292. rt_thread_delay(10000);
  1293. while(1)
  1294. {
  1295. if(device_status.update_flag==1) break;
  1296. if(device_status.version_type==5) break;
  1297. if(device_status.selfcheck_ws==2)
  1298. {
  1299. while(current_second <= total_seconds)
  1300. {
  1301. process = (current_second * 100) / total_seconds;
  1302. device_status.process = process / 100.0;
  1303. LOG_I("process:%d",process);
  1304. HMI_WRITE("selfcheck.s106.val=%d",process);
  1305. rt_thread_delay(1000);
  1306. current_second++;
  1307. if(device_status.selfcheck_ws==0)
  1308. {
  1309. HMI_WRITE("selfcheck.s106.val=100");
  1310. break;
  1311. }
  1312. }
  1313. HMI_WRITE("selfcheck.s106.val=100");
  1314. }else {
  1315. process = 0;
  1316. current_second = 0;
  1317. }
  1318. rt_thread_delay(1000);
  1319. }
  1320. }
  1321. static void thread_485_play_entry(void *param)
  1322. {
  1323. static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
  1324. rt_uint32_t e;
  1325. // 创建事件控制块
  1326. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  1327. if (event_485_play == RT_NULL)
  1328. {
  1329. LOG_E("Event create failed!");
  1330. return;
  1331. }
  1332. while(1)
  1333. {
  1334. if(device_status.update_flag==1) break;
  1335. //等待事件
  1336. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  1337. {
  1338. LOG_I("Interrupt occurred and processed.");
  1339. _485_play(sound105, 4);
  1340. rt_thread_delay(3000);
  1341. //发送串口消息,通知JETSON计算卡进行拍照
  1342. DTU_Write("{\"guangshan\":\"on\"}");
  1343. }
  1344. }
  1345. }
  1346. /*清扫线程*/
  1347. int Control_clear(void)
  1348. {
  1349. /* 创建线程*/
  1350. rt_thread_t tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  1351. Control_Clear_thread_entry, RT_NULL,
  1352. 2048, //2048
  1353. 24, //23
  1354. WORK_CHECK_THREAD_TIMESLICE); //5
  1355. if(tid_ctr!=RT_NULL)
  1356. {
  1357. rt_thread_startup(tid_ctr);
  1358. }
  1359. return 0;
  1360. }
  1361. /*恒温加热线程*/
  1362. int Control_Check(void)
  1363. {
  1364. /* 创建线程*/
  1365. tid_heat = rt_thread_create("Control_Check_thread_entry",
  1366. Control_Check_thread_entry, RT_NULL,
  1367. 1024, //2048
  1368. 25, //23
  1369. WORK_CHECK_THREAD_TIMESLICE); //5
  1370. if(tid_heat!=RT_NULL)
  1371. {
  1372. rt_thread_startup(tid_heat);
  1373. }
  1374. return 0;
  1375. }
  1376. int thread_485_play(void)
  1377. {
  1378. tid_485_play = rt_thread_create("thread_485play",
  1379. thread_485_play_entry, RT_NULL,
  1380. 1024, //1024
  1381. 26, //23
  1382. 5); //5
  1383. if(tid_485_play!=RT_NULL)
  1384. {
  1385. rt_thread_startup(tid_485_play);
  1386. }
  1387. else {
  1388. LOG_E("thread_485_play start failed!");
  1389. }
  1390. return 0;
  1391. }
  1392. /* 创建线程 -工作状态检测*/
  1393. int Workstatus_check(void)
  1394. {
  1395. /* 创建线程*/
  1396. rt_thread_t tid_wkstc = rt_thread_create("work_status_check_thread",
  1397. Work_status_thread_entry, RT_NULL,
  1398. WORK_CHECK_THREAD_STACK_SIZE, //1024
  1399. WORK_CHECK_THREAD_PRIORITY, //23
  1400. WORK_CHECK_THREAD_TIMESLICE); //5
  1401. if(tid_wkstc!=RT_NULL)
  1402. {
  1403. rt_thread_startup(tid_wkstc);
  1404. }
  1405. return 0;
  1406. }
  1407. /*工作流程线程*/
  1408. int Work_Loop_Task(void)
  1409. {
  1410. rt_err_t result_loop = 0;
  1411. /* 创建线程*/
  1412. tid_workloop = rt_thread_create("work_loop_thread",
  1413. Work_Loop_thread_entry, RT_NULL,
  1414. WORK_LOOP_THREAD_STACK_SIZE, //2048
  1415. WORK_LOOP_THREAD_PRIORITY, //12
  1416. WORK_LOOP_THREAD_TIMESLICE); //5
  1417. if(tid_workloop!=RT_NULL)
  1418. {
  1419. result_loop = rt_thread_startup(tid_workloop);
  1420. }
  1421. return result_loop;
  1422. }
  1423. void timer_shibie_stop()
  1424. {
  1425. rt_timer_stop(timer_shibie);
  1426. LOG_W("timer_shibie_stop!");
  1427. }
  1428. void timer_motor_stop()
  1429. {
  1430. rt_timer_stop(timer_motor);
  1431. LOG_W("timer_motor_stop!");
  1432. }
  1433. static void t_shibie_callback(void *parameter)
  1434. {
  1435. LOG_E("识别超时!");
  1436. device_status.autotakepic = 10;
  1437. }
  1438. static void t_motor_callback(void *parameter)
  1439. {
  1440. LOG_E("电机超时!");
  1441. stop_motor_86();
  1442. }
  1443. int rt_software_timer_init(void)
  1444. {
  1445. //创建拍照超时定时器
  1446. timer_shibie = rt_timer_create("t_shibie",t_shibie_callback,RT_NULL,15000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
  1447. //创建电机运行超时定时器
  1448. timer_motor = rt_timer_create("t_motor",t_motor_callback,RT_NULL,2000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER); //2S
  1449. if(timer_motor==RT_NULL||timer_shibie==RT_NULL)
  1450. {
  1451. LOG_E("rt_timer_create t_timer failed!");
  1452. return RT_ERROR;
  1453. }
  1454. else
  1455. {
  1456. return RT_EOK;
  1457. }
  1458. }
  1459. INIT_APP_EXPORT(rt_software_timer_init);
  1460. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  1461. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  1462. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  1463. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  1464. //INIT_APP_EXPORT(Control_heat);