From: kyz Date: Tue, 30 Jan 2024 07:14:31 +0000 (+0800) Subject: 整机功能调整可以运行 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=3e0342d28fe4bf4020fed8e21213d85a1c6d8044;p=yn%2Fweldingcontroller.git 整机功能调整可以运行 --- diff --git a/Core/Src/adc.c b/Core/Src/adc.c index 303e46c..e67f89c 100644 --- a/Core/Src/adc.c +++ b/Core/Src/adc.c @@ -124,7 +124,11 @@ void MX_ADC2_Init(void) hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT; hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc2.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; - hadc2.Init.OversamplingMode = DISABLE; + hadc2.Init.OversamplingMode = ENABLE; + hadc2.Init.Oversampling.Ratio = 16; + hadc2.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_4; + hadc2.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; + hadc2.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; if (HAL_ADC_Init(&hadc2) != HAL_OK) { Error_Handler(); diff --git a/Core/Src/spi.c b/Core/Src/spi.c index b824c03..d6276b3 100644 --- a/Core/Src/spi.c +++ b/Core/Src/spi.c @@ -45,7 +45,7 @@ void MX_SPI1_Init(void) hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; diff --git a/MDK-ARM/welding_controller.uvprojx b/MDK-ARM/welding_controller.uvprojx index f7f341d..70542a1 100644 --- a/MDK-ARM/welding_controller.uvprojx +++ b/MDK-ARM/welding_controller.uvprojx @@ -339,7 +339,7 @@ USE_HAL_DRIVER,STM32H730xx - ../Core/Inc;../USB_DEVICE/App;../USB_DEVICE/Target;../Drivers/STM32H7xx_HAL_Driver/Inc;../Drivers/STM32H7xx_HAL_Driver/Inc/Legacy;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc;../Drivers/CMSIS/Device/ST/STM32H7xx/Include;../Drivers/CMSIS/Include;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch + ../Core/Inc;../USB_DEVICE/App;../USB_DEVICE/Target;../Drivers/STM32H7xx_HAL_Driver/Inc;../Drivers/STM32H7xx_HAL_Driver/Inc/Legacy;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc;../Drivers/CMSIS/Device/ST/STM32H7xx/Include;../Drivers/CMSIS/Include;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch;../lib @@ -961,6 +961,16 @@ + + lib + + + gas_flow.lib + 4 + ..\lib\gas_flow.lib + + + ::CMSIS diff --git a/lib/gas_flow.lib b/lib/gas_flow.lib new file mode 100644 index 0000000..8fb9825 Binary files /dev/null and b/lib/gas_flow.lib differ diff --git a/lib/weld_alg.h b/lib/weld_alg.h new file mode 100644 index 0000000..aa51bdb --- /dev/null +++ b/lib/weld_alg.h @@ -0,0 +1,59 @@ +#ifndef WELD_ALG_H_ +#define WELD_ALG_H_ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#if DEBUG + #define DBG_PRINTF(fmt, args...) \ + {\ + printf("[%s:%d] ", __FILE__, __LINE__);\ + printf(fmt, ##args);\ + } +#else + #define DBG_PRINTF(fmt, args...) +#endif + +#define UID_BASE (0x1FF1E800UL) /*!< Unique device ID register base address */ +#define DEVICE_ID1 (UID_BASE) +#define DEVICE_ID2 (UID_BASE + 0x4) +#define DEVICE_ID3 (UID_BASE + 0x8) + +typedef enum ResultCode_t +{ + OK, + FAILED, + LICENSE_INVALID, + WELD_METHOD_INVALID, + CURRENT_TOO_LOW, + CURRENT_TOO_HIGH, + MCU_UUID_CHECK_SUCCESS, + MCU_UUID_CHECK_FAILED, + DECODE_SUCCESS, + DECODE_FAILED, + PARAMETER_INVAILD, + FACTOR_INVALID +}ResultCode; + +typedef enum WeldMethod_t +{ + TIG, + MIG, + MAG, + INVALID +}WeldMethod; + + +/** + * current[in]: 输入电流值 合法值区间[5, 500] + * factor[in]: 焊接参数影响因子 合法值区间[2, 30] + * gas[out]: 需要输出的气体流量值 + */ +ResultCode caculate(WeldMethod method, uint32_t current, uint32_t factor, double* gas); +#ifdef __cplusplus +} +#endif +#endif /* WELD_ALG_H_ */ diff --git a/tools/welding.py b/tools/welding.py index 3750d63..f18061e 100644 --- a/tools/welding.py +++ b/tools/welding.py @@ -57,6 +57,13 @@ class App( QDialog, ui_welding.Ui_Dialog ): self.rx_buf = b'' self.generate_image() + self.btn_current.released.connect( self.on_btn_current ) + + def on_btn_current( self ): + curr = int( self.lineEdit_current.text() ) + cmd_str = "I:%d"%( curr*1000 ) + self.ser.write( cmd_str.encode( "utf-8" ) ) + def generate_image(self): verticalLayout = QVBoxLayout(self.frame) win = pg.GraphicsLayoutWidget(self.frame) diff --git a/tools/welding.ui b/tools/welding.ui index d8a35ce..29a332c 100644 --- a/tools/welding.ui +++ b/tools/welding.ui @@ -12,7 +12,7 @@ 0 0 - 969 + 967 797 @@ -39,7 +39,7 @@ 330 10 - 631 + 481 101 @@ -126,6 +126,9 @@ 连接 + + false + @@ -389,6 +392,48 @@ QFrame::Raised + + + + 930 + 0 + 31 + 31 + + + + 100 + + + + + + 840 + 40 + 121 + 71 + + + + 设置电流 + + + true + + + + + + 850 + 0 + 71 + 31 + + + + 焊接电流 A: + + diff --git a/user/user.h b/user/user.h index 1ba1730..942d96f 100644 --- a/user/user.h +++ b/user/user.h @@ -47,12 +47,12 @@ typedef struct{ float pid_kd; }pid_param_t; -#define DEVICE_CFG_FLAG 2 //FLAG: 1--255:normal, 0:invalid +#define DEVICE_CFG_FLAG 0x11 //FLAG: 1--255:normal, 0:invaild typedef struct{ uint8_t flag; uint8_t save_gas_factor; //0-100 uint8_t valve_open_default; //0-100 - uint8_t welding_method; //0-3d + uint8_t welding_method; //0-2d pid_param_t pid_param[5]; //根据不同气压选择合适pid控制参数 }dev_cfg_t; @@ -64,6 +64,7 @@ typedef struct { flowmeter_t flowmeter; //流量计采集实时状态 float pressure; //压力计采集信息 float welding_current; //互感器电流 + uint32_t welding_current_raw; float valve_temperature; //比例阀温度采集电压,<0.55V热温保流程,打开旁路双通气阀,(触发后延时>5min散热后,主切回原值) //输出控制 diff --git a/user/user_adc.c b/user/user_adc.c index 4d2113e..8125acd 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -53,6 +53,11 @@ void user_adc_task( void* unused ) uint32_t raw; float voltage; + HAL_ADCEx_Calibration_Start( &hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED ); + HAL_ADCEx_Calibration_Start( &hadc2, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED ); + HAL_ADCEx_Calibration_Start( &hadc3, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED ); + osDelay(100); + HAL_TIM_Base_Start_IT( &htim2); //ADC 用TIM2 1毫秒触发采集,dma采集500次0.5秒产生中断 @@ -92,6 +97,7 @@ void user_adc_task( void* unused ) //非脉冲模式,取平均值 raw = adc_average( adc_raw_copy[1], 500 ); voltage = raw*VREF/0xffff; //16bit adc2 + g_dev.welding_current_raw = raw; } //计算公式:2.5V±0.625V 0V基准 @@ -99,16 +105,16 @@ void user_adc_task( void* unused ) //反向:2.5-(I/Ipn)*0.625 //额定输入 Ipn = 600A, 测量范围 Ipm = Ipn*1.2 //电流 = (V-2.5)/0.625 * Ipn - voltage *=5; //互感器输出电压 分压 100+20 1/5 - #if 1 + + voltage *=6; //互感器输出电压 分压 100+20 1/5 + + { + //第一台样机,实际测量,传感器空闲时 输出时2.49V, 这里认为2.49 为0A + //TODO:后面这个地方在UI界面上加一个校0的值来完成 + //voltage += 0.01f; + } + g_dev.welding_current = (voltage-2.5f)/0.625f*600; - #else - //设置电流数据调试用 - if( last_tick < 5000 ) - g_dev.welding_current = 0; - else - g_dev.welding_current = 10+last_tick/1000; - #endif } if( adc_update_flag[2] ){ diff --git a/user/user_app.c b/user/user_app.c index 51130b9..4ab234d 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -49,6 +49,19 @@ dev_info_t g_dev; EventGroupHandle_t ui_ev_group; //test lib +#define UID_BASE (0x1FF1E800UL) /*!< Unique device ID register base address */ +#define DEVICE_ID1 (UID_BASE) +#define DEVICE_ID2 (UID_BASE + 0x4) +#define DEVICE_ID3 (UID_BASE + 0x8) + +void GetChipID ( uint32_t *id ) +{ + //小端模式 + id[0] = *( uint32_t *) ( DEVICE_ID1 ); //高32位地址 + id[1] = *( uint32_t *) ( DEVICE_ID2 ); //中32位地址 + id[2] = *( uint32_t *) ( DEVICE_ID3 ); //低32位地址 + printf ("此芯片唯一ID为: 0x%X-%X-%X\r\n",id[0],id[1],id[2] ); +} void StartDefaultTask(void *argument) { @@ -65,6 +78,9 @@ void StartDefaultTask(void *argument) g_dev.ev_group = xEventGroupCreate(); printf( "WeldingController %s\n", sw_ver ); + + volatile uint32_t id[3]; + GetChipID( id ); adcTaskHandle = osThreadNew(user_adc_task, NULL, &adcTask_attributes); ctrlTaskHandle = osThreadNew(user_valve_ctrl_task, NULL, &ctrlTask_attributes); @@ -113,19 +129,21 @@ int32_t dev_cfg_load( dev_cfg_t *cfg ) { dev_cfg_t rd_cfg; user_nv_read( 0, (uint8_t*)&rd_cfg, sizeof( rd_cfg ) ); - if( rd_cfg.flag == DEVICE_CFG_FLAG ){ + if( rd_cfg.flag == DEVICE_CFG_FLAG + && rd_cfg.welding_method < 3 + && rd_cfg.save_gas_factor>=2 && rd_cfg.save_gas_factor <=30 ){ memcpy( cfg, &rd_cfg, sizeof(rd_cfg) ); }else{ //load default value cfg->flag = DEVICE_CFG_FLAG; for( int i=0; i<5; i++ ){ - cfg->pid_param[i].pid_kp = 0.5; - cfg->pid_param[i].pid_ki = 0.1; - cfg->pid_param[i].pid_kd = 0.1; + cfg->pid_param[i].pid_kp = 0.2; + cfg->pid_param[i].pid_ki = 0.0; + cfg->pid_param[i].pid_kd = 0.05; } - cfg->save_gas_factor = 0; + cfg->save_gas_factor = 30; //2--30 cfg->valve_open_default = 0; - cfg->welding_method = 0; + cfg->welding_method = 0; //TIG } return 0; @@ -215,7 +233,7 @@ void welding_data_store( uint32_t index ) item.flow_speed_x10 = g_dev.flowmeter.flow_speed*10; item.pressure_x10 = g_dev.pressure * 10; item.save_factor = g_dev.cfg.save_gas_factor; - item.target_flowspeed_x10 = g_dev.target_flowspeed; + item.target_flowspeed_x10 = g_dev.target_flowspeed *10 ; item.valve_curr_ma = g_dev.valve_curr_out*1000; item.valve_temp_x10 = g_dev.valve_temperature*10; item.whelding_method = g_dev.cfg.welding_method; diff --git a/user/user_debug.c b/user/user_debug.c index 4bb974d..9458295 100644 --- a/user/user_debug.c +++ b/user/user_debug.c @@ -37,7 +37,7 @@ const osThreadAttr_t debugTask_attributes = { void user_debug_task( void * unused ); void debug_task_start( void ) { - // debugTaskHandle = osThreadNew(user_debug_task, NULL, &debugTask_attributes); + debugTaskHandle = osThreadNew(user_debug_task, NULL, &debugTask_attributes); } void user_cdc_rx_xb( uint8_t *data, uint32_t len ) @@ -67,10 +67,14 @@ void lcd_uart_rx_cb( void ) lcd_uart_rx_start(); #endif +float dbg_current = -100.0f; + void user_debug_task( void * unused ) { //调试接口,用于从usb cdc接收执行指令 EventBits_t uxBits; + uint32_t ma; + uint32_t mv; while(1){ const TickType_t xTicksToWait = 10000 / portTICK_PERIOD_MS; @@ -84,6 +88,25 @@ void user_debug_task( void * unused ) if( uxBits & EVENT_CDC_RX_DATA ){ //解析和处理 cdc_rx_buf, cdc_rx_number DBG_PRINTF( "cdc rx %d Bytes\n", cdc_rx_number ); + cdc_rx_buf[cdc_rx_number] = 0; + char *ptr = strstr( (char*)cdc_rx_buf, "DAC:" ); + if( ptr ){ + uint32_t dac_raw=0; + if( sscanf( ptr, "DAC:%d\n", &mv )==1){ + uint32_t dac_raw = mv*0xfff/2500; //最大12bit=VERF_BUF 2.5V + HAL_DAC_SetValue( &hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dac_raw ); + HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); + DBG_PRINTF( "DAC Set: %dmV %d\n", mv, dac_raw ); + } + + + } + + if((ptr=strstr( (char*)cdc_rx_buf, "I:" ))&& sscanf( ptr, "I:%d\n", &ma )==1){ + dbg_current = ma/1000.0f; + DBG_PRINTF( "I Set: %0.3fA\n", dbg_current ); + + } cdc_rx_number = 0; } if( uxBits & EVENT_DBG_UART_RX_DATA ){ diff --git a/user/user_ui.c b/user/user_ui.c index 5aa1707..6ebfedc 100644 --- a/user/user_ui.c +++ b/user/user_ui.c @@ -77,16 +77,22 @@ void ui_handle_lcd_pkt( void ) ctrl_value = (lcd_resp.value[j*2]<<8)| lcd_resp.value[j*2+1]; if( ctrl_addr == lcd_save_gas_factor_addr ){//节气因子 uint8_t save_gas_factor = ctrl_value; - g_dev.cfg.save_gas_factor = save_gas_factor; - dev_cfg_store( &g_dev.cfg ); + if( save_gas_factor >= 2 && save_gas_factor <= 30 && save_gas_factor != g_dev.cfg.save_gas_factor ){ + g_dev.cfg.save_gas_factor = save_gas_factor; + dev_cfg_store( &g_dev.cfg ); + } }else if( ctrl_addr == lcd_valve_open_default_addr ){//比例阀开度 uint8_t valve_open_default = ctrl_value; g_dev.cfg.valve_open_default = valve_open_default; dev_cfg_store( &g_dev.cfg ); }else if( ctrl_addr == lcd_welding_method_addr ){//焊接工艺 - uint8_t welding_method = ctrl_value; - g_dev.cfg.welding_method = welding_method; - dev_cfg_store( &g_dev.cfg ); + if( ctrl_value > 0 ) + ctrl_value -= 1; //ui 1..3/TIG.MIG.MAG -> mcu 0..2 + if( ctrl_value < 3 ){ + uint8_t welding_method = ctrl_value; + g_dev.cfg.welding_method = welding_method; + dev_cfg_store( &g_dev.cfg ); + } } } break; @@ -109,7 +115,7 @@ int32_t ui_wait_lcd_rx_event( uint32_t event, uint32_t timeout ) xTicksToWait );/* Wait a maximum of xxx ms for either bit to be set. */ } -void lcd_read( uint16_t addr, uint8_t reg_nr ) +int32_t lcd_read( uint16_t addr, uint8_t reg_nr ) { uint8_t tx_buf[100]; uint8_t tx_n=0; @@ -132,9 +138,10 @@ void lcd_read( uint16_t addr, uint8_t reg_nr ) if( ui_wait_lcd_rx_event( EVENT_LCD_RX_DATA, 1000 ) ){ //收到回应 ui_handle_lcd_pkt(); - break; + return 0; } } + return -1; }; void lcd_write( uint16_t addr, uint16_t *value, uint8_t reg_nr ) @@ -179,15 +186,19 @@ void user_ui_task( void* unused ) lcd_uart_rx_start(); - osDelay(1000); + osDelay(2000); + //等待lcd通信建立 + //TODO + + osDelay(100); //初始化, 将变量发送到串口屏 - + reg_values[0] = g_dev.welding_current*10; reg_values[1] = g_dev.flowmeter.flow_speed*10; reg_values[2] = g_dev.pressure; reg_values[3] = g_dev.flowmeter.flow_total; - reg_values[4] = g_dev.cfg.welding_method; + reg_values[4] = g_dev.cfg.welding_method+1; reg_values[5] = g_dev.cfg.save_gas_factor; reg_values[6] = g_dev.cfg.valve_open_default; lcd_write( lcd_welding_current_addr, reg_values, 7 ); @@ -206,7 +217,7 @@ void user_ui_task( void* unused ) reg_values[2] = g_dev.pressure; reg_values[3] = g_dev.flowmeter.flow_total; lcd_write( lcd_welding_current_addr, reg_values, 4 ); - //reg_values[4] = g_dev.cfg.welding_method; + //reg_values[4] = g_dev.cfg.welding_method+1; //reg_values[5] = g_dev.cfg.save_gas_factor; //reg_values[6] = g_dev.cfg.valve_open_default; //lcd_write( lcd_welding_current_addr, reg_values, 7 ); diff --git a/user/user_valve_ctrl.c b/user/user_valve_ctrl.c index d2deb44..4905830 100644 --- a/user/user_valve_ctrl.c +++ b/user/user_valve_ctrl.c @@ -21,71 +21,18 @@ void valve_bypass( uint32_t enabled ) osDelay( 1000 ); } -float flowspeed_to_current( float flowspeed ) -{ - //按60mA = 0L/min --> 165mA -> 100 L/min - float current = 0.0f; - - if( flowspeed > 100 ) - current = 165; - else if( flowspeed > 0) - current = flowspeed*(165-60)/100 + 60; - - return current; -} - -float current_to_flowspeed( float current ) -{ - //按60mA = 0L/min --> 165mA -> 100 L/min - float flow = 0.0f; - - if( current > 165 ) - flow = 100; - else if( current >60 ) - flow = (current-60)*(100-0)/(165-60); - - return flow; - -} - -float dac_to_current( float dac_vol ) -{ - //60~165mA -- 1.16~1.73V - float current=0.0f; - if( dac_vol < 1.16f ) - current = 0; - else if( dac_vol > 1.73f ) - current = 165; - else - current = (dac_vol-1.16f)/(1.73f-1.16f)*(165-60)+60; - return current; -} -float current_to_dac( float current ) -{ - float vol = 0.0f; - //60~165mA -- 1.16~1.73V - if( current < 60 ) - vol = 1.16; - else if( current > 165 ) - vol = 1.73; - else - vol = (current-60)/(165-60)*(1.73-1.16)+1.16; - return vol; -} - float flowspeed_to_curr_add( float flowspeed ) { //增量换算, 增加的流速换算到电流 //电流到流量 60--165mA -> 0-100 L/min - //电流到电压 60~165mA -> 1.16~1.73V - //流量到电压 1.16-1.73V -> 0-100 L/min - return flowspeed*( 0.165f-0.06f)/100; -} - -float curr_to_flowspeed_add( float current ) -{ - //增量换算, 增加的电流换算到流速 - return current*100/( 0.165-0.06); + if( g_dev.pressure < 300 ) //0.2Mp + 大气压0.101 + return flowspeed*( 0.165f-0.13f)/30; + else if( g_dev.pressure < 450 ) //0.35Mp + 0.101 + return flowspeed*( 0.165f-0.11f)/60; + else if( g_dev.pressure < 600 ) //0.5Mp + 0.101 + return flowspeed*( 0.165f-0.09f)/80; + else //0.7Mpa + 0.101 + return flowspeed*( 0.165f-0.06f)/110; } //PVQ 31 5 G 16 01 @@ -95,25 +42,15 @@ float curr_to_flowspeed_add( float current ) //流量 L/min(最高动作压力差时) 0--100 L/min //用MCU的DAC输出信号控制比例阀的电流 -float get_target_flow( float current, uint8_t param2 ) -{ - float target_flow; - //需要调用客户提供算法,根据焊接电流获取目标流量 - //ToDo - - //客户提供算法前,暂使用如下瞎写的调试代码,后面有设备时再调整 - if( current < 0.5f ){ - //焊接没有工作,气路需要打开 - target_flow = 10; - }else if( current > 100 ){ - //最大流量 - target_flow = 50; - }else { - //按比例计算 - target_flow = (current-0.5f)/50; - } - return target_flow; -} +/*实际测量值 +750mV 0mA +800mV 10mA +850mV 35mA +900mV 100mA +950mV 150mA +100mV 170mA +*/ + typedef struct{ arm_pid_instance_f32 S; @@ -173,7 +110,35 @@ void pid_ctrl_update( void ) float valve_current_to_vol( float current ) { float vol; - vol = (current-0.06f)/(0.165f-0.06f)*(1.73-1.16)+1.16; + //vol = (current-0.06f)/(0.165f-0.06f)*(1.73-1.16)+1.16; + /* + 实际测量值: + 850mV 35mA + 900mV 100mA + 950mV 150mA + 1000mV 170mA + 电流到电压的转换 + */ + + if( current < 0.035f ){ + //35毫安以下,按0设置,直接DAC输出0V + vol = 0; + }else if( current < 0.1f ){ + //100mA以下计算电压 + //850mV/35mA -- 900mV/100mA + vol = 0.85f + (0.9f-0.85f)*(current - 0.035f)/(0.100f-0.035f); + }else if( current < 0.15f ){ + //150mA以下计算电压 + //950mV/150mA -- 900mV 100mA + vol = 0.9f + (0.95f-0.9f)*(current - 0.100f)/(0.150f-0.100f); + }else if( current < 0.17f ){ + //170mA以下计算电压 + //1000mV/170mA -- 950mV/150mA + vol = 0.95f + (1.000f-0.95f)*(current - 0.150f)/(0.170f-0.150f); + }else{ + //最大输出1v对应170mA + vol = 1.0f; + } return vol; } @@ -181,11 +146,11 @@ void user_valve_set_current( float current ) { float vol; uint32_t dac_raw; - + vol = valve_current_to_vol( current ); + dac_raw = vol*0xfff/VREF; //最大12bit=VERF_BUF 2.5V static float current_last = 0.0f; - if( (current - current_last) > 0.0001f || current - current_last < -0.0001f ){ printf( "open:%d%%, current:%0.3fA/vol:%0.3fV, dac:%d\n", g_dev.cfg.valve_open_default, current, vol, dac_raw ); current_last = current; @@ -198,41 +163,12 @@ void user_valve_set_current( float current ) } } -#if 1 -void get_mac(uint8_t * mac ) -{ - mac[0] = 1; - mac[1] = 2; - mac[2] = 3; - mac[3] = 4; - mac[4] = 5; - mac[5] = 6; -} - -/** - * current[in]: 输入电流值 - * factor[in]: 焊接参数影响因子 - * macAddr[in]:网卡的MAC地址,用于校验license - * gas[out]: 需要输出的气体流量值 - */ -#define ResultCode int32_t -#define WeldMethod int32_t - -ResultCode caculate(WeldMethod method, uint32_t current, uint32_t factor, const char* macAddr, double* gas); +#include "weld_alg.h" -ResultCode caculate(WeldMethod method, uint32_t current, uint32_t factor, const char* macAddr, double* gas) -{ - //Todo: 等待客户提供算法 +#define VALVE_TEMP_CHECK 0 - //临时调试代码 - //current 0--200A, 线性换算成流量 0 -- 20 L/min - //factory 节气因子百分比 - *gas = current/200.0f * 20.0f * (100-factor)/100; - return 0; -} -#endif +extern float dbg_current; -#define VALVE_TEMP_CHECK 0 void user_valve_ctrl_task( void* unused ) { uint32_t last_tick; @@ -246,6 +182,40 @@ void user_valve_ctrl_task( void* unused ) // uint32_t tem_state_last = 0; // uint32_t now; + HAL_DAC_SetValue( &hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 0 ); + HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); + +#if 0 + while(1){ + osDelay(1000); + } + + while(0){ + double gas = 0; + ResultCode ret = 0; + volatile int32_t factor = 10; + volatile WeldMethod method=TIG; + volatile int32_t curr_A = 100; + + ret = caculate( method, curr_A, factor, &gas ); + printf( "ret:%d\n", ret ); + osDelay(1000); + } + + while(1){ + volatile uint32_t mv = 0; + volatile uint32_t dac_raw = 0; + + while(1){ + dac_raw = mv*0xfff/2500; //最大12bit=VERF_BUF 2.5V + HAL_DAC_SetValue( &hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dac_raw ); + HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); + osDelay( 500); + mv += 50; + } + } +#endif + vTaskDelay( 2000 ); pid_ctrl_init(); @@ -257,13 +227,16 @@ void user_valve_ctrl_task( void* unused ) while(1){ //500毫秒扫描一次, 考虑pid的流量计反馈,让扫描周期大于流量计的采集周期, - vTaskDelayUntil(&last_tick, 500 ); + vTaskDelayUntil(&last_tick, 200 ); // now = xTaskGetTickCount(); welding_current = g_dev.welding_current; if( welding_current < 0 ) welding_current = -welding_current; + if( dbg_current >= 0.0f ){ + welding_current = dbg_current; + } #if VALVE_TEMP_CHECK //电磁阀温度检查 @@ -306,14 +279,17 @@ void user_valve_ctrl_task( void* unused ) g_dev.valve_curr_out = 0; //比例电磁阀关闭, }else{ //检查焊接电流,判断是否开始焊接 - if( welding_current < 0.5f ){ + if( welding_current < 5.0f ){ //未开始焊接 - //按比例阀开度设置电流, 最小1.16v 最大1.73v, 按开度百分比计算电流 - //电流到流量 60--165mA -> 0-100 L/min - //电流到电压 60~165mA -> 1.16~1.73V + //按比例阀开度设置电流, 最小0.7v 最大1.0v, 按开度百分比计算电流 static float dbg_curr = 0; - g_dev.valve_curr_out = g_dev.cfg.valve_open_default*(0.165f-0.060f)/100 + 0.06f; + + if( g_dev.cfg.valve_open_default > 0 ) + g_dev.valve_curr_out = g_dev.cfg.valve_open_default*(0.16f-0.06f)/100 + 0.06f; + else + g_dev.valve_curr_out = 0; + if( dbg_curr - g_dev.valve_curr_out > 0.001f || dbg_curr - g_dev.valve_curr_out < -0.001f ){ printf( "open %d%%, curr:%0.3fA -> %0.3fA\n", g_dev.cfg.valve_open_default, dbg_curr, g_dev.valve_curr_out ); dbg_curr = g_dev.valve_curr_out; @@ -333,13 +309,11 @@ void user_valve_ctrl_task( void* unused ) welding_time += 500; //0.5秒扫描一次 } -//ResultCode caculate(WeldMethod method, uint32_t current, uint32_t factor, const char* macAddr, double* gas); - uint8_t mac[20]; - get_mac( mac ); double target_flowspeed; - caculate( g_dev.cfg.welding_method, welding_current, g_dev.cfg.save_gas_factor, (const char*)mac, &target_flowspeed ); - //控制流量输出, 按焊接电流和节气因子计算目标流量 - g_dev.target_flowspeed = target_flowspeed; + if( caculate( g_dev.cfg.welding_method, welding_current, g_dev.cfg.save_gas_factor, &target_flowspeed ) == 0 ){ + //控制流量输出, 按焊接电流和节气因子计算目标流量 + g_dev.target_flowspeed = target_flowspeed; + } //根据目标流量,和实际流量,使用pid调整比例阀 pid_ctrl_update(); diff --git a/welding_controller.ioc b/welding_controller.ioc index d6b6967..f436369 100644 --- a/welding_controller.ioc +++ b/welding_controller.ioc @@ -19,12 +19,16 @@ ADC2.ClockPrescalerADC3=ADC_CLOCK_ASYNC_DIV4 ADC2.ConversionDataManagement=ADC_CONVERSIONDATA_DMA_ONESHOT ADC2.EnableRegularConversion=ENABLE ADC2.ExternalTrigConv=ADC_EXTERNALTRIG_T2_TRGO -ADC2.IPParameters=ClockPrescaler,Resolution,ConversionDataManagement,EnableRegularConversion,Rank-4\#ChannelRegularConversion,Channel-4\#ChannelRegularConversion,SamplingTime-4\#ChannelRegularConversion,OffsetNumber-4\#ChannelRegularConversion,NbrOfConversionFlag,NbrOfConversion,ExternalTrigConv,ClockPrescalerADC3 +ADC2.IPParameters=ClockPrescaler,Resolution,ConversionDataManagement,EnableRegularConversion,Rank-4\#ChannelRegularConversion,Channel-4\#ChannelRegularConversion,SamplingTime-4\#ChannelRegularConversion,OffsetNumber-4\#ChannelRegularConversion,NbrOfConversionFlag,NbrOfConversion,ExternalTrigConv,ClockPrescalerADC3,OversamplingMode,RightBitShift,Ratio,OversamplingStopReset ADC2.NbrOfConversion=1 ADC2.NbrOfConversionFlag=1 ADC2.OffsetNumber-4\#ChannelRegularConversion=ADC_OFFSET_NONE +ADC2.OversamplingMode=ENABLE +ADC2.OversamplingStopReset=ADC_REGOVERSAMPLING_CONTINUED_MODE ADC2.Rank-4\#ChannelRegularConversion=1 +ADC2.Ratio=16 ADC2.Resolution=ADC_RESOLUTION_16B +ADC2.RightBitShift=ADC_RIGHTBITSHIFT_4 ADC2.SamplingTime-4\#ChannelRegularConversion=ADC_SAMPLETIME_1CYCLE_5 ADC3.Channel-3\#ChannelRegularConversion=ADC_CHANNEL_2 ADC3.ClockPrescaler=ADC_CLOCK_ASYNC_DIV4 @@ -496,8 +500,8 @@ SH.COMP_DAC12_group.0=DAC1_OUT2,DAC_OUT2 SH.COMP_DAC12_group.ConfNb=1 SH.S_TIM13_CH1.0=TIM13_CH1,PWM Generation1 CH1 SH.S_TIM13_CH1.ConfNb=1 -SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 -SPI1.CalculateBaudRate=32.0 MBits/s +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8 +SPI1.CalculateBaudRate=16.0 MBits/s SPI1.DataSize=SPI_DATASIZE_8BIT SPI1.Direction=SPI_DIRECTION_2LINES SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler,DataSize