Jump to content

Recommended Posts

Posted (edited)

Hi All, 

I am fairly new to VR eye tracking and have run into a block, I am trying to output some data into a file and every time that I test this out Unity seems to freeze up I also get errors with System.IO and I could really use some help.

(I have included my code to make it easier) 

Thank you in advance

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
using System.IO;
using Valve.VR;
using Valve.VR.InteractionSystem;
using ViveSR.anipal.Eye;
using System.Runtime.InteropServices;
using TMPro;

public class TrackPlayer : MonoBehaviour
{
    // Object references for use
    public GameObject playerObj = null;
    public GameObject attractorRef = null;
    public Rigidbody attractorrb;
    public Camera playercamera;

    // Participant Order
    public string UserID; 
    public  static string participantName;
    public static string Path = Directory.GetCurrentDirectory();
    string File_Path;

    // Distance between variable 
    public float DistanceBetweenAandB;

    // Attractor animation controls
    private float speed = 0f;
    public Animator deerAnimator;
    
    // Eye data variables 
    private static EyeData eyeData = new EyeData();
    private static bool eye_callback_registered = false;

    //Debug text display
    public TMP_Text uiText;
    private float updateSpeed = 0;
    private static float lastTime, currentTime;

    // Gaze Directon varibales 
    private static Vector3 Eyeposition;

    // 
    private static UInt64 eye_valid_L, eye_valid_R;                 // The bits explaining the validity of eye data.
    private static float openness_L, openness_R;                    // The level of eye openness.
    private static float pupil_diameter_L, pupil_diameter_R;        // Diameter of pupil dilation.
    private static Vector2 pos_sensor_L, pos_sensor_R;              // Positions of pupils.

    // Focus information vairables 
    private static Ray testRay;
    private static FocusInfo focusInfo;

    // Variables that control how fast to report data back 
    private float nextActionTime = 0.0f;
    public float period = .08f;
    private static int cnt_callback = 0;
    private const int maxframe_count = 120 * 1800;

    private void Start()
    {
     UserID = participantName;
     SRanipal_Eye_v2.LaunchEyeCalibration();     // Perform calibration for eye tracking just before running actual experiement.
        
        File_Path = Directory.GetCurrentDirectory() + "\\Test_" + participantName + ".txt";
        Debug.Log(File_Path);
        if (participantName == "")
        {
            participantName = "Blank"; // make sure to fill in particpant ID 
        }
        if (File.Exists(File_Path))
        {
            Debug.Log("File with the same UserID already exists. Please change the UserID in the C# code.");

            //  When the same file name is found, we stop playing Unity.

            if (UnityEditor.EditorApplication.isPlaying)
            {
                UnityEditor.EditorApplication.isPlaying = false;
            }
        }

    }

