mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
update the way that date send to uart. Add Flow route selection Algorithm
This commit is contained in:
@@ -15,11 +15,16 @@ extern "C" {
|
|||||||
#define EN2_PORT GPIOA
|
#define EN2_PORT GPIOA
|
||||||
#define EN2_PIN GPIO_PIN_15
|
#define EN2_PIN GPIO_PIN_15
|
||||||
|
|
||||||
//
|
// Flow route selection
|
||||||
#define HIGH_CUR 2
|
#define HIGH_CUR 2
|
||||||
#define MID_CUR 1
|
#define MID_CUR 1
|
||||||
#define LOW_CUR 0
|
#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 Gate_Port_Init(void);
|
||||||
void flow_route_selection(uint8_t selection);
|
void flow_route_selection(uint8_t selection);
|
||||||
uint8_t Gate_get_status(void);
|
uint8_t Gate_get_status(void);
|
||||||
|
|||||||
@@ -30,12 +30,12 @@
|
|||||||
/* protocol frame
|
/* protocol frame
|
||||||
+ 0x55 0xAA header
|
+ 0x55 0xAA header
|
||||||
+ 1byte direct & unit, 0x0? for positive, 0x1? for negative,
|
+ 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)
|
0x?0 for uA (0-500.00uA), 0x?1 for uA (0.50-500.00mA), 0x?2 for mA (0.50-5.00A)
|
||||||
+ 2byte current (100 times)
|
+ 4byte current (100 times)
|
||||||
+ 2byte voltage (100 times)
|
+ 2byte voltage (100 times)
|
||||||
+ 0xAA 0x55 footer
|
+ 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 -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,71 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "gate.h"
|
#include "gate.h"
|
||||||
|
#include "usart.h"
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
|
|
||||||
/* Private define ------------------------------------------------------------*/
|
/* 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 variables ---------------------------------------------------------*/
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* 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
|
* @brief task for send messages or data
|
||||||
@@ -32,25 +89,37 @@ void UartSendTask(void *argument)
|
|||||||
for(int i = 0 ; i < ADC_TIMES ; i++)
|
for(int i = 0 ; i < ADC_TIMES ; i++)
|
||||||
{
|
{
|
||||||
// direct & unit
|
// direct & unit
|
||||||
uint8_t dir_unit = 0x00; // positive, uA
|
uint8_t dir_unit = 0x00; // 0x00 means: positive, (A) unit
|
||||||
uint8_t unit = Gate_get_status(); // 0 - low current, 1 - mid current, 2 - high current
|
uint8_t unit = Gate_get_status(); // 0 - low current(500uA), 1 - mid current(50mA), 2 - high current(5A)
|
||||||
dir_unit |= (unit & 0x0F); // unit
|
dir_unit = (unit & 0x0F); // unit
|
||||||
// current
|
// current
|
||||||
uint32_t cur;
|
uint32_t cur;
|
||||||
// voltage *100times
|
uint64_t cur_adc; // 64位避免溢出
|
||||||
uint32_t volt = adc_buf[i][3] * 3 * 100 / 4096; // 12bit ADC, 0-4095
|
// 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
|
// cur direction and value
|
||||||
if(adc_buf[i][2-unit] > cur_bias) {
|
if(adc_buf[i][2-unit] > cur_bias) {
|
||||||
dir_unit |= 0x10; // negative
|
dir_unit |= 0x10; // negative
|
||||||
// cur *100times
|
cur_adc = adc_buf[i][2-unit] - cur_bias;
|
||||||
cur = (adc_buf[i][2-unit] - cur_bias) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// cur *100times
|
cur_adc = cur_bias - adc_buf[i][2-unit];
|
||||||
cur = (cur_bias - adc_buf[i][2-unit]) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC
|
|
||||||
}
|
}
|
||||||
|
// 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
|
// 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));
|
UART6_TX_Send(protocol_frame, sizeof(protocol_frame));
|
||||||
}
|
}
|
||||||
osDelay(1);
|
osDelay(1);
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ const osThreadAttr_t HardwareInitTask_attributes = {
|
|||||||
osThreadId_t UartSendTaskHandle;
|
osThreadId_t UartSendTaskHandle;
|
||||||
const osThreadAttr_t UartSendTask_attributes = {
|
const osThreadAttr_t UartSendTask_attributes = {
|
||||||
.name = "UartSendTask",
|
.name = "UartSendTask",
|
||||||
.stack_size = 128 * 1,
|
.stack_size = 128 * 2,
|
||||||
.priority = (osPriority_t) osPriorityHigh1,
|
.priority = (osPriority_t) osPriorityHigh1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user