-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogManager.cs
346 lines (273 loc) · 11.8 KB
/
LogManager.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
using System.IO;
public class LogManager : MonoBehaviour
{
string FILE_NAME = "test";
string OBJ_FILE_NAME = "test";
private GameObject object31, object32;
public Vector3 playerPos;
public Vector3 playerRot;
private FirstPersonController fps;
private static bool created = false;
public GameObject[] targetsWithTag;
public GameObject target, spawn;
private GameObject targetObject;
public Vector3 playerStart;
public static float delayTime;
public Randomizev2 m_Randomizev2;
public float totalDistanceTraveled = 0f;
public float totalDegreesRotatedY = 0f;
public float totalDegreesRotatedW = 0f;
public float tdrAngle = 0f;
public float avgSpeed = 0f;
public float sumSpeed = 0f;
public int cpdIteration = 0;
public Vector3 lastPosition = Vector3.zero;
public Vector3 lastRotation, lastAngle, playerRotAngle;
// public Text targetText;
public static float radiusThreshold = 5;
public static string targetName;
public static string positionData = "";
public static string responseData = "";
public static bool newTrial = false;
public bool target_visible;
public static string ipAddress = "";
public string inputDomain = "128.200.148.93/OFT";
// Start is called before the first frame update
void Start()
{
// Call the CollectPositionData() Method Once every 1/10th of a second (10Hz)
InvokeRepeating("CollectPositionData", 0f, 0.1f);
m_Randomizev2 = GameObject.FindObjectOfType(typeof(Randomizev2)) as Randomizev2;
ipAddress = "http://" + inputDomain + "/OFTreceipt.php";
}
void Awake()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
Debug.Log("Awake: " + this.gameObject);
}
}
IEnumerator sendPosTexttoFile()
{
// Hard write to folder
StreamWriter sw = File.AppendText(FILE_NAME);
if (new FileInfo(FILE_NAME).Length == 0)
{
sw.WriteLine("pos_x, pos_z, rot_y, run_time, trial_time, target_obj, trial_level, delta_target, " +
"delta_start, speed, tot_dist, tot_rot_y, target_visible");
}
sw.WriteLine(positionData);
sw.Close();
// PHP write to folder
bool successful = true;
WWWForm form = new WWWForm();
form.AddField("input", positionData);
form.AddField("filename", ParticipantLog.file_name_pos);
WWW www = new WWW(ipAddress, form);
yield return www;
if (www.error != null)
{
successful = false;
}
else
{
Debug.Log(www.text);
successful = true;
}
}
IEnumerator sendObjTexttoFile()
{
// Hard write to folder
StreamWriter writedist = File.AppendText(OBJ_FILE_NAME);
if (new FileInfo(OBJ_FILE_NAME).Length == 0)
{
writedist.WriteLine("target_obj, trial_level, start_x, start_z, start_rot_y, end_x, end_z, end_rot_y, " +
"delta_start, delta_target, run_time, completion_time," +
"tot_dist, tot_rot_y, sl_dist, efficiency, avg_speed");
}
writedist.WriteLine(responseData);
writedist.Close();
// PHP write to folder
bool successful = true;
WWWForm form2 = new WWWForm();
form2.AddField("input2", responseData);
form2.AddField("filename2", ParticipantLog.file_name_obj);
WWW www2 = new WWW(ipAddress, form2);
yield return www2;
if (www2.error != null)
{
successful = false;
}
else
{
Debug.Log(www2.text);
successful = true;
}
}
void CollectPositionData()
{
// Every 10 Hz after start, grab Variables
// Distance from player to target object, and distance from player spawn to current player location
++cpdIteration;
avgSpeed = (fps.m_Speed + sumSpeed) / cpdIteration;
sumSpeed = fps.m_Speed + sumSpeed;
float deltaTarget = Vector3.Distance(target.transform.position, fps.transform.position);
float deltaStart = Vector3.Distance(GameObject.FindWithTag("spawn").transform.position, fps.transform.position);
if (GameObject.FindWithTag("target").name == "Bucket_clean")
{
target_visible = GameObject.Find("bucket_low").GetComponent<MeshRenderer>().enabled;
} else
{
target_visible = GameObject.FindWithTag("target").GetComponent<MeshRenderer>().enabled;
}
;
positionData += playerPos.x + "," + playerPos.z + "," +
playerRot.y + "," +
Time.time + "," + (Time.time - Randomizev2.startTime) + "," +
target.name + "," +
SceneManager.GetActiveScene().name + "," +
deltaTarget + "," + deltaStart + "," +
fps.m_Speed + "," + totalDistanceTraveled + "," +
tdrAngle + ", " + target_visible + "\n";
}
void CollectResponseData()
{
target = GameObject.FindWithTag("target");
spawn = GameObject.FindWithTag("spawn");
// Upon response event from participant, record data variables to #_responses:
float slDist = Vector3.Distance(target.transform.position, spawn.transform.position);
responseData = target.name + "," + SceneManager.GetActiveScene().name + "," +
spawn.transform.position.x + "," + spawn.transform.position.z + "," +
spawn.transform.eulerAngles.y + "," +
fps.transform.position.x + "," + fps.transform.position.z + "," +
fps.transform.eulerAngles.y + "," +
Vector3.Distance(spawn.transform.position, fps.transform.position) + "," +
Vector3.Distance(target.transform.position, fps.transform.position) + "," +
Time.time + "," + (Time.time - Randomizev2.startTime) + "," +
totalDistanceTraveled + "," + tdrAngle + "," +
slDist + "," + (totalDistanceTraveled / slDist) + "," + avgSpeed;
StartCoroutine(sendObjTexttoFile());
StartCoroutine(sendPosTexttoFile());
// Check to see if we are in testing phase (test trials), if so, set trial phase to 2 (uncollected data)
if(ParticipantLog.trialPhase != 3)
{
ParticipantLog.trialPhase = 2;
}
}
// Update is called once per frame
void Update()
{
fps = FindObjectOfType<FirstPersonController>();
// With each update, obtain necessary data
// Player position and player rotation
playerPos = fps.transform.position;
playerRot = fps.transform.eulerAngles;
playerRotAngle = fps.transform.forward;
// If new trial, assign last position and last rotation to current position and rotation
if (newTrial)
{
spawn = GameObject.FindWithTag("spawn");
lastPosition = spawn.transform.position;
lastAngle = spawn.transform.forward;
avgSpeed = 0f;
sumSpeed = 0f;
cpdIteration = 0;
positionData = "";
// targetText = GameObject.FindObjectOfType<Text>();
target = GameObject.FindWithTag("target");
targetName = target.name;
totalDistanceTraveled = 0;
tdrAngle = 0;
/* lastPosition = Vector3.zero;*/
sumSpeed = 0f;
avgSpeed = 0f;
cpdIteration = 0;
switch (targetName)
{
case "Bucket_clean":
targetName = "Bucket";
break;
case "cone_clean":
targetName = "Cone";
break;
case "chFolding.A_LOD0":
targetName = "Chair";
break;
}
// targetText.text = targetName;
// targetText.color = Color.black;
newTrial = false;
}
// Calculate total degrees rotated since last update, do same for distance.
// These will be used in CollectResponseData() and CollectPositionData() methods.
float angleTrav = Vector3.Angle(playerRotAngle, lastAngle);
tdrAngle += angleTrav;
float distTrav = Vector3.Distance(playerPos, lastPosition);
totalDistanceTraveled += distTrav;
// Update last position and rotation to current rotation for use within the next update
lastPosition = playerPos;
lastAngle = playerRotAngle;
target = GameObject.FindWithTag("target");
// Upon initial trial load, set file name to participant number entered on launch
if (FILE_NAME == "test" && OBJ_FILE_NAME == "test")
{
FILE_NAME = ParticipantLog.user + "_position.csv";
OBJ_FILE_NAME = ParticipantLog.user + "_objdistance.csv";
Debug.Log("FN: " + FILE_NAME);
}
if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("SL: " + ParticipantLog.trialPhase);
// Trial phase 2: Learn & Practice trials only. It means that the first phase has passed and the participant pressed the first Enter. This phase asks for confirmation before moving on.
if (ParticipantLog.trialPhase == 2)
{
m_Randomizev2.NextScene();
}
// Trial phase 1: Learn & Practice trials only. This is where OFT is waiting for the participant to respond with the first Enter press.
else if (ParticipantLog.trialPhase == 1)
{
targetObject = GameObject.FindWithTag("target");
float distanceToTO = Vector3.Distance(targetObject.transform.position, fps.transform.position);
if (targetObject.name == "Bucket_clean")
{
object31 = GameObject.Find("bucket_low");
object32 = GameObject.Find("rings_low");
object31.GetComponent<MeshRenderer>().enabled = true;
object32.GetComponent<MeshRenderer>().enabled = true;
}
else
{
MeshRenderer m = targetObject.GetComponent<MeshRenderer>();
m.enabled = true;
}
if (distanceToTO < radiusThreshold)
{
// targetText.text = "Good Job!";
TipboxManager.text_tip = "*** Good Job! Press ENTER to continue to the next trial! ***";
CollectResponseData();
}
else
{
// targetText.text = "Move closer to the center of the " + targetName + "!";
TipboxManager.text_tip = "*** Move closer to the center of the " + targetName + "! ***";
}
}
// Check to see if Test trial. Upon response, no feedback (reveal object).
// Immediately begin next test trial.
else if (ParticipantLog.trialPhase == 3)
{
CollectResponseData();
m_Randomizev2.NextScene();
}
}
}
}