From 67b8a11089be062e2136f748cf02d98c3337ad4a Mon Sep 17 00:00:00 2001
From: till <till.raab@student.uni-tuebingen.de>
Date: Mon, 18 Nov 2019 14:06:23 +0100
Subject: [PATCH] latest changes made in colombia and in germany

---
 fishgrid.cfg     |  58 ++++------------------
 rasp_grid.py     | 122 +++++++++++++++++++++++------------------------
 rasp_grid_cfg.py | 103 ++++++++++++++++-----------------------
 3 files changed, 111 insertions(+), 172 deletions(-)

diff --git a/fishgrid.cfg b/fishgrid.cfg
index 06838c9..624374b 100644
--- a/fishgrid.cfg
+++ b/fishgrid.cfg
@@ -1,20 +1,21 @@
 *FishGrid
   Grid &1:
       Used1            : true
-      Columns1         : 8
-      Rows1            : 2
+      Gridno1          : 1
+      RecordMode1      : standalone
+      Columns1         : 2
+      Rows1            : 4
       ColumnDistance1  : 50.0cm
       RowDistance1     : 50.0cm
-      ChannelOffset1   : 0
-      ElectrodeType1   : plain
-      RefElectrodeType1: plain
+      ElectrodeType1   : headstage
+      RefElectrodeType1: screw
       RefElectrodePosX1: 0.00m
       RefElectrodePosY1: 0.00m
       WaterDepth1      : 0.25m
   Hardware Settings:
       DAQ board:
           AISampleRate: 20.000kHz
-          AIMaxVolt   : 1.0mV
+          AIMaxVolt   : 20.0mV
       Amplifier:
           AmplName      : 16-channel-amplifier
           AmplModel     : EM-64B
@@ -24,10 +25,10 @@
           LowpassCutoff : 10.000kHz
   Recording:
       General:
-          Experiment.Name  : recording fish behavior
+          Experiment.Name  : tube competition
           StartDate        : ~
           StartTime        : ~
-          Location         : Colombia - Los Llanos
+          Location         : Colombia - leticia
           Position         : ~N, ~W
           WaterTemperature : 20.0C
           WaterConductivity: 5.0uS/cm
@@ -35,46 +36,5 @@
           WaterOxygen      : 0.0mg/l
           Comment          : ~
           Experimenter     : T. Raab et al. :)
-      Buffers and timing:
-          DataTime    : 100ms
-          DataInterval: 1000ms
-          BufferTime  : 60s
-  Pre-processing:
-      PreProcessor1: none
-      PreProcessor2: none
-      PreProcessor3: none
-      PreProcessor4: none
-      PreProcessor5: none
-      PreProcessor6: none
-      PreProcessor7: none
-      PreProcessor8: none
-
-*CommonNoiseRemoval
-  CommonNoiseRemoval: none
-
-*Spectra
-  Size   : "8192"
-  Overlap: true
-  Window : Hanning
-  Decibel: true
-  Decay  : 100iterations
-  FMin   : 0Hz
-  FMax   : 2000Hz
-  Clip   : true
-
-*RMSPixel
-  TraceLength: 10s
-
 *Recording
   PathFormat: "%04Y-%02m-%02d-%02H_%02M"
-
-*Acquisition
-  device1   : /dev/comedi0
-  blacklist1: ~
-  device2   : /dev/comedi1
-  blacklist2: ~
-  device3   : /dev/comedi2
-  blacklist3: ~
-  device4   : /dev/comedi3
-  blacklist4: ~
-  reference : RSE
diff --git a/rasp_grid.py b/rasp_grid.py
index 22fc23b..2395c9e 100644
--- a/rasp_grid.py
+++ b/rasp_grid.py
@@ -18,7 +18,7 @@ from IPython import embed
 from uldaq import (get_daq_device_inventory, DaqDevice, AInScanFlag, ScanStatus,
                    ScanOption, create_float_buffer, InterfaceType, AiInputMode)
 
