-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullupsensor.py
171 lines (133 loc) · 4.57 KB
/
pullupsensor.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
import time
import sys
import wiringpi2 as wiringpi
from helper.InactivityTimer import InactivityTimer
from sensors.MotionSensor import MotionSensor
from sensors.DistanceSensor import DistanceSensor
from sensors.Display import Display
from sensors.Buzzer import Buzzer
from sensors.Led import Led
from pullup.PullupStorage import PullupStorage
import configuration as config
# Globals
INPUT = 0
OUTPUT = 1
LOW = 0
HIGH = 1
inactivity_timer = None
motion_sensor = None
distance_sensor = None
lcd = None
buzzer = None
led = None
storage = None
last_pullup_time = 0
pullup_count_today = 0
pullup_count_alltime = 0
pullups_to_go = config.PULLUPS_PER_DAY
def delay(delay_microseconds):
time.sleep(delay_microseconds/1000000.0)
def delay_milli(delay_milliseconds):
delay(delay_milliseconds * 1000)
def shutdown():
global lcd
global led
if config.DEBUG:
print("shutting down")
led.off()
lcd.off()
def wakeup():
global lcd
global led
global pullup_count_alltime
global storage
if config.DEBUG:
print("waking up")
print("pullups all time: ", storage.get_alltime_count())
print("pullups today: ", storage.get_today_count())
led.on()
lcd.on()
lcd.message(0, 0, "Welcome ATHLETE!")
lcd.message(0, 1, str(pullup_count_alltime) + " Pullups")
def count_pullup():
global pullup_count_today
global pullups_to_go
global pullup_count_alltime
global last_pullup_time
global storage
storage.count_pullup()
pullup_count_today += 1
pullup_count_alltime += 1
pullups_to_go -= 1
last_pullup_time = wiringpi.millis()
return pullup_count_today
def init_storage():
global storage
global pullup_count_today
global pullup_count_alltime
global pullups_to_go
storage = PullupStorage(config.DB_HOST, config.DB_NAME, config.DB_USER, config.DB_PASS)
pullup_count_alltime = storage.get_alltime_count()
pullup_count_today = storage.get_today_count()
pullups_to_go = pullups_to_go - pullup_count_today
def main():
global lcd
global inactivity_timer
global motion_sensor
global distance_sensor
global lcd
global buzzer
global led
global last_pullup_time
global pullup_count_alltime
global pullups_to_go
global storage
wiringpi.wiringPiSetupGpio()
init_storage()
inactivity_timer = InactivityTimer(wakeup, shutdown, config.SHUTDOWN_DELAY)
motion_sensor = MotionSensor(wiringpi, config.MOTION, inactivity_timer.trigger)
distance_sensor = DistanceSensor(wiringpi, config.SONIC_ECHO, config.SONIC_TRIG)
lcd = Display(config.LCD_RS, config.LCD_E, config.LCD_D4, config.LCD_D5, config.LCD_D6, config.LCD_D7, config.LCD_K)
buzzer = Buzzer(wiringpi, config.BUZZER)
led = Led(wiringpi, config.LED_PIN)
distance_resetted = True
shutdown()
while True:
if inactivity_timer.is_active():
distance = distance_sensor.measure()
distance_str = "{:13.2f}".format(distance)
if config.DEBUG:
print(distance_str + " cm")
# if wiringpi.millis() % 5 == 0:
# lcd.message(0, 0, distance_str + " cm")
# only count a pullup if:
# - distance is smaller than 5cm
# - distance was resetted (athlete moved more than 20 cm away from sensor)
# - last pullup was done more than 1 second ago
if 0 < distance < config.COUNT_DISTANCE and distance_resetted and wiringpi.millis() > (last_pullup_time + config.RESET_TIME):
buzzer.beep(5000)
distance_resetted = False
cnt = count_pullup()
lcd.clear()
lcd.message(0, 0, "Pullups: " + str(cnt).rjust(5))
lcd.message(0, 1, "2do2day: " + str(pullups_to_go).rjust(5))
elif distance > config.RESET_DISTANCE:
distance_resetted = True
delay_milli(100)
inactivity_timer.loop()
motion_sensor.sense()
if config.DEBUG and inactivity_timer.is_active() == 1:
print("shutting down in " + inactivity_timer.get_seconds_till_shutdown() + " seconds")
# lcd.message(0, 1, "SHUTDOWN in: " + inactivity_timer.get_seconds_till_shutdown().ljust(3))
if __name__ == "__main__":
try:
if len(sys.argv) == 2 and sys.argv[1] == "setup_storage":
print("setting up table in database")
init_storage()
storage.setup()
print("done.")
else:
main()
except (KeyboardInterrupt, SystemExit):
shutdown()