|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Billy Bass Motor Test Script |
| 4 | +----------------------------- |
| 5 | +Tests the L298N motor driver with and without ENA GPIO control. |
| 6 | +
|
| 7 | +Wiring: |
| 8 | + Pi Pin 6 (GND) -> L298N GND |
| 9 | + Pi Pin 11 (GPIO17) -> L298N IN1 |
| 10 | + Pi Pin 12 (GPIO18) -> L298N ENA (jumper cap removed) |
| 11 | + Pi Pin 13 (GPIO27) -> L298N IN2 |
| 12 | + Battery + -> L298N 12V/VIN |
| 13 | + Battery - -> L298N GND |
| 14 | + L298N OUT1 -> Motor wire 1 |
| 15 | + L298N OUT2 -> Motor wire 2 |
| 16 | +
|
| 17 | +Usage: |
| 18 | + python3 scripts/test_motor.py |
| 19 | +""" |
| 20 | + |
| 21 | +import lgpio |
| 22 | +import time |
| 23 | +import sys |
| 24 | + |
| 25 | +# GPIO pin numbers (BCM numbering) |
| 26 | +IN1 = 17 # Pi Pin 11 -> L298N IN1 |
| 27 | +ENA = 18 # Pi Pin 12 -> L298N ENA |
| 28 | +IN2 = 27 # Pi Pin 13 -> L298N IN2 |
| 29 | + |
| 30 | +# GPIO chip for Pi 5 header pins (RP1 controller) |
| 31 | +GPIO_CHIP = 4 |
| 32 | + |
| 33 | + |
| 34 | +def motor_stop(h): |
| 35 | + """Stop the motor (both LOW = coast/stop).""" |
| 36 | + lgpio.gpio_write(h, IN1, 0) |
| 37 | + lgpio.gpio_write(h, IN2, 0) |
| 38 | + |
| 39 | + |
| 40 | +def motor_forward(h): |
| 41 | + """Spin motor in direction A (IN1=HIGH, IN2=LOW).""" |
| 42 | + lgpio.gpio_write(h, IN1, 1) |
| 43 | + lgpio.gpio_write(h, IN2, 0) |
| 44 | + |
| 45 | + |
| 46 | +def motor_reverse(h): |
| 47 | + """Spin motor in direction B (IN1=LOW, IN2=HIGH).""" |
| 48 | + lgpio.gpio_write(h, IN1, 0) |
| 49 | + lgpio.gpio_write(h, IN2, 1) |
| 50 | + |
| 51 | + |
| 52 | +def run_motor_tests(h, label): |
| 53 | + """Run the three motor tests with a label prefix.""" |
| 54 | + # Test 1: Forward direction |
| 55 | + print(f"[{label} TEST 1] Motor direction A (IN1=HIGH, IN2=LOW) for 1 second...") |
| 56 | + motor_forward(h) |
| 57 | + time.sleep(1.0) |
| 58 | + motor_stop(h) |
| 59 | + print(f"[{label} STOP] Motor stopped.") |
| 60 | + print() |
| 61 | + time.sleep(1.0) |
| 62 | + |
| 63 | + # Test 2: Reverse direction |
| 64 | + print(f"[{label} TEST 2] Motor direction B (IN1=LOW, IN2=HIGH) for 1 second...") |
| 65 | + motor_reverse(h) |
| 66 | + time.sleep(1.0) |
| 67 | + motor_stop(h) |
| 68 | + print(f"[{label} STOP] Motor stopped.") |
| 69 | + print() |
| 70 | + time.sleep(1.0) |
| 71 | + |
| 72 | + # Test 3: Quick pulses (mouth flapping) |
| 73 | + print(f"[{label} TEST 3] Quick pulses (0.3s on, 0.3s off) x 5 — mouth movement...") |
| 74 | + for i in range(5): |
| 75 | + motor_forward(h) |
| 76 | + time.sleep(0.3) |
| 77 | + motor_stop(h) |
| 78 | + time.sleep(0.3) |
| 79 | + print(f"[{label} STOP] Motor stopped.") |
| 80 | + print() |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + print("=" * 50) |
| 85 | + print("Billy Bass Motor Test (with ENA control)") |
| 86 | + print("=" * 50) |
| 87 | + print() |
| 88 | + print("Wiring:") |
| 89 | + print(" Pi Pin 6 (GND) -> L298N GND") |
| 90 | + print(" Pi Pin 11 (GPIO17) -> L298N IN1") |
| 91 | + print(" Pi Pin 12 (GPIO18) -> L298N ENA") |
| 92 | + print(" Pi Pin 13 (GPIO27) -> L298N IN2") |
| 93 | + print(" Battery + -> L298N 12V/VIN") |
| 94 | + print(" Battery - -> L298N GND") |
| 95 | + print(" L298N OUT1/OUT2 -> Motor wires") |
| 96 | + print() |
| 97 | + |
| 98 | + # Open GPIO chip |
| 99 | + try: |
| 100 | + h = lgpio.gpiochip_open(GPIO_CHIP) |
| 101 | + except Exception as e: |
| 102 | + print(f"ERROR: Cannot open GPIO chip {GPIO_CHIP}: {e}") |
| 103 | + print("Try running with: sudo python3 scripts/test_motor.py") |
| 104 | + return 1 |
| 105 | + |
| 106 | + try: |
| 107 | + # Claim pins as output, initially LOW |
| 108 | + lgpio.gpio_claim_output(h, IN1, 0) |
| 109 | + lgpio.gpio_claim_output(h, IN2, 0) |
| 110 | + lgpio.gpio_claim_output(h, ENA, 0) |
| 111 | + print("[OK] GPIO17 (IN1), GPIO18 (ENA), GPIO27 (IN2) set to OUTPUT, all LOW") |
| 112 | + print() |
| 113 | + |
| 114 | + # ---- ROUND 1: WITHOUT ENA (ENA=LOW) ---- |
| 115 | + print("=" * 50) |
| 116 | + print("ROUND 1: ENA = LOW (disabled via GPIO)") |
| 117 | + print(" Motor should NOT move.") |
| 118 | + print("=" * 50) |
| 119 | + print() |
| 120 | + lgpio.gpio_write(h, ENA, 0) |
| 121 | + run_motor_tests(h, "NO-ENA") |
| 122 | + |
| 123 | + time.sleep(1.0) |
| 124 | + |
| 125 | + # ---- ROUND 2: WITH ENA (ENA=HIGH) ---- |
| 126 | + print("=" * 50) |
| 127 | + print("ROUND 2: ENA = HIGH (enabled via GPIO)") |
| 128 | + print(" Motor SHOULD move.") |
| 129 | + print("=" * 50) |
| 130 | + print() |
| 131 | + lgpio.gpio_write(h, ENA, 1) |
| 132 | + run_motor_tests(h, "ENA-ON") |
| 133 | + |
| 134 | + # Done |
| 135 | + lgpio.gpio_write(h, ENA, 0) |
| 136 | + print("=" * 50) |
| 137 | + print("Test complete!") |
| 138 | + print() |
| 139 | + print("Expected results:") |
| 140 | + print(" Round 1 (ENA LOW): Nothing moves") |
| 141 | + print(" Round 2 (ENA HIGH): Motor moves in all 3 tests") |
| 142 | + print() |
| 143 | + print("If Round 2 also didn't move:") |
| 144 | + print(" 1. Check battery switch is ON and batteries are fresh") |
| 145 | + print(" 2. Check ENA jumper cap is REMOVED from L298N") |
| 146 | + print(" 3. Check GPIO18 wire is on the correct ENA pin") |
| 147 | + print(" 4. Check all screw terminals are tight") |
| 148 | + print("=" * 50) |
| 149 | + |
| 150 | + except KeyboardInterrupt: |
| 151 | + print("\n[INTERRUPTED] Stopping motor...") |
| 152 | + finally: |
| 153 | + motor_stop(h) |
| 154 | + lgpio.gpio_write(h, ENA, 0) |
| 155 | + lgpio.gpio_free(h, IN1) |
| 156 | + lgpio.gpio_free(h, IN2) |
| 157 | + lgpio.gpio_free(h, ENA) |
| 158 | + lgpio.gpiochip_close(h) |
| 159 | + print("[CLEANUP] GPIO released, motor stopped.") |
| 160 | + |
| 161 | + return 0 |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + sys.exit(main()) |
0 commit comments