    // Update is called once per frame
    void Update()
    {
        //PlayerPositionTracking();


        if (SRanipal_Eye_Framework.Status != SRanipal_Eye_Framework.FrameworkStatus.WORKING) 
            return;

        if (SRanipal_Eye_Framework.Instance.EnableEyeDataCallback == true && eye_callback_registered == false)
        {
            SRanipal_Eye.WrapperRegisterEyeDataCallback(Marshal.GetFunctionPointerForDelegate((SRanipal_Eye.CallbackBasic)EyeCallback));
            eye_callback_registered = true;
        }
        else if (SRanipal_Eye_Framework.Instance.EnableEyeDataCallback == false && eye_callback_registered == true)
        {
            SRanipal_Eye.WrapperUnRegisterEyeDataCallback(Marshal.GetFunctionPointerForDelegate((SRanipal_Eye.CallbackBasic)EyeCallback));
            eye_callback_registered = false;
        }

            Vector3 GazeOriginCombinedLocal, GazeDirectionCombinedLocal;
        
            if (eye_callback_registered)
            {
                if (SRanipal_Eye.GetGazeRay(GazeIndex.COMBINE, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal, eyeData)) { }
                else if (SRanipal_Eye.GetGazeRay(GazeIndex.LEFT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal, eyeData)) { }
                else if (SRanipal_Eye.GetGazeRay(GazeIndex.RIGHT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal, eyeData)) { }
                else return;
            }
            else
            {
                if (SRanipal_Eye.GetGazeRay(GazeIndex.COMBINE, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { }
                else if (SRanipal_Eye.GetGazeRay(GazeIndex.LEFT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { }
                else if (SRanipal_Eye.GetGazeRay(GazeIndex.RIGHT, out GazeOriginCombinedLocal, out GazeDirectionCombinedLocal)) { }
                else return;
            }

            Eyeposition = Camera.main.transform.TransformDirection(GazeDirectionCombinedLocal);
            Focus();
            FocusName();



        if (Time.time > nextActionTime)
        {
            nextActionTime += period;

            //Debug.Log(nextActionTime);

            // execute block of code here
            //uiText.text = Eyeposition.ToString();

            //PlayerPositionTracking();
            //AddPlayerEYELocationDataToFile();
            AddPlayerLocationDataToFile();
            AddCueLocationDataToFile();

            //Debug.Log(FocusName());

            //PLAYER HEAD ROTATION
            //Debug.Log("Player X ROTATION: " + playercamera.transform.rotation.x);
            //Debug.Log("Player Y ROTATION: " + playercamera.transform.rotation.y);


        }





    }

    private void OnDisable()
    {
        Release();
    }

    void OnApplicationQuit()
    {
        Release();
    }
    /// <summary>
    /// Release callback thread when disabled or quit
    /// </summary>
    private static void Release()
    {
        if (eye_callback_registered == true)
        {
            SRanipal_Eye.WrapperUnRegisterEyeDataCallback(Marshal.GetFunctionPointerForDelegate((SRanipal_Eye.CallbackBasic)EyeCallback));
            eye_callback_registered = false;
        }
    }

    /// <summary>
    /// Required class for IL2CPP scripting backend support
    /// </summary>
    internal class MonoPInvokeCallbackAttribute : System.Attribute
    {
        public MonoPInvokeCallbackAttribute() { }
    }

    /// <summary>
    /// Eye tracking data callback thread.
    /// Reports data at ~120hz
    /// MonoPInvokeCallback attribute required for IL2CPP scripting backend
    /// </summary>
    /// <param name="eye_data">Reference to latest eye_data</param>
    [MonoPInvokeCallback]
    private static void EyeCallback(ref EyeData eye_data)
    {
        eyeData = eye_data;
        // do stuff with eyeData...
        while (cnt_callback < maxframe_count)
        {
            openness_L = eyeData.verbose_data.left.eye_openness;
            openness_R = eyeData.verbose_data.right.eye_openness;
            pupil_diameter_L = eyeData.verbose_data.left.pupil_diameter_mm;
            pupil_diameter_R = eyeData.verbose_data.right.pupil_diameter_mm;
            pos_sensor_L = eyeData.verbose_data.left.pupil_position_in_sensor_area;
            pos_sensor_R = eyeData.verbose_data.right.pupil_position_in_sensor_area;
            eye_valid_L = eyeData.verbose_data.left.eye_data_validata_bit_mask;
            eye_valid_R = eyeData.verbose_data.right.eye_data_validata_bit_mask;


            lastTime = currentTime;
            currentTime = eyeData.timestamp;



            //Player location variables and arrays
            string playerEyeX = Eyeposition.x.ToString();
            string playerEyeY = Eyeposition.y.ToString();
            string playerEyeZ = Eyeposition.z.ToString();
            string playerEyeOpen_Left = openness_L.ToString();
            string playerEyeOpen_Right = openness_R.ToString();
            string playerEyeDiameter_Left = pupil_diameter_L.ToString();
            string playerEyeDiameter_Right = pupil_diameter_R.ToString();
            string playerEyePosInSenSorArea_Left_X = pos_sensor_L.x.ToString();
            string playerEyePosInSenSorArea_Left_Y = pos_sensor_L.y.ToString();
            string playerEyePosInSenSorArea_Right_X = pos_sensor_R.x.ToString();
            string playerEyePosInSenSorArea_Right_Y = pos_sensor_R.y.ToString();
            string playerEyeValid_Left = eye_valid_L.ToString();
            string playerEyeValid_Right = eye_valid_R.ToString();


            //string objectBeingLookedAt = FocusName();


            string value =
            playerEyeX + "," +
            playerEyeY + "," +
            playerEyeZ + "," +
            playerEyeOpen_Left + "," +
            playerEyeOpen_Right + "," +
            playerEyeDiameter_Left + "," +
            playerEyeDiameter_Right + "," +
            playerEyePosInSenSorArea_Left_X + "," +
            playerEyePosInSenSorArea_Left_Y + "," +
            playerEyePosInSenSorArea_Right_X + "," +
            playerEyePosInSenSorArea_Right_Y + "," +
            playerEyeValid_Left + "," +
            playerEyeValid_Right + "," +
            //objectBeingLookedAt +
            DateTime.Now.ToString() +
            Environment.NewLine;



            File.AppendAllText("Test_" + participantName + ".txt", value);
            cnt_callback++;


            //string[] playerEYEData = { "\n" + "X = " + playerEyeX + ",", "" + "Y = " + playerEyeY + ",", "" + "Z = " + playerEyeZ + ",",
            //"" + "Eye Openess_L = " + playerEyeOpen_Left + ",", "" + "Eye Openess_R = " + playerEyeOpen_Right + ",",
            //"" + "Eye Diameter_L = " + playerEyeDiameter_Left + ",", "" + "Eye Diameter_R = " + playerEyeDiameter_Right + ",",
            //"" + "Eye Position In Sense Area_Left_X = " + playerEyePosInSenSorArea_Left_X + ",", "" + "Eye Position In Sense Area_Left_Y = " + playerEyePosInSenSorArea_Left_Y + ",",
            //"" + "Eye Position In Sense Area_Right_X = " + playerEyePosInSenSorArea_Right_X + ",", "" + "Eye Position In Sense Area_Right_Y = " + playerEyePosInSenSorArea_Right_Y + ",",
            //"" + "Eye Validation Bit Mask_Left = " + playerEyeValid_Left + ",",
            //"" + "Eye Validation Bit Mask_Rigth = " + playerEyeValid_Right + ",",
            //"" + "OBJECT = " + objectBeingLookedAt + "," + " " + System.DateTime.Now.ToString() };
            // Content of the file 

            // foreach (string playerInfo in playerEYEData)
            // {
            //File.AppendAllText(path, playerInfo);

            //}


        }

    }

/// <summary>
/// Checks for current object in focus.
/// </summary>
public static GameObject Focus()
{
    if (eye_callback_registered == true)
    {
        if (SRanipal_Eye.Focus(GazeIndex.COMBINE, out testRay, out focusInfo)) { }
        else if (SRanipal_Eye.Focus(GazeIndex.LEFT, out testRay, out focusInfo)) { }
        else if (SRanipal_Eye.Focus(GazeIndex.RIGHT, out testRay, out focusInfo)) { }
        else return null;
    }
    if(focusInfo.collider.name != "Body Collider")
        {
            return focusInfo.collider.gameObject;
        }
    else return null;
}
/// <summary>
/// Checks name for current object in focus.
/// </summary>
public static string FocusName()
{
    if (Focus() is null)
        return "";
    else
        return Focus().name;
}

 

TrackPlayer.cs

Edited by Kodiak

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...