Skip to content

Removing servo class in favor of motor.servo #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,59 +173,6 @@ def shift_out(data_pin, clock, value, msb_first=True):
clock.value = True
clock.value = False

class Servo:
"""
Easy control for hobby (3-wire) servos

:param ~microcontroller.Pin pin: PWM pin where the servo is located.
:param int min_pulse: Pulse width (milliseconds) corresponding to 0 degrees.
:param int max_pulse: Pulse width (milliseconds) corresponding to 180 degrees.

Example for Metro M0 Express:

.. code-block:: python

import simpleio
import time
from board import *

pwm = simpleio.Servo(D9)

while True:
pwm.angle = 0
print("Angle: ", pwm.angle)
time.sleep(2)
pwm.angle = pwm.microseconds_to_angle(2500)
print("Angle: ", pwm.angle)
time.sleep(2)
"""
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
self._angle = None

@property
def angle(self):
"""Get and set the servo angle in degrees"""
return self._angle

@angle.setter
def angle(self, degrees):
"""Writes a value in degrees to the servo"""
self._angle = max(min(180, degrees), 0)
pulse_width = self.min_pulse + (self._angle / 180) * (self.max_pulse - self.min_pulse)
duty_percent = pulse_width / 20.0
self.pwm.duty_cycle = int(duty_percent * 65535)

def microseconds_to_angle(self, us): #pylint: disable-msg=no-self-use, invalid-name
"""Converts microseconds to a degree value"""
return map_range(us, self.min_pulse * 1000, self.max_pulse * 1000, 0, 180)

def deinit(self):
"""Detaches servo object from pin, frees pin"""
self.pwm.deinit()

class DigitalOut:
"""
Simple digital output that is valid until reload.
Expand Down