-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyoCalibrationTest.cs
175 lines (139 loc) · 5.11 KB
/
MyoCalibrationTest.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
using UnityEngine;
using System.Collections;
public class MyoCalibration : MonoBehaviour
{
/* private const int numFvs = 8;
private const int numDataPointsToAverage = 70;//Any way to make this a public variable so we can change it in unity instead of in code?
private int[] FistAverages = new int[numFvs]; //This is just where I'm storing the average data for a gesture
private float[] StandardDeviations = new float[numFvs];
private int iteratorForRawData = 0;
private bool calibrationInProgress = false;
private bool calibrationCompleted = false;
private bool poseCheck = false;
//These arrays are where I'm storing the raw data
private int[][] rawData = new int[numFvs][]{new int[numDataPointsToAverage], new int[numDataPointsToAverage],
new int[numDataPointsToAverage], new int[numDataPointsToAverage],
new int[numDataPointsToAverage], new int[numDataPointsToAverage],
new int[numDataPointsToAverage], new int[numDataPointsToAverage]};
public GUIText[] displayTexts = new GUIText[numFvs];
public GUIText poseType;
public float errorFactor = 25;
// Use this for initialization
void Start()
{
MYO_SDK_RAW.InitializeMYO();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
MYO_SDK_RAW.RunRaw();
if (calibrationInProgress) Calibrate1Frame();
if (calibrationCompleted && !poseCheck)
{
if (!checkPoseTypeFist()) return;
poseCheck = true;
StartCoroutine(PrintPoseType());
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
//This starts the calibration process
calibrationInProgress = true;
calibrationCompleted = false;
}
}
void Calibrate1Frame()
{
for (int i = 0; i < numFvs; i++)
rawData[i][iteratorForRawData] = getFv(i);
++iteratorForRawData;
displayTexts[0].text = "" + iteratorForRawData;
if(iteratorForRawData >= numDataPointsToAverage)
{
calibrationCompleted = true;
calibrationInProgress = false;
iteratorForRawData = 0;
CalculateRawData();
}
}
void CalculateRawData()
{
for (int i = 0; i < numFvs; i++)
{
for (int j = 0; j < numDataPointsToAverage; j++)
FistAverages[i] += rawData[i][j];
FistAverages[i] /= numDataPointsToAverage;
StandardDeviations[i] = CalculateStandardDeviation(rawData[i], FistAverages[i]);
if (i == 0)
Debug.Log(i + ": Average: " + FistAverages[i] + " STDeviation: " + StandardDeviations[i]);
}
for (int i = 0; i < numDataPointsToAverage; i++)
Debug.Log("Data point " + i + " of oneRawData: " + rawData[0][i]);
PrintAverages();
}
private IEnumerator PrintPoseType()
{
poseType.text = "YOU'Z A FIST MOTHA FUCKAAAA";
yield return new WaitForSeconds(0.3f);
poseCheck = false;
}
private float CalculateStandardDeviation(int[] dataPoints, int average)
{
float sum = 0;
float tempDataPoint = 0;
for (int i = 0; i < numDataPointsToAverage; i++)
{
tempDataPoint = (dataPoints[i] - average);
sum = sum + (tempDataPoint * tempDataPoint) *errorFactor;
}
return Mathf.Sqrt(sum / average);
}
private void PrintAverages()
{
for (int i = 0; i < numFvs; i++)
{
displayTexts[i].text = FistAverages[i].ToString();
}
}
private bool checkPoseTypeFist()
{
int numError = 0;
for (int i = 0; i < numFvs; i++)
if (!checkOneFv(getFv(i), i))
++numError;
if (numError <= 2)
displayTexts[1].text = "num of errors :" + numError;
return (numError <= 2);
}
private bool checkOneFv(int fv, int fvIdx)
{
if ((fv >= FistAverages[fvIdx] - StandardDeviations[fvIdx])
&& (fv <= FistAverages[fvIdx] + StandardDeviations[fvIdx]))
return true;
else
{
poseType.text = "Failed at " + fvIdx + ": Raw: " + fv + " Average: "
+ FistAverages[fvIdx] + " STDev: " + StandardDeviations[fvIdx];
return false;
}
}
public int getFv(int fvIdx)
{
switch(fvIdx)
{
case 0: return MYO_SDK_RAW.Fv0();
case 1: return MYO_SDK_RAW.Fv1();
case 2: return MYO_SDK_RAW.Fv2();
case 3: return MYO_SDK_RAW.Fv3();
case 4: return MYO_SDK_RAW.Fv4();
case 5: return MYO_SDK_RAW.Fv5();
case 6: return MYO_SDK_RAW.Fv6();
case 7: return MYO_SDK_RAW.Fv7();
}
return -1;
}
*/
}