-
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
Conversation
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.
Thank you for picking this up as well! Please rebase this onto the latest master, it looks like you merged it instead. As a rule of thumb, only merge when combining two concurrently modified shared branches. Otherwise a rebase leads to a cleaner branch history by pretending your changes happened after the other changes.
simpleio.py
Outdated
# prevent weird values due to read before write calls | ||
self.angle = 90 | ||
|
||
def writeAngle(self, angle): |
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.
Should be formatted as write_angle
(https://www.python.org/dev/peps/pep-0008/#function-names). I think set_angle makes more sense too.
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.
Modified to conform to the style guide. Write was being used because it's similar to the .write() in arduino
simpleio.py
Outdated
time.sleep(1) | ||
""" | ||
def __init__(self, servoPin, servoMin = 0.5, servoMax = 2.5): | ||
self.myServo = pulseio.PWMOut(servoPin, frequency = 50) |
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'd call this self.pwm instead of myServo because Servo is also the name of the class. It feels like it could be self-referential which would be weird.
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.
myServo was to keep consistency with the Arduino code. self.pwm works as well and makes more sense, changed:
self.pwm = pulseio.PWMOut(pin, frequency = 50)
@@ -101,6 +102,67 @@ def shift_out(dataPin, clock, value, msb_first=True): | |||
tmpval = bool((value & (1 << i))) | |||
dataPin.value = tmpval | |||
|
|||
class Servo: |
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
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
min_pulse
and
max_pulse
to conform with the Google Python Style Guide
simpleio.py
Outdated
myServo.writeMicroseconds(5500) | ||
time.sleep(1) | ||
""" | ||
def __init__(self, servoPin, servoMin = 0.5, servoMax = 2.5): |
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.
The servo prefix isn't useful here. I'd rename them pin
, min_pulse
and max_pulse
.
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.
Agreed, revised to:
def __init__(self, pin, min_pulse = 0.5, max_pulse = 2.5):
Signed-off-by: brentru <robots199@me.com>
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.
Looks much better! Just a couple more things.
simpleio.py
Outdated
dutyPercent = pulseWidth / 20.0 | ||
self.pwm.duty_cycle = int(dutyPercent * 65535) | ||
|
||
def set_Microseconds(self, uS): |
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.
Should this set self.angle as well in case its read back? If so, maybe it makes more sense to make an angle property instead since its both set and read back.
Microseconds should be lowercase too.
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.
set ms as self.angle so it can be read back by read(). Microseconds switched to microseconds to match public functions in the style guide.
simpleio.py
Outdated
dutyPercent = ms / 20.0 | ||
self.pwm.duty_cycle = int(dutyPercent * 65535) | ||
|
||
def detach(self): |
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.
deinit
is the standard name for this in CircuitPython: https://circuitpython.readthedocs.io/en/stable/docs/design_guide.html#lifetime-and-contextmanagers
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.
changed to
self.pwm.deinit()
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 meant the Servo method itself too.
simpleio.py
Outdated
""" | ||
Easy control for hobby (3-wire) servos | ||
|
||
:param ~pulseio.PWMOut pin: PWM pin where the servo is located. |
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.
This should be microcontroller.Pin
I believe. (Thats what items in board
are.)
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.
Renamed
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.
A couple more things.
simpleio.py
Outdated
dutyPercent = ms / 20.0 | ||
self.pwm.duty_cycle = int(dutyPercent * 65535) | ||
|
||
def detach(self): |
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 meant the Servo method itself too.
simpleio.py
Outdated
"""Writes a value in microseconds to the servo""" | ||
ms = (uS/1000) | ||
ms = max(min(self.max_pulse, ms), self.min_pulse) | ||
self.angle = ms |
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 don't think this is what you want because you aren't doing the reverse of line 147. The range of ms will be very different from angle.
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.
Agreed! I used angles as my base unit, such that .setter can use uS as well as Degrees. microseconds_to_angle
helps to convert uS to degrees, which is used by the setter.
simpleio.py
Outdated
# prevent weird values due to read before write calls | ||
self.angle = 90 | ||
|
||
def set_angle(self, angle): |
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'd suggest making angle a property:
@property
def angle(self):
return self.angle
@angle.setter
def angle(self, angle):
<contents of set_angle>
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.
Ok! I made angle a property. The implementation provided caused a "Maximum Recursion Depth Exceeded" error, so it was changed to:
@property def angle(self): return self._angle
The example was modified to reflect these changes.
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.
Also, def angle(self, angle)
was changed to def angle(self, degrees)
to avoid confusion with having both a variable and a method named angle.
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.
A couple more things.
simpleio.py
Outdated
|
||
: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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to state how the uS correspond to the angle
:param int min_pulse: Pulse width (microseconds) for the absolute minimum angle on the servo.
:param int max_pulse: Pulse width (microseconds) for the absolute maximum angle on the servo.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Reworded as:
Pulse width (microseconds) corresponding to 180 degrees
and
Pulse width (microseconds) corresponding to 0 degrees
simpleio.py
Outdated
"""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 comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this to deinit
please
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.
Done!
simpleio.py
Outdated
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted.
…artifacts from old code
Looks good! Thank you! |
Added easy control for hobby servos with variable constraints. Added an writeMicroseconds method, along with writeAngle.
Addressing Issue #3 - servo control