Bladeren bron

Listening to Polar for Tilting

wwdmbvbb 3 jaren geleden
bovenliggende
commit
191a2878d5

+ 1 - 1
Assembly-CSharp.csproj

@@ -255,7 +255,7 @@
      <Compile Include="Assets\Scripts\ANT+\SpeedSensorReceiver.cs" />
      <Compile Include="Assets\Scripts\BicyleController\BicyleController.cs" />
      <Compile Include="Assets\Scripts\HrDisplay.cs" />
-     <Compile Include="Assets\Scripts\Polar\PolarClient.cs" />
+     <Compile Include="Assets\Scripts\Polar\PolarAccData.cs" />
      <Compile Include="Assets\Scripts\Polar\PolarListener.cs" />
      <Compile Include="Assets\Scripts\Polar\UdpConnection.cs" />
      <Compile Include="Assets\Scripts\SimpleCameraController.cs" />

+ 35 - 0
Assets/Scripts/Polar/PolarAccData.cs

@@ -0,0 +1,35 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using UnityEngine;
+using Valve.VR.InteractionSystem;
+
+public class AccData
+{
+    private long timestamp;
+    private List<Vector3> values;
+
+    public long Timestamp => timestamp;
+
+    public List<Vector3> Values => values;
+
+    AccData(long timestamp, List<Vector3> values)
+    {
+        this.timestamp = timestamp;
+        this.values = values;
+    }
+
+    public static AccData fromString(string s)
+    {
+        var data = s.Split(';');
+        if (data.Length == 2)
+        {
+            var t = long.Parse(data[0]);
+            var tupleStrings = data[1].Split(',').Select(item => item.Replace("(", "").Replace(")", ""));
+            var floats = tupleStrings.Select(s1 => s1.Split('/').Select(value => float.Parse(value)).ToList()).ToList();
+            return new AccData(t, floats.Select(a => new Vector3(a[0], a[1], a[2])).ToList());
+        }
+
+        return null;
+    }
+}

+ 1 - 1
Assets/Scripts/Polar/PolarClient.cs.meta → Assets/Scripts/Polar/PolarAccData.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: d9c3134e7e3cb9f498ae1b556aa3320d
+guid: f344a20e9aeabc643931c7aaec8ced83
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 56
Assets/Scripts/Polar/PolarClient.cs

@@ -1,56 +0,0 @@
-using System;
-using System.Net;
-using System.Net.Sockets;
-using System.Text;
-using System.Threading.Tasks;
-using UnityEngine;
-
-public class PolarClient
-{
-    private static bool running = false;
-    private static UdpClient client;
-
-    //TODO: onData callback
-    public static void Start(string ipAddress, int port)
-    {
-        var endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
-        client = new UdpClient();
-        try
-        {
-            Debug.Log("PolarClient: Connecting to server");
-            client.Connect(endPoint);
-            running = true;
-            Debug.Log($"PolarClient: Connected to server: {client.Client.Connected}");
-
-            var d = Encoding.UTF8.GetBytes("Blabla");
-            client.Send(d, d.Length);
-            
-            while (running)
-            {
-                var data = client.Receive(ref endPoint);
-                HandleData(data);
-            }
-            
-            /*await Task.Run(()
-                =>
-            {
-                
-            })*;*/
-        }
-        catch (Exception e)
-        {
-            Debug.Log($"PolarClient Error: {e}");
-        }
-    }
-
-    private static void Stop()
-    {
-        running = false;
-        client.Dispose();
-    }
-
-    private static void HandleData(byte[] data)
-    {
-        Debug.Log($"PolarClient Data: {Encoding.UTF8.GetString(data)}");
-    }
-}

+ 21 - 11
Assets/Scripts/Polar/PolarListener.cs

