要完全调试电机的各个参数,例如最大速度,加速度等,就需要了解其计算公式和原理。

以下是代码注释:

/// This code uses speed calculations as described in 
/// "Generate stepper-motor speed profiles in real time" by David Austin 
/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf or
/// http://www.embedded.com/design/mcus-processors-and-socs/4006438/Generate-stepper-motor-speed-profiles-in-real-time or
/// http://web.archive.org/web/20140705143928/http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf

/// with the exception that AccelStepper uses steps per second rather than radians per second
/// (because we dont know the step angle of the motor)
/// An initial step interval is calculated for the first step, based on the desired acceleration
/// On subsequent steps, shorter step intervals are calculated based 
/// on the previous step until max speed is achieved.

红色部分内容可以从云盘下载:

链接:https://pan.baidu.com/s/16pFTsVpk6bxtFFilVpFYjw 
提取码:7wgc

其原理来自于

A new algorithm for stepper-motor acceleration allows speed profiles to be parameterized and calculated in real time. This algorithm can run on a low-end microcontroller using only simple fixed-point arithmetic operations and no data tables. It develops an accurate approximation for the timing of a linear ramp with constant acceleration and deceleration.

一种新的步进电机加速算法允许速度分布参数化和实时计算。该算法只需要简单的定点算术运算,不需要数据表,就可以在低端微控制器上运行。它是一个精确的近似恒定速度的线性加速与减速时序。

It's commonly thought that the timing of a linear speed ramp for a stepper motor is too complex to be calculated in real time. The exact formula for the step delay is in Equation 8. The solution has been to store the ramp data in precompiled arrays, but this method is inflexible and wastes memory. The alternative has been to use a more powerful and expensive processor than otherwise needed or a high-level stepper-control IC. This article develops an accurate approximation that has been implemented in C using 24.8 fixed-point arithmetic on a mid-range PIC microcontroller.

人们普遍认为步进电机的线性速度加速的时序太过复杂,无法实时计算。步进延迟的精确公式如式8所示。解决方案是将加速数据存储在预编译的数组中,但是这种方法不灵活,而且会浪费内存。另一种选择是使用更强大、更昂贵的处理器或高级的步进控制芯片。本文开发了一种精确的近似方法,在C语言中,在中程PIC微控制器上使用了24.8的定点算法。

Motor step signals can be generated by a 16-bit timer-comparator module as commonly integrated in microcontrollers. On the PIC, the CCP (capture/compare/pwm) performs this function. It allows steps to be timed to the resolution of one timer period. Each step advances the motor by a constant increment, typically 1.8 degrees on a hybrid stepper motor.

电机步进信号可以由一个16位的时间比较器模块产生,通常集成在微控制器中。在PIC上,CCP(捕获/比较/pwm)执行这个功能。它允许将步数计时到一个计时器周期的分辨率。每一次步进都使电机前进一个恒定的增量,在混合步进电机上通常是1.8度。

The timer frequency should be as high as possible while still allowing long delays as the motor is accelerated from stop. A timer frequency of 1MHz has been used. A maximum motor speed of 300rpm is then equivalent to a delay count of 1,000. It's necessary to have high timer resolution to give smooth acceleration at high speed.

定时器频率应该尽可能高,当电机从停止开始加速时应仍然允许长时间的延迟。定时器的频率为1MHz。最大电机转速为300rpm时,相当于延迟计数为1000。要在高速运转时,以提供平稳的加速度,需要高分辨率定时器。

Notation and basic formulas  符号和基本公式
Delay (sec) programmed by timer countc :

定时器计数c计算延时(秒)

Equation 1

f = timer frequency (Hz). 定时器频率
Motor speedω(rad/sec) at fixed timer countc :

定时器计数c一定时计算电机的速度ω(rad/sec) (弧度每秒)(电机的每步走过的弧度/每步时间间隔(延时)=弧度每秒)

Equation 2

α= motor step angle (radian).电机的步进角
1rad = 180/π= 57.3deg. 1rad/sec = 30/π= 9.55rpm.

Accelerationω' (rad/sec2) from adjacent timer countsc 1 andc 2:

相邻的定时器计数c c 1和c 2计算加速度ω”(rad / sec2):(加速度=(V1-V2)  /t)

Equation 3

Equation 3 assumes fixed-count speed (Equation 2) at the midpoint of each step interval (Equation 1), as on a linear ramp, Figure 1. Note thatω' resolution is inversely proportional to the cube of the speed.

方程3假设在每个步进间隔(方程1)的中点处具有固定计数的速度(方程2),就像在线性加速一样,如图1所示。注意,加速度ω”分辨率与速度围成的巨型大小成反比。(分的越细分辨率越高)对速度求导就是加速度,线性速度对应恒定加速度。


