質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

370閲覧

Xam.Plugin.DeviceMotionで加速度とジャイロの値が同じになる

naritamago

総合スコア15

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2018/08/29 07:59

前提・実現したいこと

Xamarin学習中です。
NugetからXam.Plugin.DeviceMotionをインストールして、
加速度センサーとジャイロセンサーの値を取得してXAMLにデータバインディングで表示させているのですが、
どんな動かし方をしても加速度センサーの値とジャイロセンサーの値が全く同じになってしまいます。
エミュレータ上でも実機でも同じです。

イメージ説明
(Label要素にデータバインディングさせています)

ソースコード

加速度センサー

ViewModel

1public void StartACCEL(){ 2 motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui); 3 if (motion.IsActive(MotionSensorType.Accelerometer)) 4 { 5 motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => 6 { 7 8 Device.BeginInvokeOnMainThread(() => 9 { 10 this.xAccel = ((MotionVector)a.Value).X.ToString("0.0000"); 11 this.yAccel = ((MotionVector)a.Value).Y.ToString("0.0000"); 12 this.zAccel = ((MotionVector)a.Value).Z.ToString("0.0000"); 13 }); 14 }; 15 } 16 }

ジャイロセンサー

ViewModel

1public void StartGYRO() 2 { 3 motion.Start(MotionSensorType.Gyroscope, MotionSensorDelay.Default); 4 if (motion.IsActive(MotionSensorType.Gyroscope)) 5 { 6 motion.SensorValueChanged += (object s, SensorValueChangedEventArgs v) => 7 { 8 Device.BeginInvokeOnMainThread(() => 9 { 10 this.xGyro = ((MotionVector)v.Value).X.ToString("0.0000"); 11 this.yGyro = ((MotionVector)v.Value).Y.ToString("0.0000"); 12 this.zGyro = ((MotionVector)v.Value).Z.ToString("0.0000"); 13 }); 14 }; 15 } 16 } 17

どちらも同じViewModel内に記述しています(動作が安定したらModelに分割するつもりです)
それぞれのセンサー値のx、y、zの値をString型に変換してデータバインディングでViewに表示させています。

試したこと

どちらもメインスレッド上で動くのが原因なのかと思い、Device.BeginInvokeOnMainThread(() =>を削除したりしましたが結果は変わりませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

SensorValueChangedは、変化した値の種類のチェックをする必要があります。
https://github.com/rdelrosario/xamarin-plugins/tree/master/DeviceMotion#events

加速度センサー

C#

1motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => 2{ 3 if(a.SensorType == MotionSensorType.Accelerometer) 4 { 5 Device.BeginInvokeOnMainThread(() => 6 { 7 this.xAccel = ((MotionVector)a.Value).X.ToString("0.0000"); 8 this.yAccel = ((MotionVector)a.Value).Y.ToString("0.0000"); 9 this.zAccel = ((MotionVector)a.Value).Z.ToString("0.0000"); 10 }); 11 } 12};

ジャイロセンサー

C#

1motion.SensorValueChanged += (object s, SensorValueChangedEventArgs v) => 2{ 3 if(v.SensorType == MotionSensorType.Gyroscope) 4 { 5 Device.BeginInvokeOnMainThread(() => 6 { 7 this.xGyro = ((MotionVector)v.Value).X.ToString("0.0000"); 8 this.yGyro = ((MotionVector)v.Value).Y.ToString("0.0000"); 9 this.zGyro = ((MotionVector)v.Value).Z.ToString("0.0000"); 10 }); 11 } 12};

投稿2018/08/29 09:08

編集2018/08/29 09:09
f-miyu

総合スコア1625

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

naritamago

2018/08/30 00:49

f-miyuさんの解答通りに修正して無事動かす事ができました。早々の回答助かりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問