別棟備忘録4

*

「WiimoteLibを使う」第三話~加速度計再び~

      2015/10/24

先回のボタンの状態の取得に続き、今回は加速度センサーの情報を取得する。といっても、ぼぼ前回のプログラムのコピペでよくて…
加速度取得のプログラム


Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports WiimoteLib
Public Class Form1
Private Delegate Sub UpdateWiimoteStateDelegate(ByVal args As WiimoteChangedEventArgs)
Private wm As New Wiimote
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
wm.Connect()
wm.SetReportType(InputReport.ButtonsAccel, True) ’←注1
wm.SetLEDs(False, True, True, False)
AddHandler wm.WiimoteChanged, AddressOf wm_WiimoteChanged
End Sub
Private Sub wm_WiimoteChanged(ByVal sender As Object, ByVal args As WiimoteChangedEventArgs)
UpdateState(args)
End Sub
Public Sub UpdateState(ByVal args As WiimoteChangedEventArgs)
BeginInvoke(New UpdateWiimoteStateDelegate(AddressOf UpdateWiimoteChanged), args)
End Sub
Private Sub UpdateWiimoteChanged(ByVal args As WiimoteChangedEventArgs)
Dim ws As WiimoteState = args.WiimoteState
Label4.Text = ws.AccelState.Values.X ’←注2
Label5.Text = ws.AccelState.Values.Y
Label6.Text = ws.AccelState.Values.Z
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
wm.SetLEDs(False, False, False, False)
wm.Disconnect()
End Sub
End Class

変更点は2つだけ!
注1:ReportのタイプをBottonsからButtonsAccelに。でないと、リアルタイムの値取得ができない様子。
注2:前回ボタンの情報を呼び出していた部分をごりっと削除。加速度をLabel4-6に書き出します。
FormにLabel4-6をつくると、こんな感じ。
wiitest07.jpg
落し物
サンプルプログラム:WiiAccel←右クリック+対象をファイルに保存
サンプルソース:wiitest07s.zip←右クリック+対象をファイルに保存
加速度の値は重力加速度を1として出力されている様子。リモコンからは+4G~-4Gを256段階の16進数が送られてくるはず、なので、32に1を割り当てている感じ…か。しかし、これでは数字が目まぐるしく変化して捉えづらい。と、いうことで…WiimoteLibを使ったWiikskを制作。Wiiksk0.45にあった書き出し機能がないなど幾分ごまかしているところはあるものの、「すぐに繋がる」点で、幾分改善が。
wiitest08.jpg
落し物
サンプルプログラム:Wii加速度計←右クリック+対象をファイルに保存
サンプルソース:wiitest08s.zip←右クリック+対象をファイルに保存
(2010.11.17追記) なぜか動きません!私のPCでは動きますが…なぜ?
プログラム部分はこんな感じ。あちこち要改善な部分あり。


Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports WiimoteLib
Public Class Form1
Private Delegate Sub UpdateWiimoteStateDelegate(ByVal args As WiimoteChangedEventArgs)
Private waittimes As Integer = 30
Private mkLange As Integer = 2
Private wm As New Wiimote
Private cx, cy, cz As Single
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
wm.Connect()
wm.SetReportType(InputReport.ButtonsAccel, True)
wm.SetLEDs(False, True, True, False)
AddHandler wm.WiimoteChanged, AddressOf wm_WiimoteChanged
Button1.BackColor = Color.Pink
End Sub
Private Sub wm_WiimoteChanged(ByVal sender As Object, ByVal args As WiimoteChangedEventArgs)
UpdateState(args)
End Sub
Public Sub UpdateState(ByVal args As WiimoteChangedEventArgs)
System.Threading.Thread.Sleep(waittimes)
BeginInvoke(New UpdateWiimoteStateDelegate(AddressOf UpdateWiimoteChanged), args)
End Sub
Private Sub UpdateWiimoteChanged(ByVal args As WiimoteChangedEventArgs)
Dim ws As WiimoteState = args.WiimoteState
Label4.Text = Int((98 * ws.AccelState.Values.X) – cx * 10 + 0.5) / 10
Label5.Text = Int((98 * ws.AccelState.Values.Y) – cy * 10 + 0.5) / 10
Label6.Text = Int((98 * ws.AccelState.Values.Z) – cz * 10 + 0.5) / 10
OvalShape1.Top = 262 + 25 * (1 + mkLange) * Label6.Text / 9.8
OvalShape1.Left = 362 + 25 * (1 + mkLange) * Label4.Text / 9.8
OvalShape2.Top = 195 + 25 * (1 + mkLange) * Label5.Text / 9.8
OvalShape2.Left = 138 + 25 * (1 + mkLange) * Label4.Text / 9.8
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
wm.SetLEDs(False, False, False, False)
wm.Disconnect()
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label4.ForeColor = Color.LightGray
End Sub
Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
Label4.ForeColor = Color.Black
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
Label5.ForeColor = Color.LightGray
End Sub
Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
Label5.ForeColor = Color.Black
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
Label6.ForeColor = Color.LightGray
End Sub
Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
Label6.ForeColor = Color.Black
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
waittimes = ComboBox1.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
mkLange = ComboBox2.Text
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Button4.BackColor = Color.Pink
If CheckBox1.Checked = True Then
cx = Label4.Text
End If
If CheckBox2.Checked = True Then
cy = Label5.Text
End If
If CheckBox3.Checked = True Then
cz = Label6.Text
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
cx = 0
cy = 0
cz = 0
Button4.BackColor = Color.Silver
End Sub
End Class

(2010.11.16追記) 軸の方向がちがってますね…暇ができたら修正します。暇ができたら…。
内部リンク
まとめページ「WiimoteLibを使ってみる。」に戻る
「WiimoteLibを使う」第四話~赤外線センサーの活用~ →進む
戻る← 「WiimoteLibを使う」第二話~ボタン情報の取得~

 - WiimoteLib