-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
231 lines (197 loc) · 5.98 KB
/
Main.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
226
227
228
229
230
231
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pyPS4Controller.controller import Controller
import threading
import time
from PCA9685 import PCA9685
class MyController(Controller):
def __init__(self, queue=None, **kwargs):
Controller.__init__(self, **kwargs)
self.queue = queue
self.left = 0
self.right = 0
self.forward = 0
self.backward = 0
self.close = 0
self.open = 0
self.up = 0
self.down = 0
self.rotateright = 0
self.rotateleft = 0
self.wristup = 0
self.wristdown = 0
def on_L3_up(self, value):
self.forward = value * -1 / 32767.0
self.backward = 0
def on_L3_down(self, value):
self.forward = 0
self.backward = value / 32767.0
def on_L3_left(self, value):
self.left = value * -1 / 32767.0
self.right = 0
def on_L3_right(self, value):
self.left = 0
self.right = value / 32767.0
def on_L3_x_at_rest(self):
self.left = 0
self.right = 0
def on_L3_y_at_rest(self):
self.forward = 0
self.backward = 0
def on_L2_press(self, value):
self.close = (value + 32768) / 65535.0
self.open = 0
def on_L2_release(self):
self.close = 0
def on_R2_press(self, value):
self.close = 0
self.open = (value + 32768) / 65535.0
def on_R2_release(self):
self.open = 0
def on_R3_up(self, value):
self.up = value * -1 / 32768.0
self.down = 0
def on_R3_down(self, value):
self.up = 0
self.down = value / 32767.0
def on_R3_y_at_rest(self):
self.up = 0
self.down = 0
def on_R3_left(self, value):
self.wristup = 0
self.wristdown = value / 32767.0
def on_R3_right(self, value):
self.wristup = value / 32767.0
self.wristdown = 0
def on_R3_x_at_rest(self):
self.wristup = 0
self.wristdown = 0
def on_right_arrow_press(self, value):
self.rotateright = (value + 32768) / 65535.0
self.rotateleft = 0
def on_right_arrow_release(self):
self.rotateright = 0
def on_left_arrow_press(self, value):
self.rotateright = 0
self.rotateleft = (value + 32768) / 65535.0
def on_left_arrow_release(self):
self.rotateleft = 0
class RoboArt(threading.Thread):
S3_CLOSED = 500
S3_OPEN = 2000
S0_IN = 600
S0_OUT = 2000
S2_LEFT = 500
S2_RIGHT = 2000
S1_HIGH = 3000
S1_LOW = 200
S4_ROTATERIGHT = 3000
S4_ROTATELEFT = 500
S5_WRISTUP = 3000
S5_WRISTDOWN = 500
def __init__(self, controller):
threading.Thread.__init__(self)
self.driver = PCA9685(0x40, debug=False)
self.driver.setPWMFreq(50)
self._lock = threading.Lock()
self.controller = controller
def check(
self,
s0,
s1,
s2,
s3,
s4,
s5,
):
if s3 > RoboArt.S3_OPEN:
s3 = RoboArt.S3_OPEN
if s3 < RoboArt.S3_CLOSED:
s3 = RoboArt.S3_CLOSED
if s2 > RoboArt.S2_RIGHT:
s2 = RoboArt.S2_RIGHT
if s2 < RoboArt.S2_LEFT:
s2 = RoboArt.S2_LEFT
if s0 > RoboArt.S0_OUT:
s0 = RoboArt.S0_OUT
if s0 < RoboArt.S0_IN:
s0 = RoboArt.S0_IN
if s1 > RoboArt.S1_HIGH:
s1 = RoboArt.S1_HIGH
if s1 < RoboArt.S1_LOW:
s1 = RoboArt.S1_LOW
if s4 > RoboArt.S4_ROTATERIGHT:
s4 = RoboArt.S4_ROTATERIGHT
if s4 < RoboArt.S4_ROTATELEFT:
s4 = RoboArt.S4_ROTATELEFT
if s5 > RoboArt.S5_WRISTUP:
s5 = RoboArt.S5_WRISTUP
if s5 < RoboArt.S5_WRISTDOWN:
s5 = RoboArt.S5_WRISTDOWN
return (
s0,
s1,
s2,
s3,
s4,
s5,
)
def run(self):
servo0 = 1000
servo1 = 1320
servo2 = 1000
servo3 = 1000
servo4 = 1000
servo5 = 1000
while True:
if controller.open > 0:
servo3 -= int(50 * controller.open)
elif controller.close:
servo3 += int(50 * controller.close)
if controller.left > 0:
servo2 += int(25 * controller.left)
elif controller.right > 0:
servo2 -= int(25 * controller.right)
if controller.up > 0:
servo1 -= int(25 * controller.up)
elif controller.down > 0:
servo1 += int(25 * controller.down)
if controller.forward > 0:
servo0 += int(25 * controller.forward)
elif controller.backward > 0:
servo0 -= int(25 * controller.backward)
if controller.wristup > 0:
servo5 -= int(25 * controller.wristup)
elif controller.wristdown > 0:
servo5 += int(25 * controller.wristdown)
if controller.rotateleft > 0:
servo4 += int(25 * controller.rotateleft)
elif conroller.rotateright > 0:
servo4 -= int(25 * controller.rotateright)
(
servo0,
servo1,
servo2,
servo3,
servo4,
servo5,
) = self.check(
servo0,
servo1,
servo2,
servo3,
servo4,
servo5,
)
self.driver.setServoPulse(0, servo0)
self.driver.setServoPulse(1, servo1)
self.driver.setServoPulse(2, servo2)
self.driver.setServoPulse(3, servo3)
self.driver.setServoPulse(4, servo4)
self.driver.setServoPulse(5, servo5)
time.sleep(0.025)
controller = MyController(interface='/dev/input/js0',
connecting_using_ds4drv=False)
arm = RoboArt(controller)
arm.start()
controller.listen(timeout=60)