-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensor.py
45 lines (40 loc) · 1.56 KB
/
sensor.py
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
35
36
37
38
39
40
41
42
43
44
45
import RPi.GPIO as GPIO
import json, threading, time
from datapusher import Rider
hallPin = 3 # Hall effect sensor pin #
GPIO.setmode(GPIO.BOARD)
GPIO.setup(hallPin, GPIO.IN)
count = 0
with open("piconfig.json", "r") as file:
configjson = file.read()
configDict = json.loads(configjson)
uid = configDict["uid"]
wheel_radius = configDict["wheel_radius"]
distance = configDict["distance"]
# Object Rider defined in datapusher.py #
rider = Rider(uid=uid, wheel_radius=wheel_radius, distance=distance)
try:
print("Detecting bike cycles...")
last = 1
while True:
# If there is input set last to 1. If there is no input & last == 1, count ++ and set last to 0 #
# The code is designed as such to avoid multiple counting when the magnet stays in front of the sensors for a long time #
# As long as the wheel does not turn faster than this code, the count would be accurate #
if GPIO.input(hallPin):
last = 1
else:
if last == 1:
count += 1
print(count)
# Prevent pushing too frequently, setting a delay #
if time.time() - rider.last_push > rider.push_delay:
rider.last_push = time.time()
push = True
else:
push = False
# Thread so we can keep tracking distance without waiting #
# Updating rider and mileage information #
threading.Thread(target=rider.Changer, args=(push,)).start()
last = 0
finally:
GPIO.cleanup()