Skip to content

Commit 050553e

Browse files
Merge pull request #8 from funkyremi/feat-stop-motor
feat(BYJMotor): Stop motor method Thanks, I will add to project at next update in future.
2 parents 69a3f1d + cacd687 commit 050553e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Documentation/28BYJ.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ to initialize.
129129
for step delay of .01 second for 100 step control signal sequence, in clockwise direction,
130130
verbose output off , in half step mode, with an init start delay of 50mS
131131

132+
The second function is called to stop the motor when the motor is moving.
133+
motor_stop()
132134

133135
```sh
134136

RpiMotorLib/RpiMotorLib.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
# ==================== CLASS SECTION ===============================
2929

30+
class StopMotorInterrupt(Exception):
31+
pass
3032

3133
class BYJMotor(object):
3234
"""class to control a 28BYJ-48 stepper motor with ULN2003 controller
@@ -40,7 +42,7 @@ def __init__(self, name="BYJMotorX", motor_type="28BYJ"):
4042
# while the script is running.
4143
self.curser_spin = ["/", "-", "|", "\\", "|"]
4244
self.spin_position = 0
43-
45+
self.stop_motor = False
4446

4547
def print_cursor_spin(self):
4648
""" Prints a spinning cursor. Used when verbose not set to false.
@@ -50,6 +52,9 @@ def print_cursor_spin(self):
5052
if self.spin_position > 4:
5153
self.spin_position = 0
5254

55+
def motor_stop(self):
56+
self.stop_motor = True
57+
5358
def motor_run(self, gpiopins, wait=.001, steps=512, ccwise=False,
5459
verbose=False, steptype="half", initdelay=.001):
5560
"""motor_run, moves stepper motor based on 7 inputs
@@ -76,6 +81,7 @@ def motor_run(self, gpiopins, wait=.001, steps=512, ccwise=False,
7681
7782
"""
7883
try:
84+
self.stop_motor = False
7985
for pin in gpiopins:
8086
GPIO.setup(pin, GPIO.OUT)
8187
GPIO.output(pin, False)
@@ -142,16 +148,21 @@ def print_status(enabled_pins):
142148
while steps_remaining > 0:
143149
for pin_list in step_sequence:
144150
for pin in gpiopins:
145-
if pin in pin_list:
146-
GPIO.output(pin, True)
151+
if self.stop_motor:
152+
raise StopMotorInterrupt
147153
else:
148-
GPIO.output(pin, False)
154+
if pin in pin_list:
155+
GPIO.output(pin, True)
156+
else:
157+
GPIO.output(pin, False)
149158
print_status(pin_list)
150159
time.sleep(wait)
151160
steps_remaining -= 1
152161

153162
except KeyboardInterrupt:
154163
print("User Keyboard Interrupt : RpiMotorLib: ")
164+
except StopMotorInterrupt:
165+
print("Stop Motor Interrupt : RpiMotorLib: ")
155166
except Exception as motor_error:
156167
print(sys.exc_info()[0])
157168
print(motor_error)

0 commit comments

Comments
 (0)