Skip to content
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

Add PIO example for servo control #29

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

zeroflow
Copy link

@zeroflow zeroflow commented Apr 8, 2021

Controls one servo at GP16 and additionally the onboard LED
This demonstrates use of two servos.

Controls one servo at GP16 and additionally the LED (to see, if it's doing anything, and to demonstrate usage of two servo outputs)
@aallan aallan added the enhancement New feature or request label Apr 8, 2021
@aallan
Copy link
Contributor

aallan commented Apr 8, 2021

Could you add a README.adoc file and accompanying wiring diagram, similar to that found in other examples, e.g. see the NeoPixel ring example.

@lurch
Copy link
Contributor

lurch commented Apr 8, 2021

Nice PIO example 🙂
Just a couple of queries: a) the comments talk about "IRQ4", but the code seems to be using relative IRQs? b) is there a reason for using relative IRQs, or would it make sense to use an absolute IRQ so that a single ServoTrigger could be used for multiple Servos?

@zeroflow
Copy link
Author

zeroflow commented Apr 8, 2021

I will add a readme.

Regarding IRQs: Sharing the IRQ did not work, so I used relative IRQs.
That way, the Trigger needs to be the SM before the output program, so the IRQs match.

@aallan
Copy link
Contributor

aallan commented Apr 8, 2021

Regarding IRQs: Sharing the IRQ did not work, so I used relative IRQs.
That way, the Trigger needs to be the SM before the output program, so the IRQs match.

Sounds like this should be in the README! :)

@lurch
Copy link
Contributor

lurch commented Apr 8, 2021

Regarding IRQs: Sharing the IRQ did not work, so I used relative IRQs.

I hope you don't mind, but I had a bit of a fiddle.... 😉 (I'm still getting to grips with PIO myself)

I was able to get it working with only a single Servo_Trigger object by changing servo_trigger() to do:

    irq(clear, 4)         # Clear IRQ4, allows servo code to run again
    irq(4)                # Set IRQ4 again, ready for the wait in servo code

(which I guess then means I need to change the trig_ctr calculation to trig_ctr = (trig_frq // 1000 * trig_target) - 4), and changing servo_prog() to do:

    wait(0, "irq", 4) .side(0) # Wait here for IRQ to be released by trigger SM

😀

Other comments:

  • As this is purely an example, it might be more useful to change the for _ in range(2): to while True: ?
  • IMHO the Servo_Trigger class ought to be renamed ServoTrigger (which is the typical Python convention)
  • Given the wide variability of servos, it's probably worth adding min_pulse and max_pulse arguments to your Servo init-method, and then doing
self.base_pulse = min_pulse
self.free_pulse = max_pulse - min_pulse

But of course these are only suggestions, I'll leave the final decision up to you.

@zeroflow
Copy link
Author

zeroflow commented Apr 8, 2021

I don't mind - I'll check your suggestions later. During my testing, the 1-Trigger 2-Servo code compiled, but the output was garbage.

@zeroflow
Copy link
Author

zeroflow commented Apr 8, 2021

Thanks! The replacement with IRQ/Clear + Wait did work.

@lurch
Copy link
Contributor

lurch commented Apr 8, 2021

When testing the changes that I mentioned above, I had three separate servos all running from a single trigger :) But I had to insert some sleeps between moving each servo individually, because if I tried moving multiple servos at the same time, it tried to pull too much current from my computer's USB port and the Pico reset. (this is fixable by powering the servos from a separate PSU, but I couldn't be bothered to wire that up!)

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

Successfully merging this pull request may close these issues.

3 participants