Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackIR only updates at 4hz #182

Closed
johnflux opened this issue Dec 30, 2019 · 11 comments
Closed

TrackIR only updates at 4hz #182

johnflux opened this issue Dec 30, 2019 · 11 comments

Comments

@johnflux
Copy link

@johnflux johnflux commented Dec 30, 2019

TrackIR should be updating at 60hz, but it's actually only updating at 4hz.

import time
import os
from os.path import expanduser 

def timestamp_ms():
	return int(time.time() * 1000).ToString()

def update():
    diagnostics.debug("Time in ms: " + timestamp_ms() )

if starting:
	trackIR.update += update

This shows it a gap of about 250 milliseconds between update() calls.

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 30, 2019

I had a look at the code, it polls trackir as fast as the script loop which defaults to system clock (16ms interval).

The polling checks the trackir for data

public bool ReadPosition(ref HeadPoseData output)

Maybe something is wrong with your local trackir setup?

@johnflux
Copy link
Author

@johnflux johnflux commented Dec 30, 2019

Afaics, there's not really any way to setup trackir incorrectly. I just installed the trackir software, and that was it. There's no configuration of update frequency or anything, as far as I can see.

Looking at this NPClientSpoof.cs file - is this file used for reading from a real TrackIR device? The name and comments mention spoofing this npclient.dll
It uses a worker thread - how often does the worker thread update?

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 30, 2019

Yeah, it's called each script iteration.

if (spoofer.ReadPosition(ref data))

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 30, 2019

You can override your system clock but setting it to 4hz should lead to severe problems in the OS but please check at which rate the script runs

@johnflux
Copy link
Author

@johnflux johnflux commented Dec 30, 2019

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 30, 2019

I'm not the author behind the plugin so I do not know all details around design decisions. But as I can recall trackir dll sometimes crashes without managed code being able to survive, so the entire trackir dll is exeucted inside another process and then we read the data using shared memory.

DoExecute in this class execute in another process

https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.Core.Plugins/MemoryMapping/TrackIRWorker.cs

@johnflux
Copy link
Author

@johnflux johnflux commented Dec 30, 2019

@johnflux
Copy link
Author

@johnflux johnflux commented Dec 31, 2019

Again just fwiw, I polished up my python reimplementation, and pushed to here: https://github.com/johnflux/python_trackir

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 31, 2019

We only use python as a mean to map input and output, the actual code is written in c# and c so it's not usable for freepie.

Our update event only trigger if new data and the frame ID have updated maybe its as simple as there is not new data from trackir, are you moving the tracker around when you get 4hz?

@johnflux
Copy link
Author

@johnflux johnflux commented Dec 31, 2019

so it's not usable for freepie.

I know, but it shows that the problem isn't with TrackIR. And it might be useful for someone else.

Yes I'm moving the tracker around. It's not a problem with setup, since I get the full 120hz from my own program.

I looked at the code - and in particular TrackIRWorker.cs which I believe is the problem, but I don't see any problem. I see nothing in this code that could introduce >200ms delays.

For now I will leave it, since my reimplementation solves the problem for myself.

@AndersMalmgren
Copy link
Owner

@AndersMalmgren AndersMalmgren commented Dec 31, 2019

I wrote this little dummy plugin that does the same thing as the trackir plugin minus the TrackIR stuff. I get 5ms intervals, which is strange in itself, the system clock is abut 62hz so it should get 16ms. :D

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using FreePIE.Core.Contracts;
using FreePIE.Core.Plugins.MemoryMapping;

namespace FreePIE.Core.Plugins
{
    [GlobalType(Type = typeof(FoobarGlobal))]
    public class FoobarPlugin : Plugin
    {
        private readonly MappedMemory<DisconnectedFreepieData>  data = new MappedMemory<DisconnectedFreepieData>(DisconnectedFreepieData.SharedMemoryName);
        private readonly Stopwatch watch = new Stopwatch();
        
        private long lastTick = -1;


        public override object CreateGlobal()
        {
            return new FoobarGlobal(this);
        }

        public override string FriendlyName => "Foobar";

        public override Action Start()
        {
            var worker = new WorkerProcess<MyWorker>();
            worker.Start();

            watch.Start();
            return null;
        }

        public override void DoBeforeNextExecute()
        {
            var trackirData = data.Read(x => x.TrackIRData);
            if (trackirData.LastUpdatedTicks != lastTick)
            {
                Debug.WriteLine($"Elapsed ms: {watch.ElapsedMilliseconds}");
                watch.Restart();

                lastTick = trackirData.LastUpdatedTicks;
                OnUpdate();
            }
        }
    }

    [Global(Name = "foobar")]
    public class FoobarGlobal : UpdateblePluginGlobal<FoobarPlugin>
    {
        public FoobarGlobal(FoobarPlugin plugin) : base(plugin)
        {
        }
    }

    public class MyWorker : SharedMemoryWorker
    {


        protected override void DoExecute(IEnumerable<string> arguments)
        {
            var data = freePIEData.Read(x => x.TrackIRData);
            data.LastUpdatedTicks = DateTime.Now.Ticks;
            freePIEData.Write(x => x.TrackIRData, data);

            Thread.Sleep(1);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants