This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgaze.py
155 lines (116 loc) · 3.73 KB
/
gaze.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
import time
import numpy
from reachy import Leachy
leachy = Leachy()
screen_size = (520, 320) # in mm
joints = { # in degrees
'pyramide_rouge': [-28.33, 20.1, 36.97, -64.22, -34.16, 10.7, -20.0],
'prisme_jaune': [-49.69, 18.31, 17.27, -35.47, -53.52, -0.15, -7.89],
'hexa_jaune': [4.7, -2.35, 41.19, -98.51, -7.18, 22.43, -19.21],
'pyramide_vert': [-21.9, 2.2, 12.79, -71.08, -45.6, 13.34, -20.38] ,
'cube_rouge': [-33.43, -6.6, 15.6, -55.43, -61.14, -11.58, -6.6],
'boule_jaune': [3.85, -0.68, 24.84, -85.67, -56.16, -23.02, -20.67],
'cylindre_bleu': [-44.07, 6.3, -33.36, -44.18, -22.43, -3.08, -21.26],
'chapeau_vert':[4.8, 15.9, -15.69, -94.55, -34.75, -1.32, -17.74] ,
'rest': [16.6, 13.6, 17.1, -101.93, -19.5, -0.15, -16.28],
'center': [-19.2, 9.3, 2.24, -93.23, -28.3, 14.52, -7.48],
}
inter = {
'pyramide_rouge': True,
'prisme_jaune': True,
'hexa_jaune': False,
'pyramide_vert': True,
'cube_rouge': True,
'boule_jaune': False,
'cylindre_bleu': True,
'chapeau_vert': False,
}
pos_2d = { # in mm (origin top left)
'pyramide_rouge': (35, 145),
'prisme_jaune': (145, 65),
'hexa_jaune': (110, 260),
'pyramide_vert': (270, 160),
'cube_rouge': (350, 70),
'boule_jaune': (300, 250),
'cylindre_bleu': (470, 135),
'chapeau_vert': (465, 275),
}
time_traj = {
'pyramide_rouge': 1,
'prisme_jaune': 1,
'hexa_jaune': 0.75,
'pyramide_vert': 1,
'cube_rouge': 1,
'boule_jaune': 0.75,
'cylindre_bleu': 1,
'chapeau_vert': 0.75,
}
def find_nearest_object(x, y):
dist = {
corner: (xc - x) ** 2 + (yc - y) ** 2
for (corner, (xc, yc)) in pos_2d.items()
}
return min(dist, key=dist.get)
def goto_object(obj, t, wait=True):
print('Goto obj:', obj)
q = joints[obj]
for m, p in zip(leachy.motors, q):
m.goto_position(p, t, control='linear')
if wait:
time.sleep(t)
def goto_2d(x2, y2, t):
x2 = min(max(x2, 0), 1)
y2 = min(max(y2, 0), 1)
x2 *= screen_size[0]
y2 *= screen_size[1]
obj = find_nearest_object(x2, y2)
goto(obj)
def goto_rest(t):
for m, p in zip(leachy.motors, joints['center']):
m.goto_position(p, t, control='linear')
time.sleep(t)
def goto(obj):
t = time_traj[obj]
if inter[obj]:
goto_object('center', t, wait=True)
#time.sleep(0.75 * t)
goto_object(obj, t)
time.sleep(t)
if inter[obj]:
goto_object('center', t, wait=True)
#time.sleep(0.75 * t)
goto_object('rest', t)
import myo
class Listener(myo.DeviceListener):
def on_connect(self, device, timestamp, version):
print('Connected!')
device.set_stream_emg(myo.StreamEmg.enabled)
for _ in range(3):
device.vibrate('short')
time.sleep(0.5)
def on_emg_data(self, device, timestamp, emg):
self.emg = abs(numpy.array(emg))
if __name__ == '__main__':
""" Example: python pointing_objects.py snes cow cat """
import sys
for m in leachy.motors:
m.compliant = False
m.moving_speed = 0
goto_rest(2)
myo.init('../../myo-sdk-win-0.9.0/bin')
listener = Listener()
listener.emg = numpy.array([numpy.nan])
hub = myo.Hub()
hub.run(50, listener)
from gazepoint import GazePoint
tracker = GazePoint()
try:
while True:
input('press enter')
if True:
#if listener.emg.mean() > 10:
x, y = tracker.gaze_position
goto_2d(x, y, 1)
except KeyboardInterrupt:
tracker.stop()
hub.shutdown()