Figure 1: Ramp geometry: move of m =12 steps

速度曲线,12步的速度曲线

Linear speed ramp—exact 线性速度渐变

On a linear ramp, accelerationω' is constant, and speedω(t ) =ω'.t . Integration gives the motor shaft angle θ(t ):

在一个线性速度渐变过程总,加速ω”是常数,和速度ω(t) =ω'。t。集成给出了电机轴角θ(t):求积分计算出电机转过的角度。

Equation 4

n ≥ 0 step number (real). When the shaft is at θ =n .α, (integern ) it's time for then th step pulse:

n≥0步数(实时)。当轴θ= n .α(整数n) 刚好是第n次脉冲:

Equation 5

The exact timer count to program the delay between then th and (n +1)th pulses (n ≥ 0) is:

精确定时器计数计算第n个脉冲(n≥0)与(n +1)个脉冲之间的延时为:

Equation 6

The initial countc 0factorizes out to give Equations 7 and 8:

初始计数c,0分解得到方程7和8:

Equation 7(将n=0和n=1带入公式5和公式6而得)

Equation 8

Note thatc 0sets the acceleration, proportional to (1/c 0)2.

注意,c0设置加速度,与(1/ c0)的比值为2。

In real-time, Equation 8 would require calculation of a square-root for each step, with the added problem of loss of precision by subtraction.

在实时情况下,方程8需要为每一步计算一个平方根,加上减法导致精度损失的问题。

Approximating linear ramp  近似线性加速
Ratio of successive exact timer counts from Equation 8:

从公式8中连续精确计时器计数的比率:

Equation 9

Taylor series:泰勒系列

Equation 10

Equation 11 is the second-order approximation to Equation 9 using Equation 10:

利用方程10得方程11是方程9的二阶近似,:

Equation 11

Equation 11 can be rearranged for faster calculation:

公式11可以重新排列,计算速度更快:

Equation 12

Finally, we can disconnect the physical step number,i , from the step numbern on a ramp from zero, to give the general-purpose ramp algorithm shown in Equation 13. Heren determines the acceleration and increments withi for constant acceleration. To ramp up from stop,ni =i ,i =1,2, . . . :

最后,我们可以将物理步长i与一个渐进上的步长n从0断开,从而得到如式13所示的通用渐进算法。这里n决定了加速度与i增量,为恒定加速度。从停止状态开始加速,ni = i, i =1,2,…:

Equation 13

Negativen -values give deceleration. In particular, Equation 14, withni =i m , can be used to ramp any speed down to stop in the final steps of a move ofm steps:

负n值表示减速。特别是,方程14,ni = i - m,可以用来渐变任何速度在移动m步后速度下降到停止状态:

Equation 14

Table 1: Accuracy of the step-delay approximation

表1:步进延迟近似的值

Step n

Exact (9)

Approx (11)

Relative error

1

0.4142

0.6000

0.4485

2

0.7673

0.7778

0.0136

3

0.8430

0.8462

0.00370

4

0.8810

0.8824

0.00152

5

0.9041

0.9048

7.66E-4

6

0.9196

0.9200

4.41E-4

10

0.9511

0.9512

9.42E-5

100

0.9950

0.9950

9.38E-8

1,000

0.9995

0.9995

9.37E-11

Accuracy of approximation

近似精度值

Table 1 shows that the approximation is accurate even at low step numbern and relative error decreases withn3 . However,n =1 has a significant inaccuracy. The inaccuracy atn =1 can be handled in two ways:

从表1可以看出,即使在较低步进数n的情况下,该近似也是准确的,相对误差随着n3的增大而减小。然而,n =1有显著的不准确性。n =1处的误差可以用两种方法处理:

  • Treatn =1 as a special case. Usingc 1 0.4056c 0 compensates for the inaccuracies at the start of the ramp and allows Equation 7 to be used to calculatec 0.
  • 将n =1作为特殊情况处理。使用c1 0.4056 c0可以补偿加速开始时的误差,并允许使用公式7来计算c0。
  • Ignore the inaccuracy. In place of Equation 7 use Equation 15:
  • 忽略错误。用方程15代替方程7:

Equation 15

The first alternative gives an almost perfect linear ramp. The second alternative starts with a fast step. This is to the good, as it helps keep the motor moving between step pulses 0 and 1-and establishes the angle error needed to generate torque. It also allows a wider range of accelerations to be generated with a 16-bit timer and has the advantage of simplicity. It's therefore recommended to ignore the inaccuracy atn =1.

