Beispiel #1
0
    /// <summary>
    /// Add a new EMG sensor
    /// </summary>
    public void AddSensor()
    {
        // Create and add sensor
        ThalmicMyobandManager emgSensor = new ThalmicMyobandManager();

        AvatarSystem.AddActiveSensor(emgSensor);

        // Get prosthesis and add sensor when tH
        if (AvatarSystem.AvatarType == AvatarType.Transhumeral)
        {
            GameObject prosthesisManagerGO        = GameObject.FindGameObjectWithTag("ProsthesisManager");
            ConfigurableElbowManager elbowManager = prosthesisManagerGO.GetComponent <ConfigurableElbowManager>();
            elbowManager.AddSensor(emgSensor);
        }

        else if (AvatarSystem.AvatarType == AvatarType.Transradial)
        {
            GameObject      prosthesisManagerGO = GameObject.FindGameObjectWithTag("ProsthesisManager");
            FakeEMGBoniHand prosthesisManager   = prosthesisManagerGO.GetComponent <FakeEMGBoniHand>();
            prosthesisManager.InitialiseInputSystem(emgSensor);
        }

        // Return to settings menu.
        addSensorMenu.ReturnToSettingsMenu();
    }
    /// <summary>
    /// Add a new EMG sensor
    /// </summary>
    public void AddSensor()
    {
        if (ip == null || ip == string.Empty || port == 0 || channelSize <= 0)
        {
            StartCoroutine(DisplayInformationOnLog(3.0f, "The provided sensor info is invalid."));
        }

        try
        {
            // Create and add sensor
            EMGWiFiManager emgSensor = new EMGWiFiManager(ip, port, channelSize, isRaw);
            AvatarSystem.AddActiveSensor(emgSensor);

            // Get prosthesis and add sensor when tH
            if (AvatarSystem.AvatarType == AvatarType.Transhumeral)
            {
                GameObject prosthesisManagerGO        = GameObject.FindGameObjectWithTag("ProsthesisManager");
                ConfigurableElbowManager elbowManager = prosthesisManagerGO.GetComponent <ConfigurableElbowManager>();
                elbowManager.AddSensor(emgSensor);
            }

            else if (AvatarSystem.AvatarType == AvatarType.Transradial)
            {
                GameObject      prosthesisManagerGO = GameObject.FindGameObjectWithTag("ProsthesisManager");
                FakeEMGBoniHand prosthesisManager   = prosthesisManagerGO.GetComponent <FakeEMGBoniHand>();
                prosthesisManager.InitialiseInputSystem(emgSensor);
            }

            // Return to settings menu.
            addSensorMenu.ReturnToSettingsMenu();
        }
        // 10048 address duplicate
        catch (SocketException e) when(e.ErrorCode == 10048)
        {
            StartCoroutine(DisplayInformationOnLog(3.0f, "IP address already in use."));
        }
        // 10045 port duplicate
        catch (SocketException e) when(e.ErrorCode == 10045)
        {
            StartCoroutine(DisplayInformationOnLog(3.0f, "Port already in use."));
        }
        catch (System.Exception e)
        {
            StartCoroutine(DisplayInformationOnLog(3.0f, "An error occured while adding the sensor.\nError message: " + e.Message));
        }
    }
    private void LoadDebugAvatar()
    {
        // Load avatar
        if (avatarType == AvatarType.Transhumeral)
        {
            AvatarSystem.LoadAvatar(SaveSystem.ActiveUser, AvatarType.Transhumeral);

            // Find the residual limb and change the follower
            GameObject   residualLimbGO = GameObject.FindGameObjectWithTag("ResidualLimbAvatar");
            LimbFollower limbFollower   = residualLimbGO.GetComponent <LimbFollower>();
            Destroy(limbFollower);
            AngleFollower angleFollower = residualLimbGO.AddComponent <AngleFollower>();
            angleFollower.fixedTransform = fixedProsthesisPosition;


            // Initialize prosthesis
            GameObject prosthesisManagerGO        = GameObject.FindGameObjectWithTag("ProsthesisManager");
            ConfigurableElbowManager elbowManager = prosthesisManagerGO.AddComponent <ConfigurableElbowManager>();
            elbowManager.InitializeProsthesis(SaveSystem.ActiveUser.upperArmLength, (SaveSystem.ActiveUser.forearmLength + SaveSystem.ActiveUser.handLength / 2.0f));
            // Set the reference generator to jacobian-based.
            elbowManager.ChangeReferenceGenerator("VAL_REFGEN_JACOBIANSYN");
            instructionsText = synergyInstructions;

            // Enable & configure EMG
            if (emgEnable)
            {
                // Create and add sensor
                //EMGWiFiManager emgSensor = new EMGWiFiManager(ip, port, channelSize);
                ThalmicMyobandManager emgSensor = new ThalmicMyobandManager();
                //emgSensor.ConfigureLimits(0, 1023, 0);
                //emgSensor.ConfigureLimits(1, 1023, 0);
                AvatarSystem.AddActiveSensor(emgSensor);
                elbowManager.AddSensor(emgSensor);
                //emgSensor.StartSensorReading();

                // Set active sensor and reference generator to EMG.
                elbowManager.ChangeSensor("VAL_SENSOR_SEMG");
                elbowManager.ChangeReferenceGenerator("VAL_REFGEN_EMGPROP");
                instructionsText = emgInstructions;
            }
        }
        else
        {
            throw new System.NotImplementedException();
        }
    }
    // Update is called once per frame
    void Update()
    {
        switch (experimentState)
        {
        /*
         *************************************************
         *  HelloWorld
         *************************************************
         */
        // Welcome subject to the virtual world.
        case ExperimentState.Welcome:
            // Load avatar
            if (avatarType == AvatarType.AbleBodied)
            {
                AvatarSystem.LoadAvatar(SaveSystem.ActiveUser, AvatarType.AbleBodied);
            }
            else if (avatarType == AvatarType.Transhumeral)
            {
                AvatarSystem.LoadAvatar(SaveSystem.ActiveUser, AvatarType.Transhumeral);

                // Initialize prosthesis
                GameObject prosthesisManagerGO        = GameObject.FindGameObjectWithTag("ProsthesisManager");
                ConfigurableElbowManager elbowManager = prosthesisManagerGO.AddComponent <ConfigurableElbowManager>();
                elbowManager.InitializeProsthesis(SaveSystem.ActiveUser.upperArmLength, (SaveSystem.ActiveUser.forearmLength + SaveSystem.ActiveUser.handLength / 2.0f));
                // Set the reference generator to jacobian-based.
                elbowManager.ChangeReferenceGenerator("VAL_REFGEN_JACOBIANSYN");

                // Enable & configure EMG
                if (emgEnable)
                {
                    // Create and add sensor
                    EMGWiFiManager emgSensor = new EMGWiFiManager(ip, port, channelSize);
                    AvatarSystem.AddActiveSensor(emgSensor);
                    elbowManager.AddSensor(emgSensor);
                    emgSensor.StartSensorReading();

                    // Set active sensor and reference generator to EMG.
                    elbowManager.ChangeSensor("VAL_SENSOR_SEMG");
                    elbowManager.ChangeReferenceGenerator("VAL_REFGEN_EMGPROP");
                }
            }

            experimentState = ExperimentState.Initialising;
            break;

        /*
         *************************************************
         *  InitializingApplication
         *************************************************
         */
        // Perform initialization functions before starting experiment.
        case ExperimentState.Initialising:
            //
            // Perform experiment initialization procedures
            //

            //
            // Initialize data logs
            //

            //
            // Go to training
            //
            experimentState = ExperimentState.Training;
            break;

        /*
         *************************************************
         *  Practice
         *************************************************
         */
        // Perform initialization functions before starting experiment.
        case ExperimentState.Training:
            //
            // Guide subject through training
            //

            //
            // Go to instructions
            //
            experimentState = ExperimentState.Instructions;
            break;

        /*
         *************************************************
         *  GivingInstructions
         *************************************************
         */
        case ExperimentState.Instructions:
            // Skip instructions when repeating sessions
            if (SkipInstructions)
            {
                HudManager.DisplayText("Move to start", 2.0f);
                // Turn targets clear
                experimentState = ExperimentState.WaitingForStart;
                break;
            }

            //
            // Give instructions
            //

            //
            // Go to waiting for start
            //
            HudManager.DisplayText("Move to start", 2.0f);
            // Turn targets clear
            experimentState = ExperimentState.WaitingForStart;

            break;

        /*
         *************************************************
         *  WaitingForStart
         *************************************************
         */
        case ExperimentState.WaitingForStart:

            // Check if pause requested
            UpdatePause();
            switch (waitState)
            {
            // Waiting for subject to get to start position.
            case WaitState.Waiting:
                SetWaitFlag(3.0f);
                waitState = WaitState.Countdown;
                break;

            case WaitState.Countdown:
                if (WaitFlag)
                {
                    experimentState = ExperimentState.PerformingTask;
                }
                break;

            default:
                break;
            }
            break;

        /*
         *************************************************
         *  PerformingTask
         *************************************************
         */
        case ExperimentState.PerformingTask:
            // Task performance is handled deterministically in FixedUpdate.
            break;

        /*
         *************************************************
         *  AnalizingResults
         *************************************************
         */
        case ExperimentState.AnalizingResults:
            // Allow 3 seconds after task end to do calculations
            SetWaitFlag(3.0f);

            //
            // Data analysis and calculations
            //

            //
            // System update
            //

            //
            // Data logging
            //

            //
            // Flow managment
            //
            // Rest for some time when required
            if (IsRestTime())
            {
                SetWaitFlag(RestTime);
                experimentState = ExperimentState.Resting;
            }
            // Check whether the new session condition is met
            else if (IsEndOfSession())
            {
                experimentState = ExperimentState.InitializingNext;
            }
            // Check whether the experiment end condition is met
            else if (IsEndOfExperiment())
            {
                experimentState = ExperimentState.End;
            }
            else
            {
                experimentState = ExperimentState.UpdatingApplication;
            }
            break;

        /*
         *************************************************
         *  UpdatingApplication
         *************************************************
         */
        case ExperimentState.UpdatingApplication:
            if (WaitFlag)
            {
                //
                // Update iterations and flow control
                //

                //
                // Update log requirements
                //

                //
                //
                // Go to start of next iteration
                experimentState = ExperimentState.WaitingForStart;
            }
            break;

        /*
         *************************************************
         *  InitializingNext
         *************************************************
         */
        case ExperimentState.InitializingNext:
            //
            // Perform session closure procedures
            //

            //
            // Initialize new session variables and flow control
            //
            iterationNumber = 1;
            sessionNumber++;
            skipInstructions = true;

            //
            // Initialize data logging
            //
            //ExperimentSystem.GetActiveLogger(1).AddNewLogFile(sessionNumber, iterationNumber, "Data format");

            experimentState = ExperimentState.Initialising;     // Initialize next session
            break;

        /*
         *************************************************
         *  Resting
         *************************************************
         */
        case ExperimentState.Resting:
            //
            // Check for session change or end request from experimenter
            //
            if (UpdateNext())
            {
                ConfigureNextSession();
                break;
            }
            else if (UpdateEnd())
            {
                EndExperiment();
                break;
            }
            //
            // Restart after flag is set by wait coroutine
            //
            if (WaitFlag)
            {
                HudManager.DisplayText("Get ready to restart!", 3.0f);
                SetWaitFlag(5.0f);
                experimentState = ExperimentState.UpdatingApplication;
                break;
            }
            break;

        /*
         *************************************************
         *  Paused
         *************************************************
         */
        case ExperimentState.Paused:
            //
            // Check for session change or end request from experimenter
            //
            UpdatePause();
            if (UpdateNext())
            {
                ConfigureNextSession();
                break;
            }
            else if (UpdateEnd())
            {
                EndExperiment();
                break;
            }
            break;

        /*
         *************************************************
         *  End
         *************************************************
         */
        case ExperimentState.End:
        //
        // Update log data and close logs.
        //

        //
        // Return to main menu
        //
        default:
            break;
        }

        //
        // Update information displayed on monitor
        //

        //
        // Update information displayed for debugging purposes
        //
        if (debug)
        {
            debugText.text = experimentState.ToString() + "\n";
            if (experimentState == ExperimentState.WaitingForStart)
            {
                debugText.text += waitState.ToString() + "\n";
            }
        }
    }