ViveSensorDataLogger.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Logging.Base;
  5. using Tracking;
  6. using UnityEngine;
  7. namespace Logging.Data
  8. {
  9. public readonly struct ViveSensorDataLog : ISerializable
  10. {
  11. private readonly long timestamp;
  12. private readonly float steerAngle;
  13. private readonly float frontWheelTrackerPositionX;
  14. private readonly float frontWheelTrackerPositionY;
  15. private readonly float frontWheelTrackerPositionZ;
  16. private readonly float frontWheelTrackerRotationX;
  17. private readonly float frontWheelTrackerRotationY;
  18. private readonly float frontWheelTrackerRotationZ;
  19. private readonly float kineticTrackerPositionX;
  20. private readonly float kineticTrackerPositionY;
  21. private readonly float kineticTrackerPositionZ;
  22. private readonly float kineticTrackerRotationX;
  23. private readonly float kineticTrackerRotationY;
  24. private readonly float kineticTrackerRotationZ;
  25. private readonly float hmdPositionX;
  26. private readonly float hmdPositionY;
  27. private readonly float hmdPositionZ;
  28. private readonly float hmdRotationX;
  29. private readonly float hmdRotationY;
  30. private readonly float hmdRotationZ;
  31. public ViveSensorDataLog(long timestamp, float steerAngle, float frontWheelTrackerPositionX,
  32. float frontWheelTrackerPositionY, float frontWheelTrackerPositionZ, float frontWheelTrackerRotationX,
  33. float frontWheelTrackerRotationY, float frontWheelTrackerRotationZ, float kineticTrackerPositionX,
  34. float kineticTrackerPositionY, float kineticTrackerPositionZ, float kineticTrackerRotationX,
  35. float kineticTrackerRotationY, float kineticTrackerRotationZ, float hmdPositionX, float hmdPositionY,
  36. float hmdPositionZ, float hmdRotationX, float hmdRotationY, float hmdRotationZ)
  37. {
  38. this.timestamp = timestamp;
  39. this.steerAngle = steerAngle;
  40. this.frontWheelTrackerPositionX = frontWheelTrackerPositionX;
  41. this.frontWheelTrackerPositionY = frontWheelTrackerPositionY;
  42. this.frontWheelTrackerPositionZ = frontWheelTrackerPositionZ;
  43. this.frontWheelTrackerRotationX = frontWheelTrackerRotationX;
  44. this.frontWheelTrackerRotationY = frontWheelTrackerRotationY;
  45. this.frontWheelTrackerRotationZ = frontWheelTrackerRotationZ;
  46. this.kineticTrackerPositionX = kineticTrackerPositionX;
  47. this.kineticTrackerPositionY = kineticTrackerPositionY;
  48. this.kineticTrackerPositionZ = kineticTrackerPositionZ;
  49. this.kineticTrackerRotationX = kineticTrackerRotationX;
  50. this.kineticTrackerRotationY = kineticTrackerRotationY;
  51. this.kineticTrackerRotationZ = kineticTrackerRotationZ;
  52. this.hmdPositionX = hmdPositionX;
  53. this.hmdPositionY = hmdPositionY;
  54. this.hmdPositionZ = hmdPositionZ;
  55. this.hmdRotationX = hmdRotationX;
  56. this.hmdRotationY = hmdRotationY;
  57. this.hmdRotationZ = hmdRotationZ;
  58. }
  59. public KeyValuePair<long, string[]> Serialize()
  60. {
  61. return new KeyValuePair<long, string[]>
  62. (timestamp,
  63. new[]
  64. {
  65. steerAngle.ToString("F6", CultureInfo.InvariantCulture),
  66. frontWheelTrackerPositionX.ToString("F6", CultureInfo.InvariantCulture),
  67. frontWheelTrackerPositionY.ToString("F6", CultureInfo.InvariantCulture),
  68. frontWheelTrackerPositionZ.ToString("F6", CultureInfo.InvariantCulture),
  69. frontWheelTrackerRotationX.ToString("F6", CultureInfo.InvariantCulture),
  70. frontWheelTrackerRotationY.ToString("F6", CultureInfo.InvariantCulture),
  71. frontWheelTrackerRotationZ.ToString("F6", CultureInfo.InvariantCulture),
  72. kineticTrackerPositionX.ToString("F6", CultureInfo.InvariantCulture),
  73. kineticTrackerPositionY.ToString("F6", CultureInfo.InvariantCulture),
  74. kineticTrackerPositionZ.ToString("F6", CultureInfo.InvariantCulture),
  75. kineticTrackerRotationX.ToString("F6", CultureInfo.InvariantCulture),
  76. kineticTrackerRotationY.ToString("F6", CultureInfo.InvariantCulture),
  77. kineticTrackerRotationZ.ToString("F6", CultureInfo.InvariantCulture),
  78. hmdPositionX.ToString("F6", CultureInfo.InvariantCulture),
  79. hmdPositionY.ToString("F6", CultureInfo.InvariantCulture),
  80. hmdPositionZ.ToString("F6", CultureInfo.InvariantCulture),
  81. hmdRotationX.ToString("F6", CultureInfo.InvariantCulture),
  82. hmdRotationY.ToString("F6", CultureInfo.InvariantCulture),
  83. hmdRotationZ.ToString("F6", CultureInfo.InvariantCulture)
  84. }
  85. );
  86. }
  87. }
  88. public class ViveSensorDataLogger : SensorDataLogger<ViveSensorDataLog>
  89. {
  90. public FrontWheelTracker fwt;
  91. public KineticLegTracker klt;
  92. public Transform hmd;
  93. public override string Key => "vive_sensor_data";
  94. private void Update()
  95. {
  96. var steerAngle = fwt.SteerRotation;
  97. var fwtPosition = fwt.RelativePosition;
  98. var fwtRotation = fwt.RelativeRotation;
  99. var kltPosition = klt.RelativePosition;
  100. var kltRotation = klt.RelativeRotation;
  101. var hmdPosition = hmd.position;
  102. var hmdRotation = hmd.rotation.eulerAngles;
  103. //TODO: data always there?
  104. Log(new ViveSensorDataLog(Helpers.RoundToLong(Time.time * 1000), steerAngle,
  105. fwtPosition.x, fwtPosition.y, fwtPosition.z,
  106. fwtRotation.x, fwtRotation.y, fwtRotation.z,
  107. kltPosition.x, kltPosition.y, kltPosition.z,
  108. kltRotation.x, kltRotation.y, kltRotation.z,
  109. hmdPosition.x, hmdPosition.y, hmdPosition.z,
  110. hmdRotation.x, hmdRotation.y, hmdRotation.z));
  111. }
  112. public override IEnumerable<ViveSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. }
  117. }