You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CozmoMovementTester.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using UnityEngine;
  6. public enum TestStates { MovementSpeed, RotationSpeed } // enum to determine test scenarios
  7. [RequireComponent(typeof(CozmoMovement))]
  8. public class CozmoMovementTester : MonoBehaviour
  9. {
  10. private const float DISTANCE_TO_MEASURE_SPEED = 1f; // Distance that is used to calculate cozmos movementspeed (in m)
  11. private const float ROTATION_ANGLE = 90f; // Angle that is used to calculate cozmos rotationspeed
  12. private CozmoMovement movement; // Reference to the movement script of cozmo
  13. private Vector3 startPositionCozmo; // Original position of cozmo
  14. private float lastY; // Last Y of cozmo
  15. private Stopwatch movementWatch; // Stopwatch to measure the time for the movement usecase
  16. private Stopwatch rotationWatch; // Stopwatch to measure the time for the rotation usecase
  17. [Tooltip("Choose which speed of the virtual cozmo should be tested.")]
  18. public TestStates testState = TestStates.MovementSpeed;
  19. [Tooltip("Start and stop testing")]
  20. public bool isTesting = false;
  21. void Start()
  22. {
  23. movement = GetComponent<CozmoMovement>();
  24. movementWatch = new Stopwatch();
  25. rotationWatch = new Stopwatch();
  26. startPositionCozmo = movement.transform.position; // Cache the starting position of Cozmo
  27. lastY = movement.transform.rotation.y; // Cache the rotation Vector of Cozmo
  28. }
  29. private void FixedUpdate()
  30. {
  31. if (isTesting)
  32. {
  33. if (testState == TestStates.MovementSpeed)
  34. {
  35. TestMovementSpeed();
  36. }
  37. else if (testState == TestStates.RotationSpeed)
  38. {
  39. TestRotationSpeed();
  40. }
  41. }
  42. }
  43. private void TestRotationSpeed()
  44. {
  45. float currentY = movement.transform.rotation.eulerAngles.y;
  46. // If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
  47. if (currentY >= lastY + ROTATION_ANGLE || currentY <= lastY - ROTATION_ANGLE)
  48. {
  49. if (rotationWatch.IsRunning)
  50. {
  51. rotationWatch.Stop();
  52. UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
  53. "\nElapsed time in milliseconds (Rotation): " + rotationWatch.ElapsedMilliseconds);
  54. }
  55. isTesting = false;
  56. return;
  57. }
  58. else
  59. {
  60. // Start stopwatch to measure the time cozmo needs for a specific rotation
  61. if (!rotationWatch.IsRunning)
  62. {
  63. rotationWatch = Stopwatch.StartNew();
  64. }
  65. // Turn right
  66. movement.Turn(1);
  67. }
  68. }
  69. private void TestMovementSpeed()
  70. {
  71. // If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
  72. if (Vector3.Distance(startPositionCozmo, movement.transform.position) >= DISTANCE_TO_MEASURE_SPEED)
  73. {
  74. if (movementWatch.IsRunning)
  75. {
  76. movementWatch.Stop();
  77. UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
  78. "/nElapsed time in milliseconds (Movement): " + movementWatch.ElapsedMilliseconds);
  79. }
  80. isTesting = false;
  81. return;
  82. }
  83. else
  84. {
  85. // Start stopwatch to measure the time cozmo needs for a specific distance
  86. if (!movementWatch.IsRunning)
  87. {
  88. movementWatch = Stopwatch.StartNew();
  89. }
  90. // Move cozmo in forward direction
  91. movement.Move(1);
  92. }
  93. }
  94. }