Getting Analog Input

Something's not clear? Encountered a bug or an error? This is the place to report.
christop
Posts: 3
Joined: Mon Feb 06, 2017 9:13 am

Getting Analog Input

Postby christop » Mon Feb 06, 2017 9:19 am

Hi
i am trying to get some analog input from a 3.5V joystick. I connected the x/y of the joystick to A1.A2.
The 3.5 and GND to the respective pins on the breadboard extension of the Red Gem.
Inside Unity i query the value regularly.
The problem is that the values i read out change don't seem to be right and change with a significant delay.
I attach a photo of my connection and the unity src.
Do i do anything wrong? Is the analog port supported?
16506867_1324589014229118_327811814_n.jpg
16506867_1324589014229118_327811814_n.jpg (59.13 KiB) Viewed 2915 times


Code: Select all

public class ReadRedGem : MonoBehaviour {

   private IGem gem;
   public Text fireStatusText;
   public Text connectionStatusText;
   public Text JoystickHorizontalText;
   public Text JoystickVerticalText;

   // Use this for initialization
   void Start () {

      if (Application.platform == RuntimePlatform.Android) {
         //To get gem by number instead of address on Android the Gem should be paired to Gem SDK Utility app
         //Also it is possible to get the gem by address
         gem = GemManager.Instance.GetGem(0);
      } else if (Application.platform == RuntimePlatform.WindowsPlayer ||
                Application.platform == RuntimePlatform.WindowsEditor) {
         //On Windows it is possible to connect only by address
         string Address = ""; //For example "00:80:98:DC:9F:CD"

         if (string.IsNullOrEmpty(Address)) {
            Debug.LogError("Please specify the address for gem to connect");
         }
         gem = GemManager.Instance.GetGem(Address);
      }

      // Check if gem is available before doing anything else
      if(gem != null) {
         ConfigExtensions();
      }
   }

   void FixedUpdate () {
      if(gem != null) {

         // Read value on digital pin0
         gem.ReadDigital(DigitalPin.D0);

         // Read value of analog ports
         gem.ReadAnalog(AnalogPin.A1);
         gem.ReadAnalog(AnalogPin.A2);

         gem.CalibrateAzimuth();
      }
   }

   private void ConfigExtensions() {
      // Enable extensions for the digital and analog ports
      gem.ExtensionsActive = true;

      // Set the Events for state and port changing
      gem.OnStateChanged += (GemState state) => {
         // Check if the gem device is connected
         if (state == GemState.Connected) {
            connectionStatusText.text = "Connected";
            ConfigDigitalPorts();
         } else {
            connectionStatusText.text = "Disconnected";
         }
      };

      gem.OnDigital += (DigitalPinData data) => {
         if(data.Pin == DigitalPin.D0) {
            fireStatusText.text = data.Value.ToString();
         }
      };

      gem.OnAnalog += (AnalogPinData data) => {

         if(data.Pin == AnalogPin.A1) {
            JoystickHorizontalText.text = "Vert: " + data.Value.ToString();
         }
         if (data.Pin == AnalogPin.A2) {
            JoystickVerticalText.text = "Horz: " + data.Value.ToString();
         }
      };
   }

   // Config digital ports
   private void ConfigDigitalPorts() {
      gem.ConfigDigital(DigitalPin.D0, PinConfig.Input);    // D0 as input
      gem.ConfigDigital(DigitalPin.D0, PinConfig.InputNotify); // Will rise OnDigital event when input value changes
   }
}




Thanks
Dimi

christop
Posts: 3
Joined: Mon Feb 06, 2017 9:13 am

Re: Getting Analog Input

Postby christop » Mon Feb 06, 2017 9:23 am

Note that on the photo i marked where the cables are connected on the breadboard extension.
P26 = GND from joystick
P25 = A1 = VERTZ joystick
P22 = A2 = HORTZ joystick
P1 = VCC for joystick

This is a regular thumb joystick, which is widely used in Arduino projects.

Tomer
Gemsense Team
Gemsense Team
Posts: 33
Joined: Thu Jan 28, 2016 10:12 am

Re: Getting Analog Input

Postby Tomer » Sun Feb 12, 2017 12:28 pm

Hi christop,

I see that you sample (call ReadAnalog) each frame in the FixedUpdate function, which probably mean you try to sample it in 60Hz (60 samples per second). Can you try reduce it to 20 Hz just to see if you get the correct values faster? The BLE connection might cause buffering when the connection is bad.

P.S:

You don't need to call

Code: Select all

gem.ReadDigital(DigitalPin.D0)
if you set the DigitalPin to InputNotify, it will be called automatically every state change.

christop
Posts: 3
Joined: Mon Feb 06, 2017 9:13 am

Re: Getting Analog Input

Postby christop » Thu Feb 16, 2017 10:03 am

Hi Tomer
thank you for the reply. I updated the code as you suggested.
Used the callback for the Digital port and read the analog port every 20Hz using a timer.
With this changes the A1 port seemed to read correct values and was responsive (1.8 at idle joystick position, 0 left, 3.6 right).
Unfortunately the A2 port only displayed 0.5 and did not change.
But when i switched to using A0 and A1, things worked as expected.
Can it be that A2 is not working as an analog port, or do i have a faulty chip?

I attach the new code, please take a look and let me know about any comments.

We still have some problems with the gyro but i will create a new thread for this.

How come there is no callback for analog input, like it exist for digital?
Can you also explain what the use of enabling the extensions is? Should this enable all callbacks?

Code: Select all

gem.ExtensionsActive = true;



Thanks a lot
Dimi

Code: Select all

using UnityEngine;
using UnityEngine.UI;
using GemSDK.Unity;

public class VRGun : MonoBehaviour {

   private float timeDiff;

   private IGem gem;
   public Text fireStatusText;
   public Text connectionStatusText;
   public Text JoystickHorizontalText;
   public Text JoystickVerticalText;

   // Use this for initialization
   void Start () {

      timeDiff = 0;

      if (Application.platform == RuntimePlatform.Android) {
         //To get gem by number instead of address on Android the Gem should be paired to Gem SDK Utility app
         //Also it is possible to get the gem by add  ress
         gem = GemManager.Instance.GetGem(0);
      } else if (Application.platform == RuntimePlatform.WindowsPlayer ||
                Application.platform == RuntimePlatform.WindowsEditor) {
         //On Windows it is possible to connect only by address
         string Address = ""; //For example "00:80:98:DC:9F:CD"

         if (string.IsNullOrEmpty(Address)) {
            Debug.LogError("Please specify the address for gem to connect");
         }
         gem = GemManager.Instance.GetGem(Address);
      }

      // Check if gem is available before doing anything else
      if(gem != null) {
         ConfigExtensions();
      }
   }

   void FixedUpdate () {
      if(gem != null) {

         timeDiff += Time.deltaTime;

         //Read port every 20Hz, (60Hz ->16ms, so 20->48)
         if(timeDiff > 0.048f) {
            // Read value of analog ports
            gem.ReadAnalog(AnalogPin.A1);
            gem.ReadAnalog(AnalogPin.A0);

            //gem.CalibrateAzimuth();
         }
      }
   }

   private void ConfigExtensions() {
      // Enable extensions for the digital and analog ports
      gem.ExtensionsActive = true;

      // Set the Events for state and port changing
      gem.OnStateChanged += (GemState state) => {
         // Check if the gem device is connected
         if (state == GemState.Connected) {
            connectionStatusText.text = "Connected";
            ConfigDigitalPorts();
         } else {
            connectionStatusText.text = "Disconnected";
         }
      };

      gem.OnDigital += (DigitalPinData data) => {
         if(data.Pin == DigitalPin.D0) {
            fireStatusText.text = data.Value.ToString();
         }
      };

      gem.OnAnalog += (AnalogPinData data) =>
      {
           if(data.Pin == AnalogPin.A1) {
            JoystickHorizontalText.text = "Vert: " + data.Value.ToString();
           }
           if (data.Pin == AnalogPin.A0) {
            JoystickVerticalText.text = "Horz: " + data.Value.ToString();
           }
      };
   }

   // Config digital ports
   private void ConfigDigitalPorts()
   {
      gem.ConfigDigital(DigitalPin.D0, PinConfig.Input);    // D0 as input
      gem.ConfigDigital(DigitalPin.D0, PinConfig.InputNotify); // Will rise OnDigital event when input value changes
   }
}


Tomer
Gemsense Team
Gemsense Team
Posts: 33
Joined: Thu Jan 28, 2016 10:12 am

Re: Getting Analog Input

Postby Tomer » Fri Feb 24, 2017 7:27 pm

Hi,

One of the analog ports is used in the Battery Extension board (the round board) to measure the battery voltage. If you are using this board it might be the problem.


Return to “Support”

Who is online

Users browsing this forum: No registered users and 0 guests