From 0c88f7892ba80d59bfd4103f5028117e4c532d26 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 12 May 2022 14:45:20 +0100 Subject: [PATCH] Added MP encoder examples --- examples/pico_motor_shim/README.md | 2 +- micropython/examples/encoder/README.md | 40 +++++++++++++++++++ micropython/examples/encoder/item_selector.py | 39 ++++++++++++++++++ ...rotary_encoder_delta.py => read_change.py} | 5 ++- ...tary_encoder_simple.py => read_encoder.py} | 3 +- ...otary_encoder_capture.py => read_speed.py} | 16 ++++---- micropython/examples/encoder/value_dial.py | 40 +++++++++++++++++++ .../examples/pico_motor_shim/README.md | 6 +-- 8 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 micropython/examples/encoder/README.md create mode 100644 micropython/examples/encoder/item_selector.py rename micropython/examples/encoder/{rotary_encoder_delta.py => read_change.py} (93%) rename micropython/examples/encoder/{rotary_encoder_simple.py => read_encoder.py} (99%) rename micropython/examples/encoder/{rotary_encoder_capture.py => read_speed.py} (62%) create mode 100644 micropython/examples/encoder/value_dial.py diff --git a/examples/pico_motor_shim/README.md b/examples/pico_motor_shim/README.md index 5fd51595..0556d1fb 100644 --- a/examples/pico_motor_shim/README.md +++ b/examples/pico_motor_shim/README.md @@ -7,7 +7,7 @@ - [Motor Song](#motor-song) -## Motor Examples +## Examples ### Single Motor [motorshim_single_motor.cpp](motorshim_single_motor.cpp) diff --git a/micropython/examples/encoder/README.md b/micropython/examples/encoder/README.md new file mode 100644 index 00000000..92c6ea76 --- /dev/null +++ b/micropython/examples/encoder/README.md @@ -0,0 +1,40 @@ +# Encoder MicroPython Examples + +- [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. diff --git a/micropython/examples/encoder/item_selector.py b/micropython/examples/encoder/item_selector.py new file mode 100644 index 00000000..78dc97e7 --- /dev/null +++ b/micropython/examples/encoder/item_selector.py @@ -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 diff --git a/micropython/examples/encoder/rotary_encoder_delta.py b/micropython/examples/encoder/read_change.py similarity index 93% rename from micropython/examples/encoder/rotary_encoder_delta.py rename to micropython/examples/encoder/read_change.py index 6e7430a5..1adda86c 100644 --- a/micropython/examples/encoder/rotary_encoder_delta.py +++ b/micropython/examples/encoder/read_change.py @@ -3,7 +3,7 @@ from encoder import Encoder # 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 @@ -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 # enc.direction(REVERSED) + # Print out the initial count, step, and turn (they should all be zero) print("Count =", enc.count(), end=", ") print("Step =", enc.step(), end=", ") @@ -29,4 +30,4 @@ while True: # Print out the new count, step, and turn print("Count =", enc.count(), end=", ") print("Step =", enc.step(), end=", ") - print("Turn =", enc.turn()) \ No newline at end of file + print("Turn =", enc.turn()) diff --git a/micropython/examples/encoder/rotary_encoder_simple.py b/micropython/examples/encoder/read_encoder.py similarity index 99% rename from micropython/examples/encoder/rotary_encoder_simple.py rename to micropython/examples/encoder/read_encoder.py index 60056bb7..3f8e7a23 100644 --- a/micropython/examples/encoder/rotary_encoder_simple.py +++ b/micropython/examples/encoder/read_encoder.py @@ -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 # enc.direction(REVERSED) + # Loop forever while True: - + # Print out the count, delta, step, and turn print("Count =", enc.count(), end=", ") print("Delta =", enc.delta(), end=", ") diff --git a/micropython/examples/encoder/rotary_encoder_capture.py b/micropython/examples/encoder/read_speed.py similarity index 62% rename from micropython/examples/encoder/rotary_encoder_capture.py rename to micropython/examples/encoder/read_speed.py index afbb3fc1..78ecbbcf 100644 --- a/micropython/examples/encoder/rotary_encoder_capture.py +++ b/micropython/examples/encoder/read_speed.py @@ -4,7 +4,7 @@ from encoder import Encoder # 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 @@ -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 # enc.direction(REVERSED) + # Loop forever 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) - diff --git a/micropython/examples/encoder/value_dial.py b/micropython/examples/encoder/value_dial.py new file mode 100644 index 00000000..1e8c773b --- /dev/null +++ b/micropython/examples/encoder/value_dial.py @@ -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) diff --git a/micropython/examples/pico_motor_shim/README.md b/micropython/examples/pico_motor_shim/README.md index 9bc04211..13b8fdc2 100644 --- a/micropython/examples/pico_motor_shim/README.md +++ b/micropython/examples/pico_motor_shim/README.md @@ -1,4 +1,4 @@ -# Pico Motor Shim C++ Examples +# Pico Motor Shim MicroPython Examples - [Examples](#examples) - [Single Motor](#single-motor) @@ -8,7 +8,7 @@ - [Stop Motors](#stop-motors) -## Motor Examples +## Examples ### Single Motor [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.py](motorshim_motor_song.py) +[stop_motors.py](stop_motors.py) A simple program that stops the motors.