control.c 67 KB

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