-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add RPI PIO example for quadrature encoder. #6894
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
Open
sanyi
wants to merge
3
commits into
micropython:master
Choose a base branch
from
sanyi:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
""" | ||
This implementation offloads the quadrature sensor continuous pooling to the StateMachine pio_quadrature, | ||
pio_quadrature will detect state changes and push the values then raises an interrupt that is handled by | ||
encoder_state_changed_irq_handler | ||
|
||
encoder_state_changed_irq_handler was inspired from | ||
https://electronics.stackexchange.com/questions/360637/quadrature-encoder-most-efficient-software-implementation | ||
|
||
""" | ||
|
||
from machine import Pin | ||
import rp2 | ||
from rp2 import PIO, StateMachine | ||
import utime | ||
import array | ||
|
||
|
||
state_look_up_table = array.array("b", [ | ||
#Direction = 1 | ||
0, # 00 to 00 | ||
-1, # 00 to 01 | ||
+1, # 00 to 10 | ||
+2, # 00 to 11 | ||
|
||
+1, # 01 to 00 | ||
0, # 01 to 01 | ||
+2, # 01 to 10 | ||
-1, # 01 to 11 | ||
|
||
-1, # 10 to 00 | ||
+2, # 10 to 01 | ||
0, # 10 to 10 | ||
+1, # 10 to 11 | ||
|
||
+2, # 11 to 00 | ||
+1, # 11 to 01 | ||
-1, # 11 to 10 | ||
0, # 11 to 11 | ||
|
||
#Direction = 0 | ||
0, # 00 to 00 | ||
-1, # 00 to 01 | ||
+1, # 00 to 10 | ||
-2, # 00 to 11 | ||
|
||
+1, # 01 to 00 | ||
0, # 01 to 01 | ||
-2, # 01 to 10 | ||
-1, # 01 to 11 | ||
|
||
-1, # 10 to 00 | ||
-2, # 10 to 01 | ||
0, # 10 to 10 | ||
+1, # 10 to 11 | ||
|
||
-2, # 11 to 00 | ||
+1, # 11 to 01 | ||
-1, # 11 to 10 | ||
0, # 11 to 11 | ||
]) | ||
|
||
|
||
counter = 0 | ||
direction = 0 | ||
lut_index = 0 | ||
|
||
def encoder_state_changed_irq_handler(sm): | ||
global counter, direction, lut_index | ||
while sm.rx_fifo(): | ||
lut_index |= sm.get() & 3 | ||
counter += state_look_up_table[lut_index] | ||
if state_look_up_table[lut_index] != 0: | ||
direction = 1 if (state_look_up_table[lut_index] > 0) else 0 | ||
lut_index = ((lut_index << 2) & 0b1100) | (direction << 4) | ||
|
||
|
||
@rp2.asm_pio() | ||
def pio_quadrature(in_init=rp2.PIO.IN_LOW): | ||
wrap_target() | ||
label("again") | ||
in_(pins, 2) | ||
mov(x, isr) | ||
jmp(x_not_y, "push_data") | ||
mov(isr, null) | ||
jmp("again") | ||
label("push_data") | ||
push() | ||
irq(block, rel(0)) | ||
mov(y, x) | ||
wrap() | ||
|
||
|
||
|
||
sm = StateMachine(0, pio_quadrature, freq=160000, in_base=Pin(2)) | ||
sm.irq(encoder_state_changed_irq_handler) | ||
sm.exec("set(y, 99)") # add a last value for y that would be always different then the input | ||
sm.active(1) | ||
|
||
while True: | ||
utime.sleep(2) | ||
print(counter) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.