-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_distance_sensor.py
More file actions
34 lines (28 loc) · 1022 Bytes
/
test_distance_sensor.py
File metadata and controls
34 lines (28 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from time import sleep
from gpiozero import DistanceSensor
from statistics import median
import colorama
from colorama import Fore
from robot.settings import sets
colorama.init(autoreset=True)
distance_sensor = DistanceSensor(echo = sets['distance_echo'], trigger = sets['distance_trigger'])
def nice_distance(distanse: float) -> float:
SLEEP_TIME = 0.1
mesure_list_5 = []
while True:
# что-то перемудрил
for _ in range(5):
dist = distance_sensor.distance
# print(f"{dist}")
if isinstance(dist, float):
mesure_list_5.append(dist)
# print(dist)
while len(mesure_list_5) > 5:
mesure_list_5.pop(0)
sleep(SLEEP_TIME/6)
yield median(mesure_list_5)
sleep(SLEEP_TIME/6)
# while True:
# print(f"distance = {distance_sensor.distance}")
for nice_dist in nice_distance(distance_sensor.distance):
print(Fore.RED + f"distance = {nice_dist:.2f}")