-
Notifications
You must be signed in to change notification settings - Fork 0
/
tankbot_XBOX_1.1.py
179 lines (150 loc) · 5.14 KB
/
tankbot_XBOX_1.1.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
#Tank movement script version 1.1
#By Matias Raisanen
#Works with Xbox controller.
#Left analog stick to move left track, right analog for right.
#Before running this script, you should type "sudo xboxdrv --silent --detach-kernel-driver &" into console
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import sys
import time
import Robot
import XboxController
xboxCont = XboxController.XboxController(
controllerCallBack = None,
joystickNo = 0,
deadzone = 0.05,
#deadzone = 0.14, #Deadzone of 0.14 eliminates the accidental tiny nudges on the controller
scale = 1,
invertYAxis = True) #Y-axis must be inverted in order for the thumbsticks to work intuitively (forward=positive, back=negative)
LEFT_TRIM = 0
RIGHT_TRIM = 0
speed = 255 #This determines the speed multiplier of the DC motors. Use values 0-255
robot = Robot.Robot(left_trim=LEFT_TRIM, right_trim=RIGHT_TRIM)
#Next we define the controls:
#If the value of the thumbstick is zero or greater, move forward
#ElseIf the value is negative, move backward.
#int() converts the value into a non-decimal number to be used by the motor to determine speed
#Left Stick
def LeftThumbYCallBack(value):
if (value) >= 0:
robot.LT_forward(int(value*(1)*speed)) #200 determines the speed of the motor (0-255)
#print "Left track forward: ", int(value*(1)*100),"%" #Uncomment this line to see visual feedback
elif (value) < 0:
robot.LT_backward(int(value*(-1)*speed))
#print "Left track backward: ", int(value*(-1)*100),"%" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.LTHUMBY,
LeftThumbYCallBack)
#Right Stick
def RightThumbYCallBack(value):
if (value) >= 0:
robot.RT_forward(int(value*(1)*speed))
#print "Right track forward: ", int(value*(1)*100),"%" #Uncomment this line to see visual feedback
elif (value) < 0:
robot.RT_backward(int(value*(-1)*speed))
#print "Right track backward: ", int(value*(-1)*100),"%" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.RTHUMBY,
RightThumbYCallBack)
#Use Dpad to turn (the camera)
#Camera movement not yet implemented
def DpadCallBack(value):
x = 100 #X-axis turning speed multiplier, use values 0-255
if (value) == (-1, 0): #Dpad left
#print "Pan left"
robot.RT_forward(x)
robot.LT_backward(x)
#print ""
elif (value) == (1, 0): #Dpad right
#print "Pan right"
robot.LT_forward(x)
robot.RT_backward(x)
#print ""
elif (value) == (0, 1): #Dpad up
#print "Camera up!"
print ""
elif (value) == (0, -1):
#print "Camera down!" #Dpad down
print ""
elif (value) == (1, 1): #Dpad up right
#print "Camera up!"
#print "Pan right!"
robot.LT_forward(x)
robot.RT_backward(x)
#print ""
elif (value) == (1, -1): #Dpad down right
#print "Camera down!"
#print "Pan right!"
robot.LT_forward(x)
robot.RT_backward(x)
#print ""
elif (value) == (-1, 1): #Dpad up left
#print "Camera up!"
#print "Pan left!"
robot.RT_forward(x)
robot.LT_backward(x)
#print ""
elif (value) == (-1, -1): #Dpad down left
#print "Camera down!"
#print "Pan left!"
robot.RT_forward(x)
robot.LT_backward(x)
#print ""
elif (value) == (0, 0):
robot.stop()
xboxCont.setupControlCallback(
xboxCont.XboxControls.DPAD,
DpadCallBack)
#Press Y to move forward at full speed (255)
def YButtonCallBack(value):
if (value) > 0:
robot.forward(255)
#print "FULL STEAM AHEAD!" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.Y,
YButtonCallBack)
#Press B to stop motors
def StopButtonCallBack(value):
if (value) > 0:
robot.stop()
#print "Stop" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.B,
StopButtonCallBack)
#Press Select to stop motors
def BackButtonCallBack(value):
if (value) > 0:
robot.stop()
#print "Stop" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.BACK,
BackButtonCallBack)
#Next part runs the actual program:
try:
#start the controller
xboxCont.start()
#Cool visuals :-)
print '\033[92m' + " " #Green color
print "*****************TANK CONTROL V 1.1*******************"
print "* Use analog sticks for left and right track control *"
print "* Press B to stop the tank *"
print "* Press Y to go full steam ahead *"
print "* CTRL + C to quit *"
print "******************************************************"
print '\033[0m' + " " #Default color
print "Ready for input!"
while True:
time.sleep(1)
#Ctrl C
except KeyboardInterrupt:
print '\033[91m' + "\nEXIT via CTRL + C" + '\033[0m' #Print in RED, then return to Defaults
#error
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
#stop the controller
xboxCont.stop()
#Sources:
#http://www.stuffaboutcode.com/2014/10/raspberry-pi-xbox-360-controller-python.html
#https://learn.adafruit.com/adafruit-dc-and-stepper-motor-hat-for-raspberry-pi/overview