diff --git a/rasp_grid.py b/rasp_grid.py index 85da674..39b72c9 100644 --- a/rasp_grid.py +++ b/rasp_grid.py @@ -45,7 +45,7 @@ def GPIO_setup(LED1_pin, LED2_pin, LED_out_pin, Button1_pin, Button2_pin, power_ return LED_status -def read_cfg(cfg_file, now, init_read=False): +def read_cfg(cfg_file, now, start_time=None, init_read=False): cfg_f = open(cfg_file, 'r+') cfg = cfg_f.readlines() @@ -70,7 +70,6 @@ def read_cfg(cfg_file, now, init_read=False): max_v = float(line.split(':')[1].strip().replace('mV', '')) elif 'Gain' in line: gain = int(line.split(':')[1].strip()) - gain = int(line.split(':')[1].strip()) channels = n_rows * n_cols + n_extra ### alter information and re-write ### @@ -78,7 +77,10 @@ def read_cfg(cfg_file, now, init_read=False): if "StartDate" in line: cfg[enu] = (' StartDate : %s\n' % now.strftime('%Y-%m-%d')) elif "StartTime" in line: - cfg[enu] = (' StartTime : %s\n' % (now.strftime('%H:%M:%S') + now.strftime(".%f")[:4])) + if start_time == None: + cfg[enu] = (' StartTime : %s\n' % (now.strftime('%H:%M:%S') + now.strftime(".%f")[:4])) + else: + cfg[enu] = (' StartTime : %s\n' % ('%2.f:%2.f:00' % (start_time[0], start_time[1]))) cfg_f.close() cfg_f = open(cfg_file, 'w+') @@ -87,138 +89,7 @@ def read_cfg(cfg_file, now, init_read=False): cfg_f.close() return channels, samplerate, n_cols, n_rows, max_v, gain - # for line in cfg: - # if 'Columns1' in line: - # self.Grid.columns_val = int(line.split(':')[1].strip()) - # elif 'Rows1' in line: - # self.Grid.rows_val = int(line.split(':')[1].strip()) - # elif "ColumnDistance1" in line: - # self.Grid.col_dist_val = float(line.split(':')[-1].strip().replace('cm', '')) - # elif "RowDistance1" in line: - # 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: - # self.Grid.elec_type_val = line.split(':')[-1].strip() - # elif "RefElectrodeType1" in line: - # self.Grid.ref_elec_type_val = line.split(":")[-1].strip() - # elif "RefElectrodePosX1" in line: - # self.Grid.ref_elec_posx_val = float(line.split(':')[-1].strip().replace('m', '')) - # elif 'RefElectrodePosY1' in line: - # self.Grid.ref_elec_posy_val = float(line.split(':')[-1].strip().replace('m', '')) - # elif 'WaterDepth1' in line: - # self.Grid.water_depth_val = float(line.split(':')[-1].strip().replace('m', '')) - # - # elif "AISampleRate" in line: - # self.HardWare.ai_sr_val = float(line.split(':')[-1].strip().replace('kHz', '')) - # elif "AIMaxVolt" in line: - # self.HardWare.ai_max_vol_val = float(line.split(':')[-1].strip().replace('mV', '')) - # elif "AmplName" in line: - # self.HardWare.amp_name_val = line.split(':')[-1].strip() - # elif "AmplModel" in line: - # self.HardWare.amp_model_val = line.split(':')[-1].strip() - # elif ' Type ' in line: - # self.HardWare.amp_type_val = line.split(':')[-1].strip() - # elif 'Gain' in line: - # self.HardWare.gain_val = line.split(':')[-1].strip() - # elif "HighpassCutoff" in line: - # self.HardWare.highpass_cutoff_val = int(line.split(':')[-1].strip().replace('Hz', '')) - # elif 'LowpassCutoff' in line: - # self.HardWare.lowpass_cutoff_val = float(line.split(':')[-1].strip().replace('kHz', '')) - # - # elif "Experiment.Name" in line: - # self.Recording.experiment_name_val = line.split(':')[-1].strip() - # elif "StartDate" in line: - # self.Recording.startdate_val = line.split(':')[-1].strip() - # elif "StartTime" in line: - # self.Recording.starttime_val = ':'.join(line.split(':')[1:]).strip() - # elif "Location" in line: - # self.Recording.location_val = line.split(':')[-1].strip() - # elif "Position" in line: - # self.Recording.position_val = line.split(':')[-1].strip() - # elif "WaterTemperature" in line: - # self.Recording.water_temp_val = float(line.split(':')[-1].strip().replace('C', '')) - # elif "WaterConductivity" in line: - # self.Recording.water_cond_val = float(line.split(':')[-1].strip().replace('uS/cm', '')) - # elif 'WaterpH' in line: - # self.Recording.water_ph_val = float(line.split(':')[-1].strip().replace('pH', '')) - # elif "WaterOxygen" in line: - # self.Recording.water_oxy_val = float(line.split(':')[-1].strip().replace('mg/l', '')) - # elif "Comment" in line: - # self.Recording.comment_val = ':'.join(line.split(':')[1:]).strip() - # elif "Experimenter" in line: - # self.Recording.experimenter_val = ':'.join(line.split(':')[1:]).strip() - # elif "DataTime" in line: - # self.Recording.datatime_val = int(line.split(':')[-1].strip().replace('ms', '')) - # elif "DataInterval" in line: - # self.Recording.datainterval_val = int(line.split(':')[-1].strip().replace('ms', '')) - # elif "BufferTime" in line: - # self.Recording.buffertime_val = int(line.split(':')[-1].strip().replace('s', '')) - # else: - # continue - - -# def led_controll(pin, t0, dt, stop_flag, save_f=None): -# next = t0 -# -# while True: -# if stop_flag(): -# break -# if time() > next: -# save_t = next + time() - next -# GPIO.output(pin, GPIO.HIGH) -# sleep(0.1) -# GPIO.output(pin, GPIO.LOW) -# -# if save_f != None: -# save_f.write('%.4f' % (save_t)) -# save_f.flush() -# next += dt -# -# -# -# def temp_rec(w1_bus_path, temp_f, t0, dt, stop_flag): -# next = t0 -# while True: -# if stop_flag(): -# break -# if time() > next: -# w1_f = open(w1_bus_path, 'r') -# w1_file = w1_f.readlines() -# for line in w1_file: -# if 't=' in line: -# temp = float((line.split('=')[-1].strip())) / 1000 -# temp_f.write('%6.0f; %7.3f\n' % (next, temp)) -# temp_f.flush() -# break -# w1_f.close() -# next += dt -# -# def clock_controll(end_clock, run_led_pin, sync_led_pin, w1_bus_path, temp_f, led_f, stop_flag): -# t0 = time() -# sub_stop_flag = False -# -# run_led_interval = 2 -# run_thread = threading.Thread(target=led_controll, args=(run_led_pin, t0, run_led_interval, lambda: sub_stop_flag, None)) -# -# sync_led_interval = 1 -# sync_thread = threading.Thread(target=led_controll, args=(sync_led_pin, t0, sync_led_interval, lambda: sub_stop_flag, led_f)) -# -# temp_interval = 300 -# temp_thread = threading.Thread(target=temp_rec, args=(w1_bus_path, temp_f, t0, temp_interval, lambda: sub_stop_flag)) -# -# run_thread.start() -# sync_thread.start() -# temp_thread.start() -# -# while True: -# if stop_flag(): -# sub_stop_flag = True -# sync_thread.join() -# run_thread.join() -# break -# if datetime.datetime.now().hour == end_clock[0] and datetime.datetime.now().minute == end_clock[1]: -# break + def clock_process(end_clock, run_led_pin, sync_led_pin, w1_bus_path, temp_f, led_f): t0 = time() @@ -236,10 +107,10 @@ def clock_process(end_clock, run_led_pin, sync_led_pin, w1_bus_path, temp_f, led print('\nEnd time reached ...') break if (time() - t0) % temp_interval <= 0.1: - GPIO.output(run_led_pin, GPIO.HIGH) + #GPIO.output(run_led_pin, GPIO.HIGH) GPIO.output(sync_led_pin, GPIO.HIGH) sleep(0.1) - GPIO.output(run_led_pin, GPIO.LOW) + #GPIO.output(run_led_pin, GPIO.LOW) GPIO.output(sync_led_pin, GPIO.LOW) w1_f = open(w1_bus_path, 'r') @@ -254,10 +125,10 @@ def clock_process(end_clock, run_led_pin, sync_led_pin, w1_bus_path, temp_f, led next_t_t += temp_interval elif (time() - t0) % run_led_interval <= 0.1: - GPIO.output(run_led_pin, GPIO.HIGH) + #GPIO.output(run_led_pin, GPIO.HIGH) GPIO.output(sync_led_pin, GPIO.HIGH) sleep(0.1) - GPIO.output(run_led_pin, GPIO.LOW) + #GPIO.output(run_led_pin, GPIO.LOW) GPIO.output(sync_led_pin, GPIO.LOW) elif (time() - t0) % sync_led_interval <= 0.1: @@ -284,70 +155,17 @@ def save_process(q, f, gain): Cdata.tofile(f) f.flush() -# def duration_control(end_clock, stop_flag): -# while True: -# if stop_flag(): -# break -# -# if datetime.datetime.now().hour == end_clock[0] and datetime.datetime.now().minute == end_clock[1]: -# break -# -# def led_controll_box(LED_t, LED1_pin, LED_out_pin, led_f, stop_flag): -# LED_t_interval = 2 -# out_LED_interval = 1 -# -# led_on_times = [] -# -# status0 = False -# status1 = False -# while True: -# if stop_flag(): -# GPIO.output(LED1_pin, GPIO.LOW) -# GPIO.output(LED_out_pin, GPIO.LOW) -# break -# -# if (time() - LED_t) % LED_t_interval < .1: -# status0 = True -# GPIO.output(LED1_pin, GPIO.HIGH) -# elif (time() - LED_t) % LED_t_interval >= .1 and status0 == True: -# status0 = False -# GPIO.output(LED1_pin, GPIO.LOW) -# else: -# pass -# -# if (time() - LED_t) % out_LED_interval < .1: -# status1 = True -# GPIO.output(LED_out_pin, GPIO.HIGH) -# -# led_on_times.append(time() - LED_t) -# led_f.write('%.4f' % (time() - LED_t)) -# led_f.flush() -# elif (time() - LED_t) % out_LED_interval >= .1 and status1 == True: -# GPIO.output(LED_out_pin, GPIO.LOW) -# status1 = False -# else: -# pass - -# def record_temperature(temp_t0, w1_bus_path, temp_f, stop_flag): -# next_temp_t = 0 -# temp_interval = 300 -# -# while True: -# if stop_flag(): -# break -# -# if time() - temp_t0 > next_temp_t: -# w1_f = open(w1_bus_path, 'r') -# w1_file = w1_f.readlines() -# for line in w1_file: -# if 't=' in line: -# temp = float((line.split('=')[-1].strip())) / 1000 -# temp_f.write('%6.0f; %7.3f\n' % (next_temp_t, temp)) -# temp_f.flush() -# break -# -# w1_f.close() -# next_temp_t += temp_interval +def save_controll(q, pin): + while True: + if q.empty(): + sleep(0.1) + pass + else: + trigger = q.get() + GPIO.output(pin, GPIO.HIGH) + sleep(0.5) + GPIO.output(pin, GPIO.LOW) + def main(): LED1_pin = 11 @@ -378,14 +196,23 @@ def main(): valid = True if not valid: start_clock = [10, 00] - end_clock = [16, 00] + end_clock = [10, 30] # get init cfg if os.path.exists('/media/pi/data1'): init_path = '/media/pi/data1' - else: + elif os.path.exists('/home/raab/data/rasp_test'): init_path = '/home/raab/data/rasp_test' + else: + for i in range(10): + GPIO.output(LED1_pin, GPIO.HIGH) + sleep(0.5) + GPIO.output(LED1_pin, GPIO.LOW) + sleep(0.5) + print('got no path') + quit() + init_cfgfile = os.path.join(init_path, 'fishgrid.cfg') if os.path.exists(init_cfgfile): @@ -395,16 +222,22 @@ def main(): quit() # create save folder and copy cfg file - path = os.path.join(init_path, now.strftime(path_format)) - os.makedirs(path) + + start_str = ('%2.f_%2.f' % (start_clock[0], start_clock[1])).replace(' ', '0') + path = os.path.join(init_path, now.strftime(path_format[:-9]) + start_str) #ToDo: Edit here + + if not os.path.exists(path): + os.makedirs(path) copyfile(os.path.join(os.path.split(path)[0], 'fishgrid.cfg'), os.path.join(path, 'fishgrid.cfg')) cfgfile = os.path.join(path, 'fishgrid.cfg') # read and edit config file - channels, rate, n_cols, n_rows, max_v, gain = read_cfg(cfgfile, now) + channels, rate, n_cols, n_rows, max_v, gain = read_cfg(cfgfile, now, start_clock) file = os.path.join(path, 'traces-grid1.raw') temp_file = os.path.join(path, 'temperatures.csv') led_file = os.path.join(path, 'led_times.csv') + led_file2 = os.path.join(path, 'led_idxs.csv') + # find w1bus for temp record_temp = False @@ -418,6 +251,7 @@ def main(): f = open(file, 'wb') led_f = open(led_file, 'w') + led_f2 = open(led_file2, 'w') # f.close() @@ -497,24 +331,30 @@ def main(): GPIO.output(LED1_pin, GPIO.HIGH) LED_status[0] = True - GPIO.output(LED2_pin, GPIO.HIGH) - LED_status[0] = True + GPIO.output(LED2_pin, GPIO.LOW) + LED_status[1] = False sleep(.5) GPIO.output(LED1_pin, GPIO.LOW) LED_status[0] = False - GPIO.output(LED2_pin, GPIO.LOW) - LED_status[0] = False + GPIO.output(LED2_pin, GPIO.HIGH) + LED_status[1] = True sleep(.5) GPIO.output(LED1_pin, GPIO.HIGH) LED_status[0] = True - GPIO.output(LED2_pin, GPIO.HIGH) - LED_status[0] = True + GPIO.output(LED2_pin, GPIO.LOW) + LED_status[1] = False sleep(.5) GPIO.output(LED1_pin, GPIO.LOW) LED_status[0] = False + GPIO.output(LED2_pin, GPIO.HIGH) + LED_status[1] = True + sleep(.5) + + GPIO.output(LED2_pin, GPIO.LOW) + LED_status[1] = False disp_eth_power = True stop_flag = False @@ -522,21 +362,28 @@ def main(): while True: if datetime.datetime.now().hour == start_clock[0] and datetime.datetime.now().minute == start_clock[1]: break - elif datetime.datetime.now().hour > start_clock[0] or datetime.datetime.now().minute > start_clock[1]: + elif datetime.datetime.now().hour * 60 + datetime.datetime.now().minute >= start_clock[0] * 60 + start_clock[1]: + # elif datetime.datetime.now().hour >= start_clock[0] and datetime.datetime.now().minute >= start_clock[1]: h = datetime.datetime.now().hour m = datetime.datetime.now().minute start_clock = [h, m] - end_clock = [h + 6, m] + # end_clock = [h + 6, m] + + + GPIO.output(LED2_pin, GPIO.HIGH) + LED_status[1] = True #clock_thread = threading.Thread(target=clock_controll, args=(end_clock, LED1_pin, LED_out_pin, w1_bus_path, temp_f, led_f, lambda: stop_flag)) #clock_thread.start() - clock_thread = mp.Process(target=clock_process, args=(end_clock, LED1_pin, LED_out_pin, w1_bus_path, temp_f, led_f)) - clock_thread.start() - - q = mp.Queue() - save_thread = mp.Process(target=save_process, args=(q, f, gain)) - save_thread.start() + # clock_thread = mp.Process(target=clock_process, args=(end_clock, LED1_pin, LED_out_pin, w1_bus_path, temp_f, led_f)) + # clock_thread.start() + # + # q = mp.Queue() + # # save_thread = mp.Process(target=save_process, args=(q, f, gain)) + # # save_thread.start() + # save_thread = mp.Process(target=save_controll, args=(q, LED1_pin)) + # save_thread.start() print('\nRecording started.') data = create_float_buffer(channel_count, samples_per_channel) @@ -546,17 +393,32 @@ def main(): print('Samples per channel: %.0f' % samples_per_channel) print('----') + LED_t0 = time() + LED_t_interval = 5 + + sync_LED_t_interval = 5 + + temp_t0 = time() + next_temp_t = 0 + temp_interval = 300 # sec --> 5 min + + next_l_t = 0 + + disp_eth_power = True + rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel, rate, scan_options, flags, data) + status, transfer_status = ai_device.get_scan_status() - save_ranges = np.array([[int(buffer_size / 2), len(data)], [0, int(buffer_size / 2)]]) - save_range = save_ranges[0] + last_idx = 0 + # while GPIO.input(Button1_pin) == GPIO.LOW and clock_thread.is_alive(): + #while GPIO.input(Button1_pin) == GPIO.LOW and datetime.datetime.now().hour * 60 + datetime.datetime.now().minute < end_clock[0] * 60 + end_clock[1]: + buffer_no = 0 - cont_t = time() - while GPIO.input(Button1_pin) == GPIO.LOW and clock_thread.is_alive(): + while GPIO.input(Button1_pin) == GPIO.LOW: if GPIO.input(Button2_pin) == GPIO.HIGH: if (time() - last_button_2_t) > 5: @@ -580,58 +442,118 @@ def main(): disp_eth_power = True last_button_2_t = time() + ################# + if record_temp == True: + if time() - temp_t0 > next_temp_t: + w1_f = open(w1_bus_path, 'r') + w1_file = w1_f.readlines() + for line in w1_file: + if 't=' in line: + temp = float((line.split('=')[-1].strip())) / 1000 + temp_f.write('%6.0f; %7.3f\n' % (next_temp_t, temp)) + temp_f.flush() + break + + w1_f.close() + next_temp_t += temp_interval + + # blinking LED (run) + if (time() - LED_t0) % LED_t_interval < .5 and LED_status[0] == False: + # if (time() - LED_t0) % LED_t_interval < .5: + LED_status[0] = True + GPIO.output(LED1_pin, GPIO.HIGH) + elif (time() - LED_t0) % LED_t_interval >= .5 and LED_status[0] == True: +# elif (time() - LED_t0) % LED_t_interval >= .5: + LED_status[0] = False + GPIO.output(LED1_pin, GPIO.LOW) + else: + pass + + if (time() - LED_t0) % sync_LED_t_interval < .5 and LED_status[2] == False: + if led_f != None: + Cidx = int((buffer_size * buffer_no + last_idx) / channels) + led_f.write('%.4f\n' % (time()-LED_t0)) + led_f.flush() + led_f2.write('%.0f\n' % Cidx) + led_f2.flush() + + LED_status[2] = True + GPIO.output(LED_out_pin, GPIO.HIGH) + elif (time() - LED_t0) % sync_LED_t_interval >= .5 and LED_status[2] == True: + LED_status[2] = False + GPIO.output(LED_out_pin, GPIO.LOW) + else: + pass + # 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() + + ########################################## # Get the status of the background operation status, transfer_status = ai_device.get_scan_status() index = transfer_status.current_index # print(index) - if index >= save_range[0] and index < save_range[1]: - GPIO.output(LED2_pin, GPIO.HIGH) - dt = time() - cont_t - cont_t = time() - print("dt = %.3f sec" % (dt)) - print('samples = %.0f' % (len(data[save_range[0]:save_range[1]]) / 15)) - print('rate = %.2f Hz\n' % (len(data[save_range[0]:save_range[1]]) / 15 /dt)) + ### the new way ### + # if index == -1: + # continue + # + # if index > save_range[0] and index <= save_range[1]: + # pass + # + # else: + # GPIO.output(LED1_pin, GPIO.HIGH) + # dt = time() - cont_t + # cont_t = time() + # + # Cdata = np.copy(np.array(data[save_range[0]:save_range[1]], dtype=np.float32) / gain) + # q.put(Cdata) + # + # save_ranges = np.roll(save_ranges, -1, axis=0) + # save_range = save_ranges[0] + # + # + # GPIO.output(LED1_pin, GPIO.LOW) + ###---### - # (np.array(data[save_range[0]:save_range[1]], dtype=np.float32) / gain).tofile(f) - # f.flush() + ### the old way ### + if index < 0 or index == last_idx: + continue - Cdata = np.copy(np.array(data[save_range[0]:save_range[1]], dtype=np.float32) / gain) - q.put(Cdata) + if index > last_idx: + (np.array(data[last_idx:index] / gain, dtype=np.float32)).tofile(f) - save_ranges = np.roll(save_ranges, 1, axis=0) - save_range = save_ranges[0] + else: + (np.array(data[last_idx:] / gain, dtype=np.float32)).tofile(f) + if datetime.datetime.now().hour * 60 + datetime.datetime.now().minute >= end_clock[0] * 60 + end_clock[1]: + f.flush() + GPIO.output(LED1_pin, GPIO.LOW) + GPIO.output(LED_out_pin, GPIO.LOW) + break + (np.array(data[:index] / gain, dtype=np.float32)).tofile(f) + f.flush() - GPIO.output(LED2_pin, GPIO.LOW) + buffer_no += 1 + last_idx = index - # if index < 0 or index == last_idx: - # continue - # - # if index > last_idx: - # - # if index-last_idx > buffer_size / 5: - # (np.array(data[last_idx:index], dtype=np.float32) / gain).tofile(f) - # last_idx = index - # print('save') - # else: - # buffer_size = samples_per_channel * channels - # if buffer_size - last_idx + index > buffer_size / 5: - # (np.array(data[last_idx:], dtype=np.float32) / gain).tofile(f) - # (np.array(data[:index], dtype=np.float32) / gain).tofile(f) - # f.flush() - # print('save n flush') - # last_idx = index + ###---### #stop_flag = True #clock_thread.join() - print('\nEmpty Queue') - while not q.empty(): - sleep(0.01) - clock_thread.terminate() - save_thread.terminate() + # print('\nEmpty Queue') + # while not q.empty(): + # sleep(0.01) + # clock_thread.terminate() + # save_thread.terminate() + # save_thread.terminate() print('\nDone!') f.close()