@@ -5,26 +5,36 @@ using UnityEngine;
 public class PolarListener : MonoBehaviour
 {
     private UdpConnection connection;
+    public int port = 9099;
+    public String ipAddress = "0.0.0.0";
+    private float rotation = 0f;
+    public float divisionValue = 60f;
     private void Start()
     {
-        string sendIp = "127.0.0.1";
-        int sendPort = 8881;
-        int receivePort = 6789;
- 
-        connection = new UdpConnection();
-        connection.StartConnection(sendIp, sendPort, receivePort);
+        connection = new UdpConnection(ipAddress, port, onAccData);
+        connection.Listen();
+    }
+
+    private void OnGUI()
+    {
+        GUI.TextField(new Rect(10,10, 100, 20), $"{rotation}");
+    }
+
+    private void onAccData(AccData data)
+    {
+        //Debug.Log(data.Timestamp);
+        rotation = data.Values[0].y / divisionValue;
+        Debug.Log($"Rotate {data.Values[0].y} degrees");
+        //transform.RotateAround(transform.position, Vector3.forward, data.Values[0].y);
     }
 
     private void Update()
     {
-        
-        foreach (var message in connection.getMessages()) Debug.Log(message);
- 
-        connection.Send("Hi!");
+        transform.rotation = Quaternion.Euler(0,90,rotation);
     }
 
     private void OnDestroy()
     {
-        connection.Stop();
+        connection.StopListening();
     }
 }

+ 40 - 85
Assets/Scripts/Polar/UdpConnection.cs

@@ -4,102 +4,57 @@ using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Threading;
+using System.Threading.Tasks;
 using UnityEngine;
- 
+
 public class UdpConnection
 {
-    private UdpClient udpClient;
- 
-    private readonly Queue<string> incomingQueue = new Queue<string>();
-    Thread receiveThread;
-    private bool threadRunning = false;
-    private string senderIp;
-    private int senderPort;
- 
-    public void StartConnection(string sendIp, int sendPort, int receivePort)
+    //public int port = 9090;
+    //public String ipAddress = "192.168.1.7";
+
+    private UdpClient client;
+
+    private bool listening = false;
+    private Action<AccData> onAccData;
+
+    public UdpConnection(String ipAddress, int port, Action<AccData> onAccData)
     {
-        try { udpClient = new UdpClient(receivePort); }
-        catch (Exception e)
-        {
-            Debug.Log("Failed to listen for UDP at port " + receivePort + ": " + e.Message);
-            return;
-        }
-        Debug.Log("Created receiving client at ip  and port " + receivePort);
-        this.senderIp = sendIp;
-        this.senderPort = sendPort;
- 
-        Debug.Log("Set sendee at ip " + sendIp + " and port " + sendPort);
- 
-        StartReceiveThread();
+        this.onAccData = onAccData;
+        client = new UdpClient(new IPEndPoint(IPAddress.Parse(ipAddress), port));
     }
- 
-    private void StartReceiveThread()
+
+    public void Listen() => Task.Run(async () =>
     {
-        receiveThread = new Thread(() => ListenForMessages(udpClient));
-        receiveThread.IsBackground = true;
-        threadRunning = true;
-        receiveThread.Start();
-    }
- 
-    private void ListenForMessages(UdpClient client)
-    {
-        IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- 
-        while (threadRunning)
+        listening = true;
+        while (listening)
         {
-            try
+            var result = await client.ReceiveAsync();
+            String receivedStr = Encoding.UTF8.GetString(result.Buffer);
+
+            var data = receivedStr.Split(':');
+            if (data.Length == 2)
             {
-                Byte[] receiveBytes = client.Receive(ref remoteIpEndPoint); // Blocks until a message returns on this socket from a remote host.
-                string returnData = Encoding.UTF8.GetString(receiveBytes);
- 
-                lock (incomingQueue)
+                var name = data[0];
+                if (name.Equals("ACC"))
+                {
+                    onAccData(AccData.fromString(data[1]));
+                }else if (name.Equals("ECG"))
                 {
-                    incomingQueue.Enqueue(returnData);
+                    //TODO
+                }else if (name.Equals("HR"))
+                {
+                    //TODO
                 }
+                
             }
-            catch (SocketException e)
-            {
-                // 10004 thrown when socket is closed
-                if (e.ErrorCode != 10004) Debug.Log("Socket exception while receiving data from udp client: " + e.Message);
-            }
-            catch (Exception e)
-            {
-                Debug.Log("Error receiving data from udp client: " + e.Message);
-            }
-            Thread.Sleep(1);
+            
+            Debug.Log($"RECEIVED DATA VIA UDP: {receivedStr}");
         }
-    }
- 
-    public string[] getMessages()
-    {
-        string[] pendingMessages = new string[0];
-        lock (incomingQueue)
-        {
-            pendingMessages = new string[incomingQueue.Count];
-            int i = 0;
-            while (incomingQueue.Count != 0)
-            {
-                pendingMessages[i] = incomingQueue.Dequeue();
-                i++;
-            }
-        }
- 
-        return pendingMessages;
-    }
- 
-    public void Send(string message)
-    {
-        Debug.Log(String.Format("Send msg to ip:{0} port:{1} msg:{2}",senderIp,senderPort,message));
-        IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Parse(senderIp), senderPort);
-        Byte[] sendBytes = Encoding.UTF8.GetBytes(message);
-        udpClient.Send(sendBytes, sendBytes.Length, serverEndpoint);
-    }
- 
-    public void Stop()
+    });
+
+    public void StopListening()
     {
-        threadRunning = false;
-        receiveThread.Abort();
-        udpClient.Close();
+        listening = false;
     }