-def GPIO_setup(LED1_pin, LED2_pin, Button1_pin, Button2_pin, power_controll_pin):
+def GPIO_setup(LED1_pin, LED2_pin, LED_out_pin, Button1_pin, Button2_pin, power_controll_pin):
     # LED output pins
     GPIO.setmode(GPIO.BOARD)
 
@@ -27,7 +27,13 @@ def GPIO_setup(LED1_pin, LED2_pin, Button1_pin, Button2_pin, power_controll_pin)
     GPIO.setup(LED2_pin, GPIO.OUT)  # 2
     GPIO.output(LED2_pin, GPIO.HIGH)
 
-    LED_status = [False, True]
+    if LED_out_pin != None:
+        GPIO.setup(LED_out_pin, GPIO.OUT)
+        GPIO.output(LED_out_pin, GPIO.LOW)
+
+        LED_status = [False, True, False]
+    else:
+        LED_status = [False, True]
 
     # switch controlled input
     # GPIO.setup(Button1_pin, GPIO.IN)
@@ -191,11 +197,12 @@ def main():
 
     LED1_pin = 11
     LED2_pin = 13
+    LED_out_pin = 35
     Button1_pin = 16
     Button2_pin = 18
     power_controll_pin = 37
 
-    LED_status = GPIO_setup(LED1_pin, LED2_pin, Button1_pin, Button2_pin, power_controll_pin)
+    LED_status = GPIO_setup(LED1_pin, LED2_pin, LED_out_pin, Button1_pin, Button2_pin, power_controll_pin)
 
     last_button_1_t = time()
     last_button_2_t = time()
@@ -213,7 +220,7 @@ def main():
         low_channel = 0
         high_channel = channels
 
-        samples_per_channel = rate * 2  # * channels = Buffer size
+        samples_per_channel = rate * 20  # * channels = Buffer size
         # rate = 20000
         scan_options = ScanOption.CONTINUOUS
         flags = AInScanFlag.DEFAULT
@@ -273,13 +280,6 @@ def main():
         print(ranges[range_index])
 
         # Allocate a buffer to receive the data.
-        data = create_float_buffer(channel_count, samples_per_channel)
-        # system('clear')
-
-        # Start the acquisition.
-        rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel,
-                                   rate, scan_options, flags, data)
-        last_idx = 0
         # f = open('/media/pi/data1/test_file.raw', 'wb')
 
 
@@ -290,22 +290,32 @@ def main():
         sleep(.1)
     GPIO.output(LED1_pin, GPIO.LOW)
     LED_status[0] = False
+    sleep(2.)
+    data = create_float_buffer(channel_count, samples_per_channel)
+    # system('clear')
 
-    sleep(2)
+    # Start the acquisition.
+    rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel,
+                                   rate, scan_options, flags, data)
+    last_idx = 0 
+    
+    
 
     emergency_LED_t = time()
     emergency_LED_interval = 0.5
 
-
     LED_t = time()
     LED_t_interval = 2
 
+    out_LED_t = time()
+    out_LED_interval = 1
+    
     temp_t0 = time()
     next_temp_t = 0
     temp_interval = 300  # sec --> 5 min
 
     disp_eth_power = True
-
+    #while True:
     while GPIO.input(Button1_pin) == GPIO.LOW:
         if record_temp == True:
             if time() - temp_t0 > next_temp_t:
@@ -321,44 +331,35 @@ def main():
                 w1_f.close()
                 next_temp_t += temp_interval
 
