/* * 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 #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) { device_status.autotakepic = 0; LOG_E("t_takephoto_callback"); //关闭补光灯 ctr_led(OFF); //关闭滑台驱动 slidelock(); Beep_Tip(5000); } 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); /********************************************************/ //读取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); } if(atoi(device_status.gps_state)==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); } if(Read_Ntc_Voltage()==0) { HMI_WRITE("selfcheck.t15.bco=63488"); } else{ HMI_WRITE("selfcheck.t15.bco=2024"); } 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); } if(device_status.rain_status) { HMI_WRITE("selfcheck.t6.bco=63488"); device_status.C01 |= (1<<5); } else { device_status.C01 &= ~(1<<5); HMI_WRITE("selfcheck.t6.bco=2024"); } 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"); } if(device_status.sun_status) { HMI_WRITE("selfcheck.t7.bco=63488"); device_status.C01 |= (1<<6); } else { device_status.C01 &= ~(1<<6); HMI_WRITE("selfcheck.t7.bco=2024"); } 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_I("待机电流:%d",current_); LOG_I("镇流器开启电流:%d",current_open); if(current_open-current_>100) { HMI_WRITE("selfcheck.t1.bco=2024"); device_status.C01 &= ~(1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管正常\""); } else { HMI_WRITE("selfcheck.t1.bco=63488"); device_status.C01 |= (1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管异常\""); } rt_thread_delay(1000); /********************************************************/ //打开上仓门 ctr_cangmen_up(OPEN); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_up(OFF); current_open = calculate_average(current_i,5); if(current_open-current_>100) { HMI_WRITE("selfcheck.t9.bco=2024"); device_status.C01 &= ~(1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门正常\""); } else{ HMI_WRITE("selfcheck.t9.bco=63488"); device_status.C01 |= (1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门故障\""); } rt_thread_delay(1000); /********************************************************/ //打开下仓门 ctr_cangmen_down(OPEN); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_down(OFF); current_open = calculate_average(current_i,5); if(current_open-current_>100) { HMI_WRITE("selfcheck.t10.bco=2024"); device_status.C01 &= ~(1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门正常\""); } else{ HMI_WRITE("selfcheck.t10.bco=63488"); device_status.C01 |= (1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门故障\""); } //开启加热 rt_thread_delay(1000); // if(atoi(device_status.heat_power)>5000) { HMI_WRITE("selfcheck.t2.bco=2024"); device_status.C01 &= ~(1<<1); HMI_WRITE("sysSet.va0.txt=\"加热正常\""); } 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=\"加热正常\""); } else { HMI_WRITE("selfcheck.t2.bco=63488"); device_status.C01 |= (1<<1); HMI_WRITE("sysSet.va0.txt=\"加热故障\""); } } 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 = 30;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); rt_thread_delay(1000); if(atoi(device_status.power)-current_open>150) { HMI_WRITE("selfcheck.t11.bco=2024"); device_status.C01 &= ~(1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫正常\""); } else { HMI_WRITE("selfcheck.t11.bco=63488"); device_status.C01 |= (1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫故障\""); } /*******************************************************/ 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, 60000, 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);