forked from Coretec-Robotics/Tiny_4wd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyPirate.py
129 lines (97 loc) · 3.55 KB
/
TinyPirate.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want
from inputs import get_gamepad
from explorerhat import motor
def mixer(inYaw, inThrottle,):
left = inThrottle + inYaw
right = inThrottle - inYaw
scaleLeft = abs(left / 125.0)
scaleRight = abs(right / 125.0)
scaleMax = max(scaleLeft, scaleRight)
scaleMax = max(1, scaleMax)
out_left = int(constrain(left / scaleMax, -125, 125))
out_right = int(constrain(right / scaleMax, -125, 125))
results = [out_right, out_left]
return results
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))
# Setup
maxPower = 1.0
power_left = 0.0
power_right = 0.0
x_axis = 0.0
y_axis = 0.0
try:
print('Press CTRL+C to quit')
# Loop indefinitely
while True:
events = get_gamepad()
for event in events:
print(event.code, event.state)
if event.code == "ABS_Y":
if event.state > 130:
print("Backwards")
elif event.state < 125:
print("Forward")
y_axis = event.state
if y_axis > 130:
y_axis = -(y_axis - 130)
elif y_axis < 125:
y_axis = ((-y_axis) + 125)
else:
y_axis = 0.0
print("Y: " + str(-y_axis))
if event.code == "ABS_Z":
if event.state > 130:
print("Right")
elif event.state < 125:
print("Left")
x_axis = event.state
if x_axis > 130:
x_axis = (x_axis - 130)
elif x_axis < 125:
x_axis = -((-x_axis) + 125)
else:
x_axis = 0.0
print("X: " + str(x_axis))
if event.code == "BTN_TL":
if event.state == True:
print("Botton Left")
if event.code == "BTN_TR":
if event.state == True:
print("Botton Right")
if event.code == "BTN_Z":
if event.state == True:
print("Top right")
if event.code == "BTN_WEST":
if event.state == True:
print("Top left")
if event.code == "BTN_TL2":
if event.state == True:
print("Select")
x_axis = 0
y_axis = 0
if event.code == "ABS_HAT0X":
if event.state == -1:
print("D pad Left")
elif event.state == 1:
print("D pad Right")
if event.code == "ABS_HAT0Y":
if event.state == -1:
print("D pad Up")
elif event.state == 1:
print("D pad Down")
mixer_results = mixer(x_axis, y_axis)
#print (mixer_results)
power_left = int((mixer_results[0] / 125.0)*100)
power_right = int((mixer_results[1] / 125.0)*100)
print("left: " + str(power_left) + " right: " + str(power_right))
motor.one.speed((-power_right * maxPower))
motor.two.speed(power_left * maxPower)
# print(event.ev_type, event.code, event.state)
except KeyboardInterrupt:
# CTRL+C exit, disable all drives
print("stop")
motor.stop()
print("bye")