control.c 63 KB

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