Go Down

Topic: Pyton to VB.net Joystick Arduino (Read 1 time) previous topic - next topic


Sep 08, 2010, 02:16 am Last Edit: Sep 08, 2010, 02:19 am by rw Reason: 1
Hi people!

I want to convert this http://principialabs.com/joystick-control-of-a-servo/ program pyton to VB 2010.

Code: [Select]

import serial
import pygame

# allow multiple joysticks
joy = []

# Arduino USB port address (try "COM5" on Win32)
usbport = "/dev/ttyUSB0"

# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600)

[u]# handle joystick event
def handleJoyEvent(e):
   if e.type == pygame.JOYAXISMOTION:[/u]
       axis = "unknown"
       if (e.dict['axis'] == 0):
           axis = "X"

       if (e.dict['axis'] == 1):
           axis = "Y"

       if (e.dict['axis'] == 2):
           axis = "Throttle"

       if (e.dict['axis'] == 3):
           axis = "Z"

       if (axis != "unknown"):
           str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
           # uncomment to debug
           #output(str, e.dict['joy'])

          [u] # Arduino joystick-servo hack
           if (axis == "X"):
               pos = e.dict['value']
               # convert joystick position to servo increment, 0-180
               move = round(pos * 90, 0)
               if (move < 0):
                   servo = int(90 - abs(move))
                   servo = int(move + 90)[/u]
               # convert position to ASCII character
               servoPosition = chr(servo)
               # and send to Arduino over serial connection
               # uncomment to debug
               #print servo, servoPosition

   elif e.type == pygame.JOYBUTTONDOWN:
       str = "Button: %d" % (e.dict['button'])
       # uncomment to debug
       #output(str, e.dict['joy'])
       # Button 0 (trigger) to quit
       if (e.dict['button'] == 0):
           print "Bye!\n"

# print the joystick position
def output(line, stick):
   print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
   while True:
       e = pygame.event.wait()
       if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):

# main method
def main():
   # initialize pygame
   if not pygame.joystick.get_count():
       print "\nPlease connect a joystick and run again.\n"
   print "\n%d joystick(s) detected." % pygame.joystick.get_count()
   for i in range(pygame.joystick.get_count()):
       myjoy = pygame.joystick.Joystick(i)
       print "Joystick %d: " % (i) + joy[i].get_name()
   print "Depress trigger (button 0) to quit.\n"

   # run joystick listener loop

# allow use as a module or standalone script
if __name__ == "__main__":

Ok, I try some moves and some help to other vb forums where I really want to convert the code (in underline) to vb.net.
This line is to Arduino joystick servo hack. Its just Im need.
Code: [Select]

' Variables needed:
Dim axis As String
Dim pos As Double
Dim move As Integer
Dim servo As Integer

' Ported code
If axis = "X" Then
   pos = ???
   move = CInt(Math.Round(pos * 90, 0))
   If move < 0 Then
       servo = 90 - Math.Abs(move)
       servo = 90 + move
   End If
End IF))

But not I didnt not find some library or function similar in VB.net

Code: [Select]

if e.type == pygame.JOYAXISMOTION:

Can some one help?

Or please, tell me the code in VB or C#  to communicate arduino from joystick via serial?



Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131