working code

This commit is contained in:
till 2020-02-28 13:14:45 +01:00
parent f8bdc9df90
commit 844a834ab9

View File

@ -158,67 +158,131 @@ def read_cfg(cfg_file, now, init_read=False):
# continue # continue
def led_controll(pin, t0, dt, stop_flag, save_f=None): # def led_controll(pin, t0, dt, stop_flag, save_f=None):
next = t0 # next = t0
#
while True: # while True:
if stop_flag(): # if stop_flag():
break # break
if time() > next: # if time() > next:
save_t = next + time() - next # save_t = next + time() - next
GPIO.output(pin, GPIO.HIGH) # GPIO.output(pin, GPIO.HIGH)
sleep(0.1) # sleep(0.1)
GPIO.output(pin, GPIO.LOW) # GPIO.output(pin, GPIO.LOW)
#
if save_f != None: # if save_f != None:
save_f.write('%.4f' % (save_t)) # save_f.write('%.4f' % (save_t))
save_f.flush() # save_f.flush()
next += dt # 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()
run_led_interval = 2
sync_led_interval = 1
temp_interval = 300
next_t_t = 0
next_l_t = 0
print("\nDatetime: %.0f:%0f o'clock" % (datetime.datetime.now().hour, datetime.datetime.now().minute))
print("Terminate: %.0f:%0f o'clock" % (end_clock[0], end_clock[1]))
def temp_rec(w1_bus_path, temp_f, t0, dt, stop_flag):
next = t0
while True: while True:
if stop_flag(): if datetime.datetime.now().hour >= end_clock[0] and datetime.datetime.now().minute >= end_clock[1]:
print('\nEnd time reached ...')
break break
if time() > next: if (time() - t0) % temp_interval <= 0.1:
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(sync_led_pin, GPIO.LOW)
w1_f = open(w1_bus_path, 'r') w1_f = open(w1_bus_path, 'r')
w1_file = w1_f.readlines() w1_file = w1_f.readlines()
for line in w1_file: for line in w1_file:
if 't=' in line: if 't=' in line:
temp = float((line.split('=')[-1].strip())) / 1000 temp = float((line.split('=')[-1].strip())) / 1000
temp_f.write('%6.0f; %7.3f\n' % (next, temp)) temp_f.write('%6.0f; %7.3f\n' % (next_t_t, temp))
temp_f.flush() temp_f.flush()
break break
w1_f.close() w1_f.close()
next += dt next_t_t += temp_interval
def clock_controll(end_clock, run_led_pin, sync_led_pin, w1_bus_path, temp_f, led_f, stop_flag): elif (time() - t0) % run_led_interval <= 0.1:
t0 = time() GPIO.output(run_led_pin, GPIO.HIGH)
sub_stop_flag = False GPIO.output(sync_led_pin, GPIO.HIGH)
sleep(0.1)
run_led_interval = 2 GPIO.output(run_led_pin, GPIO.LOW)
run_thread = threading.Thread(target=led_controll, args=(run_led_pin, t0, run_led_interval, lambda: sub_stop_flag, None)) GPIO.output(sync_led_pin, GPIO.LOW)
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 elif (time() - t0) % sync_led_interval <= 0.1:
temp_thread = threading.Thread(target=temp_rec, args=(w1_bus_path, temp_f, t0, temp_interval, lambda: sub_stop_flag)) GPIO.output(sync_led_pin, GPIO.HIGH)
sleep(0.1)
GPIO.output(sync_led_pin, GPIO.LOW)
run_thread.start() if led_f != None:
sync_thread.start() led_f.write('%.4f' % (next_l_t))
temp_thread.start() led_f.flush()
next_l_t += sync_led_interval
else:
pass
def save_process(q, f, gain):
while True: while True:
if stop_flag(): if q.empty():
sub_stop_flag = True pass
sync_thread.join() elif q.full():
run_thread.join() print('\n!!! Queue full !!!')
break else:
if datetime.datetime.now().hour == end_clock[0] and datetime.datetime.now().minute == end_clock[1]: Cdata = q.get()
break # print(Cdata[:10])
Cdata.tofile(f)
f.flush()
# def duration_control(end_clock, stop_flag): # def duration_control(end_clock, stop_flag):
# while True: # while True:
@ -368,9 +432,10 @@ def main():
low_channel = 0 low_channel = 0
high_channel = channels - 1 high_channel = channels - 1
samples_per_channel = rate * 20 # * channels = Buffer size buffer_sec = 20
samples_per_channel = rate * buffer_sec # * channels = Buffer size
buffer_size = samples_per_channel * channels buffer_size = samples_per_channel * channels
print('channels: %.0f' % channels) print('\nChannels: %.0f' % channels)
# rate = 20000 # rate = 20000
scan_options = ScanOption.CONTINUOUS scan_options = ScanOption.CONTINUOUS
flags = AInScanFlag.DEFAULT flags = AInScanFlag.DEFAULT
@ -463,15 +528,22 @@ def main():
start_clock = [h, m] start_clock = [h, m]
end_clock = [h + 6, m] end_clock = [h + 6, m]
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 = 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() clock_thread.start()
print('go') q = mp.Queue()
save_thread = mp.Process(target=save_process, args=(q, f, gain))
save_thread.start()
print('\nRecording started.')
data = create_float_buffer(channel_count, samples_per_channel) data = create_float_buffer(channel_count, samples_per_channel)
print('----') print('----')
print(len(data)) print('Buffer size: %.0fn; %.0fsec' % (len(data), buffer_sec))
print(samples_per_channel) print('Channels: %.0f' % channel_count)
print(channel_count) print('Samples per channel: %.0f' % samples_per_channel)
print('----') print('----')
rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel, rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel,
@ -515,6 +587,7 @@ def main():
# print(index) # print(index)
if index >= save_range[0] and index < save_range[1]: if index >= save_range[0] and index < save_range[1]:
GPIO.output(LED2_pin, GPIO.HIGH)
dt = time() - cont_t dt = time() - cont_t
cont_t = time() cont_t = time()
@ -522,11 +595,18 @@ def main():
print('samples = %.0f' % (len(data[save_range[0]:save_range[1]]) / 15)) 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)) print('rate = %.2f Hz\n' % (len(data[save_range[0]:save_range[1]]) / 15 /dt))
(np.array(data[save_range[0]:save_range[1]], dtype=np.float32) / gain).tofile(f) # (np.array(data[save_range[0]:save_range[1]], dtype=np.float32) / gain).tofile(f)
f.flush() # f.flush()
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_ranges = np.roll(save_ranges, 1, axis=0)
save_range = save_ranges[0] save_range = save_ranges[0]
GPIO.output(LED2_pin, GPIO.LOW)
# if index < 0 or index == last_idx: # if index < 0 or index == last_idx:
# continue # continue
# #
@ -545,9 +625,14 @@ def main():
# print('save n flush') # print('save n flush')
# last_idx = index # last_idx = index
stop_flag = True #stop_flag = True
clock_thread.join() #clock_thread.join()
print('end') print('\nEmpty Queue')
while not q.empty():
sleep(0.01)
clock_thread.terminate()
save_thread.terminate()
print('\nDone!')
f.close() f.close()
temp_f.close() temp_f.close()