I bought a Raspberry Pi thinking that I might find a new hobby that I can do indoors during lockdown. I thought that if I could control the motor, it would be useful in experiments later, so I bought a stepper motor together, so I will try to control it. It is interesting just to rotate at a constant speed, but the goal was to be able to do any exercise. Knowledge of electrical and electronic circuits I started from almost zero, but I could easily do it because there was a lot of information on the net. I really appreciate it. It seems that it can be controlled in various ways, but I would like to introduce the method that I found the easiest.
Below is a list of the hardware used.
First is the basic constant speed rotation. Use RpiMotorLib, A Raspberry pi python motor library on GitHub. It seems that not only stepper motors but also DC motors and servo motors can be controlled. You can install it from the terminal with sudo pip install rpimotorlib
.
After installing the library, wire it according to the Very polite tutorial and just copy and paste the code to turn the motor. I will. I wired it as follows (sorry for the hand-drawn rough drawing).
The resolution of the image is poor and it is difficult to see, but the wiring is as shown in the table below.
Raspberry GPIO | driver | motor |
---|---|---|
17 | IN1 | - |
18 | IN2 | - |
27 | IN3 | - |
22 | IN4 | - |
- | OUT1 | A- |
- | OUT2 | B- |
- | OUT3 | A+ |
- | OUT4 | B+ |
The main function is motor_run (GpioPins, wait, steps, ccwise, verbose, steptype, initdelay)
. The explanation of each argument is as follows.
'True'
is counterclockwise, 'False'
is clockwise. The default is 'False'
.'True'
, the status etc. will be displayed. The default is 'False'
.'full'
,'half'
, 'wave'
. See the tutorial for the strengths and weaknesses of each.For example, if you want to rotate 5Hz counterclockwise at a rotation speed of 0.5Hz in half-step mode, the code is as follows. It's very simple.
python
import time
import RPi.GPIO as GPIO
from RpiMotorLib import RpiMotorLib
GpioPins = [17, 18, 27, 22] #GPIO pin. IN1 from the left, IN2, IN3,Pin to connect to IN4.
f = 0.5 #Rotational speed[Hz]
Rev = 5 #Rotate 5 times
s_angle = 1.8 #Step angle[deg]
wait = (1/f)*(s_angle/360)/2 #Time between pulses. Half-Divide by 2 for step mode
#Give the motor a name and specify the motor type (Nema or 28BYJ).
mymotortest = RpiMotorLib.BYJMotor('MyMotorOne', 'Nema')
#Main. Arguments are GPIO pin, wait, number of steps, reverse rotation, verbose, step mode, first delay from the left[ms]
mymotortest.motor_run(GpioPins, wait, Rev*50, True, False, 'half', 0.05)
#Finally clean up the pins
GPIO.cleanup()
pleasant!
Since I was able to turn it, I think I can do any exercise depending on the chord. I would like to make the motor move arbitrarily. As an example, let's make a sine wave pitch motion.
7 in 1 step.Since it rotates 2 °, the amplitudewait
Can only take a positive value, so take an absolute valueccwise
To'True'
All you have to do is pass.
The code is as follows.
python
GpioPins = [17, 18, 27, 22]
T = 2 # Period [s]
N_period = 2 # No. of periods
amp = 72
unit_angle = 7.2
s_angle = 1.8 # [deg]
N = int(4*amp/unit_angle)
theta = amp*sin(np.linspace(0, 2*pi, N)) # Angle time history [deg]
dt = T/len(theta) # time-step [s]
omega = np.gradient(theta, dt)/360 # [Hz]
omega_pos = abs(omega)
pm = omega < 0 # boolean for CW/CCW
wait = (1/omega_pos)*(s_angle/360)/2
mymotortest = RpiMotorLib.BYJMotor, 'MyMotorOne', 'Nema')
time.sleep(0.002)
for _ in np.arange(N_period):
for k in np.arange(N):
mymotortest.motor_run(GpioPins, wait[k], 1, pm[k], False, "half", 0)
GPIO.cleanup()
did it!
Recommended Posts