Skip to content

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

Merged
merged 6 commits into from
Aug 8, 2017
Merged

Servo #15

merged 6 commits into from
Aug 8, 2017

Conversation

brentru
Copy link
Member

@brentru brentru commented Aug 1, 2017

Added easy control for hobby servos with variable constraints. Added an writeMicroseconds method, along with writeAngle.

Addressing Issue #3 - servo control

@brentru brentru requested a review from tannewt August 1, 2017 16:54
Copy link
Member

@tannewt tannewt left a 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):
Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

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:
Copy link
Member

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

Copy link
Member Author

@brentru brentru Aug 3, 2017

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):
Copy link
Member

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.

Copy link
Member Author

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

brentru added 2 commits August 3, 2017 17:43
Copy link
Member

@tannewt tannewt left a 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):
Copy link
Member

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.

Copy link
Member Author

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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

Copy link
Member

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.
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

Copy link
Member

@tannewt tannewt left a 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):
Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

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):
Copy link
Member

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>

Copy link
Member Author

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.

Copy link
Member Author

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.

@tannewt tannewt self-assigned this Aug 6, 2017
Copy link
Member

@tannewt tannewt left a 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.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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):
Copy link
Member

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

Copy link
Member Author

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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted.

@tannewt tannewt merged commit f4d2736 into adafruit:master Aug 8, 2017
@tannewt
Copy link
Member

tannewt commented Aug 8, 2017

Looks good! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants