control.c 75 KB

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