From 32924da682b6678a7ace2bdef0f7aadfaa1d4baa Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 1 Aug 2017 12:48:18 -0400 Subject: [PATCH 1/2] Added easy control for hobby servos --- simpleio.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/simpleio.py b/simpleio.py index 1c4f0f7..e1cd8aa 100644 --- a/simpleio.py +++ b/simpleio.py @@ -27,6 +27,10 @@ """ import digitalio +<<<<<<< HEAD +======= +import pulseio +>>>>>>> Added easy control for hobby servos import math import time @@ -101,6 +105,70 @@ def shift_out(dataPin, clock, value, msb_first=True): tmpval = bool((value & (1 << i))) dataPin.value = tmpval +<<<<<<< HEAD +======= +class Servo: + """ + Easy control for hobby (3-wire) servos + + :param ~pulseio.PWMOut servoPin: PWM pin where the servo is located. + :param int servoMin: Minimum amount of microseconds allowed. Varies depending on type of servo. + :param int servoMax: Maximum amount of microseconds allowed. Varies depending on type of servo. + + Example for Metro M0 Express: + + .. code-block:: python + + import simpleio + import time + from board import * + + # attach a servo to pin Digital 10 + myServo = simpleio.Servo(D10) + + while True: + # write an angle of 90deg to the servo object + myServo.writeAngle(90) + time.sleep(1) + # write an angle of 150deg to the servo object + myServo.writeAngle(150) + # print the location of the servo + print(myServo.read()) + time.sleep(1) + # write to the servo in microseconds + myServo.writeMicroseconds(5500) + time.sleep(1) + """ + def __init__(self, servoPin, servoMin = 0.5, servoMax = 2.5): + self.myServo = pulseio.PWMOut(servoPin, frequency = 50) + self.servoMin = servoMin + self.servoMax = servoMax + # prevent weird values due to read before write calls + self.angle = 90 + + def writeAngle(self, angle): + """Writes a value in degrees to the servo""" + self.angle = max(min(180, angle), 0) + pulseWidth = 0.5 + (self.angle / 180) * (self.servoMax - self.servoMin) + dutyPercent = pulseWidth / 20.0 + self.myServo.duty_cycle = int(dutyPercent * 65535) + + def writeMicroseconds(self, uS): + """Writes a value in microseconds to the servo""" + ms = (uS/1000) + ms = max(min(self.servoMax, ms), self.servoMin) + dutyPercent = ms / 20.0 + self.myServo.duty_cycle = int(dutyPercent * 65535) + + + def detach(self): + """Detaches servo object from pin, frees pin""" + self.myServo.detach() + + def read(self): + return self.angle + +>>>>>>> Added easy control for hobby servos class DigitalOut: """ Simple digital output that is valid until soft reset. From 992c3b7b92d43fefc04f68dd46323b0a017dd20d Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 1 Aug 2017 13:59:24 -0400 Subject: [PATCH 2/2] =?UTF-8?q?fixed=20per=20tannewt=E2=80=99s=20review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: brentru --- simpleio.py | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/simpleio.py b/simpleio.py index e1cd8aa..e3451f8 100644 --- a/simpleio.py +++ b/simpleio.py @@ -27,10 +27,7 @@ """ import digitalio -<<<<<<< HEAD -======= import pulseio ->>>>>>> Added easy control for hobby servos import math import time @@ -105,15 +102,13 @@ def shift_out(dataPin, clock, value, msb_first=True): tmpval = bool((value & (1 << i))) dataPin.value = tmpval -<<<<<<< HEAD -======= class Servo: """ Easy control for hobby (3-wire) servos - :param ~pulseio.PWMOut servoPin: PWM pin where the servo is located. - :param int servoMin: Minimum amount of microseconds allowed. Varies depending on type of servo. - :param int servoMax: Maximum amount of microseconds allowed. Varies depending on type of servo. + :param ~pulseio.PWMOut pin: PWM pin where the servo is located. + :param int min_pulse: Minimum amount of microseconds allowed. Varies depending on type of servo. + :param int max_pulse: Maximum amount of microseconds allowed. Varies depending on type of servo. Example for Metro M0 Express: @@ -124,51 +119,49 @@ class Servo: from board import * # attach a servo to pin Digital 10 - myServo = simpleio.Servo(D10) + pwm = simpleio.Servo(D10) while True: # write an angle of 90deg to the servo object - myServo.writeAngle(90) + pwm.set_angle(90) time.sleep(1) # write an angle of 150deg to the servo object - myServo.writeAngle(150) + pwm.set_angle(150) # print the location of the servo - print(myServo.read()) + print(pwm.read()) time.sleep(1) # write to the servo in microseconds - myServo.writeMicroseconds(5500) + pwm.set_Microseconds(5500) time.sleep(1) """ - def __init__(self, servoPin, servoMin = 0.5, servoMax = 2.5): - self.myServo = pulseio.PWMOut(servoPin, frequency = 50) - self.servoMin = servoMin - self.servoMax = servoMax + def __init__(self, pin, min_pulse = 0.5, max_pulse = 2.5): + self.pwm = pulseio.PWMOut(pin, frequency = 50) + self.min_pulse = min_pulse + self.max_pulse = max_pulse # prevent weird values due to read before write calls self.angle = 90 - def writeAngle(self, angle): + def set_angle(self, angle): """Writes a value in degrees to the servo""" self.angle = max(min(180, angle), 0) - pulseWidth = 0.5 + (self.angle / 180) * (self.servoMax - self.servoMin) + pulseWidth = 0.5 + (self.angle / 180) * (self.max_pulse - self.min_pulse) dutyPercent = pulseWidth / 20.0 - self.myServo.duty_cycle = int(dutyPercent * 65535) + self.pwm.duty_cycle = int(dutyPercent * 65535) - def writeMicroseconds(self, uS): + def set_Microseconds(self, uS): """Writes a value in microseconds to the servo""" ms = (uS/1000) - ms = max(min(self.servoMax, ms), self.servoMin) + ms = max(min(self.max_pulse, ms), self.min_pulse) dutyPercent = ms / 20.0 - self.myServo.duty_cycle = int(dutyPercent * 65535) - + self.pwm.duty_cycle = int(dutyPercent * 65535) def detach(self): """Detaches servo object from pin, frees pin""" - self.myServo.detach() + self.pwm.detach() def read(self): return self.angle ->>>>>>> Added easy control for hobby servos class DigitalOut: """ Simple digital output that is valid until soft reset.