123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /*
- * @Description:
- 该bms协议主动上传,只需要解析即可
- 对外3个接口:
- 数据解析,存在结构体
- 对外提供结构体查询
- 在线计时
- Bug0020
- * @version:
- * @Author: Joe
- * @Date: 2021-11-13 13:23:14
- * @LastEditTime: 2021-11-13 18:30:31
- */
- #include "bms.h"
- #include "string.h"
- #include "tfmini.h"
- #define DBG_TAG "bms"
- #define DBG_LVL DBG_INFO
- #include <rtdbg.h>
- #define CRC_16_POLYNOMIALS 0xA001
- static Battery_TypeDef bat = {0};
- /************************维旭电池****************************/
- uint8_t bms_parse(struct rt_can_msg *msg) //数据解析
- {
- uint8_t temp = 1;
- if(msg->ide!=RT_CAN_EXTID) //扩展帧
- return temp;
- temp = 0;
- bat.enable = 1;
- bat.miss_cnt = 0;
- switch(msg->id) /*解析数据*/
- {
- case 0xA6D0D09: //高压信息
- {
- bat.voltage = msg->data[0]<<8 | msg->data[1];
- bat.current = msg->data[2]<<8 | msg->data[3];
- }
- break;
- case 0xA6E0D09: //电量信息
- {
- bat.rsoc = (msg->data[0]<<8 | msg->data[1])/10; //%
- bat.current = msg->data[2]<<8 | msg->data[3];
- }
- break;
- case 0xAB40D09: //故障信息
- {
- bat.protect_status = msg->data[0]<<24 | msg->data[1]<<16 | msg->data[2]<<8 | msg->data[3];
- }
- break;
- default:
- break;
- }//数据解析
- return temp;
- }
- /************************奥冠电池****************************/
- /****************************************
- 函数功能 : 计算info字节长度校验项
- 参数描述 :
- 返回值 : 校验码,hex
- ****************************************/
- static uint16_t ag_bms_crc16(uint8_t* pchMsg,uint8_t wDataLen)
- {
- uint8_t i, chChar;
- uint16_t wCRC = 0xFFFF;
- while (wDataLen--)
- {
- chChar = *pchMsg++;
- wCRC ^= (uint16_t) chChar;
- for (i = 0; i < 8; i++)
- {
- if (wCRC & 0x0001)
- wCRC = (wCRC >> 1) ^ CRC_16_POLYNOMIALS;
- else
- wCRC >>= 1;
- }
- }
- return wCRC;
- }
- uint8_t ag_bms_parse(struct rt_can_msg *msg) //数据解析
- {
- uint8_t temp = 1;
- uint16_t chksum;
- if(msg->id != 0x101 || msg->rtr != RT_CAN_DTR) /* 地址为0x101,返回值为数据帧 */
- return temp;
- //CRC-16校验
- chksum = ag_bms_crc16((uint8_t*)msg->data,(msg->len-2));
- if( msg->data[msg->len-2]!=(chksum >> 8) || msg->data[msg->len-1]!=(chksum & 0x00FF))
- return temp;
- temp = 0;
- bat.enable = 1;
- //充满容量、循环次数、RSOC
- bat.rsoc = msg->data[4]<<8 | msg->data[5];
- return temp;
- }
- /****************************************
- * 查询bms数据
- *函数功能 :
- *参数描述 : 无
- *返回值 : 返回发送的can结构体
- ****************************************/
- struct rt_can_msg require_ag_bms_msg(void)
- {
- struct rt_can_msg tx_msg;
-
- tx_msg.id = 0x101;
- tx_msg.ide = RT_CAN_STDID; /* 标准格式 */
- tx_msg.rtr = RT_CAN_RTR; /* 遥控帧 */
- tx_msg.len = 1; /* 数据长度为 1 */
- /* 返回发送的can结构体 */
- return tx_msg;
- }
- uint16_t get_bat_rsoc(void)
- {
- return bat.rsoc;
- }
- uint8_t get_bat_enable(void)
- {
- return bat.enable;
- }
- /****************************************
- * get
- *函数功能 : 获取状态
- *参数描述 : 无
- *返回值 : 无
- ****************************************/
- int get(int argc, char **argv)
- {
- const char* help_info[] =
- {
- [0] = " get param - get machine param",
- [1] = " get bat - get battery param",
- [2] = " get tfmini - get tfmini param",
- };
- if (argc < 2)
- {
- LOG_I("Usage:");
- for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
- {
- LOG_I("%s", help_info[i]);
- }
- }
- else
- {
- const char *operator = argv[1];
-
- if(!strcmp(operator, "bat"))
- {
- LOG_I("bat:enable[%d] rsoc[%d] current[%d] voltage[%s]", bat.enable,bat.rsoc,bat.current,bat.voltage);
- }
- else
- if(!strcmp(operator, "tfmini"))
- {
- TFMINI_TypeDef fmini;
- fmini = get_tfmini_for();
- LOG_I("tfmini_for:dist[%d] strength[%d]", fmini.dist,fmini.strength) ;
- fmini = get_tfmini_back();
- LOG_I("tfmini_back:dist[%d] strength[%d]", fmini.dist,fmini.strength) ;
- fmini = get_tfmini_left();
- LOG_I("tfmini_left:dist[%d] strength[%d]", fmini.dist,fmini.strength) ;
- fmini = get_tfmini_right();
- LOG_I("tfmini_right:dist[%d] strength[%d]", fmini.dist,fmini.strength) ;
- }
-
- }
- return 0;
- }
- MSH_CMD_EXPORT(get , get machine param);
- /****************************************
- * bat_init
- *函数功能 : 配置初始化
- *参数描述 : 无
- *返回值 : 无
- ****************************************/
- int bat_init(void)
- {
- bat.rsoc = 0;
- bat.enable = 0;
- return RT_EOK;
- }
- INIT_APP_EXPORT(bat_init);
|