Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add real time plotting functionality #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions src/flow_meter.py
robinzhang24 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def __init__(self, port='/dev/ttyUSB0', baudrate=460800, slave_address=2):
self.device = Sfc5xxxShdlcDevice(ShdlcConnection(self.port), slave_address=slave_address)
self.device.activate_calibration(3) # specify calibration file index in list; default now on Helium (3)
# set units
self.unit = Sfc5xxxMediumUnit(
self._unit = Sfc5xxxMediumUnit(
Sfc5xxxUnitPrefix.ONE,
Sfc5xxxUnit.STANDARD_LITER,
Sfc5xxxUnitTimeBase.MINUTE
)
self.device.set_user_defined_medium_unit(self.unit)
self.device.set_user_defined_medium_unit(self._unit)


def set_baudrate(self, baudrate):
Expand All @@ -54,7 +54,7 @@ def get_reading(self, duration):
# dump what's already inside the buffer. this is only useful for low overhead
#buffer = self.device.read_measured_value_buffer(Sfc5xxxScaling.USER_DEFINED)
while len(reading) <= duration * 1000: # flow meter reads at 1kHz
robinzhang24 marked this conversation as resolved.
Show resolved Hide resolved
buffer = self.device.read_measured_value_buffer(Sfc5xxxScaling.USER_DEFINED)
buffer = self.device.read_measured_value_buffer(Sfc5xxxScaling.USER_DEFINED, max_reads=2)
#print(buffer.sampling_time) # the only "time" returned from read buffer command
reading.extend(buffer.values)
return reading
robinzhang24 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
40 changes: 29 additions & 11 deletions src/kernel.py
hjia94 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import matplotlib.pyplot as plt
import RPi.GPIO as GPIO
import time
#from wavegen_control import wavegen_control
Expand Down Expand Up @@ -37,7 +38,7 @@ def __init__(self, gpio_channel) -> None:

# def burst_mode(self, ncycles):
# self.wavegen.burst(enable=True, ncycles=ncycles, phase=0)


def acquire(self, duration, acquisition_limit=100):
"""
Expand All @@ -51,24 +52,41 @@ def acquire(self, duration, acquisition_limit=100):
acquisition_limit : Maximum number of acquisition the command can perform.
"""
shot_counts = 0
<<<<<<< HEAD
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(np.zeros(int(duration*1000)))
ax.set_title('real time flow rate')
ax.set_ylabel('flow rate ('+self.flow_meter._unit.__str__()+')')
t = time.time()
=======
fig, ax = plt.subplots()
line, = ax.plot(np.zeros(100))
>>>>>>> b958ecfa45898f915dbcd9bcc629f6a464d36aab
try:
while shot_counts <= acquisition_limit:
print('waiting for signals...')
GPIO.wait_for_edge(self.gpio_channel, GPIO.RISING) # stop the code until receiving a trigger
#time.sleep(.1)
t = time.time()
#readings = np.array(self.flow_meter.get_reading(duration))
readings = np.array(self.flow_meter.get_reading_single_cycle(duration))
readings = np.array(self.flow_meter.get_reading(duration))
#readings = np.array(self.flow_meter.get_reading_single_cycle(duration))
#readings = np.array(self.flow_meter.get_single_buffer())
np.savetxt(f'/home/pi/flow_meter/data/output_single_cycle_{shot_counts}.csv', readings)
np.savetxt(f'/home/pi/flow_meter/data/0208/output_{shot_counts}.csv', readings)
print('shot count {}'.format(shot_counts))
print(f'shot interval {time.time()-t}')
<<<<<<< HEAD
t = time.time()
line.set_ydata(readings[:int(duration*1000)])
ax.set_ylim(0,max(readings)*1.2)
=======
line.set_ydata(readings)
>>>>>>> b958ecfa45898f915dbcd9bcc629f6a464d36aab
fig.canvas.draw()
fig.canvas.flush_events()
shot_counts += 1
print('Maximum number of shot records reached!')
except KeyboardInterrupt:
GPIO.cleanup()
print('exit on ctrl-C keyboard interrupt')
except:
GPIO.cleanup()
print('an error occured')
print('Maximum number of shot records reached!')
GPIO.cleanup()
except Exception as error:
print('an error occured:\n', error)

3 changes: 3 additions & 0 deletions src/plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

#matplotlib.rcParams['backend'] = 'agg'

data = np.loadtxt('/home/pi/flow_meter/data/output_0.csv')
plt.plot(data)
Expand Down