ViveSensorDataLogger.cs 6.1 KB

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