-
Notifications
You must be signed in to change notification settings - Fork 1
/
rc.py
executable file
·96 lines (80 loc) · 3.82 KB
/
rc.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Import triangula module to interact with SixAxis
from triangula.input import SixAxis, SixAxisResource
import drivetrain
import time
import sounds
class rc:
def __init__(self, drive, sounds):
"""Class Constructor"""
self.killed = False
self.drive = drive
self.sounds = sounds
def stop(self):
"""Simple method to stop the RC loop"""
self.killed = True
def run(self):
"""Start listening to the controller and pass state to drivetrain"""
# Get a joystick, this will fail unless the SixAxis controller is
# paired and active The bind_defaults argument specifies that we
# should bind actions to the SELECT and START buttons to
# centre the controller and reset the calibration respectively.
self.sounds.Play("start.wav")
while not self.killed:
try:
with SixAxisResource(bind_defaults=True) as joystick:
# Loop indefinitely
while not self.killed:
# Get button and joystick axis state
buttons_pressed = joystick.get_and_clear_button_press_history()
# Read the x and y axes of the left hand stick
lx = joystick.axes[0].corrected_value()
ly = joystick.axes[1].corrected_value()
# Read the x and y axes of the right hand stick
rx = joystick.axes[2].corrected_value()
ry = joystick.axes[3].corrected_value()
# DPad Buttons
if buttons_pressed & 1 << SixAxis.BUTTON_D_DOWN:
print('DPad Down pressed since last check')
self.sounds.Play("pew.wav")
# DPad Buttons
if buttons_pressed & 1 << SixAxis.BUTTON_D_UP:
print('DPad Up pressed since last check')
self.sounds.Play("start.wav")
# DPad Buttons
if buttons_pressed & 1 << SixAxis.BUTTON_D_LEFT:
print('DPad Left pressed since last check')
# DPad Buttons
if buttons_pressed & 1 << SixAxis.BUTTON_D_RIGHT:
print('DPad Right pressed since last check')
# Square Button
if buttons_pressed & 1 << SixAxis.BUTTON_SQUARE:
print('SQUARE pressed since last check')
# Triangle Button
if buttons_pressed & 1 << SixAxis.BUTTON_TRIANGLE:
print('Enable Motors')
self.drive.enable_motors(True)
# Cross Button
if buttons_pressed & 1 << SixAxis.BUTTON_CROSS:
print('Disable Motors')
self.drive.enable_motors(False)
# Show the values to the screen
if self.drive is not None:
self.drive.mix_channels_and_send(lx, ly, rx, ry)
# Sleep a small amount between loop iterations
time.sleep(0.05)
except (IOError):
print("No PS3 Controller")
# Sleep for half a second to give user time to connect a PS3 Controller
time.sleep(0.5)
if __name__ == "__main__":
sound = sounds.Sounds()
drive = drivetrain.DriveTrain()
rc = rc(drive, sound)
try:
rc.run()
except (KeyboardInterrupt) as e:
# except (Exception, KeyboardInterrupt) as e:
# Stop any active threads before leaving
rc.stop()
drive.stop()
print("Quitting")