Beispiel #1
0
        public void GetLatestSensorUpdatesInternal()
        {
            const string GetLengthMethod          = "length";
            const string GetFrameAtIndexMethod    = "getFrameAtIndex";
            const string GetAccelerationMethod    = "getAcceleration";
            const string GetAngularVelocityMethod = "getAngularVelocity";
            const string GetRotationSixDofMethod  = "getRotationSixDof";
            const string GetRotationNineDofMethod = "getRotationNineDof";

            const string GetWMethod           = "getW";
            const string GetXMethod           = "getX";
            const string GetYMethod           = "getY";
            const string GetZMethod           = "getZ";
            const string GetAccuracyMethod    = "getAccuracyValue";
            const string GetUncertaintyMethod = "getAccuracy";

            const string GetTimestampMethod = "getTimestamp";
            const string GetDeltaTimeMethod = "getDeltaTime";

            const string GetInput = "getInput";

            AndroidJavaObject androidObj = AndroidPlugin.GetFrames();
            int count = androidObj.Call <int>(GetLengthMethod);

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    AndroidJavaObject frame = androidObj.Call <AndroidJavaObject>(GetFrameAtIndexMethod, i);

                    AndroidJavaObject accelValue      = frame.Call <AndroidJavaObject>(GetAccelerationMethod);
                    AndroidJavaObject angVelValue     = frame.Call <AndroidJavaObject>(GetAngularVelocityMethod);
                    AndroidJavaObject rotSixDofValue  = frame.Call <AndroidJavaObject>(GetRotationSixDofMethod);
                    AndroidJavaObject rotNineDofValue = frame.Call <AndroidJavaObject>(GetRotationNineDofMethod);

                    byte gesture = frame.Call <byte>(GetInput);

                    SensorVector3    accel      = new SensorVector3();
                    SensorVector3    gyro       = new SensorVector3();
                    SensorQuaternion rotSixDof  = new SensorQuaternion();
                    SensorQuaternion rotNineDof = new SensorQuaternion();

                    accel.value = new Vector3(
                        (float)accelValue.Call <double>(GetXMethod),
                        (float)accelValue.Call <double>(GetYMethod),
                        (float)accelValue.Call <double>(GetZMethod)
                        );
                    accel.accuracy = (SensorAccuracy)accelValue.Call <byte>(GetAccuracyMethod);

                    gyro.value = new Vector3(
                        (float)angVelValue.Call <double>(GetXMethod),
                        (float)angVelValue.Call <double>(GetYMethod),
                        (float)angVelValue.Call <double>(GetZMethod)
                        );
                    gyro.accuracy = (SensorAccuracy)angVelValue.Call <byte>(GetAccuracyMethod);

                    rotSixDof.value = new Quaternion(
                        (float)rotSixDofValue.Call <double>(GetXMethod),
                        (float)rotSixDofValue.Call <double>(GetYMethod),
                        (float)rotSixDofValue.Call <double>(GetZMethod),
                        (float)rotSixDofValue.Call <double>(GetWMethod)
                        );
                    rotSixDof.measurementUncertainty = (float)rotSixDofValue.Call <double>(GetUncertaintyMethod);

                    rotNineDof.value = new Quaternion(
                        (float)rotNineDofValue.Call <double>(GetXMethod),
                        (float)rotNineDofValue.Call <double>(GetYMethod),
                        (float)rotNineDofValue.Call <double>(GetZMethod),
                        (float)rotNineDofValue.Call <double>(GetWMethod)
                        );
                    rotNineDof.measurementUncertainty = (float)rotNineDofValue.Call <double>(GetUncertaintyMethod);


                    _currentSensorFrames.Add(
                        new SensorFrame
                    {
                        timestamp       = WearableConstants.Sensor2UnityTime * frame.Call <int>(GetTimestampMethod),
                        deltaTime       = WearableConstants.Sensor2UnityTime * frame.Call <int>(GetDeltaTimeMethod),
                        acceleration    = accel,
                        angularVelocity = gyro,
                        rotationSixDof  = rotSixDof,
                        rotationNineDof = rotNineDof,
                        gestureId       = (GestureId)gesture
                    }
                        );
                }
            }
        }
        /// <summary>
        /// Used internally by WearableControl to get the latest buffer of SensorFrame updates from
        /// the Wearable Device; the newest frame in that batch is set as the CurrentSensorFrame.
        /// </summary>
        private void GetLatestSensorUpdates()
        {
            _currentSensorFrames.Clear();

                        #if UNITY_IOS && !UNITY_EDITOR
            unsafe
            {
                BridgeSensorFrame *frames = null;
                int count = 0;
                WearableGetSensorFrames(&frames, &count);
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        var frame = frames + i;
                        _currentSensorFrames.Add(new SensorFrame {
                            timestamp       = WearableConstants.Sensor2UnityTime * frame->timestamp,
                            deltaTime       = WearableConstants.Sensor2UnityTime * frame->deltaTime,
                            acceleration    = frame->acceleration,
                            angularVelocity = frame->angularVelocity,
                            rotation        = frame->rotation,
                            gestureId       = frame->gestureId
                        });
                    }

                    _lastSensorFrame = _currentSensorFrames[_currentSensorFrames.Count - 1];

                    OnSensorsOrGestureUpdated(_lastSensorFrame);
                }
            }
                        #elif UNITY_ANDROID && !UNITY_EDITOR
            const string GetLengthMethod          = "length";
            const string GetFrameAtIndexMethod    = "getFrameAtIndex";
            const string GetAccelerationMethod    = "getAcceleration";
            const string GetAngularVelocityMethod = "getAngularVelocity";
            const string GetRotationMethod        = "getRotation";

            const string GetWMethod           = "getW";
            const string GetXMethod           = "getX";
            const string GetYMethod           = "getY";
            const string GetZMethod           = "getZ";
            const string GetAccuracyMethod    = "getAccuracyValue";
            const string GetUncertaintyMethod = "getAccuracy";

            const string GetTimestampMethod = "getTimestamp";
            const string GetDeltaTimeMethod = "getDeltaTime";

            const string GetInput = "getInput";

            AndroidJavaObject androidObj = AndroidPlugin.GetFrames();
            int count = androidObj.Call <int>(GetLengthMethod);

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    AndroidJavaObject frame = androidObj.Call <AndroidJavaObject>(GetFrameAtIndexMethod, i);

                    AndroidJavaObject accelValue  = frame.Call <AndroidJavaObject>(GetAccelerationMethod);
                    AndroidJavaObject angVelValue = frame.Call <AndroidJavaObject>(GetAngularVelocityMethod);
                    AndroidJavaObject rotValue    = frame.Call <AndroidJavaObject>(GetRotationMethod);

                    byte gesture = frame.Call <byte>(GetInput);

                    SensorVector3    accel = new SensorVector3();
                    SensorVector3    gyro  = new SensorVector3();
                    SensorQuaternion rot   = new SensorQuaternion();

                    accel.value = new Vector3(
                        (float)accelValue.Call <double>(GetXMethod),
                        (float)accelValue.Call <double>(GetYMethod),
                        (float)accelValue.Call <double>(GetZMethod)
                        );
                    accel.accuracy = (SensorAccuracy)accelValue.Call <byte>(GetAccuracyMethod);

                    gyro.value = new Vector3(
                        (float)angVelValue.Call <double>(GetXMethod),
                        (float)angVelValue.Call <double>(GetYMethod),
                        (float)angVelValue.Call <double>(GetZMethod)
                        );
                    gyro.accuracy = (SensorAccuracy)angVelValue.Call <byte>(GetAccuracyMethod);

                    rot.value = new Quaternion(
                        (float)rotValue.Call <double>(GetXMethod),
                        (float)rotValue.Call <double>(GetYMethod),
                        (float)rotValue.Call <double>(GetZMethod),
                        (float)rotValue.Call <double>(GetWMethod)
                        );
                    rot.measurementUncertainty = (float)rotValue.Call <double>(GetUncertaintyMethod);

                    _currentSensorFrames.Add(
                        new SensorFrame
                    {
                        timestamp       = WearableConstants.Sensor2UnityTime * frame.Call <int>(GetTimestampMethod),
                        deltaTime       = WearableConstants.Sensor2UnityTime * frame.Call <int>(GetDeltaTimeMethod),
                        acceleration    = accel,
                        angularVelocity = gyro,
                        rotation        = rot,
                        gestureId       = (GestureId)gesture
                    }
                        );
                }

                _lastSensorFrame = _currentSensorFrames[_currentSensorFrames.Count - 1];

                OnSensorsOrGestureUpdated(_lastSensorFrame);
            }
                        #endif
        }