TestManagement.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Hecto\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. }
  47. Instance = this;
  48. // Handle the sequence of the mode to test
  49. if(SceneManager.GetActiveScene().name == "Simulation"){
  50. initModeList();
  51. ModeList[TestMode-1].SetActive(true);
  52. }
  53. loader = gameObject.GetComponent<SteamVR_LoadLevel>();
  54. StartCoroutine(Time());
  55. targetList = GameObject.FindGameObjectsWithTag("target");
  56. currentTestRobot = GameObject.FindGameObjectWithTag("robot").GetComponent<TestRobot>();
  57. totalTarget = targetList.Length;
  58. }
  59. // Caculate the sequence of the modes based on Latin Square (size = 4)
  60. private void initModeList()
  61. {
  62. // if the ID is illegal, the default Sequence will be used
  63. // all the date will be saved in 0.csv as a test date
  64. bool illegalID = false;
  65. if(TestID <=0){
  66. TestID = 1;
  67. illegalID = true;
  68. }
  69. var modes = ModeList;
  70. ModeList = new GameObject[4];
  71. for(int i =0;i<modes.Length;i++){
  72. ModeList[i] = modes[(TestID-1+i)%4];
  73. }
  74. if(illegalID){
  75. TestID = 0;
  76. }
  77. }
  78. // Update is called once per frame
  79. void Update()
  80. {
  81. if(currentTestRobot != null){
  82. triggerCount = currentTestRobot.triggerCount;
  83. totalDriveDistance = currentTestRobot.totalDriveDistance;
  84. totalDriveTime = currentTestRobot.totalDriveTime;
  85. averageSpeed = currentTestRobot.averageSpeed;
  86. }
  87. // caculate the target info
  88. visibleTarget = 0;
  89. foreach(GameObject target in targetList){
  90. if(target!=null && target.layer != LayerMask.NameToLayer("Invisible")){
  91. visibleTarget++;
  92. }
  93. }
  94. unvisibleTarget = totalTarget - rescuedTarget - visibleTarget;
  95. }
  96. public void loadSceneAsync(string name)
  97. {
  98. GameObject.Find("Input").GetComponent<VRInput>().CurrentMode.SetActive(false);
  99. Destroy(Player.instance.gameObject);
  100. // delete targets and robot
  101. foreach(Transform child in transform){
  102. Destroy(child.gameObject);
  103. }
  104. // change scene
  105. loader.levelName = name;
  106. loader.Trigger();
  107. }
  108. IEnumerator Time()
  109. {
  110. while (TotalTime > 0)
  111. {
  112. yield return new WaitForSeconds(1);
  113. // all the people are rescued, end test
  114. if(totalTarget == rescuedTarget){
  115. break;
  116. }
  117. TotalTime--;
  118. }
  119. endTest();
  120. }
  121. // rescue the selected person
  122. public void rescue(GameObject target){
  123. if( target.layer != LayerMask.NameToLayer("Invisible")){
  124. GameObject.Destroy(target);
  125. rescuedTarget ++;
  126. InteractionManagement.Instance.SetPlayerText("Rescued People: "+ (rescuedTarget-1) + " => " + rescuedTarget,5,false);
  127. }
  128. }
  129. public void endTest(){
  130. var testinfo ="";
  131. testinfo += "End Test: " + SceneManager.GetActiveScene().name + "\n";
  132. testinfo += "Remained Time: " + TotalTime + "\n";
  133. // Robot
  134. testinfo += "Collision:" + triggerCount + "\n";
  135. testinfo += "Drive Distance: " + totalDriveDistance + "\n";
  136. testinfo += "Total driving time:" + totalDriveTime + "\n";
  137. testinfo += "Adverage speed: " + averageSpeed + "\n";
  138. // Target
  139. testinfo += "Rescued Target:" + rescuedTarget + "/" + totalTarget + "\n";
  140. testinfo += "Remained Visible Target: " + visibleTarget + "\n";
  141. testinfo += "Remained Unvisible Target: " + unvisibleTarget + "\n";
  142. Debug.Log(testinfo);
  143. writeCSV();
  144. InteractionManagement.Instance.SetMenu(true);
  145. InteractionManagement.Instance.SetPlayerText("The test has ended. Please wait.",5,false);
  146. // next text
  147. TestMode++;
  148. }
  149. private void writeCSV(){
  150. string strFilePath = csvPath + TestID +".csv";
  151. string strSeperator = ",";
  152. string line = "";
  153. if(!File.Exists(strFilePath)){
  154. line += "participant" + strSeperator;
  155. line += "condition" + strSeperator;
  156. line += "Remained Time" + strSeperator;
  157. line += "Collision" + strSeperator;
  158. line += "Drive Distance" + strSeperator;
  159. line += "Total driving time" + strSeperator;
  160. line += "Adverage speed" + strSeperator;
  161. line += "Rescued Target" + strSeperator;
  162. line += "Remained Visible Target" + strSeperator;
  163. line += "Remained Unvisible Target" + strSeperator;
  164. line += "time" + strSeperator;
  165. line += "\n";
  166. // Create and write the csv file
  167. File.WriteAllText(strFilePath, line);
  168. line = "";
  169. }
  170. line += TestID + strSeperator;
  171. line += SceneManager.GetActiveScene().name + strSeperator;
  172. line += TotalTime + strSeperator;
  173. line += triggerCount + strSeperator;
  174. line += totalDriveDistance + strSeperator;
  175. line += totalDriveTime + strSeperator;
  176. line += averageSpeed + strSeperator;
  177. line += rescuedTarget + strSeperator;
  178. line += visibleTarget + strSeperator;
  179. line += unvisibleTarget + strSeperator;
  180. line += System.DateTime.Now.ToString(("yyyy/MM/dd HH:mm"))+ strSeperator;
  181. line += "\n";
  182. // To append line to the csv file
  183. File.AppendAllText(strFilePath, line);
  184. }
  185. }