Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·55 lines (43 loc) · 1.64 KB
/
Copy pathrun.py
File metadata and controls
executable file
·55 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import time
import board
from adafruit_motorkit import MotorKit
from pyPS4Controller.controller import Controller
import RPi.GPIO as GPIO
class MyController(Controller):
kit = MotorKit(i2c=board.I2C())
max_speed = 0.9
current_speed = 0
def __init__(self, **kwargs):
Controller.__init__(self, **kwargs)
GPIO.setup(17, GPIO.OUT)
def on_x_press(self):
self.kit.motor1.throttle = 0
self.kit.motor2.throttle = 0
self.kit.motor3.throttle = 0
GPIO.output(17, GPIO.LOW)
def on_circle_release(self):
self.kit.motor3.throttle = 0.8
GPIO.output(17, GPIO.HIGH)
def on_x_release(self):
self.kit.motor1.throttle = 0
def on_L3_up(self, value):
speed = self.max_speed * ((-1 * value) / 30000)
self.current_speed = speed
self.kit.motor1.throttle = speed
#print("Up: value: {}".format(speed))
def on_L3_down(self, value):
speed = -1 * (self.max_speed * (value / 30000))
self.current_speed = speed
self.kit.motor1.throttle = speed
def on_R3_up(self, value):
speed = self.max_speed * ((-1 * value) / 30000)
self.current_speed = speed
self.kit.motor2.throttle = speed
#print("Up: value: {}".format(speed))
def on_R3_down(self, value):
speed = -1 * (self.max_speed * (value / 30000))
self.current_speed = speed
self.kit.motor2.throttle = speed
controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
# you can start listening before controller is paired, as long as you pair it within the timeout window
controller.listen(timeout=60)