Alright to summarise about the project I intend to build on with my Arduino. I'm planning to build an unmanned ground vehicle that will be controlled wireless using the Xbee modules, a computer, a Logitech Attack 3 and lastly, the ever important Python. I had figured out some codes but right now, I have trouble figuring out how to make sure my servo moves as long as I press down the button on the joystick. Its frustrating that I have to keep pressing and (un)pressing the joystick buttons to move the servo. Can anyone help me figure out how to make sure the servo can move as long as I keep the buttons down? And also can anyone guide me about how I can use the analog stick to control motors with PWM? For example, when I move the joystick forwards, two motors will rotate clockwise and when i move the joystick to the left or right, one motor will move forwards and another backwards. And as a bonus if I move the joystick diagonally, one motor will maintain speed/increase speed and the other lower speed/maintain speed? Thanks guys!
Here are my codes.
Python code for joystick event control.
#!/usr/bin/env python
################################################################
# Module: multijoystick.py
# Created: 2 April 2008
# Author: Brian D. Wendt
# http://principialabs.com/
# Version: 0.2
# License: GPLv3
# http://www.fsf.org/licensing/
'''
Provides four-axis joystick servo control from a PC
using the Arduino "MultipleServos" sketch
and the Python "servo.py" serial abstraction module.
Parts of this code were 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/
servo - http://principialabs.com/
Win32 users may also need:
pywin32 - http://sourceforge.net/projects/pywin32/
'''
################################################################
import servo
import pygame
# allow multiple joysticks
joy = []
# 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)
#serv = int(90 + move)
# and send to Arduino over serial connection
#servo.move(1, serv)
# Arduino joystick-servo hack
#if (axis == "Y"):
#pos = e.dict['value']
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
#servo.move(2, serv)
# Arduino joystick-servo hack
#if (axis == "Z"):
#pos = e.dict['value']
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
#servo.move(3, serv)
# Arduino joystick-servo hack
#if (axis == "Throttle"):
#pos = e.dict['value']
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
#servo.move(4, serv)
if e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
# uncomment to debug
# uncomment to debug
output(str, e.dict['joy'])
# Button 8 to quit
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#=========================Rifle control section========================
#-------------------------Axis Y---------------------------------------
if (e.dict['button'] == 1):
#serv = 1
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
servo.move(1, 0)
if (e.dict['button'] == 2):
#serv = 1
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
servo.move(1, 1)
#----------------------------------------------------------------------
#-------------------------Axis X---------------------------------------
if (e.dict['button'] == 3):
#serv = 1
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
servo.move(3, 3)
if (e.dict['button'] == 4):
#serv = 1
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
servo.move(3, 4)
#======================================================================
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#========================Rifle cocking section=========================
for serv in range(0, 180, 1):
if (e.dict['button'] == 5):
#serv = serv + 1
# convert joystick position to servo increment, 0-180
#move = round(pos * 90, 0)
#serv = int(90 + move)
# and send to Arduino over serial connection
servo.move(12, serv)
#======================================================================
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#========================Program Exit==================================
if (e.dict['button'] == 7):
print "Mission End"
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 Button 8 to quit.\n"
# run joystick listener loop
joystickControl()
# allow use as a module or standalone script
if __name__ == "__main__":
main()
and here is the servo.py that communicates with the serial port to Arduino.
#!/usr/bin/env python
################################################
# Module: servo.py
# Created: 2 April 2008
# Author: Brian D. Wendt
# http://principialabs.com/
# Version: 0.2
# License: GPLv3
# http://www.fsf.org/licensing/
'''
Provides a serial connection abstraction layer
for use with Arduino "MultipleServos" sketch.
'''
################################################
import serial
usbport = 'COM7'
ser = serial.Serial(usbport, 9600)
#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"