Skip to content

Check fraction range; add post-constructor access to actuation_range and pulse widths. #10

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 5 commits into from
Jul 3, 2018

Conversation

dhalbert
Copy link
Contributor

The latest Crickit library API doesn't provide explicit constructors for servos, so actuation_range and min_pulse and max_pulse need to be settable after construction.

I just made actuation_range a visible attribute (it was previously hidden).

For pulse widths I added a function to set both, instead of property access. They are not currently saved, but just used to compute duty cycle values Setting one pulse width at a time via a property would require extra code for computation or saving the values, and usually one wants to set both anyway. If you disagree I can make it property access.

@dhalbert dhalbert requested a review from tannewt June 30, 2018 22:22
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.

Two minor things and then good to go! Thanks!

self._pwm_out = pwm_out
self.set_pulse_widths(min_pulse, max_pulse)

def set_pulse_widths(self, min_pulse=750, max_pulse=2250):
Copy link
Member

Choose a reason for hiding this comment

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

I like set_pulse_width_range more.

Copy link
Member

Choose a reason for hiding this comment

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

I dropped the s too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think it shouldn't have default values for the args, so you have to give both? I'm vacillating on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having default values was what motivated the original choice of name.

Copy link
Member

Choose a reason for hiding this comment

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

I like defaults because it allows one to use the name in the call. aka servo.set_pulse_width_range(min_pulse=700, max_pulse=2000) You could even force it with: def set_pulse_widths(self, *, min_pulse=750, max_pulse=2250):

@@ -83,19 +96,19 @@ class Servo(_BaseServo):
"""
def __init__(self, pwm_out, *, actuation_range=180, min_pulse=750, max_pulse=2250):
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
self._actuation_range = actuation_range
self.actuation_range = actuation_range
Copy link
Member

Choose a reason for hiding this comment

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

Add a triple quoted doc string here so it is included in RTD.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the Servo class has a docstring, it's already in RTD. See https://circuitpython.readthedocs.io/projects/motor/en/latest/api.html#adafruit_motor.servo.Servo.

Alphabetically ContinuousServo came first -- maybe that was confusing?

Copy link
Member

Choose a reason for hiding this comment

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

It does for the argument but not the object attribute (like angle).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Right. actuation_angle should look like angle too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was impressively difficult to do this. I can add a docstring after the self.actuation_range = actuation_range, but then it shows up in the RTD as "actuation_range = None". After some Googling, found a hack to add to conf.py to fix it.

The other alternative is to document actuation_range as an :ivar, but then it shows up with the parameters. The current new way looks better.

@dhalbert
Copy link
Contributor Author

dhalbert commented Jul 2, 2018

In motor.py, I can remember the throttle value as self._throttle and just return it, instead of this right now. The code below is more faithful to the underlying pwm, but it's a lot of code that we could just get rid of. What do you think?

    @property
    def throttle(self):
        """How much power is being delivered to the motor. Values range from ``-1.0`` (full
           throttle reverse) to ``1.0`` (full throttle forwards.) ``0`` will stop the motor from
           spinning and ``None`` will let the motor spin freely."""
        if self._positive.duty_cycle == 0 and self._negative.duty_cycle == 0:
            return None
        if self._positive.duty_cycle == 0xffff and self._negative.duty_cycle == 0xffff:
            return float(0)
        if self._positive.duty_cycle > 0 and self._negative.duty_cycle > 0:
            raise RuntimeError("PWMs in invalid state")
        value = max(self._positive.duty_cycle, self._negative.duty_cycle) / 0xffff
        if self._negative.duty_cycle > 0:
            return -1 * value
        return value`

That will also accidentally fix #11.

@tannewt
Copy link
Member

tannewt commented Jul 2, 2018

@dhalbert I'm ok removing it.

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.

Thanks!

@tannewt tannewt merged commit e0b709f into adafruit:master Jul 3, 2018
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jul 4, 2018
Updating https://github.com/adafruit/Adafruit_CircuitPython_AM2320 to 1.1.1 from 1.1.0:
  > add setup.py, add pypi to .travis.yml, misc. tweaks for pypi
  > add build artifacts to .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_APDS to 1.1.1 from 1.1.0:
  > Update apds9960_proximity_simpletest.py
  > Update apds9960_color_simpletest.py
  > setup.py: add adafruit-circuitpython-register to dependencies
  > add setup.py, add pypi to .travis.yml, misc. tweaks for pypi, .gitignore
  > Merge pull request adafruit/Adafruit_CircuitPython_APDS#8 from tannewt/fix_register_in_docs
  > Merge pull request adafruit/Adafruit_CircuitPython_APDS#7 from sommersoft/new_docs
  > Merge pull request adafruit/Adafruit_CircuitPython_APDS#6 from sommersoft/new_docs

Updating https://github.com/adafruit/Adafruit_CircuitPython_AS726x to 1.0.2 from 1.0.1:
  > .travis.yml: add missing `pip` invocation
  > add setup.py, add pypi to .travis.yml, misc. tweaks for pypi, .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_Crickit to 2.0.1 from 1.0.2:
  > change pypi to know it's a single-file library
  > Merge pull request adafruit/Adafruit_CircuitPython_Crickit#7 from dhalbert/rework_api
  > Merge pull request adafruit/Adafruit_CircuitPython_Crickit#6 from dhalbert/rework_api
  > Merge pull request adafruit/Adafruit_CircuitPython_Crickit#5 from dhalbert/rework_api

Updating https://github.com/adafruit/Adafruit_CircuitPython_Se to 1.2.2 from 1.2.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Se#17 from dhalbert/fix-pin-mapping-AGAIN

Updating https://github.com/adafruit/Adafruit_CircuitPython_Motor to 1.2.0 from 1.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Motor#10 from dhalbert/check_fraction_range
  > Merge pull request adafruit/Adafruit_CircuitPython_Motor#9 from dhalbert/better_servo_limits
@dhalbert dhalbert deleted the check_fraction_range branch April 7, 2022 13:16
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