-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brain.cs
314 lines (286 loc) · 8.31 KB
/
Brain.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using UnityEngine;
using System.Collections;
using LockingPolicy = Thalmic.Myo.LockingPolicy;
using Pose = Thalmic.Myo.Pose;
using UnlockType = Thalmic.Myo.UnlockType;
using VibrationType = Thalmic.Myo.VibrationType;
public class Brain : MonoBehaviour
{
public GameObject myo = null;
private Animation animation;
private int _side;
public GameObject hand;
private bool _fist_stop;
private bool _point_stop;
private bool _fist_out_stop;
private bool _point_out_stop;
private bool _cylindrical_stop;
private string _key;
private string _last_key;
// The pose from the last update. This is used to determine if the pose has changed
// so that actions are only performed upon making them rather than every frame during
// which they are active.
private Pose _lastPose = Pose.Unknown;
void Start()
{
animation = GetComponent<Animation>();
}
void Update()
{
// Access the ThalmicMyo component attached to the Myo game object.
ThalmicMyo thalmicMyo = myo.GetComponent<ThalmicMyo>();
// Check if the pose has changed since last update.
// The ThalmicMyo component of a Myo game object has a pose property that is set to the
// currently detected pose (e.g. Pose.Fist for the user making a fist). If no pose is currently
// detected, pose will be set to Pose.Rest. If pose detection is unavailable, e.g. because Myo
// is not on a user's arm, pose will be set to Pose.Unknown.
// Next two if loops checks which key is pressed for each frame. It takes Input.inputString to see which key
// If no key is pressed then it will contain an empty string. We check for this.
// This is necessary since the way EPOC is implemented is by EmoKey. A key is being pressed once every 0.2sec. This is captured.
//Writes the first input from keyboard to _key
if (string.IsNullOrEmpty(_key)) {
_key = Input.inputString;
}
// if the new input is not empty then it must be a new key
if (!string.IsNullOrEmpty(Input.inputString)) {
_key = Input.inputString;
}
Debug.Log (_key);
if (thalmicMyo.pose != _lastPose)
{
_lastPose = thalmicMyo.pose;
// Vibrate the Myo armband when a fist is made.
if (thalmicMyo.pose == Pose.Fist)
{
if(_key == "u") {
_side = 3;
}
// First test of "key active" for EEG headset. This works.
if(_key == "t"){
_side = 10;
}
if(_key == "y"){
_side = 7;
}
}
else if (thalmicMyo.pose == Pose.WaveIn)
{
if(_key == "u") {
_side = 5;
}
// First test of "key active" for EEG headset. This works.
if(_key == "t"){
_side = 1;
}
if(_key == "y"){
_side = 9;
}
}
else if (thalmicMyo.pose == Pose.WaveOut)
{
if(_key == "u") {
_side = 6;
}
// First test of "key active" for EEG headset. This works.
if(_key == "t"){
_side = 2;
}
if(_key == "y"){
_side = 8;
}
}
else if (thalmicMyo.pose == Pose.DoubleTap)
{
//_side = 4;
//ExtendUnlockAndNotifyUserAction(thalmicMyo);
}
else if (thalmicMyo.pose == Pose.FingersSpread)
{
_side = 4;
}
else if (thalmicMyo.pose == Pose.Rest)
{
_side = 0;
}
}
// The following constaints the animation until a specific time (frame)
// This stops the animation from completing or going into a wrong playtime-frame
// Fist
if (animation["Fist"].time > 0.65f) {
animation["Fist"].time = 0.65f;
}
else if (animation["Fist"].time < 0.1f) {
animation["Fist"].time = 0.1f;
}
// Cylindrical
else if (animation["Cylindrical"].time > 0.7f) {
animation["Cylindrical"].time = 0.7f;
}
else if (animation["Cylindrical"].time < 0.1f) {
animation["Cylindrical"].time = 0.1f;
}
// Rotate Left
else if (animation["RotateLeft"].time > 0.65f) {
animation["RotateLeft"].time = 0.65f;
}
else if (animation["RotateLeft"].time < 0.1f) {
animation["RotateLeft"].time = 0.1f;
// Rotate Right
}
else if (animation["RotateRight"].time > 0.65f) {
animation["RotateRight"].time = 0.65f;
}
else if (animation["RotateRight"].time < 0.1f) {
animation["RotateRight"].time = 0.1f;
// 2-finger pinch
}
else if (animation["TwoPinch"].time > 0.7f) {
animation["TwoPinch"].time = 0.7f;
}
else if (animation["TwoPinch"].time < 0.1f) {
animation["TwoPinch"].time = 0.1f;
// 1-finger pinch
}
else if (animation["Pinch"].time > 0.7f) {
animation["Pinch"].time = 0.7f;
}
else if (animation["Pinch"].time < 0.1f) {
animation["Pinch"].time = 0.1f;
// Wave in
}
else if (animation["WaveIn"].time > 0.7f) {
animation["WaveIn"].time = 0.7f;
}
else if (animation["WaveIn"].time < 0.1f) {
animation["WaveIn"].time = 0.1f;
// Wave out
}
else if (animation["WaveOut"].time > 0.7f) {
animation["WaveOut"].time = 0.7f;
}
else if (animation["WaveOut"].time < 0.1f) {
animation["WaveOut"].time = 0.1f;
// Point with index finger
}
else if (animation["Point"].time < 0.1f) {
animation["Point"].time = 0.1f;
}
else if (animation ["Point"].time > 0.70f) {
animation["Point"].time = 0.70f;
}
// The following plays the animations based on the input.
// CrossFade is used in order to blend between animations
// Speed can be adjusted to play faster or slower. Higher number = faster
// _side == 4 is the one going from any grip to idle
if (_side == 3)
{
// _fist_out_stop = false;
animation["Fist"].speed = 0.6f;
animation.CrossFade ("Fist");
Debug.Log (animation["Fist"].time + " - FIST IN TIME");
//box.transform.Translate(Vector3.left * Time.deltaTime);
}
else if (_side == 4)
{
_fist_stop = false;
animation["Fist"].speed = -0.6f;
animation.CrossFade("Fist");
Debug.Log (animation["Fist"].time + " - POSE OUT TIME");
}
else if (_side == 1)
{
_point_out_stop = false;
animation["RotateLeft"].speed = 0.6f;
animation.CrossFade("RotateLeft");
Debug.Log (animation["RotateLeft"].time + " - ROTATE LEFT TIME");
}
else if (_side == 2)
{
animation["RotateRight"].speed = 0.6f;
animation.CrossFade("RotateRight");
Debug.Log (animation["RotateRight"].time + " - ROTATE RIGHT TIME");
}
else if (_side == 5)
{
animation["WaveIn"].speed = 0.6f;
animation.CrossFade("WaveIn");
Debug.Log (animation["WaveIn"].time + " - WaveIn TIME");
}
else if (_side == 6)
{
animation["WaveOut"].speed = 0.6f;
animation.CrossFade("WaveOut");
Debug.Log (animation["WaveOut"].time + " - WAVE OUT TIME");
}
else if (_side == 7)
{
animation["Point"].speed = 0.6f;
animation.CrossFade("Point");
Debug.Log (animation["Point"].time + " - POINT TIME");
}
else if (_side == 8)
{
animation["TwoPinch"].speed = 0.6f;
animation.CrossFade("TwoPinch");
Debug.Log (animation["TwoPinch"].time + " - TWO PINCH TIME");
}
else if (_side == 9)
{
animation["Pinch"].speed = 0.6f;
animation.CrossFade("Pinch");
Debug.Log (animation["Pinch"].time + " - PINCH TIME");
}
else if (_side == 10)
{
animation["Cylindrical"].speed = 0.6f;
animation.CrossFade("Cylindrical");
Debug.Log (animation["Cylindrical"].time + " - CYLINDRICAL TIME");
}
else {
animation["Fist"].speed = 0.0f;
animation["Point"].speed = 0.0f;
animation["WaveIn"].speed = 0.0f;
animation["WaveOut"].speed = 0.0f;
animation["RotateLeft"].speed = 0.0f;
animation["RotateRight"].speed = 0.0f;
animation["TwoPinch"].speed = 0.0f;
animation["Pinch"].speed = 0.0f;
animation["Cylindrical"].speed = 0.0f;
}
}
void OnGUI () {
GUI.skin.label.fontSize = 30;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.color = Color.red;
if (_key == "t") {
GUI.Label(new Rect (12, 250, Screen.width, Screen.height),
"PUSH"
);
}
else if (_key == "y") {
GUI.Label(new Rect (12, 250, Screen.width, Screen.height),
"LIFT"
);
}
else if (_key == "u") {
GUI.Label(new Rect (12, 250, Screen.width, Screen.height),
"ROTATE"
);
}
else {
GUI.Label(new Rect (12, 250, Screen.width, Screen.height),
"NO INPUT YET"
);
}
}
// Extend the unlock if ThalmcHub's locking policy is standard, and notifies the given myo that a user action was
// recognized.
void ExtendUnlockAndNotifyUserAction (ThalmicMyo myo)
{
ThalmicHub hub = ThalmicHub.instance;
if (hub.lockingPolicy == LockingPolicy.Standard) {
myo.Unlock (UnlockType.Timed);
}
myo.NotifyUserAction ();
}
}