加入Bsp_can
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
#ifndef CAN_TASK_H
|
||||
#define CAN_TASK_H
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,141 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : Config.c
|
||||
* @brief : Configuare the Robot Functions
|
||||
* @author : Yan Yuanbin
|
||||
* @date : 2023/05/21
|
||||
* @version : v1.0
|
||||
******************************************************************************
|
||||
* @attention : To be perfected
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef ROBOT_CONFIG_H
|
||||
#define ROBOT_CONFIG_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "math.h"
|
||||
|
||||
/* General physics and mathematics constants ---------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief the value of local gravity acceleration
|
||||
*/
|
||||
|
||||
#define VAL_LIMIT(x,min,max) do{ \
|
||||
if ((x) > (max)) {(x) = (max);} \
|
||||
else if ((x) < (min)) {(x) = (min);} \
|
||||
}while(0U)
|
||||
|
||||
#define GravityAccel 9.718f
|
||||
|
||||
#define Angle_to_rad 0.01745329f
|
||||
|
||||
#define Rad_to_angle 57.2957732f
|
||||
|
||||
/**
|
||||
* @brief Euler's Number
|
||||
*/
|
||||
#define Euler_Number 2.718281828459045f
|
||||
|
||||
/**
|
||||
* @brief radian system rotation degrees system , 180.f/PI
|
||||
*/
|
||||
#define RadiansToDegrees 57.295779513f
|
||||
|
||||
/**
|
||||
* @brief degrees system rotation radian system , PI/180.f
|
||||
*/
|
||||
#define DegreesToRadians 0.01745329251f
|
||||
|
||||
/* Vision reslove constants -------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Decision Marking mode
|
||||
* 0: select the minimum yaw armor
|
||||
* 1: select the minimum distance armor
|
||||
*/
|
||||
#define Yaw_Distance_Decision 0
|
||||
|
||||
/**
|
||||
* @brief ballistic coefficient
|
||||
* @note 17mm: 0.038
|
||||
* 42mm: 0.019
|
||||
*/
|
||||
#define Bullet_Coefficient 0.038f
|
||||
|
||||
/**
|
||||
* @brief the half width of little armor
|
||||
*/
|
||||
#define LittleArmor_HalfWidth 0.07f
|
||||
|
||||
/**
|
||||
* @brief the half width of Large armor
|
||||
*/
|
||||
#define LargeArmor_HalfWidth 0.1175f
|
||||
|
||||
/* IMU reslove constants ---------------------------------------------------*/
|
||||
/**
|
||||
* @brief the flag of bmi088 Calibration
|
||||
* 0: DISABLE
|
||||
* 1: ENABLE
|
||||
*/
|
||||
#define IMU_Calibration_ENABLE 0U
|
||||
|
||||
/**
|
||||
* @brief the index of pitch angle update
|
||||
*/
|
||||
#define IMU_ANGLE_INDEX_PITCH 2U
|
||||
/**
|
||||
* @brief the index of yaw angle update
|
||||
*/
|
||||
#define IMU_ANGLE_INDEX_YAW 0U
|
||||
/**
|
||||
* @brief the index of roll angle update
|
||||
*/
|
||||
#define IMU_ANGLE_INDEX_ROLL 1U
|
||||
|
||||
/**
|
||||
* @brief the index of pitch gyro update
|
||||
*/
|
||||
#define IMU_GYRO_INDEX_PITCH 0U
|
||||
/**
|
||||
* @brief the index of yaw gyro update
|
||||
*/
|
||||
#define IMU_GYRO_INDEX_YAW 2U
|
||||
/**
|
||||
* @brief the index of roll gyro update
|
||||
*/
|
||||
#define IMU_GYRO_INDEX_ROLL 1U
|
||||
|
||||
/**
|
||||
* @brief the index of pitch accel update
|
||||
*/
|
||||
#define IMU_ACCEL_INDEX_PITCH 0U
|
||||
/**
|
||||
* @brief the index of yaw accel update
|
||||
*/
|
||||
#define IMU_ACCEL_INDEX_YAW 2U
|
||||
/**
|
||||
* @brief the index of roll accel update
|
||||
*/
|
||||
#define IMU_ACCEL_INDEX_ROLL 1U
|
||||
|
||||
/* Remote reslove constants -----------------------------------------------*/
|
||||
/**
|
||||
* @brief the flag of remote control receive frame data
|
||||
* @note 0: CAN
|
||||
* 1: USART
|
||||
*/
|
||||
#define REMOTE_FRAME_USART_CAN 0U
|
||||
|
||||
#endif //ROBOT_CONFIG_H
|
||||
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : Control_Task.c
|
||||
* @brief : Control task
|
||||
* @author : Yan Yuanbin
|
||||
* @date : 2023/04/27
|
||||
* @version : v1.0
|
||||
******************************************************************************
|
||||
* @attention : None
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef CONTROL_TASK_H
|
||||
#define CONTROL_TASK_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
|
||||
/**
|
||||
* @brief typedef structure that contains the information of chassis control
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
struct{
|
||||
|
||||
float Chassis_Velocity;
|
||||
|
||||
|
||||
}Target;
|
||||
|
||||
struct{
|
||||
|
||||
float Chassis_Velocity;
|
||||
|
||||
|
||||
}Measure;
|
||||
|
||||
int16_t SendValue[4];
|
||||
|
||||
}Control_Info_Typedef;
|
||||
|
||||
|
||||
extern Control_Info_Typedef Control_Info;
|
||||
#endif //CONTROL_TASK_H
|
||||
@@ -1,52 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : CAN_Task.c
|
||||
* @brief : CAN task
|
||||
* @author : GrassFam Wang
|
||||
* @date : 2025/1/22
|
||||
* @version : v1.1
|
||||
******************************************************************************
|
||||
* @attention : None
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "cmsis_os.h"
|
||||
#include "CAN_Task.h"
|
||||
#include "Control_Task.h"
|
||||
#include "INS_Task.h"
|
||||
#include "Motor.h"
|
||||
#include "bsp_can.h"
|
||||
#include "Remote_Control.h"
|
||||
#include "Control_Task.h"
|
||||
|
||||
/* USER CODE BEGIN Header_CAN_Task */
|
||||
/**
|
||||
* @brief Function implementing the StartCANTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_CAN_Task */
|
||||
|
||||
void CAN_Task(void const * argument)
|
||||
{
|
||||
|
||||
TickType_t CAN_Task_SysTick = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
|
||||
CAN_Task_SysTick = osKernelSysTick();
|
||||
|
||||
if(CAN_Task_SysTick % 2 == 0){
|
||||
|
||||
//500Hz<48><7A><EFBFBD><EFBFBD> <20>뱣֤<EBB1A3><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>osDelay(1)
|
||||
|
||||
}
|
||||
osDelay(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : Control_Task.c
|
||||
* @brief : Control task
|
||||
* @author : GrassFan Wang
|
||||
* @date : 2025/01/22
|
||||
* @version : v1.1
|
||||
******************************************************************************
|
||||
* @attention : None
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "Control_Task.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "Control_Task.h"
|
||||
#include "bsp_uart.h"
|
||||
#include "Remote_Control.h"
|
||||
#include "PID.h"
|
||||
#include "Motor.h"
|
||||
|
||||
static void Control_Init(Control_Info_Typedef *Control_Info);
|
||||
static void Control_Measure_Update(Control_Info_Typedef *Control_Info);
|
||||
static void Control_Target_Update(Control_Info_Typedef *Control_Info);
|
||||
static void Control_Info_Update(Control_Info_Typedef *Control_Info);
|
||||
|
||||
Control_Info_Typedef Control_Info;
|
||||
// KP KI KD Alpha Deadband I_MAX Output_MAX
|
||||
static float Chassis_PID_Param[7] = {13.f,0.1f,0.f,0.f, 0.f, 5000.f, 12000.f};
|
||||
|
||||
PID_Info_TypeDef Chassis_PID;
|
||||
|
||||
void Control_Task(void const * argument)
|
||||
{
|
||||
/* USER CODE BEGIN Control_Task */
|
||||
TickType_t Control_Task_SysTick = 0;
|
||||
|
||||
Control_Init(&Control_Info);
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
Control_Task_SysTick = osKernelSysTick();
|
||||
|
||||
|
||||
Control_Measure_Update(&Control_Info);
|
||||
Control_Target_Update(&Control_Info);
|
||||
Control_Info_Update(&Control_Info);
|
||||
USART_Vofa_Justfloat_Transmit(Control_Info.Measure.Chassis_Velocity,0.f,0.f);
|
||||
|
||||
osDelay(1);
|
||||
}
|
||||
}
|
||||
/* USER CODE END Control_Task */
|
||||
|
||||
static void Control_Init(Control_Info_Typedef *Control_Info){
|
||||
|
||||
PID_Init(&Chassis_PID,PID_POSITION,Chassis_PID_Param);
|
||||
|
||||
}
|
||||
|
||||
static void Control_Measure_Update(Control_Info_Typedef *Control_Info){
|
||||
|
||||
Control_Info->Measure.Chassis_Velocity = Chassis_Motor[0].Data.Velocity;
|
||||
|
||||
}
|
||||
|
||||
static void Control_Target_Update(Control_Info_Typedef *Control_Info){
|
||||
|
||||
Control_Info->Target.Chassis_Velocity = remote_ctrl.rc.ch[3] * 5.f;
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void Control_Info_Update(Control_Info_Typedef *Control_Info){
|
||||
|
||||
PID_Calculate(&Chassis_PID, Control_Info->Target.Chassis_Velocity, Control_Info->Measure.Chassis_Velocity);
|
||||
|
||||
Control_Info->SendValue[0] = (int16_t)(Chassis_PID.Output);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
82
Bsp/Inc/bsp_can.h
Normal file
82
Bsp/Inc/bsp_can.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : bsp_can.h
|
||||
* @brief : The header file of bsp_can.c
|
||||
* @author : YueLu
|
||||
* @date : 2026/04/28
|
||||
* @version : v1.0
|
||||
******************************************************************************
|
||||
* @attention : 复制自跃鹿战队,Testing......
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#ifndef BSP_CAN_H
|
||||
#define BSP_CAN_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "can.h"
|
||||
|
||||
// 最多能够支持的CAN设备数
|
||||
#define CAN_MX_REGISTER_CNT 12 // 这个数量取决于CAN总线的负载
|
||||
#define MX_CAN_FILTER_CNT (2 * 14) // 最多可以使用的CAN过滤器数量,目前远不会用到这么多
|
||||
#define DEVICE_CAN_CNT 2 // 根据板子设定,F407IG有CAN1,CAN2,因此为2;F334只有一个,则设为1
|
||||
// 如果只有1个CAN,还需要把bsp_can.c中所有的hcan2变量改为hcan1(别担心,主要是总线和FIFO的负载均衡,不影响功能)
|
||||
|
||||
/* can instance typedef, every module registered to CAN should have this variable */
|
||||
#pragma pack(1)
|
||||
typedef struct
|
||||
{
|
||||
CAN_HandleTypeDef *can_handle; // can句柄
|
||||
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
|
||||
uint32_t tx_id; // 发送id
|
||||
uint32_t tx_mailbox; // CAN消息填入的邮箱号
|
||||
uint8_t tx_buff[8]; // 发送缓存,发送消息长度可以通过CANSetDLC()设定,最大为8
|
||||
uint8_t rx_buff[8]; // 接收缓存,最大消息长度为8
|
||||
uint32_t rx_id; // 接收id
|
||||
uint8_t rx_len; // 接收长度,可能为0-8
|
||||
// 接收的回调函数,用于解析接收到的数据
|
||||
void (*can_module_callback)(struct _ *); // callback needs an instance to tell among registered ones
|
||||
void *id; // 使用can外设的模块指针(即id指向的模块拥有此can实例,是父子关系)
|
||||
} CANInstance;
|
||||
#pragma pack()
|
||||
|
||||
/* CAN实例初始化结构体,将此结构体指针传入注册函数 */
|
||||
typedef struct
|
||||
{
|
||||
CAN_HandleTypeDef *can_handle; // can句柄
|
||||
uint32_t tx_id; // 发送id
|
||||
uint32_t rx_id; // 接收id
|
||||
void (*can_module_callback)(CANInstance *); // 处理接收数据的回调函数
|
||||
void *id; // 拥有can实例的模块地址,用于区分不同的模块(如果有需要的话),如果不需要可以不传入
|
||||
} CAN_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief Register a module to CAN service,remember to call this before using a CAN device
|
||||
* 注册(初始化)一个can实例,需要传入初始化配置的指针.
|
||||
* @param config init config
|
||||
* @return CANInstance* can instance owned by module
|
||||
*/
|
||||
CANInstance *CANRegister(CAN_Init_Config_s *config);
|
||||
|
||||
/**
|
||||
* @brief 修改CAN发送报文的数据帧长度;注意最大长度为8,在没有进行修改的时候,默认长度为8
|
||||
*
|
||||
* @param _instance 要修改长度的can实例
|
||||
* @param length 设定长度
|
||||
*/
|
||||
void CANSetDLC(CANInstance *_instance, uint8_t length);
|
||||
|
||||
/**
|
||||
* @brief transmit mesg through CAN device,通过can实例发送消息
|
||||
* 发送前需要向CAN实例的tx_buff写入发送数据
|
||||
*
|
||||
* @attention 超时时间不应该超过调用此函数的任务的周期,否则会导致任务阻塞
|
||||
*
|
||||
* @param timeout 超时时间,单位为ms;后续改为us,获得更精确的控制
|
||||
* @param _instance* can instance owned by module
|
||||
*/
|
||||
uint8_t CANTransmit(CANInstance *_instance,float timeout);
|
||||
|
||||
#endif
|
||||
214
Bsp/Src/bsp_can.c
Normal file
214
Bsp/Src/bsp_can.c
Normal file
@@ -0,0 +1,214 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : bsp_can.c
|
||||
* @brief : bsp can functions
|
||||
* @author : YueLu
|
||||
* @date : 2026/04/28
|
||||
* @version : v1.0
|
||||
******************************************************************************
|
||||
* @attention : 复制自跃鹿战队,进行了部分修改,Testing......
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "bsp_can.h"
|
||||
#include "main.h"
|
||||
|
||||
/**
|
||||
* @brief can instance ptrs storage, used for recv callback
|
||||
* 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
|
||||
* @todo: 后续为每个CAN总线单独添加一个can_instance指针数组,提高回调查找的性能
|
||||
*/
|
||||
|
||||
static CANInstance *can_instance[CAN_MX_REGISTER_CNT] = {NULL};
|
||||
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
|
||||
|
||||
/* ----------------two static function called by CANRegister()-------------------- */
|
||||
|
||||
/**
|
||||
* @brief 添加过滤器以实现对特定id的报文的接收,会被CANRegister()调用
|
||||
* 给CAN添加过滤器后,BxCAN会根据接收到的报文的id进行消息过滤,符合规则的id会被填入FIFO触发中断
|
||||
*
|
||||
* @note f407的bxCAN有28个过滤器,这里将其配置为前14个过滤器给CAN1使用,后14个被CAN2使用
|
||||
* 初始化时,奇数id的模块会被分配到FIFO0,偶数id的模块会被分配到FIFO1
|
||||
* 注册到CAN1的模块使用过滤器0-13,CAN2使用过滤器14-27
|
||||
*
|
||||
* @attention 你不需要完全理解这个函数的作用,因为它主要是用于初始化,在开发过程中不需要关心底层的实现
|
||||
* 享受开发的乐趣吧!如果你真的想知道这个函数在干什么,请联系作者或自己查阅资料(请直接查阅官方的reference manual)
|
||||
*
|
||||
* @param _instance can instance owned by specific module
|
||||
*/
|
||||
|
||||
static void CANAddFilter(CANInstance *_instance)
|
||||
{
|
||||
CAN_FilterTypeDef can_filter_conf;
|
||||
static uint8_t can1_filter_idx = 0, can2_filter_idx = 14; // 0-13给can1用,14-27给can2用
|
||||
|
||||
can_filter_conf.FilterMode = CAN_FILTERMODE_IDLIST; // 使用id list模式,即只有将rxid添加到过滤器中才会接收到,其他报文会被过滤
|
||||
can_filter_conf.FilterScale = CAN_FILTERSCALE_16BIT; // 使用16位id模式,即只有低16位有效
|
||||
can_filter_conf.FilterFIFOAssignment = (_instance->tx_id & 1) ? CAN_RX_FIFO0 : CAN_RX_FIFO1; // 奇数id的模块会被分配到FIFO0,偶数id的模块会被分配到FIFO1
|
||||
can_filter_conf.SlaveStartFilterBank = 14; // 从第14个过滤器开始配置从机过滤器(在STM32的BxCAN控制器中CAN2是CAN1的从机)
|
||||
can_filter_conf.FilterIdLow = _instance->rx_id << 5; // 过滤器寄存器的低16位,因为使用STDID,所以只有低11位有效,高5位要填0
|
||||
can_filter_conf.FilterBank = _instance->can_handle == &hcan1 ? (can1_filter_idx++) : (can2_filter_idx++); // 根据can_handle判断是CAN1还是CAN2,然后自增
|
||||
can_filter_conf.FilterActivation = CAN_FILTER_ENABLE; // 启用过滤器
|
||||
|
||||
HAL_CAN_ConfigFilter(_instance->can_handle, &can_filter_conf);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 在第一个CAN实例初始化的时候会自动调用此函数,启动CAN服务
|
||||
*
|
||||
* @note 此函数会启动CAN1和CAN2,开启CAN1和CAN2的FIFO0 & FIFO1溢出通知
|
||||
*
|
||||
*/
|
||||
static void CANServiceInit()
|
||||
{
|
||||
HAL_CAN_Start(&hcan1);
|
||||
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);
|
||||
HAL_CAN_Start(&hcan2);
|
||||
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
|
||||
}
|
||||
|
||||
/* ----------------------- two extern callable function -----------------------*/
|
||||
|
||||
CANInstance *CANRegister(CAN_Init_Config_s *config)
|
||||
{
|
||||
if (!idx)
|
||||
{
|
||||
CANServiceInit(); // 第一次注册,先进行硬件初始化
|
||||
//LOGINFO("[bsp_can] CAN Service Init");
|
||||
//跃鹿的Log输出使用了SEGGER_RTT,需要配合Ozone使用(虽然Ozone很好用),考虑到我们使用keil5进行开发,后续将会修改为UART输出或USB-CDC输出
|
||||
}
|
||||
if (idx >= CAN_MX_REGISTER_CNT) // 超过最大实例数
|
||||
{
|
||||
while (1)
|
||||
//LOGERROR("[bsp_can] CAN instance exceeded MAX num, consider balance the load of CAN bus");
|
||||
}
|
||||
for (size_t i = 0; i < idx; i++)
|
||||
{ // 重复注册 | id重复
|
||||
if (can_instance[i]->rx_id == config->rx_id && can_instance[i]->can_handle == config->can_handle)
|
||||
{
|
||||
while (1)
|
||||
//LOGERROR("[}bsp_can] CAN id crash ,tx [%d] or rx [%d] already registered", &config->tx_id, &config->rx_id);
|
||||
}
|
||||
}
|
||||
|
||||
CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
|
||||
memset(instance, 0, sizeof(CANInstance)); // 分配的空间未必是0,所以要先清空
|
||||
// 进行发送报文的配置
|
||||
instance->txconf.StdId = config->tx_id; // 发送id
|
||||
instance->txconf.IDE = CAN_ID_STD; // 使用标准id,扩展id则使用CAN_ID_EXT(目前没有需求)
|
||||
instance->txconf.RTR = CAN_RTR_DATA; // 发送数据帧
|
||||
instance->txconf.DLC = 0x08; // 默认发送长度为8
|
||||
// 设置回调函数和接收发送id
|
||||
instance->can_handle = config->can_handle;
|
||||
instance->tx_id = config->tx_id; // 好像没用,可以删掉
|
||||
instance->rx_id = config->rx_id;
|
||||
instance->can_module_callback = config->can_module_callback;
|
||||
instance->id = config->id;
|
||||
|
||||
CANAddFilter(instance); // 添加CAN过滤器规则
|
||||
can_instance[idx++] = instance; // 将实例保存到can_instance中
|
||||
|
||||
return instance; // 返回can实例指针
|
||||
}
|
||||
|
||||
/* @todo 目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
|
||||
/* 如果让CANinstance保存txbuff,会增加一次复制的开销 */
|
||||
uint8_t CANTransmit(CANInstance *_instance, float timeout)
|
||||
{
|
||||
static uint32_t busy_count;
|
||||
static volatile float wait_time __attribute__((unused)); // for cancel warning
|
||||
float dwt_start = DWT_GetTimeline_ms();
|
||||
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0) // 等待邮箱空闲
|
||||
{
|
||||
if (DWT_GetTimeline_ms() - dwt_start > timeout) // 超时
|
||||
{
|
||||
//LOGWARNING("[bsp_can] CAN MAILbox full! failed to add msg to mailbox. Cnt [%d]", busy_count);
|
||||
busy_count++;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
wait_time = DWT_GetTimeline_ms() - dwt_start;
|
||||
// tx_mailbox会保存实际填入了这一帧消息的邮箱,但是知道是哪个邮箱发的似乎也没啥用
|
||||
if (HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox))
|
||||
{
|
||||
//LOGWARNING("[bsp_can] CAN bus BUS! cnt:%d", busy_count);
|
||||
busy_count++;
|
||||
return 0;
|
||||
}
|
||||
return 1; // 发送成功
|
||||
}
|
||||
|
||||
void CANSetDLC(CANInstance *_instance, uint8_t length)
|
||||
{
|
||||
// 发送长度错误!检查调用参数是否出错,或出现野指针/越界访问
|
||||
if (length > 8 || length == 0) // 安全检查
|
||||
while (1)
|
||||
//LOGERROR("[bsp_can] CAN DLC error! check your code or wild pointer");
|
||||
_instance->txconf.DLC = length;
|
||||
}
|
||||
|
||||
/* -----------------------belows are callback definitions--------------------------*/
|
||||
|
||||
/**
|
||||
* @brief 此函数会被下面两个函数调用,用于处理FIFO0和FIFO1溢出中断(说明收到了新的数据)
|
||||
* 所有的实例都会被遍历,找到can_handle和rx_id相等的实例时,调用该实例的回调函数
|
||||
*
|
||||
* @param _hcan
|
||||
* @param fifox passed to HAL_CAN_GetRxMessage() to get mesg from a specific fifo
|
||||
*/
|
||||
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
|
||||
{
|
||||
static CAN_RxHeaderTypeDef rxconf; // 同上
|
||||
uint8_t can_rx_buff[8];
|
||||
while (HAL_CAN_GetRxFifoFillLevel(_hcan, fifox)) // FIFO不为空,有可能在其他中断时有多帧数据进入
|
||||
{
|
||||
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff); // 从FIFO中获取数据
|
||||
for (size_t i = 0; i < idx; ++i)
|
||||
{ // 两者相等说明这是要找的实例
|
||||
if (_hcan == can_instance[i]->can_handle && rxconf.StdId == can_instance[i]->rx_id)
|
||||
{
|
||||
if (can_instance[i]->can_module_callback != NULL) // 回调函数不为空就调用
|
||||
{
|
||||
can_instance[i]->rx_len = rxconf.DLC; // 保存接收到的数据长度
|
||||
memcpy(can_instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
|
||||
can_instance[i]->can_module_callback(can_instance[i]); // 触发回调进行数据解析和处理
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 注意,STM32的两个CAN设备共享两个FIFO
|
||||
* 下面两个函数是HAL库中的回调函数,他们被HAL声明为__weak,这里对他们进行重载(重写)
|
||||
* 当FIFO0或FIFO1溢出时会调用这两个函数
|
||||
*/
|
||||
// 下面的函数会调用CANFIFOxCallback()来进一步处理来自特定CAN设备的消息
|
||||
|
||||
/**
|
||||
* @brief rx fifo callback. Once FIFO_0 is full,this func would be called
|
||||
*
|
||||
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_0 comes from
|
||||
*/
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
||||
{
|
||||
CANFIFOxCallback(hcan, CAN_RX_FIFO0); // 调用我们自己写的函数来处理消息
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief rx fifo callback. Once FIFO_1 is full,this func would be called
|
||||
*
|
||||
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_1 comes from
|
||||
*/
|
||||
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
||||
{
|
||||
CANFIFOxCallback(hcan, CAN_RX_FIFO1); // 调用我们自己写的函数来处理消息
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -208,7 +208,7 @@
|
||||
|
||||
<Group>
|
||||
<GroupName>Application/User/Core</GroupName>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
@@ -964,7 +964,7 @@
|
||||
|
||||
<Group>
|
||||
<GroupName>Middlewares/USB_Device_Library</GroupName>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
|
||||
- 深圳北理莫斯科大学 `北极熊` 战队 [StandardRobot++](https://gitee.com/SMBU-POLARBEAR/StandardRobotpp)
|
||||
- 辽宁科技大学 `COD` 战队 [COD战队电控通用控制系统](https://gitee.com/wangcaofan/cod-h7-template)
|
||||
- 湖南大学 `跃鹿` 战队
|
||||
@@ -5,11 +5,11 @@
|
||||
本项目采取Google的开源代码风格
|
||||
|
||||
### 编译要求
|
||||
最终编译结果要做到 `0-error,0-warning` ,本项目启用了 `--diag_error=warning` 在编译时将 `warning` 视作 `error`。
|
||||
最终编译结果要做到 `0-error,0-warning` ,~~本项目启用了 `--diag_error=warning` 在编译时将 `warning` 视作 `error`。~~(开发中,暂时没有启用)
|
||||
|
||||
### 提交要求
|
||||
禁止将自己的 **开发插件** 夹带私货上传!
|
||||
如EIDE WorkSpace 等
|
||||
如 `EIDE WorkSpace` 等
|
||||
|
||||
## 硬件上的
|
||||
**禁止在DJI电机工作时开启调试!!!!**
|
||||
@@ -13,4 +13,6 @@
|
||||
|
||||
> (COD的代码框架是给达妙H7的,得自己移植)
|
||||
|
||||
> (跃鹿战队的框架各种内存越界)
|
||||
|
||||
> 综上,我受不了了,代码框架一定要自己的!
|
||||
|
||||
Reference in New Issue
Block a user