更改电流切换逻辑

This commit is contained in:
不吃油炸鸡
2025-10-18 11:25:33 +08:00
parent 3c6e3539ef
commit 5e4040aad4
4 changed files with 65 additions and 62 deletions

View File

@@ -4,10 +4,8 @@
#include "math.h" #include "math.h"
// 量程阈值 // 量程阈值
#define ADC_TRUST_MAX 1952 #define THRESH_HIGH 4000-2048 // 对应550uA 50mA
#define ADC_TRUST_MIN 10 #define THRESH_LOW 15 // 对应大概450uA 45mA
#define THRESH_HIGH 1800
#define THRESH_LOW 18
// 次数 // 次数
#define THRESH_TIMES 8 #define THRESH_TIMES 8
@@ -79,7 +77,9 @@ void Gate_Swich_and_UART_Send(ADC_Packet adc_packet)
{ {
static uint8_t high_times = 0; static uint8_t high_times = 0;
static uint8_t low_times = 0; static uint8_t low_times = 0;
// low mid high // adc码值
uint16_t ref_adc = adc_packet.data[0][4];
uint16_t vol_adc = adc_packet.data[0][0];
uint16_t cur_adc = 0; uint16_t cur_adc = 0;
switch(Gate_get_status()) { switch(Gate_get_status()) {
case LOW_CUR: case LOW_CUR:
@@ -92,28 +92,28 @@ void Gate_Swich_and_UART_Send(ADC_Packet adc_packet)
cur_adc = adc_packet.data[0][3]; cur_adc = adc_packet.data[0][3];
break; break;
} }
// 可信范围内 // 计算电压电流值
float voltage; float voltage;
float current; float current;
uint16_t diff = abs((int)cur_adc - 2048); uint16_t diff = abs((int)cur_adc - ref_adc);
if(diff < ADC_TRUST_MAX && diff > ADC_TRUST_MIN) {
adc_packet.header[3] = Gate_get_status(); adc_packet.header[3] = Gate_get_status();
ADC_Packet adc_packet_trans = adc_packet; ADC_Packet adc_packet_trans = adc_packet;
HAL_UART_Transmit_DMA(&huart6, (uint8_t*)&adc_packet_trans, sizeof(adc_packet_trans)); HAL_UART_Transmit_DMA(&huart6, (uint8_t*)&adc_packet_trans, sizeof(adc_packet_trans));
// 计算电压电流值 // 计算电压电流值
voltage = adc_packet.data[0][0] * 3.0f / 4096.0f * 11.0f; // 11 = (100+10)/10 voltage = vol_adc * 3.0f / 4096.0f * 11.0f; // 11 = (100+10)/10
// 计算电流值 // 计算电流值
if(Gate_get_status() == LOW_CUR) { if(Gate_get_status() == LOW_CUR) {
current = adc_packet.data[0][1]; current = adc_packet.data[0][1];
current = (current - 2048.0) * SCALE_LOW; current = (current - ref_adc) * SCALE_LOW;
} }
else if(Gate_get_status() == MID_CUR) { else if(Gate_get_status() == MID_CUR) {
current = adc_packet.data[0][2]; current = adc_packet.data[0][2];
current = (current - 2048.0) * SCALE_MID; current = (current - ref_adc) * SCALE_MID;
} }
else if(Gate_get_status() == HIGH_CUR) { else if(Gate_get_status() == HIGH_CUR) {
current = adc_packet.data[0][3]; current = adc_packet.data[0][3];
current = (current - 2048.0) * SCALE_HIGH; current = (current - ref_adc) * SCALE_HIGH;
} }
// 入队列 // 入队列
queue_push(global_voltage_queue, voltage); queue_push(global_voltage_queue, voltage);
@@ -141,11 +141,3 @@ void Gate_Swich_and_UART_Send(ADC_Packet adc_packet)
low_times = 0; low_times = 0;
} }
} }
// 不可信范围内
else {
if(diff >= ADC_TRUST_MAX) // 过大,切换到更大档位
flow_route_selection(Gate_get_status()+1);
else if(diff <= ADC_TRUST_MIN) // 过小,切换到更小档位
flow_route_selection(Gate_get_status()-1);
}
}

View File

@@ -31,7 +31,7 @@ extern "C" {
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#define ADC_TIMES 1 #define ADC_TIMES 1
#define ADC_CHANELS 4 #define ADC_CHANELS 5
typedef struct { typedef struct {
uint8_t header[4]; //2的倍数 uint8_t header[4]; //2的倍数
@@ -39,7 +39,7 @@ typedef struct {
// 0x0A adc times, 0x05 adc channels (so data lenth is times * channels * 2 bytes) // 0x0A adc times, 0x05 adc channels (so data lenth is times * channels * 2 bytes)
// 0x?0 for LOW, 0x?1 for MID, 0x?2 for HIGH. // 0x?0 for LOW, 0x?1 for MID, 0x?2 for HIGH.
uint16_t data[ADC_TIMES][ADC_CHANELS]; uint16_t data[ADC_TIMES][ADC_CHANELS];
} __attribute__((packed)) ADC_Packet; } ADC_Packet;
extern ADC_Packet adc_packet; extern ADC_Packet adc_packet;

View File

@@ -54,7 +54,7 @@ void MX_ADC1_Init(void)
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 4; hadc1.Init.NbrOfConversion = 5;
hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK) if (HAL_ADC_Init(&hadc1) != HAL_OK)
@@ -98,6 +98,15 @@ void MX_ADC1_Init(void)
{ {
Error_Handler(); Error_Handler();
} }
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = 5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE BEGIN ADC1_Init 2 */
__HAL_DMA_ENABLE_IT(&hdma_adc1, DMA_IT_TC); /*开启DMA传输完成中断*/ __HAL_DMA_ENABLE_IT(&hdma_adc1, DMA_IT_TC); /*开启DMA传输完成中断*/
/* USER CODE END ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */
@@ -123,13 +132,14 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
PA6 ------> ADC1_IN6 PA6 ------> ADC1_IN6
PA7 ------> ADC1_IN7 PA7 ------> ADC1_IN7
PB0 ------> ADC1_IN8 PB0 ------> ADC1_IN8
PB1 ------> ADC1_IN9
*/ */
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
@@ -175,10 +185,11 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
PA6 ------> ADC1_IN6 PA6 ------> ADC1_IN6
PA7 ------> ADC1_IN7 PA7 ------> ADC1_IN7
PB0 ------> ADC1_IN8 PB0 ------> ADC1_IN8
PB1 ------> ADC1_IN9
*/ */
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0); HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
/* ADC1 DMA DeInit */ /* ADC1 DMA DeInit */
HAL_DMA_DeInit(adcHandle->DMA_Handle); HAL_DMA_DeInit(adcHandle->DMA_Handle);

View File

@@ -52,9 +52,9 @@ void HardwareInitTask(void *argument)
// HAL_UART_Transmit_DMA(&huart6,(uint8_t *)"power-pico\r\n",13); // HAL_UART_Transmit_DMA(&huart6,(uint8_t *)"power-pico\r\n",13);
// queue for voltage and current // queue for voltage and current
global_voltage_queue = queue_create(8); global_voltage_queue = queue_create(32);
global_current_queue = queue_create(8); global_current_queue = queue_create(32);
for(int i = 0; i < 8; i++) { for(int i = 0; i < 32; i++) {
queue_push(global_voltage_queue, 0.0); queue_push(global_voltage_queue, 0.0);
queue_push(global_current_queue, 0.0); queue_push(global_current_queue, 0.0);
} }