From: kyz Date: Fri, 24 May 2024 06:41:18 +0000 (+0800) Subject: 增加设备离线时间计算 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=779b49d0874dded450ee4bec0d2bcbcf42668f13;p=yn%2Fweldingcontroller.git 增加设备离线时间计算 增加bypass功能是否启用判断 累计流量计算调用改为使用事件触发 --- diff --git a/Core/Src/main.c b/Core/Src/main.c index cd68679..7faf471 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -132,7 +132,7 @@ int main(void) MX_UART4_Init(); MX_OCTOSPI1_Init(); MX_TIM1_Init(); - //MX_IWDG1_Init(); + MX_IWDG1_Init(); MX_TIM3_Init(); /* USER CODE BEGIN 2 */ @@ -316,17 +316,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) } /* USER CODE BEGIN Callback 1 */ else if( htim->Instance == TIM3 ){ - extern void flow_total_update( uint32_t ms ); -#if 1 - flow_total_update( 1000 ); //1秒定时器触发 -#else - static uint32_t cnt_1s = 0; - cnts++; //1 毫秒定时器触发 - if( cnt_1s >= 1000 ){ //1000ms 计算一次累计流量 - flow_total_update( cnt ); - cnt_1s = 0; - } -#endif + osEventFlagsSet( g_dev.ev_group, EVENT_TIMER_1S ); } /* USER CODE END Callback 1 */ } diff --git a/user/user.h b/user/user.h index 16ad659..a2ccc4a 100644 --- a/user/user.h +++ b/user/user.h @@ -22,6 +22,7 @@ #include "usart.h" #include "usb_device.h" #include "gpio.h" +#include "user_lfs.h" //#include "fmc.h" //-------------------- #define APP_VER_MAJOR 3 @@ -73,6 +74,7 @@ void debug_printf(char* str , ...); #define EVENT_ADC1_UPDATE ( 1<<4) //ADC1 DMA采集完成 #define EVENT_ADC2_UPDATE ( 1<<5) //ADC2 DMA采集完成 #define EVENT_ADC3_UPDATE ( 1<<6) //ADC3 DMA采集完成 +#define EVENT_TIMER_1S ( 1<<7) //1秒定时器信息,用于保存累计流量和计算离线时间 //SPI FLASH 空间分配, 一共4M //APP存储分区 @@ -229,6 +231,9 @@ typedef struct { char serial_number[32]; alarm_t alarm; uint32_t hw_ver; //2 or 3 + uint32_t has_bypass; + + uint32_t offline_secs; //offline time; }dev_info_t; extern TIM_HandleTypeDef htim2; //htim2 用于触发DMA ADC采集 diff --git a/user/user_app.c b/user/user_app.c index 5d41ea0..085c427 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -58,6 +58,10 @@ dev_info_t g_dev; extern void debug_task_start( void ); extern void wifi_task_start( void ); extern void eth_task_start( void ); + +extern void flow_total_update( uint32_t ms ); +extern void offline_time_update( uint32_t add_secs ); + void ui_task_start_h3( void ); int32_t lcd_init_h3( void ); void beep_init( void ); @@ -94,14 +98,20 @@ void StartDefaultTask(void *argument) dev_cfg_load( &g_dev.cfg ); dev_cfg_v2_load( &g_dev.cfg2 ); - lfs_load_device_serial( g_dev.serial_number, sizeof( g_dev.serial_number ) ); + lfs_load_device_serial( g_dev.serial_number, sizeof( g_dev.serial_number ) ); + g_dev.offline_secs = lfs_load_offline_time(); g_dev.ev_group = xEventGroupCreate(); - if( lcd_init_h3() == 0 ) + if( lcd_init_h3() == 0 ){ g_dev.hw_ver = 3; - else + g_dev.has_bypass = 0; + g_dev.cfg.bypass = 0; + g_dev.cfg.bypass_save = 0; + }else{ g_dev.hw_ver = 2; + g_dev.has_bypass = 1; + } DBG_PRINTF( "WeldingController V%d.%d\n", APP_VER_MAJOR, APP_VER_MINOR ); adcTaskHandle = osThreadNew(user_adc_task, NULL, &adcTask_attributes); @@ -118,13 +128,26 @@ void StartDefaultTask(void *argument) void ui_task_start_h3( void ); ui_task_start_h3(); }else{ - g_dev.hw_ver = 2; uiTaskHandle = osThreadNew(user_ui_task, NULL, &uiTask_attributes); } + HAL_TIM_Base_Start_IT( &htim3 ); + EventBits_t uxBits; while(1){ - HAL_IWDG_Refresh( &hiwdg1 ); //4s iwdg - osDelay(1000); + uxBits = xEventGroupWaitBits( + g_dev.ev_group, /* The event group being tested. */ + EVENT_TIMER_1S, /* The bits within the event group to wait for. */ + pdTRUE, /* BIT_x should be cleared before returning. */ + pdFALSE, /* Don't wait for all bits, either bit will do. */ + osWaitForever );/* Wait a maximum of xxx ms for either bit to be set. */ + + if( uxBits & EVENT_TIMER_1S ){ + //debug_printf( "offline time:%d\n", g_dev.offline_secs ); + flow_total_update( 1000 ); //1秒定时器触发 + offline_time_update( 1 ); //离线更新,加1秒 + + HAL_IWDG_Refresh( &hiwdg1 ); //4s iwdg + } } } diff --git a/user/user_cdc_rx_2.c b/user/user_cdc_rx_2.c index 528bb80..5f738d5 100644 --- a/user/user_cdc_rx_2.c +++ b/user/user_cdc_rx_2.c @@ -519,7 +519,11 @@ int32_t handle_cdc_pkt( cdc_pkt_t *pkt ) uint32_t coeff = get_u32_big( &pkt->pdata[16] ); uint32_t bypass_save = get_u32_big( &pkt->pdata[20] ); uint32_t bypass = get_u32_big( &pkt->pdata[24] ); - + + if( !g_dev.has_bypass ){ + bypass = 0; + } + if( factor >=2 && factor <= 30 ) g_dev.cfg.save_gas_factor = factor; if( method <3 ) @@ -640,23 +644,6 @@ int32_t handle_cdc_pkt( cdc_pkt_t *pkt ) resp[0] = 1; tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 ); break; -#if 0 - case 0xC2: //气路读取 1B 0:bypass_off, 1:bypass_on - resp[0] = g_dev.cfg.bypass; - tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 ); - break; - case 0xC3: //气路选择 1B 0:bypass_off, 1:bypass_on - if( pkt->pdata[0] == 1 || pkt->pdata[0] == 0 ){ - g_dev.cfg.bypass = pkt->pdata[0]; - dev_cfg_store( &g_dev.cfg ); - resp[0] = 1; - lcd_update_cfg(); - }else{ - resp[0] = 0; - } - tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 ); - break; -#endif #endif } diff --git a/user/user_eth.c b/user/user_eth.c index 4d56541..c47a61c 100644 --- a/user/user_eth.c +++ b/user/user_eth.c @@ -230,6 +230,7 @@ int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port ) tick = xTaskGetTickCount(); while( client->connected==0 && xTaskGetTickCount() - tick < 5*1000 ){ + osDelay(100); } if( client->connected > 0 ){ client->pcb = tpcb; diff --git a/user/user_flowmeter.c b/user/user_flowmeter.c index 99d15ed..7a86e26 100644 --- a/user/user_flowmeter.c +++ b/user/user_flowmeter.c @@ -202,10 +202,8 @@ extern float lfs_load_flow_total( void ); void flow_total_update( uint32_t ms ) { - if( g_dev.flowmeter.flow_speed > 0 ){ - //debug_printf( "------ update flow total %0.1f -> ", g_flow_total ); + if( g_dev.flowmeter.id == FLOWMETER_SMC && g_dev.flowmeter.flow_speed > 0 ){ g_flow_total += g_dev.flowmeter.flow_speed*ms/(60*1000.0f); - //debug_printf( " %0.1f \n", g_flow_total ); } } @@ -220,9 +218,6 @@ void user_flowmeter_task( void* unused ) float flow_comp; float save_flow_total = 0; - osDelay( 100 ); - save_flow_total = lfs_load_flow_total(); - //检查流量计类型 g_dev.flowmeter.id = g_dev.cfg2.flowmeter; debug_printf( "flowmeter task start, flowmeter:%d ...\n", g_dev.flowmeter ); @@ -281,20 +276,10 @@ void user_flowmeter_task( void* unused ) if( g_dev.flowmeter.id== FLOWMETER_SMC ){ - //load flow_total - g_flow_total = lfs_load_flow_total(); - g_dev.flowmeter.flow_total = g_flow_total; - - HAL_TIM_Base_Start_IT( &htim3 ); - } -#if 0 - if( g_dev.flowmeter.id== FLOWMETER_AFM0750 ){ - //测试软件计算累计流量 + //load flow_total from lfs g_flow_total = lfs_load_flow_total(); g_dev.flowmeter.flow_total = g_flow_total; - HAL_TIM_Base_Start_IT( &htim3 ); } -#endif if( g_dev.flowmeter.id == FLOWMETER_MF4008 ){ //MF4008 检查修正系数, 只开机检查, 如果通过上位机修改参数,需要重启系统。 @@ -358,9 +343,7 @@ void user_flowmeter_task( void* unused ) g_dev.flowmeter.tem = reg_values[3]/10.0f; g_dev.flowmeter.state = reg_values[4]; flow_err = 0; -#if 0 - total_raw = g_flow_total; //for test -#endif + }else{ flow_err ++; //读取失败, 故障计数到10次报错 } @@ -406,7 +389,7 @@ void user_flowmeter_task( void* unused ) g_dev.flowmeter.flow_speed = flow_raw; g_dev.flowmeter.flow_total = total_raw; - if( g_dev.flowmeter.id == FLOWMETER_SMC || g_dev.flowmeter.id == FLOWMETER_AFM0750 ){ + if( g_dev.flowmeter.id == FLOWMETER_SMC ){ static int n = 0; if( n++ >= 100 ){ //10秒 保存一次 n = 0; diff --git a/user/user_lfs.c b/user/user_lfs.c index e960455..e24b5db 100644 --- a/user/user_lfs.c +++ b/user/user_lfs.c @@ -164,3 +164,23 @@ int32_t lfs_store_device_serial( char *sn ) { return user_lfs_write( DEVICE_SN_PATH, (uint8_t*)sn, strlen( sn ) ) == strlen(sn)?0:-1; } + +uint32_t lfs_load_offline_time( void ) +{ + uint32_t secs = 0; + + if( user_lfs_read( OFFLINE_TIME_PATH, (uint8_t*)&secs, sizeof( secs ) ) == sizeof( secs ) ){ + + } + return secs; +} + +int32_t lfs_store_offline_time( uint32_t secs ) +{ + int32_t ret = -1; + + if( user_lfs_write( OFFLINE_TIME_PATH, (uint8_t*)&secs, sizeof( secs ) ) == sizeof( secs ) ){ + ret = 0; + } + return ret; +} \ No newline at end of file diff --git a/user/user_lfs.h b/user/user_lfs.h index 0940292..29157ff 100644 --- a/user/user_lfs.h +++ b/user/user_lfs.h @@ -3,7 +3,8 @@ #include "lfs.h" -#define FLOW_TOTAL_PATH "flowmeter_total" +#define FLOW_TOTAL_PATH "flow_total" +#define OFFLINE_TIME_PATH "offline_time" #define DEVICE_SN_PATH "device_serial" #define DEVICE_CONFIG_PATH "device_config" #define DEVICE_CONFIG_V2_PATH "device_config_v2" @@ -16,5 +17,6 @@ float lfs_load_flow_total( void ); int32_t lfs_store_flow_total( float total ); int32_t lfs_load_device_serial( char *read_buffer, uint32_t buffer_size ); int32_t lfs_store_device_serial( char *sn ); - +uint32_t lfs_load_offline_time( void ); +int32_t lfs_store_offline_time( uint32_t secs ); #endif //__USER_LFS_H__ diff --git a/user/user_report.c b/user/user_report.c index e55aa23..69efc71 100644 --- a/user/user_report.c +++ b/user/user_report.c @@ -43,6 +43,28 @@ static const char *fmt = "{\n\ #endif +void offline_time_update( uint32_t add_secs ) + { + static uint32_t n = 0; + if( g_dev.wifi_connected == 2 || g_dev.eth_connected == 2 ){ + //在线 + if( g_dev.offline_secs != 0 ){ + g_dev.offline_secs = 0; + lfs_store_offline_time( 0 ); + } + n = 0; + return; + }else{ + //离线,更新离线时间 + g_dev.offline_secs += add_secs; + if( n++ > 60 ){ //1分钟保存一次 + n = 0; + //这个timer中断中调用,不能调用文件系统读写操作 + lfs_store_offline_time( g_dev.offline_secs ); + } + } +} + static void mac_str_init( void ) { uint8_t mac[6]; diff --git a/user/user_ui.c b/user/user_ui.c index 40fb829..fd74275 100644 --- a/user/user_ui.c +++ b/user/user_ui.c @@ -222,6 +222,8 @@ void ui_handle_lcd_pkt( void ) }else if( ctrl_addr == lcd_valve_bypass_addr ){//比例阀旁路 uint8_t bypass = ctrl_value; //g_dev.ui_bypass = bypass; + if( !g_dev.has_bypass ) + bypass = 0; g_dev.cfg.bypass = bypass; dev_cfg_store( &g_dev.cfg ); }else if( ctrl_addr == lcd_wifi_config_addr ){ //点击配置按钮 @@ -365,14 +367,14 @@ void lcd_write_u16( uint16_t addr, uint16_t value ) void lcd_power_on( void ) { - if( g_dev.hw_ver != 3 ){ + if( g_dev.hw_ver == 2 ){ HAL_GPIO_WritePin( LCM_EN_GPIO_Port, LCM_EN_Pin, GPIO_PIN_SET ); } } void lcd_power_off( void ) { - if( g_dev.hw_ver != 3 ){ + if( g_dev.hw_ver == 2 ){ HAL_GPIO_WritePin( LCM_EN_GPIO_Port, LCM_EN_Pin, GPIO_PIN_RESET ); } } diff --git a/user/user_valve_ctrl.c b/user/user_valve_ctrl.c index 748b1cd..1e53252 100644 --- a/user/user_valve_ctrl.c +++ b/user/user_valve_ctrl.c @@ -557,14 +557,16 @@ void user_valve_ctrl_task( void* unused ) // welding_data_store( g_dev.welding_time/100 ); } - if( g_dev.cfg.bypass || tem_err_state ){ - //屏上点击旁路或温度故障,开启旁路 - valve_bypass( 1 ); //旁路电磁阀开启 - g_dev.valve_curr_out = 0; //比例电磁阀关闭 - }else if( !g_dev.cfg.bypass && !tem_err_state ){ - //恢复正常,关闭旁路 - valve_bypass( 0 ); //旁路电磁阀关闭 - } + if( g_dev.has_bypass){ + if( g_dev.cfg.bypass || tem_err_state ){ + //屏上点击旁路或温度故障,开启旁路 + valve_bypass( 1 ); //旁路电磁阀开启 + g_dev.valve_curr_out = 0; //比例电磁阀关闭 + }else{ + //恢复正常,关闭旁路 + valve_bypass( 0 ); //旁路电磁阀关闭 + } + } //更新比例阀驱动电流 user_valve_set_current( g_dev.valve_curr_out ); @@ -640,7 +642,7 @@ void flow_low_alarm_check( void ) if( now - alarm_start > 10*1000 ){ //计时到10秒,开直通阀 alarm = 3; - if( g_dev.cfg.bypass == 0 ){ + if( g_dev.cfg.bypass == 0 && g_dev.has_bypass ){ g_dev.cfg.bypass = 1; ui_cfg_remote_update(); //刷新lcd }