- Home /
UDP using HTC VIVE tracker
I'm working on sending the POSE of the HTC Vive tracker via UDP from a "client" PC Running the main VR experience and I want to update objects POSE on another computer (eventually phone) that has a simple app to engage with the player on the main PC. I'm using some code from Triad Semiconductors but having some trouble. Here is the "emitter" running on the PC:
import triad_openvr
import time
import sys
import struct
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('unitypc IPAddress', 8051) # will replace with cpu B IPaddress
v = triad_openvr.triad_openvr()
v.print_discovered_objects()
if len(sys.argv) == 1:
interval = 1/250
elif len(sys.argv) == 2:
interval = 1/float(sys.argv[0])
else:
print("Invalid number of arguments")
interval = False
if interval:
while(True):
start = time.time()
txt = ""
data = v.devices["tracker_1"].get_pose_quaternion()
sent = sock.sendto(struct.pack('d'*len(data), *data), server_address)
#print('\r' + txt, end='')
sleep_time = interval-(time.time()-start)
if sleep_time>0:
time.sleep(sleep_time)
And here is my code on my other computer I'm sending the data too:
//The following code can be used to receive pose data from udp_emitter.py and use it to track an object in unity
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class udp_tracked_object : MonoBehaviour {
Thread receiveThread;
UdpClient client;
private Double[] float_array;
private int port = 8051;
// Use this for initialization
void Start () {
float_array = new Double[7];
receiveThread = new Thread(new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// Update is called once per frame
void Update () {
transform.position = new Vector3((float) float_array[0], (float)float_array[1], (float)float_array[2]);
transform.rotation = new Quaternion((float)float_array[3], (float)float_array[4], (float)float_array[5], (float)float_array[6]);
}
void OnApplicationQuit()
{
if (receiveThread != null)
receiveThread.Abort();
client.Close();
}
// receive thread
private void ReceiveData()
{
port = 8051;
client = new UdpClient(port);
print("Starting Server");
while (true)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
for (int i = 0; i < data.Length; i++)
float_array[i] = BitConverter.ToDouble(data, i * 8);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
}
I can send simple strings between two python programs already on the two computers so I'm stuck on what's going on with my UDP receiving end in unity. I don't think it's the firewall but I'm sending it all over my LAN wifi network at home. Thanks for the help in advance and I'm happy to clarify anything!
Solved the problem! Turns out to be a firewall issue. I was able to narrow it down to seeing where the program stopped or got hung up. I placed a print() right after the client.Receive() and it never got to it. Turns out - the receive function will sit there until something comes through but won't time out so back tracked it and found it was the firewall blocking the info.