From 83aff179eebd7a1c37354d7947f46dd4172368b9 Mon Sep 17 00:00:00 2001 From: Robofish <1683502971@qq.com> Date: Sat, 20 Sep 2025 01:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 10244 -> 10244 bytes bsp/bsp.h | 12 +++ bsp/can.c | 142 ++++++++++--------------- bsp/can.h | 26 ++++- bsp/dwt.c | 16 +++ bsp/dwt.h | 16 +++ bsp/gpio.c | 16 +++ bsp/gpio.h | 12 +++ bsp/i2c.c | 12 +++ bsp/i2c.h | 11 ++ bsp/mm.c | 18 +++- bsp/mm.h | 12 +++ bsp/pwm.c | 12 +++ bsp/pwm.h | 12 +++ bsp/spi.c | 16 +++ bsp/spi.h | 12 +++ bsp/time.c | 18 +++- bsp/time.h | 12 +++ bsp/uart.c | 18 +++- bsp/uart.h | 12 +++ component/ahrs.c | 12 +++ component/ahrs.h | 16 +++ component/capacity.c | 12 +++ component/capacity.h | 12 +++ component/cmd.c | 12 +++ component/cmd.h | 14 ++- component/crc16.c | 12 +++ component/crc16.h | 12 +++ component/crc8.c | 12 +++ component/crc8.h | 12 +++ component/error_detect.h | 16 +++ component/filter.h | 16 +++ component/limiter.h | 8 ++ component/mixer.h | 16 +++ component/pid.h | 12 +++ component/ui.h | 8 ++ component/user_math.c | 8 +- component/user_math.h | 18 ++++ device/bmi088.c | 13 +++ device/bmi088.h | 16 +++ device/buzzer.c | 12 +++ device/buzzer.h | 16 +++ device/device.h | 16 +++ device/dm_imu.c | 2 +- device/dr16.c | 12 +++ device/dr16.h | 11 ++ device/ist8310.c | 17 +++ device/led.c | 36 ++++--- device/led.h | 8 +- device/motor.c | 15 +++ device/motor.h | 16 +++ device/motor_lk.c | 94 ++++++++++------- device/motor_lz.c | 221 +++++++++++++-------------------------- device/motor_lz.h | 2 - device/motor_rm.c | 18 ++++ device/servo.c | 14 ++- device/servo.h | 16 +++ device/vofa.c | 17 +++ device/ws2812.c | 18 ++++ 59 files changed, 918 insertions(+), 305 deletions(-) diff --git a/.DS_Store b/.DS_Store index d799689820726d65a45b4f67f3644afb8a37b7e3..99e21013e9c7f2c41795d59a3bf5ae69f0ea8428 100644 GIT binary patch delta 164 zcmZn(XbG6$&nUeyU^hRb^kg0ZO?GC6M1}%}g2{OT4IGBXMmh?H7Dki53D^rmCCY+} z@^bR?(is>S7$+ME>_Jvk$SAuxL(qhoiLra~JQ4HB*@8TiABre2>TPBc)n#U5lm%*< kH`!F8aPk|8?USn|r8e)CT*JDVUEvqYWLc4VY}(|Q09%PI!2kdN delta 148 zcmZn(XbG6$&nUGqU^hRb)MOq3&B>C2J2^~DOm!5D4GkvW5-gm&Ra9Vew2&$@6I1%+ zCQ)<7d6RdEYBSE;{7h7rnT^4bfq{{4vZQ38sxX5uLl8qgLlO{|FnBWLF%$#oM1~w7 sOl2ry&;ybclP`#hOs #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ #define CAN_QUEUE_MUTEX_TIMEOUT 100 /* 队列互斥锁超时时间(ms) */ -#define CAN_TX_SEMAPHORE_TIMEOUT 1000 /* 发送信号量超时时间(ms) */ #define CAN_TX_MAILBOX_NUM 3 /* CAN发送邮箱数量 */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ typedef struct BSP_CAN_QueueNode { @@ -20,10 +27,13 @@ typedef struct BSP_CAN_QueueNode { struct BSP_CAN_QueueNode *next; /* 指向下一个节点的指针 */ } BSP_CAN_QueueNode_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static BSP_CAN_QueueNode_t *queue_list = 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 bool inited = false; 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 void BSP_CAN_RxFifo0Callback(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 uint32_t BSP_CAN_DefaultIdParser(uint32_t original_id, BSP_CAN_FrameType_t frame_type); /* Private functions -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ /** * @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 --------------------------------------------------- */ void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxCompleteCallback(hcan); // 调用用户回调 if (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) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxCompleteCallback(hcan); // 调用用户回调 if (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) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxCompleteCallback(hcan); // 调用用户回调 if (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) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxAbortCallback(hcan); // 调用用户回调 if (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) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxAbortCallback(hcan); // 调用用户回调 if (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) { BSP_CAN_t bsp_can = CAN_Get(hcan); if (bsp_can != BSP_CAN_ERR) { - // 自动释放发送信号量 - BSP_CAN_TxAbortCallback(hcan); // 调用用户回调 if (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; } - // 创建发送信号量,每个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 */ inited = true; @@ -424,14 +378,6 @@ int8_t BSP_CAN_DeInit(void) { 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) { osMutexDelete(queue_mutex); @@ -494,20 +440,8 @@ int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format, 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); if (hcan == NULL) { - // 如果获取句柄失败,需要释放信号量 - osSemaphoreRelease(tx_semaphore[can]); 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; break; default: - // 如果格式错误,需要释放信号量 - osSemaphoreRelease(tx_semaphore[can]); 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); if (result != HAL_OK) { - // 如果发送失败,需要释放信号量 - osSemaphoreRelease(tx_semaphore[can]); return BSP_ERR; } - // 发送成功,信号量将在发送完成回调中释放 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); } + +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); + } +} diff --git a/bsp/can.h b/bsp/can.h index c1bc147..f987abe 100644 --- a/bsp/can.h +++ b/bsp/can.h @@ -12,12 +12,19 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ #define BSP_CAN_MAX_DLC 8 #define BSP_CAN_DEFAULT_QUEUE_SIZE 10 #define BSP_CAN_TIMEOUT_IMMEDIATE 0 #define BSP_CAN_TIMEOUT_FOREVER osWaitForever -#define CAN_TX_SEMAPHORE_TIMEOUT 1000 /* 发送信号量超时时间(ms) */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ @@ -94,6 +101,10 @@ typedef struct { /* ID解析回调函数类型 */ 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 -------------------------------------------- */ /** @@ -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); +/** + * @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 接收队列 * @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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/bsp/dwt.c b/bsp/dwt.c index 15e3e51..c16af9e 100644 --- a/bsp/dwt.c +++ b/bsp/dwt.c @@ -12,6 +12,18 @@ */ #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; static uint32_t CPU_FREQ_Hz, CPU_FREQ_Hz_ms, CPU_FREQ_Hz_us; static uint32_t CYCCNT_RountCount; @@ -19,6 +31,10 @@ static uint32_t CYCCNT_LAST; uint64_t CYCCNT64; static void DWT_CNT_Update(void); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + void DWT_Init(uint32_t CPU_Freq_mHz) { /* 使能DWT外设 */ diff --git a/bsp/dwt.h b/bsp/dwt.h index 817e922..65a731e 100644 --- a/bsp/dwt.h +++ b/bsp/dwt.h @@ -16,6 +16,14 @@ #include "main.h" #include "stdint.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + typedef struct { uint32_t s; @@ -23,6 +31,10 @@ typedef struct uint16_t us; } DWT_Time_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + void DWT_Init(uint32_t CPU_Freq_mHz); float DWT_GetDeltaT(uint32_t *cnt_last); double DWT_GetDeltaT64(uint32_t *cnt_last); @@ -34,4 +46,8 @@ void DWT_SysTimeUpdate(void); extern DWT_Time_t SysTime; +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #endif /* DWT_H_ */ diff --git a/bsp/gpio.c b/bsp/gpio.c index 5001f39..5c3113c 100644 --- a/bsp/gpio.c +++ b/bsp/gpio.c @@ -4,7 +4,15 @@ #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ typedef struct { @@ -12,6 +20,10 @@ typedef struct { GPIO_TypeDef *gpio; } BSP_GPIO_MAP_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static const BSP_GPIO_MAP_t GPIO_Map[BSP_GPIO_NUM] = { /* 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); /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { for (uint8_t i = 0; i < 16; i++) { if (GPIO_Pin & (1 << i)) { diff --git a/bsp/gpio.h b/bsp/gpio.h index 75027f1..0f30a9b 100644 --- a/bsp/gpio.h +++ b/bsp/gpio.h @@ -10,8 +10,16 @@ extern "C" { #include "bsp/bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ typedef 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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/bsp/i2c.c b/bsp/i2c.c index 2885125..6120f59 100644 --- a/bsp/i2c.c +++ b/bsp/i2c.c @@ -1,9 +1,21 @@ /* Includes ----------------------------------------------------------------- */ #include "bsp\i2c.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static void (*I2C_Callback[BSP_I2C_NUM][BSP_I2C_CB_NUM])(void); diff --git a/bsp/i2c.h b/bsp/i2c.h index ab75d48..79086e7 100644 --- a/bsp/i2c.h +++ b/bsp/i2c.h @@ -11,8 +11,16 @@ extern "C" { #include "bsp/bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* 要添加使用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, uint8_t *data, uint16_t size, bool dma); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ #ifdef __cplusplus } diff --git a/bsp/mm.c b/bsp/mm.c index b78e3c4..13d20c0 100644 --- a/bsp/mm.c +++ b/bsp/mm.c @@ -1,14 +1,30 @@ /* Includes ----------------------------------------------------------------- */ -#include "bsp\mm.h" +#include "bsp/mm.h" #include "FreeRTOS.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private variables -------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private function -------------------------------------------------------- */ /* Exported functions ------------------------------------------------------- */ inline void *BSP_Malloc(size_t size) { return pvPortMalloc(size); } inline void BSP_Free(void *pv) { vPortFree(pv); } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/bsp/mm.h b/bsp/mm.h index 57bdcac..d24634e 100644 --- a/bsp/mm.h +++ b/bsp/mm.h @@ -8,13 +8,25 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* Exported functions prototypes -------------------------------------------- */ void *BSP_Malloc(size_t size); void BSP_Free(void *pv); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/bsp/pwm.c b/bsp/pwm.c index 34e9893..3caedb0 100644 --- a/bsp/pwm.c +++ b/bsp/pwm.c @@ -3,7 +3,15 @@ #include "bsp/pwm.h" #include "bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ typedef struct { @@ -11,6 +19,10 @@ typedef struct { uint16_t channel; } BSP_PWM_Config_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static const BSP_PWM_Config_t PWM_Map[BSP_PWM_NUM] = { /* AUTO GENERATED BSP_PWM_MAP */ diff --git a/bsp/pwm.h b/bsp/pwm.h index a1fa3c9..c50c127 100644 --- a/bsp/pwm.h +++ b/bsp/pwm.h @@ -9,9 +9,17 @@ extern "C" { #include "tim.h" #include "bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* PWM通道 */ 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_Stop_DMA(BSP_PWM_Channel_t ch); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/bsp/spi.c b/bsp/spi.c index b4701de..d8ac00e 100644 --- a/bsp/spi.c +++ b/bsp/spi.c @@ -2,9 +2,21 @@ #include #include "bsp/spi.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ 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); return BSP_SPI_Transmit(spi, data, size, true); } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/bsp/spi.h b/bsp/spi.h index 374b34a..8c0afae 100644 --- a/bsp/spi.h +++ b/bsp/spi.h @@ -11,8 +11,16 @@ extern "C" { #include "bsp/bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* 要添加使用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_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/bsp/time.c b/bsp/time.c index 5817cea..21918ed 100644 --- a/bsp/time.c +++ b/bsp/time.c @@ -6,9 +6,21 @@ #include "FreeRTOS.h" #include "main.h" #include "task.h" + +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ /* Private function -------------------------------------------------------- */ /* Exported functions ------------------------------------------------------- */ @@ -62,4 +74,8 @@ int8_t BSP_TIME_Delay_us(uint32_t us) { return BSP_OK; } -int8_t BSP_TIME_Delay(uint32_t ms) __attribute__((alias("BSP_TIME_Delay_ms"))); \ No newline at end of file +int8_t BSP_TIME_Delay(uint32_t ms) __attribute__((alias("BSP_TIME_Delay_ms"))); + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ \ No newline at end of file diff --git a/bsp/time.h b/bsp/time.h index 5273f0f..c69085b 100644 --- a/bsp/time.h +++ b/bsp/time.h @@ -9,8 +9,16 @@ extern "C" { #include "bsp/bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* Exported functions prototypes -------------------------------------------- */ 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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/bsp/uart.c b/bsp/uart.c index 8cde78a..a403fdc 100644 --- a/bsp/uart.c +++ b/bsp/uart.c @@ -3,9 +3,21 @@ #include "bsp/uart.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static void (*UART_Callback[BSP_UART_NUM][BSP_UART_CB_NUM])(void); @@ -134,4 +146,8 @@ int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma) } else { return HAL_UART_Receive_IT(BSP_UART_GetHandle(uart), data, size); } -} \ No newline at end of file +} + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ \ No newline at end of file diff --git a/bsp/uart.h b/bsp/uart.h index 49a758b..06ff849 100644 --- a/bsp/uart.h +++ b/bsp/uart.h @@ -11,8 +11,16 @@ extern "C" { #include "bsp/bsp.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ /* 要添加使用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_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/ahrs.c b/component/ahrs.c index 5886297..ffdb870 100644 --- a/component/ahrs.c +++ b/component/ahrs.c @@ -9,9 +9,17 @@ #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + #define BETA_IMU (0.033f) #define BETA_AHRS (0.041f) +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* 2 * proportional gain (Kp) */ static float beta = BETA_IMU; @@ -403,3 +411,7 @@ int8_t AHRS_GetEulr(AHRS_Eulr_t *eulr, const AHRS_t *ahrs) { * \param eulr 被操作的数据 */ void AHRS_ResetEulr(AHRS_Eulr_t *eulr) { memset(eulr, 0, sizeof(*eulr)); } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/component/ahrs.h b/component/ahrs.h index add8b8b..2245b1f 100644 --- a/component/ahrs.h +++ b/component/ahrs.h @@ -11,6 +11,14 @@ extern "C" { #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* 欧拉角(Euler angle) */ typedef struct { float yaw; /* 偏航角(Yaw angle) */ @@ -55,6 +63,10 @@ typedef struct { float inv_sample_freq; /* 采样频率的的倒数 */ } AHRS_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /** * @brief 初始化姿态解算 * @@ -93,6 +105,10 @@ int8_t AHRS_GetEulr(AHRS_Eulr_t *eulr, const AHRS_t *ahrs); */ void AHRS_ResetEulr(AHRS_Eulr_t *eulr); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/capacity.c b/component/capacity.c index a7bf801..fd64eb2 100644 --- a/component/capacity.c +++ b/component/capacity.c @@ -5,6 +5,14 @@ #include "capacity.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** * @brief 通过电压计算电池剩余电量 * @@ -56,3 +64,7 @@ float Capacity_GetCapacitorRemain(float vcap, float vbat, float v_cutoff) { return percentage; } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/component/capacity.h b/component/capacity.h index a3839d4..b4010e9 100644 --- a/component/capacity.h +++ b/component/capacity.h @@ -12,6 +12,14 @@ extern "C" { #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** * @brief 通过电压计算电池剩余电量 * @@ -30,6 +38,10 @@ float Capacity_GetBatteryRemain(float volt); */ float Capacity_GetCapacitorRemain(float vcap, float vbat, float v_cutoff); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/cmd.c b/component/cmd.c index 205869c..1effe69 100644 --- a/component/cmd.c +++ b/component/cmd.c @@ -6,6 +6,14 @@ #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** * @brief 行为转换为对应按键 * @@ -373,3 +381,7 @@ int8_t CMD_RefereeAdd(CMD_RefereeCmd_t *ref, CMD_UI_t cmd) { ref->counter++; return 0; } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/component/cmd.h b/component/cmd.h index 8561239..df84538 100644 --- a/component/cmd.h +++ b/component/cmd.h @@ -13,7 +13,15 @@ extern "C" { #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 { @@ -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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/crc16.c b/component/crc16.c index 2d9de2a..0d17eb0 100644 --- a/component/crc16.c +++ b/component/crc16.c @@ -1,5 +1,13 @@ #include "crc16.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + static const uint16_t crc16_tab[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 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 + (len % 2)))[len / sizeof(uint16_t) - 1]; } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/component/crc16.h b/component/crc16.h index dc54b30..68b0a87 100644 --- a/component/crc16.h +++ b/component/crc16.h @@ -8,11 +8,23 @@ extern "C" { #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + #define CRC16_INIT 0XFFFF uint16_t CRC16_Calc(const uint8_t *buf, size_t len, uint16_t crc); bool CRC16_Verify(const uint8_t *buf, size_t len); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/crc8.c b/component/crc8.c index bf0bcf9..66f4ad2 100644 --- a/component/crc8.c +++ b/component/crc8.c @@ -1,5 +1,13 @@ #include "crc8.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + static const uint8_t crc8_tab[256] = { 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 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); return expected == buf[len - sizeof(uint8_t)]; } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/component/crc8.h b/component/crc8.h index 61b221a..a376c71 100644 --- a/component/crc8.h +++ b/component/crc8.h @@ -8,11 +8,23 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + #define CRC8_INIT 0xFF uint8_t CRC8_Calc(const uint8_t *buf, size_t len, uint8_t crc); bool CRC8_Verify(const uint8_t *buf, size_t len); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/error_detect.h b/component/error_detect.h index eb79614..3dba7a2 100644 --- a/component/error_detect.h +++ b/component/error_detect.h @@ -11,6 +11,14 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + typedef enum { /* Low priority */ ERROR_DETECT_UNIT_NO_DEV = 0, @@ -53,6 +61,10 @@ typedef struct { ErrorDetect_Error_t error[ERROR_DETECT_UNIT_NUM]; } ErrorDetect_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + int8_t ErrorDetect_Init(void); void ErrorDetect_Processing(uint32_t sys_time); 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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/filter.h b/component/filter.h index c075946..ae2b072 100644 --- a/component/filter.h +++ b/component/filter.h @@ -10,6 +10,14 @@ extern "C" { #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* 二阶低通滤波器 */ typedef struct { float cutoff_freq; /* 截止频率 */ @@ -42,6 +50,10 @@ typedef struct { } NotchFilter_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /** * @brief 初始化滤波器 * @@ -99,6 +111,10 @@ float NotchFilter_Apply(NotchFilter_t *f, float sample); */ float NotchFilter_Reset(NotchFilter_t *f, float sample); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/limiter.h b/component/limiter.h index 7c24cb5..d0aa92a 100644 --- a/component/limiter.h +++ b/component/limiter.h @@ -11,6 +11,14 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** * @brief 限制底盘功率不超过power_limit * diff --git a/component/mixer.h b/component/mixer.h index 98de3b0..b8e4401 100644 --- a/component/mixer.h +++ b/component/mixer.h @@ -10,6 +10,14 @@ extern "C" { #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** 四轮布局 */ /* 前 */ /* 2 1 */ @@ -33,6 +41,10 @@ typedef struct { Mixer_Mode_t mode; } Mixer_t; /* 混合器主结构体 */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /** * @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 len, float scale); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/pid.h b/component/pid.h index a93acaa..4b451eb 100644 --- a/component/pid.h +++ b/component/pid.h @@ -14,6 +14,14 @@ extern "C" { #include "filter.h" #include "user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* PID模式 */ typedef enum { KPID_MODE_NO_D = 0, /* 不使用微分项,PI控制器 */ @@ -90,6 +98,10 @@ int8_t PID_ResetIntegral(KPID_t *pid); */ int8_t PID_Reset(KPID_t *pid); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/component/ui.h b/component/ui.h index e569985..4f742d3 100644 --- a/component/ui.h +++ b/component/ui.h @@ -13,6 +13,10 @@ extern "C" { #include "component/user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + #define UI_DEL_OPERATION_NOTHING (0) #define UI_DEL_OPERATION_DEL (1) #define UI_DEL_OPERATION_DEL_ALL (2) @@ -31,6 +35,10 @@ extern "C" { #define UI_GRAPIC_LAYER_CMD (6) #define UI_DEFAULT_WIDTH (0x01) + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ #define UI_CHAR_DEFAULT_WIDTH (0x02) typedef enum { diff --git a/component/user_math.c b/component/user_math.c index f3f3964..49a4723 100644 --- a/component/user_math.c +++ b/component/user_math.c @@ -3,8 +3,10 @@ */ #include "user_math.h" - #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ inline float InvSqrt(float x) { //#if 0 @@ -130,3 +132,7 @@ inline float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm) { // __NOP(); // } // } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ \ No newline at end of file diff --git a/component/user_math.h b/component/user_math.h index fea820f..d4531e6 100644 --- a/component/user_math.h +++ b/component/user_math.h @@ -14,6 +14,10 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + #define M_DEG2RAD_MULT (0.01745329251f) #define M_RAD2DEG_MULT (57.2957795131f) @@ -43,6 +47,12 @@ extern "C" { _a < _b ? _a : _b; \ }) +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + + + /* 移动向量 */ typedef struct { float vx; /* 前后平移 */ @@ -50,6 +60,10 @@ typedef struct { float wz; /* 转动 */ } MoveVector_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + float InvSqrt(float x); float AbsClip(float in, float limit); @@ -159,3 +173,7 @@ float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm); // * @param line 行号 // */ // void VerifyFailed(const char *file, uint32_t line); + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ \ No newline at end of file diff --git a/device/bmi088.c b/device/bmi088.c index 328f728..0f4c154 100644 --- a/device/bmi088.c +++ b/device/bmi088.c @@ -15,6 +15,11 @@ #include "bsp/spi.h" #include "component/user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* Private define ----------------------------------------------------------- */ /* Private define ----------------------------------------------------------- */ #define BMI088_REG_ACCL_CHIP_ID (0x00) #define BMI088_REG_ACCL_ERR (0x02) @@ -79,6 +84,10 @@ typedef enum { BMI_GYRO, } BMI_Device_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static uint8_t buffer[2]; static uint8_t bmi088_rxbuf[BMI088_LEN_RX_BUFF]; @@ -87,6 +96,10 @@ static osThreadId_t thread_alert; static bool inited = false; /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + static void BMI_WriteSingle(BMI_Device_t dv, uint8_t reg, uint8_t data) { buffer[0] = (reg & 0x7f); buffer[1] = data; diff --git a/device/bmi088.h b/device/bmi088.h index 0f33ccc..eb44e0c 100644 --- a/device/bmi088.h +++ b/device/bmi088.h @@ -11,6 +11,14 @@ extern "C" { #include "component/ahrs.h" #include "device/device.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ @@ -32,6 +40,10 @@ typedef struct { const BMI088_Cali_t *cali; } BMI088_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Exported functions prototypes -------------------------------------------- */ int8_t BMI088_Init(BMI088_t *bmi088, const BMI088_Cali_t *cali); int8_t BMI088_Restart(void); @@ -60,6 +72,10 @@ int8_t BMI088_ParseAccl(BMI088_t *bmi088); int8_t BMI088_ParseGyro(BMI088_t *bmi088); float BMI088_GetUpdateFreq(BMI088_t *bmi088); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/device/buzzer.c b/device/buzzer.c index 89f616b..013c1a8 100644 --- a/device/buzzer.c +++ b/device/buzzer.c @@ -1,5 +1,13 @@ #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) { if (buzzer == NULL) return DEVICE_ERR; @@ -42,3 +50,7 @@ int8_t BUZZER_Set(BUZZER_t *buzzer, float freq, float duty_cycle) { return result; } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/device/buzzer.h b/device/buzzer.h index 3939e23..f743dff 100644 --- a/device/buzzer.h +++ b/device/buzzer.h @@ -9,14 +9,26 @@ extern "C" { #include "bsp/pwm.h" #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Exported constants ------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported types ----------------------------------------------------------- */ typedef struct { DEVICE_Header_t header; BSP_PWM_Channel_t channel; } BUZZER_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Exported functions prototypes -------------------------------------------- */ 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); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/device/device.h b/device/device.h index 79f1140..3fd8bf5 100644 --- a/device/device.h +++ b/device/device.h @@ -7,6 +7,14 @@ extern "C" { #include #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + #define DEVICE_OK (0) #define DEVICE_ERR (-1) #define DEVICE_ERR_NULL (-2) @@ -26,6 +34,14 @@ typedef struct { uint64_t last_online_time; } DEVICE_Header_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/device/dm_imu.c b/device/dm_imu.c index 96c1feb..e05c1de 100644 --- a/device/dm_imu.c +++ b/device/dm_imu.c @@ -166,7 +166,7 @@ int8_t DM_IMU_Request(DM_IMU_t *imu, DM_IMU_RID_t rid) { .dlc = 4, }; memcpy(frame.data, tx_data, 4); - + BSP_CAN_WaitTxMailboxEmpty(imu->param.can, 1); // 等待发送邮箱空闲 int8_t result = BSP_CAN_TransmitStdDataFrame(imu->param.can, &frame); return (result == BSP_OK) ? DEVICE_OK : DEVICE_ERR; } diff --git a/device/dr16.c b/device/dr16.c index c2c8f95..d2a1c8d 100644 --- a/device/dr16.c +++ b/device/dr16.c @@ -9,11 +9,19 @@ #include "bsp/uart.h" #include "bsp/time.h" + +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ /* Private define ----------------------------------------------------------- */ #define DR16_CH_VALUE_MIN (364u) #define DR16_CH_VALUE_MID (1024u) #define DR16_CH_VALUE_MAX (1684u) +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ /* Private variables -------------------------------------------------------- */ @@ -83,3 +91,7 @@ bool DR16_WaitDmaCplt(uint32_t timeout) { return (osThreadFlagsWait(SIGNAL_DR16_RAW_REDY, osFlagsWaitAll, timeout) == SIGNAL_DR16_RAW_REDY); } + +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ diff --git a/device/dr16.h b/device/dr16.h index 6972647..da21ebf 100644 --- a/device/dr16.h +++ b/device/dr16.h @@ -10,6 +10,14 @@ extern "C" { #include "component/user_math.h" #include "device/device.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ @@ -41,6 +49,9 @@ int8_t DR16_Restart(void); int8_t DR16_StartDmaRecv(DR16_t *dr16); bool DR16_WaitDmaCplt(uint32_t timeout); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ #ifdef __cplusplus } diff --git a/device/ist8310.c b/device/ist8310.c index cb5ae18..fa98506 100644 --- a/device/ist8310.c +++ b/device/ist8310.c @@ -13,6 +13,10 @@ #include "bsp/gpio.h" #include "bsp/i2c.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ #define IST8310_WAI (0x00) #define IST8310_STAT1 (0x02) @@ -31,6 +35,11 @@ #define IST8310_IIC_ADDRESS (0x0E << 1) #define IST8310_LEN_RX_BUFF (6) + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ #define IST8310_SET() \ BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_SET) @@ -38,6 +47,10 @@ BSP_GPIO_WritePin(CMPS_RST_Pin, GPIO_PIN_RESET) /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ uint8_t ist8310_rxbuf[IST8310_LEN_RX_BUFF]; @@ -45,6 +58,10 @@ static osThreadId_t thread_alert; static bool inited = false; /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + static void IST8310_WriteSingle(uint8_t reg, uint8_t data) { BSP_I2C_MemWriteByte(BSP_I2C_COMP, IST8310_IIC_ADDRESS, reg, data); } diff --git a/device/led.c b/device/led.c index 16b893b..eab5735 100644 --- a/device/led.c +++ b/device/led.c @@ -6,29 +6,33 @@ #include "bsp/gpio.h" #include "bsp/pwm.h" #include "device.h" + + /* Private define ----------------------------------------------------------- */ /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ -int8_t LED_PWMSet(BSP_PWM_Channel_t channel,float duty_cycle) -{ - if (duty_cycle < 0.0f || duty_cycle > 1.0f) { - return DEVICE_ERR_NULL; // 错误:占空比超出范围 - } - uint16_t pulse = (uint16_t)(duty_cycle * (float)UINT16_MAX); - BSP_PWM_Start(channel); - BSP_PWM_SetComp(channel, pulse); - return DEVICE_OK; + + +int8_t DEVICE_LED_PWM_Set(BSP_PWM_Channel_t channel, float duty_cycle) +{ + if (duty_cycle < 0.0f || duty_cycle > 1.0f) { + return DEVICE_ERR_NULL; // 错误:占空比超出范围 + } + uint16_t pulse = (uint16_t)(duty_cycle * (float)UINT16_MAX); + BSP_PWM_Start(channel); + BSP_PWM_SetComp(channel, pulse); + return DEVICE_OK; } -int8_t LED_GPIOSet(BSP_GPIO_t gpio,bool value) -{ - BSP_GPIO_WritePin(gpio,value); +int8_t DEVICE_LED_GPIO_Set(BSP_GPIO_t gpio, bool value) +{ + if (value) { + BSP_GPIO_WritePin(gpio, true); + } else { + BSP_GPIO_WritePin(gpio, false); + } return DEVICE_OK; } - - - - diff --git a/device/led.h b/device/led.h index 7765a57..04fdb54 100644 --- a/device/led.h +++ b/device/led.h @@ -14,14 +14,18 @@ extern "C" { /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ + typedef struct { BSP_GPIO_t gpio; BSP_PWM_Channel_t channel; } DEVICE_LED_t; + + extern DEVICE_LED_t LED_Map; /* 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 } diff --git a/device/motor.c b/device/motor.c index ffea060..1fb059d 100644 --- a/device/motor.c +++ b/device/motor.c @@ -7,13 +7,28 @@ #include +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ /* Exported functions ------------------------------------------------------- */ float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor) { diff --git a/device/motor.h b/device/motor.h index 6dc6a85..e1f945b 100644 --- a/device/motor.h +++ b/device/motor.h @@ -7,6 +7,14 @@ extern "C" { /* Includes ----------------------------------------------------------------- */ #include "device/device.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ /* Exported types ----------------------------------------------------------- */ @@ -41,12 +49,20 @@ typedef struct { MOTOR_Feedback_t feedback; } MOTOR_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Exported functions prototypes -------------------------------------------- */ float MOTOR_GetRotorAbsAngle(const MOTOR_t *motor); float MOTOR_GetRotorSpeed(const MOTOR_t *motor); float MOTOR_GetTorqueCurrent(const MOTOR_t *motor); float MOTOR_GetTemp(const MOTOR_t *motor); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } #endif diff --git a/device/motor_lk.c b/device/motor_lk.c index d145868..c7911d6 100644 --- a/device/motor_lk.c +++ b/device/motor_lk.c @@ -10,6 +10,10 @@ #include "bsp/time.h" #include "component/user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ #define LK_CTRL_ID_BASE (0x140) #define LK_FB_ID_BASE (0x240) @@ -36,12 +40,24 @@ #define LK_ENC_15BIT_MAX (32767) // 15位编码器最大值 #define LK_ENC_16BIT_MAX (65535) // 16位编码器最大值 +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static MOTOR_LK_CANManager_t *can_managers[BSP_CAN_NUM] = {NULL}; /* Private functions -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + static float MOTOR_LK_GetCurrentLSB(MOTOR_LK_Module_t module) { switch (module) { case MOTOR_LK_MF9025: @@ -76,11 +92,7 @@ static int8_t MOTOR_LK_CreateCANManager(BSP_CAN_t can) { } 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) { // 如果不是标准反馈命令,可能是其他格式的数据 @@ -90,32 +102,32 @@ static void MOTOR_LK_Decode(MOTOR_LK_t *motor, BSP_CAN_Message_t *msg) { // 解析温度 (DATA[1]) motor->motor.feedback.temp = (int8_t)msg->data[1]; - + // 解析转矩电流值或功率值 (DATA[2], DATA[3]) int16_t raw_current_or_power = (int16_t)((msg->data[3] << 8) | msg->data[2]); - + // 根据电机类型解析电流或功率 switch (motor->param.module) { case MOTOR_LK_MF9025: case MOTOR_LK_MF9035: - // MF/MG电机:转矩电流值 motor->motor.feedback.torque_current = raw_current_or_power * MOTOR_LK_GetCurrentLSB(motor->param.module); break; default: - // MS电机:功率值(范围-1000~1000) - motor->motor.feedback.torque_current = (float)raw_current_or_power; // 将功率存储在torque_current字段中 + motor->motor.feedback.torque_current = (float)raw_current_or_power; break; } - + // 解析转速 (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]) uint16_t raw_encoder = (uint16_t)((msg->data[7] << 8) | msg->data[6]); uint16_t encoder_max = MOTOR_LK_GetEncoderMax(motor->param.module); - + // 将编码器值转换为弧度 (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 ------------------------------------------------------- */ @@ -147,7 +159,7 @@ int8_t MOTOR_LK_Register(MOTOR_LK_Param_t *param) { // 对于某些LK电机,反馈数据可能通过命令ID发送 // 根据实际测试,使用命令ID接收反馈数据 - uint16_t feedback_id = 0x140 + param->id; // 使用命令ID作为反馈ID + uint16_t feedback_id = param->id; // 使用命令ID作为反馈ID // 注册CAN接收ID 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]; if (motor && motor->param.id == param->id) { // 对于某些LK电机,反馈数据通过命令ID发送 - uint16_t feedback_id = 0x140 + param->id; + uint16_t feedback_id = param->id; BSP_CAN_Message_t rx_msg; if (BSP_CAN_GetMessage(param->can, feedback_id, &rx_msg, BSP_CAN_TIMEOUT_IMMEDIATE) != BSP_OK) { @@ -211,35 +223,37 @@ int8_t MOTOR_LK_UpdateAll(void) { int8_t MOTOR_LK_SetOutput(MOTOR_LK_Param_t *param, float value) { if (param == NULL) return DEVICE_ERR_NULL; - + MOTOR_LK_CANManager_t *manager = MOTOR_LK_GetCANManager(param->can); if (manager == NULL) return DEVICE_ERR_NO_DEV; - + // 限制输出值范围 if (value > 1.0f) value = 1.0f; if (value < -1.0f) value = -1.0f; - + MOTOR_LK_t *motor = MOTOR_LK_GetMotor(param); if (motor == NULL) return DEVICE_ERR_NO_DEV; - + + // 根据反转参数调整输出 + float output = param->reverse ? -value : value; + // 转矩闭环控制命令 - 将输出值转换为转矩控制值 - int16_t torque_control = (int16_t)(value * (float)LK_TORQUE_RANGE); - - // 构建CAN帧(根据协议:命令报文标识符 = 0x140 + ID) + int16_t torque_control = (int16_t)(output * (float)LK_TORQUE_RANGE); + + // 构建CAN帧 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.data[0] = LK_CMD_TORQUE_CTRL; // 命令字节 - tx_frame.data[1] = 0x00; // NULL - tx_frame.data[2] = 0x00; // NULL - tx_frame.data[3] = 0x00; // NULL - tx_frame.data[4] = (uint8_t)(torque_control & 0xFF); // 转矩电流控制值低字节 - tx_frame.data[5] = (uint8_t)((torque_control >> 8) & 0xFF); // 转矩电流控制值高字节 - tx_frame.data[6] = 0x00; // NULL - tx_frame.data[7] = 0x00; // NULL - + + tx_frame.data[0] = LK_CMD_TORQUE_CTRL; + tx_frame.data[1] = 0x00; + tx_frame.data[2] = 0x00; + tx_frame.data[3] = 0x00; + tx_frame.data[4] = (uint8_t)(torque_control & 0xFF); + tx_frame.data[5] = (uint8_t)((torque_control >> 8) & 0xFF); + tx_frame.data[6] = 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; } @@ -253,7 +267,7 @@ int8_t MOTOR_LK_MotorOn(MOTOR_LK_Param_t *param) { if (param == NULL) return DEVICE_ERR_NULL; BSP_CAN_StdDataFrame_t tx_frame; - tx_frame.id = 0x140 + param->id; + tx_frame.id = param->id; 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[6] = 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; } @@ -273,7 +287,7 @@ int8_t MOTOR_LK_MotorOff(MOTOR_LK_Param_t *param) { if (param == NULL) return DEVICE_ERR_NULL; BSP_CAN_StdDataFrame_t tx_frame; - tx_frame.id = 0x140 + param->id; + tx_frame.id = param->id; 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[6] = 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; } diff --git a/device/motor_lz.c b/device/motor_lz.c index e9b6607..eb55f36 100644 --- a/device/motor_lz.c +++ b/device/motor_lz.c @@ -1,5 +1,10 @@ /* 灵足电机驱动 + + 灵足电机通信协议: + - CAN 2.0通信接口,波特率1Mbps + - 采用扩展帧格式(29位ID) + - ID格式:Bit28~24(通信类型) + Bit23~8(数据区2) + Bit7~0(目标地址) */ /* Includes ----------------------------------------------------------------- */ #include "motor_lz.h" @@ -22,17 +27,17 @@ #define LZ_RAW_VALUE_MAX (65535) /* 16位原始值最大值 */ #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_RX_BUF_SIZE (8) /* Private macro ------------------------------------------------------------ */ -MOTOR_LZ_MotionParam_t lz_recover_param = { +MOTOR_LZ_MotionParam_t lz_relax_param = { .target_angle = 0.0f, .target_velocity = 0.0f, - .kp = 20.0f, - .kd = 1.0f, + .kp = 0.0f, + .kd = 0.0f, .torque = 0.0f, }; /* Private typedef ---------------------------------------------------------- */ @@ -129,7 +134,7 @@ static int8_t MOTOR_LZ_SendExtFrame(BSP_CAN_t can, uint32_t ext_id, uint8_t *dat } else { memset(tx_frame.data, 0, dlc); } - + BSP_CAN_WaitTxMailboxEmpty(can, 1); // 等待发送邮箱空闲 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) { if (motor == NULL || msg == NULL) return; - - // 检查是否为反馈数据帧 (通信类型2) - // 需要使用原始ID来解析,因为parsed_id已经被IdParser处理过了 uint8_t cmd_type = (msg->original_id >> 24) & 0x1F; if (cmd_type != MOTOR_LZ_CMD_FEEDBACK) return; - - // 解析原始ID中的数据区2 (bit23~8) uint16_t id_data2 = (msg->original_id >> 8) & 0xFFFF; - uint8_t motor_can_id = id_data2 & 0xFF; // Bit8~15: 当前电机CAN ID - uint8_t fault_info = (id_data2 >> 8) & 0x3F; // Bit16~21: 故障信息 - uint8_t mode_state = (id_data2 >> 14) & 0x03; // Bit22~23: 模式状态 - - // 更新电机CAN ID + uint8_t motor_can_id = id_data2 & 0xFF; + uint8_t fault_info = (id_data2 >> 8) & 0x3F; + uint8_t mode_state = (id_data2 >> 14) & 0x03; motor->lz_feedback.motor_can_id = motor_can_id; - - // 解析故障信息 - motor->lz_feedback.fault.under_voltage = (fault_info & 0x01) != 0; // bit16 - motor->lz_feedback.fault.driver_fault = (fault_info & 0x02) != 0; // bit17 - motor->lz_feedback.fault.over_temp = (fault_info & 0x04) != 0; // bit18 - motor->lz_feedback.fault.encoder_fault = (fault_info & 0x08) != 0; // bit19 - motor->lz_feedback.fault.stall_overload = (fault_info & 0x10) != 0; // bit20 - motor->lz_feedback.fault.uncalibrated = (fault_info & 0x20) != 0; // bit21 - - // 解析模式状态 + motor->lz_feedback.fault.under_voltage = (fault_info & 0x01) != 0; + motor->lz_feedback.fault.driver_fault = (fault_info & 0x02) != 0; + motor->lz_feedback.fault.over_temp = (fault_info & 0x04) != 0; + motor->lz_feedback.fault.encoder_fault = (fault_info & 0x08) != 0; + motor->lz_feedback.fault.stall_overload = (fault_info & 0x10) != 0; + motor->lz_feedback.fault.uncalibrated = (fault_info & 0x20) != 0; 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]); - motor->lz_feedback.current_angle = MOTOR_LZ_RawToFloat(raw_angle, LZ_ANGLE_RANGE_RAD); - - // Byte2~3: 当前角速度 (高字节在前,低字节在后) + float angle = MOTOR_LZ_RawToFloat(raw_angle, LZ_ANGLE_RANGE_RAD); 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); - - // Byte4~5: 当前力矩 (高字节在前,低字节在后) + float velocity = MOTOR_LZ_RawToFloat(raw_velocity, LZ_VELOCITY_RANGE_RAD_S); 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); - - // Byte6~7: 当前温度 (温度*10) (高字节在前,低字节在后) + 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; + uint16_t raw_temp = (uint16_t)((msg->data[6] << 8) | msg->data[7]); motor->lz_feedback.temperature = (float)raw_temp / LZ_TEMP_SCALE; - - // 更新通用电机反馈信息 - motor->motor.feedback.rotor_abs_angle = motor->lz_feedback.current_angle; - motor->motor.feedback.rotor_speed = motor->lz_feedback.current_velocity * 180.0f / M_PI * 6.0f; // 转换为RPM - motor->motor.feedback.torque_current = motor->lz_feedback.current_torque; // 使用力矩作为电流反馈 + + motor->motor.feedback.rotor_abs_angle = angle; + motor->motor.feedback.rotor_speed = velocity; + motor->motor.feedback.torque_current = torque; motor->motor.feedback.temp = (int8_t)motor->lz_feedback.temperature; - - // 更新在线状态 motor->motor.header.online = true; 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) { if (param == NULL || motion_param == NULL) return DEVICE_ERR_NULL; - MOTOR_LZ_t *motor = MOTOR_LZ_GetMotor(param); 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)); - - // 根据协议,bit23~8数据区2包含力矩信息 - // 力矩范围:-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 + + uint16_t raw_torque = MOTOR_LZ_FloatToRaw(send_param.torque, LZ_TORQUE_RANGE_NM); uint32_t ext_id = MOTOR_LZ_BuildExtID(MOTOR_LZ_CMD_MOTION, raw_torque, param->motor_id); - - // 准备8字节数据区 uint8_t data[8]; - - // Byte0~1: 目标角度 [0~65535] 对应 (-12.57f~12.57f rad) (高字节在前,低字节在后) - uint16_t raw_angle = MOTOR_LZ_FloatToRaw(motion_param->target_angle, LZ_ANGLE_RANGE_RAD); - data[0] = (raw_angle >> 8) & 0xFF; // 高字节 - data[1] = raw_angle & 0xFF; // 低字节 - - // Byte2~3: 目标角速度 [0~65535] 对应 (-20rad/s~20rad/s) (高字节在前,低字节在后) - uint16_t raw_velocity = MOTOR_LZ_FloatToRaw(motion_param->target_velocity, LZ_VELOCITY_RANGE_RAD_S); - data[2] = (raw_velocity >> 8) & 0xFF; // 高字节 - data[3] = raw_velocity & 0xFF; // 低字节 - - // Byte4~5: Kp [0~65535] 对应 (0.0~5000.0) (高字节在前,低字节在后) - uint16_t raw_kp = MOTOR_LZ_FloatToRawPositive(motion_param->kp, LZ_KP_MAX); - 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; // 低字节 - + uint16_t raw_angle = MOTOR_LZ_FloatToRaw(send_param.target_angle, LZ_ANGLE_RANGE_RAD); + data[0] = (raw_angle >> 8) & 0xFF; + data[1] = raw_angle & 0xFF; + uint16_t raw_velocity = MOTOR_LZ_FloatToRaw(send_param.target_velocity, LZ_VELOCITY_RANGE_RAD_S); + data[2] = (raw_velocity >> 8) & 0xFF; + data[3] = raw_velocity & 0xFF; + uint16_t raw_kp = MOTOR_LZ_FloatToRawPositive(send_param.kp, LZ_KP_MAX); + data[4] = (raw_kp >> 8) & 0xFF; + data[5] = raw_kp & 0xFF; + uint16_t raw_kd = MOTOR_LZ_FloatToRawPositive(send_param.kd, LZ_KD_MAX); + data[6] = (raw_kd >> 8) & 0xFF; + data[7] = raw_kd & 0xFF; + BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲 return MOTOR_LZ_SendExtFrame(param->can, ext_id, data, 8); } + int8_t MOTOR_LZ_Enable(MOTOR_LZ_Param_t *param) { 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) { - return MOTOR_LZ_Disable(param, false); + return MOTOR_LZ_MotionControl(param, &lz_relax_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; } - -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); -} \ No newline at end of file diff --git a/device/motor_lz.h b/device/motor_lz.h index 8bc3bd1..76a72ae 100644 --- a/device/motor_lz.h +++ b/device/motor_lz.h @@ -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_RecoverToZero(MOTOR_LZ_Param_t *param); - #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/device/motor_rm.c b/device/motor_rm.c index 49a867e..4c63810 100644 --- a/device/motor_rm.c +++ b/device/motor_rm.c @@ -10,6 +10,10 @@ #include "bsp/time.h" #include "component/user_math.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ #define GM6020_FB_ID_BASE (0x205) #define GM6020_CTRL_ID_BASE (0x1ff) @@ -30,11 +34,24 @@ #define MOTOR_ENC_RES (8192) /* 电机编码器分辨率 */ #define MOTOR_CUR_RES (16384) /* 电机转矩电流分辨率 */ +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ 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) { switch (module) { case MOTOR_M2006: @@ -223,6 +240,7 @@ int8_t MOTOR_RM_Ctrl(MOTOR_RM_Param_t *param) { default: return DEVICE_ERR; } + BSP_CAN_WaitTxMailboxEmpty(param->can, 1); // 等待发送邮箱空闲 return BSP_CAN_TransmitStdDataFrame(param->can, &tx_frame) == BSP_OK ? DEVICE_OK : DEVICE_ERR; } diff --git a/device/servo.c b/device/servo.c index 85cdcd7..7981bf7 100644 --- a/device/servo.c +++ b/device/servo.c @@ -1,5 +1,5 @@ /* - pwmƶ + pwm���ƶ�� */ /*Includes -----------------------------------------*/ @@ -7,9 +7,17 @@ #include "bsp/pwm.h" #include "servo.h" +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + #define SERVO_MIN_DUTY 0.025f #define SERVO_MAX_DUTY 0.125f +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /** * @brief * @param @@ -24,10 +32,10 @@ int8_t SERVO_Init(SERVO_t *servo) { int8_t SERVO_SetAngle(SERVO_t *servo, float angle) { if (servo == NULL) return BSP_ERR; - /*ƽǶȷΧ*/ + /*���ƽǶȷ�Χ*/ if (angle < 0.0f) angle = 0.0f; if (angle > 180.0f) angle = 180.0f; - /*Ƕӳ䵽ռձ*/ + /*�Ƕ�ӳ�䵽ռ�ձ�*/ float duty = servo->min_duty + (angle / 180.0f) * (servo->max_duty - servo->min_duty); return BSP_PWM_Set(servo->pwm_ch, duty); diff --git a/device/servo.h b/device/servo.h index e59ac4e..ba8562d 100644 --- a/device/servo.h +++ b/device/servo.h @@ -8,6 +8,14 @@ extern "C" { #include #include "bsp/pwm.h" + +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ /* Exported constants ------------------------------------------------------- */ /* Exported macro ----------------------------------------------------------- */ @@ -22,6 +30,10 @@ typedef struct { float max_duty; } SERVO_t; +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /** * @brief * @param servo @@ -46,6 +58,10 @@ int8_t SERVO_SetAngle(SERVO_t *servo, float angle); int8_t SERVO_Stop(SERVO_t *servo); +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + #ifdef __cplusplus } diff --git a/device/vofa.c b/device/vofa.c index 0cd4c82..8561042 100644 --- a/device/vofa.c +++ b/device/vofa.c @@ -3,18 +3,35 @@ #include #include "device/vofa.h" #include "bsp/uart.h" + +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* Private define ----------------------------------------------------------- */ #define MAX_CHANNEL 64u // 根据实际最大通道数调整 #define JUSTFLOAT_TAIL 0x7F800000 + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static uint8_t vofa_tx_buf[sizeof(float) * MAX_CHANNEL + sizeof(uint32_t)]; static VOFA_Protocol_t current_protocol = VOFA_PROTOCOL_FIREWATER; // 默认协议 /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ /************************ RawData *************************/ void VOFA_RawData_Send(const char* data, bool dma) { diff --git a/device/ws2812.c b/device/ws2812.c index 45747b9..101edcb 100644 --- a/device/ws2812.c +++ b/device/ws2812.c @@ -4,18 +4,36 @@ #include "bsp/pwm.h" #include + +/* USER INCLUDE BEGIN */ + +/* USER INCLUDE END */ + /* 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_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_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 + +/* USER DEFINE BEGIN */ + +/* USER DEFINE END */ + /* Private macro ------------------------------------------------------------ */ /* Private typedef ---------------------------------------------------------- */ +/* USER STRUCT BEGIN */ + +/* USER STRUCT END */ + /* Private variables -------------------------------------------------------- */ static uint16_t DEVICE_WS2812_LED_NUM; // Total number of LEDs static uint16_t *DEVICE_WS2812_RGB_Buff = NULL;// PWM duty buffer for DMA /* Private function -------------------------------------------------------- */ +/* USER FUNCTION BEGIN */ + +/* USER FUNCTION END */ + /* Exported functions ------------------------------------------------------- */ /** * Set color of a single WS2812 LED