// Use this for initialization public SocketImpedance() { // Open Socket (talker) and starting UdpClient (listener) this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.client = new UdpClient(portRecv); this.ipSendPoint = new IPEndPoint(IPAddress.Parse(IP), portSend); this.ipRecvPoint = new IPEndPoint(IPAddress.Parse(IP), portRecv); SocketImpedance inst = this; this.client.BeginReceive(new AsyncCallback(ReceiveCallback), inst); print("INIT:: Link to Haptic Interface Established"); }
private static void ReceiveCallback(IAsyncResult ar) { SocketImpedance inst = (SocketImpedance)(ar.AsyncState); UdpClient u = (UdpClient)inst.client; IPEndPoint e = (IPEndPoint)inst.ipRecvPoint; Byte[] data = u.EndReceive(ar, ref e); inst.position = new Vector3((float)BitConverter.ToDouble(data, 8), (float)BitConverter.ToDouble(data, 16), -1 * ((float)BitConverter.ToDouble(data, 0))); inst.orientation = new Vector3((float)BitConverter.ToDouble(data, 32), (float)BitConverter.ToDouble(data, 40), (float)BitConverter.ToDouble(data, 24)); //Debug.Log("Recv: " + inst.position.x + ", " + inst.position.y + ", " + inst.position.z); inst.vitesse = new Vector3((float)BitConverter.ToDouble(data, 56), (float)BitConverter.ToDouble(data, 64), -1 * ((float)BitConverter.ToDouble(data, 48))); inst.vitesseAngulaire = new Vector3((float)BitConverter.ToDouble(data, 80), (float)BitConverter.ToDouble(data, 88), -1 * ((float)BitConverter.ToDouble(data, 72))); // Formatting data to send byte[] dataX = BitConverter.GetBytes((double)-1 * inst.force.z); byte[] dataY = BitConverter.GetBytes((double)inst.force.x); byte[] dataZ = BitConverter.GetBytes((double)inst.force.y); //Debug.Log("Send: " + inst.force.x + ", " + inst.force.y + ", " + inst.force.z); byte[] dataToSend = new byte[24]; for (int i = 0; i < dataX.Length; i++) { dataToSend[i] = dataX[i]; dataToSend[i + 8] = dataY[i]; dataToSend[i + 16] = dataZ[i]; } // Send force through socket try { inst.socket.SendTo(dataToSend, inst.ipSendPoint); } catch (Exception exception) { print("Exception caught: " + exception.Message); } inst.client.BeginReceive(new AsyncCallback(ReceiveCallback), inst); }
//Vector3 B = new Vector3(1/8, 1/8, 1/8); // Use this for initialization void Start() { this.socket = ScriptableObject.CreateInstance <SocketImpedance>(); this.force = Vector3.zero; // force vector to be sent to haptic interface // Getting proxy Rigidbody and its parameters this.rb = GetComponent <Rigidbody>(); this.m = this.rb.mass; // Computing dampening (stability) this.b = 2f * (float)Math.Pow(this.k * this.m, 0.5f); print("INIT:: Coefficients: k = " + this.k + ", b = " + this.b); // Getting Handle GameObject and its script handle = GameObject.Find("Handle"); handleScript = this.handle.GetComponent <HandleManager>(); // Initialize position of handle and proxy to avoid huge forces // at the start of the game this.rb.position = this.sizeFactor * (socket.GetPosition()); handle.transform.position = this.sizeFactor * (socket.GetPosition()); print("INIT:: Haptics Done"); }