control.c 76 KB

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