小米移植可用

This commit is contained in:
ws 2025-05-23 20:59:23 +08:00
parent 8fc2f50677
commit 8689fdaea7
9 changed files with 218 additions and 38 deletions

View File

@ -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

View File

@ -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

View File

@ -1 +1 @@
2025/5/20 23:55:49
2025/5/22 20:12:51

View File

@ -103,7 +103,7 @@
<bEvRecOn>1</bEvRecOn>
<bSchkAxf>0</bSchkAxf>
<bTchkAxf>0</bTchkAxf>
<nTsel>3</nTsel>
<nTsel>6</nTsel>
<sDll></sDll>
<sDllPa></sDllPa>
<sDlgDll></sDlgDll>
@ -114,7 +114,7 @@
<tDlgDll></tDlgDll>
<tDlgPa></tDlgPa>
<tIfile></tIfile>
<pMon>BIN\CMSIS_AGDI.dll</pMon>
<pMon>STLink\ST-LINKIII-KEIL_SWO.dll</pMon>
</DebugOpt>
<TargetDriverDllRegistry>
<SetRegEntry>
@ -140,7 +140,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>DLGUARM</Key>
<Name></Name>
<Name>(105=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -255,6 +255,31 @@
<WinNumber>1</WinNumber>
<ItemText>angle1,0x0A</ItemText>
</Ww>
<Ww>
<count>20</count>
<WinNumber>1</WinNumber>
<ItemText>speed5,0x0A</ItemText>
</Ww>
<Ww>
<count>21</count>
<WinNumber>1</WinNumber>
<ItemText>xxxxx,0x0A</ItemText>
</Ww>
<Ww>
<count>22</count>
<WinNumber>1</WinNumber>
<ItemText>rx_header</ItemText>
</Ww>
<Ww>
<count>23</count>
<WinNumber>1</WinNumber>
<ItemText>rx_data</ItemText>
</Ww>
<Ww>
<count>24</count>
<WinNumber>1</WinNumber>
<ItemText>motor_5065</ItemText>
</Ww>
</WatchWindow1>
<WatchWindow2>
<Ww>
@ -985,7 +1010,7 @@
<Group>
<GroupName>User/device</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -1065,7 +1090,7 @@
<Group>
<GroupName>User/module</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

View File

@ -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);
}

View File

@ -10,7 +10,7 @@ extern "C"{
#include "device.h"
#include "can.h"
#include <math.h>
#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

View File

@ -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()

View File

@ -44,6 +44,7 @@ public:
volatile BallState_t ballStatus;//是否有球
volatile uint32_t flag_thread;//接收传回的线程通知
JZ_xiaomi_t JZ_xiaomi;
private:
//扳机2006

View File

@ -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);