Last active
August 28, 2019 15:46
-
-
Save energee/7fc4187fbfa857a4ee7b15c61773624f to your computer and use it in GitHub Desktop.
User xbox controller to command openpilot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from evdev import InputDevice | |
from select import select | |
import time | |
import numpy as np | |
import zmq | |
from cereal import car | |
import selfdrive.messaging as messaging | |
from selfdrive.services import service_list | |
from common.realtime import Ratekeeper | |
from common.fingerprints import fingerprint | |
if __name__ == "__main__": | |
# ***** connect to joystick ***** | |
# Xbox One Controller | |
dev = InputDevice("/dev/input/event7") | |
print dev | |
button_values = [0]*13 | |
axis_values = [0]*7 | |
# ***** connect to car ***** | |
context = zmq.Context() | |
logcan = messaging.sub_sock(context, service_list['can'].port) | |
sendcan = messaging.pub_sock(context, service_list['sendcan'].port) | |
CP = fingerprint(logcan) | |
exec('from selfdrive.car.'+CP.carName+'.interface import CarInterface') | |
CI = CarInterface(CP, logcan, sendcan) | |
rk = Ratekeeper(100) | |
while 1: | |
# **** handle joystick **** | |
r, w, x = select([dev], [], [], 0.0) | |
if dev in r: | |
for event in dev.read(): | |
print event | |
## Button events | |
if event.type == 1: | |
btn = event.code - 303 | |
if btn >= 1 and btn < 13: | |
button_values[btn] = int(event.value) | |
# # Axis move events | |
if event.type == 3: | |
## Left Joystick | |
if event.code == 0: | |
axis_values[event.code] = np.clip(-event.value/32768.0, -1.0, 1.0) | |
elif event.code == 1: | |
axis_values[event.code] = np.clip(-event.value/32768.0, -1.0, 1.0) | |
## Left and Right Triggers | |
elif event.code in [2,5]: | |
if event.code == 2: | |
event.value = -event.value | |
axis_values[2] = np.clip(event.value/1023.0, -1.0, 1.0) | |
## Right Joystick | |
elif event.code == 3: | |
axis_values[event.code] = np.clip(-event.value/32768.0, -1.0, 1.0) | |
elif event.code == 4: | |
axis_values[event.code] = np.clip(-event.value/32768.0, -1.0, 1.0) | |
## D-Pad (left) | |
elif event.code == 16: | |
print event.value | |
axis_values[5] = -int(event.value) | |
elif event.code == 17: | |
axis_values[6] = -int(event.value) | |
print axis_values, button_values | |
# **** handle car **** | |
CS = CI.update() | |
print CS | |
CC = car.CarControl.new_message() | |
CC.enabled = True | |
CC.gas = float(np.clip(-axis_values[0], 0, 1.0)) | |
CC.brake = float(np.clip(axis_values[0], 0, 1.0)) | |
CC.steeringTorque = float(axis_values[2]) | |
CC.hudControl.speedVisible = bool(button_values[1]) | |
CC.hudControl.lanesVisible = bool(button_values[2]) | |
CC.hudControl.leadVisible = bool(button_values[3]) | |
CC.cruiseControl.override = bool(button_values[0]) | |
CC.cruiseControl.cancel = bool(button_values[-1]) | |
CC.hudControl.setSpeed = float(axis_values[2] * 100.0) | |
# TODO: test alerts | |
CC.hudControl.visualAlert = "none" | |
CC.hudControl.audibleAlert = "none" | |
print CC | |
if not CI.apply(CC): | |
print "CONTROLS FAILED" | |
rk.keep_time() |
how/where does this run? any doc on how to get this working?
Comma made a better one: https://github.com/commaai/openpilot-tools
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What are all those libraries? Also AMAZING work.