-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMR.py
135 lines (114 loc) · 4.71 KB
/
AMR.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
'''
This class is for reading water flow from AMR
'''
import RPi.GPIO as GPIO
import time
import os
class AMR():
def __init__(self, mode = 'BCM', pin = 23, flow_unit = 'cm', time_unit = 'min',
flow_per_pulse = 1, past_water_flow = None):
self.flow_unit = flow_unit
self.time_unit = time_unit
# Water passed for each pulse in the sensor
self.flow_per_pulse = flow_per_pulse
# Water already passed
self.past_water_flow = past_water_flow
# Make a file for saving past water
# w = create a file for writing, if it's not already there
# + = both read nad write
current_folder = os.path.dirname(os.path.abspath(__file__))
Water_Passed_file = os.path.join(current_folder, 'Water_Passed.txt')
self.file = open(Water_Passed_file, 'r+')
if past_water_flow != None:
self.file.write(str(self.past_water_flow))
else:
self.past_water_flow = int(self.file.read())
self.file.close()
# pin numbering mode.
# 'BCM' means using GPIO numbering
# 'BOARD' means using pin numbering with respect to the board
# 'BCM' is preferred
self.mode = mode
# The pin to which sensors one terminal is connected
# The other terminal is connected to Gnd
self.pin = pin
# This counts the number of pulse
self.pulse_count = 0
# This one is for measureing the flow per unit time
self.prev_pulse_count = 0
# This is for keeping track of water flow per unit time
self.tic = time.time()
self.toc = 0
self.current_flow_rate = 0
# This will be used for unit conversion
# Standard unit for time is 1 second and standard unit for volume is 1 cubic meter (cm)
# All parameters are converted to the standard unit
self.units = {'min' : 60, 'second' : 1, 'hour' : 3600, 'day' : 24*3600, 'L' : 1000, 'cm' : 1}
if self.mode == 'BCM':
GPIO.setmode(GPIO.BCM)
elif self.mode == 'BOARD':
GPIO.setmode(GPIO.BOARD)
else:
print("Incorrect Mode!")
# Pin is set to Input Pullup.
# This way noise will be canceled
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Adding Interrupt callback function
#GPIO.add_event_detect(self.pin, GPIO.FALLING, callback = self.pulse_counter, bouncetime = 200)
def pulse_counter(self, channel):
self.pulse_count += 1
self.toc = time.time()
self.current_flow_rate = self.flow_per_pulse / (self.toc - self.tic) * self.units[self.time_unit]
self.tic = self.toc
print(self.pulse_count)
def get_flow_unit(self, flow_unit = 'cm'):
self.flow_unit = flow_unit
def get_time_unit(self, time_unit = 'second'):
self.time_unit = time_unit
def get_flow_per_pulse(self, flow_per_pulse):
self.flow_per_pulse = flow_per_pulse
def get_past_water_flow(self, past_water_flow):
self.past_water_flow = past_water_flow
self.file = open('Water_Passed.txt', 'w+')
self.file.write(str(self.past_water_flow))
self.file.close()
def print_current_count(self):
print(self.pulse_count)
def total_water_passed(self):
self.file = open('Water_Passed.txt', 'r')
self.past_water_flow = int(self.file.read())
self.file.close()
total_water = self.past_water_flow + self.pulse_count*self.flow_per_pulse
return total_water*self.units[self.flow_unit]
def total_water_passed_unit(self):
return self.flow_unit
def flow_rate(self):
return self.current_flow_rate
# self.toc = time.time()
# elapsed_time = (self.toc - self.tic)/self.units[self.time_unit]
# #print("Time elapsed:", elapsed_time)
# self.tic = time.time()
# water_flow = ((self.pulse_count - self.prev_pulse_count)*self.flow_per_pulse)*self.units[self.flow_unit]
# #print("Water Flow:", water_flow)
# self.prev_pulse_count = self.pulse_count
# return 2
# if elapsed_time != 0:
# return water_flow/elapsed_time
# else:
# return 0
def flow_rate_unit(self):
return self.flow_unit + "/" + self.time_unit
def convertTo(self, flow_unit = 'cm', time_unit = 'second'):
self.flow_unit = flow_unit
self.time_unit = time_unit
def reset_counter(self):
self.pulse_count = 0
self.prev_pulse_count = 0
'''
amr = AMR()
while True:
time.sleep(5)
f = amr.flow_rate()
print(f, amr.flow_rate_unit())
#amr.reset_counter()
'''