| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401 |
- #include <control.h>
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-30 Administrator the first version
- * 2025-06-26 RTC闹钟使用定时开启和关闭路由器供电
- */
- #include "beep.h"
- #include "hmi.h"
- #include "easyflash.h"
- #include "stdio.h"
- #include "sound.h"
- #include "dtu_uart2.h"
- #include "sht30_collect.h"
- #include <user_mb_app.h>
- #include <selfcheck.h>
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "control"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- #include "sys_monitor.h"
- //工作检测
- #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
- #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
- #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
- //工作流程
- #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
- #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
- #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
- #define PWM_EN 1 //0关闭,1-打开
- //设备状态表结构体
- Device_Status_typedef device_status;
- System_stats_t system_status;
- rt_thread_t tid_workloop = RT_NULL;
- rt_thread_t tid_wkstc = RT_NULL;
- rt_thread_t tid_ctr = RT_NULL;
- rt_thread_t tid_heat = RT_NULL;
- rt_thread_t tid_485_play = RT_NULL;
- extern rt_device_t dtuserial; /*串口设备句柄*/
- rt_event_t event_485_play;
- extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
- /***************************************************************************/
- int thread_delete_workloop(void)
- {
- rt_err_t result;
- if(tid_workloop!=RT_NULL)
- {
- result = rt_thread_delete(tid_workloop);
- }
- return result;
- }
- int thread_restart_workloop(void)
- {
- rt_err_t result;
- result = Work_Loop_Task();
- return result;
- }
- /**************************1.0定制2000W*************************************/
- /****************************5.0***********************************/
- void ctr_turnv5(void)
- {
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- // 转出
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //转入
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- }
- /*************************吸虫塔*********************************/
- //吸虫电机
- uint8_t ctr_Motor_xichong(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- uint8_t ctr_Camera_Power(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- //吸附电机
- uint8_t ctr_Motor_xifu(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR2_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR2_CTR_PIN, OFF);
- }
- return 0;
- }
- //清扫-卷带电机
- void ctr_JD_Clear(uint8_t shake_sw)
- {
- rt_uint16_t turn_timeout = 0;
- rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
- ctr_cangmen_up(OFF); //清扫传送带的电机
- if(shake_sw==ON)
- {
- ctr_Heat(ON); //清洁风机
- Motor_shake(ON, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- else {
- Motor_shake(OFF, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- while(rt_pin_read(MOTOR_C_LIMIT)==0)
- {
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- turn_timeout = 0;
- while(rt_pin_read(MOTOR_C_LIMIT)==1)
- {
- Motor_Juandai(ON);
- turn_timeout++;
- rt_thread_delay(50);
- if(turn_timeout==1000) break;
- }
- Motor_shake(OFF, 1);
- Motor_Juandai(OFF);
- ctr_cangmen_up(OFF); //关闭清扫
- ctr_Heat(OFF);
- }
- //工作任务流程
- void WorkTask_XCT(void)
- {
- while(device_status.work_status==1&&device_status.ds==1)
- {
- ctr_Motor_xifu(ON);
- //打开相机供电
- ctr_Camera_Power(ON);
- //ctr_Heat(ON); //清洁风机
- uint32_t photo_fre = 0;
- photo_fre = device_status.dattim * 60;
- while(photo_fre!=0)
- {
- photo_fre --;
- rt_thread_delay(1000);
- LOG_I("收集中,等待开始拍照:%d",photo_fre);
- }
- //卷带
- ctr_JD_Clear(OFF);
- //拍照
- HK_TakePhoto();
- //关闭吸附风机
- ctr_Motor_xifu(OFF);
- //关闭相机供电
- //ctr_Heat(OFF);
- ctr_Camera_Power(OFF);
- //卷带
- ctr_JD_Clear(ON);
- // rt_thread_delay(1000);
- // ctr_JD_Clear(ON);
- //震动
- Motor_shake(ON, device_status.shake_sec);
- }
- //关闭风机
- ctr_Motor_xichong(OFF);
- ctr_Motor_xifu(OFF);
- LOG_I("不在工作时间,未工作!!!");
- }
- /**********************************************************************/
- /*光控*/
- uint8_t In_Light_status(void)
- {
- rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
- return rt_pin_read(LIGHT_PIN);
- }
- /*雨控*/
- uint8_t In_Rain_status(void)
- {
- rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
- return rt_pin_read(RAIN_PIN);
- }
- /*加热*/
- uint8_t ctr_Heat(uint8_t status_heat)
- {
- rt_bool_t is_currently_on;
- rt_tick_t now;
- now = rt_tick_get();
- is_currently_on = (system_status.heat_start_time != 0);
- device_status.heat_status = status_heat;
- rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Heat_Pin,status_heat);
- if(status_heat==ON){
- // 已经开着 → 不重复计时
- if (is_currently_on) {
- return ON;
- }
- // 关 → 开,记录起始时间
- system_status.heat_start_time = now;
- }else{
- // 已经关着 → 不重复结算
- if (!is_currently_on) {
- return OFF;
- }
- // 开 → 关,结算运行时间
- system_status.heater_running_time +=
- (now - system_status.heat_start_time);
- system_status.heat_start_time = 0;
- }
- return status_heat;
- }
- /*制冷*/
- uint8_t ctr_Cold(uint8_t status_cold)
- {
- device_status.cold_status = status_cold;
- rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Cold_Pin,status_cold);
- return status_cold;
- }
- /*相机供电*/
- void ctr_photo(uint8_t status_photo)
- {
- rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
- rt_pin_write(PHOTO,status_photo);
- }
- /*************************************************
- Function: ctr_Cool_Fan
- Description: 散热风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- uint8_t ctr_Cool_Fan(uint8_t status_cool)
- {
- device_status.cool_fan_status = status_cool;
- rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
- rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
- if(status_cool==OFF)
- {
- rt_pin_write(Cool_Fan_L,OFF);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- if(status_cool==ON)
- {
- rt_pin_write(Cool_Fan_L,ON);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- return device_status.cool_fan_status;
- }
- /*************************************************
- Function: ctr_Collect_Fan
- Description: 采集风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- uint8_t ctr_Collect_Fan(uint8_t status_collect)
- {
- device_status.collect_fan_status = status_collect;
- rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
- if(status_collect==OFF)
- {
- rt_pin_write(Collect_Fan,OFF);
- }
- if(status_collect==ON)
- {
- rt_pin_write(Collect_Fan,ON);
- }
- return device_status.collect_fan_status;
- }
- /*************************************************
- Function: ctr_develop
- Description: 培养载玻片孢子
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: v=0(复位) v=90转90度
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- /*上仓门*/
- uint8_t ctr_cangmen_up(uint8_t status_up)
- {
- rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
- rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
- if(status_up==OFF)
- {
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,ON);
- device_status.ctr_up_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_up==OPEN)
- {
- device_status.up_status = 1;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- #if PCB_V1 == 1
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- #else
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- #endif
- }
- if(status_up==CLOSE)
- {
- device_status.up_status = 0;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- #if PCB_V1 == 1
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- #else
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- #endif
- }
- return device_status.up_status;
- }
- //下仓门
- uint8_t ctr_cangmen_down(uint8_t status_down)
- {
- rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
- rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
- if(status_down==OFF)
- {
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,ON);
- device_status.ctr_down_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_down==OPEN)
- {
- device_status.down_status = 1;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,OFF);
- }
- if(status_down==CLOSE)
- {
- device_status.down_status = 0;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,ON);
- }
- return device_status.down_status;
- }
- //镇流器
- //镇流器
- uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
- {
- rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
- rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
- rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
- rt_bool_t is_currently_on;
- rt_tick_t now;
- now = rt_tick_get();
- is_currently_on = (system_status.lamp_start_time != 0);
- if (status_lamp == ON)
- {
- // ====== 开灯 ======
- switch (lamp_num) {
- case 1:
- rt_pin_write(LAMP_SW, ON);
- device_status.lamp_status = ON;
- break;
- case 2:
- rt_pin_write(LAMP_1_SW, ON);
- device_status.lamp1_status = ON;
- break;
- case 3:
- rt_pin_write(LAMP_2_SW, ON);
- device_status.lamp2_status = ON;
- break;
- default:
- break;
- }
- // 已经开着 → 不重复计时
- if (is_currently_on) {
- return ON;
- }
- // 关 → 开,记录起始时间
- system_status.lamp_start_time = now;
- }
- else
- {
- // ====== 关灯 ======
- switch (lamp_num) {
- case 1:
- rt_pin_write(LAMP_SW, OFF);
- device_status.lamp_status = OFF;
- break;
- case 2:
- rt_pin_write(LAMP_1_SW, OFF);
- device_status.lamp1_status = OFF;
- break;
- case 3:
- rt_pin_write(LAMP_2_SW, OFF);
- device_status.lamp2_status = OFF;
- break;
- default:
- break;
- }
- // 已经关着 → 不重复结算
- if (!is_currently_on) {
- return OFF;
- }
- // 开 → 关,结算运行时间
- system_status.lamp_running_time +=
- (now - system_status.lamp_start_time);
- system_status.lamp_start_time = 0;
- }
- HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
- return device_status.lamp_status;
- }
- //震动电机
- uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
- {
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- #if 0
- rt_pin_write(SHAKE_SW,ON);
- return 0;
- #endif
- if(status_shake==ON&&device_status.shake_ws==1)
- {
- if(device_status.dtype==3)
- {
- for(uint8_t shake_i = 0;shake_i<shake_sec;shake_i++)
- {
- rt_pin_write(SHAKE_SW,ON);
- rt_thread_delay(100);
- rt_pin_write(SHAKE_SW,OFF);
- rt_thread_delay(1000);
- }
- }
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- system_status.vibration_count += 1;
- }
- else
- {
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- }
- if(device_status.dtype==7)
- HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
- return status_shake;
- }
- //补光
- uint8_t ctr_led(uint8_t status_led)
- {
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- if(status_led==ON)
- {
- rt_pin_write(LED_SW,ON);
- HMI_WRITE("sysTest.switchFill.val=%d",ON);
- }
- else if(status_led==OFF){
- rt_pin_write(LED_SW,OFF);
- HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
- HMI_WRITE("sysTest.switchFill.val=%d",OFF);
- }
- return status_led;
- }
- /*************************************************
- Function: ctr_turn
- Description: 转盘驱动
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: v=0(复位) v=90转90度
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- void ctr_turn(uint8_t v,uint8_t sta)
- {
- DTU_Write("turn.mp3");
- LOG_I("================v===============:%d",v);
- rt_uint16_t turn_cnt = 0;
- rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
- rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
- rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
- //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
- while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- turn_cnt ++;
- rt_thread_delay(100);
- if(turn_cnt==1000)
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
- DTU_Write("%s",turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- turn_cnt = 0;
- while(v==0&&rt_pin_read(LIMIT_0)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(ON,0);
- rt_thread_delay(200); //10 to 100
- turn_cnt ++;
- if(turn_cnt==300) //6000 to 600
- {
- turn_cnt = 0;
- // rt_pin_write(TURN_ZP_R, OFF);
- // rt_pin_write(TURN_ZP_L, OFF);
- // char *turn_warn =makeJson_Warn("超时", "转盘复位");
- // DTU_Write("%s",turn_warn);
- // rt_free(turn_warn);
- break;
- }
- }
- while(v==0&&rt_pin_read(LIMIT_0)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- rt_thread_delay(50); //10 to 50
- turn_cnt ++;
- if(turn_cnt==10) //6000 to 1200
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("超时", "转盘复位");
- DTU_Write("%s",turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- turn_cnt = 0;
- while(v==90&&rt_pin_read(LIMIT_90)==1)
- {
- rt_pin_write(TURN_ZP_R, ON);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(ON,0);
- rt_thread_delay(200);
- turn_cnt ++;
- if(turn_cnt==150)
- {
- turn_cnt = 0;
- // rt_pin_write(TURN_ZP_R, OFF);
- // rt_pin_write(TURN_ZP_L, OFF);
- // char *turn_warn =makeJson_Warn("超时", "转盘");
- // DTU_Write(turn_warn);
- // rt_free(turn_warn);
- break;
- }
- }
- while(v==90&&rt_pin_read(LIMIT_90)==1)
- {
- // rt_pin_write(TURN_ZP_R, ON);
- // rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(ON,0);
- rt_thread_delay(50);
- turn_cnt ++;
- if(turn_cnt==10)
- {
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- //Motor_shake(OFF,0);
- char *turn_warn =makeJson_Warn("超时", "转盘");
- DTU_Write(turn_warn);
- rt_free(turn_warn);
- break;
- }
- }
- if(sta!=0)
- {
- //保存位置
- device_status.run_sta = sta;
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
- }
- turn_cnt = 0;
- rt_pin_write(TURN_ZP_R, OFF);
- rt_pin_write(TURN_ZP_L, OFF);
- Motor_shake(OFF,0);
- HMI_WRITE("cangWei.rotateFlag.val=0");
- }
- /*
- *
- * */
- void ctr_Motor_Driver_Power(uint8_t status_ws)
- {
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- rt_pin_write(SHAKE_SW, status_ws);
- }
- /*************************************************
- Function: ctr_Motor_Driver
- Description: 步进电机驱动
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: 50000000
- Output: NULL
- Return: shake_sec
- Others: NULL
- *************************************************/
- void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
- {
- beep_init();
- rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
- rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
- rt_pin_write(Motor_EN, EN);
- rt_pin_write(Motor_DIR, DIR);
- beep_set(pwm, pwm/2); //200kzh - 50%
- while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- beep_set(0, 0); //200kzh - 50%
- rt_thread_delay(50);
- }
- /*************************************************
- Function: ctr_Petroleum_jelly
- Description: 涂凡士林
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: NULL
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- void ctr_Petroleum_jelly(rt_uint32_t delay)
- {
- beep_init();
- pwm_on(2,50000);
- rt_thread_delay(delay*1000);
- pwm_off(2,0);
- rt_thread_delay(1000);
- pwm_on(3, 50000);
- rt_thread_delay(delay*1000);
- pwm_off(3,0);
- device_status.fsl_sw = 0;
- }
- //1.0转仓
- void turnzp(void)
- {
- LOG_W("转仓执行!");
- static uint16_t turn_timeout = 0;
- // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- // rt_pin_write(CLEAR_R,OFF);
- // rt_pin_write(CLEAR_L,OFF);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- ctr_Collect_Fan(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到限位,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- break;
- }
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- // rt_pin_write(CLEAR_R,ON);
- // rt_pin_write(CLEAR_L,OFF);
- ctr_Collect_Fan(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- break;
- }
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_thread_delay(20);
- if(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- // rt_pin_write(CLEAR_R,OFF);
- // rt_pin_write(CLEAR_L,OFF);
- ctr_Collect_Fan(OFF);
- break;
- }
- else {
- break;
- }
- }
- ctr_Collect_Fan(OFF);
- turn_timeout = 0;
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- rt_thread_delay(1000);
- }
- //清扫-常闭型限位开关
- //新结构使用推杆电机做清扫
- uint8_t ctr_clear(uint16_t status_clear)
- {
- static uint16_t clear_num = 0;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
- /*************************************************************外接清扫
- rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- rt_pin_write(POWER_CLEAR,ON);
- rt_thread_delay(1000*100);
- rt_pin_write(POWER_CLEAR,OFF);
- ************************************************************/
- //停止
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
- rt_thread_delay(100);
- //如果清扫位置在中间
- while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(50);
- clear_num++;
- //已到达检测位置
- if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(2000);
- break;
- }
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- //如果清扫位置在左侧
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
- //DTU_Write("%s",Clear_ERR);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_free(Clear_ERR);
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(device_status.version_type==41)
- {
- rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
- //DTU_Write("%s",Clear_ERR);
- rt_free(Clear_ERR);
- }
- return 0;
- }
- clear_num = 0;
- rt_thread_delay(2000);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 1;
- }
- //路由器供电
- void ctr_router(uint8_t status_router)
- {
- rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
- //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- if(status_router==ON)
- {
- rt_pin_write(ROUTER,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- }
- }
- //清扫-推杆电机
- void ctr_TG_Clear(uint16_t clear_timeout)
- {
- #if 0
- int current_clear[200];
- int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
- LOG_I("清扫电机待机:%d",Electric_current);
- #endif
- static uint16_t clear_count = 0;
- clear_count = clear_timeout;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- while(clear_count!=0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- #if QINGSAO_IO == 1
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- #endif
- clear_count--;
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(3000);
- clear_count = 0;
- while(clear_timeout!=clear_count)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- #if QINGSAO_IO == 1
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- #endif
- // if(clear_count<200)
- // current_clear[clear_count] = atoi(device_status.power);
- clear_count++;
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- #if 0
- bubblesort(current_clear,200);
- if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
- {
- LOG_E("清扫电机电流不足:%d!",current_clear[199]);
- char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
- DTU_Write("%s",ctr_clear_warn);
- rt_free(ctr_clear_warn);
- }
- #endif
- rt_thread_delay(2000);
- device_status.clear_status = 0;
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- }
- // 全局静态变量,保存上次关闭的 tick
- static rt_uint32_t s_last_off_tick = 0;
- void ctr_RTU_DTU(uint8_t status_rtu)
- {
- // 1. 先设置引脚模式(只需要设置一次即可,放这里也兼容多次调用)
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- if (status_rtu == ON)
- {
- // 开机逻辑:必须满足冷却时间才能开机
- rt_uint32_t current_tick = rt_tick_get();
- rt_uint32_t cool_ticks = 30 * RT_TICK_PER_SECOND; // 30秒
- if (s_last_off_tick != 0)
- {
- rt_uint32_t diff_tick = current_tick - s_last_off_tick;
- if (diff_tick < cool_ticks)
- {
- // 未冷却完成,计算剩余时间
- rt_uint32_t wait_ticks = cool_ticks - diff_tick;
- rt_uint32_t wait_ms = wait_ticks * (1000 / RT_TICK_PER_SECOND);
- LOG_W("RTU电源关闭后需等待 %d ms 冷却, 禁止开机", wait_ms);
- // 阻塞等待冷却(如果不允许阻塞,就直接return,不要delay)
- rt_thread_delay(wait_ticks);
- }
- }
- // 冷却完成,执行开机
- rt_pin_write(RTU_DTU, ON);
- s_last_off_tick = 0; // 清除关机时间
- LOG_W("RTU电源启动!!!");
- }
- else
- {
- // 关机逻辑:记录关机时刻tick
- rt_pin_write(RTU_DTU, OFF);
- s_last_off_tick = rt_tick_get();
- LOG_W("RTU电源关闭:%d",s_last_off_tick);
- }
- }
- /**
- * @brief 安全关闭 RTU 12V 电源 (防假掉电流程)
- */
- void RTU_Power_Off_Safe(void)
- {
- /* 2. 【关键】关闭串口设备。
- * 这会触发底层的 HAL_UART_MspDeInit (如果BSP实现了的话),
- * 或者至少停止了UART外设的时钟和中断,防止它继续驱动引脚。
- */
- rt_device_close(dtuserial);
- /* 可选:如果 BSP 的 close 没有反初始化引脚,可以手动调用底层 HAL 函数
- * extern UART_HandleTypeDef huart2;
- * HAL_UART_DeInit(&huart2);
- */
- /* 3. 将 TX 和 RX 引脚配置为 高阻输入 (PIN_MODE_INPUT)
- * 此时内部上下拉电阻均被关闭,引脚呈现高阻抗,彻底切断寄生供电回路。
- */
- rt_pin_mode(RTU_TX_PIN, PIN_MODE_INPUT);
- rt_pin_mode(RTU_RX_PIN, PIN_MODE_INPUT);
- /* 4. 延时一小段时间,确保引脚状态稳定,且总线电容放电 */
- rt_thread_mdelay(50);
- /* 5. 拉低 MOS 管控制引脚,切断 12V 物理电源 */
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- rt_pin_write(RTU_DTU, OFF); // 假设低电平关断MOS
- LOG_I("RTU 12V Power OFF safely.\n");
- }
- /**
- * @brief 恢复 RTU 12V 电源并重新初始化串口
- */
- void RTU_Power_On_Restore(void)
- {
- rt_thread_mdelay(5000);
- /* 1. 拉高 MOS 管控制引脚,恢复 12V 物理电源 */
- rt_pin_write(RTU_DTU, ON); // 假设高电平打开MOS
- LOG_I("RTU 12V Power ON.\n");
- /* 2. 等待 RTU 核心板 PMIC 启动和 Linux/U-Boot 跑起来
- * i.MX6ULL 启动到串口有输出通常需要 3~10 秒,这里保守给 5 秒
- */
- rt_thread_mdelay(5000);
- /* 3. 【关键】重新打开串口设备。
- * 这会触发底层的 HAL_UART_MspInit,自动将 PA2/PA3 重新配置为
- * 复用推挽/开漏模式 (Alternate Function),恢复正常的 UART 通信能力。
- */
- GPIO_InitTypeDef gpioInitStruct;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- //PA2 TXD
- gpioInitStruct.Mode = GPIO_MODE_AF_PP; //设置为复用模式
- gpioInitStruct.Pin = GPIO_PIN_2; //初始化Pin9
- gpioInitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //承载的最大频率
- HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
- //PA3 RXD
- gpioInitStruct.Mode = GPIO_MODE_INPUT; //设置为浮空模式
- gpioInitStruct.Pin = GPIO_PIN_3; //初始化Pin10
- gpioInitStruct.Pull = GPIO_NOPULL; //承载的最大频率
- HAL_GPIO_Init(GPIOA, &gpioInitStruct); //初始化GPIOA
- #ifdef RT_USING_SERIAL_V2
- rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
- if(uart_res!=RT_EOK)
- {
- LOG_E("%s打开失败!",RTU_UART_NAME);
- }else{
- LOG_I("%s Open Success!",RTU_UART_NAME);
- }
- #endif
- #ifdef RT_USING_SERIAL_V1
- rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
- #endif
- /* 4. 【关键】重新注册接收回调
- * serial_v2 的 rt_serial_close() 会将 rx_indicate 和 tx_complete 都清为 RT_NULL。
- * 如果不重新注册,串口收到数据后驱动层调用 dev->rx_indicate(RT_NULL) 什么都不做,
- * 导致 dtu_thread_entry_recv 线程永远阻塞在信号量等待上,无法接收任何数据。
- */
- dtu_uart2_reinit_rx();
- }
- /* 测试命令:在 msh 终端中输入 rtu_restart 即可测试 */
- void cmd_rtu_restart(int argc, char** argv)
- {
- rt_kprintf("Starting RTU safe restart sequence...\n");
- // 1. 安全断电
- RTU_Power_Off_Safe();
- // 2. 保持断电 30 秒 (确保电容彻底放电)
- rt_kprintf("Waiting 30s for capacitor discharge...\n");
- rt_thread_mdelay(30000);
- // 3. 恢复供电并重建串口
- RTU_Power_On_Restore();
- rt_kprintf("RTU restart sequence completed.\n");
- }
- //海康拍照
- void Hk_Take_io(void)
- {
- rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
- rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
- rt_pin_write(HK_TAKE, 1);
- rt_pin_write(HK_IO, 0);
- if(device_status.dtype==3)
- Beep_Tip(1000);
- if(device_status.dtype==7)
- rt_thread_mdelay(1000);
- rt_pin_write(HK_TAKE, 0);
- rt_pin_write(HK_IO, 1);
- }
- void calorstat(uint16_t heat_time)
- {
- //uint8_t heat_up_cnt = 0;
- uint16_t n = 0;
- uint16_t delay_heat = 60;
- FlagStatus heat_flg = RESET;
- device_status.heat_time_min = 0;
- device_status.heat_tem_max = device_status.heattem; //初始化加热实时温度
- ctr_Heat(ON);
- // UpLoad_Data(); //开始加热时的第一条数据
- //恒温模式
- for(n=0;n < heat_time;n++)
- {
- if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
- {
- heat_flg = SET;
- ctr_Heat(OFF);
- while(delay_heat!=0)
- {
- device_status.C01 &= ~(1<<1); //加热 正常
- delay_heat --;
- rt_kprintf("正在恒温加热!!!:%d",delay_heat);
- HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
- rt_thread_delay(1000);
- if(device_status.heattem>device_status.heat_tem_max + 1){
- device_status.heat_tem_max = device_status.heattem;
- }
- }
- delay_heat = 60;
- //跳出FOR循环
- n = n + 60;
- }
- if(device_status.heattem>device_status.run_sta + 1){
- device_status.heat_tem_max = device_status.heattem;
- }
- if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
- {
- device_status.heat_tem_max = device_status.heattem;
- ctr_Heat(ON);
- }
- else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
- {
- // if(heat_up_cnt==0)
- // {
- // UpLoad_Data(); //加热到设定温度的数据
- // heat_up_cnt = 1;
- // }
- device_status.heat_tem_max = device_status.heattem;
- ctr_Heat(OFF);
- }
- if((n % 60)==0)
- {
- device_status.heat_time_min = device_status.heat_time_min + 1;
- if(device_status.heat_time_min % 5 == 0) //加热5分钟温度低于50,判定加热故障
- {
- if(device_status.heattem<50) {
- device_status.C01 |= (1 << 1); // 第1位变为1,加热故障
- }else {
- device_status.C01 &= ~(1<<1); //加热 正常
- }
- }
- }
- //如果触发异常退出加热
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- rt_thread_delay(1000);
- if(device_status.bat_status == 1)
- {
- rt_kprintf("============================触发异常退出加热==========================");
- ctr_lamp(OFF,1);
- ctr_Heat(OFF);
- break;
- }
- }
- if(device_status.work_mode==1){
- LOG_W("切换模式,退出加热!");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_Heat(OFF);
- rt_kprintf("异常退出!!!\n");
- break;
- }
- rt_thread_delay(1000);
- rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
- HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
- }
- ctr_Heat(OFF);
- //UpLoad_Data(); //加热结束的数据
- }
- //拍照
- void HK_TakePhoto(void)
- {
- //打开补光灯
- //DTU_Write("takephoto.mp3");
- rt_thread_delay(10000);
- rt_timer_start(timer_takephoto);
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- //吸虫塔
- if(device_status.version_type!=6)
- {
- ctr_led(ON);
- if(device_status.version_type==48)
- {
- rt_thread_delay(1000*60);
- }
- else {
- rt_thread_delay(1000*15);
- }
- }
- //触发海康相机拍照
- Hk_Take_io();
- if(device_status.version_type!=6)
- {
- rt_thread_delay(1000*45);
- }
- else {
- ctr_led(ON);
- rt_thread_delay(1000*19);
- }
- ctr_led(OFF);
- }
- //光控测试任务
- void SunTest(void)
- {
- rt_uint16_t ui_timeout_l = 0;
- //未检测到光照,一秒扫描等待,超时30S
- while(Read_Light_Voltage()<500)
- {
- ui_timeout_l ++;
- rt_thread_delay(1000);
- if(ui_timeout_l==30) break;
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()<500)
- {
- device_status.sun_status = 0;//光控状态
- }
- if(Read_Light_Voltage()>500)
- {
- rt_thread_delay(1000);
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()>500)
- {
- device_status.sun_status = 1;
- ctr_lamp(OFF,1);
- }
- HMI_WRITE("sysTest.switchSun.val=%d",0);
- device_status.sun_test = 0;
- device_status.work_status=0;
- }
- /*上电初始化*/
- void ResetInit(void)
- {
- if(device_status.version_type!=6&&device_status.version_type!=7)
- {
- device_status.work_done = 1;
- //1.打开路由器供电
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- ctr_RTU_DTU(ON);
- //3.关闭镇流器
- ctr_lamp(OFF,1);
- //4.关闭震动电机
- ctr_shake(OFF, 0);
- //5.关闭加热
- ctr_Heat(OFF);
- //6.关闭补光灯
- if(device_status.photo_ws==0)
- ctr_led(OFF);
- if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
- {
- LOG_W("欠压!");
- return;
- }
- //7.关闭上仓门
- device_status.ctr_up_motor = 2;
- // while(device_status.ctr_up_motor!=0)
- // {
- // LOG_I("关闭上仓门...");
- // rt_thread_delay(100);
- // }
- //8.关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- LOG_I("关闭下仓门....");
- rt_thread_delay(100);
- }
- //9.清扫
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- LOG_W("异常退出!!!\n");
- return;
- }
- if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
- {
- if(device_status.photo_ws==0) device_status.clear_status=1;
- while(device_status.clear_status==1){
- rt_thread_delay(1000);
- }
- }
- }
- else {
- if(device_status.power_status==1)
- {
- //1.打开路由器供电
- #if MATCH_DEVICE ==0
- ctr_router(ON);
- //2.打开相机供电
- ctr_photo(ON);
- #endif
- ctr_RTU_DTU(ON);
- }
- }
- }
- /*三色光源*/
- void Led_Check(uint8_t tp)
- {
- if(tp==3)
- {
- if(device_status.light_over_tim>2*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<1*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- else if (tp==6) {
- if(device_status.light_over_tim>4*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<7200) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- else if (tp==9) {
- if(device_status.light_over_tim>6*3600)
- {
- device_status.work_status = 1;
- device_status.lamp_status = 1;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 1;
- device_status.lamp2_status = 0;
- LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
- }
- else if (device_status.light_over_tim<3*3600) {
- device_status.work_status = 1;
- device_status.lamp_status = 0;
- device_status.lamp1_status = 0;
- device_status.lamp2_status = 1;
- LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
- }
- }
- }
- void WorkCheck(void)
- {
- static rt_uint32_t light_timeout = 3600;
- while(device_status.ds == 1 &&
- device_status.rain_status == 0 &&
- device_status.temp_status == 0 &&
- device_status.bat_status == 0)
- {
- if(device_status.timeSwitch == 1) // 时控模式
- {
- LOG_I("***********时控*****************");
- time_t now;
- time(&now);
- struct tm now_tm = *localtime(&now);
- if(device_status.startBar == device_status.stopBar)
- {
- // 开始结束时间相同则24小时模式
- device_status.work_status = 1;
- rt_thread_delay(10000);
- LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
- device_status.work_status ? "工作" : "待机",
- now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
- device_status.startBar, device_status.stopBar);
- continue;
- }
- rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
- rt_uint32_t start_sec = device_status.startBar * 3600;
- rt_uint32_t end_sec = device_status.stopBar * 3600;
- // 修复跨天判断逻辑
- if (start_sec < end_sec)
- {
- // 不跨天模式
- device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
- }
- else
- {
- // 跨天模式
- device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
- }
- LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
- device_status.work_status ? "工作" : "待机",
- now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
- device_status.startBar, device_status.stopBar);
- rt_thread_delay(10000);
- }
- else // 光控模式
- {
- LOG_I("光控定时模式!");
- if(device_status.sun_status == 0 && device_status.light_over_tim > 0)
- {
- device_status.light_over_tim--;
- light_timeout = 3600; // 重置超时计数器
- device_status.work_status = 1;
- LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
- }
- else
- {
- // 仅在白天递减超时计数器
- if(device_status.sun_status == 1 && light_timeout > 0) {
- light_timeout--;
- }
- device_status.work_status = 0;
- //没有在测试并且没有在自检
- if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
- {
- ctr_lamp(OFF,1);
- }
- // 连续1小时白天后重置
- if(light_timeout == 0) {
- device_status.light_over_tim = 0;
- light_timeout = 3600; // 重置防止负数
- device_status.light_over_tim = device_status.sunBar * 3600;
- }
- }
- }
- rt_thread_delay(1000);
- }
- // 异常状态处理
- device_status.work_status = 0;
- rt_thread_delay(1000);
- }
- //工作任务流程s
- void WorkTask(void)
- {
- static int clt_time = 0; //收集时间差秒数
- static int heat_time = 0;
- LOG_I("工作状态:%d",device_status.work_status);
- LOG_I("开关机状态:%d",device_status.ds);
- while(device_status.clear_status==1){
- LOG_I("等待清扫执行完成....");
- rt_thread_delay(1000);
- }
- 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)
- {
- if(device_status.work_mode==1){
- break;
- }
- device_status.work_done = 0;
- if(clt_time==0)
- {
- DTU_Write("workstart.mp3");
- if(device_status.debug_ws==1)
- {
- time_t stamp=time(RT_NULL);
- static struct tm tm;
- gmtime_r(&stamp, &tm);
- 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);
- char* debug_str = makeJson_Warn("Work start!", device_status.stamp);
- DTU_Write(debug_str);
- rt_free(debug_str);
- }
- }
- //1.打开灯管
- #if LAMP_EN == 1
- if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
- {
- ctr_lamp(ON,1);
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- }
- else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
- ctr_lamp(OFF,1);
- ctr_lamp(ON,2);
- ctr_lamp(OFF,3);
- }
- else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
- ctr_lamp(OFF,1);
- ctr_lamp(OFF,2);
- ctr_lamp(ON,3);
- }
- #else
- ctr_lamp(ON,1);
- #endif
- LOG_I("打开灯管!!!");
- if(device_status.version_type!=5)
- {
- rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
- //2.打开上仓门
- LOG_I("打开上仓门!!!");
- device_status.ctr_up_motor = 1;
- //3.收集
- clt_time = device_status.clt_time * 60;
- while(clt_time!=1)
- {
- clt_time --;
- rt_thread_delay(1000);
- rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
- if(device_status.work_mode==1){
- break;
- }
- //如果触发异常
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- }
- clt_time = 1;
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- if(device_status.timeSwitch==0&&device_status.sun_status==1)
- {
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- rt_kprintf("异常退出!!!\n");
- break;
- }
- #if 0//LAMP_EN == 0
- ctr_Fan_IN(OFF);
- #endif
- //4.关闭上仓门
- device_status.ctr_up_motor = 2;
- rt_kprintf("关闭上仓门!!!\n");
- while(device_status.ctr_up_motor!=0)
- {
- rt_thread_delay(100);
- LOG_I("等待上仓门关闭!!!");
- }
- //5.加热
- if(device_status.heat_sw==0)
- {
- calorstat(device_status.heattime*60);
- }
- else {
- heat_time = device_status.heattime * 60;
- while(heat_time!=0)
- {
- if(device_status.work_mode==1)
- {
- break;
- }
- heat_time--;
- LOG_I("正在加热状态!!!");
- rt_thread_delay(1000);
- }
- }
- #if TG_MODULE == 0
- //6.打开下仓门
- if(device_status.rain_status==0)
- {
- device_status.ctr_down_motor =1;
- }
- else {
- device_status.ctr_down_motor = 2;
- device_status.ctr_up_motor = 2;
- }
- //待下仓门完全打开
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门打开!!!\n");
- }
- rt_thread_delay(3000);
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- }
- //2022-04-12add:
- LOG_I("打开上仓门!!!");
- device_status.ctr_up_motor = 1;
- #else
- //打开下仓门
- ctr_cangmen_down(OPEN);
- uint16_t timeout_down_open = 0;
- while(1)
- {
- timeout_down_open++;
- if(rt_pin_read(CTR_UPC_LIMIT)==0||timeout_down_open>20*20) break;
- rt_thread_delay(50);
- }
- ctr_cangmen_down(OFF);
- rt_thread_delay(1000);
- #endif
- if(device_status.work_mode==1){
- break;
- }
- if(device_status.version_type==4||device_status.version_type==41||device_status.version_type==42||device_status.version_type==43)
- {
- //震动
- ctr_shake(ON, device_status.shake_sec);
- //补光&&拍照
- LOG_I("拍照!!!");
- device_status.photo_ws = 1;
- }
- #if TG_MODULE == 1
- while(device_status.photo_ws == 1)
- {
- rt_thread_delay(1000);
- LOG_I("等待拍照完成...");
- }
- rt_thread_delay(1000);
- while(device_status.clear_status==1)
- {
- rt_thread_delay(1000);
- LOG_I("等待清扫完成...");
- }
- ctr_cangmen_down(OPEN);
- //待下仓门完全打开
- uint8_t i = 0;
- while(1)
- {
- i++;
- rt_thread_delay(100);
- rt_kprintf("等待下仓门打开!!!\n");
- if(i>100) {
- ctr_cangmen_down(OFF);
- break;
- }
- }
- rt_thread_delay(2000);
- ctr_shake(ON, device_status.shake_sec);
- device_status.photo_ws = 1;
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- //待下仓门完全关闭
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- }
- device_status.ctr_up_motor = 1; //打开上仓门
- #endif
- }
- }
- if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
- {
- LOG_I("-------------------not work--------------------");
- device_status.work_status = 0;
- }
- //工作结束
- if(device_status.work_done==0&&device_status.work_status==0)
- {
- DTU_Write("workdone");
- ResetInit();
- clt_time = 0;
- }
- rt_kprintf("***not work-device status:%d\n",device_status.ds);
- }
- #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
- void slidelock()
- {
- ctr_Motor_Driver_Power(ON); //电机驱动供电
- rt_thread_delay(10);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
- rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
- }
- /* 入口函数-工作状态检测 */
- static void Work_status_thread_entry(void *parameter)
- {
- static uint16_t sun_status_num = 120;
- //如果是光控定时,进行光控监测的消抖
- if(device_status.timeSwitch==0&&device_status.sun_status==0)
- {
- rt_kprintf("光控定时!!!\n");
- while(sun_status_num!=0)
- {
- sun_status_num--;
- rt_thread_delay(1000);
- rt_kprintf("开机光控检测:%d\n",sun_status_num);
- }
- device_status.light_over_tim = device_status.sunBar * 3600;
- device_status.test_flag = 0;
- }
- while(1)
- {
- WorkCheck();
- }
- }
- //指令
- //
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- const uint8_t open_Led[] = {0x02,0x06,0x00,0x32,0x00,0x01,0xE9,0xF6}; //02 06 00 32 00 01 E9 F6
- const uint8_t close_Led[] = {0x02,0x06,0x00,0x32,0x00,0x00,0x28,0x36}; //02 06 00 32 00 01 E9 F6
- const uint8_t motor1_Forword[] = {0x02,0x06,0x00,0x00,0x00,0x01,0x48,0x39}; //02 06 00 00 00 01 48 39
- const uint8_t motor1_Backward[] = {0x02,0x06,0x00,0x00,0x00,0x02,0x08,0x38}; //02 06 00 00 00 01 48 39
- const uint8_t motor1_Stop[] = {0x02,0x06,0x00,0x00,0x00,0x00,0x89,0xF9}; //02 06 00 00 00 01 48 39
- const uint8_t motor2_Forword[] = {0x02,0x06,0x00,0x00,0x00,0x10,0x88,0x35}; //02 06 00 00 00 01 48 39
- const uint8_t motor2_Backward[] = {0x02,0x06,0x00,0x00,0x00,0x20,0x88,0x31}; //02 06 00 00 00 01 48 39
- const uint8_t motor2_Stop[] = {0x02,0x06,0x00,0x00,0x00,0x00,0x89,0xF9}; //02 06 00 00 00 01 48 39
- const uint8_t get_status[] = {0x02,0x03,0x00,0x00,0x00,0x01,0x84,0x39}; //02 03 02 00 00 FC 44
- rt_thread_delay(1000);
- ResetInit();
- rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
- static struct tm tm_tg_run;
- static uint8_t alarm_h,alarm_m,alarm_s;
- while(1)
- {
- if(device_status.work_mode==0)
- {
- WorkTask();
- }else{
- if(device_status.work_status==1){
- ctr_lamp(ON, 1);
- }else{
- ctr_lamp(OFF, 1);
- }
- char buf[7] = {0};
- // 拷贝闹钟字符串(保证安全截断)
- rt_sprintf(buf, "%-6s", device_status.alarm_tg);
- // 截取 时: 前2位
- alarm_h = (buf[0] - '0') * 10 + (buf[1] - '0');
- // 截取 分: 中间2位
- alarm_m = (buf[2] - '0') * 10 + (buf[3] - '0');
- // 截取 秒: 后2位
- alarm_s = (buf[4] - '0') * 10 + (buf[5] - '0');
- device_status.ctr_up_motor = 2; //关闭上仓门
- device_status.ctr_down_motor = 2; //关闭下仓门
- LOG_I("当前B模式,触发时间:%d:%d:%d",alarm_h,alarm_m,alarm_s);
- //判断时间
- time_t time_run_tg;
- time(&time_run_tg);
- tm_tg_run =*localtime(&time_run_tg);
- LOG_I("当前时间:%d:%d:%d",tm_tg_run.tm_hour,tm_tg_run.tm_min,tm_tg_run.tm_sec);
- if((tm_tg_run.tm_hour)==alarm_h&&(tm_tg_run.tm_min==alarm_m)&&(tm_tg_run.tm_sec>=alarm_s))
- {
- LOG_W("预制执行 开始!");
- if(device_status.motor_active==0){
- ctr_Cold(ON); //推杆打开
- rt_thread_delay(20000);
- //震动
- ctr_Collect_Fan(ON);
- for(uint8_t i = 0; i < 30; i++){
- rt_thread_delay(1000);
- LOG_I("等待震动完成:%d",i);
- }
- ctr_Collect_Fan(OFF);
- rt_thread_delay(2000);
- ctr_Cold(OFF); //推杆关闭
- rt_thread_delay(1000);
- device_status.motor_active=1;
- ef_set_env_blob("motor_active", &device_status.motor_active, sizeof(device_status.motor_active));
- }else{
- /* 发送 Modbus RTU 获取电机状态 */
- uart5_rx_flag = 0; /* 先清除上次的接收标志 */
- Uart5_Write_Bin(get_status, sizeof(get_status)); /* 必须用_Bin,%s会截断0x00字节 */
- /* 等待回复,超时2秒上报预警 */
- rt_tick_t status_start = rt_tick_get();
- uint8_t status_timeout = 1; /* 默认超时 */
- while(rt_tick_get() - status_start < 2 * RT_TICK_PER_SECOND) {
- if(uart5_rx_flag) {
- status_timeout = 0; /* 收到回复 */
- LOG_I("电机状态查询有回复");
- break;
- }
- rt_thread_delay(50); /* 每50ms轮询一次 */
- }
- if(status_timeout) {
- LOG_W("电机状态查询超时,发送预警!");
- char *warn = makeJson_Warn("485_motor_timeout", "get_status");
- if(warn) {
- DTU_Write("%s", warn);
- rt_free(warn);
- }
- }
- uart5_rx_flag = 0; /* 重置标志,供下次使用 */
- Uart5_Write_Bin(motor1_Forword, sizeof(motor1_Forword)); //推杆打开
- rt_thread_delay(20000);
- Uart5_Write_Bin(motor2_Forword, sizeof(motor2_Forword)); //震动
- for(uint8_t i = 0; i < 30; i++){
- rt_thread_delay(1000);
- LOG_I("等待震动完成:%d",i);
- }
- Uart5_Write_Bin(motor2_Stop, sizeof(motor2_Stop)); //关闭震动
- rt_thread_delay(2000);
- Uart5_Write_Bin(motor1_Backward, sizeof(motor1_Backward)); //推杆关闭
- rt_thread_delay(1000);
- device_status.motor_active=0;
- ef_set_env_blob("motor_active", &device_status.motor_active, sizeof(device_status.motor_active));
- }
- char *temp_s =makeJson_Warn("执行完成", "预制模式");
- DTU_Write("%s",temp_s);
- rt_free(temp_s);
- rt_thread_delay(2000);
- //{"jsonrpc":"2.0","method":"COPY_IMAGE","params":{"st":20,"et":7},"id":1}
- cJSON *params = cJSON_CreateObject(); // params 子对象
- cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
- cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
- char* temp = makeJsonRpc2_0("COPY_IMAGE", params, 1);
- if(temp){
- DTU_Write(temp);
- free(temp);
- }
- rt_thread_delay(20000);
- }
- }
- rt_thread_delay(1000);
- }
- }
- /*入口函数清扫*/
- static void Control_Clear_thread_entry(void *parameter)
- {
- while(1)
- {
- if(device_status.update_flag==1) break;
- if(device_status.clear_status==1)
- {
- if(device_status.version_type==41){
- ctr_TG_Clear(300);
- }else {
- ctr_TG_Clear(350);
- }
- system_status.cleaning_count +=1; //清扫次数
- }
- if(device_status.clear_status==2)
- {
- turnzp();
- device_status.clear_status = 0;
- }
- rt_thread_delay(100);
- }
- }
- /*入口函数,自检*/
- static void Control_Check_thread_entry(void *parameter)
- {
- static int total_seconds = 200;
- static int current_second = 0;
- static int process = 0;
- device_status.C01 = 0;
- rt_thread_delay(10000);
- while(1)
- {
- if(device_status.update_flag==1) break;
- if(device_status.work_status==1&&device_status.heat_sw==1)
- {
- calorstat(device_status.heattime*60);
- rt_thread_delay(10000);
- }
- else
- {
- if(device_status.selfcheck_ws==2)
- {
- while(current_second <= total_seconds)
- {
- process = (current_second * 100) / total_seconds;
- device_status.process = process / 100.0;
- LOG_I("process:%d",process);
- HMI_WRITE("selfcheck.s106.val=%d",process);
- rt_thread_delay(1000);
- current_second++;
- if(device_status.selfcheck_ws==0)
- {
- HMI_WRITE("selfcheck.s106.val=100");
- break;
- }
- }
- HMI_WRITE("selfcheck.s106.val=100");
- }else {
- process = 0;
- current_second = 0;
- #if 0
- if(device_status.rain_status==1)
- {
- LOG_E("雨控!");
- rt_thread_delay(10000);
- }
- //根据故障码 亮指示灯
- //灯管故障
- if(device_status.C01 & (1 << 0))
- {
- LOG_E("灯管故障!");
- //发送红灯常亮信号
- write_single_holding_register(0x01,0x00C2,0x0011,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //加热故障
- if(device_status.C01 & (1 << 1))
- {
- LOG_E("加热故障!");
- //发送红灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0021,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //相机拍照故障
- if(device_status.C01 & (1 << 2))
- {
- LOG_E("拍照故障!");
- //发送红灯快闪信号
- write_single_holding_register(0x01,0x00C2,0x0031,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //补光灯故障
- if(device_status.C01 & (1 << 3))
- {
- LOG_E("补光灯故障!");
- //发送绿灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0023,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //上仓门故障
- if(device_status.C01 & (1 << 8))
- {
- LOG_E("上仓门故障!");
- //发送黄灯常亮信号
- write_single_holding_register(0x01,0x00C2,0x0012,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //下仓门故障
- if(device_status.C01 & (1 << 9))
- {
- LOG_E("下仓门故障!");
- //发送黄灯慢闪信号
- write_single_holding_register(0x01,0x00C2,0x0022,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //清扫故障
- if(device_status.C01 & (1 << 10))
- {
- LOG_E("清扫故障!");
- //发送黄灯爆闪信号
- write_single_holding_register(0x01,0x00C2,0x0032,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- //通信故障
- if(device_status.C01 & (1 << 13))
- {
- LOG_E("通信故障!");
- //发送绿灯爆闪信号
- write_single_holding_register(0x01,0x00C2,0x0033,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- if(device_status.C01==0)
- {
- LOG_W("无故障!");
- //发送绿灯常亮信号
- //write_single_holding_register(0x01,0x00C2,0x0013,1000);
- //延时10S
- rt_thread_delay(10000);
- }
- #endif
- }
- }
- rt_thread_delay(1000);
- }
- }
- static void thread_485_play_entry(void *param)
- {
- static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
- rt_uint32_t e;
- // 创建事件控制块
- event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
- if (event_485_play == RT_NULL)
- {
- LOG_E("Event create failed!");
- return;
- }
- while(1)
- {
- if(device_status.update_flag==1) break;
- //等待事件
- if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
- {
- LOG_I("Interrupt occurred and processed.");
- _485_play(sound105, 4);
- //发送串口消息,通知JETSON计算卡进行拍照
- DTU_Write("{\"guangshan\":\"on\"}");
- }
- }
- }
- /*清扫线程*/
- int Control_clear(void)
- {
- /* 创建线程*/
- tid_ctr = rt_thread_create("Control_Clear_thread_entry",
- Control_Clear_thread_entry, RT_NULL,
- 2048, //2048
- 24, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_ctr!=RT_NULL)
- {
- rt_thread_startup(tid_ctr);
- }
- return 0;
- }
- /*恒温加热线程*/
- int Control_Check(void)
- {
- /* 创建线程*/
- tid_heat = rt_thread_create("Control_Check_thread_entry",
- Control_Check_thread_entry, RT_NULL,
- 1024, //2048
- 25, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_heat!=RT_NULL)
- {
- rt_thread_startup(tid_heat);
- }
- return 0;
- }
- int thread_485_play(void)
- {
- tid_485_play = rt_thread_create("thread_485play",
- thread_485_play_entry, RT_NULL,
- 1024, //1024
- 26, //23
- 5); //5
- if(tid_485_play!=RT_NULL)
- {
- rt_thread_startup(tid_485_play);
- }
- else {
- LOG_E("thread_485_play start failed!");
- }
- return 0;
- }
- /* 创建线程 -工作状态检测*/
- int Workstatus_check(void)
- {
- /* 创建线程*/
- tid_wkstc = rt_thread_create("work_status_check_thread",
- Work_status_thread_entry, RT_NULL,
- WORK_CHECK_THREAD_STACK_SIZE, //1024
- WORK_CHECK_THREAD_PRIORITY, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_wkstc!=RT_NULL)
- {
- rt_thread_startup(tid_wkstc);
- }
- return 0;
- }
- /*工作流程线程*/
- int Work_Loop_Task(void)
- {
- rt_err_t result_loop = 0;
- /* 创建线程*/
- tid_workloop = rt_thread_create("work_loop_thread",
- Work_Loop_thread_entry, RT_NULL,
- WORK_LOOP_THREAD_STACK_SIZE, //2048
- WORK_LOOP_THREAD_PRIORITY, //12
- WORK_LOOP_THREAD_TIMESLICE); //5
- if(tid_workloop!=RT_NULL)
- {
- result_loop = rt_thread_startup(tid_workloop);
- }
- return result_loop;
- }
- #ifdef RT_USING_ALARM
- void alarm_callback_router_on(rt_alarm_t alarm, time_t timestamp)
- {
- LOG_W("alarm_callback_router_on.\n");
- ctr_router(ON);
- }
- void alarm_callback_router_off(rt_alarm_t alarm, time_t timestamp)
- {
- LOG_W("alarm_callback_router_off.\n");
- ctr_router(OFF);
- }
- void alarm_app_router(void)
- {
- struct rt_alarm_setup setup;
- struct rt_alarm * alarm_router_on = RT_NULL;
- if (alarm_router_on != RT_NULL)
- return;
- setup.flag = RT_ALARM_DAILY;
- setup.wktime.tm_hour = 23;
- setup.wktime.tm_min = 0;
- setup.wktime.tm_sec = 0;
- alarm_router_on = rt_alarm_create(alarm_callback_router_on, &setup);
- if(RT_NULL != alarm_router_on)
- {
- LOG_I("设置闹钟: %d:%d:%d",setup.wktime.tm_hour,setup.wktime.tm_min,setup.wktime.tm_sec);
- rt_alarm_start(alarm_router_on);
- }
- }
- #endif
- MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
- MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
- MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
- MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
- MSH_CMD_EXPORT(cmd_rtu_restart, Safe restart RTU power);
- //INIT_APP_EXPORT(Control_heat);
|