別棟備忘録4

*

Wiiリモコンで計る距離 第2話WiiZahyouの作成

      2015/10/25

下準備が整ったので、いよいよセンサーバーからの距離を計るソフトを作ってみる。加速度同様リアルタイムで表示されると快適か。
とすると、概念として「きまった時間ごとに情報を取得」か、「変化があれば情報を取得」かの2通りの作り方があるが、今回は前者を採択。測定機器として使える布石を打っておく。
Wiiksk2のエントリあたりを参考にしつつ…起動画面。
WiiZahyou003.jpg
いつもながら情けないソース。


Imports WiimoteLib
Public Class Form1
Private wm As New Wiimote
Private save As Boolean = False
Private wir As Boolean = True
Private wsd As Single = 150
Private sw As New System.IO.StreamWriter(“data.csv”, True, System.Text.Encoding.GetEncoding(“shift_jis”))
Private Declare Function timeGetTime Lib “winmm.dll” Alias “timeGetTime” () As Long
Private startTime As Long = 0
Private ird As Double
Dim timeCount1 As String
Public Sub New()
InitializeComponent()
End Sub
‘データ取得時間間隔の変更ボタン
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = ComboBox1.Text
End Sub
‘データの記録・非記録変更ボタン
Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If save = False Then
Button6.Text = “記録中”
Button6.BackColor = Color.Pink
save = True
startTime = timeGetTime()
sw.WriteLine(“time(s)” & “,” & “x(mm)”)
Else
Button6.Text = “記録”
Button6.BackColor = Color.Silver
save = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
timeCount1 = Format((timeGetTime() – startTime) / 1000, “0.000”)
Dim ws As WiimoteState = wm.WiimoteState
If ws.IRState.IRSensors(0).Found = True Then
If wir = True Then
OvalShape1.FillColor = Color.Red ‘IR(0)位置の描画
OvalShape1.Left = 160 * ws.IRState.IRSensors(0).Position.X
OvalShape1.Top = 160 – 160 * ws.IRState.IRSensors(0).Position.Y
End If
Else
OvalShape1.FillColor = Color.Silver
End If
If ws.IRState.IRSensors(1).Found = True Then
If wir = True Then
OvalShape2.FillColor = Color.Red ‘IR(1)位置の描画
OvalShape2.Left = 160 * ws.IRState.IRSensors(1).Position.X
OvalShape2.Top = 160 – 160 * ws.IRState.IRSensors(1).Position.Y
End If
Else
OvalShape2.FillColor = Color.Silver
End If
‘センサーバーからの距離の計算
ird = Math.Sqrt((ws.IRState.IRSensors(0).Position.X – ws.IRState.IRSensors(1).Position.X) ^ 2 + (ws.IRState.IRSensors(0).Position.Y – ws.IRState.IRSensors(1).Position.Y) ^ 2)
Label2.Text = Int(1.3228 * (wsd / ird) – 9.7373)’第1話で得られた関係式
If save = True Then
sw.WriteLine(timeCount1 & “,” & Label2.Text)
End If
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()
sw.Close()
End Sub
‘WiiセンサーバーのIRLED2つの距離設定ボタン初期値は150mm
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
wsd = TextBox1.Text
End Sub
‘Wiiリモコンの接続ボタン
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
wm.Connect()
wm.SetReportType(InputReport.IRAccel, True)
wm.SetLEDs(False, True, True, False)
Button1.BackColor = Color.Pink
End Sub
‘Wiiリモコンが検出したIR2点位置図示の表示・非表示ボタン
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If wir = False Then
Button4.Text = “表示”
Button4.BackColor = Color.Pink
wir = True
Else
Button4.Text = “非表示”
Button4.BackColor = Color.Silver
wir = False
End If
End Sub
End Class

・落し物・
サンプルプログラム:Wiitest12.zip(WiiZahyou)
サンプルソース:Wiitest12s.zip
・実験風景・

・解説・
自作Wiiセンサーバーからの距離が画面上に表示されている。センサーバーの位置が金尺での+200mmだから、最初の画面の表示が961mmってことは、リモコンは大体1161mm(116cm)の位置にある…ってわけでぴったり。その後リモコンを1200mm(120cm)の位置におくと、画面の表示は1000mmってことでやっぱりぴったり測れているわけです。
リモコンの乗った台車を動かすと、一応ほぼリアルタイムでの位置座標計測ができることがわかります。

・備忘録事項・
・精度は大甘の甘あまでmm。ただ、シビアに見てもcm位は出そう。
・精度が本当に大味になるものの、3メートルを超えても値は表示される。意外と遠くからでも捉えられる手作りセンサーバー。
・第1話の関係式が非常に重要な意味を持つ。改善によって精度は上がる。
・校正を行える機能を備えるのも懸案事項。

・内部リンク・
Wiiリモコンで計る距離に戻る
戻る← Wiiリモコンで計る距離 第1話l,d,xの関係式の決定

 - Wiiリモコン→座標計