PolarReceiver.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Plotting;
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7. [System.Serializable]
  8. public struct PolarSensorConfig
  9. {
  10. public int port;
  11. public String ipAddress;
  12. public bool plotAcc;
  13. public bool plotEcg;
  14. public int accSampleRate; //TODO: let user choose between 25, 50, 100, 200
  15. public PolarSensorConfig(int port = 9099, string ipAddress = "0.0.0.0", bool plotAcc = false, bool plotEcg = false,
  16. int accSampleRate = 25)
  17. {
  18. this.port = port;
  19. this.ipAddress = ipAddress;
  20. this.plotAcc = plotAcc;
  21. this.plotEcg = plotEcg;
  22. this.accSampleRate = accSampleRate;
  23. }
  24. }
  25. public struct PolarSensorData
  26. {
  27. public Vector3 Acc;
  28. public float EcgValue;
  29. }
  30. public class PolarReceiver
  31. {
  32. private UdpConnection connection;
  33. private PolarSensorData sensorData;
  34. public PolarSensorConfig SensorConfig { get; private set; }
  35. public PolarSensorData SensorData => sensorData;
  36. private PlotFileWriter ecgPlotFile;
  37. private PlotFileWriter accPlotFile;
  38. private static DateTime startTime;
  39. private static long previousTime;
  40. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  41. private static void InitTime()
  42. {
  43. startTime = DateTime.Now;
  44. }
  45. public PolarReceiver(PolarSensorConfig config)
  46. {
  47. SensorConfig = config;
  48. }
  49. public async Task StartListening()
  50. {
  51. if (SensorConfig.plotAcc)
  52. {
  53. accPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/acc.tsv");
  54. await accPlotFile.WriteDataLine("timestamp", new[] {"x", "y", "z"});
  55. }
  56. if (SensorConfig.plotEcg)
  57. {
  58. ecgPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/ecg.tsv");
  59. await accPlotFile.WriteDataLine("timestamp", new[] {"voltage"});
  60. }
  61. if (SensorConfig.plotAcc || SensorConfig.plotEcg)
  62. {
  63. DebugPlot.Instance.ShowPlots();
  64. }
  65. connection = new UdpConnection(SensorConfig.ipAddress, SensorConfig.port, OnAccData, OnEcgData);
  66. connection.Listen();
  67. }
  68. public void Dispose()
  69. {
  70. connection.StopListening();
  71. DebugPlot.DestroyInstance();
  72. }
  73. private async void OnAccData(AccData data)
  74. {
  75. var now = DateTime.Now;
  76. Debug.Log($"#ACC DATA items: {data.Values.Count}");
  77. Debug.Log($"ACC DATA s since last: {data.Timestamp - previousTime} /1000000000f");
  78. previousTime = data.Timestamp;
  79. await Task.WhenAll(UpdateSensorDataForAcc(data), PlotAcc(data));
  80. }
  81. private async Task PlotAcc(AccData data)
  82. {
  83. if (SensorConfig.plotAcc)
  84. {
  85. var timestamp =
  86. (int) (DateTime.Now - startTime)
  87. .TotalMilliseconds; //not perfect because of delay. But that's the time the game get's the data
  88. //var timestamp = data.Timestamp;
  89. foreach (var item in data.Values)
  90. {
  91. await accPlotFile.WriteDataLine(timestamp, new[] {item.x, item.y, item.z});
  92. timestamp += 1000 / SensorConfig.accSampleRate;
  93. }
  94. }
  95. }
  96. private async Task UpdateSensorDataForAcc(AccData data)
  97. {
  98. foreach (var item in data.Values)
  99. {
  100. sensorData.Acc = data.Values[0];
  101. await Task.Delay(1000 / SensorConfig.accSampleRate);
  102. }
  103. }
  104. private async void OnEcgData(EcgData data)
  105. {
  106. if (SensorConfig.plotEcg)
  107. {
  108. //var timestamp = data.Timestamp; // if we wanted to use the timestamp of the polar sensor directly
  109. var timestamp =
  110. (int) (DateTime.Now - startTime)
  111. .TotalMilliseconds; //not perfect because of delay. But that's the time the game get's the data
  112. foreach (var item in data.Values)
  113. {
  114. await ecgPlotFile.WriteDataLine(timestamp, new[] {item});
  115. timestamp += 1000 / 130;
  116. }
  117. }
  118. sensorData.EcgValue = data.Values[0]; //TODO
  119. }
  120. }