forked from DeepNav/ControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaterSpeed.py
54 lines (40 loc) · 1.71 KB
/
WaterSpeed.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
import logging
import math
from Phidget22.Devices.FrequencyCounter import *
from DeviceManager import Device
DIAMETER = 0.0127
SECTIONAL_AREA = math.pi * math.pow(DIAMETER/2, 2)
'''
How it works
We use water flow sensor
(https://www.robotshop.com/en/seeedstudio-water-flow-sensor.html) to hook to the
Versatile Input Phidget's FrequencyCounter to get speed of flow.
We have 4 of such sensors to measure the water speed in 4 directions: forward,
backward, left and right.
In order to just measure the direction we desire, we mount a L shape pipe to the
exit end of the seasor, so that when going other directions, no significant
flow is forced to pass the sensor.
The Math
As documented, f = Q*7.5, Q is the flowspeed in L/min, thus Q = f/7.5 which means
the volume of water pass the meter in one min. devide it by area of the sectional
area to get the speed of water.
'''
class WaterSpeedDevice(Device):
def __init__(self, water_direction, hub_serial_num, port_num):
ch = FrequencyCounter()
ch.setDeviceSerialNumber(hub_serial_num)
ch.setHubPort(port_num)
self.event_key_name = "water_speed_" + water_direction
self.ch = ch
super(WaterSpeedDevice, self).__init__(ch)
def get_water_speed(self, frequency):
# f = Q*7.5, Q is the flowspeed in L/min
flowspeed = (frequency/7.5) / 60000 # L/min to m3/s
return flowspeed / SECTIONAL_AREA
def on_attach(self):
device = self
self.ch.setDataInterval(500)
def on_frequency_change(ch, frequency):
water_speed = device.get_water_speed(frequency)
device.set_event_val(device.event_key_name, water_speed)
self.listen("FrequencyChange", on_frequency_change)