Search Unity

Resolved How to access Sensor values and display them?

Discussion in 'VR' started by AugustoGartner, Dec 22, 2020.

  1. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Hi guys,

    I am trying to make a really simple App, where the user can see the sensor values from the sensors inside Oculus Quest in real-time. Does anyone have any idea how to access those values? I searched all over and it seems nobody ever tried to do such thing.

    I tried to use the variables used in OVR Manager Script, but it didn't work (compiler always threw some errors).
    Since I am pretty new to this whole thing, I would be really grateful if someone could post a simple Script where the values are being accessed and displayed as text.

    Thank you very much and happy christmas :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Which sensors are you talking about? Do you have reason to believe we have access to those at all?
     
  3. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    I am talking about accelerometer and gyroscope. Well I don't have reason to believe we can't access it.
    I believe Headtracking is done with those values. Somewhere must be a script that says when I turn my head to the left, the picture in the VR should also turn to the left. The only thing is that I understand to little about those scripts used in unity, to recognize those values.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  5. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Thank you very much! I will take a look at it :)
     
  6. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Unfortunately I couldn't really achieve anything with the types above. I don't understand how I can implement them in my code, so that I get a value I can display as Text.

    I tried accessing the gyroscope values with
    Input.gyro.rotationRate
    for instance, but I all I get is a value of 0. I believe the gyroscope is not enabled although I explicitly say
    Input.gyro.enabled = true
    in my code.

    I am building it to an Oculus Quest.

    Could anyone help me?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.  
    5. public class SensorValues : MonoBehaviour
    6. {
    7.     public Text gyro;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.         Input.gyro.enabled = true;
    13.  
    14.     }
    15.     void Update()
    16.     {
    17.        Vector3 gyro_value = Input.gyro.rotationRateUnbiased;
    18.  
    19.         gyro.text = gyro_value.ToString();
    20.  
    21.     }
    22.  
    23. }
    24.  
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Did you actually read the documentation in the first link I posted? It gives sample code for various things you may need to do, including accessing feature values:

    Code (CSharp):
    1. bool triggerValue;
    2. if (device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.triggerButton, out triggerValue) && triggerValue)
    3. {
    4.     Debug.Log("Trigger button is pressed.");
    5. }
    Your code should look like that (just with a different data type and feature identifier).
     
  8. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Hi JoeStrout,

    I did read it, but as I said I didn't quite understand how to apply it to my case. I tried something like this:

    var Devices = new List<UnityEngine.XR.InputDevice>();
    float AccValue;
    foreach (var device in Devices)
    if (device.TryGetFeatureValue(UnityEngine.XR.CommonUsages.deviceAcceleration, out AccValue) && AccValue)
    {
    Debug.Log(AccValue);

    }


    but I got two errors from it, to which I didn't find any solution:

    Assets\SensorValues.cs(48,24): error CS1061: 'InputDevice' does not contain a definition for 'TryGetFeatureValue' and no accessible extension method 'TryGetFeatureValue' accepting a first argument of type 'InputDevice' could be found (are you missing a using directive or an assembly reference?)

    Assets\SensorValues.cs(48,43): error CS0234: The type or namespace name 'CommonUsages' does not exist in the namespace 'UnityEngine.XR' (are you missing an assembly reference?)

    Are you familiar with this kind of error?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm, that second error is a concern. It suggests that you're using a version of Unity that doesn't match the docs we're working from. (Unity's XR stuff has unfortunately been in a lot of flux, and changes substantially from version to version.)

    Check your Unity version, and then make sure you're working from the corresponding version of the documentation.
     
  10. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Ok thank you. I was on version 2018. I changed to 2019 and now it works.
    My code looks like this:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.XR;
    7.  
    8. public class valuesDisplay : MonoBehaviour
    9. {
    10.     public Text value1;
    11.     public Text value2;
    12.     public Text value3;
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         Input.gyro.enabled = true;
    19.  
    20.     }
    21.     void Update()
    22.     {
    23.  
    24.         value1.text = InputTracking.GetLocalRotation(XRNode.Head).ToString("+#.00;-#.00");
    25.  
    26.         if (Input.gyro.enabled = true)
    27.         {
    28.             value2.text = InputTracking.GetLocalRotation(XRNode.RightHand).ToString("+#.00;-#.00");
    29.         }
    30.         else
    31.         {
    32.             value2.text = "No Gyro";
    33.         }
    34.  
    35.         value3.text = CommonUsages.centerEyeAcceleration.ToString();
    36.  
    37.     }
    38. }
    39.  
    40.  
    And what I see in my App I uploaded as a Picture.

    The whole Text " UnityEngine.XR.InputFeaturesUsage'1[[Unity.Engine.Vector3,Unity.Engine.CoreModule, Version = 0.0.0.0, Culture = neutral, PublicKeyToken=null]]

    What does it mean??
     

    Attached Files:

  11. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
  12. LeninRaul

    LeninRaul

    Joined:
    Jan 27, 2021
    Posts:
    2
    Hi, Augusto.
    I'm currently trying to do the same thing you achieved on this post. Quick question, have you tried accesing to the sensors data from both the HMD and the controllers?

    Greetings, Lenin.
     
  13. AugustoGartner

    AugustoGartner

    Joined:
    Dec 7, 2020
    Posts:
    18
    Hi LeninRaul,

    sorry for the late reply. At first I tried accessing only HMD values and then from the controllers. I am still not able to access the raw sensor values. Instead I went for the Local Rotation of the device.
    Example: InputTracking.GetLocalRotation(XRNode.Head)

    Everytime I get a warning that this API is obsolete, but I was not able to get to this values with the recommended features "InputDevice.TryGetFeatureValue" and "CommonUsages.deviceRotation".

    If you are able to get to the raw values, please tell me :)
     
  14. LeninRaul

    LeninRaul

    Joined:
    Jan 27, 2021
    Posts:
    2
    No worries!

    it took me some time to understand how the "TryGetFeatureValue" worked and this is what I did, hope it helps


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.XR;
    6. using UnityEngine.UI;
    7.  
    8.  
    9. public class Track : MonoBehaviour
    10. {
    11.     [SerializeField]
    12.  
    13.     public Text valores;
    14.  
    15.     private XRNode lxRNode = XRNode.LeftHand;
    16.  
    17.     private List<InputDevice> devices = new List<InputDevice>();
    18.     private InputDevice device;
    19.  
    20.     private bool devicePositionChosen;
    21.     private Vector3 devicePositionValue = Vector3.zero;
    22.     private Vector3 prevdevicePositionValue;
    23.  
    24.     //Access to the hardware device and gets its information saving it in the variable device
    25.     void GetDevice()
    26.     {
    27.         InputDevices.GetDevicesAtXRNode(lxRNode, devices);
    28.         device = devices[0];
    29.  
    30.     }
    31.  
    32.     // Checks if the device is enable, if not it takes action and calls the GetDevice function
    33.     void OnEnable()
    34.     {
    35.         if (!device.isValid)
    36.         {
    37.             GetDevice();
    38.         }
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         if (!device.isValid)
    44.         {
    45.             GetDevice();
    46.         }
    47.  
    48.                 // capturing position changes
    49.         InputFeatureUsage<Vector3> devicePositionsUsage = CommonUsages.devicePosition;
    50.                 // make sure the value is not zero and that it has changed
    51.         if (devicePositionValue != prevdevicePositionValue)
    52.         {
    53.             devicePositionChosen = false;
    54.         }
    55.    
    56.         if (device.TryGetFeatureValue(devicePositionsUsage, out devicePositionValue) && devicePositionValue != Vector3.zero && !devicePositionChosen)
    57.         {
    58.             valores.text = devicePositionValue.ToString("F3");
    59.             prevdevicePositionValue = devicePositionValue;
    60.             devicePositionChosen = true;
    61.         }
    62.         else if (devicePositionValue == Vector3.zero && devicePositionChosen)
    63.         {
    64.             valores.text = devicePositionValue.ToString("F3");
    65.             prevdevicePositionValue = devicePositionValue;
    66.             devicePositionChosen = false;
    67.         }
    68.     }
    69. }
    70.  
     
  15. SuperGenesis1985

    SuperGenesis1985

    Joined:
    Dec 8, 2019
    Posts:
    4
    I'm looking into to doing something similar -- getting the raw sensor data from the Oculus Quest 2 headset.
    I want to export the values to a robot camera that sits on a remote-control car for a 1st person experience while driving an RC.

    Do you guys (Lennin or Augusto) have any advice with your work in the past?
     
  16. unity_018D0158D140CCA36066

    unity_018D0158D140CCA36066

    Joined:
    Aug 11, 2022
    Posts:
    1
    Hi SuperGenesis1985,

    Have you had any sucess? I'm also looking at a similar problem, and haven't been able to find any good solutions online.
     
  17. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    is possible access oculus quest proximity sensor from openXR?