By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

keyboard controlling arduino rc car with UDP packets (VB 2008)

P: 1
Hi, this is my first post on these forums. Hope this is in the right place.

Doing a college project where we control an rc car wired into an Arduino over the network from a computer.

It's been brilliant doing this project and all the stuff I've learned from the electronics to the coding, and I've gotten pretty well as far as I thought I could with a lot of help from this forum and many others, so thanks for that.

Decided to do the coding in VB 2008 cos of how quick and easy it was to get things running with VB in first year, but there was no constraint on language choice, and if anyone thinks another language is better suited to this I'd love to hear thoughts on that too. Anyway, having not used VB in a couple of years this forum and a few others were invaluable for rooting out which methods and things I needed to put this together, couldn't have even gotten started without it. But I wanted to postpone posting for as long as possible and get as much done on my own as I could first, which I think I have.

I have a solution pretty much working, and I wanted to get more expert opinions on how it could be improved or implemented better (even if it should be scrapped and a different approach taken, I'm sure it's far from perfect).

Here's the skiinny:

VB app checks for held keys with GetAsyncKeyState, and various combos (of arrow keys) cause particular character to be sent in UDP packet to Arduino, which does it's thing based on the character in the packet. The held keys was chosen as the most intuitive method of controlling an rc car from the pc, and I'm using a timer's tick to run the GetAsyncKeyState, and what I suspect might be a somewhat convoluted system of variable comparisons to prevent a packet from being sent until the state of the held keys changes.
UDP was chosen for speed, though I'm really not sure what difference it would make in the real world if trying to control the rc over the internet say with live keyboard control, and whether it would be viable with UDP and not an alternative, or whether it wouldn't make a practical difference, or whether it wouldn't be viable at all!

Anyway, the thing is working, and sending packets only on initial press or release of key(s). The one glitch I've so far detected is that when releasing all the keys, which should send the "z" character and stop the car, the same character that was previously sent is re-sent, rather than the 'stop' character "z".

So here's the code, VB 2008:

__________________________________________________ ____

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4.  
  5. Public Class Form1
  6.  
  7.     Dim GLOIP As IPAddress
  8.     Dim GLOINTPORT As Integer
  9.     Dim bytCommand As Byte() = New Byte() {}
  10.     Dim udpClient As New UdpClient
  11.     Dim controlChoice As New Integer
  12.     Dim controlChoicePrev As New Integer
  13.     Dim cc As String = "z"
  14.  
  15.  
  16.  
  17.     Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
  18.     Dim WithEvents Tmr As New Timer
  19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.         controlChoice = 0
  21.         controlChoicePrev = controlChoice
  22.  
  23.         Tmr.Interval = 100
  24.         Tmr.Start()
  25.     End Sub
  26.     Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
  27.         Tmr.Stop()
  28.  
  29.         If Not controlChoice = controlChoicePrev Then
  30.  
  31.             'UDP_______________________________________________________
  32.  
  33.             Try
  34.                 GLOIP = IPAddress.Parse("192.168.1.177")
  35.                 GLOINTPORT = 8888
  36.                 udpClient.Connect(GLOIP, GLOINTPORT)
  37.                 bytCommand = Encoding.ASCII.GetBytes(cc)
  38.                 udpClient.Send(bytCommand, bytCommand.Length)
  39.  
  40.             Catch ex As Exception
  41.                 Console.WriteLine(ex.Message)
  42.  
  43.             End Try
  44.  
  45.             'UDP_______________________________________________________
  46.  
  47.         End If
  48.  
  49.  
  50.         If GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down) Then
  51.  
  52.  
  53.             Select Case True
  54.  
  55.                 Case GetAsyncKeyState(Keys.Up)
  56.  
  57.                     If (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left)) Then
  58.                         Label1.Text = "Forwards Left"
  59.                         controlChoicePrev = controlChoice
  60.                         controlChoice = 11
  61.                         cc = "b"
  62.  
  63.                     ElseIf (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right)) Then
  64.                         Label1.Text = "Forwards Right"
  65.                         controlChoicePrev = controlChoice
  66.                         controlChoice = 12
  67.                         cc = "c"
  68.  
  69.                     Else
  70.                         Label1.Text = "Forwards"
  71.                         controlChoicePrev = controlChoice
  72.                         controlChoice = 10
  73.                         cc = "a"
  74.  
  75.                     End If
  76.  
  77.  
  78.                 Case GetAsyncKeyState(Keys.Down)
  79.  
  80.  
  81.  
  82.  
  83.                     If (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left)) Then
  84.                         Label1.Text = "Backwards Left"
  85.                         controlChoicePrev = controlChoice
  86.                         controlChoice = 21
  87.                         cc = "e"
  88.  
  89.                     ElseIf (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right)) Then
  90.                         Label1.Text = "Backwards Right"
  91.                         controlChoicePrev = controlChoice
  92.                         controlChoice = 22
  93.                         cc = "f"
  94.  
  95.                     Else
  96.                         Label1.Text = "Backwards"
  97.                         controlChoicePrev = controlChoice
  98.                         controlChoice = 20
  99.                         cc = "d"
  100.  
  101.                     End If
  102.  
  103.             End Select
  104.  
  105.         Else
  106.             Label1.Text = "Stopped"
  107.             controlChoicePrev = controlChoice
  108.             controlChoice = 0
  109.  
  110.         End If
  111.  
  112.         Tmr.Start()
  113.  
  114.     End Sub
  115. End Class
