vesc的bug消除了

This commit is contained in:
ws 2025-04-22 15:15:35 +08:00
parent 36ad4c7c96
commit e26c0c3aae
6 changed files with 93 additions and 28 deletions

View File

@ -24,7 +24,7 @@ void detect_init(void)
{ {
BSP_GPIO_RegisterCallback(KEY_Pin, detect_exit); 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) { if (BSP_GPIO_RegisterCallback(KEY_Pin, detect_exit) != BSP_OK) {
// 错误处理 // 错误处理

View File

@ -53,28 +53,18 @@
(ptr)->offset_ecd = (ptr)->ecd; \ (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 // 检查接收到的ID是否匹配我们期望的VESC状态消息ID
if (rx_header->ExtId == CAN_VESC5065_M1_MSG1) // 电机 1 的消息 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->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 = ((int16_t)(rx_data[5]) << 8) | (int16_t)(rx_data[4]);
feedback->torque_current /= 1000; // 将单位从 0.1A 转换为实际电流值 feedback->torque_current /= 1000; // 将单位从 0.1A 转换为实际电流值
feedback->duty_cycle = ((int16_t)(rx_data[7]) << 8) | (int16_t)(rx_data[6]); feedback->duty_cycle = ((int16_t)(rx_data[7]) << 8) | (int16_t)(rx_data[6]);
feedback->duty_cycle /= 1000.0f; // 将单位从千分之一转换为实际的占空比值 (-1.0 到 1.0) 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 #if DEBUG == 1
//电机回传数据结构体 //电机回传数据结构体
@ -98,6 +88,8 @@ uint8_t rx_data[8];
static CAN_TxHeaderTypeDef vesc_tx_message; static CAN_TxHeaderTypeDef vesc_tx_message;
static uint8_t vesc_send_data[4]; static uint8_t vesc_send_data[4];
CAN_RawTx_t raw_tx;
/** /**
* @brief * @brief
* @param[in] none * @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]; 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电机转速 * @brief vesc电机转速
* @param[in/out] rpm: vesce电机转速 * @param[in/out] rpm: vesce电机转速

View File

@ -7,6 +7,7 @@ extern "C"{
#define FREERTOS_DJI 1 #define FREERTOS_DJI 1
#include "struct_typedef.h" #include "struct_typedef.h"
#include "device.h"
#include "can.h" #include "can.h"
#include <math.h> #include <math.h>
@ -17,6 +18,21 @@ typedef enum{
M2006 = 2 M2006 = 2
}motor_type_e; }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{ enum{
M3508_1 = 0x201, M3508_1 = 0x201,
M3508_2 = 0x202, M3508_2 = 0x202,
@ -60,6 +76,9 @@ typedef struct {
#define wtrcfg_VESC_COMMAND_ERPM_MAX 55000 //限速 #define wtrcfg_VESC_COMMAND_ERPM_MAX 55000 //限速
#define CAN_VESC_CTRL_ID_BASE (0x300) #define CAN_VESC_CTRL_ID_BASE (0x300)
#define CAN_MOTOR_TX_BUF_SIZE (8)
#define CAN_MOTOR_RX_BUF_SIZE (8)
//rm motor data //rm motor data
typedef struct typedef struct
{ {
@ -146,6 +165,8 @@ void CAN_VESC_RPM(uint8_t controller_id, float RPM);
void CAN_VESC_HEAD(uint8_t controller_id); void CAN_VESC_HEAD(uint8_t controller_id);
int8_t CAN_VESC_Control(int id,int speed ,CAN_HandleTypeDef*hcan);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -15,6 +15,8 @@ ShootState_t currentState = SHOOT_IDLE;
Shoot::Shoot() 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(1, STOP);
CAN_VESC_RPM(2, STOP); CAN_VESC_RPM(2, STOP);
@ -25,15 +27,28 @@ void Shoot::shootThree()
if((rc_ctrl.sw[1]>500)) if((rc_ctrl.sw[1]>500))
{ {
speed_5065 = map((float)rc_ctrl.sw[1],500,1694,30000,45000); 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 else
{ {
CAN_VESC_RPM(1, STOP); speed_5065=STOP;
CAN_VESC_RPM(2, 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() void Shoot::shootBack()
@ -42,7 +57,7 @@ void Shoot::shootBack()
{ {
CAN_VESC_RPM(1, SHOOT_SPEED_BACK); CAN_VESC_RPM(1, SHOOT_SPEED_BACK);
CAN_VESC_RPM(2, -5000); CAN_VESC_RPM(2, SHOOT_SPEED_BACK);
} }
} }

View File

@ -26,12 +26,7 @@ public:
float speed_5065; float speed_5065;
private: private:
CAN_MotorFeedback_t *motor_5065[2];
float accelLeft;
float accelRight;
float accelZero;
uint8_t flagRightShoot;
uint8_t flagLeftShoot;
}; };

View File

@ -26,7 +26,7 @@ while(1)
#endif #endif
shoot.shootThree(); shoot.shootThree();
shoot.shootBack(); //shoot.shootBack();
osDelay(2); osDelay(2);