Ancia 发表于 2022-12-18 14:02:54

速度环

平衡小车之家的速度环


/**
* @function:int Velocity(int Encoder_left,int Encoder_right,int Mechanical_velocity)
* @description:速度环PI控制
* @param {int} Encoder_left 数据 :左电机编码器的值
* @param {int} Encoder_right 数据 :右电机编码器的值
* @param {int} Mechanical_velocity 数据 :目标速度 ,因为只是让小车尽快平衡并静止,这里目标速度也设为0
* @return {int} Up_balance :速度环控制PWM
*/
int velocity(int encoder_left,int encoder_right)
{
        int Velocity,Encoder_Least; //速度环控制PWM,获取最新速度偏差
        static float Encoder,Encoder_Integral; //一阶低通滤波后的速度值,速度的积分;因为积分累加和滤波的需要,故设置为静态量,存储在全局区域类似于全局变量
    Encoder_Least =(encoder_left+encoder_right)-0;                  //===获取最新速度偏差==测量速度(左右编码器之和)-目标速度(此处为零)
                Encoder *= 0.8;                                                                //===一阶低通滤波器      
                Encoder += Encoder_Least*0.2;                                            //===一阶低通滤波器   
                Encoder_Integral +=Encoder;                                       //===积分出位移 积分时间:10ms
                if(Encoder_Integral>10000)        Encoder_Integral=10000;             //===积分限幅
                if(Encoder_Integral<-10000)        Encoder_Integral=-10000;            //===积分限幅       
                Velocity=Encoder*Velocity_Kp+Encoder_Integral*Velocity_Ki;      //===速度控制       
                if(Turn_Off(roll)==1)   Encoder_Integral=0;      //===电机关闭后清除积分
          return Velocity;
}

入口参数是单纯的读取左右编码器的值,还是单位时间内左右编码器的变化值?第一种是0到65535的那种,第二种是上一次读取的值减去这次读取的值

LIMIT 发表于 2023-1-11 09:49:28

入口参数是读取单位时间内左右编码器的变化值,是0-65535,Read_Encoder(void)函数会每次计数清零
页: [1]
查看完整版本: 速度环