Last active
December 13, 2016 08:16
-
-
Save ojisan/dc6e45a2c3b611f6eb7d2ec70410041a to your computer and use it in GitHub Desktop.
WiFi_Udp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!mruby | |
#GR-CITRUS Version 2.15 | |
#ここには全体で使用する変数を定義しています | |
@Usb = Serial.new(0,115200) | |
################################# | |
# I2C L3GD20 | |
################################# | |
@L3GD20 = 0x6A # 0b01101011 ジャイロセンサのアドレス ADDRESS-> 0x6A or 0x6B | |
@L3GD20_dps = 0.0175 # 00:0.00875, 10:0.0175, 30:0.07 ジャイロセンサの分解能(deg/s) | |
@Interval = 500 # 間隔処理の間隔時間が入る(ms) | |
@Usb = Serial.new(0, 115200) #USBシリアル通信の初期化 | |
@Dev = I2c.new(1) #I2C1 pin0,1を選択 | |
#ESP8266を一度停止させる(リセットと同じ) | |
pinMode(5,1) | |
digitalWrite(5,0) # LOW:Disable | |
delay 500 | |
digitalWrite(5,1) # HIGH:Enable | |
if( System.useWiFi() == 0)then | |
@Usb.println "WiFi Card can't use." | |
System.exit() | |
end | |
@Usb.println "WiFi Ready" | |
@Usb.println "WiFi Get Version" | |
@Usb.println WiFi.version | |
@Usb.println "WiFi disconnect" | |
@Usb.println WiFi.disconnect | |
@Usb.println "WiFi connecting" | |
#Usb.println WiFi.connect("TAROSAY","37000") | |
@Usb.println WiFi.connect("xxxxxxxxxx","xxxxxxxxxx") | |
@Usb.println "WiFi ipconfig" | |
@Usb.println WiFi.ipconfig | |
@Usb.println "Set wifi mode" | |
@Usb.println WiFi.setMode 3 #Station-Mode & SoftAPI-Mode | |
@Usb.println "Enable multiple connection" | |
@Usb.println WiFi.multiConnect 1 | |
@Usb.println "Create a UDP transmission, for example, id is 4" | |
#UDP通信,WAMIKAN受信:5555, 送信: 5556 | |
@Usb.println WiFi.udpOpen(4,"192.168.1.3",5555,5556) | |
#// Motor setup | |
def set_up() | |
#//L Motor PinSET | |
pinMode(14, 1) | |
pinMode(15, 1) | |
#//R Motor PinSET | |
pinMode(16, 1) | |
pinMode(17, 1) | |
end | |
def motor_stop() | |
pwm(14, 0) | |
pwm(15,0) | |
pwm(16, 0) | |
pwm(17, 0) | |
end | |
def l_motorf() | |
pwm(14, 0) | |
pwm(15, 255) | |
end | |
def l_motorb() | |
pwm(14, 255) | |
pwm(15, 0) | |
end | |
def r_motorf() | |
pwm(16, 0) | |
pwm(17, 255) | |
end | |
def r_motorb() | |
pwm(16, 255) | |
pwm(17, 0) | |
end | |
################################### | |
# I2CのWrite | |
################################### | |
def I2cWrite(id, add, dat) | |
@Dev.begin(id) | |
@Dev.lwrite(add) | |
@Dev.lwrite(dat) | |
@Dev.end(1) | |
end | |
################################### | |
# ジャイロセンサの初期化 | |
################################### | |
def initGyro() | |
L3GD20_CTRL_REG1 = 0x20 # Control register | |
L3GD20_ENABLE = 0x0F | |
L3GD20_CTRL_REG4 = 0x23 # Control register | |
L3GD20_CTRL_REG3 = 0x22 # Control register | |
L3GD20_RANGE = 0x10 # 00:+-250dps, 10:+-500dps, 30:+-2000dps | |
#@L3GD20_dps = 0.0175 # 00:0.00875, 10:0.0175, 30:0.07 | |
# Turn on all axes, disable power down | |
I2cWrite(@L3GD20, L3GD20_CTRL_REG1, L3GD20_ENABLE) | |
delay(100) #100ms待つ | |
# +-500dps | |
I2cWrite(@L3GD20, L3GD20_CTRL_REG4, L3GD20_RANGE) | |
delay(100) #100ms待つ | |
end | |
################################### | |
# I2CのREAD1 | |
################################### | |
def I2cRead1(id, addL) | |
@Dev.begin(id) | |
@Dev.lwrite(addL) | |
@Dev.end(1) | |
@Dev.request(id, 1) | |
dl = @Dev.lread() | |
return dl | |
end | |
################################### | |
# I2CのREAD2 | |
################################### | |
def I2cRead2(id, addL, addH) | |
@Dev.begin(id) | |
@Dev.lwrite(addL) | |
@Dev.end(1) | |
@Dev.request(id, 1) | |
dl = @Dev.lread() | |
@Dev.begin(id) | |
@Dev.lwrite(addH) | |
@Dev.end(1) | |
@Dev.request(id, 1) | |
dh = @Dev.lread() | |
return dh*256 + dl | |
end | |
#// main start | |
@Usb.println("main") | |
set_up() | |
motor_stop() | |
initGyro() | |
sw = 1 | |
#UDP 送受信開始 | |
#Usb.println "UDP受信した分がarray配列で返ります" | |
while (true) do | |
#500.times do | |
#角速度を取得します -------------------------------------- | |
v0 = I2cRead2(@L3GD20, 0x28, 0x29) | |
v1 = I2cRead2(@L3GD20, 0x2A, 0x2B) | |
v2 = I2cRead2(@L3GD20, 0x2C, 0x2D) | |
if v0 > 32767 | |
v0 = v0 - 65536 | |
end | |
if v1 > 32767 | |
v1 = v1 - 65536 | |
end | |
if v2 > 32767 | |
v2 = v2 - 65536 | |
end | |
v0 = (v0 * @L3GD20_dps).round(1) | |
v1 = (v1 * @L3GD20_dps).round(1) | |
v2 = (v2 * @L3GD20_dps).round(1) | |
@Usb.print("gyairo:" + v0.to_s + "," + v1.to_s + "," + v2.to_s + "\n\r") | |
#--------------------------------------------------------- | |
array = WiFi.recv 4 #受信データがない場合は array[0]に -1 が返ります | |
if(array[0] >= 0)then | |
@Usb.println array[0].to_s | |
if(array[0] == 0x66) then #//"f" | |
WiFi.send 4, "MC11" | |
l_motorf() | |
r_motorf() | |
@Usb.println("motor FF") | |
end | |
if(array[0] == 0x62) then #//"b" | |
WiFi.send 4, "MC21" | |
l_motorb() | |
r_motorb() | |
@Usb.println("motor back") | |
end | |
if(array[0] == 0x6C) then #//"l" | |
WiFi.send 4, "MC31" | |
l_motorb() | |
r_motorf() | |
@Usb.println("motor left") | |
end | |
if(array[0] == 0x72) then #//"r" | |
WiFi.send 4, "MC41" | |
l_motorf() | |
r_motorb() | |
@Usb.println("motor right") | |
end | |
if(array[0] == 0x73) then #//"s" | |
WiFi.send 4, "MC51" | |
motor_stop() | |
@Usb.println("motor stop") | |
end | |
if(array[0] == 0x6D) then #//"m" | |
@Usb.println WiFi.send(4, "MA" + 0x20.chr + v0.to_s + 0x20.chr + v1.to_s + 0x20.chr + v2.to_s + "\r\n").to_s | |
end | |
if(array[0] == 0x65) then #//"e" | |
@Usb.println WiFi.cClose 4 | |
@Usb.println WiFi.disconnect | |
break | |
end | |
end | |
#LEDを点滅させます | |
led(sw) | |
sw = 1 - sw | |
delay 100 | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Socketクラスの呼び出し | |
Imports System | |
Imports System.Text | |
Imports System.IO | |
Imports System.Net | |
Imports System.Net.Sockets | |
Imports Microsoft.VisualBasic | |
Public Class Form1 | |
'共通変数の定義 | |
Dim returnData As String | |
Dim temp As Long | |
'送信コマンドデータ定義 | |
Dim Forward As Byte() = Encoding.ASCII.GetBytes("f") | |
Dim Back As Byte() = Encoding.ASCII.GetBytes("b") | |
Dim L_turn As Byte() = Encoding.ASCII.GetBytes("l") | |
Dim R_turn As Byte() = Encoding.ASCII.GetBytes("r") | |
Dim Stp As Byte() = Encoding.ASCII.GetBytes("s") | |
Dim Measure As Byte() = Encoding.ASCII.GetBytes("m") | |
Dim Shut As Byte() = Encoding.ASCII.GetBytes("e") | |
'Receiving Dimension | |
Dim threads1 As New System.Threading.Thread(AddressOf Receive) | |
Dim x_gyairo As String | |
Dim y_gyairo As String | |
Dim z_gyairo As String | |
Dim str As String = "" | |
Dim LoopFlag As Boolean = True | |
'Socketクラス インスタンス生成 | |
Dim udpClient As New UdpClient(5555) 'ローカルポート5555でインスタンス生成 | |
'リモートホスト定義(任意アドレス) | |
Dim RemoteIPEndPoint As IPEndPoint = Nothing '相手は特に決めない | |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load | |
threads1.Start() | |
Timer1.Enabled = False 'タイマは停止させる | |
Timer3.Enabled = False | |
End Sub | |
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick | |
Timer3.Enabled = False | |
TextBox2.Text = str | |
TextBox6.Text = x_gyairo | |
TextBox7.Text = y_gyairo | |
TextBox8.Text = z_gyairo | |
Timer3.Enabled = True | |
End Sub | |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click | |
'リモートの接続(IPアドレスで指定) | |
Try | |
udpClient.Connect("192.168.1.5", 5556) '通信相手を特定する | |
TextBox1.Text = "接続成功!" | |
Timer3.Interval() = 10 '10msecタイマ起動 | |
Timer3.Enabled() = True 'タイマ1起動 | |
Catch ex As Exception | |
TextBox1.Text = "接続失敗?" | |
End Try | |
End Sub | |
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click | |
udpClient.Send(Shut, Shut.Length) | |
Timer3.Enabled = False | |
Timer1.Enabled = False 'タイマ1停止 | |
udpClient.Close() 'UDPのクローズ | |
Close() | |
End Sub | |
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click | |
'現在状態によりオンオフを反転制御 | |
udpClient.Send(Forward, Forward.Length) | |
Button4.BackColor = Color.Green | |
Button5.BackColor = Color.Green | |
Button7.BackColor = Color.Green | |
Button8.BackColor = Color.Green | |
End Sub | |
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click | |
'現在状態によりオンオフを反転制御 | |
udpClient.Send(Back, Back.Length) | |
Button3.BackColor = Color.Green | |
Button5.BackColor = Color.Green | |
Button7.BackColor = Color.Green | |
Button8.BackColor = Color.Green | |
End Sub | |
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click | |
udpClient.Send(L_turn, L_turn.Length) | |
Button3.BackColor = Color.Green | |
Button4.BackColor = Color.Green | |
Button7.BackColor = Color.Green | |
Button8.BackColor = Color.Green | |
End Sub | |
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click | |
udpClient.Send(R_turn, R_turn.Length) | |
Button3.BackColor = Color.Green | |
Button4.BackColor = Color.Green | |
Button5.BackColor = Color.Green | |
Button8.BackColor = Color.Green | |
End Sub | |
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click | |
udpClient.Send(Stp, Stp.Length) | |
Button3.BackColor = Color.Green | |
Button4.BackColor = Color.Green | |
Button5.BackColor = Color.Green | |
Button7.BackColor = Color.Green | |
End Sub | |
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click | |
'計測要求用タイマ起動 | |
If Timer1.Enabled = False Then | |
Timer1.Interval() = 500 '500msecタイマ起動 | |
Timer1.Enabled() = True 'タイマ1起動 | |
Button6.Text = "停止" | |
Else | |
Timer1.Enabled = False | |
Button6.Text = "計測" | |
End If | |
End Sub | |
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick | |
'計測要求 | |
udpClient.Send(Measure, Measure.Length) | |
End Sub | |
Private Sub Receive() | |
Do | |
Try | |
Dim receiveBytes As Byte() = udpClient.Receive(RemoteIPEndPoint) | |
'↑受信するまでこの行でとまっています。 | |
Dim returnData As String = System.Text.Encoding.Default.GetString(receiveBytes) | |
str = returnData | |
'RemoteIpEndPoint.Address.ToString() '送信側のアドレスが格納されてます | |
'RemoteIpEndPoint.Port.ToString() '送信側のポート番号が格納されてます | |
If receiveBytes(0) = &H4D Then 'Mの場合 | |
' ***** motor 制御の応答の場合 ***** | |
If receiveBytes(1) = &H43 Then 'Cの場合 | |
If receiveBytes(2) = &H31 Then 'MC1前進の場合 | |
'オンオフ状態により色とテキストを変更 | |
If receiveBytes(3) = &H31 Then | |
Button3.BackColor = Color.Red | |
End If | |
ElseIf receiveBytes(2) = &H32 Then 'MC2後退の場合 | |
'オンオフ状態により色とテキストを変更 | |
If receiveBytes(3) = &H31 Then | |
Button4.BackColor = Color.Red | |
End If | |
ElseIf receiveBytes(2) = &H33 Then 'MC3左回転の場合 | |
'オンオフ状態により色とテキストを変更 | |
If receiveBytes(3) = &H31 Then | |
Button5.BackColor = Color.Red | |
End If | |
ElseIf receiveBytes(2) = &H34 Then 'MC4右回転の場合 | |
'オンオフ状態により色とテキストを変更 | |
If receiveBytes(3) = &H31 Then | |
Button7.BackColor = Color.Red | |
End If | |
ElseIf receiveBytes(2) = &H35 Then 'MC5STOPの場合 | |
'オンオフ状態により色とテキストを変更 | |
If receiveBytes(3) = &H31 Then | |
Button8.BackColor = Color.Red | |
End If | |
End If | |
' ***** ジャイロセンサ計測応答の場合 ***** | |
ElseIf receiveBytes(1) = &H41 Then 'MAの場合 | |
'ジャイロデータ変換と表示 | |
Dim GyairoString As String = returnData | |
Dim GyairoArray() As String = Split(GyairoString) | |
x_gyairo = GyairoArray(1) | |
y_gyairo = GyairoArray(2) | |
z_gyairo = GyairoArray(3) | |
End If | |
End If | |
Catch err As Exception | |
'Console.WriteLine(err.ToString()) | |
End Try | |
Loop While (LoopFlag) | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment