diff --git a/Power_Pico/BSP/GATE/gate.h b/Power_Pico/BSP/GATE/gate.h index 42f545d..0e49b85 100644 --- a/Power_Pico/BSP/GATE/gate.h +++ b/Power_Pico/BSP/GATE/gate.h @@ -15,11 +15,16 @@ extern "C" { #define EN2_PORT GPIOA #define EN2_PIN GPIO_PIN_15 -// +// Flow route selection #define HIGH_CUR 2 #define MID_CUR 1 #define LOW_CUR 0 +// Resistance values in ohms +#define HIGH_CUR_RES 0.005 // 5 m ohm +#define MID_CUR_RES 0.5 // 500 m ohm +#define LOW_CUR_RES 50.0 // 50 ohm + void Gate_Port_Init(void); void flow_route_selection(uint8_t selection); uint8_t Gate_get_status(void); diff --git a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c index 1c4c2ec..09df813 100644 --- a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c +++ b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c @@ -30,12 +30,12 @@ /* protocol frame + 0x55 0xAA header + 1byte direct & unit, 0x0? for positive, 0x1? for negative, - 0x?0 for uA (0-500.00uA), 0x?1 for mA (0.50-500.00mA), 0x?2 for A (0.50-5.00A) -+ 2byte current (100 times) + 0x?0 for uA (0-500.00uA), 0x?1 for uA (0.50-500.00mA), 0x?2 for mA (0.50-5.00A) ++ 4byte current (100 times) + 2byte voltage (100 times) + 0xAA 0x55 footer */ -uint8_t protocol_frame[] = {0x55, 0xAA, 0x02, 0x01, 0x23, 0x01, 0xf4, 0xAA, 0x55}; +uint8_t protocol_frame[] = {0x55, 0xAA, 0x02, 0x00, 0x00, 0x01, 0x23, 0x01, 0xf4, 0xAA, 0x55}; /* Private function prototypes -----------------------------------------------*/ diff --git a/Power_Pico/User/Tasks/Src/user_SendTask.c b/Power_Pico/User/Tasks/Src/user_SendTask.c index 2f288fa..9fa688c 100644 --- a/Power_Pico/User/Tasks/Src/user_SendTask.c +++ b/Power_Pico/User/Tasks/Src/user_SendTask.c @@ -4,14 +4,71 @@ #include "main.h" #include "adc.h" #include "gate.h" +#include "usart.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ +// 定义档位阈值 +#define LOW_TO_MID_THRESHOLD 55000 // 550uA +#define MID_TO_LOW_THRESHOLD 45000 // 450uA (滞回) +#define MID_TO_HIGH_THRESHOLD 5000000 // 50mA +#define HIGH_TO_MID_THRESHOLD 4500 // 45mA (滞回) + +// 定义连续超过阈值的次数 +#define SWITCH_COUNT_THRESHOLD 2 + +// 切换计数器 +uint8_t low_to_mid_count = 0; +uint8_t mid_to_low_count = 0; +uint8_t mid_to_high_count = 0; +uint8_t high_to_mid_count = 0; + /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ +// 检查当前电流并根据阈值切换流路 +void check_and_switch_flow_route(uint8_t unit, uint32_t cur) { + if (unit == LOW_CUR) { + if (cur > LOW_TO_MID_THRESHOLD) { + low_to_mid_count++; + if (low_to_mid_count >= SWITCH_COUNT_THRESHOLD) { + flow_route_selection(MID_CUR); // 切换到中档位 + low_to_mid_count = 0; // 重置计数器 + } + } else { + low_to_mid_count = 0; // 清零计数器 + } + } else if (unit == MID_CUR) { + if (cur > MID_TO_HIGH_THRESHOLD) { + mid_to_high_count++; + if (mid_to_high_count >= SWITCH_COUNT_THRESHOLD) { + flow_route_selection(HIGH_CUR); // 切换到高档位 + mid_to_high_count = 0; // 重置计数器 + } + } else if (cur < MID_TO_LOW_THRESHOLD) { + mid_to_low_count++; + if (mid_to_low_count >= SWITCH_COUNT_THRESHOLD) { + flow_route_selection(LOW_CUR); // 切换到低档位 + mid_to_low_count = 0; // 重置计数器 + } + } else { + mid_to_high_count = 0; // 清零计数器 + mid_to_low_count = 0; // 清零计数器 + } + } else if (unit == HIGH_CUR) { + if (cur < HIGH_TO_MID_THRESHOLD) { + high_to_mid_count++; + if (high_to_mid_count >= SWITCH_COUNT_THRESHOLD) { + flow_route_selection(MID_CUR); // 切换到中档位 + high_to_mid_count = 0; // 重置计数器 + } + } else { + high_to_mid_count = 0; // 清零计数器 + } + } +} /** * @brief task for send messages or data @@ -32,25 +89,37 @@ void UartSendTask(void *argument) for(int i = 0 ; i < ADC_TIMES ; i++) { // direct & unit - uint8_t dir_unit = 0x00; // positive, uA - uint8_t unit = Gate_get_status(); // 0 - low current, 1 - mid current, 2 - high current - dir_unit |= (unit & 0x0F); // unit + uint8_t dir_unit = 0x00; // 0x00 means: positive, (A) unit + uint8_t unit = Gate_get_status(); // 0 - low current(500uA), 1 - mid current(50mA), 2 - high current(5A) + dir_unit = (unit & 0x0F); // unit // current uint32_t cur; - // voltage *100times - uint32_t volt = adc_buf[i][3] * 3 * 100 / 4096; // 12bit ADC, 0-4095 + uint64_t cur_adc; // 64位避免溢出 + // voltage = VADC/4096 * 3.0Vref * 11( this means: 1MR + 100kR) + uint16_t volt = adc_buf[i][3] * 3 * 11 * 100 / 4096; // 100times // cur direction and value if(adc_buf[i][2-unit] > cur_bias) { dir_unit |= 0x10; // negative - // cur *100times - cur = (adc_buf[i][2-unit] - cur_bias) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC + cur_adc = adc_buf[i][2-unit] - cur_bias; } else { - // cur *100times - cur = (cur_bias - adc_buf[i][2-unit]) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC + cur_adc = cur_bias - adc_buf[i][2-unit]; } + // cur = (IADC/4096 * 3.0Vref - 1.5Vref) / 50(ina199) / R + if(unit == HIGH_CUR) { + cur = cur_adc * 3 * 100 * 1000 / HIGH_CUR_RES / 50 / 4096; // 100times. (mA) + } + else if(unit == MID_CUR) { + cur = cur_adc * 3 * 100 * 1000000 / MID_CUR_RES / 50 / 4096; // 100times. (uA) + } + else if(unit == LOW_CUR) { + cur = cur_adc * 3 * 100 * 1000000 / LOW_CUR_RES / 50 / 4096; // 100times. (uA) + } + + check_and_switch_flow_route(unit, cur); // 检查并切换流路 + // protocol frame - uint8_t protocol_frame[9] = {0x55, 0xAA, dir_unit, (cur<<8 & 0xff), (cur & 0xff), (volt<<8 & 0xff), (volt & 0xff), 0xAA, 0x55}; + uint8_t protocol_frame[11] = {0x55, 0xAA, dir_unit, (cur>>24 & 0xff), (cur>>16 & 0xff), (cur>>8 & 0xff), (cur & 0xff), (volt>>8 & 0xff), (volt & 0xff), 0xAA, 0x55}; UART6_TX_Send(protocol_frame, sizeof(protocol_frame)); } osDelay(1); diff --git a/Power_Pico/User/Tasks/Src/user_TasksInit.c b/Power_Pico/User/Tasks/Src/user_TasksInit.c index f05e970..4b62998 100644 --- a/Power_Pico/User/Tasks/Src/user_TasksInit.c +++ b/Power_Pico/User/Tasks/Src/user_TasksInit.c @@ -39,7 +39,7 @@ const osThreadAttr_t HardwareInitTask_attributes = { osThreadId_t UartSendTaskHandle; const osThreadAttr_t UartSendTask_attributes = { .name = "UartSendTask", - .stack_size = 128 * 1, + .stack_size = 128 * 2, .priority = (osPriority_t) osPriorityHigh1, };