Last active
April 10, 2018 14:02
-
-
Save vadella/2aa96874af45a48982ee2fae20827ee4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
# import RPi.GPIO as GPIO #library for raspberry pins | |
import time | |
from collections import OrderedDict | |
from appJar import gui | |
HYDROREELS = OrderedDict() | |
HYDROREELS['125'] = ( | |
(1.98, 1.93, 31.17), | |
(10.28, 0.19, 32.05), | |
(float('inf'), -14.09, 33.44), | |
) | |
HYDROREELS['80'] = ( | |
(1, 2.8, 31.34), | |
(float('inf'), .82, 33.32), | |
) | |
HYDROREELS['50'] = ( | |
(1.74, 1.23, 24.73), | |
(float('inf'), -0.42, 25.68), | |
) | |
HYDROREELS['30'] = ( | |
(1.16, 1.90, 10.57), | |
(float('inf'), 2.26, 10.26), | |
) | |
def speed(hydroreel, spin): | |
"""this is the function that calculates the speed from the spins of the hydroreel""" | |
if spin in {0, ''}: | |
return spin | |
spin = float(spin) | |
spin_1s = spin / 30 | |
coefficients = HYDROREELS[hydroreel] | |
for limit, c0, c1 in coefficients: | |
if spin < limit: | |
return (c0 + (c1 * spin_1s)) / 100 | |
def spin_counter(t): | |
"""this function measures the spins through raspberry""" | |
spin = input( | |
'Inserire numero di spin') # here i used this input for the spins because i don't use the raspberry every time | |
final_t = time.time() + float(t) | |
return float(spin) | |
"""GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(32,GPIO.IN) | |
#set up a counter | |
spin = 0 | |
#set up a variable for reed activation | |
reed_state = 0 | |
print('Misurazione in corso...') | |
t_fine = time.time()+temp_mis | |
#while loop until 30s | |
while time.time()<t_fine: | |
#check if reed newly activated | |
if GPIO.input(32) == 1 and reed_state == 0: | |
#turn on LED. Set reed_state to 1. Add to counter . | |
reed_state = 1 | |
spin = spin + 1 | |
#pause to debounce | |
time.sleep(.01) | |
#check if reed released | |
if GPIO.input(32) == 0 and reed_state == 1: | |
# set reed_state to 0 | |
reed_state = 0 | |
#now that loop has finished, print the final count | |
return spin """ | |
LABEL_NAMES = { | |
'reel_size': 'Reel Size', | |
'measurement_interval': 'Seconds of measurement', | |
'measure_start': 'Start Measure', | |
'measure_save': 'Inserisci misura', | |
'measurement_change': 'Cambia misura', | |
'depth_change': 'Altra profondità', | |
'station_number': 'Station number', | |
'vertical_no': 'Vertical n°', | |
'depth_total': 'Total depth', | |
'hydroreel_distance': 'Hydro reel distance', | |
'station_change': 'Cambia stazione', | |
} | |
ENTRIES = ['station_number', 'vertical_no', 'depth_total', 'hydroreel_distance'] | |
class MyGUI: | |
def __init__(self): | |
self.app = gui() | |
self._populate() | |
self.current_file = '' | |
self.mis_0 = 0 | |
self.l = [] | |
def put_into(self, button): | |
focus = self.app.getFocus() | |
entry = self.app.getEntry(focus) | |
entry = entry + button | |
self.app.setEntry(focus, entry) | |
def press(self, button): | |
self.put_into(button) | |
def file_name(self): # | |
"""Station where you do the measures have a code, and we call the filename with that code""" | |
return self.app.getEntry(ENTRIES[0]) + '.csv' | |
def vertical(self): | |
"""The point where you measure the speed of the river""" | |
return float(self.app.getEntry(ENTRIES[1])) | |
def depth_tot(self): | |
"""The depth of the river where you are doing the measure""" | |
return float(self.app.getEntry(ENTRIES[2])) | |
def edge_dist(self): | |
"""Distance from the edge of the river""" | |
return float(self.app.getEntry(ENTRIES[1])) | |
def hydro_depth(self): | |
"""Depth where you have the hydrometric reel""" | |
return float(self.app.getEntry(ENTRIES[3])) | |
def hydro_reel(self): | |
"""Gets the size of the Hydrometric reel""" | |
return self.app.getOptionBox(LABEL_NAMES['reel_size']) | |
def measurement_time(self): | |
"""The measure of speed needs to be over a period of time, so you have less error""" | |
return self.app.getOptionBox(LABEL_NAMES['measurement_interval']) | |
def save_measure(self): | |
"""this function inserts all the data into a csv file""" | |
with open(self.file_name(), 'a') as my_file: | |
writer = csv.writer(my_file, lineterminator='\n') | |
if self.current_file != self.file_name(): | |
first_line = ['Vertical', 'Edge distance', 'Depth'] + ['HydroDepth', 'Speed'] * 5 | |
writer.writerow(first_line) | |
self.current_file = self.file_name() | |
self.mis_0 = float(self.edge_dist()) | |
depth = self.hydro_depth() | |
v = self.app.getLabel('current_speed') | |
self.l.append(depth) | |
self.l.append(v) | |
writer.writerow([self.vertical(), self.vertical() - self.mis_0, self.depth_tot()] + self.l) | |
self.l = [] | |
def change_depth(self): | |
"""This clears only the last entry, as if the river is a lot deep you need to do more measures on the same vertical""" | |
prof = float(self.app.getEntry(ENTRIES[3])) | |
v = self.app.getLabel('current_speed') | |
self.l.append(prof) | |
self.l.append(v) | |
self.app.clearEntry(ENTRIES[3]) | |
self.app.showButton(LABEL_NAMES['measure_start']) | |
self.app.setFocus(ENTRIES[3]) | |
def change_station(self): # | |
"""Clears all entries, when you change station""" | |
entries = ENTRIES | |
for entry in entries: | |
self.app.clearEntry(entry) | |
self.app.clearLabel('current_speed') | |
self.app.showButton(LABEL_NAMES['measure_start']) | |
self.app.setFocus(entries[0]) | |
def clear_entries(self): | |
"""Function that clears the entries for the measures, as you do many measures on the same station""" | |
entries = ENTRIES[1:] | |
for entry in entries: | |
self.app.clearEntry(entry) | |
self.app.clearLabel('current_speed') | |
self.app.showButton(LABEL_NAMES['measure_start']) | |
self.app.setFocus(entries[0]) | |
def start_measure(self): # | |
"""this is the function triggered to start the measure of speed""" | |
v = speed(self.hydro_reel(), spin_counter(self.measurement_time())) | |
v = round(v, 4) | |
self.l = [self.hydro_depth(), v] | |
self.app.setLabel('current_speed', v) | |
self.app.hideButton(LABEL_NAMES['measure_start']) | |
def _populate(self): | |
self.app.setTitle('Water app') | |
self.app.setFont(size=12, family='Calibri') | |
self.app.addLabel('l1', 'Measures', 0, 0) | |
self.app.addLabel('l2', 'Speed', 5, 0) | |
self.app.addLabelOptionBox(LABEL_NAMES['reel_size'], HYDROREELS.keys(), 5, 1) | |
self.app.addLabelOptionBox(LABEL_NAMES['measurement_interval'], ['15', '30', '60', '120', '180'], 6, 1, 2) | |
self.app.setLabelFont(size=13, weight='bold') | |
self.app.setGuiPadding(15, 5) | |
for i, entry in enumerate(ENTRIES, 1): | |
self.app.addEntry(entry, i, 0) | |
self.app.setEntryRelief(entry, 'raised') | |
self.app.setEntryDefault(entry, LABEL_NAMES[entry]) | |
self.app.addLabel('current_speed', "", 6, 0) | |
self.app.setEntryWidths(ENTRIES, 20) | |
self.app.addButton(LABEL_NAMES['measure_start'], self.start_measure, 6, 0) | |
self.app.addButton(LABEL_NAMES['measure_save'], self.save_measure, 4, 1) | |
self.app.addButton(LABEL_NAMES['depth_change'], self.change_depth, 3, 1) | |
self.app.addButton(LABEL_NAMES['measurement_change'], self.clear_entries, 2, 1) | |
self.app.addButton(LABEL_NAMES['station_change'], self.change_station, 1, 1) | |
self.app.addButtons([['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['0', '.', 'O']], self.press, 1, 2, 3, | |
4) | |
self.app.addButtons( | |
[['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H'], ['I', 'L', 'M', 'N'], ['P', 'Q', 'R', 'S'], | |
['T', 'U', 'V', 'Z']], | |
self.press, 1, 5, 4, 5) | |
def go(self): | |
self.app.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment