-
Notifications
You must be signed in to change notification settings - Fork 0
/
contadorpassos.py
147 lines (116 loc) · 4.01 KB
/
contadorpassos.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
import RPi.GPIO as GPIO
import time
from espeak import espeak
from threading import Timer,Thread,Event
import threading
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(20 , GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Recebe as oscilacoes
GPIO.setup(19 , GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Reseta
GPIO.setup(16 , GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Total voltas
espeak.set_voice('brazil')
espeak.set_parameter(espeak.Parameter.Rate, 175, 0)
GPIO.setup(26 , GPIO.OUT)
espeak.synth("Vamos começar")
cont = 0
cont_total = 0
# Encontrando a frequência de rotação
def frequencia():
global cont, cont_total
GPIO.wait_for_edge(20, GPIO.RISING)
t_inicio = time.time()
GPIO.wait_for_edge(20, GPIO.RISING)
t_fim = time.time()
tempo = t_fim - t_inicio # Tempo de uma rotacao
freq = 1/tempo
cont = cont + 2
cont_total = cont_total + 2
return freq
# Encontrando a velocidade
class x_rpm(Thread):
def __init__(self, event):
Thread.__init__(self)
self.stopped = event
def run(self):
global cont_total
while not self.stopped.wait(10):
x1 = cont_total
self.stopped.wait(5)
x2 = cont_total
rpm = (x2 - x1)*60/5
x1 = x2
espeak.set_parameter(espeak.Parameter.Rate, 150, 0)
espeak.synth("Sua RPM atual é de:" + str(int(rpm)))
espeak.set_parameter(espeak.Parameter.Rate, 375, 0)
# Resetando a contagem
def reset_save():
global cont
while(1):
if(GPIO.input(19) == 1): # Verificação de borda
cont = 0
time.sleep(0.5)
# Retornando a contagem total de passos
def get_rot_total():
global cont_total
while(1):
if(GPIO.input(16) == 1): # Verificação de borda
espeak.set_parameter(espeak.Parameter.Rate, 150, 0)
espeak.synth("Foram dadas" + str(cont_total) + "voltas.")
espeak.set_parameter(espeak.Parameter.Rate, 150, 0)
time.sleep(0.5)
# Se GPIO(19) acionado -> Reseta e Salva o intervalo
t1 = threading.Thread(target=reset_save)
t1.start()
# Retorna a RPM relativa a cada 15seg
stopFlag1 = Event()
get_rpm = x_rpm(stopFlag1)
get_rpm.start()
# Se GPIO(16) acionado -> Retorna a distancia total
t2 = threading.Thread(target=get_rot_total)
t2.start()
freq = frequencia();
# Definindo conjuntos de frequências e retornando em áudio
while(freq < 1500):
if(freq >= 500):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 500 == 0):
# Retornando a contagem em áudio
espeak.synth(str(cont))
elif(freq >= 200):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 200 == 0):
espeak.synth(str(cont))
elif(freq >= 75):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 75 == 0):
espeak.synth(str(cont))
elif(freq >= 25):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 25 == 0):
espeak.synth(str(cont))
elif(freq >= 10):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 10 == 0):
espeak.synth(str(cont))
elif(freq >= 3.5):
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
if(cont % 5 == 0):
espeak.synth(str(cont))
else:
espeak.synth(str(cont))
GPIO.wait_for_edge(20, GPIO.RISING)
cont = cont + 1
cont_total = cont_total + 1
exit(0)