27
27
28
28
# ==================== CLASS SECTION ===============================
29
29
30
+ class StopMotorInterrupt (Exception ):
31
+ pass
30
32
31
33
class BYJMotor (object ):
32
34
"""class to control a 28BYJ-48 stepper motor with ULN2003 controller
@@ -40,7 +42,7 @@ def __init__(self, name="BYJMotorX", motor_type="28BYJ"):
40
42
# while the script is running.
41
43
self .curser_spin = ["/" , "-" , "|" , "\\ " , "|" ]
42
44
self .spin_position = 0
43
-
45
+ self . stop_motor = False
44
46
45
47
def print_cursor_spin (self ):
46
48
""" Prints a spinning cursor. Used when verbose not set to false.
@@ -50,6 +52,9 @@ def print_cursor_spin(self):
50
52
if self .spin_position > 4 :
51
53
self .spin_position = 0
52
54
55
+ def motor_stop (self ):
56
+ self .stop_motor = True
57
+
53
58
def motor_run (self , gpiopins , wait = .001 , steps = 512 , ccwise = False ,
54
59
verbose = False , steptype = "half" , initdelay = .001 ):
55
60
"""motor_run, moves stepper motor based on 7 inputs
@@ -76,6 +81,7 @@ def motor_run(self, gpiopins, wait=.001, steps=512, ccwise=False,
76
81
77
82
"""
78
83
try :
84
+ self .stop_motor = False
79
85
for pin in gpiopins :
80
86
GPIO .setup (pin , GPIO .OUT )
81
87
GPIO .output (pin , False )
@@ -142,16 +148,21 @@ def print_status(enabled_pins):
142
148
while steps_remaining > 0 :
143
149
for pin_list in step_sequence :
144
150
for pin in gpiopins :
145
- if pin in pin_list :
146
- GPIO . output ( pin , True )
151
+ if self . stop_motor :
152
+ raise StopMotorInterrupt
147
153
else :
148
- GPIO .output (pin , False )
154
+ if pin in pin_list :
155
+ GPIO .output (pin , True )
156
+ else :
157
+ GPIO .output (pin , False )
149
158
print_status (pin_list )
150
159
time .sleep (wait )
151
160
steps_remaining -= 1
152
161
153
162
except KeyboardInterrupt :
154
163
print ("User Keyboard Interrupt : RpiMotorLib: " )
164
+ except StopMotorInterrupt :
165
+ print ("Stop Motor Interrupt : RpiMotorLib: " )
155
166
except Exception as motor_error :
156
167
print (sys .exc_info ()[0 ])
157
168
print (motor_error )
0 commit comments