Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get the Accelerometer and Magenetometer values simultaniously? #130

Open
SunainaDG opened this issue Jul 21, 2017 · 0 comments
Open

Comments

@SunainaDG
Copy link

SunainaDG commented Jul 21, 2017

Like when my device senses a jerk, it has both change in accelerometer and magnetometer values. I need both of their values of the 3 axes separately. That is X,Y,Z for accelerometer separately and X,Y,Z for magnetometer separately .

I have already tried the below code, but it does not work.Please help me on this.

//code below:

    private void StartListening()
    {
        CrossDeviceMotion.Current.SensorValueChanged += (s, a) =>
        {
            if (lastUpdate > DateTime.MinValue)
            { 

                    //I feel like there should be some kind of a type casting of the value here but I have no 
                       idea how to do that.

                    if (a.SensorType == MotionSensorType.Accelerometer)
                    {
                        CurrentData.X = ((MotionVector)a.Value).X;
                        CurrentData.Y = ((MotionVector)a.Value).Y;
                        CurrentData.Z = ((MotionVector)a.Value).Z;
                    }

                    if (a.SensorType == MotionSensorType.Magnetometer)
                    {
                        CurrentData.Pitch = ((MotionVector)a.Value).X;
                        CurrentData.Roll = ((MotionVector)a.Value).Y;
                        CurrentData.Yaw = ((MotionVector)a.Value).Z;
                    }
            }
            else
            {
                 //I feel like there should be some kind of a type casting of the value here but I have no 
                       idea how to do that.

                if (a.SensorType == MotionSensorType.Accelerometer)
                {
                    LastData.X = ((MotionVector)a.Value).X;
                    LastData.Y = ((MotionVector)a.Value).Y;
                    LastData.Z = ((MotionVector)a.Value).Z;
                }

                if (a.SensorType == MotionSensorType.Magnetometer)
                {
                    LastData.Pitch = ((MotionVector)a.Value).X;
                    LastData.Roll = ((MotionVector)a.Value).Y;
                    LastData.Yaw = ((MotionVector)a.Value).Z;
                }
                lastUpdate = currentTime;
            }

        };
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant