Files
Gimbal/application/gimbal/gimbal.c
T
SkyLiCoder e1a01e6b53 feat: add 0xA9 feedforward frame and wire feedforward controller to motor output
- host 0xA9 frame → master_machine decode → robot_cmd publish
  via gimbal_feedforward topic → gimbal subscribe → yaw_motor.feed_input
- add DM_Feedforward_Controller_s with Kp and feed_input fields
- add feedforward term (Kp * feed_input) to motor control output
- add DM_Feedforward_Kp to motor init config
2026-06-07 11:54:23 +08:00

138 lines
4.8 KiB
C

#include "gimbal.h"
#include "robot_def.h"
#include "dmmotor.h"
#include "ins_task.h"
#include "message_center.h"
#include "general_def.h"
static INS_t *gimbal_IMU_data;
static Publisher_t *gimbal_pub;
static Subscriber_t *gimbal_sub;
static Subscriber_t *motor_enable_sub;
static Subscriber_t *motor_pid_sub;
static Subscriber_t *feedforward_sub;
static Gimbal_Upload_Data_s gimbal_feedback_data;
static Gimbal_Ctrl_Cmd_s gimbal_cmd_recv;
static DMMotorInstance *yaw_motor; // DM-G6220
static DMMotorInstance *pitch_motor; // DM-J4310-2EC
void GimbalInit()
{
gimbal_IMU_data = INS_Init();
gimbal_pub = PubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s));
gimbal_sub = SubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s));
motor_enable_sub = SubRegister("motor_enable", sizeof(Motor_Enable_s));
motor_pid_sub = SubRegister("motor_pid", sizeof(Motor_PID_Tune_s));
feedforward_sub = SubRegister("gimbal_feedforward", sizeof(Gimbal_Feedforward_s));
/* ---- Yaw: DM-G6220, CAN1, TX=0x01, RX=0xF1, Velocity模式 ---- */
DM_Motor_Init_Config_s yaw_config = {
.motor_config = {
.can_init_config = {
.can_handle = &hcan1,
.tx_id = 0x01,
.rx_id = 0xF1,
},
.controller_param_init_config = {
.angle_PID = {
.Kp = 1, .Ki = 0, .Kd = 0,
.IntegralLimit = 45,
.CoefA = 15, .CoefB = 5,
.Improve = PID_Trapezoid_Intergral | PID_Integral_Limit |
PID_Derivative_On_Measurement | PID_ChangingIntegrationRate,
.MaxOut = 2000,
},
.other_angle_feedback_ptr = &gimbal_IMU_data->YawTotalAngle,
.other_speed_feedback_ptr = &gimbal_IMU_data->Gyro[2],
},
.output_reverse = OUTPUT_DIRECTION_NORMAL,
.motor_type = DM_MOTOR,
},
.ctrl_mode = DM_MODE_VELOCITY,
.DM_Feedforward_Kp = 0.9f,
.mit_kp = 0, .mit_kd = 0,
};
yaw_motor = DMMotorInit(&yaw_config);
/* ---- Pitch: DM-J4310-2EC, CAN1, TX=0x02, RX=0xF2, Velocity模式 ---- */
DM_Motor_Init_Config_s pitch_config = {
.motor_config = {
.can_init_config = {
.can_handle = &hcan1,
.tx_id = 0x02,
.rx_id = 0xF2,
},
.controller_param_init_config = {
.angle_PID = {
.Kp = 1, .Ki = 0, .Kd = 0,
.IntegralLimit = 45,
.CoefA = 15, .CoefB = 5,
.Improve = PID_Trapezoid_Intergral | PID_Integral_Limit |
PID_Derivative_On_Measurement | PID_ChangingIntegrationRate,
.MaxOut = 2000,
},
.other_angle_feedback_ptr = &gimbal_IMU_data->Pitch,
.other_speed_feedback_ptr = &gimbal_IMU_data->Gyro[0],
},
.output_reverse = OUTPUT_DIRECTION_NORMAL,
.motor_type = DM_MOTOR,
},
.ctrl_mode = DM_MODE_VELOCITY,
.mit_kp = 0, .mit_kd = 0,
};
pitch_motor = DMMotorInit(&pitch_config);
gimbal_feedback_data.gimbal_imu_data = gimbal_IMU_data;
}
void GimbalTask()
{
/* 角度环参考值设定 */
if(SubGetMessage(gimbal_sub, &gimbal_cmd_recv))
{
DMMotorSetRef(yaw_motor, gimbal_cmd_recv.yaw);
DMMotorSetRef(pitch_motor, gimbal_cmd_recv.pitch);
}
/* 前馈输入 */
static Gimbal_Feedforward_s feedforward_msg;
if (SubGetMessage(feedforward_sub, &feedforward_msg))
{
yaw_motor->feedforward_controller.feed_input = feedforward_msg.feedforward;
}
/* 电机使能/失能控制 */
static Motor_Enable_s enable_msg;
if (SubGetMessage(motor_enable_sub, &enable_msg))
{
if (enable_msg.enable)
{
DMMotorSetMode(DM_CMD_ENABLE_MODE, yaw_motor);
DMMotorSetMode(DM_CMD_ENABLE_MODE, pitch_motor);
}
else
{
DMMotorSetMode(DM_CMD_DISABLE_MODE, yaw_motor);
DMMotorSetMode(DM_CMD_DISABLE_MODE, pitch_motor);
}
}
/* 电机角度环 PID 调参 */
static Motor_PID_Tune_s pid_tune;
if (SubGetMessage(motor_pid_sub, &pid_tune))
{
yaw_motor->motor_controller.angle_PID.Kp = pid_tune.yaw_Kp;
yaw_motor->motor_controller.angle_PID.Ki = pid_tune.yaw_Ki;
yaw_motor->motor_controller.angle_PID.Kd = pid_tune.yaw_Kd;
pitch_motor->motor_controller.angle_PID.Kp = pid_tune.pitch_Kp;
pitch_motor->motor_controller.angle_PID.Ki = pid_tune.pitch_Ki;
pitch_motor->motor_controller.angle_PID.Kd = pid_tune.pitch_Kd;
}
PubPushMessage(gimbal_pub, (void *)&gimbal_feedback_data);
}