关于网友f40372660的提问其实是 速度环与电流环之间衔接问题,很多大大们其实说的是电流环的本质,本人从***论坛的 asdmail 的一个程序,与大大们探讨下
//----------------------------------------------------------------------
//速度闭环PID计算程序
//----------------------------------------------------------------------
void Speed_Pid(void)
{
//DesiredSpeed = Motor_Speed/14;
if(Speed >= DesiredSpeed) //计算偏差
{
SpeedError = Speed - DesiredSpeed; //测量值大于给定值
Flags.Speed_error_dir = 1; //偏差为负数
}
else
{
SpeedError = DesiredSpeed - Speed; //给定值大于测量值
Flags.Speed_error_dir= 0; //偏差为正负数
}
Txbuff[4] = SpeedError >>8; //将速度偏差值存入发送缓存
Txbuff[5] = SpeedError & 0x00FF;
//Speed = 0x0000; //得到偏差后,将测量到得数据清空
if(Flags.Speed_error_dir ==0) //偏差为正数,则积分数据和偏差相加
{
SpeedIntegral = SpeedIntegral + SpeedError; //对偏差进行积分
}
else //偏差为负数,则积分数据和偏差相减
{
if(SpeedIntegral>=SpeedError) //积分大于偏差
{
SpeedIntegral = SpeedIntegral - SpeedError;
}
else //偏差大于积分
{
SpeedIntegral = SpeedError - SpeedIntegral;
}
}
if(SpeedIntegral >2000) //积分限幅
{
SpeedIntegral = 2000;
}
Speed_PID = (SpeedError + SpeedIntegral*0 )/100;
Txbuff[8] = Speed_PID >>8; //将电机速度PID数据存入发送缓存
Txbuff[9] = Speed_PID & 0x00FF;
这是速度环的代码
//----------------------------------------------------------------
//电流闭环PID计算程序
//----------------------------------------------------------------
void Current_Pid(void)
{
DesiredCurrent = Speed_PID; //将速度环输出转换为电流环输入
if(Current >= DesiredCurrent) //计算电流偏差
{
CurrentError = Current - DesiredCurrent; //测量值大于给定值
Flags.Current_error_dir = 1; //偏差为负
}
else
{
CurrentError = DesiredCurrent - Current; //给定值大于测量值
Flags.Current_error_dir= 0; //偏差为正
}
//Current = 0x0000; //得到偏差后,将测量到的数据清空
if(Flags.Speed_error_dir ==0) //根据偏差方向,决定积分数据和偏差是相加还是相减
{
CurrentIntegral = CurrentIntegral + CurrentError; //对偏差进行积分
}
else
{
if(CurrentIntegral>=CurrentError)
{
CurrentIntegral = CurrentIntegral - CurrentError;
}
else
{
CurrentIntegral = CurrentError - CurrentIntegral;
}
}
if(CurrentIntegral >2000) //积分限幅
{
CurrentIntegral = 2000;
}
Current_PID = (CurrentError + CurrentIntegral/20 )/20;
if(Flags.Speed_error_dir==0)
{
if(DutyCycle >= 65000)
{
DutyCycle = 65535;
}
else
{
DutyCycle = Current_PID + DutyCycle; //电机加速
if(DutyCycle >=65000)
{
DutyCycle = 65535;
}
}
}
else ////电机减速
{
if(DutyCycle >= Current_PID)
{
DutyCycle = DutyCycle - Current_PID;
}
else
{
DutyCycle = Current_PID - DutyCycle;
}
}
PDC1 = DutyCycle;
PDC2 = PDC1;
PDC3 = PDC1;
Txbuff[6] = DutyCycle >>8; //将电机占空比数据存入发送缓存
Txbuff[7] = DutyCycle & 0x00FF;
这是电流环的代码
其实问题很简单,就是在代码中红色的 DesiredCurrent = Speed_PID; //将速度环输出转换为电流环输入
在速度环中处理出来的输出 Speed_PID 电流环的给定 DesiredCurrent,中间不需要一些变换吗?
|