|
| 1 | +from picozero import Stepper, pico_led |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +# Create devices |
| 5 | +stepper = Stepper((1, 2, 3, 4), step_sequence="half", step_delay=0.003) |
| 6 | + |
| 7 | +print("Stepper Motor Positioning Demo") |
| 8 | +print("Press Ctrl+C to exit") |
| 9 | +print() |
| 10 | + |
| 11 | +# Define preset positions in degrees |
| 12 | +positions = [0, 90, 180, 270, 360, 270, 180, 90] # Forward and return cycle |
| 13 | +position_names = ["Home", "90° CW", "180° CW", "270° CW", "Full rotation", "270° CCW", "180° CCW", "90° CCW"] |
| 14 | + |
| 15 | +def move_to_position(target_angle, description=""): |
| 16 | + """Move stepper to target angle from current position.""" |
| 17 | + current_angle = stepper.angle |
| 18 | + angle_diff = target_angle - current_angle |
| 19 | + |
| 20 | + print(f"Target: {description} ({target_angle}°)") |
| 21 | + print(f" Moving from {current_angle:.1f}° to {target_angle}°") |
| 22 | + pico_led.on() # Indicate movement |
| 23 | + |
| 24 | + if angle_diff > 0: |
| 25 | + print(f" → Rotating {angle_diff}° clockwise...") |
| 26 | + stepper.rotate_clockwise(angle_diff) |
| 27 | + elif angle_diff < 0: |
| 28 | + print(f" → Rotating {-angle_diff}° counter-clockwise...") |
| 29 | + stepper.rotate_counterclockwise(-angle_diff) |
| 30 | + else: |
| 31 | + print(" → Already at target position") |
| 32 | + |
| 33 | + pico_led.off() # Movement complete |
| 34 | + print(f" ✓ Position: {stepper.angle:.1f}° ({stepper.step_count} steps)") |
| 35 | + print() |
| 36 | + |
| 37 | +def demonstrate_positioning_methods(): |
| 38 | + """Demonstrate different positioning approaches.""" |
| 39 | + print("=== POSITIONING METHOD COMPARISON ===") |
| 40 | + print() |
| 41 | + |
| 42 | + # Method 1: Using convenience methods (current approach) |
| 43 | + print("Method 1: Convenience methods (rotate_clockwise/rotate_counterclockwise)") |
| 44 | + stepper.reset_position() |
| 45 | + move_to_position(90, "90° using rotate_clockwise") |
| 46 | + move_to_position(45, "45° using rotate_counterclockwise") |
| 47 | + |
| 48 | + sleep(1) |
| 49 | + |
| 50 | + # Method 2: Using parameterized methods |
| 51 | + print("Method 2: Parameterized methods (rotate with direction parameter)") |
| 52 | + stepper.reset_position() |
| 53 | + |
| 54 | + print("Target: 120° using rotate(120, direction='cw')") |
| 55 | + stepper.rotate(120, direction='cw') |
| 56 | + print(f" ✓ Position: {stepper.angle:.1f}°") |
| 57 | + |
| 58 | + print("Target: 60° using rotate(60, direction='ccw')") |
| 59 | + stepper.rotate(60, direction='ccw') |
| 60 | + print(f" ✓ Position: {stepper.angle:.1f}°") |
| 61 | + print() |
| 62 | + |
| 63 | + sleep(1) |
| 64 | + |
| 65 | +# Start demonstration |
| 66 | +stepper.reset_position() |
| 67 | +print(f"Starting position: {stepper.angle}° ({stepper.step_count} steps)") |
| 68 | +print() |
| 69 | + |
| 70 | +try: |
| 71 | + # Demonstrate positioning methods |
| 72 | + demonstrate_positioning_methods() |
| 73 | + |
| 74 | + # Main positioning sequence |
| 75 | + print("=== AUTOMATIC POSITIONING SEQUENCE ===") |
| 76 | + print("Moving through preset positions...") |
| 77 | + print() |
| 78 | + |
| 79 | + for i, (target_pos, name) in enumerate(zip(positions, position_names)): |
| 80 | + print(f"Step {i+1}/8:") |
| 81 | + move_to_position(target_pos, name) |
| 82 | + sleep(1.5) |
| 83 | + |
| 84 | + print("=== ADVANCED POSITIONING DEMO ===") |
| 85 | + print("Demonstrating precise positioning capabilities...") |
| 86 | + print() |
| 87 | + |
| 88 | + # Reset and demonstrate fractional positioning |
| 89 | + stepper.reset_position() |
| 90 | + |
| 91 | + # Small precise movements |
| 92 | + precise_positions = [22.5, 67.5, 112.5, 157.5, 202.5] |
| 93 | + for pos in precise_positions: |
| 94 | + move_to_position(pos, f"Precise positioning to {pos}°") |
| 95 | + sleep(1) |
| 96 | + |
| 97 | + # Return home |
| 98 | + move_to_position(0, "Return to home") |
| 99 | + |
| 100 | + print("Positioning demonstration complete!") |
| 101 | + |
| 102 | +except KeyboardInterrupt: |
| 103 | + print("\nInterrupted by user") |
| 104 | + |
| 105 | +finally: |
| 106 | + # Clean shutdown |
| 107 | + stepper.off() |
| 108 | + pico_led.off() |
| 109 | + stepper.close() |
| 110 | + pico_led.close() |
0 commit comments