ViveSensorDataLogger.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public ViveSensorDataLog(float timestamp, float steerAngle, float frontWheelTrackerPositionX,
  26. float frontWheelTrackerPositionY, float frontWheelTrackerPositionZ, float frontWheelTrackerRotationX,
  27. float frontWheelTrackerRotationY, float frontWheelTrackerRotationZ, float kineticTrackerPositionX,
  28. float kineticTrackerPositionY, float kineticTrackerPositionZ, float kineticTrackerRotationX,
  29. float kineticTrackerRotationY, float kineticTrackerRotationZ)
  30. {
  31. this.timestamp = timestamp;
  32. this.steerAngle = steerAngle;
  33. this.frontWheelTrackerPositionX = frontWheelTrackerPositionX;
  34. this.frontWheelTrackerPositionY = frontWheelTrackerPositionY;
  35. this.frontWheelTrackerPositionZ = frontWheelTrackerPositionZ;
  36. this.frontWheelTrackerRotationX = frontWheelTrackerRotationX;
  37. this.frontWheelTrackerRotationY = frontWheelTrackerRotationY;
  38. this.frontWheelTrackerRotationZ = frontWheelTrackerRotationZ;
  39. this.kineticTrackerPositionX = kineticTrackerPositionX;
  40. this.kineticTrackerPositionY = kineticTrackerPositionY;
  41. this.kineticTrackerPositionZ = kineticTrackerPositionZ;
  42. this.kineticTrackerRotationX = kineticTrackerRotationX;
  43. this.kineticTrackerRotationY = kineticTrackerRotationY;
  44. this.kineticTrackerRotationZ = kineticTrackerRotationZ;
  45. }
  46. public string[] Serialize() => new[]
  47. {
  48. timestamp.ToString("F6", CultureInfo.InvariantCulture),
  49. steerAngle.ToString("F6", CultureInfo.InvariantCulture),
  50. frontWheelTrackerPositionX.ToString("F6", CultureInfo.InvariantCulture),
  51. frontWheelTrackerPositionY.ToString("F6", CultureInfo.InvariantCulture),
  52. frontWheelTrackerPositionZ.ToString("F6", CultureInfo.InvariantCulture),
  53. frontWheelTrackerRotationX.ToString("F6", CultureInfo.InvariantCulture),
  54. frontWheelTrackerRotationY.ToString("F6", CultureInfo.InvariantCulture),
  55. frontWheelTrackerRotationZ.ToString("F6", CultureInfo.InvariantCulture),
  56. kineticTrackerPositionX.ToString("F6", CultureInfo.InvariantCulture),
  57. kineticTrackerPositionY.ToString("F6", CultureInfo.InvariantCulture),
  58. kineticTrackerPositionZ.ToString("F6", CultureInfo.InvariantCulture),
  59. kineticTrackerRotationX.ToString("F6", CultureInfo.InvariantCulture),
  60. kineticTrackerRotationY.ToString("F6", CultureInfo.InvariantCulture),
  61. kineticTrackerRotationZ.ToString("F6", CultureInfo.InvariantCulture),
  62. };
  63. }
  64. public class ViveSensorDataLogger : SensorDataLogger<ViveSensorDataLog>
  65. {
  66. public FrontWheelTracker fwt;
  67. public KineticLegTracker klt;
  68. public override string Key => "vive_sensor_data";
  69. private void Update()
  70. {
  71. var steerAngle = fwt.SteerRotation;
  72. var fwtPosition = fwt.RelativePosition;
  73. var fwtRotation = fwt.RelativeRotation;
  74. var kltPosition = klt.RelativePosition;
  75. var kltRotation = klt.RelativeRotation;
  76. //TODO: data always there?
  77. Log(new ViveSensorDataLog(Time.time, steerAngle,
  78. fwtPosition.x, fwtPosition.y, fwtPosition.z,
  79. fwtRotation.x, fwtRotation.y, fwtRotation.z,
  80. kltPosition.x, kltPosition.y, kltPosition.z,
  81. kltRotation.x, kltRotation.y, kltRotation.z));
  82. }
  83. public override void Log(ViveSensorDataLog value)
  84. {
  85. Buffer.Add(value.Serialize());
  86. }
  87. public override IEnumerable<ViveSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  88. {
  89. throw new System.NotImplementedException();
  90. }
  91. }
  92. }