rhandycan1

PYTHON_PI_PICO W_ 4SERVO_ROBOT

Aug 3rd, 2025 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | Source Code | 0 0
  1. '''PROGRAM BY: RANDY N. CAÑADA
  2. SUBSCRIBE: FB PAGES:
  3. Playlist.ph:
  4. https://www.facebook.com/profile.php?id=61573177858722
  5.  
  6. Arduino:
  7. https://www.facebook.com/rhacan2?__tn__=R
  8.  
  9. YOUTUBE:
  10. PlaylistPH
  11. https://www.youtube.com/@PlaylistPh-h2v
  12.  
  13. ElectroCrafters:
  14. https://www.youtube.com/channel/UCu1aqzUdTqSnTN5BHNlbVgA '''
  15.  
  16.  
  17.  
  18. from inputs import get_gamepad
  19. import socket
  20. import time
  21.  
  22. piPico_IP = ''  # ← Replace with your Pico W's actual IP
  23. PORT = 1234
  24.  
  25. def connect_socket():
  26.     while True:
  27.         try:
  28.             sock = socket.socket()
  29.             sock.connect((piPico_IP, PORT))
  30.             print(f"✅ Connected to Pico W at {piPico_IP}:{PORT}")
  31.             return sock
  32.         except Exception as e:
  33.             print(f"🔌 Connection failed: {e}")
  34.             time.sleep(3)
  35.  
  36. sock = connect_socket()
  37.  
  38. try:
  39.     while True:
  40.         events = get_gamepad()
  41.         for event in events:
  42.             code = event.code
  43.             value = event.state
  44.             typ = event.ev_type
  45.  
  46.             print(f"📥 Event → Code: {code} | Type: {typ} | Value: {value}")
  47.  
  48.             if typ == 'Absolute':
  49.                 if code == 'ABS_RX':
  50.                     msg = f"AXIS:ABS_RX:{value}\n"
  51.                 elif code == 'ABS_X':
  52.                     msg = f"AXIS:ABS_X:{value}\n"
  53.                 elif code == 'ABS_Y':
  54.                     msg = f"AXIS:ABS_Y:{value}\n"
  55.                 elif code == 'ABS_RY':
  56.                     msg = f"AXIS:ABS_RY:{value}\n"
  57.                 else:
  58.                     continue  # Skip unknown axis
  59.  
  60.                 try:
  61.                     sock.sendall(msg.encode())
  62.                     print(f"📤 Sent → {msg.strip()}")
  63.                 except Exception as e:
  64.                     print(f"⚠️ Send failed: {e}")
  65.                     sock.close()
  66.                     sock = connect_socket()  # Reconnect if needed
  67.  
  68. except KeyboardInterrupt:
  69.     print("👋 Shutting down dongle script")
  70.     sock.close()
  71.  
Advertisement
Add Comment
Please, Sign In to add comment