Added first order filter to distance measurement to reduce noise.
This commit is contained in:
22
main.py
22
main.py
@@ -1,14 +1,11 @@
|
||||
# Built-in's
|
||||
import network
|
||||
import rp2
|
||||
import time
|
||||
import json
|
||||
import machine
|
||||
|
||||
# From PyPI
|
||||
from machine import Pin
|
||||
from umqtt.robust import MQTTClient
|
||||
|
||||
# From this project...
|
||||
import wifi_config
|
||||
import mqtt_config
|
||||
from pico_funcs import read_cpu_temp, wlan_up, led_error_code
|
||||
from hcsr04_funcs import read_hc_sr04
|
||||
@@ -43,13 +40,17 @@ def main():
|
||||
)
|
||||
|
||||
# Create HC-SR04
|
||||
trig = machine.Pin(TRIG_PIN, machine.Pin.OUT)
|
||||
echo = machine.Pin(ECHO_PIN, machine.Pin.IN, machine.Pin.PULL_DOWN)
|
||||
trig = Pin(TRIG_PIN, Pin.OUT)
|
||||
echo = Pin(ECHO_PIN, Pin.IN, Pin.PULL_DOWN)
|
||||
|
||||
# Let's Go!
|
||||
if DEBUG:
|
||||
print("Enter main loop")
|
||||
|
||||
# First Time flag allows the 1st order filter to be initialized.
|
||||
first_time = True
|
||||
FILTER_CONSTANT = 0.9
|
||||
|
||||
while True:
|
||||
|
||||
# Loop Clean-Up and Prep
|
||||
@@ -71,7 +72,12 @@ def main():
|
||||
|
||||
# HC-SR04 Reading.
|
||||
try:
|
||||
distance = read_hc_sr04(trig, echo)
|
||||
sensor_value = read_hc_sr04(trig, echo)
|
||||
if not first_time:
|
||||
distance = distance * FILTER_CONSTANT + (1 - FILTER_CONSTANT) * sensor_value
|
||||
else:
|
||||
distance = sensor_value
|
||||
first_time = False
|
||||
hc_sr04_ready = True
|
||||
except:
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user