|
@@ -31,6 +31,13 @@ public struct PolarSensorData
|
|
|
public float EcgValue;
|
|
|
}
|
|
|
|
|
|
+public struct TimeSync
|
|
|
+{
|
|
|
+ public DateTime StartTime;
|
|
|
+ public long DifStartAccStart;
|
|
|
+ public long DifStartEcgStart;
|
|
|
+}
|
|
|
+
|
|
|
public class PolarReceiver
|
|
|
{
|
|
|
private UdpConnection connection;
|
|
@@ -41,13 +48,18 @@ public class PolarReceiver
|
|
|
|
|
|
private PlotFileWriter ecgPlotFile;
|
|
|
private PlotFileWriter accPlotFile;
|
|
|
- private static DateTime startTime;
|
|
|
- private static long previousTime;
|
|
|
+
|
|
|
+ private static long startTimeAcc = -1;
|
|
|
+ private static long startTimeEcg = -1;
|
|
|
+
|
|
|
+ private static TimeSync timeSync;
|
|
|
+ private const int ECG_SAMPLE_RATE = 130;
|
|
|
+
|
|
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
|
private static void InitTime()
|
|
|
{
|
|
|
- startTime = DateTime.Now;
|
|
|
+ timeSync = new TimeSync {StartTime = DateTime.Now, DifStartAccStart = -1, DifStartEcgStart = -1};
|
|
|
}
|
|
|
|
|
|
public PolarReceiver(PolarSensorConfig config)
|
|
@@ -66,7 +78,7 @@ public class PolarReceiver
|
|
|
if (SensorConfig.plotEcg)
|
|
|
{
|
|
|
ecgPlotFile = DebugPlot.Instance.StartPlotting("Assets/Plotting/plots/ecg.tsv");
|
|
|
- await accPlotFile.WriteDataLine("timestamp", new[] {"voltage"});
|
|
|
+ await ecgPlotFile.WriteDataLine("timestamp", new[] {"voltage"});
|
|
|
}
|
|
|
|
|
|
if (SensorConfig.plotAcc || SensorConfig.plotEcg)
|
|
@@ -86,21 +98,27 @@ public class PolarReceiver
|
|
|
|
|
|
private async void OnAccData(AccData data)
|
|
|
{
|
|
|
- var now = DateTime.Now;
|
|
|
+ if (timeSync.DifStartAccStart < 0)
|
|
|
+ {
|
|
|
+ timeSync.DifStartAccStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
|
|
|
+ }
|
|
|
+
|
|
|
Debug.Log($"#ACC DATA items: {data.Values.Count}");
|
|
|
- Debug.Log($"ACC DATA s since last: {data.Timestamp - previousTime} /1000000000f");
|
|
|
- previousTime = data.Timestamp;
|
|
|
await Task.WhenAll(UpdateSensorDataForAcc(data), PlotAcc(data));
|
|
|
}
|
|
|
|
|
|
private async Task PlotAcc(AccData data)
|
|
|
{
|
|
|
+ if (startTimeAcc < 0)
|
|
|
+ {
|
|
|
+ startTimeAcc = data.Timestamp;
|
|
|
+ }
|
|
|
+
|
|
|
if (SensorConfig.plotAcc)
|
|
|
{
|
|
|
- var timestamp =
|
|
|
- (int) (DateTime.Now - startTime)
|
|
|
- .TotalMilliseconds; //not perfect because of delay. But that's the time the game get's the data
|
|
|
- //var timestamp = data.Timestamp;
|
|
|
+ var internalTimestamp =
|
|
|
+ (data.Timestamp - startTimeAcc) / 1000000;
|
|
|
+ var timestamp = internalTimestamp + timeSync.DifStartAccStart;
|
|
|
foreach (var item in data.Values)
|
|
|
{
|
|
|
await accPlotFile.WriteDataLine(timestamp, new[] {item.x, item.y, item.z});
|
|
@@ -113,23 +131,32 @@ public class PolarReceiver
|
|
|
{
|
|
|
foreach (var item in data.Values)
|
|
|
{
|
|
|
- sensorData.Acc = data.Values[0];
|
|
|
+ sensorData.Acc = item;
|
|
|
await Task.Delay(1000 / SensorConfig.accSampleRate);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async void OnEcgData(EcgData data)
|
|
|
{
|
|
|
+ if (timeSync.DifStartEcgStart < 0)
|
|
|
+ {
|
|
|
+ timeSync.DifStartEcgStart = (long) (DateTime.Now - timeSync.StartTime).TotalMilliseconds;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (startTimeEcg < 0)
|
|
|
+ {
|
|
|
+ startTimeEcg = data.Timestamp;
|
|
|
+ }
|
|
|
+
|
|
|
if (SensorConfig.plotEcg)
|
|
|
{
|
|
|
- //var timestamp = data.Timestamp; // if we wanted to use the timestamp of the polar sensor directly
|
|
|
- var timestamp =
|
|
|
- (int) (DateTime.Now - startTime)
|
|
|
- .TotalMilliseconds; //not perfect because of delay. But that's the time the game get's the data
|
|
|
+ var internalTimestamp =
|
|
|
+ (data.Timestamp - startTimeEcg) / 1000000;
|
|
|
+ var timestamp = internalTimestamp + timeSync.DifStartEcgStart;
|
|
|
foreach (var item in data.Values)
|
|
|
{
|
|
|
await ecgPlotFile.WriteDataLine(timestamp, new[] {item});
|
|
|
- timestamp += 1000 / 130;
|
|
|
+ timestamp += 1000 / ECG_SAMPLE_RATE;
|
|
|
}
|
|
|
}
|
|
|
|