@@ -17,9 +17,6 @@ HAL_StatusTypeDef ENCODER_CANFilterInit(CAN_HandleTypeDef* hcan) {
1717 sFilterConfig .FilterIdHigh = (0x50 << 5 ); // 左移 5 位
1818 sFilterConfig .FilterIdLow = 0x0000 ;
1919
20- // 设置掩码为 0xFFFF,表示匹配所有位
21- // sFilterConfig.FilterMaskIdHigh = 0xFFFF;
22- // sFilterConfig.FilterMaskIdLow = 0xFFFF;
2320 sFilterConfig .FilterMaskIdHigh = 0x0000 ;
2421 sFilterConfig .FilterMaskIdLow = 0x0000 ;
2522
@@ -28,97 +25,19 @@ HAL_StatusTypeDef ENCODER_CANFilterInit(CAN_HandleTypeDef* hcan) {
2825 sFilterConfig .SlaveStartFilterBank = 28 ;
2926
3027 if (HAL_CAN_ConfigFilter (hcan , & sFilterConfig ) != HAL_OK ) {
31- Error_Handler () ;
28+ return HAL_ERROR ;
3229 }
3330
3431 if (HAL_CAN_Start (hcan ) != HAL_OK ) {
35- Error_Handler () ;
32+ return HAL_ERROR ;
3633 }
3734
3835 if (HAL_CAN_ActivateNotification (hcan , CAN_IT_RX_FIFO0_MSG_PENDING ) != HAL_OK ) {
39- Error_Handler () ;
36+ return HAL_ERROR ;
4037 }
4138
4239 return HAL_OK ;
4340}
44- // void CheckCANError() {
45- // uint32_t errorCode = HAL_CAN_GetError(&hcan2);
46- // // 检查是否有错误
47- // if (errorCode != HAL_CAN_ERROR_NONE) {
48- // // 打印错误状态
49- // printf("CAN Error Code: %lu\n", errorCode);
50- // // 检查特定的错误类型
51- // if (errorCode & HAL_CAN_ERROR_EWG) {
52- // printf("Error Warning Flag set\n");
53- // }
54- // if (errorCode & HAL_CAN_ERROR_EPV) {
55- // printf("Error Passive Flag set\n");
56- // }
57- // if (errorCode & HAL_CAN_ERROR_BOF) {
58- // printf("Bus Off Flag set\n");
59- // }
60- // if (errorCode & HAL_CAN_ERROR_ACK) {
61- // printf("ACK Error Flag set\n");
62- // }
63- // if (errorCode & HAL_CAN_ERROR_STF) {
64- // printf("Stuff Error Flag set\n");
65- // }
66- // if (errorCode & HAL_CAN_ERROR_FOR) {
67- // printf("Form Error Flag set\n");
68- // }
69- // if (errorCode & HAL_CAN_ERROR_BR) {
70- // printf("Bit Recessive Error Flag set\n");
71- // }
72- // // 去掉 HAL_CAN_ERROR_BS 和 HAL_CAN_ERROR_ID
73- // if (errorCode & HAL_CAN_ERROR_CRC) {
74- // printf("CRC Error Flag set\n");
75- // }
76- // } else {
77- // printf("No CAN errors detected\n");
78- // }
79- // }
80-
81- // void ReceiveCANData(CAN_HandleTypeDef* hcan, EncoderData* data) {
82- // CAN_RxHeaderTypeDef RxHeader;
83- // uint8_t RxData[8];
84- // CheckCANError();
85- // // 检查接收FIFO是否有消息
86- // if (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) > 0) {
87- // // 接收消息
88- // if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK) {
89- // printf("Received message: ");
90- // for (int i = 0; i < RxHeader.DLC; i++) {
91- // printf("%02X ", RxData[i]);
92- // }
93- // printf("\n");
94- // // 解析数据
95- // if (RxData[0] == 0x55 && RxData[1] == 0x55) {
96- // uint16_t angleReg = (RxData[2] << 8) | RxData[3];
97- // data->angle = angleReg * 360.0f / 32768.0f;
98- // uint16_t angularSpeedReg = (RxData[4] << 8) | RxData[5];
99- // data->angularSpeed = angularSpeedReg * 360.0f / 32768.0f / 0.1f;
100- // data->revolutions = (RxData[6] << 8) | RxData[7];
101- // } else if (RxData[0] == 0x55 && RxData[1] == 0x56) {
102- // uint16_t tempReg = (RxData[2] << 8) | RxData[3];
103- // data->temperature = tempReg / 100.0f;
104- // }
105- // } else {
106- // printf("Failed to get message from FIFO\n");
107- // }
108- // } else {
109- // printf("No message in FIFO\n");
110- // }
111- // if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK) {
112- // // 处理接收到的数据
113- // printf("Received message ID: %X, Data: ", RxHeader.StdId);
114- // for (int i = 0; i < RxHeader.DLC; i++) {
115- // printf("%02X ", RxData[i]);
116- // }
117- // printf("\n");
118- // } else {
119- // printf("Failed to get message from FIFO\n");
120- // }
121- // }
12241
12342void ParseCANData (CAN_RxHeaderTypeDef * RxHeader , uint8_t * RxData , EncoderData * data ,EncoderCalculateData * caldata ) {
12443 // 解析数据
@@ -153,30 +72,6 @@ void ParseCANData(CAN_RxHeaderTypeDef *RxHeader, uint8_t *RxData, EncoderData *d
15372 }
15473}
15574
156- // void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
157- // CAN_RxHeaderTypeDef RxHeader;
158- // uint8_t RxData[8];
159- // EncoderData data;
160- // CheckCANError();
161- // 检查接收FIFO是否有消息
162- // if (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) > 0) {
163- // // 接收消息
164- // if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK) {
165- // printf("Received message ID: %X, Data: ", RxHeader.StdId);
166- // for (int i = 0; i < RxHeader.DLC; i++) {
167- // printf("%02X ", RxData[i]);
168- // }
169- // printf("\n");
170- // // 解析数据
171- // ParseCANData(&RxHeader, RxData, &data);
172- // } else {
173- // printf("Failed to get message from FIFO\n");
174- // }
175- // } else {
176- // printf("No message in FIFO\n");
177- // }
178- // }
179-
18075// 发送配置命令的函数
18176HAL_StatusTypeDef SendConfigCommand (CAN_HandleTypeDef * hcan , uint8_t * command , uint8_t length ) {
18277 CAN_TxHeaderTypeDef TxHeader ;
@@ -191,7 +86,7 @@ HAL_StatusTypeDef SendConfigCommand(CAN_HandleTypeDef* hcan, uint8_t* command, u
19186
19287//该角编码器是掉电保存配置,只需要配置一次即可
19388//白线置零也会置零波特率
194- void Encoder_Init (CAN_HandleTypeDef * hcan ) {
89+ void Encoder_Setup (CAN_HandleTypeDef * hcan ) {
19590 HAL_StatusTypeDef status ;
19691
19792 // Step 1: 解锁
@@ -254,4 +149,10 @@ void Encoder_Init(CAN_HandleTypeDef* hcan) {
254149 HAL_Delay (10 );
255150
256151 DEBUG_PRINT ("Encoder Initialization Complete\n" );
152+ }
153+
154+ HAL_StatusTypeDef Encoder_Init (void )
155+ {
156+ if (caldata .angle > 19000 || caldata .angle < 17000 ) return HAL_ERROR ;
157+ else return HAL_OK ;
257158}
0 commit comments