-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.py
executable file
·72 lines (57 loc) · 2.37 KB
/
home.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
#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
import time
import atexit
import threading
import random
import requests
import json
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT()
# create empty threads (these will hold the stepper 1 and 2 threads)
st1 = threading.Thread()
st2 = threading.Thread()
# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
atexit.register(turnOffMotors)
myStepper1 = mh.getStepper(200, 1) # 200 steps/rev, motor port #1
myStepper2 = mh.getStepper(200, 2) # 200 steps/rev, motor port #1
myStepper1.setSpeed(60) # 30 RPM
myStepper2.setSpeed(60) # 30 RPM
stepstyles = [Adafruit_MotorHAT.SINGLE, Adafruit_MotorHAT.DOUBLE, Adafruit_MotorHAT.INTERLEAVE, Adafruit_MotorHAT.MICROSTEP]
def stepper_worker(stepper, numsteps, direction, style):
#print("Steppin!")
stepper.step(numsteps, direction, style)
#print("Done")
while (True):
if not st1.isAlive():
randomdir = random.randint(0, 1)
print("Stepper 1"),
if (randomdir == 0):
dir = Adafruit_MotorHAT.FORWARD
print("forward"),
else:
dir = Adafruit_MotorHAT.BACKWARD
print("backward"),
randomsteps = random.randint(10,50)
print("%d steps" % randomsteps)
st1 = threading.Thread(target=stepper_worker, args=(myStepper1, randomsteps, dir, stepstyles[random.randint(0,3)],))
st1.start()
if not st2.isAlive():
print("Stepper 2"),
randomdir = random.randint(0, 1)
if (randomdir == 0):
dir = Adafruit_MotorHAT.FORWARD
print("forward"),
else:
dir = Adafruit_MotorHAT.BACKWARD
print("backward"),
randomsteps = random.randint(10,50)
print("%d steps" % randomsteps)
st2 = threading.Thread(target=stepper_worker, args=(myStepper2, randomsteps, dir, stepstyles[random.randint(0,3)],))
st2.start()
time.sleep(0.1) # Small delay to stop from constantly polling threads (see: https://forums.adafruit.com/viewtopic.php?f=50&t=104354&p=562733#p562733)