PolarReceiver.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 struct TimeSync
  31. {
  32. public DateTime StartTime;
  33. public long DifStartAccStart;
  34. public long DifStartEcgStart;
  35. }
  36. public class PolarReceiver
  37. {
  38. private UdpConnection connection;
  39. private PolarSensorData sensorData;
  40. public PolarSensorConfig SensorConfig { get; private set; }
  41. public PolarSensorData SensorData => sensorData;
  42. private PlotFileWriter ecgPlotFile;
  43. private PlotFileWriter accPlotFile;
  44. private static long startTimeAcc = -1;
  45. private static long startTimeEcg = -1;
  46. private static TimeSync timeSync;
  47. private const int ECG_SAMPLE_RATE = 130;
  48. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  49. private static void InitTime()
  50. {
  51. timeSync = new TimeSync {StartTime = DateTime.Now, DifStartAccStart = -1, DifStartEcgStart = -1};
  52. }
  53. public PolarReceiver(PolarSensorConfig config)
  54. {
  55. SensorConfig = config;
  56. }
  57. public async Task StartListening()
  58. {
  59. if (SensorConfig.plotAcc)
  60. {
  61. accPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/acc.tsv");
  62. await accPlotFile.WriteDataLine("timestamp", new[] {"x", "y", "z"});
  63. }
  64. if (SensorConfig.plotEcg)
  65. {
  66. ecgPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/ecg.tsv");
  67. await ecgPlotFile.WriteDataLine("timestamp", new[] {"voltage"});
  68. }
  69. if (SensorConfig.plotAcc || SensorConfig.plotEcg)
  70. {
  71. DebugPlot.Instance.ShowPlots();
  72. }
  73. connection = new UdpConnection(SensorConfig.ipAddress, SensorConfig.port, OnAccData, OnEcgData);
  74. connection.Listen();
  75. }
  76. public void Dispose()
  77. {
  78. connection.StopListening();
  79. DebugPlot.DestroyInstance();
  80. }
  81. private async void OnAccData(AccData data)
  82. {
  83. if (timeSync.DifStartAccStart < 0)
  84. {
  85. timeSync.DifStartAccStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
  86. }
  87. Debug.Log($"#ACC DATA items: {data.Values.Count}");
  88. await Task.WhenAll(UpdateSensorDataForAcc(data), PlotAcc(data));
  89. }
  90. private async Task PlotAcc(AccData data)
  91. {
  92. if (startTimeAcc < 0)
  93. {
  94. startTimeAcc = data.Timestamp;
  95. }
  96. if (SensorConfig.plotAcc)
  97. {
  98. var internalTimestamp =
  99. (data.Timestamp - startTimeAcc) / 1000000;
  100. var timestamp = internalTimestamp + timeSync.DifStartAccStart;
  101. foreach (var item in data.Values)
  102. {
  103. await accPlotFile.WriteDataLine(timestamp, new[] {item.x, item.y, item.z});
  104. timestamp += 1000 / SensorConfig.accSampleRate;
  105. }
  106. }
  107. }
  108. private async Task UpdateSensorDataForAcc(AccData data)
  109. {
  110. foreach (var item in data.Values)
  111. {
  112. sensorData.Acc = item;
  113. await Task.Delay(1000 / SensorConfig.accSampleRate);
  114. }
  115. }
  116. private async void OnEcgData(EcgData data)
  117. {
  118. if (timeSync.DifStartEcgStart < 0)
  119. {
  120. timeSync.DifStartEcgStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
  121. }
  122. if (startTimeEcg < 0)
  123. {
  124. startTimeEcg = data.Timestamp;
  125. }
  126. if (SensorConfig.plotEcg)
  127. {
  128. var internalTimestamp =
  129. (data.Timestamp - startTimeEcg) / 1000000;
  130. var timestamp = internalTimestamp + timeSync.DifStartEcgStart;
  131. foreach (var item in data.Values)
  132. {
  133. await ecgPlotFile.WriteDataLine(timestamp, new[] {item});
  134. timestamp += 1000 / ECG_SAMPLE_RATE;
  135. }
  136. }
  137. sensorData.EcgValue = data.Values[0]; //TODO
  138. }
  139. }