Stepper motor control
This model demonstrates how to share blocks Stepper motor driver (Stepper motor driver) and Stepper motor allows you to study the process of controlling a permanent magnet stepper motor by position and speed.
The step angle of the stepper motor under study is 1.8 degrees. In position control mode, the Ref input sets the desired number of steps. In the speed control mode, the Ref input determines the desired number of steps per second.
This system-level model is well suited for studying the dynamics of a stepper motor, as well as for checking whether steps will be skipped when working with a given load. It can be used to debug the motor controller in order to improve the accuracy of the steps. The controller (in one case partially, in the other completely) is implemented as a ready-made stepper motor control module, the code of which can be loaded onto the microcontroller.
Simulation results
Let's study how the angle of rotation of the motor shaft changes when the control signal changes. The shaft position control algorithm takes a command in the form of a number of steps and converts it into a sequence of pulses that control the stepper motor driver. Jumps in the angular velocity graph occur when the shaft is fixed in a given position.
model = engee.open("stepper-motor-with-position-control.engee")
data_p = engee.run(model)
degPerStep = 1.8;
angle_measured = data_p["Угол вала"]
angle_command = data_p["Управление"]
shaft_speed = data_p["Stepper Motor.rod_flange.w"]
p1 = plot(angle_measured.time, (rad2deg.(angle_measured.value) .+ 0.5*degPerStep)./degPerStep,
label="Измеренный", lw=2, ls=:solid, titlefont=font(10), guidefont=font(8))
plot!( p1, angle_command.time, angle_command.value, ls=:dash, label="Заданный", lc=:red)
plot!(p1, title="Угол поворота вала (кол-во шагов)", ylabel="№ шага")
plot(
p1,
plot(shaft_speed.time, shaft_speed.value, label=false, lw=2,
xlabel="Время (с)", ylabel="Скорость (градусы/сек)", title="Угловая скорость вала"),
titlefont=font(10), guidefont=font(8), layout=(2,1)
)
Now let's study how the angle of rotation of the shaft changes during speed control. The speed control algorithm takes a command in the form of the number of steps per second and converts it into a sequence of pulses that control the stepper motor driver. Jumps in the angular velocity graph occur when moving to the next step.
Pkg.add("NumericalIntegration")
using NumericalIntegration
model = engee.open("stepper-motor-with-speed-control.engee")
data = engee.run(model)
degPerStep = 1.8;
angle_measured = data["Угол вала"]
angle_command = data["Управление"]
shaft_speed = data["Stepper Motor.rod_flange.w"]
p2 = plot(angle_measured.time, (rad2deg.(angle_measured.value) .+ 0.5*degPerStep)./degPerStep, lw=2, ls=:solid, label="Измеренный")
plot!( p2, angle_command.time, cumul_integrate(angle_command.time,angle_command.value), ls=:dash, label="Заданный", lc=:red)
plot!(p2, title="Угол поворота вала (кол-во шагов)", ylabel="№ шага")
plot(
p2,
plot(shaft_speed.time, shaft_speed.value, label=false, lw=2,
xlabel="Время (с)", ylabel="Скорость (градусы/сек)", title="Угловая скорость вала"),
titlefont=font(10), guidefont=font(8), layout=(2,1)
)
The following graph shows how the condition of the terminals of the stepper motor driver is related to the movement of the shaft. The driver initiates a step each time the ENA signal exceeds the switch-on threshold voltage (Enable threshold voltage).
t = data_p["Stepper Motor Driver.p_a.v"].time;
pA = data_p["Stepper Motor Driver.p_a.v"].value;
nA = data_p["Stepper Motor Driver.n_a.v"].value;
pB = data_p["Stepper Motor Driver.p_b.v"].value;
nB = data_p["Stepper Motor Driver.n_b.v"].value;
ENA = data_p["Stepper Motor Driver.enable_port.v"].value;
REV = data_p["Stepper Motor Driver.reverse_port.v"].value;
p3 = plot(t, pA, label="Pin A+", lw=2, yticks=([-5,0,5],["-5V", "0V", "+5V"]))
plot!(p3, t, nA, label="Pin A-", lw=2)
plot!(p3, t, pB, label="Pin B+", lw=2, ls=:dash)
plot!(p3, t, nB, label="Pin B-", lw=2, ls=:dash)
plot!(p3, t, ENA.-12, label="Pin ENA", lw=2)
plot!(p3, t, REV.-12, label="Pin REV", lw=2)
plot!(p3, xlabel="Время (с)", title="Напряжение на выводах драйвера шагового двигателя",
guidefont=font(8), titlefont=font(11))
plot(p1) # Повтор графика для сопоставления
Conclusion
To focus on developing the control algorithm, we used high-level models of a driver and a no-load stepper motor. The constructed graphs allow us to estimate the reaction speed and correctness of control, and by modifying the model we could study the tendency of the system to skip steps.