//--------------------------------------------------------------------------------------------------------------------------
    // Connection Methods

    protected override bool CanLinkTo(IODevice device)
    {
        if (device != null && device is SenseGloveCs.SenseGlove)
        {
            if (this.connectionMethod == ConnectionMethod.NextGlove)
            {
                return(true);
            }

            SenseGlove glove = ((SenseGlove)device);
            return((glove.IsRight() && this.connectionMethod == ConnectionMethod.NextRightHand) ||
                   (!glove.IsRight() && this.connectionMethod == ConnectionMethod.NextLeftHand));
        }
        return(false);
    }
    //--------------------------------------------------------------------------------------------------------------------------
    // Internal Calibration Algorithms

    #region InternalCalibration

    ///// <summary> Reset the Calibration of the glove if, for instance, something went wrong, or if we are shutting down. </summary>
    //public void CancelCalibration()
    //{
    //    if (linkedGlove != null)
    //    {
    //        linkedGlove.StopCalibration();
    //        SenseGlove_Debugger.Log("Canceled Calibration");
    //    }
    //}



    ///// <summary> Calibrate a variable related to the glove or a solver, with a specific collection method. </summary>
    ///// <param name="whichFingers"></param>
    ///// <param name="simpleCalibration"></param>
    ///// <returns></returns>
    //public bool StartCalibration(CalibrateVariable whatToCalibrate, CollectionMethod howToCollect)
    //{
    //    if (this.linkedGlove != null)
    //    {
    //        return this.linkedGlove.StartCalibration(whatToCalibrate, howToCollect);
    //    }
    //    return false;
    //}


    ///// <summary> Continue the next calibration steps (no reporting of progress) </summary>
    ///// <returns></returns>
    //public bool NextCalibrationStep()
    //{
    //    if (this.linkedGlove != null)
    //        return this.linkedGlove.NextCalibrationStep();
    //    return false;
    //}


    ///// <summary> Fires when the glove's internal calibration finished, which may have come from a worker thread. </summary>
    ///// <param name="source"></param>
    ///// <param name="args"></param>
    //private void LinkedGlove_OnCalibrationFinished(object source, CalibrationArgs args)
    //{
    //    this.ReadyCalibration(new GloveCalibrationArgs(args), true);
    //}


    #endregion InternalCalibration

    #endregion Calibration

    public bool GetInterpolationProfile(out SenseGloveCs.Kinematics.InterpolationSet_IMU set)
    {
        if (linkedGlove != null)
        {
            set = SenseGloveCs.Kinematics.InterpolationSet_IMU.Deserialize(linkedGlove.GetInterpolationValues(),
                                                                           linkedGlove.IsRight()); //reserialize so that we return a copy, and not a direct reference.
        }
        else
        {
            set = null;
        }
        return(set != null);
    }
    /// <summary> Attempt to link a new glove to one of our assigned SenseGlove_Objects. Fire a GloveDetected event afterwards.? </summary>
    /// <param name="glove"></param>
    private void AssignNewGlove(SenseGlove glove)
    {
        if (GetGloveIndex(glove.DeviceID()) < 0)   //It is a new glove
        {
            SenseGlove_DeviceManager.detectedGloves.Add(glove);
            SenseGlove_DeviceManager.gloveLinked.Add(false);

            int  index   = detectedGloves.Count - 1;
            bool linked  = false;
            bool isRight = glove.IsRight();

            //attempt to assign it to existing SenseGlove_Objects?
            for (int i = 0; i < this.senseGloves.Count; i++)
            {
                if (this.senseGloves[i] != null)
                {
                    if (!this.senseGloves[i].IsLinked && SenseGlove_Object.MatchesConnection(isRight, senseGloves[i].connectionMethod))
                    {   //This Sense Glove is elligible for a connection and is not already connected.
                        bool succesfullLink = this.senseGloves[i].LinkToGlove(index);
                        if (succesfullLink)
                        {                                                       //only when it is actually assigned do we break.
                            SenseGlove_DeviceManager.gloveLinked[index] = true; //we linked the glove at Index.
                            linked = true;
                            break;
                        }
                    }
                }
                else
                {   //Warn devs when their SenseGlove_Objects have not been assigned.
                    Debug.LogError("NullRefrence exception occured in " + this.name + ". You likely haven't assigned it via the inspector.");
                }
            }

            if (!linked)
            {
                this.OnGloveDetected(glove, index); //only fire event if the glove was not assigned.
            }
        }
        else
        {
            Debug.LogWarning("GloveDetected event was fired twice for the same device. Most likely you have two instances of DeviceManager running. "
                             + "We reccommend removing duplicate instances.");
        }
    }
Ejemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        if (!standBy)
        {
            if (this.elapsedTime < SenseGlove_Object.setupTime)
            {
                this.elapsedTime += Time.deltaTime;
            }
            else if (this.glove == null) //No connection yet...
            {
                if (this.connectionMethod == ConnectionMethod.HardCoded)
                {
                    //no glove was ever assigned...
                    this.RetryConnection(); //keep trying!
                }
                else
                {
                    SenseGlove myGlove = ExtractSenseGlove(SenseGloveCs.DeviceScanner.GetDevices());
                    if (myGlove != null) //The glove matches our parameters!
                    {
                        this.glove = myGlove;
                        SenseGlove_Manager.SetUsed(this.glove.GetData(false).deviceID, true);
                        this.glove.OnFingerCalibrationFinished += Glove_OnFingerCalibrationFinished;
                    }
                    else
                    {
                        if (this.canReport)
                        {
                            string message = this.gameObject.name + " looking for SenseGlove...";
                            if (this.connectionMethod == ConnectionMethod.FindNextLeftHand)
                            {
                                message = this.gameObject.name + " looking for left-handed SenseGlove...";
                            }
                            else if (this.connectionMethod == ConnectionMethod.FindNextRightHand)
                            {
                                message = this.gameObject.name + " looking for right-handed SenseGlove...";
                            }
                            SenseGlove_Debugger.Log(message);
                            this.canReport = false;
                        }
                        this.elapsedTime = 0;
                    }
                }
            }
            else if (this.connectionMethod == ConnectionMethod.HardCoded && !this.glove.IsConnected())
            {                           //lost connection :(
                this.canReport = true;
                this.RetryConnection(); //keep trying!
            }
            else if (!gloveReady)
            {
                if (this.glove.GetData(false).dataLoaded)
                {
                    bool runSetup = this.gloveData == null; //used to raise event only once!

                    float[][] oldFingerLengths  = null;
                    float[][] oldStartPositions = null;
                    if (this.gloveData != null)
                    {
                        oldFingerLengths  = this.GetFingerLengths();
                        oldStartPositions = SenseGlove_Util.ToPosition(this.GetStartJointPositions());
                    }

                    this.gloveData = this.glove.GetData(false); //get the latest data without calculating anything.
                    this.rightHand = this.gloveData.isRight;    //so that users can use this variable during setup.

                    if (oldFingerLengths != null)
                    {
                        this.SetFingerLengths(oldFingerLengths);
                    }                                                                          //re-apply old fingerlengths, if possible.
                    if (oldStartPositions != null)
                    {
                        this.SetStartJointPositions(oldStartPositions);
                    }                                                                                  //re=apply joint positions, if possible.

                    this.convertedGloveData = new SenseGlove_Data(this.gloveData, this.glove.communicator.samplesPerSecond,
                                                                  this.glove.TotalCalibrationSteps(), this.glove.TotalCalibrationSteps());
                    this.SetupWrist();
                    this.calibratedWrist = false;
                    this.gloveReady      = true;
                    if (runSetup)
                    {
                        this.originalLengths = this.gloveData.handModel.GetFingerLengths();
                        this.originalJoints  = this.gloveData.handModel.GetJointPositions();
                        SenseGlove_Debugger.Log("Sense Glove " + this.convertedGloveData.deviceID + " is ready!");
                        this.GloveLoaded();  //raise the event!
                    }
                }
            }
            else //glove != null && gloveReady!
            {
                this.CheckCalibration();

                //Update to the latest GloveData.
                this.UpdateGloveData();

                //Calibrate once more after reconnecting to the glove.
                if (!calibratedWrist)
                {
                    this.CalibrateWrist();
                    this.calibratedWrist = true;
                }

                //Update the public values automatically.
                if (connectionMethod != ConnectionMethod.HardCoded)
                {
                    this.address = glove.communicator.Address();
                }
                this.rightHand = glove.IsRight();
            }
        }
    }
 /// <summary> Create a new instance of the GloveDetectedArgs. </summary>
 /// <param name="glove"></param>
 public GloveDetectedArgs(SenseGlove glove, int gloveIndex)
 {
     this.DeviceID    = glove.GetData(false).deviceID;
     this.RightHanded = glove.IsRight();
     this.DeviceIndex = gloveIndex;
 }