297 lines
8.6 KiB
C
297 lines
8.6 KiB
C
#include "chassis.h"
|
|
#include "hlj_pc.h"
|
|
#include <math.h>
|
|
#include "pid.h"
|
|
#include "filter.h"
|
|
#include "attitude.h"
|
|
#include "can.h"
|
|
#include "robot_param.h"
|
|
#include "c_comm.h"
|
|
|
|
#define CHASSIS_ECD_HALF_TURN 4096//编码器半圈tick数
|
|
#define CHASSIS_ECD_FULL_TURN 8192//编码器一圈tick数
|
|
|
|
dji_motor_t motor_wheel_rf;//0x201右前轮
|
|
dji_motor_t motor_wheel_lf;//0x202右后轮(变量名沿用legacy)
|
|
dji_motor_t motor_wheel_lr;//0x203左前轮(变量名沿用legacy)
|
|
dji_motor_t motor_wheel_rr;//0x204左后轮(变量名沿用legacy)
|
|
dji_motor_t motor_yaw_follow;//Yaw GM6020(CAN2 0x205,相对角零漂移)
|
|
|
|
static PID_PositionTypeDef wheel_speed_pid[4];//四轮转速PID(按CAN槽位0~3)
|
|
static PID_PositionTypeDef follow_pid;//底盘跟随角PID
|
|
|
|
volatile float dbg_follow_err_deg = 0.0f;//跟随角误差(°)
|
|
volatile float dbg_follow_out = 0.0f;//跟随环输出
|
|
volatile float dbg_follow_sign = 0.0f;//综合后的跟随符号(0=未定)
|
|
|
|
static uint8_t follow_reset_request = 0;//重置跟随PID积分请求标志
|
|
|
|
/*
|
|
*函数简介:判定单颗轮电机是否掉线
|
|
*参数说明:电机实例指针
|
|
*参数说明:当前系统tick(HAL_GetTick)
|
|
*返回类型:1-掉线,0-在线
|
|
*备注:无
|
|
*/
|
|
static uint8_t chassis_one_offline(const dji_motor_t * motor, uint32_t now)
|
|
{
|
|
return (motor->fdb.last_online_tick == 0U ||
|
|
(now - motor->fdb.last_online_tick) > DEVICE_OFFLINE_TIME_MS) ? 1U : 0U;
|
|
}
|
|
|
|
/*
|
|
*函数简介:计算Yaw编码器相对机械零位的最短有符号差值
|
|
*参数说明:无
|
|
*返回类型:编码器差值(-4096~+4096)
|
|
*备注:无
|
|
*/
|
|
static int16_t chassis_yaw_ecd_diff(void)
|
|
{
|
|
int16_t diff = (int16_t)motor_yaw_follow.fdb.raw_angle - (int16_t)YAW_GM6020_ENCODER_ZERO;
|
|
if (diff > CHASSIS_ECD_HALF_TURN)
|
|
diff -= CHASSIS_ECD_FULL_TURN;
|
|
else if (diff < -CHASSIS_ECD_HALF_TURN)
|
|
diff += CHASSIS_ECD_FULL_TURN;
|
|
return diff;
|
|
}
|
|
|
|
/*
|
|
*函数简介:初始化底盘四轮/跟随电机实例与PID控制器
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:必须在can_start()之后调用一次
|
|
*/
|
|
void chassis_motor_init(void)
|
|
{
|
|
dji_motor_init(&motor_wheel_rf, &hcan1, 0x201, DJI_MOTOR_3508);
|
|
dji_motor_init(&motor_wheel_lf, &hcan1, 0x202, DJI_MOTOR_3508);
|
|
dji_motor_init(&motor_wheel_lr, &hcan1, 0x203, DJI_MOTOR_3508);
|
|
dji_motor_init(&motor_wheel_rr, &hcan1, 0x204, DJI_MOTOR_3508);
|
|
dji_motor_init(&motor_yaw_follow, &hcan2, 0x205, DJI_MOTOR_6020);
|
|
|
|
for (uint8_t i = 0; i < 4; i++)
|
|
{
|
|
PID_PositionStructureInit(&wheel_speed_pid[i], 0);
|
|
PID_PositionSetParameter(
|
|
&wheel_speed_pid[i], MECANUM_WHEEL_SPEED_KP, MECANUM_WHEEL_SPEED_KI,
|
|
MECANUM_WHEEL_SPEED_KD);
|
|
PID_PositionSetOUTRange(
|
|
&wheel_speed_pid[i], -MECANUM_WHEEL_SPEED_OUT_LIMIT, MECANUM_WHEEL_SPEED_OUT_LIMIT);
|
|
}
|
|
|
|
PID_PositionStructureInit(&follow_pid, 0);
|
|
PID_PositionSetParameter(&follow_pid, CHASSIS_FOLLOW_KP, CHASSIS_FOLLOW_KI, CHASSIS_FOLLOW_KD);
|
|
PID_PositionSetOUTRange(&follow_pid, -CHASSIS_FOLLOW_OUT_LIMIT, CHASSIS_FOLLOW_OUT_LIMIT);
|
|
}
|
|
|
|
/*
|
|
*函数简介:判定底盘四轮电机是否有任意一颗掉线
|
|
*参数说明:无
|
|
*返回类型:1-有电机掉线,0-全部在线
|
|
*备注:无
|
|
*/
|
|
uint8_t chassis_motor_offline(void)
|
|
{
|
|
uint32_t now = HAL_GetTick();
|
|
return (chassis_one_offline(&motor_wheel_rf, now) ||
|
|
chassis_one_offline(&motor_wheel_lf, now) ||
|
|
chassis_one_offline(&motor_wheel_lr, now) ||
|
|
chassis_one_offline(&motor_wheel_rr, now));
|
|
}
|
|
|
|
/*
|
|
*函数简介:底盘麦轮逆运动学解算与四轮电流闭环下发
|
|
*参数说明:前后平移速度(m/s,+vx朝1/3号轮一侧)
|
|
*参数说明:左右平移速度(m/s,+vy朝3/4号轮一侧)
|
|
*参数说明:自转角速度(rad/s)
|
|
*参数说明:功率限制缩放系数(0~1)
|
|
*返回类型:无
|
|
*备注:固定2ms调用一次,经CAN1 0x200发送四轮电流
|
|
*/
|
|
void chassis_control(float vx, float vy, float w, float power_scale)
|
|
{
|
|
const float rear = w * (MECANUM_RX + MECANUM_RY) / 100.0f;
|
|
const float factor = MECANUM_SPEED_FACTOR / MECANUM_WHEEL_RADIUS;
|
|
const float target_rpm[4] = {
|
|
(vx - vy - rear) * factor,//ID1右前
|
|
(vx + vy - rear) * factor,//ID2右后
|
|
(-vx - vy - rear) * factor,//ID3左前
|
|
(-vx + vy - rear) * factor,//ID4左后
|
|
};
|
|
const dji_motor_t * wheels[4] = {
|
|
&motor_wheel_rf, &motor_wheel_lf, &motor_wheel_lr, &motor_wheel_rr
|
|
};
|
|
float wheel_rpm[4];
|
|
int16_t out[4];
|
|
|
|
for (uint8_t i = 0; i < 4; i++)
|
|
{
|
|
wheel_rpm[i] = (float)wheels[i]->fdb.rotor_speed;
|
|
wheel_speed_pid[i].Need_Value = target_rpm[i];
|
|
PID_PositionCalc(&wheel_speed_pid[i], wheel_rpm[i]);
|
|
out[i] = (int16_t)(wheel_speed_pid[i].OUT * power_scale);
|
|
}
|
|
|
|
#if HLJ_PC_ENABLE
|
|
hlj_pc_limit_motors(wheel_rpm, out);
|
|
#else
|
|
(void)wheel_rpm;
|
|
#endif
|
|
dji_motor_send_current_low(&hcan1, out[0], out[1], out[2], out[3]);
|
|
}
|
|
|
|
/*
|
|
*函数简介:请求重置底盘跟随基准并清积分
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:无
|
|
*/
|
|
void chassis_follow_reset(void)
|
|
{
|
|
follow_reset_request = 1;
|
|
PID_PositionClean(&follow_pid);
|
|
}
|
|
|
|
/*
|
|
*函数简介:底盘自检跟随旋转符号
|
|
*参数说明:无
|
|
*返回类型:1-自检成功,0-电机离线/姿态未就绪/车体未转动
|
|
*备注:施加小w脉冲并用底盘INS yaw判断旋转方向,阻塞约200ms
|
|
*/
|
|
uint8_t chassis_detect_follow_sign(void)
|
|
{
|
|
float yaw_before;
|
|
float dyaw;
|
|
|
|
if (chassis_motor_offline() || !att_ready)
|
|
return 0;
|
|
|
|
chassis_output_zero();
|
|
HAL_Delay(50);//静默期
|
|
|
|
yaw_before = att_deg_yaw;
|
|
chassis_control(0.0f, 0.0f, CHASSIS_DETECT_W, 1.0f);
|
|
for (uint16_t i = 0; i < CHASSIS_DETECT_TIME_MS; i++)
|
|
HAL_Delay(1);
|
|
chassis_output_zero();
|
|
|
|
dyaw = theta_format(att_deg_yaw - yaw_before);
|
|
HAL_Delay(80);//回稳
|
|
|
|
if (dyaw > CHASSIS_DETECT_YAW_MIN)
|
|
{
|
|
dbg_follow_sign = 1.0f;//w+->底盘CCW
|
|
return 1;
|
|
}
|
|
if (dyaw < -CHASSIS_DETECT_YAW_MIN)
|
|
{
|
|
dbg_follow_sign = -1.0f;//w+->底盘CW
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
*函数简介:获取综合后的底盘跟随符号
|
|
*参数说明:无
|
|
*返回类型:+1.0或-1.0
|
|
*备注:自检未完成时回退到CHASSIS_FOLLOW_SIGN
|
|
*/
|
|
float chassis_follow_sign(void)
|
|
{
|
|
if (dbg_follow_sign == 0.0f || ctoc_encoder_ccw == 0)
|
|
return CHASSIS_FOLLOW_SIGN;
|
|
return dbg_follow_sign * (float)ctoc_encoder_ccw;
|
|
}
|
|
|
|
/*
|
|
*函数简介:计算FOLLOW模式下的底盘跟随角速度
|
|
*参数说明:无
|
|
*返回类型:跟随环输出角速度w
|
|
*备注:以Yaw GM6020编码器对齐机械零位为目标,固定2ms调用
|
|
*/
|
|
float chassis_follow_w(void)
|
|
{
|
|
int16_t tick_diff;
|
|
float err_deg;
|
|
|
|
if (motor_yaw_follow.fdb.last_online_tick == 0U)
|
|
return 0.0f;
|
|
|
|
if (follow_reset_request)
|
|
{
|
|
follow_reset_request = 0;
|
|
PID_PositionClean(&follow_pid);
|
|
}
|
|
|
|
tick_diff = chassis_yaw_ecd_diff();
|
|
err_deg = (float)tick_diff * (360.0f / 8192.0f);
|
|
if (ctoc_encoder_ccw < 0)
|
|
err_deg = -err_deg;
|
|
|
|
if (err_deg > -CHASSIS_FOLLOW_DEADBAND && err_deg < CHASSIS_FOLLOW_DEADBAND)
|
|
err_deg = 0.0f;
|
|
|
|
follow_pid.Need_Value = 0.0f;
|
|
PID_PositionCalc(&follow_pid, chassis_follow_sign() * -err_deg);
|
|
|
|
dbg_follow_err_deg = err_deg;
|
|
dbg_follow_out = follow_pid.OUT;
|
|
return follow_pid.OUT;
|
|
}
|
|
|
|
/*
|
|
*函数简介:底盘四轮CAN电流输出清零
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:无
|
|
*/
|
|
void chassis_output_zero(void)
|
|
{
|
|
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
|
|
}
|
|
|
|
/*
|
|
*函数简介:将云台坐标系平移速度投影到底盘自身坐标系
|
|
*参数说明:云台系前后速度(vx_g)
|
|
*参数说明:云台系左右速度(vy_g)
|
|
*参数说明:底盘当前旋转角速度(w,用于前馈补偿通信及响应滞后)
|
|
*参数说明:输出底盘系前后速度指针
|
|
*参数说明:输出底盘系左右速度指针
|
|
*返回类型:无
|
|
*备注:若0x205编码器掉线则直通输出
|
|
*/
|
|
void chassis_vector_to_body(float vx_g, float vy_g, float w, float * vx_c, float * vy_c)
|
|
{
|
|
int16_t tick_diff;
|
|
float alpha_rad;
|
|
int8_t enc_sign;
|
|
float alpha_comp;
|
|
float cos_a;
|
|
float sin_a;
|
|
|
|
if (vx_c == NULL || vy_c == NULL)
|
|
return;
|
|
|
|
if (motor_yaw_follow.fdb.last_online_tick == 0U)
|
|
{
|
|
*vx_c = vx_g;
|
|
*vy_c = vy_g;
|
|
return;
|
|
}
|
|
|
|
tick_diff = chassis_yaw_ecd_diff();
|
|
alpha_rad = (float)tick_diff * (2.0f * PI / 8192.0f);
|
|
enc_sign = (ctoc_encoder_ccw != 0) ? ctoc_encoder_ccw : 1;
|
|
if (enc_sign < 0)
|
|
alpha_rad = -alpha_rad;
|
|
|
|
alpha_comp = alpha_rad - (CHASSIS_SPIN_FEEDFORWARD_K * w);
|
|
cos_a = cosf(alpha_comp);
|
|
sin_a = sinf(alpha_comp);
|
|
|
|
*vx_c = vx_g * cos_a + vy_g * sin_a;
|
|
*vy_c = -vx_g * sin_a + vy_g * cos_a;
|
|
}
|