代码风格更新以及加入超电的对接
This commit is contained in:
1 parent
957c535bda
commit
9de485e4ef
117 files changed
+9046
-6457
No files matched your search
+142
-70
@@ -1,45 +1,39 @@
|
||||
/* =============================================================================
|
||||
* dji_motor.c - DJI 电机统一驱动(M3508/M2006/GM6020)
|
||||
* 反馈路径(中断上下文):
|
||||
* dji_motor_init() 注册 CAN 回调 --> can_rx_callback() 按 rx_id 分发
|
||||
* --> dji_motor_parse(): 角度过零判定(+/-4096)多圈累计 total_tick
|
||||
* (无 legacy 的 +-1 圈限制,底盘小陀螺时 Yaw 可连续旋转),
|
||||
* 记录 last_online_tick 供掉线检测
|
||||
* 控制路径(任务上下文):
|
||||
* dji_motor_send_current_low/high() 0x200 / 0x1FF 电流帧(+/-16384 = 20A/10A)
|
||||
* dji_motor_send_gm6020_current_low/high() 0x1FE / 0x2FE GM6020 电流帧(+/-16384 = 3A)
|
||||
* ========================================================================== */
|
||||
#include "dji_motor.h"
|
||||
|
||||
#define DJI_MOTOR_MAX 12
|
||||
#define DJI_MOTOR_MAX 12U//单板最多注册大疆电机实例数
|
||||
#define DJI_MOTOR_HALF_TURN 4096//半圈编码器阈值(用于±4096过零多圈累计)
|
||||
|
||||
/* legacy 中的多圈判断以 4000 为阈值;这里改用对称的 ±4096 半圈阈值 */
|
||||
#define DJI_MOTOR_HALF_TURN 4096
|
||||
|
||||
static dji_motor_t * dji_motor_list[DJI_MOTOR_MAX];
|
||||
static uint8_t dji_motor_count = 0;
|
||||
static dji_motor_t * dji_motor_list[DJI_MOTOR_MAX];//已注册电机实例指针表
|
||||
static uint8_t dji_motor_count = 0;//当前已注册电机数量
|
||||
|
||||
static void dji_motor_can1_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc);
|
||||
static void dji_motor_can2_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc);
|
||||
|
||||
/* 解析一帧反馈,多圈累计(无 ±1 圈限制,底盘小陀螺时 Yaw 电机可连续旋转) */
|
||||
/*
|
||||
*函数简介:解析单帧大疆电机CAN反馈数据
|
||||
*参数说明:电机实例指针
|
||||
*参数说明:8字节CAN接收数据
|
||||
*返回类型:无
|
||||
*备注:内部使用±4096过零判定实现多圈连续累计,并记录last_online_tick用于掉线检测
|
||||
*/
|
||||
static void dji_motor_parse(dji_motor_t * motor, const uint8_t data[8])
|
||||
{
|
||||
dji_motor_feedback_t * fdb = &motor->fdb;
|
||||
uint16_t now_angle = (uint16_t)(((uint16_t)data[0] << 8) | data[1]);
|
||||
|
||||
if (fdb->last_online_tick != 0)
|
||||
if (fdb->last_online_tick != 0U)
|
||||
{
|
||||
int16_t diff = (int16_t)now_angle - (int16_t)fdb->raw_angle;
|
||||
if (diff > DJI_MOTOR_HALF_TURN) // 反向过零(如 8100 -> 100)
|
||||
if (diff > DJI_MOTOR_HALF_TURN)//反向过零(如8100->100)
|
||||
fdb->total_tick += diff - 8192;
|
||||
else if (diff < -DJI_MOTOR_HALF_TURN) // 正向过零(如 100 -> 8100)
|
||||
else if (diff < -DJI_MOTOR_HALF_TURN)//正向过零(如100->8100)
|
||||
fdb->total_tick += diff + 8192;
|
||||
else
|
||||
fdb->total_tick += diff;
|
||||
}
|
||||
|
||||
fdb->raw_angle = now_angle;
|
||||
fdb->rotor_angle_deg = (float)fdb->total_tick * 0.0439453125f; // /8192*360
|
||||
fdb->rotor_angle_deg = (float)fdb->total_tick * 0.0439453125f;//换算为度(/8192*360)
|
||||
fdb->rotor_speed = (int16_t)(((uint16_t)data[2] << 8) | data[3]);
|
||||
fdb->raw_torque = (int16_t)(((uint16_t)data[4] << 8) | data[5]);
|
||||
fdb->temperature = data[6];
|
||||
@@ -47,74 +41,124 @@ static void dji_motor_parse(dji_motor_t * motor, const uint8_t data[8])
|
||||
switch (motor->type)
|
||||
{
|
||||
case DJI_MOTOR_3508:
|
||||
fdb->torque_current = fdb->raw_torque * 0.001220703125f; // /16384*20
|
||||
fdb->torque_current = fdb->raw_torque * 0.001220703125f;//±16384对应±20A
|
||||
break;
|
||||
case DJI_MOTOR_2006:
|
||||
fdb->torque_current = fdb->raw_torque * 0.001f; // /10000*10
|
||||
fdb->torque_current = fdb->raw_torque * 0.001f;//±10000对应±10A
|
||||
break;
|
||||
case DJI_MOTOR_6020:
|
||||
fdb->torque_current = fdb->raw_torque * -3.0f; // 实际转矩电流,±3A 量程
|
||||
fdb->torque_current = fdb->raw_torque * -3.0f;//GM6020转矩电流换算
|
||||
break;
|
||||
}
|
||||
fdb->last_online_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:按(CAN句柄,标准ID)查找电机实例并解析反馈
|
||||
*参数说明:触发回调的CAN句柄指针
|
||||
*参数说明:接收报文标准ID
|
||||
*参数说明:8字节CAN接收数据
|
||||
*返回类型:无
|
||||
*备注:在RX0中断上下文中调用
|
||||
*/
|
||||
static void dji_motor_dispatch(hcan_t * hcan, uint32_t std_id, const uint8_t rx_data[8])
|
||||
{
|
||||
for (uint8_t i = 0; i < dji_motor_count; i++)
|
||||
{
|
||||
if (dji_motor_list[i]->hcan == hcan && dji_motor_list[i]->rx_id == std_id)
|
||||
{
|
||||
dji_motor_parse(dji_motor_list[i], rx_data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:CAN1电机反馈接收回调
|
||||
*参数说明:接收报文标准ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:由bsp_can中断分发调用
|
||||
*/
|
||||
static void dji_motor_can1_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)dlc;
|
||||
dji_motor_dispatch(&hcan1, std_id, rx_data);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:CAN2电机反馈接收回调
|
||||
*参数说明:接收报文标准ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:由bsp_can中断分发调用
|
||||
*/
|
||||
static void dji_motor_can2_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)dlc;
|
||||
dji_motor_dispatch(&hcan2, std_id, rx_data);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化大疆电机实例并注册CAN接收回调
|
||||
*参数说明:电机实例指针
|
||||
*参数说明:所属CAN句柄指针
|
||||
*参数说明:反馈报文标准ID
|
||||
*参数说明:电机类型枚举
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后调用
|
||||
*/
|
||||
void dji_motor_init(dji_motor_t * motor, hcan_t * hcan, uint16_t rx_id, dji_motor_type_e type)
|
||||
{
|
||||
if (motor == NULL || hcan == NULL)
|
||||
return;
|
||||
|
||||
motor->type = type;
|
||||
motor->hcan = hcan;
|
||||
motor->rx_id = rx_id;
|
||||
motor->fdb.raw_angle = 0;
|
||||
motor->fdb.total_tick = 0;
|
||||
motor->fdb.last_online_tick = 0; // 0 表示尚未收到反馈
|
||||
motor->fdb.rotor_angle_deg = 0.0f;
|
||||
motor->fdb.rotor_speed = 0;
|
||||
motor->fdb.raw_torque = 0;
|
||||
motor->fdb.torque_current = 0.0f;
|
||||
motor->fdb.temperature = 0;
|
||||
motor->fdb.last_online_tick = 0;//0表示尚未收到反馈
|
||||
|
||||
if (dji_motor_count < DJI_MOTOR_MAX)
|
||||
{
|
||||
dji_motor_list[dji_motor_count++] = motor;
|
||||
if (hcan == &hcan1)
|
||||
can_rx_register(hcan, rx_id, dji_motor_can1_rx_callback);
|
||||
else
|
||||
can_rx_register(hcan, rx_id, dji_motor_can2_rx_callback);
|
||||
}
|
||||
if (dji_motor_count >= DJI_MOTOR_MAX)
|
||||
return;
|
||||
|
||||
dji_motor_list[dji_motor_count++] = motor;
|
||||
if (hcan == &hcan1)
|
||||
can_rx_register(hcan, rx_id, dji_motor_can1_rx_callback);
|
||||
else
|
||||
can_rx_register(hcan, rx_id, dji_motor_can2_rx_callback);
|
||||
}
|
||||
|
||||
static void dji_motor_can1_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)dlc;
|
||||
for (uint8_t i = 0; i < dji_motor_count; i++)
|
||||
{
|
||||
if (dji_motor_list[i]->hcan == &hcan1 && dji_motor_list[i]->rx_id == std_id)
|
||||
{
|
||||
dji_motor_parse(dji_motor_list[i], rx_data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void dji_motor_can2_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)dlc;
|
||||
for (uint8_t i = 0; i < dji_motor_count; i++)
|
||||
{
|
||||
if (dji_motor_list[i]->hcan == &hcan2 && dji_motor_list[i]->rx_id == std_id)
|
||||
{
|
||||
dji_motor_parse(dji_motor_list[i], rx_data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- 控制帧 ---- */
|
||||
|
||||
/*
|
||||
*函数简介:通用4路int16大端打包CAN发送函数
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:4个int16控制量数组
|
||||
*返回类型:无
|
||||
*备注:每个int16按高字节在前、低字节在后填入8字节数据域
|
||||
*/
|
||||
void dji_motor_send_int16x4(hcan_t * hcan, uint32_t std_id, const int16_t val[4])
|
||||
{
|
||||
CanCtrlData_s msg;
|
||||
CanCtrlData_s msg = {0};
|
||||
|
||||
if (hcan == NULL || val == NULL)
|
||||
return;
|
||||
|
||||
msg.hcan = hcan;
|
||||
msg.tx_header.StdId = std_id;
|
||||
msg.tx_header.IDE = CAN_ID_STD;
|
||||
msg.tx_header.RTR = CAN_RTR_DATA;
|
||||
msg.tx_header.DLC = 8;
|
||||
msg.tx_header.IDE = CAN_ID_STD;//标准帧
|
||||
msg.tx_header.RTR = CAN_RTR_DATA;//数据帧
|
||||
msg.tx_header.DLC = 8;//8字节
|
||||
msg.tx_header.TransmitGlobalTime = DISABLE;
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
msg.tx_data[2 * i] = (uint8_t)(val[i] >> 8);
|
||||
@@ -123,26 +167,54 @@ void dji_motor_send_int16x4(hcan_t * hcan, uint32_t std_id, const int16_t val[4]
|
||||
CAN_SendTxMessage(&msg);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发送M3508/M2006 ID1~4电流控制帧
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:ID1~4电流控制码
|
||||
*返回类型:无
|
||||
*备注:标准帧ID 0x200
|
||||
*/
|
||||
void dji_motor_send_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4)
|
||||
{
|
||||
const int16_t v[4] = {i1, i2, i3, i4};
|
||||
dji_motor_send_int16x4(hcan, 0x200, v);
|
||||
dji_motor_send_int16x4(hcan, 0x200U, v);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发送M3508/M2006 ID5~8电流控制帧
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:ID5~8电流控制码
|
||||
*返回类型:无
|
||||
*备注:标准帧ID 0x1FF
|
||||
*/
|
||||
void dji_motor_send_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8)
|
||||
{
|
||||
const int16_t v[4] = {i5, i6, i7, i8};
|
||||
dji_motor_send_int16x4(hcan, 0x1FF, v);
|
||||
dji_motor_send_int16x4(hcan, 0x1FFU, v);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发送GM6020 ID1~4电流控制帧
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:ID1~4电流控制码
|
||||
*返回类型:无
|
||||
*备注:标准帧ID 0x1FE
|
||||
*/
|
||||
void dji_motor_send_gm6020_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4)
|
||||
{
|
||||
const int16_t v[4] = {i1, i2, i3, i4};
|
||||
dji_motor_send_int16x4(hcan, 0x1FE, v);
|
||||
dji_motor_send_int16x4(hcan, 0x1FEU, v);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发送GM6020 ID5~8电流控制帧
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:ID5~8电流控制码
|
||||
*返回类型:无
|
||||
*备注:标准帧ID 0x2FE
|
||||
*/
|
||||
void dji_motor_send_gm6020_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8)
|
||||
{
|
||||
const int16_t v[4] = {i5, i6, i7, i8};
|
||||
dji_motor_send_int16x4(hcan, 0x2FE, v);
|
||||
dji_motor_send_int16x4(hcan, 0x2FEU, v);
|
||||
}
|
||||
@@ -3,50 +3,38 @@
|
||||
|
||||
#include "bsp_can.h"
|
||||
|
||||
/* DJI 电机(M3508/M2006/GM6020)统一驱动
|
||||
* - 反馈:can_start() 后通过 can_rx_register 按 rx_id 注册,接收中断内解析(多圈累计)
|
||||
* - 控制:组装 0x200/0x1FF/0x1FE/0x2FE 控制帧,经 CAN_SendTxMessage 发送 */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
DJI_MOTOR_3508, // 减速比 3591:187(≈19:1), 转矩常数 0.3N·m/A
|
||||
DJI_MOTOR_2006, // 减速比 36:1, 转矩常数 0.18N·m/A
|
||||
DJI_MOTOR_6020, // 直驱, 电流控制(ID1~4 为 0x1FE 帧, ±16384 对应 ±3A)
|
||||
} dji_motor_type_e;
|
||||
DJI_MOTOR_3508,//M3508电机,减速比3591:187(约19:1),转矩常数0.3N·m/A
|
||||
DJI_MOTOR_2006,//M2006电机,减速比36:1,转矩常数0.18N·m/A
|
||||
DJI_MOTOR_6020,//GM6020电机,直驱,电流控制(ID1~4为0x1FE帧,±16384对应±3A)
|
||||
} dji_motor_type_e;//大疆电机类型枚举
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t raw_angle; // 本次机械角度原始数据(0~8191 对应 0~360°)
|
||||
int64_t total_tick; // 累计角度位置原始数据(8192*圈数+raw,无 ±1 圈限制)
|
||||
float rotor_angle_deg; // 累计转子机械角(°)
|
||||
int16_t rotor_speed; // 转子转速(RPM)
|
||||
int16_t raw_torque; // 转矩电流原始数据
|
||||
float torque_current; // 转矩电流(A)
|
||||
uint8_t temperature; // 电机温度(℃)
|
||||
uint32_t last_online_tick; // 最近一次收到反馈的 tick(HAL_GetTick)
|
||||
} dji_motor_feedback_t;
|
||||
uint16_t raw_angle;//本次机械角度原始值(0~8191对应0~360°)
|
||||
int64_t total_tick;//累计多圈机械位置原始值(8192*圈数+raw,无±1圈限制)
|
||||
float rotor_angle_deg;//累计转子机械角度(°)
|
||||
int16_t rotor_speed;//转子转速(RPM)
|
||||
int16_t raw_torque;//转矩电流原始编码值
|
||||
float torque_current;//换算后转矩电流(A)
|
||||
uint8_t temperature;//电机温度(℃)
|
||||
uint32_t last_online_tick;//最近一次收到反馈的系统tick(HAL_GetTick)
|
||||
} dji_motor_feedback_t;//大疆电机反馈数据结构体
|
||||
|
||||
typedef struct
|
||||
{
|
||||
dji_motor_type_e type;
|
||||
hcan_t * hcan;
|
||||
uint16_t rx_id; // 反馈 ID:M3508/M2006 0x201~0x208,GM6020 0x205~0x20B
|
||||
dji_motor_feedback_t fdb;
|
||||
} dji_motor_t;
|
||||
dji_motor_type_e type;//电机类型
|
||||
hcan_t * hcan;//所属CAN总线句柄(&hcan1/&hcan2)
|
||||
uint16_t rx_id;//反馈报文标准ID(3508/2006:0x201~0x208,6020:0x205~0x20B)
|
||||
dji_motor_feedback_t fdb;//电机反馈数据
|
||||
} dji_motor_t;//大疆电机实例结构体
|
||||
|
||||
/* 初始化并注册 CAN 接收回调(can_start() 之后调用) */
|
||||
extern void dji_motor_init(dji_motor_t * motor, hcan_t * hcan, uint16_t rx_id, dji_motor_type_e type);
|
||||
|
||||
/* ---- 控制帧发送 ----
|
||||
* M3508/M2006 ID1~4:0x200 电流帧(±16384 对应 ±20A/±10A)
|
||||
* M3508/M2006 ID5~8:0x1FF 电流帧(±16384)
|
||||
* GM6020 电流控制:ID1~4 为 0x1FE 电流帧(±16384 对应 ±3A),ID5~8 为 0x2FE 电流帧(±16384) */
|
||||
extern void dji_motor_send_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4);
|
||||
extern void dji_motor_send_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8);
|
||||
extern void dji_motor_send_gm6020_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4);
|
||||
extern void dji_motor_send_gm6020_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8);
|
||||
|
||||
/* 通用 int16x4 发送(板间通讯等复用) */
|
||||
extern void dji_motor_send_int16x4(hcan_t * hcan, uint32_t std_id, const int16_t val[4]);
|
||||
void dji_motor_init(dji_motor_t * motor, hcan_t * hcan, uint16_t rx_id, dji_motor_type_e type);//初始化电机实例并注册CAN接收回调
|
||||
void dji_motor_send_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4);//发送3508/2006 ID1~4电流帧(0x200)
|
||||
void dji_motor_send_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8);//发送3508/2006 ID5~8电流帧(0x1FF)
|
||||
void dji_motor_send_gm6020_current_low(hcan_t * hcan, int16_t i1, int16_t i2, int16_t i3, int16_t i4);//发送GM6020 ID1~4电流帧(0x1FE)
|
||||
void dji_motor_send_gm6020_current_high(hcan_t * hcan, int16_t i5, int16_t i6, int16_t i7, int16_t i8);//发送GM6020 ID5~8电流帧(0x2FE)
|
||||
void dji_motor_send_int16x4(hcan_t * hcan, uint32_t std_id, const int16_t val[4]);//通用4路int16大端打包CAN发送函数
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user