From 89edcd9e90f0735b48367b7d8f7ecdf41734171d Mon Sep 17 00:00:00 2001 From: zcj <2487150395@qq.com> Date: Fri, 20 Dec 2024 19:41:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0dmmotor=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/algorithm/user_lib.c | 13 +++++++++++++ modules/algorithm/user_lib.h | 13 ++++++++++++- modules/motor/DMmotor/dmmotor.c | 13 +------------ modules/motor/HTmotor/HT04.c | 15 ++------------- modules/motor/motor_def.h | 23 ++++++++++++----------- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/modules/algorithm/user_lib.c b/modules/algorithm/user_lib.c index 7338080..add96fa 100644 --- a/modules/algorithm/user_lib.c +++ b/modules/algorithm/user_lib.c @@ -212,7 +212,20 @@ void MatInit(mat *m, uint8_t row, uint8_t col) m->numRows = row; m->pData = (float *)zmalloc(row * col * sizeof(float)); } +/* 两个用于将uint值和float值进行映射的函数,在设定发送值和解析反馈值时使用 */ +uint16_t float_to_uint(float x, float x_min, float x_max, uint8_t bits) +{ + float span = x_max - x_min; + float offset = x_min; + return (uint16_t)((x - offset) * ((float)((1 << bits) - 1)) / span); +} +float uint_to_float(int x_int, float x_min, float x_max, int bits) +{ + float span = x_max - x_min; + float offset = x_min; + return ((float)x_int) * span / ((float)((1 << bits) - 1)) + offset; +} /** * @brief 一阶低通滤波初始化 * @author RM diff --git a/modules/algorithm/user_lib.h b/modules/algorithm/user_lib.h index f4c9c9d..627086a 100644 --- a/modules/algorithm/user_lib.h +++ b/modules/algorithm/user_lib.h @@ -53,7 +53,7 @@ void MatInit(mat *m, uint8_t row, uint8_t col); #ifndef PI #define PI 3.14159265354f #endif -typedef struct +typedef struct { float input; //输入数据 float out; //滤波输出的数据 @@ -61,6 +61,15 @@ typedef struct float frame_period; //滤波的时间间隔 单位 s } first_order_filter_type_t; +typedef struct +{ + float input; //输入数据 + float out; //输出数据 + float min_value; //限幅最小值 + float max_value; //限幅最大值 + float frame_period; //时间间隔 +} ramp_function_source_t; + #define VAL_LIMIT(val, min, max) \ do \ { \ @@ -127,6 +136,8 @@ void Cross3d(float *v1, float *v2, float *res); float Dot3d(float *v1, float *v2); float AverageFilter(float new_data, float *buf, uint8_t len); +float uint_to_float(int x_int, float x_min, float x_max, int bits); +uint16_t float_to_uint(float x, float x_min, float x_max, uint8_t bits); //一阶低通滤波初始化 void first_order_filter_init(first_order_filter_type_t *first_order_filter_type, float frame_period, const float num[1]); //一阶低通滤波计算 diff --git a/modules/motor/DMmotor/dmmotor.c b/modules/motor/DMmotor/dmmotor.c index 7cd2acc..a4d60dc 100644 --- a/modules/motor/DMmotor/dmmotor.c +++ b/modules/motor/DMmotor/dmmotor.c @@ -12,18 +12,7 @@ static uint8_t idx; static DMMotorInstance *dm_motor_instance[DM_MOTOR_CNT]; static osThreadId dm_task_handle[DM_MOTOR_CNT]; /* 两个用于将uint值和float值进行映射的函数,在设定发送值和解析反馈值时使用 */ -static uint16_t float_to_uint(float x, float x_min, float x_max, uint8_t bits) -{ - float span = x_max - x_min; - float offset = x_min; - return (uint16_t)((x - offset) * ((float)((1 << bits) - 1)) / span); -} -static float uint_to_float(int x_int, float x_min, float x_max, int bits) -{ - float span = x_max - x_min; - float offset = x_min; - return ((float)x_int) * span / ((float)((1 << bits) - 1)) + offset; -} + void DMMotorSetMode(DMMotor_Mode_e cmd, DMMotorInstance *motor) { diff --git a/modules/motor/HTmotor/HT04.c b/modules/motor/HTmotor/HT04.c index fdfad15..8a0267e 100644 --- a/modules/motor/HTmotor/HT04.c +++ b/modules/motor/HTmotor/HT04.c @@ -26,19 +26,8 @@ static void HTMotorSetMode(HTMotor_Mode_t cmd, HTMotorInstance *motor) CANTransmit(motor->motor_can_instace, 1); memcpy(motor->motor_can_instace->tx_buff, zero_buff, 6); } -/* 两个用于将uint值和float值进行映射的函数,在设定发送值和解析反馈值时使用 */ -static uint16_t float_to_uint(float x, float x_min, float x_max, uint8_t bits) -{ - float span = x_max - x_min; - float offset = x_min; - return (uint16_t)((x - offset) * ((float)((1 << bits) - 1)) / span); -} -static float uint_to_float(int x_int, float x_min, float x_max, int bits) -{ - float span = x_max - x_min; - float offset = x_min; - return ((float)x_int) * span / ((float)((1 << bits) - 1)) + offset; -} + + /** * @brief 解析电机反馈值 diff --git a/modules/motor/motor_def.h b/modules/motor/motor_def.h index bf5b782..13ec41a 100644 --- a/modules/motor/motor_def.h +++ b/modules/motor/motor_def.h @@ -73,6 +73,7 @@ typedef enum NO_POWER_LIMIT = 0, POWER_LIMIT_ON = 1, } Power_Limit_Type_e; + /* 电机控制设置,包括闭环类型,反转标志和反馈来源 */ typedef struct { @@ -83,11 +84,19 @@ typedef struct Feedback_Source_e angle_feedback_source; // 角度反馈类型 Feedback_Source_e speed_feedback_source; // 速度反馈类型 Feedfoward_Type_e feedforward_flag; // 前馈标志 - Power_Limit_Type_e power_limit_flag; //功率限制标志 } Motor_Control_Setting_s; +/* 电机控制方式枚举 */ +typedef enum +{ + CONTROL_TYPE_NONE = 0, + CURRENT_CONTROL, + VOLTAGE_CONTROL, +} Motor_Control_Type_e; + + /* 电机控制器,包括其他来源的反馈数据指针,3环控制器和电机的参考输入*/ // 后续增加前馈数据指针 typedef struct @@ -117,17 +126,10 @@ typedef enum M2006, LK9025, HT04, - ECA8210, + DM4310, + DM6006, } Motor_Type_e; -/* 电机控制方式枚举 */ -typedef enum -{ - CONTROL_TYPE_NONE = 0, - CURRENT_CONTROL, - VOLTAGE_CONTROL, -} Motor_Control_Type_e; - /** * @brief 电机控制器初始化结构体,包括三环PID的配置以及两个反馈数据来源指针 * 如果不需要某个控制环,可以不设置对应的pid config @@ -154,7 +156,6 @@ typedef struct Motor_Type_e motor_type; CAN_Init_Config_s can_init_config; Motor_Control_Type_e motor_control_type; - } Motor_Init_Config_s; #endif // !MOTOR_DEF_H