Skip to content

Added demos, renamed examples #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/motorkit_dc_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time
from adafruit_motorkit import MotorKit

kit = MotorKit()

kit.motor1.throttle = 0

while True:
print("Forward!")
kit.motor1.throttle = 0.5
time.sleep(1)

print("Speed up...")
for i in range(0, 100):
speed = i * 0.01
kit.motor1.throttle = speed
time.sleep(0.01)

print("Slow down...")
for i in reversed(range(0, 100)):
speed = i * 0.01
kit.motor1.throttle = speed
time.sleep(0.01)

print("Backward!")
kit.motor1.throttle = -0.5
time.sleep(1)

print("Speed up...")
for i in range(-100, 0):
speed = i * 0.01
kit.motor1.throttle = speed
time.sleep(0.01)

print("Slow down...")
for i in reversed(range(-100, 0)):
speed = i * 0.01
kit.motor1.throttle = speed
time.sleep(0.01)

print("Stop!")
kit.motor1.throttle = 0
time.sleep(1)
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
st1 = threading.Thread()
st2 = threading.Thread()


# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
kit.stepper1.release()
kit.stepper2.release()


atexit.register(turnOffMotors)

stepstyles = [STEPPER.SINGLE, STEPPER.DOUBLE, STEPPER.INTERLEAVE, STEPPER.MICROSTEP]


def stepper_worker(stepper, numsteps, direction, style):
#print("Steppin!")
for _ in range(numsteps):
stepper.onestep(direction=direction, style=style)
#print("Done")


while True:
if not st1.isAlive():
randomdir = random.randint(0, 1)
Expand All @@ -41,12 +45,12 @@ def stepper_worker(stepper, numsteps, direction, style):
else:
move_dir = STEPPER.BACKWARD
print("backward")
randomsteps = random.randint(10,50)
randomsteps = random.randint(10, 50)
print("%d steps" % randomsteps)
st1 = threading.Thread(target=stepper_worker, args=(kit.stepper1,
randomsteps,
move_dir,
stepstyles[random.randint(0,3)],))
stepstyles[random.randint(0, 3)],))
st1.start()

if not st2.isAlive():
Expand All @@ -58,12 +62,12 @@ def stepper_worker(stepper, numsteps, direction, style):
else:
move_dir = STEPPER.BACKWARD
print("backward")
randomsteps = random.randint(10,50)
randomsteps = random.randint(10, 50)
print("%d steps" % randomsteps)
st2 = threading.Thread(target=stepper_worker, args=(kit.stepper2,
randomsteps,
move_dir,
stepstyles[random.randint(0,3)],))
stepstyles[random.randint(0, 3)],))
st2.start()

time.sleep(0.1) # Small delay to stop from constantly polling threads
Expand Down
4 changes: 2 additions & 2 deletions examples/Robot.py → examples/motorkit_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

kit = MotorKit()

class Robot(object):

class Robot:
def __init__(self, left_trim=0, right_trim=0, stop_at_exit=True):
"""Create an instance of the robot. Can specify the following optional
parameter
Expand Down Expand Up @@ -91,7 +92,6 @@ def backward(self, speed, seconds=None):
time.sleep(seconds)
self.stop()


def right(self, speed, seconds=None):
"""Spin to the right at the specified speed. Will start spinning and
return unless a seconds value is specified, in which case the robot will
Expand Down
10 changes: 5 additions & 5 deletions examples/RobotTest.py → examples/motorkit_robot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# motor. Likewise if it veers right then adjust the _left_ motor trim to a small
# negative value. Increase or decrease the trim value until the bot moves
# straight forward/backward.
LEFT_TRIM = 0
RIGHT_TRIM = 0
LEFT_TRIM = 0
RIGHT_TRIM = 0


# Create an instance of the robot with the specified trim values.
Expand All @@ -36,9 +36,9 @@
# is optional and if not specified the robot will start moving
# forever.

robot.left(0.5,1)
robot.right(0.5,1)
robot.steer(0.5,0.2)
robot.left(0.5, 1)
robot.right(0.5, 1)
robot.steer(0.5, 0.2)
time.sleep(3)
robot.stop() # Stop the robot from moving.

Expand Down
31 changes: 31 additions & 0 deletions examples/motorkit_stepper_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from adafruit_motor import stepper
from adafruit_motorkit import MotorKit

kit = MotorKit()

kit.stepper1.release()

while True:
print("Single coil steps")
for i in range(100):
kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)
for i in range(100):
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)

print("Double coil steps")
for i in range(100):
kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
for i in range(100):
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

print("Interleaved coil steps")
for i in range(100):
kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.INTERLEAVE)
for i in range(100):
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.INTERLEAVE)

print("Microsteps")
for i in range(100):
kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.MICROSTEP)
for i in range(100):
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.MICROSTEP)