kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Added MP encoder examples
rodzic
fac3e7365a
commit
0c88f7892b
|
@ -7,7 +7,7 @@
|
||||||
- [Motor Song](#motor-song)
|
- [Motor Song](#motor-song)
|
||||||
|
|
||||||
|
|
||||||
## Motor Examples
|
## Examples
|
||||||
|
|
||||||
### Single Motor
|
### Single Motor
|
||||||
[motorshim_single_motor.cpp](motorshim_single_motor.cpp)
|
[motorshim_single_motor.cpp](motorshim_single_motor.cpp)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Encoder MicroPython Examples <!-- omit in toc -->
|
||||||
|
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Read Encoder](#read-encoder)
|
||||||
|
- [Read Change](#read-change)
|
||||||
|
- [Read Speed](#read-speed)
|
||||||
|
- [Value Dial](#value-dial)
|
||||||
|
- [Item Selector](#item-selector)
|
||||||
|
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Read Encoder
|
||||||
|
[read_encoder.py](read_encoder.py)
|
||||||
|
|
||||||
|
An example of how to read a mechanical rotary encoder.
|
||||||
|
|
||||||
|
|
||||||
|
### Read Change
|
||||||
|
[read_change.py](read_change.py)
|
||||||
|
|
||||||
|
An example of how to read a mechanical rotary encoder, only when a change has occurred.
|
||||||
|
|
||||||
|
|
||||||
|
### Read Speed
|
||||||
|
[read_speed.py](read_speed.py)
|
||||||
|
|
||||||
|
An example of how to read the speed a mechanical rotary encoder is being turned at.
|
||||||
|
|
||||||
|
|
||||||
|
### Value Dial
|
||||||
|
[value_dial.py](value_dial.py)
|
||||||
|
|
||||||
|
A demonstration of a rotary encoder being used to control a value.
|
||||||
|
|
||||||
|
|
||||||
|
### Item Selector
|
||||||
|
[item_selector.py](item_selector.py)
|
||||||
|
|
||||||
|
A demonstration of a rotary encoder being used to select items based on its physical position.
|
|
@ -0,0 +1,39 @@
|
||||||
|
import gc
|
||||||
|
from encoder import Encoder, ROTARY_CPR
|
||||||
|
# from encoder import REVERSED
|
||||||
|
|
||||||
|
"""
|
||||||
|
A demonstration of a rotary encoder being used to
|
||||||
|
select items based on its physical position.
|
||||||
|
|
||||||
|
This requires that the encoder is positioned in the same
|
||||||
|
direction (e.g. upwards) at the start of every program run.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Free up hardware resources ahead of creating a new Encoder
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
# Create an encoder on the 3 ADC pins, using PIO 0 and State Machine 0
|
||||||
|
PIN_A = 26 # The A channel pin
|
||||||
|
PIN_B = 28 # The B channel pin
|
||||||
|
PIN_C = 27 # The common pin
|
||||||
|
enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C, counts_per_rev=ROTARY_CPR)
|
||||||
|
|
||||||
|
# Uncomment the below line (and the top import) to reverse the counting direction
|
||||||
|
# enc.direction(REVERSED)
|
||||||
|
|
||||||
|
|
||||||
|
# A list of items, up to the encoder's counts_per_rev
|
||||||
|
ITEMS = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "White"]
|
||||||
|
|
||||||
|
last_step = -1
|
||||||
|
|
||||||
|
# Loop forever
|
||||||
|
while True:
|
||||||
|
step = enc.step()
|
||||||
|
if step != last_step:
|
||||||
|
if step < len(ITEMS):
|
||||||
|
print(step, ": ", ITEMS[step], sep="")
|
||||||
|
else:
|
||||||
|
print(step, ": ", "Undefined", sep="")
|
||||||
|
last_step = step
|
|
@ -3,7 +3,7 @@ from encoder import Encoder
|
||||||
# from encoder import REVERSED
|
# from encoder import REVERSED
|
||||||
|
|
||||||
"""
|
"""
|
||||||
An example of how to read a mechanical rotary encoder, only when it has turned
|
An example of how to read a mechanical rotary encoder, only when a change has occurred.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Free up hardware resources ahead of creating a new Encoder
|
# Free up hardware resources ahead of creating a new Encoder
|
||||||
|
@ -18,6 +18,7 @@ enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
|
||||||
# Uncomment the below line (and the top import) to reverse the counting direction
|
# Uncomment the below line (and the top import) to reverse the counting direction
|
||||||
# enc.direction(REVERSED)
|
# enc.direction(REVERSED)
|
||||||
|
|
||||||
|
|
||||||
# Print out the initial count, step, and turn (they should all be zero)
|
# Print out the initial count, step, and turn (they should all be zero)
|
||||||
print("Count =", enc.count(), end=", ")
|
print("Count =", enc.count(), end=", ")
|
||||||
print("Step =", enc.step(), end=", ")
|
print("Step =", enc.step(), end=", ")
|
||||||
|
@ -29,4 +30,4 @@ while True:
|
||||||
# Print out the new count, step, and turn
|
# Print out the new count, step, and turn
|
||||||
print("Count =", enc.count(), end=", ")
|
print("Count =", enc.count(), end=", ")
|
||||||
print("Step =", enc.step(), end=", ")
|
print("Step =", enc.step(), end=", ")
|
||||||
print("Turn =", enc.turn())
|
print("Turn =", enc.turn())
|
|
@ -19,9 +19,10 @@ enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
|
||||||
# Uncomment the below line (and the top import) to reverse the counting direction
|
# Uncomment the below line (and the top import) to reverse the counting direction
|
||||||
# enc.direction(REVERSED)
|
# enc.direction(REVERSED)
|
||||||
|
|
||||||
|
|
||||||
# Loop forever
|
# Loop forever
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
# Print out the count, delta, step, and turn
|
# Print out the count, delta, step, and turn
|
||||||
print("Count =", enc.count(), end=", ")
|
print("Count =", enc.count(), end=", ")
|
||||||
print("Delta =", enc.delta(), end=", ")
|
print("Delta =", enc.delta(), end=", ")
|
|
@ -4,7 +4,7 @@ from encoder import Encoder
|
||||||
# from encoder import REVERSED
|
# from encoder import REVERSED
|
||||||
|
|
||||||
"""
|
"""
|
||||||
An example of how to read a mechanical rotary encoder, only when it has turned
|
An example of how to read the speed a mechanical rotary encoder is being turned at.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Free up hardware resources ahead of creating a new Encoder
|
# Free up hardware resources ahead of creating a new Encoder
|
||||||
|
@ -19,14 +19,14 @@ enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
|
||||||
# Uncomment the below line (and the top import) to reverse the counting direction
|
# Uncomment the below line (and the top import) to reverse the counting direction
|
||||||
# enc.direction(REVERSED)
|
# enc.direction(REVERSED)
|
||||||
|
|
||||||
|
|
||||||
# Loop forever
|
# Loop forever
|
||||||
while True:
|
while True:
|
||||||
capture = enc.take_snapshot()
|
capture = enc.capture()
|
||||||
|
|
||||||
|
print("Count =", capture.count, end=", ")
|
||||||
|
print("Angle =", capture.degrees, end=", ")
|
||||||
|
print("Freq =", capture.frequency, end=", ")
|
||||||
|
print("Speed =", capture.degrees_per_second)
|
||||||
|
|
||||||
print("Count =", capture.count(), end=", ")
|
|
||||||
print("Angle =", capture.degrees(), end=", ")
|
|
||||||
print("Freq =", capture.frequency(), end=", ")
|
|
||||||
print("Speed =", capture.degrees_per_second())
|
|
||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
import gc
|
||||||
|
from encoder import Encoder
|
||||||
|
# from encoder import REVERSED
|
||||||
|
|
||||||
|
"""
|
||||||
|
A demonstration of a rotary encoder being used to control a value.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Free up hardware resources ahead of creating a new Encoder
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
# Create an encoder on the 3 ADC pins, using PIO 0 and State Machine 0
|
||||||
|
PIN_A = 26 # The A channel pin
|
||||||
|
PIN_B = 28 # The B channel pin
|
||||||
|
PIN_C = 27 # The common pin
|
||||||
|
enc = Encoder(0, 0, (PIN_A, PIN_B), PIN_C)
|
||||||
|
|
||||||
|
# Uncomment the below line (and the top import) to reverse the counting direction
|
||||||
|
# enc.direction(REVERSED)
|
||||||
|
|
||||||
|
# The min and max value
|
||||||
|
MIN_VALUE = 0
|
||||||
|
MAX_VALUE = 11
|
||||||
|
|
||||||
|
value = 1
|
||||||
|
|
||||||
|
# Print out the initial value
|
||||||
|
print("Value =", value)
|
||||||
|
|
||||||
|
# Loop forever
|
||||||
|
while True:
|
||||||
|
delta = enc.delta()
|
||||||
|
if delta != 0:
|
||||||
|
if delta > 0:
|
||||||
|
value = min(value + 1, MAX_VALUE)
|
||||||
|
else:
|
||||||
|
value = max(value - 1, MIN_VALUE)
|
||||||
|
|
||||||
|
# Print out the new value
|
||||||
|
print("Value =", value)
|
|
@ -1,4 +1,4 @@
|
||||||
# Pico Motor Shim C++ Examples <!-- omit in toc -->
|
# Pico Motor Shim MicroPython Examples <!-- omit in toc -->
|
||||||
|
|
||||||
- [Examples](#examples)
|
- [Examples](#examples)
|
||||||
- [Single Motor](#single-motor)
|
- [Single Motor](#single-motor)
|
||||||
|
@ -8,7 +8,7 @@
|
||||||
- [Stop Motors](#stop-motors)
|
- [Stop Motors](#stop-motors)
|
||||||
|
|
||||||
|
|
||||||
## Motor Examples
|
## Examples
|
||||||
|
|
||||||
### Single Motor
|
### Single Motor
|
||||||
[single_motor.py](single_motor.py)
|
[single_motor.py](single_motor.py)
|
||||||
|
@ -35,6 +35,6 @@ A fun example of how to change a motor's frequency to have it play a song.
|
||||||
|
|
||||||
|
|
||||||
### Stop Motors
|
### Stop Motors
|
||||||
[stop_motors.py](motorshim_motor_song.py)
|
[stop_motors.py](stop_motors.py)
|
||||||
|
|
||||||
A simple program that stops the motors.
|
A simple program that stops the motors.
|
||||||
|
|
Ładowanie…
Reference in New Issue