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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. void Start()
  20. {
  21. movement = GetComponent<CozmoMovement>();
  22. movementWatch = new Stopwatch();
  23. rotationWatch = new Stopwatch();
  24. startPositionCozmo = movement.transform.position; // Cache the starting position of Cozmo
  25. lastY = movement.transform.rotation.y; // Cache the rotation Vector of Cozmo
  26. }
  27. private void FixedUpdate()
  28. {
  29. if (testState == TestStates.MovementSpeed)
  30. {
  31. TestMovementSpeed();
  32. }
  33. else if (testState == TestStates.RotationSpeed)
  34. {
  35. TestRotationSpeed();
  36. }
  37. }
  38. private void TestRotationSpeed()
  39. {
  40. float currentY = movement.transform.rotation.eulerAngles.y;
  41. // If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
  42. if (currentY >= lastY + ROTATION_ANGLE || currentY <= lastY - ROTATION_ANGLE)
  43. {
  44. if (rotationWatch.IsRunning)
  45. {
  46. rotationWatch.Stop();
  47. UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
  48. "\nElapsed time in milliseconds (Rotation): " + rotationWatch.ElapsedMilliseconds);
  49. }
  50. return;
  51. }
  52. else
  53. {
  54. // Start stopwatch to measure the time cozmo needs for a specific rotation
  55. if (!rotationWatch.IsRunning)
  56. {
  57. rotationWatch = Stopwatch.StartNew();
  58. }
  59. // Turn right
  60. movement.Turn(1);
  61. }
  62. }
  63. private void TestMovementSpeed()
  64. {
  65. // If distance between start and current position of cozmo reached the DISTANCE_TO_MEASURE_SPEED stop the stopwatch
  66. if (Vector3.Distance(startPositionCozmo, movement.transform.position) >= DISTANCE_TO_MEASURE_SPEED)
  67. {
  68. if (movementWatch.IsRunning)
  69. {
  70. movementWatch.Stop();
  71. UnityEngine.Debug.Log("Cozmo: " + gameObject.name +
  72. "/nElapsed time in milliseconds (Movement): " + movementWatch.ElapsedMilliseconds);
  73. }
  74. return;
  75. }
  76. else
  77. {
  78. // Start stopwatch to measure the time cozmo needs for a specific distance
  79. if (!movementWatch.IsRunning)
  80. {
  81. movementWatch = Stopwatch.StartNew();
  82. }
  83. // Move cozmo in forward direction
  84. movement.Move(1);
  85. }
  86. }
  87. }