-        # blinking LED
-        if GPIO.input(power_controll_pin) == GPIO.HIGH:
-
-            if time() - LED_t < .1 and LED_status[0] == False:
-                LED_status[0] = True
-                GPIO.output(LED1_pin, GPIO.HIGH)
-            if time() - LED_t >= .1 and LED_status[0] == True:
-                LED_status[0] = False
-                GPIO.output(LED1_pin, GPIO.LOW)
-            if time() - LED_t >= LED_t_interval:
-                LED_t = time()
-        elif GPIO.input(power_controll_pin) == GPIO.LOW:
-            print('battery low')
-
-            if time() - emergency_LED_t > emergency_LED_interval:
-                if LED_status[0] == LED_status[1]:
-                    GPIO.output(LED1_pin, GPIO.HIGH)
-                    GPIO.output(LED2_pin, GPIO.LOW)
-                    LED_status = [True, False]
-                    emergency_LED_t = time()
-                else:
-                    if LED_status[0] == True:
-                        GPIO.output(LED1_pin, GPIO.LOW)
-                        GPIO.output(LED2_pin, GPIO.HIGH)
-                        LED_status = [False, True]
-                        emergency_LED_t = time()
-                    else:
-                        GPIO.output(LED1_pin, GPIO.HIGH)
-                        GPIO.output(LED2_pin, GPIO.LOW)
-                        LED_status = [True, False]
-                        emergency_LED_t = time()
-
-
+        # blinking LED for rec
+        if time() - LED_t < .1 and LED_status[0] == False:
+            LED_status[0] = True
+            GPIO.output(LED1_pin, GPIO.HIGH)
+        if time() - LED_t >= .1 and LED_status[0] == True:
+            LED_status[0] = False
+            GPIO.output(LED1_pin, GPIO.LOW)
+        if time() - LED_t >= LED_t_interval:
+            LED_t = time() - (time() - LED_t - LED_t_interval)
+
+        if len(LED_status) == 3:
+            if time() - out_LED_t < .1 and LED_status[2] == False:
+                LED_status[2] = True
+                GPIO.output(LED_out_pin, GPIO.HIGH)
+            elif time() - out_LED_t >= .1 and LED_status[2] == True:
+                LED_status[2] = False
+                GPIO.output(LED_out_pin, GPIO.LOW)
+            else:
+                pass
+
+            if time() - out_LED_t >= out_LED_interval:
+                out_LED_t = time() - (time() - out_LED_t - out_LED_interval )
+            
+            
         # dist & eth0 controll
         if GPIO.input(Button2_pin) == GPIO.HIGH and (time() - last_button_2_t) > 5:
             if disp_eth_power == True:
                 subprocess.run(['tvservice', '-o'])
-                subprocess.run(['vcgencmd', 'display_power', '0'])
+                subprocess.run(['vcgencmd', 'display_power', '0'])#
 
                 subprocess.run(['sudo', 'ip', 'link', 'set', 'eth0', 'down'])
                 GPIO.output(LED2_pin, GPIO.LOW)
@@ -369,7 +370,7 @@ def main():
                 subprocess.run(['tvservice', '-p'])
                 subprocess.run(['vcgencmd', 'display_power', '1'])
                 subprocess.run(['sudo', '/bin/chvt', '6'])
-                subprocess.run(['sudo', '/bin/chvt', '7'])
+                subprocess.run(['sudo', '/bin/chvt', '7'])#
 
                 subprocess.run(['sudo', 'ip', 'link', 'set', 'eth0', 'up'])
                 GPIO.output(LED2_pin, GPIO.HIGH)
@@ -380,23 +381,20 @@ def main():
         status, transfer_status = ai_device.get_scan_status()
 
         index = transfer_status.current_index
-
-        if (last_idx > index) and (index != -1):
+        #print(last_idx - index)
+        #embed()
+        #quit()
+        if index < 0 or index == last_idx:
+            continue
+
+        if index > last_idx:
+            (np.array(data[last_idx:index], dtype=np.float32) / gain).tofile(f)
+        else:
             (np.array(data[last_idx:], dtype=np.float32) / gain).tofile(f)
             (np.array(data[:index], dtype=np.float32) / gain).tofile(f)
             f.flush()
 