第一种方案提供了一个近乎完美的线性加速。第二种方案以一个快速步进开始。也算还可以,因为它有助于保持电机在步进脉冲0和1之间移动,并建立产生扭矩所需的角度误差。它还允许使用16位定时器生成更大范围的加速,并且具有简单性的优点。因此,建议忽略n =1时的不准确性。

Figure 2: Stepper-motor speed ramp 步进电机速度曲线


Figure 3: Start of ramp detail 加速开始时的细节


Figure 4: End of ramp detail 加速结束时的细节

Figures 2 through 4 compare the options for a target ramp from 0 to 120rpm in 1sec. For clarity, step changes in speed are shown, calculated from Equation 2. The true profile should be close to a straight line.

图2到图4比较了1秒内从0转到120转的目标加速的选项。为清楚起见,由公式2计算速度的步进变化显示出来。真实的轮廓应该接近一条直线。

2.c /(4.n +1) in Equation 12 could be approximated byc /2.n . Some effects would be:

式12c /(4.n +1)可近似为c /2.n .有如下影响:

  • The algorithm would still produce a linear ramp.这个算法仍然会产生一个线性渐变。
  • c0 would be closer to the “exact” value shown in Equation 7: 88.6% instead of 67.6% for the same ramp acceleration.
  • c0将更接近公式7:88.6%所示的“精确”值,而不是相同渐变加速度下的67.6%。
  • A single equation like Equation 13 could no longer be used for both acceleration and deceleration.
  • 像方程13这样的单一方程不能再同时用于加速和减速。

Changes of acceleration  加速度的变化
From Equations 4 and 5 we can obtain an expression for the step numbern as a function of speed and acceleration:

由式(4)和(5)可以得到步长n作为速度和加速度的函数的表达式:

Equation 16

Thus the number of steps needed to reach a given speed is inversely proportional to the acceleration:

因此,达到给定速度所需的步数与加速度成反比:

Equation 17

This makes it possible to change the acceleration at a point on the ramp by changing the step numbern in the ramp algorithm Equation 13. Moreover, using signedω' values results in signedn -values that behave correctly in the algorithm. Onlyω' = 0 needs special handling.

这使得通过改变加速算法方程13中的步数n来改变加速过程中某一点的加速度成为可能。此外,使用带有ω的值得出带有n值的表达式。只有ω= 0需要特殊处理。

Then -value given by Equation 17 is correct fortn . Howevercn represents an average for the intervaltn ..tn +1 . Equation 17 is usually adequate, but it's more accurate to add a half-step ton -values for use in the ramp algorithm:

方程17给出的n值对tn是正确的。而cn表示区间tn的平均值。tn + 1。方程17通常是足够的,但在加速算法中,向n个值添加半步更准确:

Equation 18

The numerical example shown in Table 2 changes acceleration from 10 to 5 and to -20rad/sec2from step 200. Complex speed profiles can be built up piecewise in this way.

表2所示的数值示例将加速度从第10步更改为第5步,并从第200步更改为-20rad/sec2步。复杂的速度轮廓可以用这种方法分段建立。

Table 2: Acceleration changes 加速度变化

Step i

ni

ci (13)

ω ' (3)

notes

198

198

2,813.067

199

199
398.5
-100.25

2,806.008

10

10.(199+.5) =
5.(398.5+.5) =
-20.(-100.25+.5)

200

399.5

2,803.498

5

201

400.5

2,799.001

5

200

-99.25

2,820.180

-20

201

-98.25

2,834.568

-20

Deceleration ramp  加速渐变
For a short move ofm steps, where the up-ramp atω'1meets the down-ramp atω'2before max speed is reached, the step numberm at which to start decelerating is, from Equation 17:

短程的m步,在加速ω1满足,加速在ω2之前达到最高速度,开始减速的步骤数m,从方程17可得:

Equation 19

ω'1= acceleration,ω'2= deceleration (positive). Roundn to integer and calculatecn ..cm-1 using Equation 14.

ω1 =加速度,ω2 =减速(正值)。将n四舍五入为整数,计算cn ..cm-1用方程14可得。

In other cases, Equations 17 or 18 can be used to calculate the number of stepsn 2 needed to stop at decelerationω'2, given that the present speed was reached at stepn 1 with accelerationω'1. Roundn 2 to integer and calculatecm-n2 .. cm-1 using Equation 14.

在其他情况下,方程17或18可以用来计算在减速度为ω2时,停下来的步数n 2,鉴于目前的速度与加速度在ω'1时达到n1步。计算cm-n2....cm-1用方程14表示。

