This commit is contained in:
2026-03-10 03:05:49 +08:00
committed by guoxy06
parent 351dfc6b71
commit a541614346
4 changed files with 114 additions and 58 deletions

View File

@@ -170,11 +170,14 @@ void ChassisInit()
SuperCap_Init_Config_s cap_conf = { SuperCap_Init_Config_s cap_conf = {
.can_config = { .can_config = {
.can_handle = &hcan2, .can_handle = &hcan2, // 保持你原有的硬件CAN接口不变
.tx_id = 0x302, // 超级电容默认接收id .tx_id = 0x001, // C板发给超级电容的ID
.rx_id = 0x301, // 超级电容默认发送id,注意tx和rx在其他人看来是反的 .rx_id = 0x100, // 超级电容发给C板的ID
}}; }};
cap = SuperCapInit(&cap_conf); // 超级电容初始化 cap = SuperCapInit(&cap_conf); // 超级电容初始化 todo 控制
// (新增)设置超级电容功率补偿,例如补偿5W,可根据实际需求调整
SuperCapSetPowerOffset(cap, 5);
// 发布订阅初始化,如果为双板,则需要can comm来传递消息 // 发布订阅初始化,如果为双板,则需要can comm来传递消息
#ifdef CHASSIS_BOARD #ifdef CHASSIS_BOARD

View File

@@ -1,15 +1,12 @@
/* /*
* @Descripttion: * @Descripttion: 适配群星战队协议的超级电容模块
* @version: * @version: V2.0
* @Author: Chenfu
* @Date: 2022-12-02 21:32:47
* @LastEditTime: 2022-12-05 15:29:49
*/ */
#include "super_cap.h" #include "super_cap.h"
#include "memory.h" #include "memory.h"
#include "stdlib.h" #include "stdlib.h"
static SuperCapInstance *super_cap_instance = NULL; // 可以由app保存此指针 static SuperCapInstance *super_cap_instance = NULL;
static void SuperCapRxCallback(CANInstance *_instance) static void SuperCapRxCallback(CANInstance *_instance)
{ {
@@ -17,9 +14,14 @@ static void SuperCapRxCallback(CANInstance *_instance)
SuperCap_Msg_s *Msg; SuperCap_Msg_s *Msg;
rxbuff = _instance->rx_buff; rxbuff = _instance->rx_buff;
Msg = &super_cap_instance->cap_msg; Msg = &super_cap_instance->cap_msg;
Msg->vol = (uint16_t)(rxbuff[0] << 8 | rxbuff[1]);
Msg->current = (uint16_t)(rxbuff[2] << 8 | rxbuff[3]); // 按照新协议解析接收数据
Msg->power = (uint16_t)(rxbuff[4] << 8 | rxbuff[5]); Msg->ready = (SuperCapReadyEnum)rxbuff[0];
Msg->state = (SuperCapStateEnum)rxbuff[1];
Msg->energy = rxbuff[2];
Msg->chassis_power = (uint16_t)(rxbuff[3] << 1); // 左移一位还原量程
Msg->bat_voltage = rxbuff[4];
Msg->bat_power = rxbuff[5];
} }
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config) SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config)
@@ -32,10 +34,24 @@ SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config)
return super_cap_instance; return super_cap_instance;
} }
void SuperCapSend(SuperCapInstance *instance, uint8_t *data) void SuperCapSend(SuperCapInstance *instance, SuperCap_TxMsg_s *tx_data)
{ {
memcpy(instance->can_ins->tx_buff, data, 8); uint8_t *txbuff = instance->can_ins->tx_buff;
CANTransmit(instance->can_ins,1);
// 按照新协议装载发送数据
txbuff[0] = tx_data->Enable;
txbuff[1] = tx_data->Charge;
txbuff[2] = tx_data->Powerlimit - instance->power_offset; // 计算功率补偿
txbuff[3] = tx_data->ChargePower;
CANTransmit(instance->can_ins, 1);
}
void SuperCapSetPowerOffset(SuperCapInstance *instance, uint8_t offset)
{
if (instance != NULL) {
instance->power_offset = offset;
}
} }
SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance) SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance)

View File

