增加一路比例阀驱动电流ADC采集
authorkyz <kongyuzhong@google.com>
Tue, 19 Aug 2025 14:01:41 +0000 (22:01 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 28 Aug 2025 01:03:39 +0000 (09:03 +0800)
Core/Src/adc.c
tools_v2/test.py
tools_v2/test.ui
user/user.h
user/user_adc.c
user/user_app.c

index 3d7d1461d61120ef2f18c1a212e61fbcc16c620d..8790e0e70660e0480ceac77cc644ed115cc5c02e 100644 (file)
@@ -39,7 +39,6 @@ void MX_ADC1_Init(void)
 
   /* USER CODE END ADC1_Init 0 */
 
-  ADC_MultiModeTypeDef multimode = {0};
   ADC_ChannelConfTypeDef sConfig = {0};
 
   /* USER CODE BEGIN ADC1_Init 1 */
@@ -51,13 +50,13 @@ void MX_ADC1_Init(void)
   hadc1.Instance = ADC1;
   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
   hadc1.Init.Resolution = ADC_RESOLUTION_16B;
-  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
+  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
   hadc1.Init.LowPowerAutoWait = DISABLE;
   hadc1.Init.ContinuousConvMode = DISABLE;
-  hadc1.Init.NbrOfConversion = 2;
+  hadc1.Init.NbrOfConversion = 1;
   hadc1.Init.DiscontinuousConvMode = DISABLE;
-  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO;
+  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO;
   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
   hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
@@ -72,19 +71,11 @@ void MX_ADC1_Init(void)
     Error_Handler();
   }
 
-  /** Configure the ADC multi-mode
-  */
-  multimode.Mode = ADC_MODE_INDEPENDENT;
-  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
-  {
-    Error_Handler();
-  }
-
   /** Configure Regular Channel
   */
-  sConfig.Channel = ADC_CHANNEL_5;
+  sConfig.Channel = ADC_CHANNEL_18;
   sConfig.Rank = ADC_REGULAR_RANK_1;
-  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
+  sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
   sConfig.SingleDiff = ADC_SINGLE_ENDED;
   sConfig.OffsetNumber = ADC_OFFSET_NONE;
   sConfig.Offset = 0;
@@ -94,14 +85,6 @@ void MX_ADC1_Init(void)
     Error_Handler();
   }
 
-  /** Configure Regular Channel
-  */
-  sConfig.Channel = ADC_CHANNEL_9;
-  sConfig.Rank = ADC_REGULAR_RANK_2;
-  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
-  {
-    Error_Handler();
-  }
   /* USER CODE BEGIN ADC1_Init 2 */
 
   /* USER CODE END ADC1_Init 2 */
@@ -115,6 +98,7 @@ void MX_ADC2_Init(void)
 
   /* USER CODE END ADC2_Init 0 */
 
+  ADC_MultiModeTypeDef multimode = {0};
   ADC_ChannelConfTypeDef sConfig = {0};
 
   /* USER CODE BEGIN ADC2_Init 1 */
@@ -126,13 +110,13 @@ void MX_ADC2_Init(void)
   hadc2.Instance = ADC2;
   hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
   hadc2.Init.Resolution = ADC_RESOLUTION_16B;
-  hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
+  hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
   hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
   hadc2.Init.LowPowerAutoWait = DISABLE;
   hadc2.Init.ContinuousConvMode = DISABLE;
-  hadc2.Init.NbrOfConversion = 1;
+  hadc2.Init.NbrOfConversion = 3;
   hadc2.Init.DiscontinuousConvMode = DISABLE;
-  hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO;
+  hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO;
   hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
   hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT;
   hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
@@ -147,9 +131,18 @@ void MX_ADC2_Init(void)
     Error_Handler();
   }
 
+
+  /** Configure the ADC multi-mode
+  */
+  multimode.Mode = ADC_MODE_INDEPENDENT;
+  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
+  {
+    Error_Handler();
+  }
+
   /** Configure Regular Channel
   */
-  sConfig.Channel = ADC_CHANNEL_18;
+  sConfig.Channel = ADC_CHANNEL_5;
   sConfig.Rank = ADC_REGULAR_RANK_1;
   sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
   sConfig.SingleDiff = ADC_SINGLE_ENDED;
@@ -160,6 +153,24 @@ void MX_ADC2_Init(void)
   {
     Error_Handler();
   }
