control.c 77 KB

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