control.c 49 KB

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