fixed some servo bugs

This commit is contained in:
Tristan Hearn
2013-05-08 18:33:30 -04:00
parent 13a163482b
commit e9cc111db4
3 changed files with 35 additions and 33 deletions

View File

@@ -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):

View File

@@ -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**

View File

@@ -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)
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