TestManagement.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR.InteractionSystem;
  5. using UnityEngine.SceneManagement;
  6. using Valve.VR;
  7. using System.Text;
  8. using System.IO;
  9. public class TestManagement : MonoBehaviour
  10. {
  11. public static TestManagement Instance { get; private set; }
  12. public int TestID = 1;
  13. public string csvPath = @"C:\Users\alley\Desktop\Thesis-Hector-VR\User Study\TestResult";
  14. public int TestMode = 1;
  15. public GameObject[] ModeList;
  16. // ------- Test Information-------------
  17. public float TotalTime = 300; // default test time
  18. // robot info
  19. [Tooltip("Number of collisions detected")]
  20. public int triggerCount = 0;
  21. [Tooltip("Total driving distance. (meter)")]
  22. public float totalDriveDistance = 0;
  23. [Tooltip("Total driving time. (second)")]
  24. public float totalDriveTime = 0;
  25. [Tooltip("Adverage speed. (m/s)")]
  26. public float averageSpeed = 0;
  27. // target info
  28. public int totalTarget = 0;
  29. public int rescuedTarget = 0;
  30. [Tooltip("visible target but not rescued")]
  31. public int visibleTarget = 0;
  32. [Tooltip("Not detected target")]
  33. public int unvisibleTarget = 0;
  34. // --------------
  35. [HideInInspector]
  36. public GameObject[] targetList;
  37. TestRobot currentTestRobot;
  38. SteamVR_LoadLevel loader;
  39. // Start is called before the first frame update
  40. void Start()
  41. {
  42. // synchronization
  43. if(Instance!=null){
  44. TestID = Instance.TestID;
  45. TestMode = Instance.TestMode;
  46. csvPath = Instance.csvPath;
  47. }
  48. Instance = this;
  49. // Handle the sequence of the mode to test in simulation
  50. if(SceneManager.GetActiveScene().name == "Simulation"){
  51. initModeList();
  52. ModeList[TestMode-1].SetActive(true);
  53. }
  54. loader = gameObject.GetComponent<SteamVR_LoadLevel>();
  55. StartCoroutine(Time());
  56. targetList = GameObject.FindGameObjectsWithTag("target");
  57. currentTestRobot = GameObject.FindGameObjectWithTag("robot").GetComponent<TestRobot>();
  58. totalTarget = targetList.Length;
  59. }
  60. // Caculate the sequence of the modes based on Latin Square (size = 4)
  61. private void initModeList()
  62. {
  63. // if the ID is illegal, the default Sequence will be used
  64. // all the date will be saved in 0.csv as a test date
  65. bool illegalID = false;
  66. if(TestID <=0){
  67. TestID = 1;
  68. illegalID = true;
  69. }
  70. var modes = ModeList;
  71. ModeList = new GameObject[4];
  72. for(int i =0;i<modes.Length;i++){
  73. ModeList[i] = modes[(TestID-1+i)%4];
  74. }
  75. if(illegalID){
  76. TestID = 0;
  77. }
  78. }
  79. // Update is called once per frame
  80. void Update()
  81. {
  82. if(currentTestRobot != null){
  83. triggerCount = currentTestRobot.triggerCount;
  84. totalDriveDistance = currentTestRobot.totalDriveDistance;
  85. totalDriveTime = currentTestRobot.totalDriveTime;
  86. averageSpeed = currentTestRobot.averageSpeed;
  87. }
  88. // caculate the target info
  89. visibleTarget = 0;
  90. foreach(GameObject target in targetList){
  91. if(target!=null && target.layer != LayerMask.NameToLayer("Invisible")){
  92. visibleTarget++;
  93. }
  94. }
  95. unvisibleTarget = totalTarget - rescuedTarget - visibleTarget;
  96. }
  97. public void loadSceneAsync(string name)
  98. {
  99. GameObject.Find("Input").GetComponent<VRInput>().CurrentMode.SetActive(false);
  100. Destroy(Player.instance.gameObject);
  101. // delete targets and robot
  102. foreach(Transform child in transform){
  103. Destroy(child.gameObject);
  104. }
  105. // change scene
  106. loader.levelName = name;
  107. loader.Trigger();
  108. }
  109. IEnumerator Time()
  110. {
  111. while (TotalTime > 0)
  112. {
  113. yield return new WaitForSeconds(1);
  114. // all the people are rescued, end test
  115. if(totalTarget == rescuedTarget){
  116. break;
  117. }
  118. TotalTime--;
  119. }
  120. endTest();
  121. }
  122. // rescue the selected person
  123. public void rescue(GameObject target){
  124. if( target.layer != LayerMask.NameToLayer("Invisible")){
  125. GameObject.Destroy(target);
  126. rescuedTarget ++;
  127. InteractionManagement.Instance.SetPlayerText("Rescued People: "+ (rescuedTarget-1) + " => " + rescuedTarget,5,false);
  128. }
  129. }
  130. public void endTest(){
  131. var testinfo ="";
  132. testinfo += "End Test: " + SceneManager.GetActiveScene().name + "\n";
  133. testinfo += "Remained Time: " + TotalTime + "\n";
  134. // Robot
  135. testinfo += "Collision:" + triggerCount + "\n";
  136. testinfo += "Drive Distance: " + totalDriveDistance + "\n";
  137. testinfo += "Total driving time:" + totalDriveTime + "\n";
  138. testinfo += "Adverage speed: " + averageSpeed + "\n";
  139. // Target
  140. testinfo += "Rescued Target:" + rescuedTarget + "/" + totalTarget + "\n";
  141. testinfo += "Remained Visible Target: " + visibleTarget + "\n";
  142. testinfo += "Remained Unvisible Target: " + unvisibleTarget + "\n";
  143. Debug.Log(testinfo);
  144. writeCSV();
  145. InteractionManagement.Instance.SetMenu(true);
  146. InteractionManagement.Instance.SetPlayerText("The test has ended. Please wait.",5,false);
  147. // next text
  148. TestMode++;
  149. }
  150. private void writeCSV(){
  151. string strFilePath = csvPath + @"\" + TestID +".csv";
  152. string strSeperator = ",";
  153. string line = "";
  154. if(!File.Exists(strFilePath)){
  155. line += "participant" + strSeperator;
  156. line += "condition" + strSeperator;
  157. line += "Remained Time" + strSeperator;
  158. line += "Collision" + strSeperator;
  159. line += "Drive Distance" + strSeperator;
  160. line += "Total driving time" + strSeperator;
  161. line += "Adverage speed" + strSeperator;
  162. line += "Rescued Target" + strSeperator;
  163. line += "Remained Visible Target" + strSeperator;
  164. line += "Remained Unvisible Target" + strSeperator;
  165. line += "time" + strSeperator;
  166. line += "\n";
  167. // Create and write the csv file
  168. File.WriteAllText(strFilePath, line);
  169. line = "";
  170. }
  171. line += TestID + strSeperator;
  172. line += SceneManager.GetActiveScene().name + strSeperator;
  173. line += TotalTime + strSeperator;
  174. line += triggerCount + strSeperator;
  175. line += totalDriveDistance + strSeperator;
  176. line += totalDriveTime + strSeperator;
  177. line += averageSpeed + strSeperator;
  178. line += rescuedTarget + strSeperator;
  179. line += visibleTarget + strSeperator;
  180. line += unvisibleTarget + strSeperator;
  181. line += System.DateTime.Now.ToString(("yyyy/MM/dd HH:mm"))+ strSeperator;
  182. line += "\n";
  183. // To append line to the csv file
  184. File.AppendAllText(strFilePath, line);
  185. }
  186. }