Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save projetsdiy/63e42eb6d9f0755be9c2e67ae903d413 to your computer and use it in GitHub Desktop.
Save projetsdiy/63e42eb6d9f0755be9c2e67ae903d413 to your computer and use it in GitHub Desktop.
Drive robotic arm with a Gamepad from a Raspberry Pi with HTTP requests and ESP8266
# HTTP Client to drive Arduino Robotic kit (ROT2U 6DOF) with a Gamepad and a Raspberry Pi 3
# Client HTTP permettant de piloter un bras robotique (kit ROT2U 6DOF) avec un Gamepad depuis un Raspberry Pi 3
# https://projetsdiy.fr - https://diyprojects.io - dev. 2017
# Tutoriel complet https://projetsdiy.fr/piloter-bras-robotique-wifi-gamepad-raspberry-pi-python-evdev-requete-http/
import requests, time
from evdev import InputDevice, categorize, ecodes
from threading import Thread
# Adresse IP et point d entree de la requete HTTP sur l'ESP8266 | ESP8266 Http and end point
esp8266url = "http://192.168.1.65/cmd"
# cree un objet 'gamepad' attache au port USB | creates object 'gamepad' to store the data
# executer ls /dev/input pour cidentifier le port USB | execute the command ls /dev/input in a Terminal to find usb port
gamepad = InputDevice('/dev/input/event2')
# Gamepad connecte | prints out device info at start
print("Gamepad: ",gamepad)
# Variables globales permettant d actualiser la requete HTTP | Global variables to update HTTP thread
servo = ""
direction = ""
send = False
httpFreq = .050
aBtn = 289
bBtn = 290
xBtn = 288
yBtn = 291
lBtn = 292
rBtn = 293
selBtn = 296
staBtn = 297
# HTPP request thread
def sendRequest():
global servo
global direction
global send
while True:
# Un seul servo a la fois pour le moment | Only one servo in the version
payload = {
servo:direction
}
# Envoi les ordres de deplacement jusqu a ce que send = false | Send HTTP request until send = False
if send:
r = requests.get(esp8266url, params=payload)
print r.url
print r.json
time.sleep(httpFreq)
# Demarre le thread
t1 = Thread(target=sendRequest)
t1.start()
#evdev takes care of polling the controller in a loop
for event in gamepad.read_loop():
print("Event: ",categorize(event)," |value: ",event.value)
if event.type == ecodes.EV_KEY:
print("button is pressed",categorize(event))
if event.value == 1:
if event.code == xBtn:
servo = "servo2"
direction = "up"
send = True
print("X")
elif event.code == bBtn:
servo = "servo2"
direction = "down"
send = True
print("B")
elif event.code == aBtn:
servo = "servo3"
direction = "up"
send = True
print("A")
elif event.code == yBtn:
servo = "servo3"
direction = "down"
send = True
print("Y")
elif event.code == lBtn:
servo = "servo4"
direction = "up"
send = True
print("LEFT BTN")
elif event.code == rBtn:
servo = "servo4"
direction = "down"
send = True
print("RIGHT BTN")
elif event.code == selBtn:
servo = "servo5"
direction = "open"
send = True
print("Select BTN")
elif event.code == staBtn:
servo = "servo5"
direction = "close"
send = True
print("Start BTN")
elif event.value == 0:
send = False
elif event.type == ecodes.EV_ABS:
absevent = categorize(event)
print ecodes.bytype[absevent.event.type][absevent.event.code], absevent.event.value
if ecodes.bytype[absevent.event.type][absevent.event.code] == "ABS_X":
if absevent.event.value == 0:
servo = "servo0"
direction = "left"
send = True
print("Left")
elif absevent.event.value == 255:
servo = "servo0"
direction = "right"
send = True
print("Right")
elif absevent.event.value == 127:
send = False
elif ecodes.bytype[absevent.event.type][absevent.event.code] == "ABS_Y":
if absevent.event.value == 0:
servo = "servo1"
direction = "up"
send = True
print("Up")
elif absevent.event.value == 255:
servo = "servo1"
direction = "down"
send = True
print("Down")
elif absevent.event.value == 127:
send = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment