-
Notifications
You must be signed in to change notification settings - Fork 20
Servo #15
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
Servo #15
Changes from 4 commits
32924da
992c3b7
ae95573
1cb1b14
a06fa57
1e9d44c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
""" | ||
|
||
import digitalio | ||
import pulseio | ||
import math | ||
import time | ||
|
||
|
@@ -101,6 +102,59 @@ def shift_out(dataPin, clock, value, msb_first=True): | |
tmpval = bool((value & (1 << i))) | ||
dataPin.value = tmpval | ||
|
||
class Servo: | ||
""" | ||
Easy control for hobby (3-wire) servos | ||
|
||
:param ~microcontroller.Pin 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to state what min_pulse and max_pulse correspond to angle wise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to state how the uS correspond to the angle
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are assuming that the servo ranges from 0 to 180 degrees correct? Why not just say that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Reworded as:
|
||
|
||
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 = 90 | ||
self._microseconds = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't needed anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted. |
||
|
||
@property | ||
def angle(self): | ||
return self._angle | ||
|
||
@angle.setter | ||
def angle(self, degrees): | ||
"""Writes a value in degrees to the servo""" | ||
self._angle = max(min(180, degrees), 0) | ||
pulseWidth = 0.5 + (self._angle / 180) * (self.max_pulse - self.min_pulse) | ||
dutyPercent = pulseWidth / 20.0 | ||
self.pwm.duty_cycle = int(dutyPercent * 65535) | ||
|
||
def microseconds_to_angle(self, us): | ||
"""Converts microseconds to a degree value""" | ||
return map_range(us, 500, 2500, 0, 180) | ||
|
||
def detach(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant the Servo method itself too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
"""Detaches servo object from pin, frees pin""" | ||
self.pwm.deinit() | ||
|
||
class DigitalOut: | ||
""" | ||
Simple digital output that is valid until soft reset. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the Google Python Style (based on PEP8) for variable naming. https://google.github.io/styleguide/pyguide.html?showone=Naming#Naming
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed servoMin/servoMax to
and
to conform with the Google Python Style Guide