@@ -1,29 +1,61 @@
/* /*
* @Descripttion: * @Descripttion: 适配群星战队协议的超级电容模块
* @version: * @version: V2.0
* @Author: Chenfu
* @Date: 2022-12-02 21:32:47
* @LastEditTime: 2022-12-05 15:25:46
*/ */
#ifndef SUPER_CAP_H #ifndef SUPER_CAP_H
#define SUPER_CAP_H #define SUPER_CAP_H
#include "bsp_can.h" #include "bsp_can.h"
// 超级电容状态枚举(从群星战队协议移植)
typedef enum
{
DISCHARGE = 0, // 放电状态
CHARGE = 1, // 充电状态
WAIT = 2, // 待机状态
SOFTSTART_PROTECTION = 3, // 处于软起动状态
OVER_LOAD_PROTECTION = 4, // 超电过载保护状态
BAT_OVER_VOLTAGE_PROTECTION = 5, // 过压保护状态
BAT_UNDER_VOLTAGE_PROTECTION = 6, // 电池欠压保护
CAP_UNDER_VOLTAGE_PROTECTION = 7, // 超级电容欠压保护
OVER_TEMPERATURE_PROTECTION = 8, // 过温保护
BOOM = 9, // 超电爆炸了
} SuperCapStateEnum;
// 超级电容准备状态
typedef enum
{
UNREADY = 0,
READY = 1,
} SuperCapReadyEnum;
#pragma pack(1) #pragma pack(1)
/* 接收到的超级电容信息结构体 */
typedef struct typedef struct
{ {
uint16_t vol; // 电压 SuperCapReadyEnum ready; // 可用标志:1为可用,0为不可用
uint16_t current; // 电流 SuperCapStateEnum state; // 状态标志
uint16_t power; // 功率 uint8_t energy; // 超级电容可用能量:0-100%
uint16_t chassis_power; // 底盘功率
uint8_t bat_voltage; // 电池电压*10
uint8_t bat_power; // 电池功率
} SuperCap_Msg_s; } SuperCap_Msg_s;
/* 需要发送给超级电容的控制信息结构体 */
typedef struct {
uint8_t Enable; // 超级电容使能。1使能,0失能
uint8_t Charge; // 超级电容充电。1充电,0放电
uint8_t Powerlimit; // 裁判系统功率限制
uint8_t ChargePower; // 超级电容充电功率
} SuperCap_TxMsg_s;
#pragma pack() #pragma pack()
/* 超级电容实例 */ /* 超级电容实例 */
typedef struct typedef struct
{ {
CANInstance *can_ins; // CAN实例 CANInstance *can_ins; // CAN实例
SuperCap_Msg_s cap_msg; // 超级电容信息 SuperCap_Msg_s cap_msg; // 超级电容接收信息
uint8_t power_offset; // 功率补偿值
} SuperCapInstance; } SuperCapInstance;
/* 超级电容初始化配置 */ /* 超级电容初始化配置 */
@@ -34,18 +66,22 @@ typedef struct
/** /**
* @brief 初始化超级电容 * @brief 初始化超级电容
*
* @param supercap_config 超级电容初始化配置
* @return SuperCapInstance* 超级电容实例指针
*/ */
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config); SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config);
/** /**
* @brief 发送超级电容控制信息 * @brief 发送超级电容控制信息
*
* @param instance 超级电容实例
* @param data 超级电容控制信息
*/ */
void SuperCapSend(SuperCapInstance *instance, uint8_t *data); void SuperCapSend(SuperCapInstance *instance, SuperCap_TxMsg_s *tx_data);
#endif // !SUPER_CAP_Hd /**
* @brief 设置超级电容功率补偿
*/
void SuperCapSetPowerOffset(SuperCapInstance *instance, uint8_t offset);
/**
* @brief 获取超级电容数据
*/
SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance);
#endif // !SUPER_CAP_H

View File

@@ -1,21 +1,16 @@
<!-- # super_cap (适配版)
* @Descripttion:
* @version:
* @Author: Chenfu
* @Date: 2022-12-02 21:32:47
* @LastEditTime: 2022-12-05 15:27:57
-->
# super_can
## 代码结构 ## 代码结构
.h中放置的是数据定义和外部接口,以及协议的定义和宏,.c中包含一些私有函数。 .h中放置的是数据和协议枚举定义、控制接口,.c中包含CAN接收中断的回调解析以及发送打包逻辑。当前版本已完全适配群星战队的超级电容协议。
## 外部接口 ## 外部接口
```c ```c
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s* supercap_config); SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s* supercap_config);
void SuperCapSend(SuperCapInstance *instance, uint8_t *data); void SuperCapSend(SuperCapInstance *instance, SuperCap_TxMsg_s *tx_data);
void SuperCapSetPowerOffset(SuperCapInstance *instance, uint8_t offset);
SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance);
``` ```
## 私有函数和变量 ## 私有函数和变量
@@ -25,7 +20,7 @@ static uint8_t *rxbuff;
static void SuperCapRxCallback(can_instance *_instance) static void SuperCapRxCallback(can_instance *_instance)
``` ```
`SuperCapRxCallback()`是super cap初始化can实例时的回调函数,用于can接收中断,进行协议解析。 SuperCapRxCallback()是super cap初始化can实例时的回调函数,用于CAN接收中断,进行协议解析(包含底盘功率右移量程的还原等处理)。
## 使用范例 ## 使用范例
@@ -33,22 +28,28 @@ static void SuperCapRxCallback(can_instance *_instance)
```c ```c
SuperCap_Init_Config_s capconfig = { SuperCap_Init_Config_s capconfig = {
.can_config = { .can_config = {
.can_handle = &hcan1, .can_handle = &hcan1,
.rx_id = 0x301, .rx_id = 0x100, // 超级电容发给C板的ID
.tx_id = 0x302 .tx_id = 0x001 // C板发给超级电容的ID
}, }
.recv_data_len = 4*sizeof(uint16_t), };
.send_data_len = sizeof(uint8_t) SuperCapInstance *ins = SuperCapInit(&capconfig);
};
SuperCapInstance *ins =SuperCapInit(&capconfig); // 如果需要设置功率补偿
SuperCapSetPowerOffset(ins, 5);
``` ```
发送通过`SuperCapSend()`,建议使用强制类型转换: 发送通过`SuperCapSend()`,建议使用强制类型转换:
发送控制指令时,使用 SuperCap_TxMsg_s 结构体:
```c ```c
uint16_t tx = 0x321; SuperCap_TxMsg_s tx_cmd = {
SuperCapSend(ins, (uint8_t*)&tx); .Enable = 1,
.Charge = 1,
.Powerlimit = 60,
.ChargePower = 40
};
SuperCapSend(ins, &tx_cmd);
``` ```