mirror of
https://gitee.com/dlmu-cone/half-steering-basic-framework
synced 2026-10-24 01:52:20 +08:00
60 lines
1.7 KiB
C
60 lines
1.7 KiB
C
/*
|
|
* @Descripttion: 适配群星战队协议的超级电容模块
|
|
* @version: V2.0
|
|
*/
|
|
#include "super_cap.h"
|
|
#include "memory.h"
|
|
#include "stdlib.h"
|
|
|
|
static SuperCapInstance *super_cap_instance = NULL;
|
|
|
|
static void SuperCapRxCallback(CANInstance *_instance)
|
|
{
|
|
uint8_t *rxbuff;
|
|
SuperCap_Msg_s *Msg;
|
|
rxbuff = _instance->rx_buff;
|
|
Msg = &super_cap_instance->cap_msg;
|
|
|
|
// 按照新协议解析接收数据
|
|
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, SuperCap_TxMsg_s *tx_data)
|
|
{
|
|
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)
|
|
{
|
|
return instance->cap_msg;
|
|
} |