mirror of
https://gitee.com/dlmu-cone/half-steering-basic-framework
synced 2026-10-24 01:52:20 +08:00
supercap
This commit is contained in:
@@ -170,11 +170,14 @@ void ChassisInit()
|
||||
|
||||
SuperCap_Init_Config_s cap_conf = {
|
||||
.can_config = {
|
||||
.can_handle = &hcan2,
|
||||
.tx_id = 0x302, // 超级电容默认接收id
|
||||
.rx_id = 0x301, // 超级电容默认发送id,注意tx和rx在其他人看来是反的
|
||||
.can_handle = &hcan2, // 保持你原有的硬件CAN接口不变
|
||||
.tx_id = 0x001, // C板发给超级电容的ID
|
||||
.rx_id = 0x100, // 超级电容发给C板的ID
|
||||
}};
|
||||
cap = SuperCapInit(&cap_conf); // 超级电容初始化
|
||||
cap = SuperCapInit(&cap_conf); // 超级电容初始化 todo 控制
|
||||
|
||||
// (新增)设置超级电容功率补偿,例如补偿5W,可根据实际需求调整
|
||||
SuperCapSetPowerOffset(cap, 5);
|
||||
|
||||
// 发布订阅初始化,如果为双板,则需要can comm来传递消息
|
||||
#ifdef CHASSIS_BOARD
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
/*
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:29:49
|
||||
* @Descripttion: 适配群星战队协议的超级电容模块
|
||||
* @version: V2.0
|
||||
*/
|
||||
#include "super_cap.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
static SuperCapInstance *super_cap_instance = NULL; // 可以由app保存此指针
|
||||
static SuperCapInstance *super_cap_instance = NULL;
|
||||
|
||||
static void SuperCapRxCallback(CANInstance *_instance)
|
||||
{
|
||||
@@ -17,25 +14,44 @@ static void SuperCapRxCallback(CANInstance *_instance)
|
||||
SuperCap_Msg_s *Msg;
|
||||
rxbuff = _instance->rx_buff;
|
||||
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)
|
||||
{
|
||||
super_cap_instance = (SuperCapInstance *)malloc(sizeof(SuperCapInstance));
|
||||
memset(super_cap_instance, 0, sizeof(SuperCapInstance));
|
||||
|
||||
|
||||
supercap_config->can_config.can_module_callback = SuperCapRxCallback;
|
||||
super_cap_instance->can_ins = CANRegister(&supercap_config->can_config);
|
||||
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);
|
||||
CANTransmit(instance->can_ins,1);
|
||||
uint8_t *txbuff = instance->can_ins->tx_buff;
|
||||
|
||||
// 按照新协议装载发送数据
|
||||
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)
|
||||
|
||||
@@ -1,29 +1,61 @@
|
||||
/*
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:25:46
|
||||
* @Descripttion: 适配群星战队协议的超级电容模块
|
||||
* @version: V2.0
|
||||
*/
|
||||
#ifndef SUPER_CAP_H
|
||||
#define SUPER_CAP_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)
|
||||
/* 接收到的超级电容信息结构体 */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t vol; // 电压
|
||||
uint16_t current; // 电流
|
||||
uint16_t power; // 功率
|
||||
SuperCapReadyEnum ready; // 可用标志:1为可用,0为不可用
|
||||
SuperCapStateEnum state; // 状态标志
|
||||
uint8_t energy; // 超级电容可用能量:0-100%
|
||||
uint16_t chassis_power; // 底盘功率
|
||||
uint8_t bat_voltage; // 电池电压*10
|
||||
uint8_t bat_power; // 电池功率
|
||||
} 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()
|
||||
|
||||
/* 超级电容实例 */
|
||||
typedef struct
|
||||
{
|
||||
CANInstance *can_ins; // CAN实例
|
||||
SuperCap_Msg_s cap_msg; // 超级电容信息
|
||||
CANInstance *can_ins; // CAN实例
|
||||
SuperCap_Msg_s cap_msg; // 超级电容接收信息
|
||||
uint8_t power_offset; // 功率补偿值
|
||||
} SuperCapInstance;
|
||||
|
||||
/* 超级电容初始化配置 */
|
||||
@@ -34,18 +66,22 @@ typedef struct
|
||||
|
||||
/**
|
||||
* @brief 初始化超级电容
|
||||
*
|
||||
* @param supercap_config 超级电容初始化配置
|
||||
* @return SuperCapInstance* 超级电容实例指针
|
||||
*/
|
||||
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config);
|
||||
|
||||
/**
|
||||
* @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
|
||||
@@ -1,21 +1,16 @@
|
||||
<!--
|
||||
* @Descripttion:
|
||||
* @version:
|
||||
* @Author: Chenfu
|
||||
* @Date: 2022-12-02 21:32:47
|
||||
* @LastEditTime: 2022-12-05 15:27:57
|
||||
-->
|
||||
# super_can
|
||||
# super_cap (适配版)
|
||||
|
||||
## 代码结构
|
||||
|
||||
.h中放置的是数据定义和外部接口,以及协议的定义和宏,.c中包含一些私有函数。
|
||||
.h中放置的是数据和协议枚举定义、控制接口,.c中包含CAN接收中断的回调解析以及发送打包逻辑。当前版本已完全适配群星战队的超级电容协议。
|
||||
|
||||
## 外部接口
|
||||
|
||||
```c
|
||||
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)
|
||||
```
|
||||
|
||||
`SuperCapRxCallback()`是super cap初始化can实例时的回调函数,用于can接收中断,进行协议解析。
|
||||
SuperCapRxCallback()是super cap初始化can实例时的回调函数,用于CAN接收中断,进行协议解析(包含底盘功率右移量程的还原等处理)。
|
||||
|
||||
## 使用范例
|
||||
|
||||
@@ -33,22 +28,28 @@ static void SuperCapRxCallback(can_instance *_instance)
|
||||
|
||||
```c
|
||||
SuperCap_Init_Config_s capconfig = {
|
||||
.can_config = {
|
||||
.can_handle = &hcan1,
|
||||
.rx_id = 0x301,
|
||||
.tx_id = 0x302
|
||||
},
|
||||
.recv_data_len = 4*sizeof(uint16_t),
|
||||
.send_data_len = sizeof(uint8_t)
|
||||
};
|
||||
SuperCapInstance *ins =SuperCapInit(&capconfig);
|
||||
.can_config = {
|
||||
.can_handle = &hcan1,
|
||||
.rx_id = 0x100, // 超级电容发给C板的ID
|
||||
.tx_id = 0x001 // C板发给超级电容的ID
|
||||
}
|
||||
};
|
||||
SuperCapInstance *ins = SuperCapInit(&capconfig);
|
||||
|
||||
// 如果需要设置功率补偿
|
||||
SuperCapSetPowerOffset(ins, 5);
|
||||
```
|
||||
|
||||
|
||||
发送通过`SuperCapSend()`,建议使用强制类型转换:
|
||||
|
||||
发送控制指令时,使用 SuperCap_TxMsg_s 结构体:
|
||||
```c
|
||||
uint16_t tx = 0x321;
|
||||
SuperCapSend(ins, (uint8_t*)&tx);
|
||||
SuperCap_TxMsg_s tx_cmd = {
|
||||
.Enable = 1,
|
||||
.Charge = 1,
|
||||
.Powerlimit = 60,
|
||||
.ChargePower = 40
|
||||
};
|
||||
SuperCapSend(ins, &tx_cmd);
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user