-
Notifications
You must be signed in to change notification settings - Fork 5
/
single_stepper.py
70 lines (56 loc) · 3.4 KB
/
single_stepper.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
from pimoroni_yukon import Yukon
from pimoroni_yukon import SLOT1 as SLOT
from pimoroni_yukon.modules import DualMotorModule
from pimoroni_yukon.devices.stepper import OkayStepper
"""
Drive a stepper motor from a Dual Motor Module connected to Slot1.
A sequence of movements will be played.
Press "Boot/User" to exit the program.
"""
# Put your movements here!
# First value is the position (in steps) to go to, second is the duration (in seconds) to get there
# 200 steps will produce a complete rotation for a typical stepper with 1.8 degrees per step
MOVEMENTS = [(200, 2),
(400, 1),
(0, 3),
(0, 2),
(-100, 2),
(0, 5)]
# Constants
CURRENT_SCALE = 0.5 # How much to scale the output current to the stepper motor by, between 0.0 and 1.0
# Variables
yukon = Yukon() # Create a new Yukon object
module = DualMotorModule() # Create a DualMotorModule object
stepper = None # A variable to store an OkayStepper object created later
next_movement = 0 # The index of the next movement to perform
# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
yukon.register_with_slot(module, SLOT) # Register the DualMotorModule object with the slot
yukon.verify_and_initialise() # Verify that a DualMotorModule is attached to Yukon, and initialise it
yukon.enable_main_output() # Turn on power to the module slots
# Create a class for controlling the stepper motor, in this case OkayStepper, and provide it with the DualMotorModule's two outputs
stepper = OkayStepper(module.motor1, module.motor2, current_scale=CURRENT_SCALE)
# Set the hardware current limit to its maximum (OkayStepper controls current with PWM instead)
module.set_current_limit(DualMotorModule.MAX_CURRENT_LIMIT)
module.enable() # Enable the motor driver on the DualMotorModule
# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():
if not stepper.is_moving():
# Have we reached the end of the movements?
if next_movement >= len(MOVEMENTS):
stepper.release() # Release the stepper motor
module.disable() # Disable the motor driver
yukon.set_led('A', False) # Show that the stepper motor is no longer moving
break # Exit the main while loop
step, duration = MOVEMENTS[next_movement] # Extract the current movement from the list
stepper.move_to(step, duration) # Initiate the movement
yukon.set_led('A', True) # Show that the stepper motor is moving
next_movement += 1 # Advance the next movement index
# Perform a single check of Yukon's internal voltage, current, and temperature sensors
# NOTE. This is currently commented out as it interfers with the precise timing needed by stepper motors
# yukon.monitor_once()
finally:
if stepper is not None:
stepper.release() # Release the stepper motor (if not already done so)
# Put the board back into a safe state, regardless of how the program may have ended
yukon.reset()