mirror of
https://github.com/goldenfishs/MRobot.git
synced 2025-11-02 20:43:11 +08:00
更新
This commit is contained in:
parent
b56d7bf8e0
commit
83aff179ee
12
bsp/bsp.h
12
bsp/bsp.h
@ -4,6 +4,14 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
#define BSP_OK (0)
|
#define BSP_OK (0)
|
||||||
#define BSP_ERR (-1)
|
#define BSP_ERR (-1)
|
||||||
#define BSP_ERR_NULL (-2)
|
#define BSP_ERR_NULL (-2)
|
||||||
@ -11,6 +19,10 @@ extern "C" {
|
|||||||
#define BSP_ERR_NO_DEV (-4)
|
#define BSP_ERR_NO_DEV (-4)
|
||||||
#define BSP_ERR_TIMEOUT (-5)
|
#define BSP_ERR_TIMEOUT (-5)
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
142
bsp/can.c
142
bsp/can.c
@ -5,11 +5,18 @@
|
|||||||
#include <cmsis_os2.h>
|
#include <cmsis_os2.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define CAN_QUEUE_MUTEX_TIMEOUT 100 /* 队列互斥锁超时时间(ms) */
|
#define CAN_QUEUE_MUTEX_TIMEOUT 100 /* 队列互斥锁超时时间(ms) */
|
||||||
#define CAN_TX_SEMAPHORE_TIMEOUT 1000 /* 发送信号量超时时间(ms) */
|
|
||||||
#define CAN_TX_MAILBOX_NUM 3 /* CAN发送邮箱数量 */
|
#define CAN_TX_MAILBOX_NUM 3 /* CAN发送邮箱数量 */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
typedef struct BSP_CAN_QueueNode {
|
typedef struct BSP_CAN_QueueNode {
|
||||||
@ -20,10 +27,13 @@ typedef struct BSP_CAN_QueueNode {
|
|||||||
struct BSP_CAN_QueueNode *next; /* 指向下一个节点的指针 */
|
struct BSP_CAN_QueueNode *next; /* 指向下一个节点的指针 */
|
||||||
} BSP_CAN_QueueNode_t;
|
} BSP_CAN_QueueNode_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static BSP_CAN_QueueNode_t *queue_list = NULL;
|
static BSP_CAN_QueueNode_t *queue_list = NULL;
|
||||||
static osMutexId_t queue_mutex = NULL;
|
static osMutexId_t queue_mutex = NULL;
|
||||||
static osSemaphoreId_t tx_semaphore[BSP_CAN_NUM] = {NULL}; /* 发送信号量,用于控制发送邮箱访问 */
|
|
||||||
static void (*CAN_Callback[BSP_CAN_NUM][BSP_CAN_CB_NUM])(void);
|
static void (*CAN_Callback[BSP_CAN_NUM][BSP_CAN_CB_NUM])(void);
|
||||||
static bool inited = false;
|
static bool inited = false;
|
||||||
static BSP_CAN_IdParser_t id_parser = NULL; /* ID解析器 */
|
static BSP_CAN_IdParser_t id_parser = NULL; /* ID解析器 */
|
||||||
@ -35,12 +45,13 @@ static int8_t BSP_CAN_CreateIdQueue(BSP_CAN_t can, uint32_t can_id, uint8_t queu
|
|||||||
static int8_t BSP_CAN_DeleteIdQueue(BSP_CAN_t can, uint32_t can_id);
|
static int8_t BSP_CAN_DeleteIdQueue(BSP_CAN_t can, uint32_t can_id);
|
||||||
static void BSP_CAN_RxFifo0Callback(void);
|
static void BSP_CAN_RxFifo0Callback(void);
|
||||||
static void BSP_CAN_RxFifo1Callback(void);
|
static void BSP_CAN_RxFifo1Callback(void);
|
||||||
static void BSP_CAN_TxCompleteCallback(CAN_HandleTypeDef *hcan);
|
|
||||||
static void BSP_CAN_TxAbortCallback(CAN_HandleTypeDef *hcan);
|
|
||||||
static BSP_CAN_FrameType_t BSP_CAN_GetFrameType(CAN_RxHeaderTypeDef *header);
|
static BSP_CAN_FrameType_t BSP_CAN_GetFrameType(CAN_RxHeaderTypeDef *header);
|
||||||
static uint32_t BSP_CAN_DefaultIdParser(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
static uint32_t BSP_CAN_DefaultIdParser(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
||||||
|
|
||||||
/* Private functions -------------------------------------------------------- */
|
/* Private functions -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 根据CAN句柄获取BSP_CAN实例
|
* @brief 根据CAN句柄获取BSP_CAN实例
|
||||||
@ -213,38 +224,10 @@ static void BSP_CAN_RxFifo1Callback(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 发送完成回调统一处理函数
|
|
||||||
*/
|
|
||||||
static void BSP_CAN_TxCompleteCallback(CAN_HandleTypeDef *hcan) {
|
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
|
||||||
// 释放发送信号量
|
|
||||||
if (tx_semaphore[bsp_can] != NULL) {
|
|
||||||
osSemaphoreRelease(tx_semaphore[bsp_can]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 发送中止回调统一处理函数
|
|
||||||
*/
|
|
||||||
static void BSP_CAN_TxAbortCallback(CAN_HandleTypeDef *hcan) {
|
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
|
||||||
// 释放发送信号量(发送中止也要释放)
|
|
||||||
if (tx_semaphore[bsp_can] != NULL) {
|
|
||||||
osSemaphoreRelease(tx_semaphore[bsp_can]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* HAL Callback Functions --------------------------------------------------- */
|
/* HAL Callback Functions --------------------------------------------------- */
|
||||||
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxCompleteCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_CPLT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_CPLT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_CPLT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_CPLT_CB]();
|
||||||
@ -254,8 +237,6 @@ void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
|
|||||||
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxCompleteCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_CPLT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_CPLT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_CPLT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_CPLT_CB]();
|
||||||
@ -265,8 +246,6 @@ void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan) {
|
|||||||
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxCompleteCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_CPLT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_CPLT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_CPLT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_CPLT_CB]();
|
||||||
@ -276,8 +255,6 @@ void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan) {
|
|||||||
void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxAbortCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_ABORT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_ABORT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_ABORT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX0_ABORT_CB]();
|
||||||
@ -287,8 +264,6 @@ void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan) {
|
|||||||
void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxAbortCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_ABORT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_ABORT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_ABORT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX1_ABORT_CB]();
|
||||||
@ -298,8 +273,6 @@ void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan) {
|
|||||||
void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan) {
|
void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan) {
|
||||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||||
if (bsp_can != BSP_CAN_ERR) {
|
if (bsp_can != BSP_CAN_ERR) {
|
||||||
// 自动释放发送信号量
|
|
||||||
BSP_CAN_TxAbortCallback(hcan);
|
|
||||||
// 调用用户回调
|
// 调用用户回调
|
||||||
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_ABORT_CB])
|
if (CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_ABORT_CB])
|
||||||
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_ABORT_CB]();
|
CAN_Callback[bsp_can][HAL_CAN_TX_MAILBOX2_ABORT_CB]();
|
||||||
@ -381,25 +354,6 @@ int8_t BSP_CAN_Init(void) {
|
|||||||
return BSP_ERR;
|
return BSP_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建发送信号量,每个CAN通道有3个发送邮箱
|
|
||||||
for (int i = 0; i < BSP_CAN_NUM; i++) {
|
|
||||||
tx_semaphore[i] = osSemaphoreNew(CAN_TX_MAILBOX_NUM, CAN_TX_MAILBOX_NUM, NULL);
|
|
||||||
if (tx_semaphore[i] == NULL) {
|
|
||||||
// 清理已创建的信号量
|
|
||||||
for (int j = 0; j < i; j++) {
|
|
||||||
if (tx_semaphore[j] != NULL) {
|
|
||||||
osSemaphoreDelete(tx_semaphore[j]);
|
|
||||||
tx_semaphore[j] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (queue_mutex != NULL) {
|
|
||||||
osMutexDelete(queue_mutex);
|
|
||||||
queue_mutex = NULL;
|
|
||||||
}
|
|
||||||
return BSP_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* AUTO GENERATED CAN_INIT */
|
/* AUTO GENERATED CAN_INIT */
|
||||||
|
|
||||||
inited = true;
|
inited = true;
|
||||||
@ -424,14 +378,6 @@ int8_t BSP_CAN_DeInit(void) {
|
|||||||
osMutexRelease(queue_mutex);
|
osMutexRelease(queue_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除发送信号量
|
|
||||||
for (int i = 0; i < BSP_CAN_NUM; i++) {
|
|
||||||
if (tx_semaphore[i] != NULL) {
|
|
||||||
osSemaphoreDelete(tx_semaphore[i]);
|
|
||||||
tx_semaphore[i] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除互斥锁
|
// 删除互斥锁
|
||||||
if (queue_mutex != NULL) {
|
if (queue_mutex != NULL) {
|
||||||
osMutexDelete(queue_mutex);
|
osMutexDelete(queue_mutex);
|
||||||
@ -494,20 +440,8 @@ int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format,
|
|||||||
return BSP_ERR;
|
return BSP_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取发送信号量,确保有可用的发送邮箱
|
|
||||||
if (tx_semaphore[can] == NULL) {
|
|
||||||
return BSP_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
osStatus_t sem_status = osSemaphoreAcquire(tx_semaphore[can], CAN_TX_SEMAPHORE_TIMEOUT);
|
|
||||||
if (sem_status != osOK) {
|
|
||||||
return BSP_ERR_TIMEOUT; // 获取信号量超时,表示发送邮箱已满
|
|
||||||
}
|
|
||||||
|
|
||||||
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle(can);
|
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle(can);
|
||||||
if (hcan == NULL) {
|
if (hcan == NULL) {
|
||||||
// 如果获取句柄失败,需要释放信号量
|
|
||||||
osSemaphoreRelease(tx_semaphore[can]);
|
|
||||||
return BSP_ERR_NULL;
|
return BSP_ERR_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -536,8 +470,6 @@ int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format,
|
|||||||
header.RTR = CAN_RTR_REMOTE;
|
header.RTR = CAN_RTR_REMOTE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// 如果格式错误,需要释放信号量
|
|
||||||
osSemaphoreRelease(tx_semaphore[can]);
|
|
||||||
return BSP_ERR;
|
return BSP_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -547,12 +479,9 @@ int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format,
|
|||||||
HAL_StatusTypeDef result = HAL_CAN_AddTxMessage(hcan, &header, data, &mailbox);
|
HAL_StatusTypeDef result = HAL_CAN_AddTxMessage(hcan, &header, data, &mailbox);
|
||||||
|
|
||||||
if (result != HAL_OK) {
|
if (result != HAL_OK) {
|
||||||
// 如果发送失败,需要释放信号量
|
|
||||||
osSemaphoreRelease(tx_semaphore[can]);
|
|
||||||
return BSP_ERR;
|
return BSP_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送成功,信号量将在发送完成回调中释放
|
|
||||||
return BSP_OK;
|
return BSP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -672,3 +601,44 @@ uint32_t BSP_CAN_ParseId(uint32_t original_id, BSP_CAN_FrameType_t frame_type) {
|
|||||||
}
|
}
|
||||||
return BSP_CAN_DefaultIdParser(original_id, frame_type);
|
return BSP_CAN_DefaultIdParser(original_id, frame_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t BSP_CAN_WaitTxMailboxEmpty(BSP_CAN_t can, uint32_t timeout) {
|
||||||
|
if (!inited) {
|
||||||
|
return BSP_ERR_INITED;
|
||||||
|
}
|
||||||
|
if (can >= BSP_CAN_NUM) {
|
||||||
|
return BSP_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle(can);
|
||||||
|
if (hcan == NULL) {
|
||||||
|
return BSP_ERR_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t start_time = HAL_GetTick();
|
||||||
|
|
||||||
|
// 如果超时时间为0,立即检查并返回
|
||||||
|
if (timeout == 0) {
|
||||||
|
uint32_t free_level = HAL_CAN_GetTxMailboxesFreeLevel(hcan);
|
||||||
|
return (free_level > 0) ? BSP_OK : BSP_ERR_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待至少有一个邮箱空闲
|
||||||
|
while (true) {
|
||||||
|
uint32_t free_level = HAL_CAN_GetTxMailboxesFreeLevel(hcan);
|
||||||
|
if (free_level > 0) {
|
||||||
|
return BSP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查超时
|
||||||
|
if (timeout != BSP_CAN_TIMEOUT_FOREVER) {
|
||||||
|
uint32_t elapsed = HAL_GetTick() - start_time;
|
||||||
|
if (elapsed >= timeout) {
|
||||||
|
return BSP_ERR_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 短暂延时,避免占用过多CPU
|
||||||
|
osDelay(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
26
bsp/can.h
26
bsp/can.h
@ -12,12 +12,19 @@ extern "C" {
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <cmsis_os.h>
|
#include <cmsis_os.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
#define BSP_CAN_MAX_DLC 8
|
#define BSP_CAN_MAX_DLC 8
|
||||||
#define BSP_CAN_DEFAULT_QUEUE_SIZE 10
|
#define BSP_CAN_DEFAULT_QUEUE_SIZE 10
|
||||||
#define BSP_CAN_TIMEOUT_IMMEDIATE 0
|
#define BSP_CAN_TIMEOUT_IMMEDIATE 0
|
||||||
#define BSP_CAN_TIMEOUT_FOREVER osWaitForever
|
#define BSP_CAN_TIMEOUT_FOREVER osWaitForever
|
||||||
#define CAN_TX_SEMAPHORE_TIMEOUT 1000 /* 发送信号量超时时间(ms) */
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
@ -94,6 +101,10 @@ typedef struct {
|
|||||||
/* ID解析回调函数类型 */
|
/* ID解析回调函数类型 */
|
||||||
typedef uint32_t (*BSP_CAN_IdParser_t)(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
typedef uint32_t (*BSP_CAN_IdParser_t)(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,6 +172,14 @@ int8_t BSP_CAN_TransmitExtDataFrame(BSP_CAN_t can, BSP_CAN_ExtDataFrame_t *frame
|
|||||||
*/
|
*/
|
||||||
int8_t BSP_CAN_TransmitRemoteFrame(BSP_CAN_t can, BSP_CAN_RemoteFrame_t *frame);
|
int8_t BSP_CAN_TransmitRemoteFrame(BSP_CAN_t can, BSP_CAN_RemoteFrame_t *frame);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 等待CAN发送邮箱空闲
|
||||||
|
* @param can CAN 枚举
|
||||||
|
* @param timeout 超时时间(毫秒),0为立即返回,osWaitForever为永久等待
|
||||||
|
* @return BSP_OK 成功,其他值失败
|
||||||
|
*/
|
||||||
|
int8_t BSP_CAN_WaitTxMailboxEmpty(BSP_CAN_t can, uint32_t timeout);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册 CAN ID 接收队列
|
* @brief 注册 CAN ID 接收队列
|
||||||
* @param can CAN 枚举
|
* @param can CAN 枚举
|
||||||
@ -225,6 +244,11 @@ int8_t BSP_CAN_UnregisterIdParser(void);
|
|||||||
*/
|
*/
|
||||||
uint32_t BSP_CAN_ParseId(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
uint32_t BSP_CAN_ParseId(uint32_t original_id, BSP_CAN_FrameType_t frame_type);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
16
bsp/dwt.c
16
bsp/dwt.c
@ -12,6 +12,18 @@
|
|||||||
*/
|
*/
|
||||||
#include "bsp/dwt.h"
|
#include "bsp/dwt.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
DWT_Time_t SysTime;
|
DWT_Time_t SysTime;
|
||||||
static uint32_t CPU_FREQ_Hz, CPU_FREQ_Hz_ms, CPU_FREQ_Hz_us;
|
static uint32_t CPU_FREQ_Hz, CPU_FREQ_Hz_ms, CPU_FREQ_Hz_us;
|
||||||
static uint32_t CYCCNT_RountCount;
|
static uint32_t CYCCNT_RountCount;
|
||||||
@ -19,6 +31,10 @@ static uint32_t CYCCNT_LAST;
|
|||||||
uint64_t CYCCNT64;
|
uint64_t CYCCNT64;
|
||||||
static void DWT_CNT_Update(void);
|
static void DWT_CNT_Update(void);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
void DWT_Init(uint32_t CPU_Freq_mHz)
|
void DWT_Init(uint32_t CPU_Freq_mHz)
|
||||||
{
|
{
|
||||||
/* 使能DWT外设 */
|
/* 使能DWT外设 */
|
||||||
|
|||||||
16
bsp/dwt.h
16
bsp/dwt.h
@ -16,6 +16,14 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint32_t s;
|
uint32_t s;
|
||||||
@ -23,6 +31,10 @@ typedef struct
|
|||||||
uint16_t us;
|
uint16_t us;
|
||||||
} DWT_Time_t;
|
} DWT_Time_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
void DWT_Init(uint32_t CPU_Freq_mHz);
|
void DWT_Init(uint32_t CPU_Freq_mHz);
|
||||||
float DWT_GetDeltaT(uint32_t *cnt_last);
|
float DWT_GetDeltaT(uint32_t *cnt_last);
|
||||||
double DWT_GetDeltaT64(uint32_t *cnt_last);
|
double DWT_GetDeltaT64(uint32_t *cnt_last);
|
||||||
@ -34,4 +46,8 @@ void DWT_SysTimeUpdate(void);
|
|||||||
|
|
||||||
extern DWT_Time_t SysTime;
|
extern DWT_Time_t SysTime;
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#endif /* DWT_H_ */
|
#endif /* DWT_H_ */
|
||||||
|
|||||||
16
bsp/gpio.c
16
bsp/gpio.c
@ -4,7 +4,15 @@
|
|||||||
#include <gpio.h>
|
#include <gpio.h>
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -12,6 +20,10 @@ typedef struct {
|
|||||||
GPIO_TypeDef *gpio;
|
GPIO_TypeDef *gpio;
|
||||||
} BSP_GPIO_MAP_t;
|
} BSP_GPIO_MAP_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static const BSP_GPIO_MAP_t GPIO_Map[BSP_GPIO_NUM] = {
|
static const BSP_GPIO_MAP_t GPIO_Map[BSP_GPIO_NUM] = {
|
||||||
/* AUTO GENERATED BSP_GPIO_MAP */
|
/* AUTO GENERATED BSP_GPIO_MAP */
|
||||||
@ -20,6 +32,10 @@ static const BSP_GPIO_MAP_t GPIO_Map[BSP_GPIO_NUM] = {
|
|||||||
static void (*GPIO_Callback[16])(void);
|
static void (*GPIO_Callback[16])(void);
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||||
for (uint8_t i = 0; i < 16; i++) {
|
for (uint8_t i = 0; i < 16; i++) {
|
||||||
if (GPIO_Pin & (1 << i)) {
|
if (GPIO_Pin & (1 << i)) {
|
||||||
|
|||||||
12
bsp/gpio.h
12
bsp/gpio.h
@ -10,8 +10,16 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/bsp.h"
|
#include "bsp/bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
/* AUTO GENERATED BSP_GPIO_ENUM */
|
/* AUTO GENERATED BSP_GPIO_ENUM */
|
||||||
@ -30,6 +38,10 @@ int8_t BSP_GPIO_TogglePin(BSP_GPIO_t gpio);
|
|||||||
|
|
||||||
bool BSP_GPIO_ReadPin(BSP_GPIO_t gpio);
|
bool BSP_GPIO_ReadPin(BSP_GPIO_t gpio);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
12
bsp/i2c.c
12
bsp/i2c.c
@ -1,9 +1,21 @@
|
|||||||
/* Includes ----------------------------------------------------------------- */
|
/* Includes ----------------------------------------------------------------- */
|
||||||
#include "bsp\i2c.h"
|
#include "bsp\i2c.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static void (*I2C_Callback[BSP_I2C_NUM][BSP_I2C_CB_NUM])(void);
|
static void (*I2C_Callback[BSP_I2C_NUM][BSP_I2C_CB_NUM])(void);
|
||||||
|
|
||||||
|
|||||||
11
bsp/i2c.h
11
bsp/i2c.h
@ -11,8 +11,16 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/bsp.h"
|
#include "bsp/bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
|
|
||||||
/* 要添加使用I2C的新设备,需要先在此添加对应的枚举值 */
|
/* 要添加使用I2C的新设备,需要先在此添加对应的枚举值 */
|
||||||
@ -61,6 +69,9 @@ int8_t BSP_I2C_MemRead(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
|||||||
int8_t BSP_I2C_MemWrite(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
int8_t BSP_I2C_MemWrite(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||||
uint8_t *data, uint16_t size, bool dma);
|
uint8_t *data, uint16_t size, bool dma);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
18
bsp/mm.c
18
bsp/mm.c
@ -1,14 +1,30 @@
|
|||||||
/* Includes ----------------------------------------------------------------- */
|
/* Includes ----------------------------------------------------------------- */
|
||||||
#include "bsp\mm.h"
|
#include "bsp/mm.h"
|
||||||
|
|
||||||
#include "FreeRTOS.h"
|
#include "FreeRTOS.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
inline void *BSP_Malloc(size_t size) { return pvPortMalloc(size); }
|
inline void *BSP_Malloc(size_t size) { return pvPortMalloc(size); }
|
||||||
|
|
||||||
inline void BSP_Free(void *pv) { vPortFree(pv); }
|
inline void BSP_Free(void *pv) { vPortFree(pv); }
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
12
bsp/mm.h
12
bsp/mm.h
@ -8,13 +8,25 @@ extern "C" {
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
void *BSP_Malloc(size_t size);
|
void *BSP_Malloc(size_t size);
|
||||||
void BSP_Free(void *pv);
|
void BSP_Free(void *pv);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
12
bsp/pwm.c
12
bsp/pwm.c
@ -3,7 +3,15 @@
|
|||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
#include "bsp.h"
|
#include "bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -11,6 +19,10 @@ typedef struct {
|
|||||||
uint16_t channel;
|
uint16_t channel;
|
||||||
} BSP_PWM_Config_t;
|
} BSP_PWM_Config_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static const BSP_PWM_Config_t PWM_Map[BSP_PWM_NUM] = {
|
static const BSP_PWM_Config_t PWM_Map[BSP_PWM_NUM] = {
|
||||||
/* AUTO GENERATED BSP_PWM_MAP */
|
/* AUTO GENERATED BSP_PWM_MAP */
|
||||||
|
|||||||
12
bsp/pwm.h
12
bsp/pwm.h
@ -9,9 +9,17 @@ extern "C" {
|
|||||||
#include "tim.h"
|
#include "tim.h"
|
||||||
#include "bsp.h"
|
#include "bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
/* PWM通道 */
|
/* PWM通道 */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -31,6 +39,10 @@ TIM_HandleTypeDef* BSP_PWM_GetHandle(BSP_PWM_Channel_t ch);
|
|||||||
int8_t BSP_PWM_Start_DMA(BSP_PWM_Channel_t ch, uint32_t *pData, uint16_t Length);
|
int8_t BSP_PWM_Start_DMA(BSP_PWM_Channel_t ch, uint32_t *pData, uint16_t Length);
|
||||||
int8_t BSP_PWM_Stop_DMA(BSP_PWM_Channel_t ch);
|
int8_t BSP_PWM_Stop_DMA(BSP_PWM_Channel_t ch);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
16
bsp/spi.c
16
bsp/spi.c
@ -2,9 +2,21 @@
|
|||||||
#include <spi.h>
|
#include <spi.h>
|
||||||
#include "bsp/spi.h"
|
#include "bsp/spi.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static void (*SPI_Callback[BSP_SPI_NUM][BSP_SPI_CB_NUM])(void);
|
static void (*SPI_Callback[BSP_SPI_NUM][BSP_SPI_CB_NUM])(void);
|
||||||
|
|
||||||
@ -163,3 +175,7 @@ int8_t BSP_SPI_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size
|
|||||||
BSP_SPI_Transmit(spi, ®, 1u, true);
|
BSP_SPI_Transmit(spi, ®, 1u, true);
|
||||||
return BSP_SPI_Transmit(spi, data, size, true);
|
return BSP_SPI_Transmit(spi, data, size, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
12
bsp/spi.h
12
bsp/spi.h
@ -11,8 +11,16 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/bsp.h"
|
#include "bsp/bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
|
|
||||||
/* 要添加使用SPI的新设备,需要先在此添加对应的枚举值 */
|
/* 要添加使用SPI的新设备,需要先在此添加对应的枚举值 */
|
||||||
@ -53,6 +61,10 @@ int8_t BSP_SPI_MemWriteByte(BSP_SPI_t spi, uint8_t reg, uint8_t data);
|
|||||||
int8_t BSP_SPI_MemRead(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
int8_t BSP_SPI_MemRead(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
||||||
int8_t BSP_SPI_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
int8_t BSP_SPI_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
16
bsp/time.c
16
bsp/time.c
@ -6,9 +6,21 @@
|
|||||||
#include "FreeRTOS.h"
|
#include "FreeRTOS.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "task.h"
|
#include "task.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
@ -63,3 +75,7 @@ int8_t BSP_TIME_Delay_us(uint32_t us) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int8_t BSP_TIME_Delay(uint32_t ms) __attribute__((alias("BSP_TIME_Delay_ms")));
|
int8_t BSP_TIME_Delay(uint32_t ms) __attribute__((alias("BSP_TIME_Delay_ms")));
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
12
bsp/time.h
12
bsp/time.h
@ -9,8 +9,16 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/bsp.h"
|
#include "bsp/bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
uint32_t BSP_TIME_Get_ms();
|
uint32_t BSP_TIME_Get_ms();
|
||||||
@ -26,6 +34,10 @@ int8_t BSP_TIME_Delay_us(uint32_t us);
|
|||||||
|
|
||||||
int8_t BSP_TIME_Delay(uint32_t ms);
|
int8_t BSP_TIME_Delay(uint32_t ms);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
16
bsp/uart.c
16
bsp/uart.c
@ -3,9 +3,21 @@
|
|||||||
|
|
||||||
#include "bsp/uart.h"
|
#include "bsp/uart.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static void (*UART_Callback[BSP_UART_NUM][BSP_UART_CB_NUM])(void);
|
static void (*UART_Callback[BSP_UART_NUM][BSP_UART_CB_NUM])(void);
|
||||||
|
|
||||||
@ -135,3 +147,7 @@ int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma)
|
|||||||
return HAL_UART_Receive_IT(BSP_UART_GetHandle(uart), data, size);
|
return HAL_UART_Receive_IT(BSP_UART_GetHandle(uart), data, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
12
bsp/uart.h
12
bsp/uart.h
@ -11,8 +11,16 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/bsp.h"
|
#include "bsp/bsp.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
|
|
||||||
/* 要添加使用UART的新设备,需要先在此添加对应的枚举值 */
|
/* 要添加使用UART的新设备,需要先在此添加对应的枚举值 */
|
||||||
@ -51,6 +59,10 @@ int8_t BSP_UART_RegisterCallback(BSP_UART_t uart, BSP_UART_Callback_t type,
|
|||||||
int8_t BSP_UART_Transmit(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
int8_t BSP_UART_Transmit(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
||||||
int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -9,9 +9,17 @@
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
#define BETA_IMU (0.033f)
|
#define BETA_IMU (0.033f)
|
||||||
#define BETA_AHRS (0.041f)
|
#define BETA_AHRS (0.041f)
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* 2 * proportional gain (Kp) */
|
/* 2 * proportional gain (Kp) */
|
||||||
static float beta = BETA_IMU;
|
static float beta = BETA_IMU;
|
||||||
|
|
||||||
@ -403,3 +411,7 @@ int8_t AHRS_GetEulr(AHRS_Eulr_t *eulr, const AHRS_t *ahrs) {
|
|||||||
* \param eulr 被操作的数据
|
* \param eulr 被操作的数据
|
||||||
*/
|
*/
|
||||||
void AHRS_ResetEulr(AHRS_Eulr_t *eulr) { memset(eulr, 0, sizeof(*eulr)); }
|
void AHRS_ResetEulr(AHRS_Eulr_t *eulr) { memset(eulr, 0, sizeof(*eulr)); }
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -11,6 +11,14 @@ extern "C" {
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* 欧拉角(Euler angle) */
|
/* 欧拉角(Euler angle) */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float yaw; /* 偏航角(Yaw angle) */
|
float yaw; /* 偏航角(Yaw angle) */
|
||||||
@ -55,6 +63,10 @@ typedef struct {
|
|||||||
float inv_sample_freq; /* 采样频率的的倒数 */
|
float inv_sample_freq; /* 采样频率的的倒数 */
|
||||||
} AHRS_t;
|
} AHRS_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化姿态解算
|
* @brief 初始化姿态解算
|
||||||
*
|
*
|
||||||
@ -93,6 +105,10 @@ int8_t AHRS_GetEulr(AHRS_Eulr_t *eulr, const AHRS_t *ahrs);
|
|||||||
*/
|
*/
|
||||||
void AHRS_ResetEulr(AHRS_Eulr_t *eulr);
|
void AHRS_ResetEulr(AHRS_Eulr_t *eulr);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -5,6 +5,14 @@
|
|||||||
|
|
||||||
#include "capacity.h"
|
#include "capacity.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 通过电压计算电池剩余电量
|
* @brief 通过电压计算电池剩余电量
|
||||||
*
|
*
|
||||||
@ -56,3 +64,7 @@ float Capacity_GetCapacitorRemain(float vcap, float vbat, float v_cutoff) {
|
|||||||
|
|
||||||
return percentage;
|
return percentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -12,6 +12,14 @@ extern "C" {
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 通过电压计算电池剩余电量
|
* @brief 通过电压计算电池剩余电量
|
||||||
*
|
*
|
||||||
@ -30,6 +38,10 @@ float Capacity_GetBatteryRemain(float volt);
|
|||||||
*/
|
*/
|
||||||
float Capacity_GetCapacitorRemain(float vcap, float vbat, float v_cutoff);
|
float Capacity_GetCapacitorRemain(float vcap, float vbat, float v_cutoff);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,6 +6,14 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 行为转换为对应按键
|
* @brief 行为转换为对应按键
|
||||||
*
|
*
|
||||||
@ -373,3 +381,7 @@ int8_t CMD_RefereeAdd(CMD_RefereeCmd_t *ref, CMD_UI_t cmd) {
|
|||||||
ref->counter++;
|
ref->counter++;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -13,7 +13,15 @@ extern "C" {
|
|||||||
|
|
||||||
#include "component/ahrs.h"
|
#include "component/ahrs.h"
|
||||||
|
|
||||||
#define CMD_REFEREE_MAX_NUM (3) /* 发送命令限定的最大数量 */
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
#define CMD_REFEREE_MAX_NUM (3) /* Lines 16 omitted */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* 机器人型号 */
|
/* 机器人型号 */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -301,6 +309,10 @@ int8_t CMD_ParseHost(const CMD_Host_t *host, CMD_t *cmd, float dt_sec);
|
|||||||
*/
|
*/
|
||||||
int8_t CMD_RefereeAdd(CMD_RefereeCmd_t *ref, CMD_UI_t cmd);
|
int8_t CMD_RefereeAdd(CMD_RefereeCmd_t *ref, CMD_UI_t cmd);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,5 +1,13 @@
|
|||||||
#include "crc16.h"
|
#include "crc16.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
static const uint16_t crc16_tab[256] = {
|
static const uint16_t crc16_tab[256] = {
|
||||||
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48,
|
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48,
|
||||||
0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108,
|
0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108,
|
||||||
@ -48,3 +56,7 @@ bool CRC16_Verify(const uint8_t *buf, size_t len) {
|
|||||||
((const uint16_t *)((const uint8_t *)buf +
|
((const uint16_t *)((const uint8_t *)buf +
|
||||||
(len % 2)))[len / sizeof(uint16_t) - 1];
|
(len % 2)))[len / sizeof(uint16_t) - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -8,11 +8,23 @@ extern "C" {
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
#define CRC16_INIT 0XFFFF
|
#define CRC16_INIT 0XFFFF
|
||||||
|
|
||||||
uint16_t CRC16_Calc(const uint8_t *buf, size_t len, uint16_t crc);
|
uint16_t CRC16_Calc(const uint8_t *buf, size_t len, uint16_t crc);
|
||||||
bool CRC16_Verify(const uint8_t *buf, size_t len);
|
bool CRC16_Verify(const uint8_t *buf, size_t len);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,5 +1,13 @@
|
|||||||
#include "crc8.h"
|
#include "crc8.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
static const uint8_t crc8_tab[256] = {
|
static const uint8_t crc8_tab[256] = {
|
||||||
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20,
|
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20,
|
||||||
0xa3, 0xfd, 0x1f, 0x41, 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
|
0xa3, 0xfd, 0x1f, 0x41, 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
|
||||||
@ -38,3 +46,7 @@ bool CRC8_Verify(const uint8_t *buf, size_t len) {
|
|||||||
uint8_t expected = CRC8_Calc(buf, len - sizeof(uint8_t), CRC8_INIT);
|
uint8_t expected = CRC8_Calc(buf, len - sizeof(uint8_t), CRC8_INIT);
|
||||||
return expected == buf[len - sizeof(uint8_t)];
|
return expected == buf[len - sizeof(uint8_t)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -8,11 +8,23 @@ extern "C" {
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
#define CRC8_INIT 0xFF
|
#define CRC8_INIT 0xFF
|
||||||
|
|
||||||
uint8_t CRC8_Calc(const uint8_t *buf, size_t len, uint8_t crc);
|
uint8_t CRC8_Calc(const uint8_t *buf, size_t len, uint8_t crc);
|
||||||
bool CRC8_Verify(const uint8_t *buf, size_t len);
|
bool CRC8_Verify(const uint8_t *buf, size_t len);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -11,6 +11,14 @@ extern "C" {
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
/* Low priority */
|
/* Low priority */
|
||||||
ERROR_DETECT_UNIT_NO_DEV = 0,
|
ERROR_DETECT_UNIT_NO_DEV = 0,
|
||||||
@ -53,6 +61,10 @@ typedef struct {
|
|||||||
ErrorDetect_Error_t error[ERROR_DETECT_UNIT_NUM];
|
ErrorDetect_Error_t error[ERROR_DETECT_UNIT_NUM];
|
||||||
} ErrorDetect_t;
|
} ErrorDetect_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
int8_t ErrorDetect_Init(void);
|
int8_t ErrorDetect_Init(void);
|
||||||
void ErrorDetect_Processing(uint32_t sys_time);
|
void ErrorDetect_Processing(uint32_t sys_time);
|
||||||
bool ErrorDetect_ErrorExist(ErrorDetect_Unit_t unit);
|
bool ErrorDetect_ErrorExist(ErrorDetect_Unit_t unit);
|
||||||
@ -61,6 +73,10 @@ const ErrorDetect_Error_t *ErrorDetect_GetDetail(ErrorDetect_Unit_t unit);
|
|||||||
|
|
||||||
void ErrorDetect_Update(ErrorDetect_Unit_t unit, uint32_t time_current);
|
void ErrorDetect_Update(ErrorDetect_Unit_t unit, uint32_t time_current);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -10,6 +10,14 @@ extern "C" {
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* 二阶低通滤波器 */
|
/* 二阶低通滤波器 */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float cutoff_freq; /* 截止频率 */
|
float cutoff_freq; /* 截止频率 */
|
||||||
@ -42,6 +50,10 @@ typedef struct {
|
|||||||
|
|
||||||
} NotchFilter_t;
|
} NotchFilter_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化滤波器
|
* @brief 初始化滤波器
|
||||||
*
|
*
|
||||||
@ -99,6 +111,10 @@ float NotchFilter_Apply(NotchFilter_t *f, float sample);
|
|||||||
*/
|
*/
|
||||||
float NotchFilter_Reset(NotchFilter_t *f, float sample);
|
float NotchFilter_Reset(NotchFilter_t *f, float sample);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -11,6 +11,14 @@ extern "C" {
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 限制底盘功率不超过power_limit
|
* @brief 限制底盘功率不超过power_limit
|
||||||
*
|
*
|
||||||
|
|||||||
@ -10,6 +10,14 @@ extern "C" {
|
|||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/** 四轮布局 */
|
/** 四轮布局 */
|
||||||
/* 前 */
|
/* 前 */
|
||||||
/* 2 1 */
|
/* 2 1 */
|
||||||
@ -33,6 +41,10 @@ typedef struct {
|
|||||||
Mixer_Mode_t mode;
|
Mixer_Mode_t mode;
|
||||||
} Mixer_t; /* 混合器主结构体 */
|
} Mixer_t; /* 混合器主结构体 */
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化混合器
|
* @brief 初始化混合器
|
||||||
*
|
*
|
||||||
@ -55,6 +67,10 @@ int8_t Mixer_Init(Mixer_t *mixer, Mixer_Mode_t mode);
|
|||||||
int8_t Mixer_Apply(Mixer_t *mixer, MoveVector_t *move_vec, float *out,
|
int8_t Mixer_Apply(Mixer_t *mixer, MoveVector_t *move_vec, float *out,
|
||||||
int8_t len, float scale);
|
int8_t len, float scale);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -14,6 +14,14 @@ extern "C" {
|
|||||||
#include "filter.h"
|
#include "filter.h"
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* PID模式 */
|
/* PID模式 */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
KPID_MODE_NO_D = 0, /* 不使用微分项,PI控制器 */
|
KPID_MODE_NO_D = 0, /* 不使用微分项,PI控制器 */
|
||||||
@ -90,6 +98,10 @@ int8_t PID_ResetIntegral(KPID_t *pid);
|
|||||||
*/
|
*/
|
||||||
int8_t PID_Reset(KPID_t *pid);
|
int8_t PID_Reset(KPID_t *pid);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -13,6 +13,10 @@ extern "C" {
|
|||||||
|
|
||||||
#include "component/user_math.h"
|
#include "component/user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
#define UI_DEL_OPERATION_NOTHING (0)
|
#define UI_DEL_OPERATION_NOTHING (0)
|
||||||
#define UI_DEL_OPERATION_DEL (1)
|
#define UI_DEL_OPERATION_DEL (1)
|
||||||
#define UI_DEL_OPERATION_DEL_ALL (2)
|
#define UI_DEL_OPERATION_DEL_ALL (2)
|
||||||
@ -31,6 +35,10 @@ extern "C" {
|
|||||||
#define UI_GRAPIC_LAYER_CMD (6)
|
#define UI_GRAPIC_LAYER_CMD (6)
|
||||||
|
|
||||||
#define UI_DEFAULT_WIDTH (0x01)
|
#define UI_DEFAULT_WIDTH (0x01)
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
#define UI_CHAR_DEFAULT_WIDTH (0x02)
|
#define UI_CHAR_DEFAULT_WIDTH (0x02)
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "user_math.h"
|
#include "user_math.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
inline float InvSqrt(float x) {
|
inline float InvSqrt(float x) {
|
||||||
//#if 0
|
//#if 0
|
||||||
@ -130,3 +132,7 @@ inline float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm) {
|
|||||||
// __NOP();
|
// __NOP();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
@ -14,6 +14,10 @@ extern "C" {
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
#define M_DEG2RAD_MULT (0.01745329251f)
|
#define M_DEG2RAD_MULT (0.01745329251f)
|
||||||
#define M_RAD2DEG_MULT (57.2957795131f)
|
#define M_RAD2DEG_MULT (57.2957795131f)
|
||||||
|
|
||||||
@ -43,6 +47,12 @@ extern "C" {
|
|||||||
_a < _b ? _a : _b; \
|
_a < _b ? _a : _b; \
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* 移动向量 */
|
/* 移动向量 */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float vx; /* 前后平移 */
|
float vx; /* 前后平移 */
|
||||||
@ -50,6 +60,10 @@ typedef struct {
|
|||||||
float wz; /* 转动 */
|
float wz; /* 转动 */
|
||||||
} MoveVector_t;
|
} MoveVector_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
float InvSqrt(float x);
|
float InvSqrt(float x);
|
||||||
|
|
||||||
float AbsClip(float in, float limit);
|
float AbsClip(float in, float limit);
|
||||||
@ -159,3 +173,7 @@ float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm);
|
|||||||
// * @param line 行号
|
// * @param line 行号
|
||||||
// */
|
// */
|
||||||
// void VerifyFailed(const char *file, uint32_t line);
|
// void VerifyFailed(const char *file, uint32_t line);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
@ -15,6 +15,11 @@
|
|||||||
#include "bsp/spi.h"
|
#include "bsp/spi.h"
|
||||||
#include "component/user_math.h"
|
#include "component/user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* Private define ----------------------------------------------------------- */
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define BMI088_REG_ACCL_CHIP_ID (0x00)
|
#define BMI088_REG_ACCL_CHIP_ID (0x00)
|
||||||
#define BMI088_REG_ACCL_ERR (0x02)
|
#define BMI088_REG_ACCL_ERR (0x02)
|
||||||
@ -79,6 +84,10 @@ typedef enum {
|
|||||||
BMI_GYRO,
|
BMI_GYRO,
|
||||||
} BMI_Device_t;
|
} BMI_Device_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static uint8_t buffer[2];
|
static uint8_t buffer[2];
|
||||||
static uint8_t bmi088_rxbuf[BMI088_LEN_RX_BUFF];
|
static uint8_t bmi088_rxbuf[BMI088_LEN_RX_BUFF];
|
||||||
@ -87,6 +96,10 @@ static osThreadId_t thread_alert;
|
|||||||
static bool inited = false;
|
static bool inited = false;
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
static void BMI_WriteSingle(BMI_Device_t dv, uint8_t reg, uint8_t data) {
|
static void BMI_WriteSingle(BMI_Device_t dv, uint8_t reg, uint8_t data) {
|
||||||
buffer[0] = (reg & 0x7f);
|
buffer[0] = (reg & 0x7f);
|
||||||
buffer[1] = data;
|
buffer[1] = data;
|
||||||
|
|||||||
@ -11,6 +11,14 @@ extern "C" {
|
|||||||
#include "component/ahrs.h"
|
#include "component/ahrs.h"
|
||||||
#include "device/device.h"
|
#include "device/device.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
@ -32,6 +40,10 @@ typedef struct {
|
|||||||
const BMI088_Cali_t *cali;
|
const BMI088_Cali_t *cali;
|
||||||
} BMI088_t;
|
} BMI088_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
int8_t BMI088_Init(BMI088_t *bmi088, const BMI088_Cali_t *cali);
|
int8_t BMI088_Init(BMI088_t *bmi088, const BMI088_Cali_t *cali);
|
||||||
int8_t BMI088_Restart(void);
|
int8_t BMI088_Restart(void);
|
||||||
@ -60,6 +72,10 @@ int8_t BMI088_ParseAccl(BMI088_t *bmi088);
|
|||||||
int8_t BMI088_ParseGyro(BMI088_t *bmi088);
|
int8_t BMI088_ParseGyro(BMI088_t *bmi088);
|
||||||
float BMI088_GetUpdateFreq(BMI088_t *bmi088);
|
float BMI088_GetUpdateFreq(BMI088_t *bmi088);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,5 +1,13 @@
|
|||||||
#include "device/buzzer.h"
|
#include "device/buzzer.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
|
|
||||||
int8_t BUZZER_Init(BUZZER_t *buzzer, BSP_PWM_Channel_t channel) {
|
int8_t BUZZER_Init(BUZZER_t *buzzer, BSP_PWM_Channel_t channel) {
|
||||||
if (buzzer == NULL) return DEVICE_ERR;
|
if (buzzer == NULL) return DEVICE_ERR;
|
||||||
@ -42,3 +50,7 @@ int8_t BUZZER_Set(BUZZER_t *buzzer, float freq, float duty_cycle) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -9,14 +9,26 @@ extern "C" {
|
|||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DEVICE_Header_t header;
|
DEVICE_Header_t header;
|
||||||
BSP_PWM_Channel_t channel;
|
BSP_PWM_Channel_t channel;
|
||||||
} BUZZER_t;
|
} BUZZER_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
|
|
||||||
int8_t BUZZER_Init(BUZZER_t *buzzer, BSP_PWM_Channel_t channel);
|
int8_t BUZZER_Init(BUZZER_t *buzzer, BSP_PWM_Channel_t channel);
|
||||||
@ -30,6 +42,10 @@ int8_t BUZZER_Stop(BUZZER_t *buzzer);
|
|||||||
|
|
||||||
int8_t BUZZER_Set(BUZZER_t *buzzer, float freq, float duty_cycle);
|
int8_t BUZZER_Set(BUZZER_t *buzzer, float freq, float duty_cycle);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -7,6 +7,14 @@ extern "C" {
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
#define DEVICE_OK (0)
|
#define DEVICE_OK (0)
|
||||||
#define DEVICE_ERR (-1)
|
#define DEVICE_ERR (-1)
|
||||||
#define DEVICE_ERR_NULL (-2)
|
#define DEVICE_ERR_NULL (-2)
|
||||||
@ -26,6 +34,14 @@ typedef struct {
|
|||||||
uint64_t last_online_time;
|
uint64_t last_online_time;
|
||||||
} DEVICE_Header_t;
|
} DEVICE_Header_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -166,7 +166,7 @@ int8_t DM_IMU_Request(DM_IMU_t *imu, DM_IMU_RID_t rid) {
|
|||||||
.dlc = 4,
|
.dlc = 4,
|
||||||
};
|
};
|
||||||
memcpy(frame.data, tx_data, 4);
|
memcpy(frame.data, tx_data, 4);
|
||||||
|
BSP_CAN_WaitTxMailboxEmpty(imu->param.can, 1); // 等待发送邮箱空闲
|
||||||
int8_t result = BSP_CAN_TransmitStdDataFrame(imu->param.can, &frame);
|
int8_t result = BSP_CAN_TransmitStdDataFrame(imu->param.can, &frame);
|
||||||
return (result == BSP_OK) ? DEVICE_OK : DEVICE_ERR;
|
return (result == BSP_OK) ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,11 +9,19 @@
|
|||||||
|
|
||||||
#include "bsp/uart.h"
|
#include "bsp/uart.h"
|
||||||
#include "bsp/time.h"
|
#include "bsp/time.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define DR16_CH_VALUE_MIN (364u)
|
#define DR16_CH_VALUE_MIN (364u)
|
||||||
#define DR16_CH_VALUE_MID (1024u)
|
#define DR16_CH_VALUE_MID (1024u)
|
||||||
#define DR16_CH_VALUE_MAX (1684u)
|
#define DR16_CH_VALUE_MAX (1684u)
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
@ -83,3 +91,7 @@ bool DR16_WaitDmaCplt(uint32_t timeout) {
|
|||||||
return (osThreadFlagsWait(SIGNAL_DR16_RAW_REDY, osFlagsWaitAll, timeout) ==
|
return (osThreadFlagsWait(SIGNAL_DR16_RAW_REDY, osFlagsWaitAll, timeout) ==
|
||||||
SIGNAL_DR16_RAW_REDY);
|
SIGNAL_DR16_RAW_REDY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|||||||
@ -10,6 +10,14 @@ extern "C" {
|
|||||||
#include "component/user_math.h"
|
#include "component/user_math.h"
|
||||||
#include "device/device.h"
|
#include "device/device.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
@ -41,6 +49,9 @@ int8_t DR16_Restart(void);
|
|||||||
int8_t DR16_StartDmaRecv(DR16_t *dr16);
|
int8_t DR16_StartDmaRecv(DR16_t *dr16);
|
||||||
bool DR16_WaitDmaCplt(uint32_t timeout);
|
bool DR16_WaitDmaCplt(uint32_t timeout);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,10 @@
|
|||||||
#include "bsp/gpio.h"
|
#include "bsp/gpio.h"
|
||||||
#include "bsp/i2c.h"
|
#include "bsp/i2c.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define IST8310_WAI (0x00)
|
#define IST8310_WAI (0x00)
|
||||||
#define IST8310_STAT1 (0x02)
|
#define IST8310_STAT1 (0x02)
|
||||||
@ -31,6 +35,11 @@
|
|||||||
#define IST8310_IIC_ADDRESS (0x0E << 1)
|
#define IST8310_IIC_ADDRESS (0x0E << 1)
|
||||||
|
|
||||||
#define IST8310_LEN_RX_BUFF (6)
|
#define IST8310_LEN_RX_BUFF (6)
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
#define IST8310_SET() \
|
#define IST8310_SET() \
|
||||||
BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_SET)
|
BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_SET)
|
||||||
@ -38,6 +47,10 @@
|
|||||||
BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_RESET)
|
BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_RESET)
|
||||||
|
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
uint8_t ist8310_rxbuf[IST8310_LEN_RX_BUFF];
|
uint8_t ist8310_rxbuf[IST8310_LEN_RX_BUFF];
|
||||||
|
|
||||||
@ -45,6 +58,10 @@ static osThreadId_t thread_alert;
|
|||||||
static bool inited = false;
|
static bool inited = false;
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
static void IST8310_WriteSingle(uint8_t reg, uint8_t data) {
|
static void IST8310_WriteSingle(uint8_t reg, uint8_t data) {
|
||||||
BSP_I2C_MemWriteByte(BSP_I2C_COMP, IST8310_IIC_ADDRESS, reg, data);
|
BSP_I2C_MemWriteByte(BSP_I2C_COMP, IST8310_IIC_ADDRESS, reg, data);
|
||||||
}
|
}
|
||||||
|
|||||||
20
device/led.c
20
device/led.c
@ -6,12 +6,16 @@
|
|||||||
#include "bsp/gpio.h"
|
#include "bsp/gpio.h"
|
||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
|
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
int8_t LED_PWMSet(BSP_PWM_Channel_t channel,float duty_cycle)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle)
|
||||||
|
{
|
||||||
if (duty_cycle < 0.0f || duty_cycle > 1.0f) {
|
if (duty_cycle < 0.0f || duty_cycle > 1.0f) {
|
||||||
return DEVICE_ERR_NULL; // 错误:占空比超出范围
|
return DEVICE_ERR_NULL; // 错误:占空比超出范围
|
||||||
}
|
}
|
||||||
@ -21,14 +25,14 @@ int8_t LED_PWMSet(BSP_PWM_Channel_t channel,float duty_cycle)
|
|||||||
return DEVICE_OK;
|
return DEVICE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t LED_GPIOSet(BSP_GPIO_t gpio,bool value)
|
int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value)
|
||||||
{
|
{
|
||||||
BSP_GPIO_WritePin(gpio,value);
|
if (value) {
|
||||||
|
BSP_GPIO_WritePin(gpio, true);
|
||||||
|
} else {
|
||||||
|
BSP_GPIO_WritePin(gpio, false);
|
||||||
|
}
|
||||||
return DEVICE_OK;
|
return DEVICE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -14,14 +14,18 @@ extern "C" {
|
|||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
BSP_GPIO_t gpio;
|
BSP_GPIO_t gpio;
|
||||||
BSP_PWM_Channel_t channel;
|
BSP_PWM_Channel_t channel;
|
||||||
} DEVICE_LED_t;
|
} DEVICE_LED_t;
|
||||||
|
|
||||||
|
|
||||||
|
extern DEVICE_LED_t LED_Map;
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
int8_t LED_PWMSet(BSP_PWM_Channel_t channel,float duty_cycle);
|
|
||||||
int8_t LED_GPIOSet(BSP_GPIO_t gpio,bool value);
|
|
||||||
|
int8_t BSP_LED_Set(char sign,DEVICE_LED_t ch,bool value,float duty_cycle);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,13 +7,28 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor) {
|
float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor) {
|
||||||
|
|||||||
@ -7,6 +7,14 @@ extern "C" {
|
|||||||
/* Includes ----------------------------------------------------------------- */
|
/* Includes ----------------------------------------------------------------- */
|
||||||
#include "device/device.h"
|
#include "device/device.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
@ -41,12 +49,20 @@ typedef struct {
|
|||||||
MOTOR_Feedback_t feedback;
|
MOTOR_Feedback_t feedback;
|
||||||
} MOTOR_t;
|
} MOTOR_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Exported functions prototypes -------------------------------------------- */
|
/* Exported functions prototypes -------------------------------------------- */
|
||||||
float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor);
|
float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor);
|
||||||
float MOTOR_GetRotorSpeed(const MOTOR_t *motor);
|
float MOTOR_GetRotorSpeed(const MOTOR_t *motor);
|
||||||
float MOTOR_GetTorqueCurrent(const MOTOR_t *motor);
|
float MOTOR_GetTorqueCurrent(const MOTOR_t *motor);
|
||||||
float MOTOR_GetTemp(const MOTOR_t *motor);
|
float MOTOR_GetTemp(const MOTOR_t *motor);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -10,6 +10,10 @@
|
|||||||
#include "bsp/time.h"
|
#include "bsp/time.h"
|
||||||
#include "component/user_math.h"
|
#include "component/user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define LK_CTRL_ID_BASE (0x140)
|
#define LK_CTRL_ID_BASE (0x140)
|
||||||
#define LK_FB_ID_BASE (0x240)
|
#define LK_FB_ID_BASE (0x240)
|
||||||
@ -36,12 +40,24 @@
|
|||||||
#define LK_ENC_15BIT_MAX (32767) // 15位编码器最大值
|
#define LK_ENC_15BIT_MAX (32767) // 15位编码器最大值
|
||||||
#define LK_ENC_16BIT_MAX (65535) // 16位编码器最大值
|
#define LK_ENC_16BIT_MAX (65535) // 16位编码器最大值
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static MOTOR_LK_CANManager_t *can_managers[BSP_CAN_NUM] = {NULL};
|
static MOTOR_LK_CANManager_t *can_managers[BSP_CAN_NUM] = {NULL};
|
||||||
|
|
||||||
/* Private functions -------------------------------------------------------- */
|
/* Private functions -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
static float MOTOR_LK_GetCurrentLSB(MOTOR_LK_Module_t module) {
|
static float MOTOR_LK_GetCurrentLSB(MOTOR_LK_Module_t module) {
|
||||||
switch (module) {
|
switch (module) {
|
||||||
case MOTOR_LK_MF9025:
|
case MOTOR_LK_MF9025:
|
||||||
@ -76,10 +92,6 @@ static int8_t MOTOR_LK_CreateCANManager(BSP_CAN_t can) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void MOTOR_LK_Decode(MOTOR_LK_t *motor, BSP_CAN_Message_t *msg) {
|
static void MOTOR_LK_Decode(MOTOR_LK_t *motor, BSP_CAN_Message_t *msg) {
|
||||||
// 调试信息:打印接收到的数据
|
|
||||||
// printf("LK Motor ID:%d, CMD:0x%02X, Data: %02X %02X %02X %02X %02X %02X %02X %02X\n",
|
|
||||||
// motor->param.id, msg->data[0], msg->data[0], msg->data[1], msg->data[2],
|
|
||||||
// msg->data[3], msg->data[4], msg->data[5], msg->data[6], msg->data[7]);
|
|
||||||
|
|
||||||
// 检查命令字节是否为反馈命令
|
// 检查命令字节是否为反馈命令
|
||||||
if (msg->data[0] != LK_CMD_FEEDBACK) {
|
if (msg->data[0] != LK_CMD_FEEDBACK) {
|
||||||
@ -98,24 +110,24 @@ static void MOTOR_LK_Decode(MOTOR_LK_t *motor, BSP_CAN_Message_t *msg) {
|
|||||||
switch (motor->param.module) {
|
switch (motor->param.module) {
|
||||||
case MOTOR_LK_MF9025:
|
case MOTOR_LK_MF9025:
|
||||||
case MOTOR_LK_MF9035:
|
case MOTOR_LK_MF9035:
|
||||||
// MF/MG电机:转矩电流值
|
|
||||||
motor->motor.feedback.torque_current = raw_current_or_power * MOTOR_LK_GetCurrentLSB(motor->param.module);
|
motor->motor.feedback.torque_current = raw_current_or_power * MOTOR_LK_GetCurrentLSB(motor->param.module);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// MS电机:功率值(范围-1000~1000)
|
motor->motor.feedback.torque_current = (float)raw_current_or_power;
|
||||||
motor->motor.feedback.torque_current = (float)raw_current_or_power; // 将功率存储在torque_current字段中
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析转速 (DATA[4], DATA[5]) - 单位:1dps/LSB
|
// 解析转速 (DATA[4], DATA[5]) - 单位:1dps/LSB
|
||||||
motor->motor.feedback.rotor_speed = (int16_t)((msg->data[5] << 8) | msg->data[4]);
|
int16_t raw_speed = (int16_t)((msg->data[5] << 8) | msg->data[4]);
|
||||||
|
motor->motor.feedback.rotor_speed = motor->param.reverse ? -raw_speed : raw_speed;
|
||||||
|
|
||||||
// 解析编码器值 (DATA[6], DATA[7])
|
// 解析编码器值 (DATA[6], DATA[7])
|
||||||
uint16_t raw_encoder = (uint16_t)((msg->data[7] << 8) | msg->data[6]);
|
uint16_t raw_encoder = (uint16_t)((msg->data[7] << 8) | msg->data[6]);
|
||||||
uint16_t encoder_max = MOTOR_LK_GetEncoderMax(motor->param.module);
|
uint16_t encoder_max = MOTOR_LK_GetEncoderMax(motor->param.module);
|
||||||
|
|
||||||
// 将编码器值转换为弧度 (0 ~ 2π)
|
// 将编码器值转换为弧度 (0 ~ 2π)
|
||||||
motor->motor.feedback.rotor_abs_angle = (float)raw_encoder / (float)encoder_max * M_2PI;
|
float angle = (float)raw_encoder / (float)encoder_max * M_2PI;
|
||||||
|
motor->motor.feedback.rotor_abs_angle = motor->param.reverse ? (M_2PI - angle) : angle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
@ -147,7 +159,7 @@ int8_t MOTOR_LK_Register(MOTOR_LK_Param_t *param) {
|
|||||||
|
|
||||||
// 对于某些LK电机,反馈数据可能通过命令ID发送
|
// 对于某些LK电机,反馈数据可能通过命令ID发送
|
||||||
// 根据实际测试,使用命令ID接收反馈数据
|
// 根据实际测试,使用命令ID接收反馈数据
|
||||||
uint16_t feedback_id = 0x140 + param->id; // 使用命令ID作为反馈ID
|
uint16_t feedback_id = param->id; // 使用命令ID作为反馈ID
|
||||||
|
|
||||||
// 注册CAN接收ID
|
// 注册CAN接收ID
|
||||||
if (BSP_CAN_RegisterId(param->can, feedback_id, 3) != BSP_OK) {
|
if (BSP_CAN_RegisterId(param->can, feedback_id, 3) != BSP_OK) {
|
||||||
@ -170,7 +182,7 @@ int8_t MOTOR_LK_Update(MOTOR_LK_Param_t *param) {
|
|||||||
MOTOR_LK_t *motor = manager->motors[i];
|
MOTOR_LK_t *motor = manager->motors[i];
|
||||||
if (motor && motor->param.id == param->id) {
|
if (motor && motor->param.id == param->id) {
|
||||||
// 对于某些LK电机,反馈数据通过命令ID发送
|
// 对于某些LK电机,反馈数据通过命令ID发送
|
||||||
uint16_t feedback_id = 0x140 + param->id;
|
uint16_t feedback_id = param->id;
|
||||||
|
|
||||||
BSP_CAN_Message_t rx_msg;
|
BSP_CAN_Message_t rx_msg;
|
||||||
if (BSP_CAN_GetMessage(param->can, feedback_id, &rx_msg, BSP_CAN_TIMEOUT_IMMEDIATE) != BSP_OK) {
|
if (BSP_CAN_GetMessage(param->can, feedback_id, &rx_msg, BSP_CAN_TIMEOUT_IMMEDIATE) != BSP_OK) {
|
||||||
@ -222,24 +234,26 @@ int8_t MOTOR_LK_SetOutput(MOTOR_LK_Param_t *param, float value) {
|
|||||||
MOTOR_LK_t *motor = MOTOR_LK_GetMotor(param);
|
MOTOR_LK_t *motor = MOTOR_LK_GetMotor(param);
|
||||||
if (motor == NULL) return DEVICE_ERR_NO_DEV;
|
if (motor == NULL) return DEVICE_ERR_NO_DEV;
|
||||||
|
|
||||||
// 转矩闭环控制命令 - 将输出值转换为转矩控制值
|
// 根据反转参数调整输出
|
||||||
int16_t torque_control = (int16_t)(value * (float)LK_TORQUE_RANGE);
|
float output = param->reverse ? -value : value;
|
||||||
|
|
||||||
// 构建CAN帧(根据协议:命令报文标识符 = 0x140 + ID)
|
// 转矩闭环控制命令 - 将输出值转换为转矩控制值
|
||||||
|
int16_t torque_control = (int16_t)(output * (float)LK_TORQUE_RANGE);
|
||||||
|
|
||||||
|
// 构建CAN帧
|
||||||
BSP_CAN_StdDataFrame_t tx_frame;
|
BSP_CAN_StdDataFrame_t tx_frame;
|
||||||
tx_frame.id = 0x140 + param->id;
|
tx_frame.id = param->id;
|
||||||
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
||||||
|
|
||||||
// 设置转矩闭环控制命令数据
|
tx_frame.data[0] = LK_CMD_TORQUE_CTRL;
|
||||||
tx_frame.data[0] = LK_CMD_TORQUE_CTRL; // 命令字节
|
tx_frame.data[1] = 0x00;
|
||||||
tx_frame.data[1] = 0x00; // NULL
|
tx_frame.data[2] = 0x00;
|
||||||
tx_frame.data[2] = 0x00; // NULL
|
tx_frame.data[3] = 0x00;
|
||||||
tx_frame.data[3] = 0x00; // NULL
|
tx_frame.data[4] = (uint8_t)(torque_control & 0xFF);
|
||||||
tx_frame.data[4] = (uint8_t)(torque_control & 0xFF); // 转矩电流控制值低字节
|
tx_frame.data[5] = (uint8_t)((torque_control >> 8) & 0xFF);
|
||||||
tx_frame.data[5] = (uint8_t)((torque_control >> 8) & 0xFF); // 转矩电流控制值高字节
|
tx_frame.data[6] = 0x00;
|
||||||
tx_frame.data[6] = 0x00; // NULL
|
tx_frame.data[7] = 0x00;
|
||||||
tx_frame.data[7] = 0x00; // NULL
|
BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲
|
||||||
|
|
||||||
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,7 +267,7 @@ int8_t MOTOR_LK_MotorOn(MOTOR_LK_Param_t *param) {
|
|||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
if (param == NULL) return DEVICE_ERR_NULL;
|
||||||
|
|
||||||
BSP_CAN_StdDataFrame_t tx_frame;
|
BSP_CAN_StdDataFrame_t tx_frame;
|
||||||
tx_frame.id = 0x140 + param->id;
|
tx_frame.id = param->id;
|
||||||
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
||||||
|
|
||||||
// 电机运行命令
|
// 电机运行命令
|
||||||
@ -265,7 +279,7 @@ int8_t MOTOR_LK_MotorOn(MOTOR_LK_Param_t *param) {
|
|||||||
tx_frame.data[5] = 0x00;
|
tx_frame.data[5] = 0x00;
|
||||||
tx_frame.data[6] = 0x00;
|
tx_frame.data[6] = 0x00;
|
||||||
tx_frame.data[7] = 0x00;
|
tx_frame.data[7] = 0x00;
|
||||||
|
BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲
|
||||||
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,7 +287,7 @@ int8_t MOTOR_LK_MotorOff(MOTOR_LK_Param_t *param) {
|
|||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
if (param == NULL) return DEVICE_ERR_NULL;
|
||||||
|
|
||||||
BSP_CAN_StdDataFrame_t tx_frame;
|
BSP_CAN_StdDataFrame_t tx_frame;
|
||||||
tx_frame.id = 0x140 + param->id;
|
tx_frame.id = param->id;
|
||||||
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
tx_frame.dlc = MOTOR_TX_BUF_SIZE;
|
||||||
|
|
||||||
// 电机关闭命令
|
// 电机关闭命令
|
||||||
@ -285,7 +299,7 @@ int8_t MOTOR_LK_MotorOff(MOTOR_LK_Param_t *param) {
|
|||||||
tx_frame.data[5] = 0x00;
|
tx_frame.data[5] = 0x00;
|
||||||
tx_frame.data[6] = 0x00;
|
tx_frame.data[6] = 0x00;
|
||||||
tx_frame.data[7] = 0x00;
|
tx_frame.data[7] = 0x00;
|
||||||
|
BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲
|
||||||
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
灵足电机驱动
|
灵足电机驱动
|
||||||
|
|
||||||
|
灵足电机通信协议:
|
||||||
|
- CAN 2.0通信接口,波特率1Mbps
|
||||||
|
- 采用扩展帧格式(29位ID)
|
||||||
|
- ID格式:Bit28~24(通信类型) + Bit23~8(数据区2) + Bit7~0(目标地址)
|
||||||
*/
|
*/
|
||||||
/* Includes ----------------------------------------------------------------- */
|
/* Includes ----------------------------------------------------------------- */
|
||||||
#include "motor_lz.h"
|
#include "motor_lz.h"
|
||||||
@ -22,17 +27,17 @@
|
|||||||
#define LZ_RAW_VALUE_MAX (65535) /* 16位原始值最大值 */
|
#define LZ_RAW_VALUE_MAX (65535) /* 16位原始值最大值 */
|
||||||
#define LZ_TEMP_SCALE (10.0f) /* 温度缩放因子 */
|
#define LZ_TEMP_SCALE (10.0f) /* 温度缩放因子 */
|
||||||
|
|
||||||
#define LZ_MAX_RECOVER_DIFF_RAD (0.4f)
|
#define LZ_MAX_RECOVER_DIFF_RAD (0.28f)
|
||||||
#define MOTOR_TX_BUF_SIZE (8)
|
#define MOTOR_TX_BUF_SIZE (8)
|
||||||
#define MOTOR_RX_BUF_SIZE (8)
|
#define MOTOR_RX_BUF_SIZE (8)
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
|
|
||||||
MOTOR_LZ_MotionParam_t lz_recover_param = {
|
MOTOR_LZ_MotionParam_t lz_relax_param = {
|
||||||
.target_angle = 0.0f,
|
.target_angle = 0.0f,
|
||||||
.target_velocity = 0.0f,
|
.target_velocity = 0.0f,
|
||||||
.kp = 20.0f,
|
.kp = 0.0f,
|
||||||
.kd = 1.0f,
|
.kd = 0.0f,
|
||||||
.torque = 0.0f,
|
.torque = 0.0f,
|
||||||
};
|
};
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
@ -129,7 +134,7 @@ static int8_t MOTOR_LZ_SendExtFrame(BSP_CAN_t can, uint32_t ext_id, uint8_t *dat
|
|||||||
} else {
|
} else {
|
||||||
memset(tx_frame.data, 0, dlc);
|
memset(tx_frame.data, 0, dlc);
|
||||||
}
|
}
|
||||||
|
BSP_CAN_WaitTxMailboxEmpty(can, 1); // 等待发送邮箱空闲
|
||||||
return BSP_CAN_TransmitExtDataFrame(can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
return BSP_CAN_TransmitExtDataFrame(can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,56 +182,53 @@ static uint32_t MOTOR_LZ_IdParser(uint32_t original_id, BSP_CAN_FrameType_t fram
|
|||||||
*/
|
*/
|
||||||
static void MOTOR_LZ_Decode(MOTOR_LZ_t *motor, BSP_CAN_Message_t *msg) {
|
static void MOTOR_LZ_Decode(MOTOR_LZ_t *motor, BSP_CAN_Message_t *msg) {
|
||||||
if (motor == NULL || msg == NULL) return;
|
if (motor == NULL || msg == NULL) return;
|
||||||
|
|
||||||
// 检查是否为反馈数据帧 (通信类型2)
|
|
||||||
// 需要使用原始ID来解析,因为parsed_id已经被IdParser处理过了
|
|
||||||
uint8_t cmd_type = (msg->original_id >> 24) & 0x1F;
|
uint8_t cmd_type = (msg->original_id >> 24) & 0x1F;
|
||||||
if (cmd_type != MOTOR_LZ_CMD_FEEDBACK) return;
|
if (cmd_type != MOTOR_LZ_CMD_FEEDBACK) return;
|
||||||
|
|
||||||
// 解析原始ID中的数据区2 (bit23~8)
|
|
||||||
uint16_t id_data2 = (msg->original_id >> 8) & 0xFFFF;
|
uint16_t id_data2 = (msg->original_id >> 8) & 0xFFFF;
|
||||||
uint8_t motor_can_id = id_data2 & 0xFF; // Bit8~15: 当前电机CAN ID
|
uint8_t motor_can_id = id_data2 & 0xFF;
|
||||||
uint8_t fault_info = (id_data2 >> 8) & 0x3F; // Bit16~21: 故障信息
|
uint8_t fault_info = (id_data2 >> 8) & 0x3F;
|
||||||
uint8_t mode_state = (id_data2 >> 14) & 0x03; // Bit22~23: 模式状态
|
uint8_t mode_state = (id_data2 >> 14) & 0x03;
|
||||||
|
|
||||||
// 更新电机CAN ID
|
|
||||||
motor->lz_feedback.motor_can_id = motor_can_id;
|
motor->lz_feedback.motor_can_id = motor_can_id;
|
||||||
|
motor->lz_feedback.fault.under_voltage = (fault_info & 0x01) != 0;
|
||||||
// 解析故障信息
|
motor->lz_feedback.fault.driver_fault = (fault_info & 0x02) != 0;
|
||||||
motor->lz_feedback.fault.under_voltage = (fault_info & 0x01) != 0; // bit16
|
motor->lz_feedback.fault.over_temp = (fault_info & 0x04) != 0;
|
||||||
motor->lz_feedback.fault.driver_fault = (fault_info & 0x02) != 0; // bit17
|
motor->lz_feedback.fault.encoder_fault = (fault_info & 0x08) != 0;
|
||||||
motor->lz_feedback.fault.over_temp = (fault_info & 0x04) != 0; // bit18
|
motor->lz_feedback.fault.stall_overload = (fault_info & 0x10) != 0;
|
||||||
motor->lz_feedback.fault.encoder_fault = (fault_info & 0x08) != 0; // bit19
|
motor->lz_feedback.fault.uncalibrated = (fault_info & 0x20) != 0;
|
||||||
motor->lz_feedback.fault.stall_overload = (fault_info & 0x10) != 0; // bit20
|
|
||||||
motor->lz_feedback.fault.uncalibrated = (fault_info & 0x20) != 0; // bit21
|
|
||||||
|
|
||||||
// 解析模式状态
|
|
||||||
motor->lz_feedback.state = (MOTOR_LZ_State_t)mode_state;
|
motor->lz_feedback.state = (MOTOR_LZ_State_t)mode_state;
|
||||||
|
|
||||||
// 解析数据区
|
// 反馈解码并自动反向
|
||||||
// Byte0~1: 当前角度 (高字节在前,低字节在后)
|
|
||||||
uint16_t raw_angle = (uint16_t)((msg->data[0] << 8) | msg->data[1]);
|
uint16_t raw_angle = (uint16_t)((msg->data[0] << 8) | msg->data[1]);
|
||||||
motor->lz_feedback.current_angle = MOTOR_LZ_RawToFloat(raw_angle, LZ_ANGLE_RANGE_RAD);
|
float angle = MOTOR_LZ_RawToFloat(raw_angle, LZ_ANGLE_RANGE_RAD);
|
||||||
|
|
||||||
// Byte2~3: 当前角速度 (高字节在前,低字节在后)
|
|
||||||
uint16_t raw_velocity = (uint16_t)((msg->data[2] << 8) | msg->data[3]);
|
uint16_t raw_velocity = (uint16_t)((msg->data[2] << 8) | msg->data[3]);
|
||||||
motor->lz_feedback.current_velocity = MOTOR_LZ_RawToFloat(raw_velocity, LZ_VELOCITY_RANGE_RAD_S);
|
float velocity = MOTOR_LZ_RawToFloat(raw_velocity, LZ_VELOCITY_RANGE_RAD_S);
|
||||||
|
|
||||||
// Byte4~5: 当前力矩 (高字节在前,低字节在后)
|
|
||||||
uint16_t raw_torque = (uint16_t)((msg->data[4] << 8) | msg->data[5]);
|
uint16_t raw_torque = (uint16_t)((msg->data[4] << 8) | msg->data[5]);
|
||||||
motor->lz_feedback.current_torque = MOTOR_LZ_RawToFloat(raw_torque, LZ_TORQUE_RANGE_NM);
|
float torque = MOTOR_LZ_RawToFloat(raw_torque, LZ_TORQUE_RANGE_NM);
|
||||||
|
|
||||||
|
while (angle <0){
|
||||||
|
angle += M_2PI;
|
||||||
|
}
|
||||||
|
while (angle > M_2PI){
|
||||||
|
angle -= M_2PI;
|
||||||
|
}
|
||||||
|
// 自动反向
|
||||||
|
if (motor->param.reverse) {
|
||||||
|
angle = M_2PI - angle;
|
||||||
|
velocity = -velocity;
|
||||||
|
torque = -torque;
|
||||||
|
}
|
||||||
|
|
||||||
|
motor->lz_feedback.current_angle = angle;
|
||||||
|
motor->lz_feedback.current_velocity = velocity;
|
||||||
|
motor->lz_feedback.current_torque = torque;
|
||||||
|
|
||||||
// Byte6~7: 当前温度 (温度*10) (高字节在前,低字节在后)
|
|
||||||
uint16_t raw_temp = (uint16_t)((msg->data[6] << 8) | msg->data[7]);
|
uint16_t raw_temp = (uint16_t)((msg->data[6] << 8) | msg->data[7]);
|
||||||
motor->lz_feedback.temperature = (float)raw_temp / LZ_TEMP_SCALE;
|
motor->lz_feedback.temperature = (float)raw_temp / LZ_TEMP_SCALE;
|
||||||
|
|
||||||
// 更新通用电机反馈信息
|
motor->motor.feedback.rotor_abs_angle = angle;
|
||||||
motor->motor.feedback.rotor_abs_angle = motor->lz_feedback.current_angle;
|
motor->motor.feedback.rotor_speed = velocity;
|
||||||
motor->motor.feedback.rotor_speed = motor->lz_feedback.current_velocity * 180.0f / M_PI * 6.0f; // 转换为RPM
|
motor->motor.feedback.torque_current = torque;
|
||||||
motor->motor.feedback.torque_current = motor->lz_feedback.current_torque; // 使用力矩作为电流反馈
|
|
||||||
motor->motor.feedback.temp = (int8_t)motor->lz_feedback.temperature;
|
motor->motor.feedback.temp = (int8_t)motor->lz_feedback.temperature;
|
||||||
|
|
||||||
// 更新在线状态
|
|
||||||
motor->motor.header.online = true;
|
motor->motor.header.online = true;
|
||||||
motor->motor.header.last_online_time = BSP_TIME_Get();
|
motor->motor.header.last_online_time = BSP_TIME_Get();
|
||||||
}
|
}
|
||||||
@ -339,49 +341,39 @@ int8_t MOTOR_LZ_UpdateAll(void) {
|
|||||||
|
|
||||||
int8_t MOTOR_LZ_MotionControl(MOTOR_LZ_Param_t *param, MOTOR_LZ_MotionParam_t *motion_param) {
|
int8_t MOTOR_LZ_MotionControl(MOTOR_LZ_Param_t *param, MOTOR_LZ_MotionParam_t *motion_param) {
|
||||||
if (param == NULL || motion_param == NULL) return DEVICE_ERR_NULL;
|
if (param == NULL || motion_param == NULL) return DEVICE_ERR_NULL;
|
||||||
|
|
||||||
MOTOR_LZ_t *motor = MOTOR_LZ_GetMotor(param);
|
MOTOR_LZ_t *motor = MOTOR_LZ_GetMotor(param);
|
||||||
if (motor == NULL) return DEVICE_ERR_NO_DEV;
|
if (motor == NULL) return DEVICE_ERR_NO_DEV;
|
||||||
|
|
||||||
// 更新运控参数
|
// 自动反向控制
|
||||||
|
MOTOR_LZ_MotionParam_t send_param = *motion_param;
|
||||||
|
if (param->reverse) {
|
||||||
|
send_param.target_angle = -send_param.target_angle;
|
||||||
|
send_param.target_velocity = -send_param.target_velocity;
|
||||||
|
send_param.torque = -send_param.torque;
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(&motor->motion_param, motion_param, sizeof(MOTOR_LZ_MotionParam_t));
|
memcpy(&motor->motion_param, motion_param, sizeof(MOTOR_LZ_MotionParam_t));
|
||||||
|
|
||||||
// 根据协议,bit23~8数据区2包含力矩信息
|
uint16_t raw_torque = MOTOR_LZ_FloatToRaw(send_param.torque, LZ_TORQUE_RANGE_NM);
|
||||||
// 力矩范围:-60Nm~60Nm 对应 0~65535
|
|
||||||
uint16_t raw_torque = MOTOR_LZ_FloatToRaw(motion_param->torque, LZ_TORQUE_RANGE_NM);
|
|
||||||
|
|
||||||
// 构建扩展ID - 运控模式控制指令
|
|
||||||
// bit28~24: 0x1 (运控模式)
|
|
||||||
// bit23~8: 力矩数据 (0~65535),协议中描述为"Byte2:力矩"
|
|
||||||
// bit7~0: 目标电机CAN_ID
|
|
||||||
uint32_t ext_id = MOTOR_LZ_BuildExtID(MOTOR_LZ_CMD_MOTION, raw_torque, param->motor_id);
|
uint32_t ext_id = MOTOR_LZ_BuildExtID(MOTOR_LZ_CMD_MOTION, raw_torque, param->motor_id);
|
||||||
|
|
||||||
// 准备8字节数据区
|
|
||||||
uint8_t data[8];
|
uint8_t data[8];
|
||||||
|
uint16_t raw_angle = MOTOR_LZ_FloatToRaw(send_param.target_angle, LZ_ANGLE_RANGE_RAD);
|
||||||
// Byte0~1: 目标角度 [0~65535] 对应 (-12.57f~12.57f rad) (高字节在前,低字节在后)
|
data[0] = (raw_angle >> 8) & 0xFF;
|
||||||
uint16_t raw_angle = MOTOR_LZ_FloatToRaw(motion_param->target_angle, LZ_ANGLE_RANGE_RAD);
|
data[1] = raw_angle & 0xFF;
|
||||||
data[0] = (raw_angle >> 8) & 0xFF; // 高字节
|
uint16_t raw_velocity = MOTOR_LZ_FloatToRaw(send_param.target_velocity, LZ_VELOCITY_RANGE_RAD_S);
|
||||||
data[1] = raw_angle & 0xFF; // 低字节
|
data[2] = (raw_velocity >> 8) & 0xFF;
|
||||||
|
data[3] = raw_velocity & 0xFF;
|
||||||
// Byte2~3: 目标角速度 [0~65535] 对应 (-20rad/s~20rad/s) (高字节在前,低字节在后)
|
uint16_t raw_kp = MOTOR_LZ_FloatToRawPositive(send_param.kp, LZ_KP_MAX);
|
||||||
uint16_t raw_velocity = MOTOR_LZ_FloatToRaw(motion_param->target_velocity, LZ_VELOCITY_RANGE_RAD_S);
|
data[4] = (raw_kp >> 8) & 0xFF;
|
||||||
data[2] = (raw_velocity >> 8) & 0xFF; // 高字节
|
data[5] = raw_kp & 0xFF;
|
||||||
data[3] = raw_velocity & 0xFF; // 低字节
|
uint16_t raw_kd = MOTOR_LZ_FloatToRawPositive(send_param.kd, LZ_KD_MAX);
|
||||||
|
data[6] = (raw_kd >> 8) & 0xFF;
|
||||||
// Byte4~5: Kp [0~65535] 对应 (0.0~5000.0) (高字节在前,低字节在后)
|
data[7] = raw_kd & 0xFF;
|
||||||
uint16_t raw_kp = MOTOR_LZ_FloatToRawPositive(motion_param->kp, LZ_KP_MAX);
|
BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲
|
||||||
data[4] = (raw_kp >> 8) & 0xFF; // 高字节
|
|
||||||
data[5] = raw_kp & 0xFF; // 低字节
|
|
||||||
|
|
||||||
// Byte6~7: Kd [0~65535] 对应 (0.0~100.0) (高字节在前,低字节在后)
|
|
||||||
uint16_t raw_kd = MOTOR_LZ_FloatToRawPositive(motion_param->kd, LZ_KD_MAX);
|
|
||||||
data[6] = (raw_kd >> 8) & 0xFF; // 高字节
|
|
||||||
data[7] = raw_kd & 0xFF; // 低字节
|
|
||||||
|
|
||||||
return MOTOR_LZ_SendExtFrame(param->can, ext_id, data, 8);
|
return MOTOR_LZ_SendExtFrame(param->can, ext_id, data, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int8_t MOTOR_LZ_Enable(MOTOR_LZ_Param_t *param) {
|
int8_t MOTOR_LZ_Enable(MOTOR_LZ_Param_t *param) {
|
||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
if (param == NULL) return DEVICE_ERR_NULL;
|
||||||
|
|
||||||
@ -437,7 +429,7 @@ MOTOR_LZ_t* MOTOR_LZ_GetMotor(MOTOR_LZ_Param_t *param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int8_t MOTOR_LZ_Relax(MOTOR_LZ_Param_t *param) {
|
int8_t MOTOR_LZ_Relax(MOTOR_LZ_Param_t *param) {
|
||||||
return MOTOR_LZ_Disable(param, false);
|
return MOTOR_LZ_MotionControl(param, &lz_relax_param);
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t MOTOR_LZ_Offline(MOTOR_LZ_Param_t *param) {
|
int8_t MOTOR_LZ_Offline(MOTOR_LZ_Param_t *param) {
|
||||||
@ -456,72 +448,3 @@ static MOTOR_LZ_Feedback_t* MOTOR_LZ_GetFeedback(MOTOR_LZ_Param_t *param) {
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t MOTOR_LZ_TorqueControl(MOTOR_LZ_Param_t *param, float torque) {
|
|
||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
|
||||||
|
|
||||||
// 创建运控参数,只设置力矩,其他参数为0
|
|
||||||
MOTOR_LZ_MotionParam_t motion_param = {0};
|
|
||||||
motion_param.torque = torque;
|
|
||||||
motion_param.target_angle = 0.0f;
|
|
||||||
motion_param.target_velocity = 0.0f;
|
|
||||||
motion_param.kp = 0.0f;
|
|
||||||
motion_param.kd = 0.0f;
|
|
||||||
|
|
||||||
return MOTOR_LZ_MotionControl(param, &motion_param);
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t MOTOR_LZ_PositionControl(MOTOR_LZ_Param_t *param, float target_angle, float max_velocity) {
|
|
||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
|
||||||
|
|
||||||
// 创建运控参数,设置位置和速度限制
|
|
||||||
MOTOR_LZ_MotionParam_t motion_param = {0};
|
|
||||||
motion_param.target_angle = target_angle;
|
|
||||||
motion_param.target_velocity = max_velocity;
|
|
||||||
motion_param.torque = 0.0f;
|
|
||||||
motion_param.kp = 100.0f; // 默认位置增益
|
|
||||||
motion_param.kd = 5.0f; // 默认微分增益
|
|
||||||
|
|
||||||
return MOTOR_LZ_MotionControl(param, &motion_param);
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t MOTOR_LZ_VelocityControl(MOTOR_LZ_Param_t *param, float target_velocity) {
|
|
||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
|
||||||
|
|
||||||
// 创建运控参数,只设置速度
|
|
||||||
MOTOR_LZ_MotionParam_t motion_param = {0};
|
|
||||||
motion_param.target_angle = 0.0f;
|
|
||||||
motion_param.target_velocity = target_velocity;
|
|
||||||
motion_param.torque = 0.0f;
|
|
||||||
motion_param.kp = 0.0f;
|
|
||||||
motion_param.kd = 1.0f; // 少量阻尼
|
|
||||||
|
|
||||||
return MOTOR_LZ_MotionControl(param, &motion_param);
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t MOTOR_LZ_RecoverToZero(MOTOR_LZ_Param_t *param) {
|
|
||||||
if (param == NULL) return DEVICE_ERR_NULL;
|
|
||||||
|
|
||||||
MOTOR_LZ_t *motor = MOTOR_LZ_GetMotor(param);
|
|
||||||
if (motor == NULL) return DEVICE_ERR_NO_DEV;
|
|
||||||
|
|
||||||
// 获取当前角度
|
|
||||||
MOTOR_LZ_Feedback_t *feedback = MOTOR_LZ_GetFeedback(param);
|
|
||||||
if (feedback == NULL) return DEVICE_ERR_NO_DEV;
|
|
||||||
|
|
||||||
float current_angle = feedback->current_angle;
|
|
||||||
|
|
||||||
// 计算目标角度为0时的最短路径
|
|
||||||
float angle_diff = -current_angle; // 目标是0,所以差值就是-current_angle
|
|
||||||
// 限制最大差值,防止过大跳变
|
|
||||||
if (angle_diff > LZ_MAX_RECOVER_DIFF_RAD) angle_diff = LZ_MAX_RECOVER_DIFF_RAD;
|
|
||||||
if (angle_diff < -LZ_MAX_RECOVER_DIFF_RAD) angle_diff = -LZ_MAX_RECOVER_DIFF_RAD;
|
|
||||||
|
|
||||||
float target_angle = current_angle + angle_diff;
|
|
||||||
|
|
||||||
// 创建运控参数,设置位置和速度限制
|
|
||||||
MOTOR_LZ_MotionParam_t motion_param = lz_recover_param; // 使用预设的恢复参数
|
|
||||||
motion_param.target_angle = target_angle;
|
|
||||||
|
|
||||||
return MOTOR_LZ_MotionControl(param, &motion_param);
|
|
||||||
}
|
|
||||||
@ -213,8 +213,6 @@ int8_t MOTOR_LZ_Relax(MOTOR_LZ_Param_t *param);
|
|||||||
*/
|
*/
|
||||||
int8_t MOTOR_LZ_Offline(MOTOR_LZ_Param_t *param);
|
int8_t MOTOR_LZ_Offline(MOTOR_LZ_Param_t *param);
|
||||||
|
|
||||||
int8_t MOTOR_LZ_RecoverToZero(MOTOR_LZ_Param_t *param);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -10,6 +10,10 @@
|
|||||||
#include "bsp/time.h"
|
#include "bsp/time.h"
|
||||||
#include "component/user_math.h"
|
#include "component/user_math.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define GM6020_FB_ID_BASE (0x205)
|
#define GM6020_FB_ID_BASE (0x205)
|
||||||
#define GM6020_CTRL_ID_BASE (0x1ff)
|
#define GM6020_CTRL_ID_BASE (0x1ff)
|
||||||
@ -30,11 +34,24 @@
|
|||||||
#define MOTOR_ENC_RES (8192) /* 电机编码器分辨率 */
|
#define MOTOR_ENC_RES (8192) /* 电机编码器分辨率 */
|
||||||
#define MOTOR_CUR_RES (16384) /* 电机转矩电流分辨率 */
|
#define MOTOR_CUR_RES (16384) /* 电机转矩电流分辨率 */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static MOTOR_RM_CANManager_t *can_managers[BSP_CAN_NUM] = {NULL};
|
static MOTOR_RM_CANManager_t *can_managers[BSP_CAN_NUM] = {NULL};
|
||||||
|
|
||||||
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
static int8_t MOTOR_RM_GetLogicalIndex(uint16_t can_id, MOTOR_RM_Module_t module) {
|
static int8_t MOTOR_RM_GetLogicalIndex(uint16_t can_id, MOTOR_RM_Module_t module) {
|
||||||
switch (module) {
|
switch (module) {
|
||||||
case MOTOR_M2006:
|
case MOTOR_M2006:
|
||||||
@ -223,6 +240,7 @@ int8_t MOTOR_RM_Ctrl(MOTOR_RM_Param_t *param) {
|
|||||||
default:
|
default:
|
||||||
return DEVICE_ERR;
|
return DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲
|
||||||
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
pwm控制舵机
|
pwm<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*Includes -----------------------------------------*/
|
/*Includes -----------------------------------------*/
|
||||||
@ -7,9 +7,17 @@
|
|||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
#include "servo.h"
|
#include "servo.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
#define SERVO_MIN_DUTY 0.025f
|
#define SERVO_MIN_DUTY 0.025f
|
||||||
#define SERVO_MAX_DUTY 0.125f
|
#define SERVO_MAX_DUTY 0.125f
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* @param
|
* @param
|
||||||
@ -24,10 +32,10 @@ int8_t SERVO_Init(SERVO_t *servo) {
|
|||||||
int8_t SERVO_SetAngle(SERVO_t *servo, float angle) {
|
int8_t SERVO_SetAngle(SERVO_t *servo, float angle) {
|
||||||
if (servo == NULL) return BSP_ERR;
|
if (servo == NULL) return BSP_ERR;
|
||||||
|
|
||||||
/*限制角度范围*/
|
/*<EFBFBD><EFBFBD><EFBFBD>ƽǶȷ<EFBFBD>Χ*/
|
||||||
if (angle < 0.0f) angle = 0.0f;
|
if (angle < 0.0f) angle = 0.0f;
|
||||||
if (angle > 180.0f) angle = 180.0f;
|
if (angle > 180.0f) angle = 180.0f;
|
||||||
/*角度映射到占空比*/
|
/*<EFBFBD>Ƕ<EFBFBD>ӳ<EFBFBD>䵽ռ<EFBFBD>ձ<EFBFBD>*/
|
||||||
float duty = servo->min_duty + (angle / 180.0f) * (servo->max_duty - servo->min_duty);
|
float duty = servo->min_duty + (angle / 180.0f) * (servo->max_duty - servo->min_duty);
|
||||||
|
|
||||||
return BSP_PWM_Set(servo->pwm_ch, duty);
|
return BSP_PWM_Set(servo->pwm_ch, duty);
|
||||||
|
|||||||
@ -9,6 +9,14 @@ extern "C" {
|
|||||||
|
|
||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Exported constants ------------------------------------------------------- */
|
||||||
/* Exported macro ----------------------------------------------------------- */
|
/* Exported macro ----------------------------------------------------------- */
|
||||||
/* Exported types ----------------------------------------------------------- */
|
/* Exported types ----------------------------------------------------------- */
|
||||||
@ -22,6 +30,10 @@ typedef struct {
|
|||||||
float max_duty;
|
float max_duty;
|
||||||
} SERVO_t;
|
} SERVO_t;
|
||||||
|
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* @param servo
|
* @param servo
|
||||||
@ -46,6 +58,10 @@ int8_t SERVO_SetAngle(SERVO_t *servo, float angle);
|
|||||||
|
|
||||||
int8_t SERVO_Stop(SERVO_t *servo);
|
int8_t SERVO_Stop(SERVO_t *servo);
|
||||||
|
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,18 +3,35 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "device/vofa.h"
|
#include "device/vofa.h"
|
||||||
#include "bsp/uart.h"
|
#include "bsp/uart.h"
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
|
|
||||||
#define MAX_CHANNEL 64u // 根据实际最大通道数调整
|
#define MAX_CHANNEL 64u // 根据实际最大通道数调整
|
||||||
|
|
||||||
#define JUSTFLOAT_TAIL 0x7F800000
|
#define JUSTFLOAT_TAIL 0x7F800000
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static uint8_t vofa_tx_buf[sizeof(float) * MAX_CHANNEL + sizeof(uint32_t)];
|
static uint8_t vofa_tx_buf[sizeof(float) * MAX_CHANNEL + sizeof(uint32_t)];
|
||||||
static VOFA_Protocol_t current_protocol = VOFA_PROTOCOL_FIREWATER; // 默认协议
|
static VOFA_Protocol_t current_protocol = VOFA_PROTOCOL_FIREWATER; // 默认协议
|
||||||
|
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
/************************ RawData *************************/
|
/************************ RawData *************************/
|
||||||
void VOFA_RawData_Send(const char* data, bool dma) {
|
void VOFA_RawData_Send(const char* data, bool dma) {
|
||||||
|
|||||||
@ -4,18 +4,36 @@
|
|||||||
|
|
||||||
#include "bsp/pwm.h"
|
#include "bsp/pwm.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* USER INCLUDE BEGIN */
|
||||||
|
|
||||||
|
/* USER INCLUDE END */
|
||||||
|
|
||||||
/* Private define ----------------------------------------------------------- */
|
/* Private define ----------------------------------------------------------- */
|
||||||
#define DEVICE_WS2812_T1H (uint16_t)(BSP_PWM_GetAutoReloadPreload(BSP_PWM_WS2812) * 0.56) // High-level width of logic-1 pulse
|
#define DEVICE_WS2812_T1H (uint16_t)(BSP_PWM_GetAutoReloadPreload(BSP_PWM_WS2812) * 0.56) // High-level width of logic-1 pulse
|
||||||
#define DEVICE_WS2812_T0H (BSP_PWM_GetAutoReloadPreload(BSP_PWM_WS2812) * 0.29) // High-level width of logic-0 pulse
|
#define DEVICE_WS2812_T0H (BSP_PWM_GetAutoReloadPreload(BSP_PWM_WS2812) * 0.29) // High-level width of logic-0 pulse
|
||||||
#define DEVICE_WS2812_WS_REST 40 // Number of reset pulses (low level) after data stream
|
#define DEVICE_WS2812_WS_REST 40 // Number of reset pulses (low level) after data stream
|
||||||
#define DEVICE_WS2812_DATA_LEN 24 // WS2812 data length: 24 bits (GRB) per LED
|
#define DEVICE_WS2812_DATA_LEN 24 // WS2812 data length: 24 bits (GRB) per LED
|
||||||
#define DEVICE_WS2812_RST_NUM 50 // Extra reset pulses reserved at the end of the buffer
|
#define DEVICE_WS2812_RST_NUM 50 // Extra reset pulses reserved at the end of the buffer
|
||||||
|
|
||||||
|
/* USER DEFINE BEGIN */
|
||||||
|
|
||||||
|
/* USER DEFINE END */
|
||||||
|
|
||||||
/* Private macro ------------------------------------------------------------ */
|
/* Private macro ------------------------------------------------------------ */
|
||||||
/* Private typedef ---------------------------------------------------------- */
|
/* Private typedef ---------------------------------------------------------- */
|
||||||
|
/* USER STRUCT BEGIN */
|
||||||
|
|
||||||
|
/* USER STRUCT END */
|
||||||
|
|
||||||
/* Private variables -------------------------------------------------------- */
|
/* Private variables -------------------------------------------------------- */
|
||||||
static uint16_t DEVICE_WS2812_LED_NUM; // Total number of LEDs
|
static uint16_t DEVICE_WS2812_LED_NUM; // Total number of LEDs
|
||||||
static uint16_t *DEVICE_WS2812_RGB_Buff = NULL;// PWM duty buffer for DMA
|
static uint16_t *DEVICE_WS2812_RGB_Buff = NULL;// PWM duty buffer for DMA
|
||||||
/* Private function -------------------------------------------------------- */
|
/* Private function -------------------------------------------------------- */
|
||||||
|
/* USER FUNCTION BEGIN */
|
||||||
|
|
||||||
|
/* USER FUNCTION END */
|
||||||
|
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Exported functions ------------------------------------------------------- */
|
||||||
/**
|
/**
|
||||||
* Set color of a single WS2812 LED
|
* Set color of a single WS2812 LED
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user