You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spydian/testscripts/motor_test.py

140 lines
2.9 KiB
Python

import time
7 years ago
import RPi.GPIO as GPIO
7 years ago
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
def full_step(phase, pins):
7 years ago
pin1, pin2, pin3, pin4 = pins
if phase == 0:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 1:
7 years ago
GPIO.output(pin1, 1)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 2:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
if phase == 3:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 1)
if phase == 4:
7 years ago
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
def half_step(phase, pins):
7 years ago
pin1, pin2, pin3, pin4 = pins
if phase == 0:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 1:
7 years ago
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 2:
7 years ago
GPIO.output(pin1, 1)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 3:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 0)
GPIO.output(pin4, 0)
if phase == 4:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 1)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
if phase == 5:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 0)
if phase == 6:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 1)
GPIO.output(pin4, 1)
if phase == 7:
7 years ago
GPIO.output(pin1, 0)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
if phase == 8:
7 years ago
GPIO.output(pin1, 1)
GPIO.output(pin2, 0)
GPIO.output(pin3, 0)
GPIO.output(pin4, 1)
7 years ago
def full_phase(pins):
# order=[1,2,3,4]
order = [4, 3, 2, 1]
7 years ago
print("Moving in {}-order".format(order))
for i in order:
full_step(i, pins)
time.sleep(0.005)
7 years ago
def half_phase(pins):
for i in range(0, 8):
half_step(i, pins)
7 years ago
time.sleep(0.0025)
7 years ago
def forward(pins):
half_phase(pins)
7 years ago
def backward(pins):
(pins[0], pins[1], pins[2], pins[3]) = (pins[1], pins[0], pins[2], pins[3])
half_phase(pins)
if __name__ == '__main__':
pins = [6, 13, 19, 26]
7 years ago
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
while True:
try:
start = time.time()
while (time.time() < start + 10):
7 years ago
forward(pins)
start = time.time()
while (time.time() < start + 10):
7 years ago
backward(pins)
except KeyboardInterrupt:
break
GPIO.cleanup()