Imports WiimoteLib Public Class Form1 Private wm As New Wiimote Private save As Boolean = False Private ksave As Boolean = False Private i As Integer = 0 Private k As Integer = 0 Private l(400) As Single '簡易記録時lのバッファ Private t(400) As Long '簡易記録時tのバッファ Private Average_Time(4) As Single Private v(4) As Single Private data_interval(4) As Long Private data_pitch As Long Private wir As Boolean = True Private wsd As Single = 300 'センサバーのLED間の距離 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 l1 As Double = 1 Private l2 As Double = 1 Private velocity As Single Private ird As Double = 200 '計測されたLED間の距離 Dim timeCount1 As Long Dim timeCount2 As Long Public Sub New() InitializeComponent() End Sub '/計測間隔の変更ボタンInterval Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Interval = ComboBox1.Text startTime = timeGetTime() End Sub '/data.csvへの書き出しボタン 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() timeCount2 = 0 sw.WriteLine("timeCount1" & "," & "velocity" & "," & "timeCount1" & "," & "timeCount2" & "," & "l1" & "," & "l2") 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 timeCount2 = timeCount1 timeCount1 = (timeGetTime() - startTime) Dim ws As WiimoteState = wm.WiimoteState '//////////////////IRLEDの位置表示に関する部分////////////////////////// If ws.IRState.IRSensors(0).Found = True Then If wir = True Then OvalShape1.FillColor = Color.Red 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 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) l2 = l1 l1 = (1.3228 * (wsd / ird) - 9.7373) distance.Text = Int(l1) velocity = Format((l1 - l2) / ((timeCount1 - timeCount2)), "0.00") Label2.Text = velocity If save = True Then sw.WriteLine(timeCount1 & "," & Label2.Text & "," & timeCount1 & "," & timeCount2 & "," & l1 & "," & l2) End If If ksave = True Then l(k) = l1 t(k) = timeCount1 k = k + 1 End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For j = 0 To 400 l(j) = 0 t(j) = 0 Next j 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 '/センサーバーの間隔設定/ Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click wsd = TextBox1.Text End Sub '/接続ボタン/ 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 '/IRCheckの表示非表示/ 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 '/簡易記録ボタン/ Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If ksave = False Then Button5.Text = "記録中" Button5.BackColor = Color.Pink i = 0 ksave = True startTime = timeGetTime() Else Button5.Text = "簡易記録" Button5.BackColor = Color.Silver For j = 0 To 4 data_interval(j) = Int((saveint.Text * (j + 1)) / 50) '5つのデータの時刻。指定の時間間隔で5点。 data_pitch = Int(saveint.Text / 100) '平均の速さを求めるときに、いくつ前といくつ後のデータを参照するか。 Next For j = 0 To 4 If t(data_interval(j) + data_pitch) = 0 Then 'データが存在しない時は、0を簡易表示。 v(j) = 0 Average_Time(j) = 0 Else '平均の速さを求める。 v(j) = (l(data_interval(j) + data_pitch) - l(data_interval(j) - data_pitch)) / (t(data_interval(j) + data_pitch) - t(data_interval(j) - data_pitch)) Average_Time(j) = ((t(data_interval(j) + data_pitch) + t(data_interval(j) - data_pitch))) / 2 End If Next j v1.Text = Format(v(0), "0.00") v2.Text = Format(v(1), "0.00") v3.Text = Format(v(2), "0.00") v4.Text = Format(v(3), "0.00") v5.Text = Format(v(4), "0.00") t1.Text = Format(Average_Time(0) / 1000, "0.00") t2.Text = Format(Average_Time(1) / 1000, "0.00") t3.Text = Format(Average_Time(2) / 1000, "0.00") t4.Text = Format(Average_Time(3) / 1000, "0.00") t5.Text = Format(Average_Time(4) / 1000, "0.00") ksave = False End If End Sub '/簡易記録のリセットボタン/ Private Sub K_saveReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles K_saveReset.Click v1.Text = 0 v2.Text = 0 v3.Text = 0 v4.Text = 0 v5.Text = 0 t1.Text = 0 t2.Text = 0 t3.Text = 0 t4.Text = 0 t5.Text = 0 For j = 0 To 400 l(j) = 0 t(j) = 0 k = 0 Next j End Sub '/グラフ表示1ボタン/ Private Sub graph_ViewR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles graph_ViewR.Click 'それぞれの点をグラフに表示。x=146t+22.2、y=-256.08v + 311.95) Rpoint1.Left = Int(t1.Text * 146.8 + 22.2) Rpoint2.Left = Int(t2.Text * 146.8 + 22.2) Rpoint3.Left = Int(t3.Text * 146.8 + 22.2) Rpoint4.Left = Int(t4.Text * 146.8 + 22.2) Rpoint5.Left = Int(t5.Text * 146.8 + 22.2) Rpoint1.Top = Int(v1.Text * (-256.08) + 311.95) Rpoint2.Top = Int(v2.Text * (-256.08) + 311.95) Rpoint3.Top = Int(v3.Text * (-256.08) + 311.95) Rpoint4.Top = Int(v4.Text * (-256.08) + 311.95) Rpoint5.Top = Int(v5.Text * (-256.08) + 311.95) 'もし、簡易表示の値が0なら、○deta1の○にマーカーを重ねる。 If (t1.Text = 0.0) Then Rpoint1.Top = 17 Rpoint1.Left = 376 End If If (t2.Text = 0.0) Then Rpoint2.Top = 17 Rpoint2.Left = 376 End If If (t3.Text = 0.0) Then Rpoint3.Top = 17 Rpoint3.Left = 376 End If If (t4.Text = 0.0) Then Rpoint4.Top = 17 Rpoint4.Left = 376 End If If (t5.Text = 0.0) Then Rpoint5.Top = 17 Rpoint5.Left = 376 End If End Sub '/グラフ表示2ボタン/ Private Sub graph_ViewB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles graph_ViewB.Click 'それぞれの点をグラフに表示。x=146t+22.2、y=-256.08v + 311.95) Bpoint1.Left = Int(t1.Text * 146.8 + 22.2) Bpoint2.Left = Int(t2.Text * 146.8 + 22.2) Bpoint3.Left = Int(t3.Text * 146.8 + 22.2) Bpoint4.Left = Int(t4.Text * 146.8 + 22.2) Bpoint5.Left = Int(t5.Text * 146.8 + 22.2) Bpoint1.Top = Int(v1.Text * (-256.08) + 311.95) Bpoint2.Top = Int(v2.Text * (-256.08) + 311.95) Bpoint3.Top = Int(v3.Text * (-256.08) + 311.95) Bpoint4.Top = Int(v4.Text * (-256.08) + 311.95) Bpoint5.Top = Int(v5.Text * (-256.08) + 311.95) 'もし、簡易表示の値が0なら、○deta2の○にマーカーを重ねる。 If (t1.Text = 0.0) Then Bpoint1.Top = 43 Bpoint1.Left = 376 End If If (t2.Text = 0.0) Then Bpoint2.Top = 43 Bpoint2.Left = 376 End If If (t3.Text = 0.0) Then Bpoint3.Top = 43 Bpoint3.Left = 376 End If If (t4.Text = 0.0) Then Bpoint4.Top = 43 Bpoint4.Left = 376 End If If (t5.Text = 0.0) Then Bpoint5.Top = 43 Bpoint5.Left = 376 End If End Sub End Class