-        if index == -1:
-            pass
-        else:
-            (np.array(data[last_idx:index], dtype=np.float32) / 1000).tofile(f)
-
-        if index == -1:
-            last_idx= len(data)
-        else:
-            last_idx = index
-
-
+        last_idx = index
 
     f.close()
     temp_f.close()
diff --git a/rasp_grid_cfg.py b/rasp_grid_cfg.py
index ddb9050..8806726 100644
--- a/rasp_grid_cfg.py
+++ b/rasp_grid_cfg.py
@@ -10,14 +10,15 @@ from PyQt5.QtCore import *
 class Grid_tab(QWidget):
     def __init__(self):
         super().__init__()
-        self.gird_no_val = 1
+        self.grid_no_val = 1
+        self.rec_mode_val = 'standalone'
         self.columns_val = 8
         self.rows_val = 8
         self.col_dist_val = 50
         self.row_dist_val = 50
         self.channel_offset_val = 0
-        self.elec_type_val = 'plain'
-        self.ref_elec_type_val = 'plain'
+        self.elec_type_val = 'headstage'
+        self.ref_elec_type_val = 'screw'
         self.ref_elec_posx_val = 0.00
         self.ref_elec_posy_val = 0.00
         self.water_depth_val = 0.25
@@ -33,78 +34,76 @@ class Grid_tab(QWidget):
 
     def get_widgets(self):
         grid_noL = QLabel('Grid no:', self)
-        self.grid_no = QLineEdit(str(self.gird_no_val), self)
+        self.grid_no = QLineEdit(str(self.grid_no_val), self)
         self.gridlayout.addWidget(grid_noL, 0, 0)
         self.gridlayout.addWidget(self.grid_no, 0, 1)
 
+        rec_modeL = QLabel('Recorde mode', self)
+        self.rec_mode = QLineEdit(self.rec_mode_val, self)
+        self.gridlayout.addWidget(rec_modeL, 1, 0)
+        self.gridlayout.addWidget(self.rec_mode, 1, 1)
+
         col_elecsU =QLabel('n', self)
         col_elecsL =QLabel('Electrod Columns:', self)
         self.col_elecs = QLineEdit(str(self.columns_val), self)
-        self.gridlayout.addWidget(col_elecsL, 1, 0)
-        self.gridlayout.addWidget(self.col_elecs, 1, 1)
-        self.gridlayout.addWidget(col_elecsU, 1, 2)
+        self.gridlayout.addWidget(col_elecsL, 2, 0)
+        self.gridlayout.addWidget(self.col_elecs, 2, 1)
+        self.gridlayout.addWidget(col_elecsU, 2, 2)
 
         row_elecsU = QLabel('n', self)
         row_elecsL = QLabel('Electrod Rows:', self)
         self.row_elecs = QLineEdit(str(self.rows_val), self)
-        self.gridlayout.addWidget(row_elecsL, 2, 0)
-        self.gridlayout.addWidget(self.row_elecs, 2, 1)
-        self.gridlayout.addWidget(row_elecsU, 2, 2)
+        self.gridlayout.addWidget(row_elecsL, 3, 0)
+        self.gridlayout.addWidget(self.row_elecs, 3, 1)
+        self.gridlayout.addWidget(row_elecsU, 3, 2)
 
         col_spaceU = QLabel('cm', self)
         col_spaceL = QLabel('Column spacing:', self)
         self.col_space = QLineEdit(str(self.col_dist_val), self)
-        self.gridlayout.addWidget(col_spaceL, 3, 0)
-        self.gridlayout.addWidget(self.col_space, 3, 1)
-        self.gridlayout.addWidget(col_spaceU, 3, 2)
+        self.gridlayout.addWidget(col_spaceL, 4, 0)
+        self.gridlayout.addWidget(self.col_space, 4, 1)
+        self.gridlayout.addWidget(col_spaceU, 4, 2)
 
         row_spaceU = QLabel('cm', self)
         row_spaceL = QLabel('Row spacing:', self)
         self.row_space = QLineEdit(str(self.row_dist_val), self)
