internal override void CheckAndEnableSensors() { base.CheckAndEnableSensors(); if (Accelerometer.ShouldBeEnabled) { motionManager.StartAccelerometerUpdates(); } if ((Compass.ShouldBeEnabled || Orientation.ShouldBeEnabled) && !locationManagerActivated) { locationManagerActivated = true; locationManager.StartUpdatingHeading(); } if (Gyroscope.ShouldBeEnabled) { motionManager.StartGyroUpdates(); } if ((UserAcceleration.ShouldBeEnabled || Gravity.ShouldBeEnabled || Orientation.ShouldBeEnabled) && !motionManager.DeviceMotionActive) { motionManager.StartDeviceMotionUpdates(); } }
public override void ViewDidLoad() { base.ViewDidLoad(); motionManager = new CMMotionManager(); StartButton.TouchUpInside += (sender, e) => { gyroData = new ObservableCollection <double>(); motionManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, (data, error) => { gyroData.Add(data.RotationRate.x); gyroData.Add(data.RotationRate.y); gyroData.Add(data.RotationRate.z); var shake = gyroData.Where(gyroData => Math.Abs(gyroData) > 3).Count(); if (shake > 3) { motionManager.StopGyroUpdates(); var alert = new UIAlertView("Gyroscope", "Shaked!", null, "OK"); alert.Show(); } this.xLabel.Text = data.RotationRate.x.ToString("0.0000"); this.yLabel.Text = data.RotationRate.y.ToString("0.0000"); this.zLabel.Text = data.RotationRate.z.ToString("0.0000"); }); }; StopButton.TouchUpInside += (sender, e) => { motionManager.StopGyroUpdates(); }; }
public void StartReading(int reportInterval = -1) { if (reportInterval > 0) { _motionManager.GyroUpdateInterval = reportInterval; } _motionManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, OnGyroscopeChanged); }
/// <summary> /// Start the specified sensorType and interval. /// </summary> /// <param name="sensorType">Sensor type.</param> /// <param name="interval">Interval.</param> public void Start(MotionSensorType sensorType, MotionSensorDelay interval) { switch (sensorType) { case MotionSensorType.Accelerometer: if (motionManager.AccelerometerAvailable) { motionManager.AccelerometerUpdateInterval = (double)interval / ms; motionManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, OnAccelerometerChanged); } else { Debug.WriteLine("Accelerometer not available"); } break; case MotionSensorType.Gyroscope: if (motionManager.GyroAvailable) { motionManager.GyroUpdateInterval = (double)interval / ms; motionManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, OnGyroscopeChanged); } else { Debug.WriteLine("Gyroscope not available"); } break; case MotionSensorType.Magnetometer: if (motionManager.MagnetometerAvailable) { motionManager.MagnetometerUpdateInterval = (double)interval / ms; motionManager.StartMagnetometerUpdates(NSOperationQueue.CurrentQueue, OnMagnometerChanged); } else { Debug.WriteLine("Magnetometer not available"); } break; case MotionSensorType.Compass: if (CLLocationManager.HeadingAvailable) { locationManager.StartUpdatingHeading(); locationManager.UpdatedHeading += OnHeadingChanged; } else { Debug.WriteLine("Compass not available"); } break; } sensorStatus[sensorType] = true; }
protected override async Task StartListeningAsync() { await base.StartListeningAsync(); _motionManager?.StartGyroUpdates(new NSOperationQueue(), async(data, error) => { if (data != null && error == null) { await StoreDatumAsync(new GyroscopeDatum(DateTimeOffset.UtcNow, data.RotationRate.x, data.RotationRate.y, data.RotationRate.z)); } }); }
public void StartReading(int reportInterval = -1) { if (reportInterval > 0) { _motionManager.GyroUpdateInterval = reportInterval; } try { _motionManager.StartGyroUpdates(NSOperationQueue.MainQueue, OnGyroscopeChanged); } catch (Exception ex) { OnReadingError?.Invoke(this, new DeviceSensorReadingErrorEventArgs("Gyroscope - Not called from the UIThread")); } }
/// <summary> /// Start the specified sensor type reading. /// </summary> /// <param name="sensorType">Sensor type</param> public override void Start(SensorType sensorType) { switch (sensorType) { case SensorType.Accelerometer: AccelerometerActive = true; motionManager.AccelerometerUpdateInterval = 0.05; motionManager.StartAccelerometerUpdates(NSOperationQueue.MainQueue, (data, error) => { EmitAccelerometer(new MotionVector(data.Acceleration.X, data.Acceleration.Y, data.Acceleration.Z)); }); break; case SensorType.Gyroscope: GyroActive = true; motionManager.GyroUpdateInterval = 0.05; motionManager.StartGyroUpdates(NSOperationQueue.MainQueue, (data, error) => { EmitGyroscope(new MotionVector(data.RotationRate.x, data.RotationRate.y, data.RotationRate.z)); }); break; case SensorType.DeviceMotion: DeviceMotionActive = true; motionManager.DeviceMotionUpdateInterval = 0.05d; motionManager.StartDeviceMotionUpdates(NSOperationQueue.MainQueue, (motion, error) => { EmitDeviceMotion(new MotionVector(motion.Attitude.Roll, motion.Attitude.Pitch, motion.Attitude.Yaw)); }); break; case SensorType.Compass: CompassActive = true; locationManager.UpdatedHeading += (sender, eventArgs) => { // TODO: Fix. EmitCompass(eventArgs.NewHeading.TrueHeading); }; locationManager.StartUpdatingHeading(); break; case SensorType.LightLevel: LightLevelActive = false; break; } }
public void Start() { // 加速度センサ if (motionManager.AccelerometerAvailable) { motionManager.AccelerometerUpdateInterval = 0.1; motionManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, (data, error) => { // 加速度センサの取得間隔を取得(sec) // CMAccelerometerData.Timestamp : デバイスが起動してからの秒単位の時間 double nowAccelTimeStamp = data.Timestamp; double accelInterval = (prevAccelTimeStamp != 0) ? (nowAccelTimeStamp - prevAccelTimeStamp) : 0; prevAccelTimeStamp = nowAccelTimeStamp; AccelerationReceived(this, new SensorEventArgs { X = data.Acceleration.X, Y = data.Acceleration.Y, Z = data.Acceleration.Z, Interval = accelInterval }); }); } // ジャイロスコープ if (motionManager.GyroAvailable) { motionManager.GyroUpdateInterval = 0.1; motionManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, (data, error) => { // ジャイロの取得間隔を取得(sec) // CMGyroData.Timestamp : デバイスが起動してからの秒単位の時間 double nowGyroTimeStamp = data.Timestamp; double gyroInterval = (prevGyroTimeStamp != 0) ? (nowGyroTimeStamp - prevGyroTimeStamp) : 0; prevGyroTimeStamp = nowGyroTimeStamp; AngularVelocityReceived(this, new SensorEventArgs { X = data.RotationRate.x, Y = data.RotationRate.y, Z = data.RotationRate.z, Interval = gyroInterval }); }); } }
public void StartSensorUpdates() { // Raw accelerometer if (_motionManager.AccelerometerAvailable) { _motionManager.StartAccelerometerUpdates(_queue, HandleCMAccelerometerHandler); } if (_motionManager.GyroAvailable) { _motionManager.StartGyroUpdates(_queue, HandleCMGyroHandler); } if (_motionManager.MagnetometerAvailable) { _motionManager.StartMagnetometerUpdates(_queue, HandleCMMagnetometerHandler); } if (_motionManager.DeviceMotionAvailable) { _motionManager.StartDeviceMotionUpdates(_queue, HandleCMDeviceMotionHandler); _pedometer.StartPedometerUpdates(NSDate.Now, HandleCMPedometer); } //start updating headings _locationManager.StartUpdatingHeading(); //// service for measurements once per minute UIDevice.CurrentDevice.BatteryMonitoringEnabled = true; Timer timer = new Timer(60000); timer.AutoReset = true; timer.Elapsed += SensorMeasurementSessionTimerElapsed; timer.Start(); IsListening = true; }
public void StartUpdatesWithMotionDataType(MotionDataType type, int sliderValue) { double updateInterval = 0.00; double delta = 0.005; switch (graphDataSource) { case MotionDataType.AccelerometerData: updateInterval = GraphViewController.accelerometrMin + delta * sliderValue; if (mManager.AccelerometerAvailable) { mManager.AccelerometerUpdateInterval = updateInterval; mManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, (data, error) => { if (primaryGraph == null) { return; } primaryGraph.AddX(data.Acceleration.X, data.Acceleration.Y, data.Acceleration.Z); SetLabelValueX(data.Acceleration.X, data.Acceleration.Y, data.Acceleration.Z); }); } primaryGraphLabel.Text = "AccelerometerData.Acceleration"; break; case MotionDataType.GyroData: updateInterval = gyroMin + delta * sliderValue; if (mManager.GyroAvailable) { mManager.GyroUpdateInterval = updateInterval; mManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, (gyroData, error) => { if (primaryGraph == null) { return; } primaryGraph.AddX(gyroData.RotationRate.x, gyroData.RotationRate.y, gyroData.RotationRate.z); SetLabelValueX(gyroData.RotationRate.x, gyroData.RotationRate.y, gyroData.RotationRate.z); }); } primaryGraphLabel.Text = "GyroData.RotationRate"; break; case MotionDataType.DeviceMotion: updateInterval = deviceMotionMin + delta * sliderValue; if (mManager.DeviceMotionAvailable) { mManager.DeviceMotionUpdateInterval = updateInterval; mManager.StartDeviceMotionUpdates(NSOperationQueue.CurrentQueue, (motion, error) => { graphs [(int)DeviceMotionGraphType.Attitude].AddX(motion.Attitude.Roll, motion.Attitude.Pitch, motion.Attitude.Yaw); graphs [(int)DeviceMotionGraphType.RotationRate].AddX(motion.RotationRate.x, motion.RotationRate.y, motion.RotationRate.z); graphs [(int)DeviceMotionGraphType.Gravity].AddX(motion.Gravity.X, motion.Gravity.Y, motion.Gravity.Z); graphs [(int)DeviceMotionGraphType.UserAcceleration].AddX(motion.UserAcceleration.X, motion.UserAcceleration.Y, motion.UserAcceleration.Z); switch ((DeviceMotionGraphType)SegmentedControl.SelectedSegment) { case DeviceMotionGraphType.Attitude: SetLabelValueRoll(motion.Attitude.Roll, motion.Attitude.Pitch, motion.Attitude.Yaw); break; case DeviceMotionGraphType.RotationRate: SetLabelValueX(motion.RotationRate.x, motion.RotationRate.y, motion.RotationRate.z); break; case DeviceMotionGraphType.Gravity: SetLabelValueX(motion.Gravity.X, motion.Gravity.Y, motion.Gravity.Z); break; case DeviceMotionGraphType.UserAcceleration: SetLabelValueX(motion.UserAcceleration.X, motion.UserAcceleration.Y, motion.UserAcceleration.Z); break; } }); } primaryGraphLabel.Text = graphTitles [SegmentedControl.SelectedSegment]; break; } UpdateIntervalLabel.Text = updateInterval.ToString(); }
public override void Update() { base.Update(); // Enable/disable supported sensors and update enabled sensors if (accelerometerSensor != null) { bool enable = accelerometerSensor.IsEnabled; if (enable != motionManager.AccelerometerActive) { if (accelerometerSensor.IsEnabled) { motionManager.StartAccelerometerUpdates(); } else { motionManager.StopAccelerometerUpdates(); } } if (enable) { var accelerometerData = motionManager.AccelerometerData; accelerometerSensor.Acceleration = accelerometerData != null?CmAccelerationToVector3(accelerometerData.Acceleration) : Vector3.Zero; } } if (compassSensor != null) { bool enable = compassSensor.IsEnabled; if (enable != locationManagerActivated) { if (compassSensor.IsEnabled) { locationManager.StartUpdatingHeading(); } else { locationManager.StopUpdatingHeading(); } locationManagerActivated = compassSensor.IsEnabled; } if (enable) { compassSensor.Heading = GetNorthInRadian(locationManager); } } if (gyroscopeSensor != null) { bool enable = gyroscopeSensor.IsEnabled; if (enable != motionManager.GyroActive) { if (gyroscopeSensor.IsEnabled) { motionManager.StartGyroUpdates(); } else { motionManager.StopGyroUpdates(); } } if (enable) { var gyroData = motionManager.GyroData; gyroscopeSensor.RotationRate = gyroData != null?CmRotationRateToVector3(gyroData.RotationRate) : Vector3.Zero; } } if (userAccelerationSensor != null) { bool enable = userAccelerationSensor.IsEnabled || gravitySensor.IsEnabled || orientationSensor.IsEnabled; if (enable != motionManager.DeviceMotionActive) { if (enable) { motionManager.StartDeviceMotionUpdates(); } else { motionManager.StopDeviceMotionUpdates(); } } if (enable) { var motion = motionManager.DeviceMotion; // Update orientation sensor if (motion != null && motion.Attitude != null) { var q = motionManager.DeviceMotion.Attitude.Quaternion; var quaternion = new Quaternion((float)q.x, (float)q.z, -(float)q.y, (float)q.w); if (compassSensor != null) { // re-adjust the orientation to align with the north (common behavior on other platforms) TODO current implementation only takes in account the first value. if (firstNorthValue <= 0) { firstNorthValue = GetNorthInRadian(locationManager); } quaternion = Quaternion.RotationY(-firstNorthValue) * quaternion; } orientationSensor.FromQuaternion(quaternion); } else { orientationSensor.Reset(); } // Update gravity sensor gravitySensor.Vector = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero; // Update user acceleration userAccelerationSensor.Acceleration = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero; } } }
/// <summary> /// Starts this instance. /// </summary> partial void Start() { _motionManager = new CMMotionManager(); _motionManager.GyroUpdateInterval = (long)Interval / 1000; _motionManager.StartGyroUpdates(NSOperationQueue.MainQueue, OnUpdate); }
protected override void Start(CMMotionManager mgr, IObserver <MotionReading> ob) => mgr.StartGyroUpdates(NSOperationQueue.CurrentQueue ?? new NSOperationQueue(), (data, err) => ob.OnNext(new MotionReading(data.RotationRate.x, data.RotationRate.y, data.RotationRate.z)) );