From e9cc111db4538011cdf0ab337e3caa5bb2bb94ff Mon Sep 17 00:00:00 2001 From: Tristan Hearn Date: Wed, 8 May 2013 18:33:30 -0400 Subject: [PATCH] fixed some servo bugs --- Arduino/arduino.py | 37 +++++++++++++++++-------------------- README.md | 18 +++++++++--------- examples.py | 13 +++++++++---- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Arduino/arduino.py b/Arduino/arduino.py index ec317d7..f11ccde 100644 --- a/Arduino/arduino.py +++ b/Arduino/arduino.py @@ -380,18 +380,19 @@ class Servos(object): def attach(self,pin,min = 544, max = 2400): cmd_str=''.join(["@sva%",str(pin),"%",str(min),"%",str(max),"$!"]) - try: + + while True: self.sr.write(cmd_str) self.sr.flush() - except: - pass - rd = self.sr.readline().replace("\r\n","") - try: - position = int(rd) - self.servo_pos[pin] = position - return 1 - except: - return 0 + + rd = self.sr.readline().replace("\r\n","") + if rd: + break + else: + print "trying to attach servo to pin",pin + position = int(rd) + self.servo_pos[pin] = position + return 1 def detach(self,pin): @@ -407,20 +408,16 @@ class Servos(object): def write(self,pin,angle): position = self.servo_pos[pin] cmd_str=''.join(["@svw%",str(position),"%",str(angle),"$!"]) - try: - self.sr.write(cmd_str) - self.sr.flush() - except: - pass + + self.sr.write(cmd_str) + self.sr.flush() def writeMicroseconds(self,pin,uS): cmd_str=''.join(["@svw%",str(position),"%",str(uS),"$!"]) - try: - self.sr.write(cmd_str) - self.sr.flush() - except: - pass + + self.sr.write(cmd_str) + self.sr.flush() def read(self,pin): diff --git a/README.md b/README.md index a3a7442..0771384 100644 --- a/README.md +++ b/README.md @@ -116,18 +116,18 @@ board.analogWrite(11) #Set analog value (PWM) based on analog measurement **Servo Library Functionality** Support is included for up to 8 servos. -- `Arduino.Servo.attach(pin, min = 544, max = 2400)` Create servo instance. Only 8 servos can be used at one time. -- `Arduino.Servo.read(pin)` Returns the angle of the servo attached to the specified pin -- `Arduino.Servo.write(pin, angle)` Move an attached servo on a pin to a specified angle -- `Arduino.Servo.writeMicroseconds(pin, uS)` Write a value in microseconds to the servo on a specified pin -- `Arduino.Servo.detach(pin)` Detaches the servo on the specified pin +- `Arduino.Servos.attach(pin, min = 544, max = 2400)` Create servo instance. Only 8 servos can be used at one time. +- `Arduino.Servos.read(pin)` Returns the angle of the servo attached to the specified pin +- `Arduino.Servos.write(pin, angle)` Move an attached servo on a pin to a specified angle +- `Arduino.Servos.writeMicroseconds(pin, uS)` Write a value in microseconds to the servo on a specified pin +- `Arduino.Servos.detach(pin)` Detaches the servo on the specified pin ```python #Servo example -board.Servo.attach(9) #declare servo on pin 9 -board.Servo.write(9, 0) #move servo on pin 9 to 0 degrees -print board.Servo.read(9) # should be 0 -board.Servo.detach(9) #free pin 9 +board.Servos.attach(9) #declare servo on pin 9 +board.Servos.write(9, 0) #move servo on pin 9 to 0 degrees +print board.Servos.read(9) # should be 0 +board.Servos.detach(9) #free pin 9 ``` **Software Serial Functionality** diff --git a/examples.py b/examples.py index 8051b3c..efe1f67 100644 --- a/examples.py +++ b/examples.py @@ -71,7 +71,12 @@ def LCD(tx,baud,ssbaud,message, port=""): if __name__=="__main__": - Blink(13,9600) - #LCD(5,9600,9600," test ") - #adjustBrightness(5,11,9600) - #softBlink(11,9600) \ No newline at end of file + a = Arduino('9600', port="/dev/tty.usbserial-A700e08i") + print a.Servos.attach(2),"attach" + import time + i = 0 + while True: + a.Servos.write(2,10*i) + time.sleep(.1) + print a.Servos.read(2),10*i + i+=1 \ No newline at end of file