摘要:本节介绍实现步进电机S曲线运动的代码
一、目标功能
实现步进电机转动总角度720°,其中加减速各90°
加速段:加速类型:S曲线
加速角度:角度为90°
起步速度:30RPM,
终止速度:500RPM
匀速段:匀速角度:角度为540°
匀速速度:500RPM
减速段:减速类型:S曲线
减速角度:角度为90°
起步速度:500RPM
终止速度:60RPM
二、生成器计算
加速参数计算
匀速参数计算,主要计算电机步数
减速参数计算
三、程序代码
//加速、匀速、减速步数
#define step0 100
#define step1 600
#define step2 100unsigned int n=0;//步数计数变量
unsigned int flag=0;//标志位,0:加速段 1:匀速段 2:减速段//加速段90°,30RPM-500RPM
const unsigned char code TimeH0[100]={0XEC,0XEC,0XEC,0XED,0XED,0XED,0XEE,0XEE,0XEE,0XEF,0XEF,0XF0,0XF0,0XF1,0XF1,0XF2,0XF2,0XF3,0XF3,0XF4,0XF4,0XF5,0XF5,0XF6,0XF6,0XF7,0XF7,0XF8,0XF8,0XF8,0XF9,0XF9,0XFA,0XFA,0XFA,0XFB,0XFB,0XFB,0XFB,0XFC,0XFC,0XFC,0XFC,0XFD,0XFD,0XFD,0XFD,0XFD,0XFD,0XFD,0XFD,0XFD,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE};
const unsigned char code TimeL0[100]={0X78,0XB4,0XF5,0X3B,0X86,0XD6,0X2A,0X84,0XE4,0X48,0XB1,0X1F,0X92,0X9,0X83,0X1,0X82,0X5,0X8A,0X10,0X96,0X1C,0XA0,0X24,0XA5,0X23,0X9E,0X15,0X88,0XF7,0X61,0XC5,0X25,0X80,0XD5,0X26,0X71,0XB7,0XF9,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90,0X88,0X81,0X78,0X6F,0X64,0X59,0X4D,0X3F,0X31,0X21,0XF,0XFD,0XE8,0XD2,0XBA,0XA0,0X84,0X66,0X46,0X24,0X0,0XD9,0XB0,0X85,0X58,0X28,0XF7,0XC4,0X8F,0X59,0X21,0XE8,0XAE,0X74,0X3A,0X0,0XC6,0X8D,0X54,0X1D,0XE7,0XB3,0X81,0X50,0X22,0XF6,0XCB,0XA4,0X7E,0X5A,0X39,0X1A,0XFD,0XE2,0XC9,0XB2,0X9D,0X89,0X77,0X66,0X57,0X48,0X3C};void main()
{ while(1){}
}//定时器0中断程序
void time0_int() interrupt 1
{//产生一个脉冲PUL=1;_nop_();_nop_();nop_();nop_();PUL=0;n++;//步数加1switch (flag)//判断运动状态{case 0://加速段{if(n==step0)//加速段完成{n=0;flag=1;TH0=TimeH2[0];//装载定时寄存器匀速段初值TL0=TimeL2[0];}else{TH0=TimeH0[n];//装载定时寄存器加速段初值TL0=TimeL0[n];}}break;case 1://匀速段{TH0=TimeH2[0];//装载定时寄存器匀速段初值TL0=TimeL2[0];if(n==step1)//匀速段完成{n=0;flag=2;TH0=TimeH2[n];//装载定时寄存器减速段初值TL0=TimeL2[n];}}break;case 2://减速段{if(n==step2)//减速段完成{n=0;flag=0;TR0=0; //电机停止}else{TH0=TimeH2[n];//装载定时寄存器减速段初值TL0=TimeL2[n];}}break;default:break;}
}
四.总结
通过上述计算,完成S曲线加减速运动。通过调整匀速运动的角度,加减速过程不变,可以实现总转动角度的变化,适用于总转角需要变化且需要较高速度的情况,并且满足总转角应大于加减速转角之和。
对于大多数情况,加减速的过程可以合并,即用S加速的逆过程去实现减速,可以实现代码的简化
五、附件
完整代码工程文件请见微信小程序: