# Stepper motor control theory (or how to rotate a PTZ camera)

That rare case has come when the need for mathematics and even physics appeared in the work of a microcontroller programmer. I had an interesting task at work on the topic of developing a heavy (70kg) PTZ camera. There was a problem in physics from the kinematics section. True, in terms of difficulty, it can be attributed to the school curriculum of the 6th grade.

Stepper motors are used in CNC machines, 2D / 3D printers, scanners, plotters, PTZ cameras, radar antennas, turrets, linear actuators, pointer devices (analogue clocks) and maybe somewhere else.

Stepper motors have one problem. Due to high load inertia, stepper motors can lose steps and fail to complete commands. To minimize this, the stepper motor shaft must be untwisted smoothly. The easiest way is to use trapezoidal motion profiles.

Formulation of the problem

given:

 Parameter Explanation Units PhiSet The angle by which the stepper motor shaft must be rotated Radians OmegaMax Maximum possible angular frequency Radians/s BettaMax Maximum angular acceleration Radians/(s^2)

Given the angle, PhiSet, by which the stepper motor shaft should be rotated. The engine operates in three modes: acceleration ACCELwork at constant cruising speed RUNdeceleration DECEL. This results in a trapezoidal velocity profile.

Or acceleration ACCEL and slowdown DECEL. This results in a triangular speed profile (in case the OmegaMax cruising speed is very high and the angular path set to be traveled is very small).

Only acceleration is given as motion parameters. BettaMax [rad/s^2] and cruising speed OmegaMax [rad/s] in mode RUN. At zero time t0, the shaft starts moving and starts to pick up the angular velocity Omega with constant acceleration BettaMax. Calculate the time t1 at which the acceleration ACCEL should stop and the rotation should start at a constant speed OmegaMax (RUN phase), and calculate the time t2 at which the movement should stop at a constant speed OmegaMax RUN and start decelerating DECEL.

Note

The times t1 and t2 can coincideif BettaMax very high and OmegaMax few. For symmetry and simplicity, we assume that the acceleration during acceleration and acceleration during deceleration are modulo BettaMaxbut opposite in sign.

Terminology

The profile is a plot of angular velocity versus time Omega
function [ t1 t2 tf wlim] = calcTimesF( phi_set, w_max, B_max )
S1=((w_max)^2)/B_max;
if(phi_set < S1)
%without RUN t1= t2
t1=sqrt(phi_set/B_max);
t2=t1;
tf=2*t1;
wlim = B_max*t1;
else
%with RUN t1 < t2
t1=w_max/B_max;
t2=(phi_set/w_max);
tf=t2+t1;
wlim = w_max;
end
end

In reality, this problem must be solved for discrete movement, since in reality we are dealing with a stepper motor. There, the solution becomes much more complicated. In changing the stage of movement (ACCEL, RUN, DECEL) you have to navigate not by time, as in a continuous setting, but by the distance traveled (the number of steps taken by the stepper motor). There is a publication about this Asynchronous Control System for Stepper Motor With an Incremental Encoder Feedback.

Conclusion

As you can see, ordinary school mathematics is quite enough to solve real evaluation problems from the sale of machine tools.

When calculating, always check the dimension in the formulas at each step.

If you are programming microcontrollers and you had to resort to mathematics, then write it in the comments.

 Acronym decoding PTZ Pan-tilt-zoom CNC computer numerical control