-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_PS3.py
124 lines (99 loc) · 3.85 KB
/
script_PS3.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
#!/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.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
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)*200)) #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)*200))
#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)*200))
#print "Right track forward: ", int(value*(1)*100),"%" #Uncomment this line to see visual feedback
elif (value) < 0:
robot.RT_backward(int(value*(-1)*200))
#print "Right track backward: ", int(value*(-1)*100),"%" #Uncomment this line to see visual feedback
xboxCont.setupControlCallback(
xboxCont.XboxControls.RTHUMBY,
RightThumbYCallBack)
#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 "Xbox controller detected, 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