Ejemplo n.º 1
0
    //==========================================================================================
    //
    //  Called once per frame. If we've connected to a tracker, get its data and move attached object with device
    //
    //==========================================================================================
    void Update()
    {
        if (handle > 0)
        {
            if (sync)
            {             // Try to predict
                ISenseLib.ISD_STATION_DATA_TYPE bufferData = new ISenseLib.ISD_STATION_DATA_TYPE();
                ISenseLib.ISD_RingBufferQuery(handle, station, ref bufferData, new IntPtr(), new IntPtr());

                float dT            = (float)Time.deltaTime;
                float curTime       = bufferData.TimeStamp;           //TODO: is this right?
                float timeToPredict = curTime + (framePredict * dT);


                GetRingBufferDataAtTime(timeToPredict);
            }
            else
            {
                ISenseLib.ISD_GetTrackingData(handle, ref data);                 // Get the tracking data
            }

            ISenseLib.ISD_STATION_DATA_TYPE dataSet = data.Station[station - 1];             // Specify the dataset

            // Only IS-1200 and IS-1500 have trackState
            if (trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS1200 || trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS1500)
            {
                trackState = dataSet.TrackingState;
            }

            Vector3 pos;

            // If device can control position, control position!

            if (trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS600 ||
                trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS900 ||
                trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS1200 ||
                trackerModel == ISenseLib.ISD_SYSTEM_MODEL.ISD_IS1500)
            {
                pos = new Vector3(dataSet.Position[1], -dataSet.Position[2], dataSet.Position[0]);                 // Their y is up/down while that's our z

                transform.localPosition = pos;
            }

            // Time to deal with rotation!

            float[] cbn = dataSet.Cbn;

            float[] eulers;

            cbn = AngleUtil.ConvertUnityCbn(cbn);              // Converts our right-handed rotation frame to unity's left-handed one.

            eulers = AngleUtil.Cbn2Euler(cbn);

            transform.localRotation = Quaternion.Euler(eulers[1], eulers[2], eulers[0]);             // Pitch, Yaw, Roll
        }
    }
