From 82ee444c00c88a7885ca116ac71c0d766dfea5ba Mon Sep 17 00:00:00 2001 From: NeoZeng Date: Sun, 27 Nov 2022 20:58:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CAN=20Comm=E9=80=9A=E4=BF=A1?= =?UTF-8?q?=E5=8A=9F=E8=83=BD,=E6=8E=A5=E6=94=B6=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=B0=9A=E6=9C=AA=E7=BC=96=E5=86=99=E5=AE=8C?= =?UTF-8?q?=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 +- bsp/bsp_can.c | 13 ++++-- modules/algorithm/controller.c | 8 ++-- modules/can_comm/can_comm.c | 52 +++++++++++++++++++++++ modules/can_comm/can_comm.h | 76 ++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 94e9aae..141913c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,9 @@ "ins_task.h": "c", "task.h": "c", "user_lib.h": "c", - "chassis.h": "c" + "chassis.h": "c", + "can_comm.h": "c", + "string.h": "c" }, "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools", "C_Cpp.intelliSenseEngineFallback": "enabled", diff --git a/bsp/bsp_can.c b/bsp/bsp_can.c index 53e9981..c3045eb 100644 --- a/bsp/bsp_can.c +++ b/bsp/bsp_can.c @@ -102,12 +102,17 @@ static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox) HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff); for (size_t i = 0; i < DEVICE_CAN_CNT; i++) { - if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id) + if(instance[i]!=NULL) { - memcpy(instance[i]->rx_buff, can_rx_buff, 8); - instance[i]->can_module_callback(instance[i]); - break; + if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id) + { + memcpy(instance[i]->rx_buff, can_rx_buff, 8); + instance[i]->can_module_callback(instance[i]); + break; + } } + else + break; } } diff --git a/modules/algorithm/controller.c b/modules/algorithm/controller.c index 4b0648e..d25379a 100644 --- a/modules/algorithm/controller.c +++ b/modules/algorithm/controller.c @@ -14,10 +14,10 @@ // PID优化环节函数声明 static void f_Trapezoid_Intergral(PID_t *pid); // 梯形积分 static void f_Integral_Limit(PID_t *pid); // 积分限幅 -static void f_Derivative_On_Measurement(PID_t *pid); // 微分先行(仅使用反馈微分) -static void f_Changing_Integration_Rate(PID_t *pid); // 变速积分 -static void f_Output_Filter(PID_t *pid); // 输出滤波 -static void f_Derivative_Filter(PID_t *pid); // 微分滤波(采集时) +static void f_Derivative_On_Measurement(PID_t *pid); // 微分先行(仅使用反馈值而不计参考输入的微分) +static void f_Changing_Integration_Rate(PID_t *pid); // 变速积分(误差小时积分作用更强) +static void f_Output_Filter(PID_t *pid); // 输出滤波(平滑输出) +static void f_Derivative_Filter(PID_t *pid); // 微分滤波(采集时,滤除高频噪声) static void f_Output_Limit(PID_t *pid); // 输出限幅 static void f_PID_ErrorHandle(PID_t *pid); // 堵转保护 diff --git a/modules/can_comm/can_comm.c b/modules/can_comm/can_comm.c index e69de29..ff0543e 100644 --- a/modules/can_comm/can_comm.c +++ b/modules/can_comm/can_comm.c @@ -0,0 +1,52 @@ +#include "can_comm.h" +#include "memory.h" +#include "stdlib.h" + +static CANCommInstance *can_comm_instance[MX_CAN_COMM_COUNT] = {NULL}; +static uint8_t idx; + +static void CANCommRxCallback(can_instance *_instance) +{ + for (size_t i = 0; i < idx; i++) + { + if (&can_comm_instance[i]->can_ins == _instance) + { + } + } +} + +CANCommInstance *CANCommInit(CANComm_Init_Config_s config) +{ + can_comm_instance[idx] = (CANCommInstance *)malloc(sizeof(CANCommInstance)); + memset(can_comm_instance[idx], 0, sizeof(CANCommInstance)); + can_comm_instance[idx]->recv_data_len = config.recv_data_len; + can_comm_instance[idx]->send_data_len = config.send_data_len; + can_comm_instance[idx]->send_buf_len = config.send_data_len + CAN_COMM_OFFSET_BYTES; + can_comm_instance[idx]->raw_sendbuf[0] = CAN_COMM_HEADER; + can_comm_instance[idx]->raw_sendbuf[1] = config.send_data_len; + can_comm_instance[idx]->raw_sendbuf[config.send_data_len + CAN_COMM_OFFSET_BYTES - 1] = CAN_COMM_TAIL; + + config.can_config.can_module_callback = CANCommRxCallback; + CANRegister(&can_comm_instance[idx]->can_ins, config.can_config); + return can_comm_instance[idx++]; +} + +void CANCommSend(CANCommInstance *instance, uint8_t *data) +{ + static uint8_t crc8; + memcpy(instance->raw_sendbuf + 2, data, instance->send_data_len); + crc8 = crc_8(data, instance->send_data_len); + instance->raw_sendbuf[2 + instance->send_data_len] = crc8; + + for (size_t i = 0; i < instance->send_buf_len; i += 8) + { + memcpy(instance->can_ins.tx_buff, instance->raw_sendbuf[i], 8); + CANTransmit(&instance->can_ins); + } +} + +void *CANCommGet(CANCommInstance *instance) +{ + instance->update_flag = 0; + return instance->unpacked_recv_data; +} \ No newline at end of file diff --git a/modules/can_comm/can_comm.h b/modules/can_comm/can_comm.h index e69de29..aa47ba8 100644 --- a/modules/can_comm/can_comm.h +++ b/modules/can_comm/can_comm.h @@ -0,0 +1,76 @@ +/** + * @file can_comm.h + * @author Neo neozng1@hnu.edu.cn + * @brief 用于多机CAN通信的收发模块 + * @version 0.1 + * @date 2022-11-27 + * + * @copyright Copyright (c) 2022 HNUYueLu EC all rights reserved + * + */ +#ifndef CAN_COMM_H +#define CAN_COMM_H + + +#include "bsp_can.h" +#include "crc8.h" + +#define MX_CAN_COMM_COUNT 4 //注意均衡负载,一条总线上不要挂载过多的外设 + +#define CAN_COMM_MAX_BUFFSIZE 68 // 最大发送/接收字节数 +#define CAN_COMM_HEADER 's' +#define CAN_COMM_TAIL 'e' +#define CAN_COMM_OFFSET_BYTES 4 // 's'+datalen+'e'+crc8 + +typedef struct +{ + can_instance can_ins; + /* 发送部分 */ + uint8_t send_data_len; + uint8_t send_buf_len; + uint8_t raw_sendbuf[CAN_COMM_MAX_BUFFSIZE+CAN_COMM_OFFSET_BYTES]; //额外4个bytes保存帧头帧尾和校验和 + /* 接收部分 */ + uint8_t recv_data_len; + uint8_t recv_buf_len; + uint8_t raw_recvbuf[CAN_COMM_MAX_BUFFSIZE+CAN_COMM_OFFSET_BYTES]; //额外4个bytes保存帧头帧尾和校验和 + uint8_t unpacked_recv_data[CAN_COMM_MAX_BUFFSIZE]; + /* 接收和更新标志位*/ + uint8_t recv_state; + uint8_t cur_recv_len; + uint8_t update_flag; +} CANCommInstance; + +typedef struct +{ + uint8_t send_data_len; + uint8_t recv_data_len; + can_instance_config_s can_config; +}CANComm_Init_Config_s; + +/** + * @brief + * + * @param config + * @return CANCommInstance* + */ +CANCommInstance* CANCommInit(CANComm_Init_Config_s config); + +/** + * @brief 发送数据 + * + * @param instance can comm实例 + * @param data 注意此地址的有效数据长度需要和初始化时传入的datalen相同 + */ +void CANCommSend(CANCommInstance* instance,uint8_t* data); + +/** + * @brief 获取CAN COMM接收的数据,需要自己使用强制类型转换将返回的void指针转换成指定类型 + * + * @return void* 返回的数据指针 + * @attention 注意如果希望直接通过转换指针访问数据,如果数据是union或struct,要检查是否使用了pack(n) + * CAN COMM接收到的数据可以看作是pack(1)之后的,是连续存放的 + */ +void* CANCommGet(CANCommInstance* instance); + + +#endif // !CAN_COMM_H \ No newline at end of file