+
+  /** Configure Regular Channel
+  */
+  sConfig.Channel = ADC_CHANNEL_9;
+  sConfig.Rank = ADC_REGULAR_RANK_2;
+  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
+
+  /** Configure Regular Channel
+  */
+  sConfig.Channel = ADC_CHANNEL_2;
+  sConfig.Rank = ADC_REGULAR_RANK_3;
+  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
   /* USER CODE BEGIN ADC2_Init 2 */
 
   /* USER CODE END ADC2_Init 2 */
@@ -256,15 +267,14 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
       __HAL_RCC_ADC12_CLK_ENABLE();
     }
 
-    __HAL_RCC_GPIOB_CLK_ENABLE();
+    __HAL_RCC_GPIOA_CLK_ENABLE();
     /**ADC1 GPIO Configuration
-    PB0     ------> ADC1_INP9
-    PB1     ------> ADC1_INP5
+    PA4     ------> ADC1_INP18
     */
-    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
+    GPIO_InitStruct.Pin = GPIO_PIN_4;
     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
     /* ADC1 DMA Init */
     /* ADC1 Init */
@@ -299,17 +309,17 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
     if(HAL_RCC_ADC12_CLK_ENABLED==1){
       __HAL_RCC_ADC12_CLK_ENABLE();
     }
-
-    __HAL_RCC_GPIOA_CLK_ENABLE();
+    __HAL_RCC_GPIOB_CLK_ENABLE();
     __HAL_RCC_GPIOF_CLK_ENABLE();
     /**ADC2 GPIO Configuration
-    PA4     ------> ADC2_INP18
+    PB0     ------> ADC2_INP9
+    PB1     ------> ADC2_INP5
     PF13     ------> ADC2_INP2
     */
-    GPIO_InitStruct.Pin = GPIO_PIN_4;
+    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
     GPIO_InitStruct.Pin = GPIO_PIN_13;
     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
@@ -403,10 +413,9 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
     }
 
     /**ADC1 GPIO Configuration
-    PB0     ------> ADC1_INP9
-    PB1     ------> ADC1_INP5
+    PA4     ------> ADC2_INP18
     */
-    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
+    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4);
 
     /* ADC1 DMA DeInit */
     HAL_DMA_DeInit(adcHandle->DMA_Handle);
@@ -426,11 +435,12 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
     }
 
     /**ADC2 GPIO Configuration
-    PA4     ------> ADC2_INP18
+    PB0     ------> ADC1_INP9
+    PB1     ------> ADC1_INP5
     PF13     ------> ADC2_INP2
-    */
-    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4);
 
+    */
+    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
     HAL_GPIO_DeInit(GPIOF, GPIO_PIN_13);
 
     /* ADC2 DMA DeInit */
index 35cc6ba18ec31077503300ce49275bc02843aabc..4309b78feb6ce5b01b65f11736e6d595cf7658fc 100644 (file)
@@ -689,6 +689,7 @@ class App( QDialog, ui_welding.Ui_Dialog ):
             self.tableWidget_devinfo.item(0,6).setText( "%d"%(method))
             self.tableWidget_devinfo.item(0,7).setText( "%d"%(factor))
             #self.tableWidget_devinfo.item(0,8).setText( "%0.1f"%(tem_x10/10.0))
+            self.tableWidget_devinfo.item(0,9).setText( "%d"%(res))
             if idx == 0:
                     self.flow_x=[]
                     self.flow_y=[]
index 93e370227525baa77d0a378e6783e0c69c627ee2..61435f59f97cea6fc8bc3f78c575d661d136231a 100644 (file)
       </column>
       <column>
        <property name="text">
-        <string>电流校准</string>
+        <string>读取控制电流</string>
        </property>
       </column>
       <column>
