ViveSensorDataLogger.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Tracking;
  6. using UnityEngine;
  7. namespace Logging
  8. {
  9. public readonly struct ViveSensorDataLog
  10. {
  11. private readonly float 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(float 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 string[] Serialize() => new[]
  60. {
  61. timestamp.ToString("F6", CultureInfo.InvariantCulture),
  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. public class ViveSensorDataLogger : SensorDataLogger<ViveSensorDataLog>
  84. {
  85. public FrontWheelTracker fwt;
  86. public KineticLegTracker klt;
  87. public Transform hmd;
  88. public override string Key => "vive_sensor_data";
  89. private void Update()
  90. {
  91. var steerAngle = fwt.SteerRotation;
  92. var fwtPosition = fwt.RelativePosition;
  93. var fwtRotation = fwt.RelativeRotation;
  94. var kltPosition = klt.RelativePosition;
  95. var kltRotation = klt.RelativeRotation;
  96. var hmdPosition = hmd.position;
  97. var hmdRotation = hmd.rotation.eulerAngles;
  98. //TODO: data always there?
  99. Log(new ViveSensorDataLog(Time.time, steerAngle,
  100. fwtPosition.x, fwtPosition.y, fwtPosition.z,
  101. fwtRotation.x, fwtRotation.y, fwtRotation.z,
  102. kltPosition.x, kltPosition.y, kltPosition.z,
  103. kltRotation.x, kltRotation.y, kltRotation.z,
  104. hmdPosition.x, hmdPosition.y, hmdPosition.z,
  105. hmdRotation.x, hmdRotation.y, hmdRotation.z));
  106. }
  107. public override void Log(ViveSensorDataLog value)
  108. {
  109. Buffer.Add(value.Serialize());
  110. }
  111. public override IEnumerable<ViveSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  112. {
  113. throw new System.NotImplementedException();
  114. }
  115. }
  116. }