/
Controller.py
225 lines (192 loc) · 7.26 KB
/
Controller.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import pygame
import XboxController
import time
from subprocess import call
from Hexapod import Hexapod
from ServoDriver import ServoDriver
from Display import Display
class Controller(object):
class Axis():
LS_X = 0
LS_Y = 1
LT = 4
RT = 5
class EventTypes():
ANALOG = 7
BUTTON = 11
class Button():
SELECT = 6
Y = 3
class Action():
STAND = 0
FORWARD = 1
BACKWARD = 2
STRAFE_LEFT = 3
STRAFE_RIGHT = 4
TURN_LEFT = 5
TURN_RIGHT = 6
UP = 7
DOWN = 8
INCREASE = 9
DECREASE = 10
class Mode():
STANDBY = 0
NORMAL = 1
CALIBRATION = 2
def moveForward(self, val):
if val < -0.3:
self.action[self.Action.FORWARD]=True
self.speed = 2 if abs(val)>0.9 else 1
elif val >= -0.3 and val <= 0.3:
self.action[self.Action.FORWARD]=False
self.speed = 0
else:
self.action[self.Action.FORWARD]=True
self.speed = -2 if abs(val)>0.9 else -1
def moveStrafe(self, val):
if val < -0.3:
self.action[self.Action.STRAFE_RIGHT]=True
self.speed = 2 if abs(val)>0.9 else 1
elif val >= -0.3 and val <= 0.3:
self.action[self.Action.STRAFE_RIGHT]=False
self.speed = 0
else:
self.action[self.Action.STRAFE_RIGHT]=True
self.speed = -2 if abs(val)>0.9 else -1
def moveRise(self, val):
if val > 0:
self.action[self.Action.UP]=True
else:
self.action[self.Action.UP]=False
def moveLower(self, val):
if val > 0:
self.action[self.Action.DOWN]=True
else:
self.action[self.Action.DOWN]=False
def calibrationMode(self):
print "Calibration mode on"
self.display.calibration(0)
self.mode = self.Mode.CALIBRATION
print self.mode
def normalMode(self):
print "Calibration mode off"
self.saveCalibrationData()
self.mode = self.Mode.NORMAL
def selectJoint(self):
if self.calibration == 17:
self.calibration = 0
else:
self.calibration += 1
print "Limb %d selected" % self.calibration
self.display.calibration(self.calibration)
def adjust(self, val):
if val < -0.3:
self.action[self.Action.INCREASE]=True
self.speed = 2 if abs(val)>0.9 else 1
elif val >= -0.3 and val <= 0.3:
self.action[self.Action.FORWARD]=False
self.speed = 0
else:
self.action[self.Action.DECREASE]=True
self.speed = -2 if abs(val)>0.9 else -1
def __init__(self):
self.state = 0
self.idle = 0
self.speed = 0
self.idle_threshold = 200
self.robot = Hexapod()
self.driver = ServoDriver()
self.robot.connectDriver(self.driver)
self.action = [True,False,False,False,False,False,False,False,False,False,False]
self.pad = XboxController.XboxController(None, deadzone = 30, scale = 100, invertYAxis = True) #only for the pygame init...
self.mode = self.Mode.NORMAL
self.calibration = 0;
self.loadCalibrationData()
self.display = Display()
print self.robot.legs[0].coxa.getCommand()
#Action mapping
self.command = {
self.Mode.NORMAL: {
self.EventTypes.ANALOG: {
self.Axis.LS_X: self.moveStrafe,
self.Axis.LS_Y: self.moveForward,
self.Axis.LT: self.moveLower,
self.Axis.RT: self.moveRise
},
self.EventTypes.BUTTON: {
self.Button.SELECT: self.calibrationMode
}
},
self.Mode.CALIBRATION: {
self.EventTypes.ANALOG: {
self.Axis.LS_Y: self.adjust,
},
self.EventTypes.BUTTON: {
self.Button.SELECT: self.normalMode,
self.Button.Y: self.selectJoint
}
}
}
def saveCalibrationData(self):
f = open("calib.dat", "w+")
for i in range(6):
f.write("%d\n" % self.robot.legs[i].coxa.calibration)
f.write("%d\n" % self.robot.legs[i].femur.calibration)
f.write("%d\n" % self.robot.legs[i].tibia.calibration)
f.close()
def speak(self, text):
call(["espeak", "-ven+m5", "-k6", "-s150", text])
def loadCalibrationData(self):
f = open("calib.dat", "r")
lines = f.read().splitlines()
print "Loading calibration data: "
for i in range(6):
print("Leg %d: [%s] [%s] [%s]" % (i,lines[i*3],lines[i*3+1],lines[i*3+2]))
self.robot.legs[i].coxa.calibration = int(lines[i*3])
self.robot.legs[i].femur.calibration = int(lines[i*3+1])
self.robot.legs[i].tibia.calibration = int(lines[i*3+2])
f.close()
def getInput(self):
for event in pygame.event.get():
print event
if event.type in self.command[self.mode]:
if event.type == self.EventTypes.ANALOG and event.axis in self.command[self.mode][event.type]:
self.command[self.mode][event.type][event.axis](event.value)
elif event.type == self.EventTypes.BUTTON and event.button in self.command[self.mode][event.type]:
self.command[self.mode][event.type][event.button]()
if sum(self.action) == 0:
self.action[self.Action.STAND] = True
def run(self):
print("Controller support is running")
while True:
self.getInput()
if self.mode == self.Mode.NORMAL:
if self.action[self.Action.FORWARD]:
self.state += self.speed
self.robot.moveForward(self.state)
elif self.action[self.Action.STRAFE_RIGHT]:
self.state += self.speed
self.robot.strafeRight(self.state)
elif self.action[self.Action.UP]:
self.robot.up()
elif self.action[self.Action.DOWN]:
self.robot.down()
elif self.action[self.Action.STAND]:
self.state = 0
self.robot.stand()
if self.mode == self.Mode.CALIBRATION:
if self.action[self.Action.INCREASE]:
c = 0
f = 0
t = 0
leg = int(self.calibration / 3)
if self.calibration % 3 == 0:
c = self.speed
elif self.calibration % 3 == 1:
f = self.speed
else:
t = self.speed
self.robot.calibrate(leg, c, f, t)
self.driver.executeCommand()
#pygame.event.clear()
time.sleep(0.020)