步进电机S曲线运动的代码实现

发布时间:2024-02-22  

一、目标功能

实现步进电机转动总角度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-500RPMconst unsigned char codeTimeH0[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 codeTimeL0[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,0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1,0X9C,0X96,0X90,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++;//步数加1  switch (flag)//判断运动状态 {    case0://加速段 { if(n==step0)//加速段完成 { n=0; flag=1; TH0=TimeH2[0];//装载定时寄存器匀速段初值 TL0=TimeL2[0]; } else { TH0=TimeH0[n];//装载定时寄存器加速段初值 TL0=TimeL0[n]; } }break;    case1://匀速段 { 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加速的逆过程去实现减速,可以实现代码的简化。


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>