-        self.gridlayout.addWidget(row_spaceL, 4, 0)
-        self.gridlayout.addWidget(self.row_space, 4, 1)
-        self.gridlayout.addWidget(row_spaceU, 4, 2)
-
-        # channel_offsetU = QLabel('n', self)
-        # channel_offsetL = QLabel('Channel offset:', self)
-        # self.channel_offset = QLineEdit(str(self.channel_offset_val), self)
-        # self.gridlayout.addWidget(channel_offsetL, 4, 0)
-        # self.gridlayout.addWidget(self.channel_offset, 4, 1)
-        # self.gridlayout.addWidget(channel_offsetU, 4, 2)
+        self.gridlayout.addWidget(row_spaceL, 5, 0)
+        self.gridlayout.addWidget(self.row_space, 5, 1)
+        self.gridlayout.addWidget(row_spaceU, 5, 2)
 
         elec_typeL = QLabel('Electrode Type:', self)
         self.elec_type = QLineEdit(self.elec_type_val, self)
-        self.gridlayout.addWidget(elec_typeL, 5, 0)
-        self.gridlayout.addWidget(self.elec_type, 5, 1)
+        self.gridlayout.addWidget(elec_typeL, 6, 0)
+        self.gridlayout.addWidget(self.elec_type, 6, 1)
 
         ref_elec_typeL = QLabel('Ref. Electrode Type:', self)
         self.ref_elec_type = QLineEdit(self.ref_elec_type_val, self)
-        self.gridlayout.addWidget(ref_elec_typeL, 6, 0)
-        self.gridlayout.addWidget(self.ref_elec_type, 6, 1)
+        self.gridlayout.addWidget(ref_elec_typeL, 7, 0)
+        self.gridlayout.addWidget(self.ref_elec_type, 7, 1)
 
         ref_elec_xU = QLabel('m', self)
         ref_elec_xL = QLabel('Ref. Electrode Pos-x:', self)
         self.ref_elec_x = QLineEdit('%.2f' % self.ref_elec_posx_val, self)
-        self.gridlayout.addWidget(ref_elec_xL, 7, 0)
-        self.gridlayout.addWidget(self.ref_elec_x, 7, 1)
-        self.gridlayout.addWidget(ref_elec_xU, 7, 2)
+        self.gridlayout.addWidget(ref_elec_xL, 8, 0)
+        self.gridlayout.addWidget(self.ref_elec_x, 8, 1)
+        self.gridlayout.addWidget(ref_elec_xU, 8, 2)
 
         ref_elec_yU = QLabel('m', self)
         ref_elec_yL = QLabel('Ref. Electrode Pos-y:', self)
         self.ref_elec_y = QLineEdit('%.2f' % self.ref_elec_posy_val, self)
-        self.gridlayout.addWidget(ref_elec_yL, 8, 0)
-        self.gridlayout.addWidget(self.ref_elec_y, 8, 1)
-        self.gridlayout.addWidget(ref_elec_yU, 8, 2)
+        self.gridlayout.addWidget(ref_elec_yL, 9, 0)
+        self.gridlayout.addWidget(self.ref_elec_y, 9, 1)
+        self.gridlayout.addWidget(ref_elec_yU, 9, 2)
 
         water_depthU = QLabel('cm', self)
         water_depthL = QLabel('Water depth:', self)
         self.water_depth = QLineEdit('%.2f' % self.water_depth_val, self)
-        self.gridlayout.addWidget(water_depthL, 9, 0)
-        self.gridlayout.addWidget(self.water_depth, 9, 1)
-        self.gridlayout.addWidget(water_depthU, 9, 2)
+        self.gridlayout.addWidget(water_depthL, 10, 0)
+        self.gridlayout.addWidget(self.water_depth, 10, 1)
+        self.gridlayout.addWidget(water_depthU, 10, 2)
 
         space = QLabel('', self)
-        self.gridlayout.addWidget(space, 10, 0)
+        self.gridlayout.addWidget(space, 11, 0)
 
 class Hardware_settings_tab(QWidget):
     def __init__(self):
@@ -276,27 +275,6 @@ class Recording_settings(QWidget):
         self.gridlayout.addWidget(experimenterL, 10, 0)
         self.gridlayout.addWidget(self.experimenter, 10, 1)
 
-        # DataTimeU = QLabel('ms', self)
-        # DataTimeL = QLabel('Data Time:', self)
-        # self.datatime = QLineEdit(str(self.datatime_val), self)
-        # self.gridlayout.addWidget(DataTimeL, 11, 0)
-        # self.gridlayout.addWidget(self.datatime, 11, 1)
-        # self.gridlayout.addWidget(DataTimeU, 11, 2)
-        #
-        # DataIntervalU = QLabel('ms', self)
-        # DataIntervalL = QLabel('Data Interval:', self)
-        # self.datainterval = QLineEdit(str(self.datainterval_val), self)
-        # self.gridlayout.addWidget(DataIntervalL, 12, 0)
-        # self.gridlayout.addWidget(self.datainterval, 12, 1)
-        # self.gridlayout.addWidget(DataIntervalU, 12, 2)
-        #
-        # BufferTimeU = QLabel('s', self)
-        # BufferTimeL = QLabel('Buffer Time:', self)
-        # self.buffertime = QLineEdit(str(self.buffertime_val), self)
-        # self.gridlayout.addWidget(BufferTimeL, 13, 0)
-        # self.gridlayout.addWidget(self.buffertime, 13, 1)
-        # self.gridlayout.addWidget(BufferTimeU, 13, 2)
-
         space = QLabel('', self)
         self.gridlayout.addWidget(space, 11, 0)
 
@@ -395,8 +373,10 @@ class MainWindow(QTabWidget):
         cfg = f.readlines()
 
         for line in cfg:
-            if 'Gridno' in line:
+            if 'Gridno1' in line:
                 self.Grid.grid_no_val = int(line.split(':')[1].strip())
+            elif 'RecordMode1' in line:
+                self.Grid.rec_mode_val = line.split(':')[1].strip()
             elif 'Columns1' in line:
                 self.Grid.columns_val = int(line.split(':')[1].strip())
             elif 'Rows1' in line:
@@ -407,7 +387,7 @@ class MainWindow(QTabWidget):
                 self.Grid.row_dist_val = float(line.split(':')[-1].strip().replace('cm', ''))
             # elif "ChannelOffset1" in line:
             #     self.Grid.channel_offset_val = int(line.split(':')[-1].strip())
-            elif "ElectrodeType1" in line:
+            elif " ElectrodeType1 " in line:
                 self.Grid.elec_type_val = line.split(':')[-1].strip()
             elif "RefElectrodeType1" in line:
                 self.Grid.ref_elec_type_val = line.split(":")[-1].strip()
@@ -475,7 +455,8 @@ class MainWindow(QTabWidget):
         f.write('*FishGrid\n')
         f.write('  Grid &1:\n')
         f.write('      Used1            : true\n')
-        f.write('      Gridno1          : 1\n')
+        f.write('      Gridno1          : %.0f\n' % int(self.Grid.grid_no.text()))
+        f.write('      RecordMode1      : %s\n' % self.Grid.rec_mode.text())
         f.write('      Columns1         : %.0f\n' % int(self.Grid.col_elecs.text()))
         f.write('      Rows1            : %.0f\n' % int(self.Grid.row_elecs.text()))
         f.write('      ColumnDistance1  : %.1fcm\n' % float(self.Grid.col_space.text()))