Accelstepper 库中的参数计算公式相关推荐

  1. AccelStepper库

    文章目录 Accelstepper 库中的参数计算公式 ACCELSTEPPER库函数解析(一) moveTo runToNewPosition runSpeed runToPosition stop ...

  2. C++STL库中不可或缺的部分—string(模拟实现)

    前文 大家好,本篇文章主要是讲解一下 string一些常用接口的模拟实现. 众所周知,在日常生活中,字符串无处不在,如 ''just do it'',''中国'',''一坤年''等,想要在计算机上将这 ...

  3. numpy库中ndarray切片操作的参数意义

    ndarray切片操作的规则总结出来叫做"三帽号规则" 三帽号规则即:[开始索引:结尾索引:步长],并且切片区间是左闭右开的,即"开始索引:结尾索引"表示的区间 ...

  4. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 二 | 准备参数 | 远程调用 mmap 函数 )

    文章目录 一.准备 mmap 函数的参数 二.mmap 函数远程调用 一.准备 mmap 函数的参数 上一篇博客 [Android 逆向]Android 进程注入工具开发 ( 注入代码分析 | 远程调 ...

  5. 【Python机器学习】Sklearn库中Kmeans类、超参数K值确定、特征归一化的讲解(图文解释)

    一.局部最优解 采用随机产生初始簇中心 的方法,可能会出现运行 结果不一致的情况.这是 因为不同的初始簇中心使 得算法可能收敛到不同的 局部极小值. 不能收敛到全局最小值,是最优化计算中常常遇到的问题 ...

  6. transformers库中使用DataParallel保存模型参数时遇到的问题记录

    pytorch中使用DataParallel保存模型参数时遇到的问题记录 之前使用Transformers库中的Bert模型在自己的文本分类任务上使用Transformers库里的Trainer方式进 ...

  7. C++标准库中各种排序归纳

    一.简介 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.我们在编程过程中会经常接触到排序,比如游戏中的排行榜等.C++标准库中提供了各种不同的排序算法,这篇博 ...

  8. log包在Golang语言的标准库中是怎么使用的?

    Golang 语言的标准库中提供了一个简单的 log 日志包,它不仅提供了很多函数,还定义了一个包含很多方法的类型 Logger.但是它也有缺点,比如不支持区分日志级别,不支持日志文件切割等. 01. ...

  9. python ctypes库中动态链接库加载方式

    最近看了<Gray hat python>一书,这才知道为什么python是黑客必学的编程语言.通过python的ctypes模块,可以直接调用动态链接库中的导出函数,而且甚至可以直接在p ...

最新文章

  1. linux中操作java进程
  2. 小巧但强大,浏览器开发常用工具插件一枚
  3. 错误 LNK2019 无法解析的外部符号 “public: void __cdecl cv::Mat::copyTo(class cv::debug_build_guard::_OutputArray
  4. bootstrap栅格分5等分
  5. matlab画二维颜色深浅,MATLAB scatter 画二维/三维散点图时 用颜色表示数值大小 colorbar
  6. 基于Java+SpringBoot+vue+element等动物救助平台设计和实现
  7. 为什么要 conda 作用_烤箱预热有什么作用?为什么烘焙一定要预热烤箱?怎么正确预热?...
  8. 居民安装光伏系统常会碰壁 怎么样做才能少走弯路?
  9. 计算机不能代替人类英语,英语作文 谈谈计算机1我们已进入了计算机时代2计算机有许多优点3计算机不能代替人类...
  10. c++实现八大排序算法
  11. 资源工具分享(第1期):后端架构师技术图谱
  12. win10开始菜单应用图标丢失
  13. Cocos Creator大厅+子游戏模式
  14. 300万高清车牌识别一体机Http推送
  15. 常用的3D三维建模软件大全有哪些?
  16. PLC控制系统如何抵抗干扰
  17. 计算机制图和应用cad哪个好,cad制图笔记本电脑排行,cad制图用哪款笔记本电脑好...
  18. 分类模型confusion matrix混淆矩阵可视化
  19. AI语音机器人有哪些需要注意事项
  20. 在线弹钢琴HTML源码,在线弹钢琴 1.0

热门文章

  1. 10天精读掌握:计算机组成与设计(COAD:Patterson and Hennessy) (第1天 2018/10.24)
  2. 时空穿梭 探寻高端存储架构的前世今生
  3. 【Python实战】中国首份姓名报告出炉!中国爸妈给孩子取什么名?点开前你先猜一下……
  4. excel按季度分类汇总_excel数据透视表按月汇总
  5. SaaSpace:2022年十大最好的免费动画软件
  6. 6个 C 语言项目的自动化构建和测试工具。(文末有位小可爱)
  7. 超详细讲解SpringMVC三层架构
  8. mysql 取年月_MySQL 获取指定时间段的年月
  9. RTL8188EUS移植到AM3352
  10. 元宇宙与ChatGPT结合 一场颠覆式场景革命或将到来?