index fbe5b4018eb12956cb359affe09410d1f6adacb7..b08da45e710370218891a7749a281b12c814eec2 100644 (file)
@@ -237,6 +237,7 @@ typedef struct {
        //输出控制
        float target_flowspeed; //目标流量
        float valve_curr_out;  //当前输出电流,对应dac输出 1.16~1.73V --- 60~165mA --- 0L/min <60mA
+       float valve_curr_read; //当前通过ADC读取到的比例实际阀控制电流
        //状态
        int32_t state; //设备状态  <非0:故障码, 0:正常
 
index e7835fedea6165776268279b354f53b8387ef96e..b8616261222f1df52020ed680176b3d6745f6a44 100644 (file)
@@ -7,26 +7,26 @@ void user_adc_task( void* unused );
 //ADC2 INP18 电流,           100微秒采集一次,500毫秒触发DMA中断,计算电流。
 
 
-#define ADC1_RAW_NRS (10*2)
-#define ADC2_RAW_NRS 5000
+#define ADC1_RAW_NRS 5000
+#define ADC2_RAW_NRS (10*3)
 #define ADC3_RAW_NRS (100*2)
 //DMA采集buffer 地址32字节对齐,大小为32字节倍数,以适配cache刷新操作
-ALIGN_32BYTES (static uint16_t adc1_raw[(ADC1_RAW_NRS+15)&~15] ); //2路采集10次, 0.1秒
-ALIGN_32BYTES (static uint16_t adc2_raw[(ADC2_RAW_NRS+15)&~15] ); //采集5000次,电流采样率10Khz 0.5秒
+ALIGN_32BYTES (static uint16_t adc2_raw[(ADC2_RAW_NRS+15)&~15] ); //3路采集10次, 0.1秒
+ALIGN_32BYTES (static uint16_t adc1_raw[(ADC1_RAW_NRS+15)&~15] ); //采集5000次,电流采样率10Khz 0.5秒
 ALIGN_32BYTES (static uint16_t adc3_raw[(ADC3_RAW_NRS+15)&~15] ); //采集100次  1秒
 
 #define SECTION_DMA_RAM 
 #define  ARRAY_SIZE( arr )   ( sizeof( arr )/sizeof( arr[0] ) ) 
 
-static uint16_t adc1_raw_copy[ADC1_RAW_NRS];
+static uint16_t adc2_raw_copy[ADC2_RAW_NRS];
 //static uint16_t adc3_raw_copy[ADC3_RAW_NRS];
 
 //__attribute__((at(0x20005000))) static uint16_t      adc2_raw_copy[ADC2_RAW_NRS];
 //__attribute__((at(0x20000000))) static float adc2_curr[ADC2_RAW_NRS];
-__attribute__((section(".DTCM"),zero_init))  static uint16_t   adc2_raw_copy[ADC2_RAW_NRS];
-__attribute__((section(".DTCM"),zero_init)) static float adc2_curr[ADC2_RAW_NRS];
+__attribute__((section(".DTCM"),zero_init))  static uint16_t   adc1_raw_copy[ADC1_RAW_NRS];
+__attribute__((section(".DTCM"),zero_init)) static float adc1_curr[ADC1_RAW_NRS];
 __attribute__((section(".DTCM"),zero_init))  static uint16_t   adc3_raw_copy[ADC3_RAW_NRS];
-__attribute__((section(".DTCM"),zero_init)) static float adc3_curr[ADC3_RAW_NRS];
+//__attribute__((section(".DTCM"),zero_init)) static float adc3_curr[ADC3_RAW_NRS];
 extern dev_info_t g_dev;
 
 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
@@ -301,6 +301,7 @@ void user_adc_task( void* unused )
     uint32_t flowrate_sum = 0;
        uint32_t temp_sum = 0;
        uint32_t vol_sum = 0;
+       uint32_t valve_curr_sum = 0;
 
     EventBits_t uxBits;
 
@@ -329,33 +330,37 @@ void user_adc_task( void* unused )
                 pdFALSE,       /* Don't wait for all bits, either bit will do. */
                 xTicksToWait );/* Wait a maximum of xxx ms for either bit to be set. */
 
