Hello,
I'm getting a SerialExceptionTimeout from PySerial while transferring data over the serial to my Arduino. What I'm trying to do, is using my USB Joystick (Logitech Joystick/Gamepad), I'd like to use it to control two servomotors (X, Y), but I keep getting an error in the Python IDLE log. Here's the codes:
Arduino
/*
* ------------------------------
* MultipleSerialServoControl
* ------------------------------
*
* Uses the Arduino Serial library
* (http://arduino.cc/en/Reference/Serial)
* and the Arduino Servo library
* (http://arduino.cc/en/Reference/Servo)
* to control multiple servos from a PC using a USB cable.
*
* Dependencies:
* Arduino 0017 or higher
* (http://www.arduino.cc/en/Main/Software)
* Python servo.py module
* (http://principialabs.com/arduino-python-4-axis-servo-control/)
*
* Created: 23 December 2009
* Author: Brian D. Wendt
* (http://principialabs.com/)
* Version: 1.1
* License: GPLv3
* (http://www.fsf.org/licensing/)
*
*/
// Import the Arduino Servo library
#include <Servo.h>
// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// TO ADD SERVOS:
// Servo servo5;
// etc...
// Common servo setup values
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us
// User input for servo and position
int userInput[3]; // raw input from serial buffer, 3 bytes
int startbyte; // start byte, begin reading input
int servo; // which servo to pulse?
int pos; // servo angle 0-180
int i; // iterator
// LED on Pin 13 for digital on/off demo
int ledPin = 13;
int pinState = LOW;
void setup()
{
// Attach each Servo object to a digital pin
servo1.attach(2, minPulse, maxPulse);
servo2.attach(3, minPulse, maxPulse);
servo3.attach(4, minPulse, maxPulse);
servo4.attach(5, minPulse, maxPulse);
// TO ADD SERVOS:
// servo5.attach(YOUR_PIN, minPulse, maxPulse);
// etc...
// LED on Pin 13 for digital on/off demo
pinMode(ledPin, OUTPUT);
// Open the serial connection, 9600 baud
Serial.begin(9600);
servo1.write(0);
servo2.write(0);
}
void loop()
{
// Wait for serial input (min 3 bytes in buffer)
if (Serial.available() > 2) {
// Read the first byte
startbyte = Serial.read();
// If it's really the startbyte (255) ...
if (startbyte == 255) {
// ... then get the next two bytes
for (i=0;i<2;i++) {
userInput[i] = Serial.read();
}
// First byte = servo to move?
servo = userInput[0];
// Second byte = which position?
pos = userInput[1];
// Packet error checking and recovery
if (pos == 255) { servo = 255; }
// Assign new position to appropriate servo
switch (servo) {
case 1:
servo1.write(pos); // move servo1 to 'pos'
break;
case 2:
servo2.write(pos);
break;
case 3:
servo3.write(pos);
break;
case 4:
servo4.write(pos);
break;
// TO ADD SERVOS:
// case 5:
// servo5.write(pos);
// break;
// etc...
// LED on Pin 13 for digital on/off demo
case 99:
if (pos == 180) {
if (pinState == LOW) { pinState = HIGH; }
else { pinState = LOW; }
}
if (pos == 0) {
pinState = LOW;
}
digitalWrite(ledPin, pinState);
break;
}
}
}
}
Python:
#!/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 = "COM3"
# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600, timeout=1)
# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
axis = "unknown"
if (e.dict['axis'] == 1):
axis = "X"
if (e.dict['axis'] == 0):
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):
abc = int(90 - abs(move))
else:
abc = int(move + 90)
ser.write(chr(255))
ser.write(chr(1))
ser.write(chr(abc))
elif (axis == "Y"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
abc = int(90 - abs(move))
else:
abc = int(move + 90)
ser.write(chr(255))
ser.write(chr(2))
ser.write(chr(abc))
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()
Error:
Traceback (most recent call last):
File "C:\Users\Noah\Desktop\servo.py", line 120, in <module>
main()
File "C:\Users\Noah\Desktop\servo.py", line 116, in main
joystickControl()
File "C:\Users\Noah\Desktop\servo.py", line 97, in joystickControl
handleJoyEvent(e)
File "C:\Users\Noah\Desktop\servo.py", line 72, in handleJoyEvent
ser.write(chr(255))
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 270, in write
raise writeTimeoutError
SerialTimeoutException: Write timeout
I'm using PySerial (Current Version), and Python 2.7.5. I am also using PyGame (Version for Python 2.7.5), but it's not throwing the errors, PySerial is.
Any help is greatly accepted!