-
Notifications
You must be signed in to change notification settings - Fork 8
/
bbpystepper.py
87 lines (63 loc) · 2.24 KB
/
bbpystepper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
bbpystepper is a Python module used to control a stepper motor via the
BeagleBone
"""
from __future__ import division
import Adafruit_BBIO.GPIO as GPIO
import time
import math
def initialize_pins(pins):
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
def set_all_pins_low(pins):
for pin in pins:
GPIO.output(pin, GPIO.LOW)
def wavedrive(pins, pin_index):
for i in range(len(pins)):
if i == pin_index:
GPIO.output(pins[i], GPIO.HIGH)
else:
GPIO.output(pins[i], GPIO.LOW)
def fullstep(pins, pin_index):
"""pin_index is the lead pin"""
GPIO.output(pins[pin_index], GPIO.HIGH)
GPIO.output(pins[(pin_index+3) % 4], GPIO.HIGH)
GPIO.output(pins[(pin_index+1) % 4], GPIO.LOW)
GPIO.output(pins[(pin_index+2) % 4], GPIO.LOW)
class Stepper(object):
def __init__(self, steps_per_rev=2048.0,
pins=["P8_13", "P8_14", "P8_15", "P8_16"]):
self.pins = pins
initialize_pins(self.pins)
set_all_pins_low(self.pins)
self.angle = 0
self.steps_per_rev = steps_per_rev
# Initialize stepping mode
self.drivemode = fullstep
def rotate(self, degrees=360, rpm=15):
step = 0
# Calculate time between steps in seconds
wait_time = 60.0/(self.steps_per_rev*rpm)
# Convert degrees to steps
steps = math.fabs(degrees*self.steps_per_rev/360.0)
self.direction = 1
if degrees < 0:
self.pins.reverse()
self.direction = -1
while step < steps:
for pin_index in range(len(self.pins)):
self.drivemode(self.pins, pin_index)
time.sleep(wait_time)
step += 1
self.angle = (self.angle + self.direction/self.steps_per_rev \
*360.0) % 360.0
if degrees < 0:
self.pins.reverse()
set_all_pins_low(self.pins)
def zero_angle(self):
self.angle = 0
def main():
stepper = Stepper()
stepper.rotate()
if __name__ == "__main__":
main()