Chen Huan
/
MotorPID
闭环步进电机
Embed:
(wiki syntax)
Show/hide line numbers
motor.h
00001 #ifndef __MOTOR_H 00002 #define __MOTOR_H 00003 #include "mbed.h" 00004 00005 #define MotorKp 1 00006 #define MotorKi 0 00007 #define MotorVKp 50 00008 #define MotorVKi 0 00009 00010 #define MotorKp_End 1 00011 #define MotorKi_End 0 00012 #define MotorVKp_End 50 00013 #define MotorVKi_End 0 00014 00015 #define MotorKp_Start 1 00016 #define MotorKi_Start 0 00017 #define MotorVKp_Start 50 00018 #define MotorVKi_Start 0 00019 00020 00021 //电机控制相关常量和变量 00022 #define APB2Freq 72000000 00023 /*#define Motor1Dir PAout(2) //电机方向引脚的宏定义 00024 #define Motor2Dir PAout(3) 00025 #define Motor3Dir PAout(8)*/ 00026 00027 //电机初始化过程状态机的状态宏定义 00028 #define MotorInitState_Out 0 //最开始的状态,默认舵臂在光电开关外面 00029 #define MotorInitState_In 1 //首先舵臂需要下降到光电开关里面,处于这个状态时,电机时往光电开关里面走,而不是已经在光电开关里面了,下面同 00030 #define MotorInitState_OutAgain 2 //然后需要到光电开关外面去 00031 #define MotorInitState_InAgain 3 //然后再到关电开关里面去,这时候要打开外部中断,舵臂的遮挡动作会触发外部中断,此时会记下初始位置,作为零位置 00032 #define MotorInitState_BackToZero 4 //初始化成功 00033 #define MotorInitState_Succeed 5 //初始化成功 00034 00035 #define ID_Motor1 1 //电机的ID号码 00036 #define ID_Motor2 2 00037 #define ID_Motor3 3 00038 #define MotorStatus_Running 1 //电机的运行状态 00039 #define MotorStatus_OK 2 00040 //防抖动算法的变量和常量 00041 #define ErrorTolerance 20 //误差容限 00042 #define INT_TimesMin 10 //连续INT_TimesMin次中断读数都在误差容限之内,则判定已经到达预定位置 00043 00044 //电机控制参数变换 需要变参数运行 00045 #define ApproachDistance 1000 //接近距离 当轴的实际位置与目标位置接近到一定程度之后,应当把参数调小一些,免得蜜汁抖动 00046 00047 //低通滤波器相关常量 00048 #define FlierCoefficient 0.31415926 //低通滤波器系数 截止频率10Hz 00049 00050 00051 #define PulseFreqLimit 10000 00052 00053 //电机控制结构体 00054 typedef struct _Motor_Type 00055 { 00056 //PI调节器部分 00057 //位置环 00058 int Encoder; //编码器读数 00059 int EncoderTemp; //编码器数据处理的中间值 00060 00061 int PIDControl; //PID控制量 00062 int PIDControl_pre; //PID控制量 00063 int Difference; //误差量 00064 int Integral; //积分量 00065 float Kp; //比例系数 00066 float Ki; //积分系数 00067 int Goal; //目标值 00068 //速度环 00069 int Velocity; //速度 00070 int V_PIDControl; //PID控制量 00071 int V_PIDControl_pre; //PID控制量 00072 int V_Difference; //误差量 00073 int V_Integral; //积分量 00074 float V_Kp; //比例系数 00075 float V_Ki; //积分系数 00076 int V_Goal; //目标值 00077 00078 //编码器过零点 00079 int LagFiliterValue; //滞后滤波值 用于编码器过零点检测 00080 int LagFiliterValuePre; //缓存值 00081 int ChangeRateLagFiliterValue; //变化率 00082 char ChangeRateLagFiliterValueDigital; //数字化之后的变化率 00083 char ChangeRateLagFiliterValueDigitalPre; //缓存值 00084 00085 char InZeroZone; //进入零点区域 1 从大侧进入 2 从小侧进入 0 没进入 00086 int ZeroZoneCounter; //零点区域的计数器 00087 00088 int BigStepEncoderValue; //大跨度编码器数据采样值 间隔若干个周期 00089 int BigStepEncoderValuePre; //缓存 00090 int BigStepCounter; //大跨度采样计数器 00091 int BigStepRevolveTimes; 00092 00093 char HandUp1; //检测到过零点动作1 00094 char HandUp2; //检测到过零点动作2 00095 00096 //int EncoderValueEstimate; //依据给的 00097 00098 //加入减速器后的处理 00099 int RevolveTimes; //旋转的圈数 00100 int ZeroPosition; //零点的编码器读数 00101 int PresentPosition; //换算后的减速器轴的位置 00102 char ZeroPosFound; //是否找到零位置的标志 00103 char ZeroPosFoundAll; //电机全部都找到了零位置 00104 00105 //初始化部分 状态机 00106 char Init_State; //记录电机正处于初始化的哪一个状态 00107 char Init_ErrorTimes; //电机初始化过程中出错的次数 00108 00109 //防抖动部分 00110 int INTCounter; //记录编码器读数进入误差容限后中断执行的次数 00111 int EncoderPre; //前一次编码器的读数值(滤波后) 00112 int Difference_pre; 00113 //增强电机性能 00114 int ChangeRatePIDControl; 00115 int ChangeRatePIDControlPre; 00116 int ChangeRateDifference; 00117 //身份识别 00118 char ID; //电机的ID号码 这个程序自动设置的 00119 //运行状态 00120 char Status; //分为 运行中 和 达到目标值 00121 char Dir; //电机的方向 00122 char GoalUpdated; //目标值是否已经更新 00123 DigitalOut *MotorDir; //控制电机方向的管脚 00124 DigitalOut *EncoderCs; //编码器片选脚 00125 SPI *SPI_Encoder; //编码器对象 00126 PwmOut *MotorPwm; //PWM对象 00127 }MotorType; 00128 00129 void StepMotorInit(MotorType *Motor, DigitalOut *MotorDirPin, DigitalOut *EncoderCsPin, SPI *SPI_EncoderObj); 00130 00131 void MotorPIDController(MotorType *Motor); 00132 00133 int Math_abs(int num); 00134 00135 00136 //读取编码器值 00137 unsigned int Encoder_ReadData(MotorType *Motor); 00138 00139 //编码器初始化 00140 void Encoder_Init(MotorType *Motor); 00141 00142 ////////////////////////////////////////////////////////////////////////////////// 00143 //功能:一阶低通滤波 00144 //参数:Value_now 本次样值 Value_pre 上次滤波输出值 00145 //返回值:本次滤波输出值 00146 ////////////////////////////////////////////////////////////////////////////////// 00147 int Fliter_1(int Value_now, int Value_pre); 00148 00149 ////////////////////////////////////////////////////////////////////////////////// 00150 //功能:设置步进电机脉冲信号的PWM频率 00151 // 00152 //返回值:无 00153 ////////////////////////////////////////////////////////////////////////////////// 00154 void StepMotorSetFreq(MotorType *Motor); 00155 00156 //控制接口 00157 void Communication(Serial *Uart, MotorType *Motor); 00158 00159 #endif
Generated on Fri Jul 15 2022 01:12:02 by 1.7.2