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

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