Ejemplo n.º 2
0
    //==========================================================================================
    //
    //  Approximates data at time using closest data in ring buffer
    //
    //==========================================================================================

    void GetRingBufferDataAtTime(float time)
    {
        ISenseLib.ISD_GetTrackingData(handle, ref data);
        float[] lastPos  = new float[3];
        float[] lastCbn  = new float[9];
        float   lastTime = -1;

        // Search for first data point after time
        while (data.Station[station - 1].NewData == 1 && data.Station[station - 1].TimeStamp < time)
        {
            // Store old data
            for (int i = 0; i < 3; i = i + 1)
            {
                lastPos[i] = data.Station[station - 1].Position[i];
            }
            for (int i = 0; i < 9; i = i + 1)
            {
                lastCbn[i] = data.Station[station - 1].Cbn[i];
            }
            lastTime = data.Station[station - 1].TimeStamp;
            ISenseLib.ISD_GetTrackingData(handle, ref data);
        }
        // If last data was saved
        if (lastTime > 0)
        {
            float postTime = data.Station[station - 1].TimeStamp - time;
            float prevTime = time - lastTime;

            float postWeight = 1 - (postTime / (postTime + prevTime));
            float prevWeight = 1 - (prevTime / (postTime + prevTime));

            // Approximate data at time by weighing data from immediately before and immediately after

            for (int i = 0; i < 3; i = i + 1)
            {
                data.Station[station - 1].Position[i] = (data.Station[station - 1].Position[i] * postWeight) + (lastPos[i] * prevWeight);
            }
            for (int i = 0; i < 9; i = i + 1)
            {
                data.Station[station - 1].Cbn[i] = (data.Station[station - 1].Cbn[i] * postWeight) + (lastCbn[i] * prevWeight);
            }
            data.Station[station - 1].TimeStamp = time;
        }
    }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            int handle;

            ISenseLib.ISD_TRACKING_DATA_TYPE  data;
            ISenseLib.ISD_STATION_INFO_TYPE[] Station;
            ISenseLib.ISD_TRACKER_INFO_TYPE   Tracker;
            ISenseLib.ISD_HARDWARE_INFO_TYPE  hwInfo;
            IntPtr handy;
            bool   done        = false;
            int    station     = 1;
            uint   maxStations = 8;
            float  lastTime;

            // Detect first tracker. If you have more than one InterSense device and
            // would like to have a specific tracker, connected to a known port,
            // initialized first, then enter the port number instead of 0. Otherwise,
            // tracker connected to the rs232 port with lower number is found first
            handy = new IntPtr();
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Out.WriteLine("Connecting to InterSense tracking device...");
            // handle = ISenseLib.ISD_OpenTracker(IntPtr.Zero, 0, false, true);
            handle = ISenseLib.ISD_OpenAllTrackers(IntPtr.Zero, ref handy, false, true);
            // Check value of handle to see if tracker was located
            if (handle < 1)
            {
                Console.Out.WriteLine("Failed to detect InterSense tracking device");
            }
            else
            {
                Console.Out.WriteLine("Connected; press 'q' to quit, 'e' for enhancement, 'i' to get info\n");

                if (handle > 0)
                {
                    Tracker = new ISenseLib.ISD_TRACKER_INFO_TYPE();
                    hwInfo  = new ISenseLib.ISD_HARDWARE_INFO_TYPE();
                    Station = new ISenseLib.ISD_STATION_INFO_TYPE[8];

                    // Get tracker configuration info
                    ISenseLib.ISD_GetTrackerConfig(handle, ref Tracker, true);

                    if (ISenseLib.ISD_GetSystemHardwareInfo(handle, ref hwInfo))
                    {
                        if (hwInfo.Valid)
                        {
                            maxStations = hwInfo.Cap_MaxStations;
                        }
                    }

                    lastTime = ISenseLib.ISD_GetTime();

                    ISenseLib.ISD_GetStationConfig(handle, ref Station[station - 1], station, true);

                    data = new ISenseLib.ISD_TRACKING_DATA_TYPE();

                    while (!done)
                    {
                        ISenseLib.ISD_GetTrackingData(handle, ref data);

                        if (ISenseLib.ISD_GetTime() - lastTime > 0.02f)
                        {
                            lastTime = ISenseLib.ISD_GetTime();

                            showStationData(handle, Tracker,
                                            Station[station - 1], data.Station[station - 1]);
                        }

                        if (Console.KeyAvailable)
                        {
                            switch (Console.ReadKey(true).KeyChar)
                            {
                            case '1':
                                station = 1;
                                Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                break;

                            case '2':
                                if (maxStations > 1)
                                {
                                    station = 2;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '3':
                                if (maxStations > 2)
                                {
                                    station = 3;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '4':
                                if (maxStations > 3)
                                {
                                    station = 4;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '5':
                                if (maxStations > 4)
                                {
                                    station = 5;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '6':
                                if (maxStations > 5)
                                {
                                    station = 6;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '7':
                                if (maxStations > 6)
                                {
                                    station = 7;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case '8':
                                if (maxStations > 7)
                                {
                                    station = 8;
                                    Console.Write("\n>> Current Station is set to {0:d} <<\n", station);
                                }
                                break;

                            case 'Q':
                            case 'q':
                                done = true;
                                break;

                            case 'I':
                            case 'i':
                                showTrackerStats(handle, ref hwInfo);
                                break;

                            case 'e':     // Set enhancement; IS-x products only, not for InterTrax
                            case 'E':

                                // First get current station configuration
                                if (ISenseLib.ISD_GetStationConfig(handle,
                                                                   ref Station[station - 1], station, true))
                                {
                                    // Cycle enhancement
                                    Station[station - 1].Enhancement =
                                        (Station[station - 1].Enhancement + 1) % 3;

                                    // Send the new configuration to the tracker
                                    if (ISenseLib.ISD_SetStationConfig(handle,
                                                                       ref Station[station - 1], station, true))
                                    {
                                        // display the results
                                        showTrackerStats(handle, ref hwInfo);
                                    }
                                }
                                break;

                            case 't':
                            case 'T':
                                // First get current station configuration
                                if (ISenseLib.ISD_GetStationConfig(handle,
                                                                   ref Station[station - 1], station, true))
                                {
                                    Station[station - 1].TimeStamped = !(Station[station - 1].TimeStamped);

                                    // Send the new configuration to the tracker
                                    if (ISenseLib.ISD_SetStationConfig(handle,
                                                                       ref Station[station - 1], station, true))
                                    {
                                        // display the results
                                        showTrackerStats(handle, ref hwInfo);
                                    }
                                }

                                break;

                            case 'd':
                            case 'D':
                                showTrackerStats(handle, ref hwInfo);
                                break;

                            case 'p':
                            case 'P':

                                // First get current station configuration
                                if (ISenseLib.ISD_GetStationConfig(handle,
                                                                   ref Station[station - 1], station, true))
                                {
                                    // Cycle enhancement
                                    Station[station - 1].Prediction =
                                        (Station[station - 1].Prediction + 10) % 60;

                                    // Send the new configuration to the tracker
                                    if (ISenseLib.ISD_SetStationConfig(handle,
                                                                       ref Station[station - 1], station, true))
                                    {
                                        // display the results
                                        showTrackerStats(handle, ref hwInfo);
                                    }
                                }
                                break;

                            case 's':
                            case 'S':

                                // First get current station configuration
                                if (ISenseLib.ISD_GetStationConfig(handle,
                                                                   ref Station[station - 1], station, true))
                                {
                                    // Cycle enhancement
                                    Station[station - 1].Sensitivity =
                                        (Station[station - 1].Sensitivity + 1) % 5;

                                    if (Station[station - 1].Sensitivity == 0)
                                    {
                                        Station[station - 1].Sensitivity = 1;
                                    }

                                    // Send the new configuration to the tracker
                                    if (ISenseLib.ISD_SetStationConfig(handle,
                                                                       ref Station[station - 1], station, true))
                                    {
                                        // display the results
                                        showTrackerStats(handle, ref hwInfo);
                                    }
                                }
                                break;

                            case 'c':
                            case 'C':

                                // First get current station configuration
                                if (ISenseLib.ISD_GetStationConfig(handle,
                                                                   ref Station[station - 1], station, true))
                                {
                                    // Cycle enhancement
                                    Station[station - 1].Compass =
                                        (Station[station - 1].Compass + 1) % 3;

                                    // Send the new configuration to the tracker
                                    if (ISenseLib.ISD_SetStationConfig(handle,
                                                                       ref Station[station - 1], station, true))
                                    {
                                        // display the results
                                        showTrackerStats(handle, ref hwInfo);
                                    }
                                }
                                break;
                            }
                        }
                    }

                    ISenseLib.ISD_CloseTracker(handle);
                }
            }
        }