From e26c0c3aae883e8781e77cf8ff6378c312e3823b Mon Sep 17 00:00:00 2001 From: ws <1621320660@qq.com> Date: Tue, 22 Apr 2025 15:15:35 +0800 Subject: [PATCH] =?UTF-8?q?vesc=E7=9A=84bug=E6=B6=88=E9=99=A4=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- User/device/detect.c | 2 +- User/device/djiMotor.c | 62 +++++++++++++++++++++++++++++++---------- User/device/djiMotor.h | 21 ++++++++++++++ User/module/shoot.cpp | 27 ++++++++++++++---- User/module/shoot.hpp | 7 +---- User/task/shootTask.cpp | 2 +- 6 files changed, 93 insertions(+), 28 deletions(-) diff --git a/User/device/detect.c b/User/device/detect.c index f4c3961..2775d38 100644 --- a/User/device/detect.c +++ b/User/device/detect.c @@ -24,7 +24,7 @@ void detect_init(void) { BSP_GPIO_RegisterCallback(KEY_Pin, detect_exit); - BSP_GPIO_RegisterCallback(ext_up_Pin, detect_led); + //BSP_GPIO_RegisterCallback(ext_up_Pin, detect_led); // 注册按键中断回调函数 if (BSP_GPIO_RegisterCallback(KEY_Pin, detect_exit) != BSP_OK) { // 错误处理 diff --git a/User/device/djiMotor.c b/User/device/djiMotor.c index f22788d..eb2fc65 100644 --- a/User/device/djiMotor.c +++ b/User/device/djiMotor.c @@ -53,28 +53,18 @@ (ptr)->offset_ecd = (ptr)->ecd; \ } - void VescMotor_Decode(CAN_MotorFeedback_t *feedback, CAN_RxHeaderTypeDef *rx_header, const uint8_t *rx_data) + static void VescMotor_Decode(CAN_MotorFeedback_t *feedback, CAN_RxHeaderTypeDef *rx_header, const uint8_t *rx_data) { - // 检查接收到的ID是否匹配我们期望的VESC状态消息ID - if (rx_header->ExtId == CAN_VESC5065_M1_MSG1) // 电机 1 的消息 ID - { + // 检查接收到的ID是否匹配我们期望的VESC状态消息ID // 提取数据字段 feedback->rotor_speed = ((int32_t)(rx_data[0]) << 24) | ((int32_t)(rx_data[1]) << 16) | ((int32_t)(rx_data[2]) << 8) | ((int32_t)(rx_data[3])); feedback->torque_current = ((int16_t)(rx_data[5]) << 8) | (int16_t)(rx_data[4]); feedback->torque_current /= 1000; // 将单位从 0.1A 转换为实际电流值 feedback->duty_cycle = ((int16_t)(rx_data[7]) << 8) | (int16_t)(rx_data[6]); feedback->duty_cycle /= 1000.0f; // 将单位从千分之一转换为实际的占空比值 (-1.0 到 1.0) - } - else if (rx_header->ExtId == CAN_VESC5065_M1_MSG1) // 电机 2 的消息 ID - { - // 提取数据字段 - feedback->rotor_speed = ((int32_t)(rx_data[0]) << 24) | ((int32_t)(rx_data[1]) << 16) | ((int32_t)(rx_data[2]) << 8) | ((int32_t)(rx_data[3])); - feedback->torque_current = ((int16_t)(rx_data[5]) << 8) | (int16_t)(rx_data[4]); - feedback->torque_current /= 1000; // 将单位从 0.1A 转换为实际电流值 - feedback->duty_cycle = ((int16_t)(rx_data[7]) << 8) | (int16_t)(rx_data[6]); - feedback->duty_cycle /= 1000.0f; // 将单位从千分之一转换为实际的占空比值 (-1.0 到 1.0) - } + } + #if DEBUG == 1 //电机回传数据结构体 @@ -98,6 +88,8 @@ uint8_t rx_data[8]; static CAN_TxHeaderTypeDef vesc_tx_message; static uint8_t vesc_send_data[4]; + +CAN_RawTx_t raw_tx; /** * @brief 数据处理函数 * @param[in] none @@ -298,6 +290,48 @@ void CAN_cmd_2FF(int16_t motor1, int16_t motor2, int16_t motor3, CAN_HandleTypeD return &motor_chassis[i]; } + int8_t CAN_VESC_Control(int id,int speed ,CAN_HandleTypeDef*hcan) + { + + uint32_t send_mail_box; + int Byte[4]; + Vesc_ByteGet raw[4]; + + //将期望的四个电机输出值分别对应到四个联合体 为了下面的拆分字节 + raw[0].as_array = speed; + raw[1].as_array = speed; + + for(int i=0 ; i < 2 ; i ++) + { + + //将单个电机的期望输出值通过联合体拆分 + Byte[0] = raw[i].byte.byte1; + Byte[1] = raw[i].byte.byte2; + Byte[2] = raw[i].byte.byte3; + Byte[3] = raw[i].byte.byte4; + + raw_tx.tx_header.ExtId = id+i+CAN_VESC_CTRL_ID_BASE; + raw_tx.tx_header.IDE = CAN_ID_EXT; + raw_tx.tx_header.RTR = CAN_RTR_DATA; + raw_tx.tx_header.DLC = CAN_MOTOR_TX_BUF_SIZE; + + raw_tx.tx_data[0] = Byte[3]; + raw_tx.tx_data[1] = Byte[2]; + raw_tx.tx_data[2] = Byte[1]; + raw_tx.tx_data[3] = Byte[0]; + raw_tx.tx_data[4] = 0; + raw_tx.tx_data[5] = 0; + raw_tx.tx_data[6] = 0; + raw_tx.tx_data[7] = 0; + + HAL_CAN_AddTxMessage(hcan, &raw_tx.tx_header, raw_tx.tx_data, &send_mail_box); + } + +return DEVICE_OK; + +} + + /** * @brief 限制vesc电机转速 * @param[in/out] rpm: vesce电机转速 diff --git a/User/device/djiMotor.h b/User/device/djiMotor.h index 9fc5b66..982e1e2 100644 --- a/User/device/djiMotor.h +++ b/User/device/djiMotor.h @@ -7,6 +7,7 @@ extern "C"{ #define FREERTOS_DJI 1 #include "struct_typedef.h" +#include "device.h" #include "can.h" #include @@ -17,6 +18,21 @@ typedef enum{ M2006 = 2 }motor_type_e; +typedef union{ + int as_array; + struct{ + int8_t byte1; + int8_t byte2; + int8_t byte3; + int8_t byte4; + }byte; +}Vesc_ByteGet; + +typedef struct { + CAN_TxHeaderTypeDef tx_header; + uint8_t tx_data[8]; +} CAN_RawTx_t; + enum{ M3508_1 = 0x201, M3508_2 = 0x202, @@ -60,6 +76,9 @@ typedef struct { #define wtrcfg_VESC_COMMAND_ERPM_MAX 55000 //限速 #define CAN_VESC_CTRL_ID_BASE (0x300) +#define CAN_MOTOR_TX_BUF_SIZE (8) +#define CAN_MOTOR_RX_BUF_SIZE (8) + //rm motor data typedef struct { @@ -146,6 +165,8 @@ void CAN_VESC_RPM(uint8_t controller_id, float RPM); void CAN_VESC_HEAD(uint8_t controller_id); +int8_t CAN_VESC_Control(int id,int speed ,CAN_HandleTypeDef*hcan); + #ifdef __cplusplus } #endif diff --git a/User/module/shoot.cpp b/User/module/shoot.cpp index c49c11e..0b31659 100644 --- a/User/module/shoot.cpp +++ b/User/module/shoot.cpp @@ -15,6 +15,8 @@ ShootState_t currentState = SHOOT_IDLE; Shoot::Shoot() { + // motor_5065[0] = get_5065_motor_point(0);//获取电机数据指针 + // motor_5065[1] = get_5065_motor_point(1);//获取电机数据指针 CAN_VESC_RPM(1, STOP); CAN_VESC_RPM(2, STOP); @@ -25,15 +27,28 @@ void Shoot::shootThree() if((rc_ctrl.sw[1]>500)) { speed_5065 = map((float)rc_ctrl.sw[1],500,1694,30000,45000); - CAN_VESC_RPM(1, speed_5065); - CAN_VESC_RPM(2, speed_5065); + + // CAN_VESC_RPM(1, speed_5065); + // CAN_VESC_RPM(2, speed_5065); } else { - CAN_VESC_RPM(1, STOP); - CAN_VESC_RPM(2, STOP); + speed_5065=STOP; + // CAN_VESC_Control(1,STOP,&hcan2); + // CAN_VESC_RPM(1, STOP); + // CAN_VESC_RPM(2, STOP); } - + if(rc_ctrl.sw[5]==1694) + { + speed_5065=SHOOT_SPEED_BACK; + // CAN_VESC_Control(1,SHOOT_SPEED_BACK,&hcan2); + // CAN_VESC_RPM(1, SHOOT_SPEED_BACK); + // CAN_VESC_RPM(2, SHOOT_SPEED_BACK); + + } + //CAN_VESC_Control(1,speed_5065,&hcan2); + CAN_VESC_RPM(1, speed_5065); + CAN_VESC_RPM(2, speed_5065); } void Shoot::shootBack() @@ -42,7 +57,7 @@ void Shoot::shootBack() { CAN_VESC_RPM(1, SHOOT_SPEED_BACK); - CAN_VESC_RPM(2, -5000); + CAN_VESC_RPM(2, SHOOT_SPEED_BACK); } } diff --git a/User/module/shoot.hpp b/User/module/shoot.hpp index 62a23c3..89cea5a 100644 --- a/User/module/shoot.hpp +++ b/User/module/shoot.hpp @@ -26,12 +26,7 @@ public: float speed_5065; private: - - float accelLeft; - float accelRight; - float accelZero; - uint8_t flagRightShoot; - uint8_t flagLeftShoot; + CAN_MotorFeedback_t *motor_5065[2]; }; diff --git a/User/task/shootTask.cpp b/User/task/shootTask.cpp index 7e92bd8..57cf53c 100644 --- a/User/task/shootTask.cpp +++ b/User/task/shootTask.cpp @@ -26,7 +26,7 @@ while(1) #endif shoot.shootThree(); - shoot.shootBack(); + //shoot.shootBack(); osDelay(2);