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 state machine to handle blocked chute #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions Code/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
from machine import Pin, Timer, ADC
import utime
from machine import Pin, ADC

sensor = ADC(0)
out = Pin(1, Pin.OUT)
led = Pin(14, Pin.OUT)

#tim = Timer()
READY = 0
PULSE = 1
BROKEN = 2

#def tick(timer):
# global led
# led.toggle()
THRESHOLD = 30000
PULSE_INTERVAL = 3
SETTLE_INTERVAL = 300
BEAM_BURST_THRESHOLD = 3

#tim.init(freq=1000, mode=Timer.PERIODIC, callback=tick)
count = 0
conversion_factor = 3.3 / (65535)
state = READY
pulse_counter = 0
settle_counter = 0
beam_burst_counter = 0

while True:
led.value(1)
reading = sensor.read_u16()# * conversion_factor
reading = sensor.read_u16()
led.value(0)
if reading < 30000:
out.value(1)
count=count+1
beam_active = reading < THRESHOLD
if state == READY:
if beam_active:
state = PULSE
pulse_counter = PULSE_INTERVAL
settle_counter = SETTLE_INTERVAL
beam_burst_counter = beam_burst_counter + 1
out.value(1)
else:
settle_counter = settle_counter - 1
if settle_counter <= 0:
beam_burst_counter = 0
settle_counter = 0
elif state == PULSE:
pulse_counter = pulse_counter - 1
if pulse_counter <= 0:
state = READY if beam_burst_counter < BEAM_BURST_THRESHOLD else BROKEN
out.value(state == BROKEN)
else:
out.value(0)
# print(reading)
print(count)
settle_counter = SETTLE_INTERVAL if beam_active else settle_counter - 1
if settle_counter <= 0:
state = READY
beam_burst_counter = 0
out.value(0)
utime.sleep(0.003)
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# aeon_feeder_fix

The IR beam break processing code runs on top of [MicroPython](https://github.com/micropython/micropython), an implementation of a Python interpreter for microcontrollers.
This IR beam break processing code is a temporary workaround to filter the adaptive threshold signal in the case where the chute is blocked by accidental delivery of multiple pellets. The following state machine describes the filtering operation:

```mermaid
stateDiagram-v2
[*] --> Ready
Ready --> Pulse: LOW
Pulse --> Ready: WAIT 3 samples
Pulse --> Broken: 3 consecutive\n pulses in less\n than 1 second
Broken --> Ready: 1 second HIGH
Broken --> Broken: LOW
```

The code runs on top of [MicroPython](https://github.com/micropython/micropython), an implementation of a Python interpreter for microcontrollers.

## How to install

### Prerequisites

* [Visual Studio Code](https://code.visualstudio.com/)
* [Raspberry Pi Pico SDK](https://www.raspberrypi.com/news/raspberry-pi-pico-windows-installer/)
* [Thonny IDE](https://thonny.org/)

### Firmware

Before running the actual Python code, we need to install the MicroPython firmware in the Pico. To do this, boot the Pico into firmware uploading mode by holding BOOTSEL while plugging the USB cable into the computer, then release. A storage drive should now be mounted on the computer, e.g. RPI-RP2.
Before running the actual Python code, we need to install the MicroPython firmware in the Pico. To do this, boot the Pico into firmware update mode by holding the BOOTSEL button while plugging the USB cable into the computer, then release. A storage drive should now be mounted on the computer, e.g. RPI-RP2.

To install the firmware, drag the file `RPI_PICO-20231005-v1.21.0.uf2` into the storage drive. The drive should be automatically unmounted and the Pico will then boot straight into MicroPython.
To install the firmware, drag the MicroPython release file for the Pi Pico, e.g. [`RPI_PICO-20231005-v1.21.0.uf2`](https://micropython.org/resources/firmware/RPI_PICO-20231005-v1.21.0.uf2) into the storage drive. The drive should be automatically unmounted and the Pico will then boot straight into MicroPython.

### MicroPython code

The IR beam break processing code is implemented in the `main.py` file. This should be transferred to the root of the pico file system so it can be executed on reset. A simple way to test and manipulate MicroPython code is to use the [Thonny IDE](https://thonny.org/).
The IR beam break processing code is implemented in the `main.py` file. This should be transferred to the root of the pico file system so it can be executed on reset. The [Thonny IDE](https://thonny.org/) provides a simple way to inspect, test and manipulate MicroPython code stored in the Pico.