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 4.6KB

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