2026-01-17 14:06:59 +08:00
|
|
|
|
/*
|
2026-03-10 03:05:49 +08:00
|
|
|
|
* @Descripttion: 适配群星战队协议的超级电容模块
|
|
|
|
|
|
* @version: V2.0
|
2026-01-17 14:06:59 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#include "super_cap.h"
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
|
|
#include "stdlib.h"
|
2026-04-15 16:10:26 +08:00
|
|
|
|
#include "bsp_dwt.h" // 【新增】引入获取时间的头文件,或者是 "main.h" 用 HAL_GetTick()
|
2026-01-17 14:06:59 +08:00
|
|
|
|
|
2026-03-10 03:05:49 +08:00
|
|
|
|
static SuperCapInstance *super_cap_instance = NULL;
|
2026-01-17 14:06:59 +08:00
|
|
|
|
|
|
|
|
|
|
static void SuperCapRxCallback(CANInstance *_instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint8_t *rxbuff;
|
|
|
|
|
|
SuperCap_Msg_s *Msg;
|
|
|
|
|
|
rxbuff = _instance->rx_buff;
|
|
|
|
|
|
Msg = &super_cap_instance->cap_msg;
|
2026-03-10 03:05:49 +08:00
|
|
|
|
|
2026-04-15 16:10:26 +08:00
|
|
|
|
// 【新增】更新最后一次收到消息的时间戳
|
|
|
|
|
|
super_cap_instance->last_update_tick = DWT_GetTimeline_ms(); // 如果没有DWT,用 HAL_GetTick() 替换
|
|
|
|
|
|
|
2026-03-10 03:05:49 +08:00
|
|
|
|
// 按照新协议解析接收数据
|
|
|
|
|
|
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];
|
2026-01-17 14:06:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-15 16:10:26 +08:00
|
|
|
|
// 【新增】在线判断函数
|
|
|
|
|
|
uint8_t SuperCapIsOnline(SuperCapInstance *instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (instance == NULL) return 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果当前时间减去上次接收时间超过 1000ms (1秒),则认为离线
|
|
|
|
|
|
if (DWT_GetTimeline_ms() - instance->last_update_tick > 2000) {
|
|
|
|
|
|
return 0; // 离线
|
|
|
|
|
|
}
|
|
|
|
|
|
return 1; // 在线
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-17 14:06:59 +08:00
|
|
|
|
SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config)
|
|
|
|
|
|
{
|
|
|
|
|
|
super_cap_instance = (SuperCapInstance *)malloc(sizeof(SuperCapInstance));
|
|
|
|
|
|
memset(super_cap_instance, 0, sizeof(SuperCapInstance));
|
2026-03-10 03:05:49 +08:00
|
|
|
|
|
2026-01-17 14:06:59 +08:00
|
|
|
|
supercap_config->can_config.can_module_callback = SuperCapRxCallback;
|
|
|
|
|
|
super_cap_instance->can_ins = CANRegister(&supercap_config->can_config);
|
|
|
|
|
|
return super_cap_instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-10 03:05:49 +08:00
|
|
|
|
void SuperCapSend(SuperCapInstance *instance, SuperCap_TxMsg_s *tx_data)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint8_t *txbuff = instance->can_ins->tx_buff;
|
|
|
|
|
|
|
|
|
|
|
|
// 按照新协议装载发送数据
|
|
|
|
|
|
txbuff[0] = tx_data->Enable;
|
2026-04-15 16:10:26 +08:00
|
|
|
|
txbuff[1] = tx_data->Reserved1;
|
2026-03-10 03:05:49 +08:00
|
|
|
|
txbuff[2] = tx_data->Powerlimit - instance->power_offset; // 计算功率补偿
|
2026-04-15 16:10:26 +08:00
|
|
|
|
txbuff[3] = tx_data->Reserved2;
|
2026-03-10 03:05:49 +08:00
|
|
|
|
|
|
|
|
|
|
CANTransmit(instance->can_ins, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SuperCapSetPowerOffset(SuperCapInstance *instance, uint8_t offset)
|
2026-01-17 14:06:59 +08:00
|
|
|
|
{
|
2026-03-10 03:05:49 +08:00
|
|
|
|
if (instance != NULL) {
|
|
|
|
|
|
instance->power_offset = offset;
|
|
|
|
|
|
}
|
2026-01-17 14:06:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SuperCap_Msg_s SuperCapGet(SuperCapInstance *instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance->cap_msg;
|
|
|
|
|
|
}
|