__________________________________________________ _______





So like I said, I'm just wondering if anyone would comment on this and make suggestions/crits.

Many TIA

PS. I'm still pretty code illiterate, so if layman's terms and really explicit and comprehensive and well commented code snippets is not too big an ask... like when people start using terms like call and pass, invoke, etc, I can just about follow things but not always. Sorry...
Mar 20 '13 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 83
Hey kickpuncher,
I hope you don`t mind but, i rewrote some of your code and got it so it does not send the key again on release and so that when all keys are released it will send z once to stop the car. I think that`s what you where looking for.

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Imports System.Text
  3. Imports System.Net
  4. Imports System.Net.Sockets
  5.  
  6. Public Class Form1
  7.  
  8.     Dim GLOIP As IPAddress
  9.     Dim GLOINTPORT As Integer
  10.     Dim bytCommand As Byte() = New Byte() {}
  11.     Dim udpClient As New UdpClient
  12.     Dim cc As String = "z"
  13.     Dim cclast As String = ""
  14.  
  15.     Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
  16.     Dim WithEvents Tmr As New Timer
  17.  
  18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19.         Tmr.Interval = 100
  20.         Tmr.Start()
  21.     End Sub
  22.  
  23.     Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
  24.         Tmr.Stop()
  25.  
  26.         If CBool(GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down)) Then
  27.  
  28.             Select Case True
  29.  
  30.                 Case CBool(GetAsyncKeyState(Keys.Up))
  31.  
  32.                     If CBool((GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left))) Then
  33.                         Label1.Text = "Forwards Left"
  34.                         cc = "b"
  35.  
  36.                     ElseIf CBool((GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right))) Then
  37.                         Label1.Text = "Forwards Right"
  38.                         cc = "c"
  39.  
  40.                     Else
  41.                         Label1.Text = "Forwards"
  42.                         cc = "a"
  43.  
  44.                     End If
  45.  
  46.                 Case CBool(GetAsyncKeyState(Keys.Down))
  47.  
  48.                     If CBool((GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left))) Then
  49.                         Label1.Text = "Backwards Left"
  50.                         cc = "e"
  51.  
  52.                     ElseIf CBool((GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right))) Then
  53.                         Label1.Text = "Backwards Right"
  54.                         cc = "f"
  55.  
  56.                     Else
  57.                         Label1.Text = "Backwards"
  58.                         cc = "d"
  59.  
  60.                     End If
  61.  
  62.             End Select
  63.  
  64.         ElseIf Not cc = "z" And Not CBool(GetAsyncKeyState(Keys.Down)) And Not CBool(GetAsyncKeyState(Keys.Up)) Then
  65.             cc = "z"
  66.             Label1.Text = "Stopped"
  67.         End If
  68.  
  69.         If Not cclast = cc Then
  70.             'UDP______________________________________________ _________
  71.  
  72.             Try
  73.                 GLOIP = IPAddress.Parse("192.168.1.177")
  74.                 GLOINTPORT = 8888
  75.                 udpClient.Connect(GLOIP, GLOINTPORT)
  76.                 bytCommand = Encoding.ASCII.GetBytes(cc)
  77.                 udpClient.Send(bytCommand, bytCommand.Length)
  78.  
  79.             Catch ex As Exception
  80.                 Console.WriteLine(ex.Message)
  81.  
  82.             End Try
  83.  
  84.             'UDP______________________________________________ _________
  85.         End If
  86.  
  87.         cclast = cc
  88.  
  89.         Tmr.Start()
  90.  
  91.     End Sub
  92.  
  93. End Class
Also just a Tip you should use this line as the very first line of code in the program.
Expand|Select|Wrap|Line Numbers
  1. Option Strict On
It will catch all the errors of type conversion. By placing it in the code it will put a little blue line with a red spot on the end under each error. Then you can put your mouse over the red spot and it will pop up a little red box. You can then click on the red box and it will tell you how to fix the error. It is a very very handy option to use.
Mar 20 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.