-}
- 
+    
+}

BIN
Library/ArtifactDB


+ 21 - 21
Library/CurrentLayout-default.dwlt

@@ -21,7 +21,7 @@ MonoBehaviour:
   m_ShowMode: 4
   m_Title: 
   m_RootView: {fileID: 2}
-  m_MinSize: {x: 875, y: 300}
+  m_MinSize: {x: 875, y: 392}
   m_MaxSize: {x: 10000, y: 10000}
   m_Maximized: 1
 --- !u!114 &2
@@ -112,10 +112,10 @@ MonoBehaviour:
     y: 30
     width: 1920
     height: 1067
-  m_MinSize: {x: 677, y: 342}
-  m_MaxSize: {x: 12002, y: 8042}
+  m_MinSize: {x: 679, y: 342}
+  m_MaxSize: {x: 12004, y: 8042}
   vertical: 0
-  controlID: 136
+  controlID: 91
 --- !u!114 &6
 MonoBehaviour:
   m_ObjectHideFlags: 52
@@ -137,10 +137,10 @@ MonoBehaviour:
     y: 0
     width: 1456
     height: 1067
-  m_MinSize: {x: 402, y: 342}
-  m_MaxSize: {x: 8002, y: 8042}
+  m_MinSize: {x: 403, y: 342}
+  m_MaxSize: {x: 8003, y: 8042}
   vertical: 1
-  controlID: 87
+  controlID: 92
 --- !u!114 &7
 MonoBehaviour:
   m_ObjectHideFlags: 52
@@ -162,10 +162,10 @@ MonoBehaviour:
     y: 0
     width: 1456
     height: 563
-  m_MinSize: {x: 402, y: 221}
-  m_MaxSize: {x: 8002, y: 4021}
+  m_MinSize: {x: 403, y: 221}
+  m_MaxSize: {x: 8003, y: 4021}
   vertical: 0
-  controlID: 61
+  controlID: 44
 --- !u!114 &8
 MonoBehaviour:
   m_ObjectHideFlags: 52
@@ -185,8 +185,8 @@ MonoBehaviour:
     y: 0
     width: 380
     height: 563
-  m_MinSize: {x: 200, y: 200}
-  m_MaxSize: {x: 4000, y: 4000}
+  m_MinSize: {x: 201, y: 221}
+  m_MaxSize: {x: 4001, y: 4021}
   m_ActualView: {fileID: 14}
   m_Panes:
   - {fileID: 14}
@@ -267,8 +267,8 @@ MonoBehaviour:
     y: 0
     width: 464
     height: 1067
-  m_MinSize: {x: 275, y: 50}
-  m_MaxSize: {x: 4000, y: 4000}
+  m_MinSize: {x: 276, y: 71}
+  m_MaxSize: {x: 4001, y: 4021}
   m_ActualView: {fileID: 19}
   m_Panes:
   - {fileID: 19}
@@ -460,9 +460,9 @@ MonoBehaviour:
   m_SceneHierarchy:
     m_TreeViewState:
       scrollPos: {x: 0, y: 0}
