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()
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))
Code:
handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
Or please, tell me the code in VB or C# to communicate arduino from joystick via serial?
Thanks!
Replies
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!
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!!
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...