Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. projetsdiy created this gist Dec 21, 2017.
    146 changes: 146 additions & 0 deletions rot2u 6dof websocket espduino esp8266 gamepad
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,146 @@
    # WebSocket Client to drive Arduino Robotic kit (ROT2U 6DOF) with a Gamepad and a Raspberry Pi 3
    # Client webSocket 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
    #
    from ws4py.client.threadedclient import WebSocketClient
    import time, requests
    from evdev import InputDevice, categorize, ecodes
    from threading import Thread

    # Adresse du serveur WebSocket sur ESP8266 | WekSocket address on ESP8266
    esp8266host = "ws://192.168.1.65:81/"

    # 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/event3')

    # 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 = .05

    aBtn = 289
    bBtn = 290
    xBtn = 288
    yBtn = 291
    lBtn = 292
    rBtn = 293
    selBtn = 296
    staBtn = 297

    class DummyClient(WebSocketClient):
    def opened(self):
    t1 = Thread(target=sendRequest)
    t1.daemon = True
    t1.start()
    print("Start Websocket thread")
    def closed(self, code, reason=None):
    print "Connexion closed down", code, reason

    def received_message(self, m):
    print m

    # Websocket request thread
    def sendRequest():
    global servo
    global direction
    global send

    #while not ws.closed:
    while True:
    # Un seul servo a la fois pour le moment | Only one servo in the version
    payload = servo+":"+direction
    if send:
    ws.send(payload)
    time.sleep(httpFreq)

    if __name__ == '__main__':
    try:
    ws = DummyClient(esp8266host, protocols=['http-only', 'chat'])
    ws.connect()
    print("Ready !")

    #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 BT")
    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
    except KeyboardInterrupt:
    ws.close()