-         if( uxBits & EVENT_ADC1_UPDATE ){
-            // ADC1 压力计 和 流量计
+         if( uxBits & EVENT_ADC2_UPDATE ){
+            // ADC2 压力计 和 流量计 和 比例阀驱动电流
              volatile static uint32_t t0, t1;
              t1 = HAL_GetTick() - t0;
              t0 = HAL_GetTick();
              
-            if( HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ADC1_RAW_NRS ) != HAL_OK){
+            if( HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ADC2_RAW_NRS ) != HAL_OK){
                 DBG_PRINTF( "start DMA fail\r\n" );
             }
 
             pressure_sum = 0;
             flowrate_sum = 0;
+            valve_curr_sum = 0;
 
-            for( int i=0; i<ADC1_RAW_NRS/2; i++ ){
-                pressure_sum += adc1_raw_copy[ i*2 ];
-                flowrate_sum += adc1_raw_copy[ i*2+1 ];
+            for( int i=0; i<ADC2_RAW_NRS/3; i++ ){
+                pressure_sum += adc2_raw_copy[ i*3 ];
+                flowrate_sum += adc2_raw_copy[ i*3+1 ];
+                valve_curr_sum += adc2_raw_copy[ i*3+2 ];
             }
             
-            raw = pressure_sum/(ADC1_RAW_NRS/2);
+            raw = pressure_sum/(ADC2_RAW_NRS/3);
             voltage = raw*VREF/0xffff; //16bit adc1
             voltage *=2; //压力计输出电压 分压 1/2
             
             g_dev.pressure = vol_to_kpa( voltage );
 
-            g_dev.flowmeter.ad_vol = flowrate_sum/(ADC1_RAW_NRS/2) *VREF/0xffff *2;
+            g_dev.flowmeter.ad_vol = flowrate_sum/(ADC2_RAW_NRS/3) *VREF/0xffff *2;
             g_dev.flowmeter.flow_speed_raw_ad = vol_to_speed( g_dev.flowmeter.ad_vol );
 
+                       //TODO: calc valve ctrl curr
+
             if( voltage < 0.2f ){
                 //传感器输出0.2V到4.7v,未通气时按一个大气压101KPa大约0.2 + 101*0.0064 = 0.85V
                 if( pressure_err > 10 ){
@@ -370,10 +375,13 @@ void user_adc_task( void* unused )
                     g_dev.state &= ~PRESSURE_SENSOR_ERR;
                 }
             }
+
+                       //计算比例阀驱动的实际电流,AD采集R35(4.7Ω)上的电压,将电压除以4.7就得到电流
+                       g_dev.valve_curr_read = (valve_curr_sum/(ADC2_RAW_NRS/3) *VREF/0xffff )/4.7;
          }
-         if( uxBits & EVENT_ADC2_UPDATE ){
-            //电流互感器 ADC2
-            HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ADC2_RAW_NRS );
+         if( uxBits & EVENT_ADC1_UPDATE ){
+            //电流互感器 ADC1
+            HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ADC1_RAW_NRS );
 #if 0
             if( g_dev.hw_ver == HW_V2 ){
                 //HW_V2 互感器输出 400A -- 2.5V +/-0.625V
@@ -389,17 +397,17 @@ void user_adc_task( void* unused )
 
              }
 #else
-            for( int32_t i=0; i< ADC2_RAW_NRS; i++ )
-                adc2_curr[i] = vol_to_curr( adc2_raw_copy[i] * VREF*2/0xffff );
+            for( int32_t i=0; i< ADC1_RAW_NRS; i++ )
+                adc1_curr[i] = vol_to_curr( adc1_raw_copy[i] * VREF*2/0xffff );
 #endif
 
             if( g_dev.debug_report == 2 ){   //发送调试数据到pc,
                 extern uint32_t dbg_report_raw_current;
                 extern uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len);
-                static  uint8_t dbg_curr_raw[ADC2_RAW_NRS*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, };
+                static  uint8_t dbg_curr_raw[ADC1_RAW_NRS*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, };
                 
-                    for( int i=0; i < ADC2_RAW_NRS; i++ ){
-                         int16_t curr = adc2_curr[i];
+                    for( int i=0; i < ADC1_RAW_NRS; i++ ){
+                         int16_t curr = adc1_curr[i];
                          dbg_curr_raw[6+i*2+1] = curr&0xff; 
                          dbg_curr_raw[6+i*2] = (curr>>8)&0xff;
                     }
@@ -415,7 +423,7 @@ void user_adc_task( void* unused )
             }else{
 //                static uint32_t t10, t11;
 //                t10 = HAL_GetTick();
-                g_dev.welding_current = calc_welding_current( adc2_curr, ADC2_RAW_NRS);
+                g_dev.welding_current = calc_welding_current( adc1_curr, ADC1_RAW_NRS);
 //                t11 = HAL_GetTick() - t10;
             }
 
index e403cb95a03edda8539e00b142a865e56d19dc9b..99d7fef07a5f192ecdf7336466d4252998d1aff4 100644 (file)
@@ -608,6 +608,8 @@ void welding_data_store( uint32_t index )
     item.whelding_method = g_dev.cfg.welding_method;
     item.reserved = 0;
 
+       item.reserved = (uint16_t)(g_dev.valve_curr_read*1000);
+
 #if 0
     if( g_dev.welding_start ){
         if( index==0 ){