PolarReceiver.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Logging;
  5. using Plotting;
  6. using UnityEngine;
  7. using UnityEngine.Serialization;
  8. namespace Sensors.Polar
  9. {
  10. [System.Serializable]
  11. public struct PolarSensorConfig
  12. {
  13. public int port;
  14. public String ipAddress;
  15. public bool plotAcc;
  16. public bool plotEcg;
  17. public int accSampleRate; //TODO: let user choose between 25, 50, 100, 200
  18. public PolarSensorConfig(int port = 9099, string ipAddress = "0.0.0.0", bool plotAcc = false, bool plotEcg = false,
  19. int accSampleRate = 25)
  20. {
  21. this.port = port;
  22. this.ipAddress = ipAddress;
  23. this.plotAcc = plotAcc;
  24. this.plotEcg = plotEcg;
  25. this.accSampleRate = accSampleRate;
  26. }
  27. }
  28. public struct PolarSensorData
  29. {
  30. public Vector3 Acc;
  31. public float EcgValue;
  32. }
  33. public struct TimeSync
  34. {
  35. public DateTime StartTime;
  36. public long DifStartAccStart;
  37. public long DifStartEcgStart;
  38. }
  39. public class PolarReceiver
  40. {
  41. private UdpConnection connection;
  42. private PolarSensorData sensorData;
  43. public PolarSensorConfig SensorConfig { get; private set; }
  44. public PolarSensorData SensorData => sensorData;
  45. private AsyncLogFileWriter ecgPlotFile;
  46. private AsyncLogFileWriter accPlotFile;
  47. private static long startTimeAcc = -1;
  48. private static long startTimeEcg = -1;
  49. private static TimeSync timeSync;
  50. private const int ECG_SAMPLE_RATE = 130;
  51. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  52. private static void InitTime()
  53. {
  54. timeSync = new TimeSync {StartTime = DateTime.Now, DifStartAccStart = -1, DifStartEcgStart = -1};
  55. }
  56. public PolarReceiver(PolarSensorConfig config)
  57. {
  58. SensorConfig = config;
  59. }
  60. public async Task StartListening()
  61. {
  62. if (SensorConfig.plotAcc)
  63. {
  64. accPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/acc.tsv");
  65. await accPlotFile.WriteDataLine("timestamp", new[] {"x", "y", "z"});
  66. }
  67. if (SensorConfig.plotEcg)
  68. {
  69. ecgPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/ecg.tsv");
  70. await ecgPlotFile.WriteDataLine("timestamp", new[] {"voltage"});
  71. }
  72. if (SensorConfig.plotAcc || SensorConfig.plotEcg)
  73. {
  74. DebugPlot.Instance.ShowPlots();
  75. }
  76. connection = new UdpConnection(SensorConfig.ipAddress, SensorConfig.port, OnAccData, OnEcgData);
  77. connection.Listen();
  78. }
  79. public void Dispose()
  80. {
  81. connection.StopListening();
  82. DebugPlot.DestroyInstance();
  83. }
  84. private async void OnAccData(AccData data)
  85. {
  86. if (timeSync.DifStartAccStart < 0)
  87. {
  88. timeSync.DifStartAccStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
  89. }
  90. Debug.Log($"#ACC DATA items: {data.Values.Count}");
  91. await Task.WhenAll(UpdateSensorDataForAcc(data), PlotAcc(data));
  92. }
  93. private async Task PlotAcc(AccData data)
  94. {
  95. if (startTimeAcc < 0)
  96. {
  97. startTimeAcc = data.Timestamp;
  98. }
  99. if (SensorConfig.plotAcc)
  100. {
  101. var internalTimestamp =
  102. (data.Timestamp - startTimeAcc) / 1000000;
  103. var timestamp = internalTimestamp + timeSync.DifStartAccStart;
  104. foreach (var item in data.Values)
  105. {
  106. await accPlotFile.WriteDataLine(timestamp, new[] {item.x, item.y, item.z});
  107. timestamp += 1000 / SensorConfig.accSampleRate;
  108. }
  109. }
  110. }
  111. private async Task UpdateSensorDataForAcc(AccData data)
  112. {
  113. foreach (var item in data.Values)
  114. {
  115. sensorData.Acc = item;
  116. await Task.Delay(1000 / SensorConfig.accSampleRate);
  117. }
  118. }
  119. private async void OnEcgData(EcgData data)
  120. {
  121. if (timeSync.DifStartEcgStart < 0)
  122. {
  123. timeSync.DifStartEcgStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
  124. }
  125. if (startTimeEcg < 0)
  126. {
  127. startTimeEcg = data.Timestamp;
  128. }
  129. if (SensorConfig.plotEcg)
  130. {
  131. var internalTimestamp =
  132. (data.Timestamp - startTimeEcg) / 1000000;
  133. var timestamp = internalTimestamp + timeSync.DifStartEcgStart;
  134. foreach (var item in data.Values)
  135. {
  136. await ecgPlotFile.WriteDataLine(timestamp, new[] {item});
  137. timestamp += 1000 / ECG_SAMPLE_RATE;
  138. }
  139. }
  140. sensorData.EcgValue = data.Values[0]; //TODO
  141. }
  142. }
  143. }