Files
dragonpilot/selfdrive/debug/test_carcontroller.py

93 lines
2.3 KiB
Python
Raw Normal View History

2017-05-11 12:41:17 -07:00
#!/usr/bin/env python
import time
import numpy as np
import zmq
2017-07-28 01:24:39 -07:00
from evdev import InputDevice
from select import select
2017-05-11 12:41:17 -07:00
from cereal import car
2017-07-28 01:24:39 -07:00
from common.realtime import Ratekeeper
2017-05-11 12:41:17 -07:00
import selfdrive.messaging as messaging
from selfdrive.services import service_list
2017-07-28 01:24:39 -07:00
from selfdrive.car import get_car
2017-05-11 12:41:17 -07:00
if __name__ == "__main__":
# ***** connect to joystick *****
# we use a Mad Catz V.1
dev = InputDevice("/dev/input/event8")
print dev
button_values = [0]*7
axis_values = [0.0, 0.0, 0.0]
# ***** connect to car *****
context = zmq.Context()
logcan = messaging.sub_sock(context, service_list['can'].port)
sendcan = messaging.pub_sock(context, service_list['sendcan'].port)
2017-07-28 01:24:39 -07:00
CI, CP = get_car(logcan, sendcan)
2017-09-30 03:07:27 -07:00
CC = car.CarControl.new_message()
2017-05-11 12:41:17 -07:00
rk = Ratekeeper(100)
while 1:
# **** handle joystick ****
r, w, x = select([dev], [], [], 0.0)
if dev in r:
for event in dev.read():
# button event
if event.type == 1:
btn = event.code - 288
if btn >= 0 and btn < 7:
button_values[btn] = int(event.value)
# axis move event
if event.type == 3:
if event.code < 3:
if event.code == 2:
axis_values[event.code] = np.clip((255-int(event.value))/250.0, 0.0, 1.0)
else:
DEADZONE = 5
if event.value-DEADZONE < 128 and event.value+DEADZONE > 128:
event.value = 128
axis_values[event.code] = np.clip((int(event.value)-128)/120.0, -1.0, 1.0)
print axis_values, button_values
# **** handle car ****
2017-09-30 03:07:27 -07:00
CS = CI.update(CC)
2017-06-28 13:57:09 -07:00
#print CS
2017-05-11 12:41:17 -07:00
CC = car.CarControl.new_message()
2017-09-30 03:07:27 -07:00
2017-05-11 12:41:17 -07:00
CC.enabled = True
CC.gas = float(np.clip(-axis_values[1], 0, 1.0))
CC.brake = float(np.clip(axis_values[1], 0, 1.0))
2017-06-28 13:57:09 -07:00
CC.steeringTorque = float(-axis_values[0])
2017-05-11 12:41:17 -07:00
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"
2017-06-28 13:57:09 -07:00
#print CC
2017-05-11 12:41:17 -07:00
if not CI.apply(CC):
print "CONTROLS FAILED"
rk.keep_time()