diff --git a/MDK-ARM/.vscode/keil-assistant.log b/MDK-ARM/.vscode/keil-assistant.log index 1ea276a..a8648ec 100644 --- a/MDK-ARM/.vscode/keil-assistant.log +++ b/MDK-ARM/.vscode/keil-assistant.log @@ -96,3 +96,7 @@ [info] Log at : 2025/5/22|16:11:31|GMT+0800 +[info] Log at : 2025/5/22|19:37:30|GMT+0800 + +[info] Log at : 2025/5/23|20:58:39|GMT+0800 + diff --git a/MDK-ARM/.vscode/uv4.log b/MDK-ARM/.vscode/uv4.log index 8ba7939..6f66c5a 100644 --- a/MDK-ARM/.vscode/uv4.log +++ b/MDK-ARM/.vscode/uv4.log @@ -1,8 +1,8 @@ *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\keil\ARM\ARMCC\Bin' Build target 'R1-shooter' -compiling ball.cpp... +compiling shootTask.cpp... linking... -Program Size: Code=28460 RO-data=1824 RW-data=256 ZI-data=23936 +Program Size: Code=28164 RO-data=1824 RW-data=264 ZI-data=23976 FromELF: creating hex file... "R1-shooter\R1-shooter.axf" - 0 Error(s), 0 Warning(s). -Build Time Elapsed: 00:00:02 +Build Time Elapsed: 00:00:05 diff --git a/MDK-ARM/.vscode/uv4.log.lock b/MDK-ARM/.vscode/uv4.log.lock index ea230dc..ce9fb9a 100644 --- a/MDK-ARM/.vscode/uv4.log.lock +++ b/MDK-ARM/.vscode/uv4.log.lock @@ -1 +1 @@ -2025/5/20 23:55:49 \ No newline at end of file +2025/5/22 20:12:51 \ No newline at end of file diff --git a/MDK-ARM/R1-shooter.uvoptx b/MDK-ARM/R1-shooter.uvoptx index e401a6a..21c9a64 100644 --- a/MDK-ARM/R1-shooter.uvoptx +++ b/MDK-ARM/R1-shooter.uvoptx @@ -103,7 +103,7 @@ 1 0 0 - 3 + 6 @@ -114,7 +114,7 @@ - BIN\CMSIS_AGDI.dll + STLink\ST-LINKIII-KEIL_SWO.dll @@ -140,7 +140,7 @@ 0 DLGUARM - + (105=-1,-1,-1,-1,0) 0 @@ -255,6 +255,31 @@ 1 angle1,0x0A + + 20 + 1 + speed5,0x0A + + + 21 + 1 + xxxxx,0x0A + + + 22 + 1 + rx_header + + + 23 + 1 + rx_data + + + 24 + 1 + motor_5065 + @@ -985,7 +1010,7 @@ User/device - 0 + 1 0 0 0 @@ -1065,7 +1090,7 @@ User/module - 1 + 0 0 0 0 diff --git a/User/device/djiMotor.c b/User/device/djiMotor.c index f92fe83..2b42bc5 100644 --- a/User/device/djiMotor.c +++ b/User/device/djiMotor.c @@ -78,11 +78,18 @@ } +//小米 +#define ID_xiaomi 1 +#define Pmax 1 +#define speed_full_scale 200.0f +#define current_full_scale 4.0f + + #if DEBUG == 1 //电机回传数据结构体 motor_measure_t motor_chassis[10]; -CAN_MotorFeedback_t motor_5065[2]; +CAN_MotorFeedback_t motor_5065[3]; #else static motor_measure_t motor_chassis[5]; #endif @@ -93,16 +100,25 @@ static uint8_t can_send_data_2ff[8]; static CAN_TxHeaderTypeDef tx_message_200; static uint8_t can_send_data_200[8]; +//can1 dji CAN_RxHeaderTypeDef dji_rx_header; uint8_t dji_rx_data[8]; +CAN_RawTx_t raw_tx; + +//can2 vesc xiaomi CAN_RxHeaderTypeDef rx_header; uint8_t rx_data[8]; static CAN_TxHeaderTypeDef vesc_tx_message; static uint8_t vesc_send_data[4]; +//小米 +uint8_t can_send_data[8]; +// 定义 CAN 传输头部信息 +static CAN_TxHeaderTypeDef tx_message; + +MotorFeedback_t MotorFeedback; -CAN_RawTx_t raw_tx; /** * @brief 数据处理函数 * @param[in] none @@ -168,15 +184,28 @@ void can2MotorEncode() // 存储消息到对应的电机结构体中 VescMotor_Decode(&motor_5065[0], &rx_header,rx_data); break; - case CAN_VESC5065_M2_MSG1: // 存储消息到对应的电机结构体中 VescMotor_Decode(&motor_5065[1], &rx_header,rx_data); + case CAN_VESC5065_M3_MSG1: + // 存储消息到对应的电机结构体中 + VescMotor_Decode(&motor_5065[2], &rx_header,rx_data); break; - + default: break; } + + switch (rx_data[0]) + { + case ID_xiaomi: + Parse_CAN_Response (rx_data,&MotorFeedback); + break ; + + default: + break; + } + } #if FREERTOS_DJI == 0 @@ -437,3 +466,70 @@ return DEVICE_OK; { return &motor_5065[i]; } + + + //小米 + void Parse_CAN_Response(uint8_t* rx_data, MotorFeedback_t* feedback) { + // 1. 解析位置 + int16_t pos_code = (rx_data[1] << 8) | rx_data[2]; + feedback->position_deg = ((int16_t)(pos_code - 0x8000) / 32768.0f) * 360.0f * Pmax; + + // 2. 解析速度 + int16_t speed_code = ((rx_data[3] << 4) | (rx_data[4] >> 4)) - 0x800; + feedback->speed_rads = (speed_code / 2048.0f) * speed_full_scale; + + // 3. 解析电流 + int16_t current_code = (((rx_data[4] & 0x0F) << 8) | rx_data[5]) - 0x800; + feedback->current_A = (current_code / 2048.0f) * current_full_scale; + + // 记录电机ID + feedback->can_id = rx_data[0]; +} + + +void CAN_cmd(int id,JZ_xiaomi_t *JZ_xiaomi) +{ + uint32_t send_mail_box; + static int is_open=0; //是否开启电机,根据手册,第一次使用需要ff开启 + tx_message.StdId = id; + tx_message.IDE = CAN_ID_STD; + tx_message.RTR = CAN_RTR_DATA; + tx_message.DLC = 0x08; + if(is_open){ + + uint16_t pos_code = (uint16_t)((JZ_xiaomi->position / (Pmax * 360.0f)) * 32768.0f + 32768.0f); + uint16_t speed_code = (uint16_t)((JZ_xiaomi->speed / 200.0f) * 2048.0f + 2048.0f); + uint16_t current_code = (uint16_t)((JZ_xiaomi->K_C / 4.0f) * 2048.0f + 2048.0f); + + + can_send_data[0] = (pos_code >> 8) & 0xFF; // 位置高8位 + can_send_data[1] = pos_code & 0xFF; // 位置低8位 + + can_send_data[2] = (speed_code >> 4) & 0xFF; // 速度高8位(取12位中的高8) + can_send_data[3] = ((speed_code & 0x0F) << 4) | ((JZ_xiaomi->K_P >> 8) & 0x0F); // 速度低4位 + Kp高4位 + + can_send_data[4] = JZ_xiaomi->K_P & 0xFF; // Kp低8位 + + can_send_data[5] = (JZ_xiaomi->K_D >> 4) & 0xFF; // Kd高8位(取12位中的高8) + can_send_data[6] = ((JZ_xiaomi->K_D & 0x0F) << 4) | ((current_code >> 8) & 0x0F); // Kd低4位 + 电流高4位 + + can_send_data[7] = current_code & 0xFF; // 电流低8位 + + } + else + { + can_send_data[0] = 0xFF; + can_send_data[1] = 0xFF; + can_send_data[2] = 0xFF; + can_send_data[3] = 0xFF; + can_send_data[4] = 0xFF; + can_send_data[5] = 0xFF; + can_send_data[6] = 0xFF; + can_send_data[7] = 0xFC; + is_open=1; + } + + + HAL_CAN_AddTxMessage(&hcan2, &tx_message, can_send_data, &send_mail_box); +} + diff --git a/User/device/djiMotor.h b/User/device/djiMotor.h index 5b91458..ecd06d4 100644 --- a/User/device/djiMotor.h +++ b/User/device/djiMotor.h @@ -10,7 +10,7 @@ extern "C"{ #include "device.h" #include "can.h" #include - +#include "string.h" typedef enum{ GM6020 = 0, @@ -44,6 +44,7 @@ enum{ GM6020_2 = 0x208, CAN_VESC5065_M1_MSG1 =0x901, //vesc的数据回传使用了扩展id,[0:7]为驱动器id,[8:15]为帧类型 CAN_VESC5065_M2_MSG1 =0x902, + CAN_VESC5065_M3_MSG1 =0x903, }; //vesc指令 @@ -170,6 +171,38 @@ void CAN_VESC_HEAD(uint8_t controller_id); int8_t CAN_VESC_Control(int id,int speed ,CAN_HandleTypeDef*hcan); + +//小米 + +typedef struct +{ + int16_t position; + int16_t speed; + int16_t K_P; + int16_t K_D; + int16_t K_C; + + int16_t Pmax;//决定最大角度值,+-1 -> 最大+-360° + +}JZ_xiaomi_t; + +typedef struct { + + uint8_t can_id; + float position_deg; + float speed_rads; + float current_A; + +} MotorFeedback_t; + + + +void Parse_CAN_Response(uint8_t* rx_data, MotorFeedback_t* feedback) ; + + +void CAN_cmd(int id,JZ_xiaomi_t *JZ_xiaomi); + + #ifdef __cplusplus } #endif diff --git a/User/module/shoot.cpp b/User/module/shoot.cpp index fde279c..109dec8 100644 --- a/User/module/shoot.cpp +++ b/User/module/shoot.cpp @@ -40,6 +40,14 @@ Shoot::Shoot() motor_5065[1] = get_vesc_point(1);//获取电机数据指针 speed_5065=0; currentState= SHOOT_IDLE; + + JZ_xiaomi.position = 270; // + JZ_xiaomi.speed = 20; // + JZ_xiaomi.K_P = 100; // + JZ_xiaomi.K_D =20; // + JZ_xiaomi.K_C = 2 ; + JZ_xiaomi.Pmax =1; + } void Shoot::trigger_control() diff --git a/User/module/shoot.hpp b/User/module/shoot.hpp index 361c0ba..c88c3e7 100644 --- a/User/module/shoot.hpp +++ b/User/module/shoot.hpp @@ -44,6 +44,7 @@ public: volatile BallState_t ballStatus;//是否有球 volatile uint32_t flag_thread;//接收传回的线程通知 + JZ_xiaomi_t JZ_xiaomi; private: //扳机2006 diff --git a/User/task/shootTask.cpp b/User/task/shootTask.cpp index ea16fe1..180da8e 100644 --- a/User/task/shootTask.cpp +++ b/User/task/shootTask.cpp @@ -11,8 +11,13 @@ Shoot shoot; int shoot_flag = 0; +int anglexiaomi = 0; +int xxxxx; +int speed5 = 0; int a2; + + // sw[0]2 下306上1694 sw[5]3前306后1694 sw[4]4前1694后306 sw[1]xuan1 sw[3]xuan2 //F键 sw[0]👆 1800 中 1000 👇200 @@ -22,6 +27,8 @@ void FunctionShoot(void *argument) { (void)argument; /* 未使用argument,消除警告 */ + HAL_Delay(500); //一定时间延时 等待电机初始化完成 + const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CTRL_SHOOT; uint32_t tick = osKernelGetTickCount(); @@ -35,34 +42,40 @@ while(1) //我放的任务通知 运球成功放置过来后 shoot.flag_thread=osThreadFlagsGet(); - if(shoot.flag_thread & BALL_OK) - { - a2=2; - // shoot.shootThree(); - } + CAN_VESC_Control(2,speed5,&hcan2); + xxxxx++; - shoot_flag=HAL_GPIO_ReadPin(STOP_GPIO_Port, STOP_Pin); + CAN_cmd(1,&shoot.JZ_xiaomi); + HAL_Delay (100); - if(rc_ctrl.sw[1]>1000) - { - shoot.shootStateMachine(); - if(rc_ctrl.sw[0]==200) - { - shoot.shootBack(); - } - if(rc_ctrl.sw[0]==1000) - { - shoot.shootStop(); - } - } - if(rc_ctrl.sw[1]==200) - { - shoot.shootStop(); + // if(shoot.flag_thread & BALL_OK) + // { + // a2=2; + // // shoot.shootThree(); + // } - } + // shoot_flag=HAL_GPIO_ReadPin(STOP_GPIO_Port, STOP_Pin); - shoot.trigger_control(); - osDelay(2); + // if(rc_ctrl.sw[1]>1000) + // { + // shoot.shootStateMachine(); + // if(rc_ctrl.sw[0]==200) + // { + // shoot.shootBack(); + // } + // if(rc_ctrl.sw[0]==1000) + // { + // shoot.shootStop(); + // } + // } + // if(rc_ctrl.sw[1]==200) + // { + // shoot.shootStop(); + + // } + + // shoot.trigger_control(); + // osDelay(2); tick += delay_tick; /* 计算下一个唤醒时刻 */ osDelayUntil(tick);