Hi people!

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

Code:
#!/usr/bin/env python
#
# joystick-servo.py
#
# created 19 December 2007
# copyleft 2007 Brian D. Wendt
# http://principialabs.com/
#
# code adapted from:
# http://svn.lee.org/swarm/trunk/mothernode/python/multijoy.py
#
# NOTE: This script requires the following Python modules:
# pyserial - http://pyserial.sourceforge.net/
# pygame - http://www.pygame.org/
# Win32 users may also need:
# pywin32 - http://sourceforge.net/projects/pywin32/
#

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)

# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
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'])

# 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))
else:
servo = int(move + 90)
# convert position to ASCII character
servoPosition = chr(servo)
# and send to Arduino over serial connection
ser.write(servoPosition)
# 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"
ser.close()
quit()
else:
pass

# 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):
handleJoyEvent(e)

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

# run joystick listener loop
joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
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:
' 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)
Else
servo = 90 + move
End If
End IF))
But not I didnt not find some library or function similar in VB.net

Code:
handleJoyEvent(e):
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?

Thanks!

Views: 636

Reply to This

Replies to This Discussion

I think that it's quite possible
I can help with this code if you like

Here's where I think that can be a good start:
http://www.codeproject.com/KB/directx/joystick.aspx
http://www.slimdx.org/features.php
http://code.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectRe...
Hey Andre!
Sorry anwers too late.

I didnt have time. I wanted to see the links more slowly.
The windows API I dont know why I cant install in my PC...


Sure I want your help!!

Its relly hard to me to make this vb code work with arduino duemilanove.

I took in internet the name of program "vb joystick" write in vb.net 2003 but work fine in vb.net 2010.

But I want to put in this program, the code to take the joystick movement to servo.

I understand the pyton code, the problem is exactly the make the code to take joystick movement to arduino and arduino to servo.


Im really appreciate your help!
Thanks!
Another option: Iron Python runs on the .NET framework, if you ran that python code in there, you should be able to call your functions from your existing VB code without having to re-write anything. Its not that much code, but if you get this working you can easily add more external python in the future without having to re-write.
After spending a bit more time looking at the code, the toughest part is the two lines up at the top:

import serial
import pygame

These probably have dependencies on some of the baseline python stuff, which may or may not be ported to IronPython. It looks like a lot of the important logic is actually happening in pygame, and that is absolutely crucial to have.

You'll either need to get it all working in IronPython or re write the above code, pygame, and serial in VB.
You right, I need to see the both documentation....
What do you think? Is a good start isnt?

Ho lord....my head will explosion!! hahahah

Thanks for your help!
Thanks Tom! I will see right now!!
sharpdevelop has built-in capability to convert between .net languages. Maybe it can do the trick.
I dont know, I saw the site, im not sure the convert pyhton to .net
But i will see more carefuly
Thanks!
Hey people!

I use Iron Pyton to work in VB.net 2010 and I have this windows to show:



If you see, there are handlejoyevent and joystick control.

Can I know ou find information about this two event? If I discovery, I
can find some similar in VB.net write?

Thaks in advance!!
Guys, its me again...
Sorry to borry...but I need help with this.

[CODE]
import serial

usbport = '/dev/ttyUSB0'
ser = serial.Serial(usbport, 9600, timeout=1)
#print ser

def move(servo, angle):
'''Moves the specified servo to the supplied angle.

Arguments:
servo
the servo number to command, an integer from 1-4
angle
the desired servo angle, an integer from 0 to 180

(e.g.) >>> servo.move(2, 90)
... # "move servo #2 to 90 degrees"'''

if (0 <= angle <= 180):
ser.write(chr(255))
ser.write(chr(servo))
ser.write(chr(angle))
else:
print "Servo angle must be an integer between 0 and 180.\n"
[/CODE]

This code is more simple:
I Try to to convert to VB and look like this:

[CODE]
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput

Public Class Form1
Dim servo As Integer
Dim RotationX As Integer


Public Function GetSliders(ByVal servo, ByVal X) As Integer()


If (0 <= X <= 180) Then
SerialPort1.Write(Chr(255))
SerialPort1.Write(Chr(servo))
SerialPort1.Write(Chr(X))

Else
Print("Sorry")

End If

End Function

End Class
[/CODE]

My question is, why i have this warning?

Warning 1 Function 'GetSliders' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

I understood the GetSliders dont return any value, but I put servo and X why this?

and this line:
[CODE]
def move(servo, angle):
[/CODE]
In google I find this information:
In the above code there is really only one new keyword: def. def is the keyword used when defining functions. Arguments are passed in paranthesis just like C, and the return statement can't return multiple values. However, since lists, tuples, and dictionaries are basic types they can be returned instead. I'll illustrate this later. A quick thing to note about Python functions: They always return a value. However, if no return value is specified, or you don't use the return keyword, Python automatically returns the value None.

Ok, so the code similiar in VB.net is the "Function" right?

Thanks and sorry to borrow!

RSS

Social Networking

Contests

Season Two of the Trust Time Trial (T3) Contest has now begun. The fourth round is an accuracy round for multicopters, which requires contestants to fly a cube. The deadline is April 14th.

A list of all T3 contests is here

Groups

Advertisement

© 2013   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service