-      m_SelectedIDs: 
-      m_LastClickedID: 0
-      m_ExpandedIDs: 5afbffff
+      m_SelectedIDs: 5c480000
+      m_LastClickedID: 18524
+      m_ExpandedIDs: 68fbffff
       m_RenameOverlay:
         m_UserAcceptedRename: 0
         m_Name: 
@@ -715,9 +715,9 @@ MonoBehaviour:
     speed: 2
     m_Value: {x: 0.16989379, y: -0.7980854, z: 0.26349825, w: 0.51455265}
   m_Size:
-    m_Target: 444.49045
+    m_Target: 281.52496
     speed: 2
-    m_Value: 425.3497
+    m_Value: 294.79053
   m_Ortho:
     m_Target: 0
     speed: 2
@@ -794,7 +794,7 @@ MonoBehaviour:
     scrollPos: {x: 0, y: 79}
     m_SelectedIDs: 0c430000
     m_LastClickedID: 17164
-    m_ExpandedIDs: 000000000c430000c0600000c2600000c46000007663000000ca9a3b
+    m_ExpandedIDs: 000000000a430000c4600000c6600000c8600000
     m_RenameOverlay:
       m_UserAcceptedRename: 0
       m_Name: 
@@ -822,7 +822,7 @@ MonoBehaviour:
     scrollPos: {x: 0, y: 0}
     m_SelectedIDs: 
     m_LastClickedID: 0
-    m_ExpandedIDs: 000000000c430000c0600000c2600000c4600000
+    m_ExpandedIDs: 000000000a430000c4600000c6600000c8600000
     m_RenameOverlay:
       m_UserAcceptedRename: 0
       m_Name: 

BIN
Library/SceneVisibilityState.asset


BIN
Library/ScriptAssemblies/Assembly-CSharp-Editor.dll


BIN
Library/ScriptAssemblies/Assembly-CSharp.dll


BIN
Library/ScriptAssemblies/Assembly-CSharp.pdb


BIN
Library/ShaderCache/EditorEncounteredVariants


BIN
Library/SourceAssetDB


+ 1 - 1
Library/StateCache/Hierarchy/46b7a0-mainStage.json

@@ -1 +1 @@
-{"m_ExpandedPrefabGameObjectFileIDs":[],"m_ExpandedSceneGameObjectInstanceIDs":[],"m_ScrollY":0.0,"m_LastClickedFileID":0,"m_LastClickedInstanceID":0}
+{"m_ExpandedPrefabGameObjectFileIDs":[],"m_ExpandedSceneGameObjectInstanceIDs":[-1176,19648],"m_ScrollY":0.0,"m_LastClickedFileID":0,"m_LastClickedInstanceID":19648}

+ 1 - 1
Library/StateCache/SceneView/d69a67-mainStage.json

@@ -1 +1 @@
-{"cameraMode":{"drawMode":0,"name":"Shaded","section":"Shading Mode"},"sceneLighting":true,"audioPlay":false,"sceneViewState":{"showFog":true,"showMaterialUpdate":false,"showSkybox":true,"showFlares":true,"showImageEffects":true,"showParticleSystems":true},"in2DMode":false,"pivot":{"x":282.9608154296875,"y":-67.98749542236328,"z":-49.091976165771487},"rotation":{"x":0.16989503800868989,"y":-0.7980912923812866,"z":0.2635001838207245,"w":0.5145564675331116},"size":444.4904479980469,"orthographic":false}
+{"cameraMode":{"drawMode":0,"name":"Shaded","section":"Shading Mode"},"sceneLighting":true,"audioPlay":false,"sceneViewState":{"showFog":true,"showMaterialUpdate":false,"showSkybox":true,"showFlares":true,"showImageEffects":true,"showParticleSystems":true},"in2DMode":false,"pivot":{"x":668.790771484375,"y":-464.05010986328127,"z":-24.957921981811525},"rotation":{"x":-0.21390113234519959,"y":-0.6867938041687012,"z":0.2233380526304245,"w":-0.6577964425086975},"size":437.1996154785156,"orthographic":false}

BIN
obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache