/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-09-29 Administrator the first version */ #include #include #include #include "selfcheck.h" #include #include #include #define DBG_ENABLE #define DBG_COLOR #define DBG_TAG "selfcheck" #define DBG_LEVEL DBG_LOG #include //must after of DBG_LVL, DBG_TAG or other options rt_timer_t timer_takephoto = RT_NULL; Device_Status_typedef device_status; static int calculate_average(int arr[], int n) { int sum = 0; // 用于存储数组元素的总和 for (int i = 0; i < n; i++) { sum += arr[i]; // 累加每个元素 } // 计算平均值 int average = sum / n; // 强制类型转换确保得到浮点数结果 return average; } static void t_takephoto_callback(void *parameter) { // rt_timer_delete(timer_takephoto); LOG_E("拍照超时!"); device_status.C01 |= (1<<2); //拍照故障 device_status.C01 |= (1<<3); //补光灯故障 HMI_WRITE("selfcheck.t4.bco=63488"); HMI_WRITE("selfcheck.t3.bco=63488"); HMI_WRITE("sysSet.va0.txt=\"拍照超时\""); } int selfcheck(uint8_t type) { int current_i[6]; int current_,current_open = 0; //删除workloop线程 防止自动运行干扰 LOG_I("删除线程workloop:%d",thread_delete_workloop()); //初始化硬件 HardWare_Init(0); //关闭上仓门 ctr_cangmen_down(CLOSE); //关闭下仓门 ctr_cangmen_up(CLOSE); for(int c=0;c<8;c++) { rt_thread_delay(2000); HMI_WRITE("sysSet.va0.txt=\"等待上下仓门复位完成......\""); LOG_W("等待上下仓门复位完成.....%d",c); } ctr_cangmen_up(OFF); ctr_cangmen_down(OFF); /********************************************************/ //读取5次电流值,取平均值 for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } current_ = calculate_average(current_i,5); if(current_<50||current_>3000) { device_status.C01 |= (1<<12); HMI_WRITE("selfcheck.t13.bco=63488"); } else { HMI_WRITE("selfcheck.t13.bco=2024"); device_status.C01 &= ~(1<<12); } HMI_WRITE("sysSet.va0.txt=\"待机电流:%d\"",current_); LOG_W("待机电流.....%d",current_); if(type==1) { char *readinfo = makeJson_ReadInfo(); DTU_Write("%s",readinfo); rt_free(readinfo); rt_thread_delay(1000); if(atoi(device_status.gps)==0) { HMI_WRITE("selfcheck.t12.bco=63488"); device_status.C01 |= (1<<11); HMI_WRITE("sysSet.va0.txt=\"GPS无定位\""); } else { HMI_WRITE("selfcheck.t12.bco=2024"); device_status.C01 &= ~(1<<11); } rt_thread_delay(1000); if(Read_Ntc_Voltage()==0) { HMI_WRITE("selfcheck.t15.bco=63488"); } else{ HMI_WRITE("selfcheck.t15.bco=2024"); } rt_thread_delay(1000); if(device_status.bat_status==0) { HMI_WRITE("selfcheck.t5.bco=2024"); device_status.C01 &= ~(1<<4); } else { HMI_WRITE("selfcheck.t5.bco=63488"); device_status.C01 |= (1<<4); } rt_thread_delay(1000); if(Read_Rain_Voltage()==0) { HMI_WRITE("selfcheck.t6.bco=63488"); device_status.C01 |= (1<<5); } else { device_status.C01 &= ~(1<<5); HMI_WRITE("selfcheck.t6.bco=2024"); } rt_thread_delay(1000); if(device_status.temp_status) { HMI_WRITE("selfcheck.t8.bco=63488"); device_status.C01 |= (1<<7); } else { device_status.C01 &= ~(1<<7); HMI_WRITE("selfcheck.t8.bco=2024"); } int sensor_light_value = 0; for(int i = 0;i<3;i++) { sensor_light_value += Read_Light_Voltage(); rt_thread_delay(500); } if(sensor_light_value<50) { HMI_WRITE("selfcheck.t7.bco=63488"); device_status.C01 |= (1<<6); } else { device_status.C01 &= ~(1<<6); HMI_WRITE("selfcheck.t7.bco=2024"); } HMI_WRITE("sysSet.va0.txt=\"光控传感器采样数值:%d\"",Read_Light_Voltage()); rt_thread_delay(1000); } /********************************************************/ ctr_lamp(ON, 1); //打开镇流器 rt_thread_delay(1000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_lamp(OFF, 1); //关闭镇流器 current_open = calculate_average(current_i,5); LOG_W("待机电流:%d",current_); LOG_W("镇流器开启电流:%d",current_open); if(current_open-current_>100) { HMI_WRITE("selfcheck.t1.bco=2024"); device_status.C01 &= ~(1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管正常:%d\"",current_open); } else { HMI_WRITE("selfcheck.t1.bco=63488"); device_status.C01 |= (1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管异常:%d\"",current_open); } rt_thread_delay(1000); /********************************************************/ //打开上仓门 ctr_cangmen_up(OPEN); rt_thread_delay(1000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_up(OFF); rt_thread_delay(1000); current_open = calculate_average(current_i,5); LOG_W("上仓门action电流:%d",current_open); if(current_open-current_>100) { HMI_WRITE("selfcheck.t9.bco=2024"); device_status.C01 &= ~(1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门正常:%d\"",current_open); } else{ HMI_WRITE("selfcheck.t9.bco=63488"); device_status.C01 |= (1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门故障:%d\"",current_open); } rt_thread_delay(1000); /********************************************************/ //打开下仓门 ctr_cangmen_down(OPEN); rt_thread_delay(1000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_down(OFF); rt_thread_delay(1000); current_open = calculate_average(current_i,5); LOG_W("下仓门action电流:%d",current_open); if(current_open-current_>100) { HMI_WRITE("selfcheck.t10.bco=2024"); device_status.C01 &= ~(1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门正常:%d\"",current_open); } else{ HMI_WRITE("selfcheck.t10.bco=63488"); device_status.C01 |= (1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门故障:%d\"",current_open); } //开启加热 rt_thread_delay(1000); if(type==1) { // if(atoi(device_status.heat_power)>5000) { HMI_WRITE("selfcheck.t2.bco=2024"); device_status.C01 &= ~(1<<1); HMI_WRITE("sysSet.va0.txt=\"加热正常:%d\"",atoi(device_status.heat_power)); } else { ctr_Heat(ON); //持续5S钟 for(int i = 5;i>0;i--) { HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i); rt_thread_delay(1000); } LOG_I("当前温度:%d电流:%s",device_status.heatvalue,device_status.heat_power); //比较当前电压、温度探头温度和开启前 if(atoi(device_status.heat_power)>5000) { HMI_WRITE("selfcheck.t2.bco=2024"); device_status.C01 &= ~(1<<1); HMI_WRITE("sysSet.va0.txt=\"加热正常:%d\"",atoi(device_status.heat_power)); } else { HMI_WRITE("selfcheck.t2.bco=63488"); device_status.C01 |= (1<<1); HMI_WRITE("sysSet.va0.txt=\"加热故障:%d\"",atoi(device_status.heat_power)); } } ctr_Heat(OFF); rt_thread_delay(1000); /*********************************************************/ HMI_WRITE("sysSet.va0.txt=\"开始拍照\""); if(device_status.camsta==0) { rt_timer_start(timer_takephoto); HK_TakePhoto(); for(int i = 15;i>0;i--) { HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i); rt_thread_delay(1000); } }else { HMI_WRITE("sysSet.va0.txt=\"相机未连接\""); HMI_WRITE("selfcheck.t4.bco=63488"); HMI_WRITE("selfcheck.t3.bco=63488"); } rt_thread_delay(1000); } /*******************************************************/ //清扫 device_status.clear_status = 1; rt_thread_delay(2000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } current_open = calculate_average(current_i,5); LOG_W("清扫action电流:%d@%d",current_open,current_); rt_thread_delay(1000); if(current_open-current_>=100) { HMI_WRITE("selfcheck.t11.bco=2024"); device_status.C01 &= ~(1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫正常:%d\"",atoi(device_status.power)); } else { HMI_WRITE("selfcheck.t11.bco=63488"); device_status.C01 |= (1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫故障:%d\"",atoi(device_status.power)); } rt_thread_delay(1000); /*******************************************************/ HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01); char *js = makeJson_C01_Warn(device_status.C01,1.0); DTU_Write("%s",js); free(js); LOG_I("重启线程workloop:%d",thread_restart_workloop()); device_status.selfcheck_ws = 0; device_status.process = 0.0; } int rt_software_timer_init(void) { //启动拍照超时定时器 timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 100000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER); if(timer_takephoto==RT_NULL) { LOG_E("rt_timer_create t_takephoto failed!"); return RT_ERROR; } else { return RT_EOK; } } INIT_APP_EXPORT(rt_software_timer_init);