control.c 44 KB

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