Stepper Motor Algorithm PDF
Stepper Motor Algorithm PDF
Mihaylo Y. STOYCHITCH
© copyright FACULTY of ENGINEERING ‐ HUNEDOARA, ROMANIA 51
ANNALS OF FACULTY ENGINEERING HUNEDOARA – International Journal Of Engineering
interval δ ti between two adjacent pulses is not linear. So, we need to calculate time δ ti , which
ensures linear change in speed. It is shown for the acceleration phases in Fig. 2.
Depending on the method used to calculate this low speed, v = const .
time, generally, there are two groups of algorithms– t
two methods, named as: “time per step” and “steps
per time”, that are described in [3,4,5] and [6,7], high speed, v = const .
respectively. The first group of algorithms is mainly t
realized by using a microcontroller, while the second accelerating, v const .
group is suitable for realization by using FPGA (field t
programmable gate array) circuits. Both of the above t0 t1 t2 t3
groups of algorithms generate pulses in real time. δ t0 = c1 / f c2 / f c3 / f
However, there are alternate techniques where the
pulses are generated based on the previously entered Fig. 2. Constant speed and accelerating step
table of data, which include all parameters of motion. sequences
Using these techniques we can not calculate the time when pulses are generated in real time, because
any change of some parameters of the motion imply a change above table of data. Today, these
techniques lose their importance, because by increasing microcontroller’s speed it becomes possible
to perform all the necessary computation within short time, which is less than δ ti .
In the sequel of the paper we give a new real time algorithm that belongs to the first group of
the above mentioned algorithms. The practical implementation of this algorithm that is based on the
Arduino platform of the Atmel microcontroller ATMega 328 is also given.
ALGORITHM
As we emphasized earlier, we observe motion with the ramp speed profile, i.e.
a = const., d = const., a, d > 0 , and let a ≠ d . In this case, in order to determine control algorithm it
means to solve two problems: to calculate the instants of time when pulses are generated and
calculate the characteristic points of the motion in which the algorithm changes its behavior
(trajectory planning), see Fig. 1. We will design a controller that solves both problems.
Calculate time when the pulse is generated
The first pulse (step) controller generates at the start of motion, or at the start of the state of
acceleration, at the time t0 , see Fig. 3. After the first v
pulse is generated, the controller needs to calculate α f / c vi
time period δ t0 until the next pulse, wait until this α f / ci −1 vi −1
i
a α d
period has elapsed, and then generate the next pulse, α f / c v1 M α L
1 δt
at time t1 . This will go on until the desired position is δ ti −1 i
achieved, or in other words, the desired number of α f / co o α
v L
pulses has been generated. At the start, the speed is
to ti −1 ti ti +1 time t
vo , and it retains its value until the moment t1 when it c t1 c1
δ to = o
becomes v1 , at the moment t2 becomes v2 , and so on. f f L L
time t
As after each pulse the motor makes one step for the to t1 t t
i −1 i ti +1
angle α , then the following is valid
Fig. 3. Calculate the time when pulse is
α
α = vi ⋅ δ ti ⇒ δ ti = , (1) generated
vi
where the vi is speed at an arbitrary instant of time ti and δ ti the time delay between two successive
instants of time ti −1 and ti . In the case when the controller is realized by using a microcontroller, the
required time delay δ ti is implemented using counter ci that counts impulses of known frequency f ,
so δ ti = ci / f is valid . Now (1) becomes
ci αf
α = vi ⇒ vi = . (2)
f ci
Based on the above considerations, the speed is changed only at the discrete time ti . But, since
inertia always exists, thus we can assume that the speed vi between two arbitrary successive instants
of time ti −1 and ti , i ≥ 1 , changes linearly (see dashed light lines in Fig. 3). Thus, the speed vi at the
arbitrary instant of time ti , and in the phase of acceleration, becomes
vi = vi −1 + a ⋅ δ ti −1 , i ≥ 1 . (3)
Using (2) and (3), the value of the counter ci becomes
αf αf , (4)
ci = =
vi vi −1 + a ⋅ δti −1
52 Tome XI (Year 2013). Fascicule 3. ISSN 1584 – 2673
ANNALS OF FACULTY ENGINEERING HUNEDOARA – International Journal Of Engineering
Fig. 4. Results of simulation for different types of Fig. 5. Results of simulation –the part of phase
algorithms acceleration
We have also compared different types of algorithms, as: this algorithm (speed v - dashed lines)
and algorithm that is widely used, and which is proposed in [4] (speed v a - light solid line). It is easy
to see that they are nearly the same except in the initial steps (one or two steps). In both cases, the
step speed is greater than the linear speed (see Fig. 5). In order to decrease the difference from step
speed and linear speed, we need to introduce some corrections. If in the acceleration phase, after the
time delay ci is calculated by (6), we introduce correction in the form
⎛ 0.08 ⎞
ci = ci ⎜ 1 + ⎟, 1 ≤ i ≤ 5 , (9)
⎝ i ⎠
we obtain the new step speed v k - dark solid line in Fig. 5. We can see that the correction step speed
v k is much better than the previous uncorrected step speed v or v a (i.e. difference between the step
speed v k and the linear speed vl at the time t i is significantly smaller). In the same way, correction
Tome XI (Year 2013). Fascicule 3. ISSN 1584 – 2673 53
ANNALS OF FACULTY ENGINEERING HUNEDOARA – International Journal Of Engineering
in the last five steps of the deceleration phase is done, so that the time delay in the last steps of this
phase is given as
⎛ 0.08 ⎞
ci = ci ⎜ 1 + ⎟, 1 ≤ n − i ≤ 5 , (10)
⎝ n−i ⎠
where the total number of steps during the desired motion is denoted by n , see Fig. 7.
vd
vr
na
vd
speed
a d
n
na
nad step
nd ndn
Fig. 6.The speed error Fig. 7. Trajectory planning
The speed error (it is defined as the difference between analog and step speed) for the cases:
this algorithm (uncorrected and corrected) and the algorithm in [4] is shown in Fig.6. (error in the
first step is omitted).
TRAJECTORY PLANNING
We need to determine characteristics points of the ramp speed profile where the algorithm
changes. Continuous motion and in the case when acceleration and deceleration are the same (for
various ways of their change) is described in [8] and [9]. Herein we consider discrete motion for the
linear ramp speed profile, see Fig. 7.
For each stepper motor, next parameters must be known: maximum speed, maximum
acceleration, maximum start speed and number of steps per round1 (spr). In addition, at the beginning
of each motion the following parameters of motion are also known: desired position sMax [rad ] or
the total number of steps n , desired speed v d , acceleration a and deceleration d . The number of
the steps n (see labels in Fig.7.) is calculated as n = sMax / α , where α = 2π / spr [rad] is an angle for
one step. In this paper we assume that all phases of motion are finished during n steps exactly. In
order to calculate remaining parameters of the speed profile, we use two different approaches which
are dependent on the following: (i) desired speed is reached before the start of deceleration
(trapezoidal speed profile, Fig. 7, solid line, v d < v r ) and (ii) deceleration starts before desired
speed has been reached (triangular speed profile, Fig. 7, dashed line, v d ≥ v r ). As to which of these
speed profiles should be used, it is dependent on values na and nad , where na is the number of steps
that are needed to reach desired speed v d during acceleration and nad is the first step when the
deceleration phase starts. From the reached speed vr and during next (n − nad ) steps, the speed
decreases from vr to the zero value. Now, the cases (i) and (ii) expressed with number of the steps na
and nad become: nad > na and na ≥ nad , respectively. In both cases the speed at the end of
acceleration must be the same as the speed at the start of deceleration (due to the continuity of
motion), which implies
d
anad = d (n − nad ) ⇒ nad = n. (11)
a+d
On the other hand, to achieve the desired speed vd = at , t ∈ [0, Ta ] in the acceleration phase,
movement s a = naα is given as
2
naα =
1 2
at =
1
(at )2 ⇒ na = vd . (12)
2 2a 2αa
The number of steps nd when deceleration starts in case (i) is calculated from ana = d (n − nd )
which implies nd = n − na ⋅ a / d , and in the case (ii) it is the same na , i.e. nd = na = nad (see more
details in Arduino program).
1
these parameters will be specified by a motor manufacturer
54 Tome XI (Year 2013). Fascicule 3. ISSN 1584 – 2673
ANNALS OF FACULTY ENGINEERING HUNEDOARA – International Journal Of Engineering
Tome XI (Year 2013). Fascicule 3. ISSN 1584 – 2673 55
ANNALS OF FACULTY ENGINEERING HUNEDOARA – International Journal Of Engineering
}
//solve the time delay
double solveC(){ if (na == 0) return (Cm);
if ( (np<=na)||(np>=nd) ){//acceleration and deceleration phase
double q = 1+( (np<=na)?Ra:Rd )*Ci*Ci; Ci = Ci/q;kasni=66;
if ( ( (np>= 1 && (np<= 5)) || ((N-np <= 5) && ( N-np >= 1)))
{ Ci = Ci*(1+0.08/np) ; kasni=120;}//correction
if (Ci < Cm) Ci = Cm; //using variable ‘kasni’ we take into
} else Ci = Cm; return Ci; //account all delays occurred during
} /the call and execute of program
//this procedure calculate the parameters of the motion'
void init(float degU){
Ra=acce/(alfa*f*f); Rd=-dece/(alfa*f*f); Cm=alfa*f/dspeed;
N = (long) degU*K/360.0 - imaK; stoj = false;
if (N == 0) {stoj = true; return; }
if (N > 0) smjer = true;
if (N < 0) {smjer = false; N = -N;}
if (N == 1) oneStep(smjer);
// trajectory planning
if (acce != 0){// acceleration exists
nad = (long)(N*dece)/(acce+dece);
na = (long)(dspeed*dspeed)/(2*alfa*acce);
if (nad > na) { nd = N - na * acce/dece; }//case (i)
else { na = nad; nd = na; } //case (ii)
Co = f * sqrt(2*alfa/acce); Ci = Co;//Co is initial time delay
} else {//without acceleration
na = 0; nd = N; Ci = Cm;
}
if (nd < na) { // this is might be due to rounding
long np=na; na=nd;nd=np;//exchange the values of 'na' and 'nd'
} np = 0;
}
//this function is called for every cycle in the loop procedure
void serialEvent(){ //command of the stepper motor
if (Serial.available() > 0 ){
char ch = toupper(Serial.read());
if ((ch == 'S') || (ch == 'M')) chh = ch;
if ((ch == 'U') || (ch == 'B') || (ch == 'A') || (ch == 'D')){
chh = ch; broj = 0;
}
//if ch is alphanumeric than calculate of the parameter value
if (ch >= '0' && ch <= '9') broj = broj * 10 + (int)ch-'0';
if (ch == ','){//comma is denoted end of command
if (chh == 'U') {ugao = broj/10.0; init(ugao);}
if (chh == 'B') dspeed = broj/10.0;
if (chh == 'A') acce = broj/10.0;
if (chh == 'D') dece = broj/10.0;
if (chh == 'S') stoj = true; //STOP moving
if (chh == 'M') stoj = false; //continuoe the moving
broj = 0;
}
}
REFERENCES
[1.] Industrial Circuits Application Note. “Stepper Motor Basics”, http://www.solarbotics.net/
library/pdflib/pdf/motor- bas.pdf
[2.] Reston Condit, Dr. Douglas W. Jones, “Stepping Motors Fundamentals”, Microchip AN907,
http://homepage.cs.uio- wa.edu/~jones /step/ an907a.pdf
[3.] Atmel Corporation, “AVR446: Linear speed control of stepper motor”, Application note,
http://fab.cba.mit.edu/classes/ MIT/961.09/prjects/i0/doc8017.pdf
[4.] David Austin, “Generate stepper motor speed profiles in real time”, Embedded Systems Programming,
January 2005, www.embedd ed.com/56800129
[5.] Aryeh Eiderman, “Real Time Stepper Motor Linear Ramping Just by Addition and Multipli-cation,”
http://hwml.com/ LeibRamp.pdf
[6.] Pramod Ranade, “Linear Motor Control Witdout the Math,“SPJ Embedded Technologies, April 2009,
https://www.eeti- mes.com/design/other/4026992/
[7.] http://picprog.strongedge.net/step_prof/step-profile.html
[8.] Paul Lambrechts, Matthijs Boerlage, Maarten Steinbuch, “Trajectory planning and feedforward design for
electromechanical motion systems,” Control Engineering Practice 13 (2005) 145-157, ScienceDirect
[9.] Mihajlo J. Stojčić, “Design electromechanical positioning system with controlled jerk”, Heavy Machinery -
HM 2011, pp. 13-17
56 Tome XI (Year 2013). Fascicule 3. ISSN 1584 – 2673