diff --git a/examples/motorkit_dc_test.py b/examples/motorkit_dc_test.py new file mode 100755 index 0000000..a6c1c5f --- /dev/null +++ b/examples/motorkit_dc_test.py @@ -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) diff --git a/examples/DualStepperTest.py b/examples/motorkit_dual_stepper_test.py similarity index 93% rename from examples/DualStepperTest.py rename to examples/motorkit_dual_stepper_test.py index fb888e7..7eb0c92 100644 --- a/examples/DualStepperTest.py +++ b/examples/motorkit_dual_stepper_test.py @@ -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) @@ -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(): @@ -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 diff --git a/examples/Robot.py b/examples/motorkit_robot.py similarity index 99% rename from examples/Robot.py rename to examples/motorkit_robot.py index 2d0fab5..54b3c88 100644 --- a/examples/Robot.py +++ b/examples/motorkit_robot.py @@ -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 @@ -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 diff --git a/examples/RobotTest.py b/examples/motorkit_robot_test.py similarity index 95% rename from examples/RobotTest.py rename to examples/motorkit_robot_test.py index fd9bec0..69d5727 100644 --- a/examples/RobotTest.py +++ b/examples/motorkit_robot_test.py @@ -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. @@ -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. diff --git a/examples/motorkit_stepper_test.py b/examples/motorkit_stepper_test.py new file mode 100755 index 0000000..32f8983 --- /dev/null +++ b/examples/motorkit_stepper_test.py @@ -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)