Skip to content

Commit

Permalink
Display the sampling rate both in the terminal and on the HTML plot w…
Browse files Browse the repository at this point in the history
…hen the program is started in debug mode.
  • Loading branch information
David00 committed Feb 12, 2021
1 parent d7d6d96 commit fbd6a20
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
webroot = '/var/www/html'


def plot_data(samples, title, *args):
def plot_data(samples, title, *args, **kwargs):
# Plots the raw sample data from the individual CT channels and the AC voltage channel.

# Check to see if the sample rate was included in the parameters passed in.
if kwargs:
if 'sample_rate' in kwargs.keys():
sample_rate = kwargs['sample_rate']

else:
sample_rate = None

if args: # Make plot for a single CT channel
ct_selection = args[0]
Expand All @@ -23,7 +31,7 @@ def plot_data(samples, title, *args):
fig.add_trace(go.Scatter(x=x, y=voltage, mode='lines', name='Original Voltage Wave'), secondary_y=True)

# Get the phase shifted voltage wave
fig.add_trace(go.Scatter(x=x, y=samples['new_v'], mode='lines', name=f'Phase corrected voltage wave ({ct_selection})'), secondary_y=True)
fig.add_trace(go.Scatter(x=x, y=samples['new_v'], mode='lines', name=f'Phase corrected voltage wave ({ct_selection})'), secondary_y=True)

else: # Make plot for all CT channels
ct0 = samples['ct0']
Expand Down Expand Up @@ -63,6 +71,10 @@ def plot_data(samples, title, *args):
div = plotly.offline.plot(fig, show_link=False, output_type='div', include_plotlyjs='cdn')
home_link = '<a href="/">Back to Index</a>'
div = home_link + div
if sample_rate:
sample_rate = f'<p>Sample Rate: {sample_rate} KSPS</p>'
div += sample_rate


with open(f"{webroot}/{title.replace(' ', '_')}.html", 'w') as f:
f.write(div)
16 changes: 14 additions & 2 deletions power-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,20 @@ def get_ip():
if MODE.lower() == 'debug':
# This mode is intended to take a look at the raw CT sensor data. It will take 2000 samples from each CT sensor, plot them to a single chart, write the chart to an HTML file located in /var/www/html/, and then terminate.
# It also stores the samples to a file located in ./data/samples/last-debug.pkl so that the sample data can be read when this program is started in 'phase' mode.

# Time sample collection
start = timeit.default_timer()
samples = collect_data(2000)
logger.debug("Finished Collecting Samples")
stop = timeit.default_timer()
duration = stop - start

# Calculate Sample Rate in Kilo-Samples Per Second.
sample_count = sum([ len(samples[x]) for x in samples.keys() if type(samples[x]) == list ])

print(f"sample count is {sample_count}")
sample_rate = round((sample_count / duration) / 1000, 2)

logger.debug(f"Finished Collecting Samples. Sample Rate: {sample_rate} KSPS")
ct0_samples = samples['ct0']
ct1_samples = samples['ct1']
ct2_samples = samples['ct2']
Expand All @@ -671,7 +683,7 @@ def get_ip():

title = title.replace(" ","_")
logger.debug("Building plot.")
plot_data(samples, title)
plot_data(samples, title, sample_rate=sample_rate)
ip = get_ip()
if ip:
logger.info(f"Chart created! Visit http://{ip}/{title}.html to view the chart. Or, simply visit http://{ip} to view all the charts created using 'debug' and/or 'phase' mode.")
Expand Down

0 comments on commit fbd6a20

Please sign in to comment.