mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 03:27:45 +08:00
增加了遥控器按键功能并修改所有函数和类型定义为标准格式
This commit is contained in:
@@ -17,7 +17,7 @@ static float uint_to_float(int x_int, float x_min, float x_max, int bits)
|
||||
return ((float)x_int) * span / ((float)((1 << bits) - 1)) + offset;
|
||||
}
|
||||
|
||||
static void DecodeJoint(can_instance *motor_instance)
|
||||
static void DecodeJoint(CANInstance *motor_instance)
|
||||
{
|
||||
uint16_t tmp;
|
||||
for (size_t i = 0; i < HT_MOTOR_CNT; i++)
|
||||
@@ -36,11 +36,11 @@ static void DecodeJoint(can_instance *motor_instance)
|
||||
}
|
||||
}
|
||||
|
||||
joint_instance *HTMotorInit(can_instance_config_s config)
|
||||
joint_instance *HTMotorInit(CAN_Init_Config_s config)
|
||||
{
|
||||
static uint8_t idx;
|
||||
joint_motor_info[idx] = (joint_instance *)malloc(sizeof(joint_instance));
|
||||
joint_motor_info[idx]->motor_can_instace =CANRegister(&config);
|
||||
joint_motor_info[idx]->motor_can_instace = CANRegister(&config);
|
||||
return joint_motor_info[idx++];
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@ typedef struct // HT04
|
||||
float speed_rpm;
|
||||
float given_current;
|
||||
|
||||
PID_t pid;
|
||||
can_instance *motor_can_instace;
|
||||
PIDInstance pid;
|
||||
CANInstance *motor_can_instace;
|
||||
} joint_instance;
|
||||
|
||||
typedef enum
|
||||
@@ -33,7 +33,7 @@ typedef enum
|
||||
CMD_ZERO_POSITION = 0xfe
|
||||
} joint_mode;
|
||||
|
||||
joint_instance *HTMotorInit(can_instance_config_s config);
|
||||
joint_instance *HTMotorInit(CAN_Init_Config_s config);
|
||||
|
||||
void JointControl(joint_instance *_instance, float current);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
static driven_instance *driven_motor_info[LK_MOTOR_CNT];
|
||||
|
||||
static void DecodeDriven(can_instance *_instance)
|
||||
static void DecodeDriven(CANInstance *_instance)
|
||||
{
|
||||
for (size_t i = 0; i < LK_MOTOR_CNT; i++)
|
||||
{
|
||||
@@ -18,12 +18,12 @@ static void DecodeDriven(can_instance *_instance)
|
||||
}
|
||||
}
|
||||
|
||||
driven_instance *LKMotroInit(can_instance_config_s config)
|
||||
driven_instance *LKMotroInit(CAN_Init_Config_s config)
|
||||
{
|
||||
static uint8_t idx;
|
||||
driven_motor_info[idx] = (driven_instance *)malloc(sizeof(driven_instance));
|
||||
config.can_module_callback = DecodeDriven;
|
||||
driven_motor_info[idx]->motor_can_instance=CANRegister(&config);
|
||||
driven_motor_info[idx]->motor_can_instance = CANRegister(&config);
|
||||
return driven_motor_info[idx++];
|
||||
}
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ typedef struct // 9025
|
||||
int16_t given_current;
|
||||
uint8_t temperate;
|
||||
|
||||
PID_t *pid;
|
||||
can_instance *motor_can_instance;
|
||||
PIDInstance *pid;
|
||||
CANInstance *motor_can_instance;
|
||||
|
||||
} driven_instance;
|
||||
|
||||
@@ -28,7 +28,7 @@ typedef enum
|
||||
unused = 0,
|
||||
} driven_mode;
|
||||
|
||||
driven_instance *LKMotroInit(can_instance_config_s config);
|
||||
driven_instance *LKMotroInit(CAN_Init_Config_s config);
|
||||
|
||||
void DrivenControl(int16_t motor1_current, int16_t motor2_current);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
static uint8_t idx = 0; // register idx,是该文件的全局电机索引,在注册时使用
|
||||
|
||||
/* DJI电机的实例,此处仅保存指针,内存的分配将通过电机实例初始化时通过malloc()进行 */
|
||||
static dji_motor_instance *dji_motor_info[DJI_MOTOR_CNT] = {NULL};
|
||||
static DJIMotorInstance *dji_motor_info[DJI_MOTOR_CNT] = {NULL};
|
||||
|
||||
/**
|
||||
* @brief 由于DJI电机发送以四个一组的形式进行,故对其进行特殊处理,用6个(2can*3group)can_instance专门负责发送
|
||||
@@ -13,7 +13,7 @@ static dji_motor_instance *dji_motor_info[DJI_MOTOR_CNT] = {NULL};
|
||||
* can1: [0]:0x1FF,[1]:0x200,[2]:0x2FF
|
||||
* can2: [0]:0x1FF,[1]:0x200,[2]:0x2FF
|
||||
*/
|
||||
static can_instance sender_assignment[6] =
|
||||
static CANInstance sender_assignment[6] =
|
||||
{
|
||||
[0] = {.can_handle = &hcan1, .txconf.StdId = 0x1ff, .txconf.IDE = CAN_ID_STD, .txconf.RTR = CAN_RTR_DATA, .txconf.DLC = 0x08, .tx_buff = {0}},
|
||||
[1] = {.can_handle = &hcan1, .txconf.StdId = 0x200, .txconf.IDE = CAN_ID_STD, .txconf.RTR = CAN_RTR_DATA, .txconf.DLC = 0x08, .tx_buff = {0}},
|
||||
@@ -46,7 +46,7 @@ static void IDcrash_Handler(uint8_t conflict_motor_idx, uint8_t temp_motor_idx)
|
||||
*
|
||||
* @param config
|
||||
*/
|
||||
static void MotorSenderGrouping(can_instance_config_s *config)
|
||||
static void MotorSenderGrouping(CAN_Init_Config_s *config)
|
||||
{
|
||||
uint8_t motor_id = config->tx_id - 1; // 下标从零开始,先减一方便赋值
|
||||
uint8_t motor_send_num;
|
||||
@@ -116,11 +116,11 @@ static void MotorSenderGrouping(can_instance_config_s *config)
|
||||
*
|
||||
* @param _instance 收到数据的instance,通过遍历与所有电机进行对比以选择正确的实例
|
||||
*/
|
||||
static void DecodeDJIMotor(can_instance *_instance)
|
||||
static void DecodeDJIMotor(CANInstance *_instance)
|
||||
{
|
||||
// 由于需要多次变址访存,直接将buff和measure地址保存在寄存器里避免多次存取
|
||||
static uint8_t *rxbuff;
|
||||
static dji_motor_measure *measure;
|
||||
static DJI_Motor_Measure_s *measure;
|
||||
|
||||
for (size_t i = 0; i < DJI_MOTOR_CNT; i++)
|
||||
{
|
||||
@@ -151,10 +151,10 @@ static void DecodeDJIMotor(can_instance *_instance)
|
||||
}
|
||||
|
||||
// 电机初始化,返回一个电机实例
|
||||
dji_motor_instance *DJIMotorInit(Motor_Init_Config_s *config)
|
||||
DJIMotorInstance *DJIMotorInit(Motor_Init_Config_s *config)
|
||||
{
|
||||
dji_motor_info[idx] = (dji_motor_instance *)malloc(sizeof(dji_motor_instance));
|
||||
memset(dji_motor_info[idx], 0, sizeof(dji_motor_instance));
|
||||
dji_motor_info[idx] = (DJIMotorInstance *)malloc(sizeof(DJIMotorInstance));
|
||||
memset(dji_motor_info[idx], 0, sizeof(DJIMotorInstance));
|
||||
|
||||
// motor basic setting
|
||||
dji_motor_info[idx]->motor_type = config->motor_type;
|
||||
@@ -177,7 +177,7 @@ dji_motor_instance *DJIMotorInit(Motor_Init_Config_s *config)
|
||||
return dji_motor_info[idx++];
|
||||
}
|
||||
|
||||
void DJIMotorChangeFeed(dji_motor_instance *motor, Closeloop_Type_e loop, Feedback_Source_e type)
|
||||
void DJIMotorChangeFeed(DJIMotorInstance *motor, Closeloop_Type_e loop, Feedback_Source_e type)
|
||||
{
|
||||
if (loop == ANGLE_LOOP)
|
||||
{
|
||||
@@ -189,23 +189,23 @@ void DJIMotorChangeFeed(dji_motor_instance *motor, Closeloop_Type_e loop, Feedba
|
||||
}
|
||||
}
|
||||
|
||||
void DJIMotorStop(dji_motor_instance *motor)
|
||||
void DJIMotorStop(DJIMotorInstance *motor)
|
||||
{
|
||||
motor->stop_flag = MOTOR_STOP;
|
||||
}
|
||||
|
||||
void DJIMotorEnable(dji_motor_instance *motor)
|
||||
void DJIMotorEnable(DJIMotorInstance *motor)
|
||||
{
|
||||
motor->stop_flag = MOTOR_ENALBED;
|
||||
}
|
||||
|
||||
void DJIMotorOuterLoop(dji_motor_instance *motor, Closeloop_Type_e outer_loop)
|
||||
void DJIMotorOuterLoop(DJIMotorInstance *motor, Closeloop_Type_e outer_loop)
|
||||
{
|
||||
motor->motor_settings.outer_loop_type = outer_loop;
|
||||
}
|
||||
|
||||
// 设置参考值
|
||||
void DJIMotorSetRef(dji_motor_instance *motor, float ref)
|
||||
void DJIMotorSetRef(DJIMotorInstance *motor, float ref)
|
||||
{
|
||||
motor->motor_controller.pid_ref = ref;
|
||||
}
|
||||
@@ -217,10 +217,10 @@ void DJIMotorControl()
|
||||
// 同样可以提高可读性
|
||||
static uint8_t group, num;
|
||||
static int16_t set;
|
||||
static dji_motor_instance *motor;
|
||||
static DJIMotorInstance *motor;
|
||||
static Motor_Control_Setting_s *motor_setting;
|
||||
static Motor_Controller_s *motor_controller;
|
||||
static dji_motor_measure *motor_measure;
|
||||
static DJI_Motor_Measure_s *motor_measure;
|
||||
static float pid_measure;
|
||||
// 遍历所有电机实例,进行串级PID的计算并设置发送报文的值
|
||||
for (size_t i = 0; i < DJI_MOTOR_CNT; i++)
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
/* 滤波系数设置为1的时候即关闭滤波 */
|
||||
#define SPEED_SMOOTH_COEF 0.9f // better to be greater than 0.85
|
||||
#define CURRENT_SMOOTH_COEF 0.98f // this coef *must* be greater than 0.95
|
||||
#define ECD_ANGLE_COEF 0.043945f // 360/8192,将编码器值转化为角度制
|
||||
|
||||
#define ECD_ANGLE_COEF 0.043945f // 360/8192,将编码器值转化为角度制
|
||||
|
||||
/* DJI电机CAN反馈信息*/
|
||||
typedef struct
|
||||
@@ -38,7 +37,7 @@ typedef struct
|
||||
uint8_t temperate; // 温度 Celsius
|
||||
int16_t total_round; // 总圈数,注意方向
|
||||
int32_t total_angle; // 总角度,注意方向
|
||||
} dji_motor_measure;
|
||||
} DJI_Motor_Measure_s;
|
||||
|
||||
/**
|
||||
* @brief DJI intelligent motor typedef
|
||||
@@ -47,7 +46,7 @@ typedef struct
|
||||
typedef struct
|
||||
{
|
||||
/* motor measurement recv from CAN feedback */
|
||||
dji_motor_measure motor_measure;
|
||||
DJI_Motor_Measure_s motor_measure;
|
||||
|
||||
/* basic config of a motor*/
|
||||
Motor_Control_Setting_s motor_settings;
|
||||
@@ -56,7 +55,7 @@ typedef struct
|
||||
Motor_Controller_s motor_controller;
|
||||
|
||||
/* the CAN instance own by motor instance*/
|
||||
can_instance *motor_can_instance;
|
||||
CANInstance *motor_can_instance;
|
||||
|
||||
/* sender assigment*/
|
||||
uint8_t sender_group;
|
||||
@@ -65,7 +64,7 @@ typedef struct
|
||||
Motor_Type_e motor_type; // 电机类型
|
||||
Motor_Working_Type_e stop_flag; // 启停标志
|
||||
|
||||
} dji_motor_instance;
|
||||
} DJIMotorInstance;
|
||||
|
||||
/**
|
||||
* @brief 调用此函数注册一个DJI智能电机,需要传递较多的初始化参数,请在application初始化的时候调用此函数
|
||||
@@ -81,9 +80,9 @@ typedef struct
|
||||
*
|
||||
* @param config 电机初始化结构体,包含了电机控制设置,电机PID参数设置,电机类型以及电机挂载的CAN设置
|
||||
*
|
||||
* @return dji_motor_instance*
|
||||
* @return DJIMotorInstance*
|
||||
*/
|
||||
dji_motor_instance *DJIMotorInit(Motor_Init_Config_s *config);
|
||||
DJIMotorInstance *DJIMotorInit(Motor_Init_Config_s *config);
|
||||
|
||||
/**
|
||||
* @brief 被application层的应用调用,给电机设定参考值.
|
||||
@@ -92,7 +91,7 @@ dji_motor_instance *DJIMotorInit(Motor_Init_Config_s *config);
|
||||
* @param motor 要设置的电机
|
||||
* @param ref 设定参考值
|
||||
*/
|
||||
void DJIMotorSetRef(dji_motor_instance *motor, float ref);
|
||||
void DJIMotorSetRef(DJIMotorInstance *motor, float ref);
|
||||
|
||||
/**
|
||||
* @brief 切换反馈的目标来源,如将角速度和角度的来源换为IMU(小陀螺模式常用)
|
||||
@@ -101,7 +100,7 @@ void DJIMotorSetRef(dji_motor_instance *motor, float ref);
|
||||
* @param loop 要切换反馈数据来源的控制闭环
|
||||
* @param type 目标反馈模式
|
||||
*/
|
||||
void DJIMotorChangeFeed(dji_motor_instance *motor, Closeloop_Type_e loop, Feedback_Source_e type);
|
||||
void DJIMotorChangeFeed(DJIMotorInstance *motor, Closeloop_Type_e loop, Feedback_Source_e type);
|
||||
|
||||
/**
|
||||
* @brief 该函数被motor_task调用运行在rtos上,motor_stask内通过osDelay()确定控制频率
|
||||
@@ -113,14 +112,14 @@ void DJIMotorControl();
|
||||
* @brief 停止电机,注意不是将设定值设为零,而是直接给电机发送的电流值置零
|
||||
*
|
||||
*/
|
||||
void DJIMotorStop(dji_motor_instance *motor);
|
||||
void DJIMotorStop(DJIMotorInstance *motor);
|
||||
|
||||
/**
|
||||
* @brief 启动电机,此时电机会响应设定值
|
||||
* 初始化时不需要此函数,因为stop_flag的默认值为0
|
||||
*
|
||||
*/
|
||||
void DJIMotorEnable(dji_motor_instance *motor);
|
||||
void DJIMotorEnable(DJIMotorInstance *motor);
|
||||
|
||||
/**
|
||||
* @brief 修改电机闭环目标(外层闭环)
|
||||
@@ -128,6 +127,6 @@ void DJIMotorEnable(dji_motor_instance *motor);
|
||||
* @param motor 要修改的电机实例指针
|
||||
* @param outer_loop 外层闭环类型
|
||||
*/
|
||||
void DJIMotorOuterLoop(dji_motor_instance *motor, Closeloop_Type_e outer_loop);
|
||||
void DJIMotorOuterLoop(DJIMotorInstance *motor, Closeloop_Type_e outer_loop);
|
||||
|
||||
#endif // !DJI_MOTOR_H
|
||||
|
||||
@@ -75,9 +75,9 @@ typedef struct
|
||||
// float *speed_foward_ptr;
|
||||
// float *current_foward_ptr;
|
||||
|
||||
PID_t current_PID;
|
||||
PID_t speed_PID;
|
||||
PID_t angle_PID;
|
||||
PIDInstance current_PID;
|
||||
PIDInstance speed_PID;
|
||||
PIDInstance angle_PID;
|
||||
|
||||
float pid_ref; // 将会作为每个环的输入和输出顺次通过串级闭环
|
||||
} Motor_Controller_s;
|
||||
@@ -114,7 +114,7 @@ typedef struct
|
||||
Motor_Controller_Init_s controller_param_init_config;
|
||||
Motor_Control_Setting_s controller_setting_init_config;
|
||||
Motor_Type_e motor_type;
|
||||
can_instance_config_s can_init_config;
|
||||
CAN_Init_Config_s can_init_config;
|
||||
} Motor_Init_Config_s;
|
||||
|
||||
#endif // !MOTOR_DEF_H
|
||||
|
||||
Reference in New Issue
Block a user