简体   繁体   English

自定义OpenVR驱动程序,带有抖动的android旋转

[英]Custom OpenVR driver, with jittery android rotation

I'm working on a simple (I thought) OpenVR driver that sends the compositors output to my android phone to display with google cardboard. 我正在研究一个简单的(我认为)OpenVR驱动程序,该驱动程序将合成器的输出发送到我的android手机中,以便与Google纸板一起显示。 I've got the basics working but when my driver receives the rotation data from my android device it's very noisy and with my phone sat still on my desk it jitters loads. 我已经掌握了基础知识,但是当我的驱动程序从我的android设备接收旋转数据时,它非常嘈杂,并且我的手机仍坐在我的桌子上,这会抖动。

the floats are being sent over in network byte order. 浮点数以网络字节顺序发送。

Here is the code receiving the rotation data: 这是接收旋转数据的代码:

float BytesToFloat(unsigned char* buffer, int start)
{
    float f = (buffer[start] << 24) | (buffer[start + 1] << 16) | (buffer[start + 2] << 8) | buffer[start + 3];
    return f;

}

void MobileDeviceReceiver::PoseThread(std::function<void(MobileVector*)> PoseUpdateCallback)
{
    sockaddr_in client;
    int inLen,clientStructLen = sizeof(client);

    MobileVector currentPose;

    DriverLog("started pose tracking thread %d\n",m_isActive);

    bool errorLastCall = false;

    char pchBuffer[65535];

    while (m_isActive)
    {
        if ((inLen = recvfrom(m_socket, pchBuffer, 65535, 0, (sockaddr*)&client, &clientStructLen)) == SOCKET_ERROR)
        {
            if (errorLastCall == false)
            {
                DriverLog("Error receiving data: %d\n", WSAGetLastError());
            }
            errorLastCall = true;
        }
        else {
            errorLastCall = false;
        }

        currentPose.x = floorf(BytesToFloat((unsigned char*)pchBuffer, 0)*10000)/10000;
        currentPose.y = 0;//BytesToFloat((unsigned char*)pchBuffer, 4);
        currentPose.z = 0;//BytesToFloat((unsigned char*)pchBuffer, 8);

        PoseUpdateCallback(&currentPose);
    }


}

And the code sending it from the phone: 以及从手机发送的代码:

class Task implements Runnable
{
    public Queue<DatagramPacket> packets = new LinkedList<DatagramPacket>();
    private boolean isActive = true;

    @Override
    public void run() {
        try{
            DatagramSocket socket = new DatagramSocket();

            while(isActive)
            {
                if(packets.size() > 0)
                {
                    socket.send(packets.remove());
                }
            }

        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void Kill()
    {
        isActive = false;
    }
}

ByteBuffer buffer = ByteBuffer.allocate(12);

    protected float ALPHA = 0.05f;

    private float[] lowPassFilter(float[] input,float[] output)
    {
        if(output == null) return input;

        for ( int i=0; i<input.length; i++ ) {
            output[i] = output[i] + ALPHA * (input[i] - output[i]);
        }

        return output;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if(sensorEvent.sensor == sRotation)
        {
            float x,y,z;
            x = (float) Math.toRadians(sensorEvent.values[0]);
            y = (float) Math.toRadians(sensorEvent.values[1]);
            z = (float) Math.toRadians(sensorEvent.values[2]);

            float[] values = lowPassFilter(new float[]{x,y,z},new float[3]);

            buffer.order(ByteOrder.BIG_ENDIAN);
            buffer.putFloat(values[0]);
            buffer.putFloat(values[1]);
            buffer.putFloat(values[2]);

            try {
                DatagramPacket packet = new DatagramPacket(buffer.array(), 12, InetAddress.getByName(IP), 8888);

                if(task != null) {
                    task.packets.add(packet);
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }

            buffer.clear();
        }
    }

Thanks in advance 提前致谢

For future reference I was doing two things wrong: 供将来参考,我做错了两件事:

  1. I was sending the float in network byteorder and not rearranging it to native byte order on the local machine. 我以网络字节顺序发送浮点数,而未在本地计算机上将其重新排列为本地字节顺序。
  2. Bitshifts don't seem to work for floats, so I used memset instead. 位移位似乎不适用于浮点数,因此我改用了memset。

After changing these two things it all worked perfectly. 更改了这两件事之后,它们都可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM