Hi @s-nadella! I believe that you started with this example:
import joulescope
import numpy as np
with joulescope.scan_require_one(config='auto') as js:
data = js.read(contiguous_duration=0.1)
current, voltage = np.mean(data, axis=0, dtype=np.float64)
print(f'{current} A, {voltage} V')
This example reads the full 1 Msps data from the JS220 into RAM, and then computes the mean over all samples to get the current and voltage. This is definitely overkill for what you want. You can use the statistics data. The statistics data is computed on instrument and dramatically lowers the data rate and host processing requirements.
The slowest statistics data rates is 1 Hz. If you just want average power, you can just get 31 statistics updates, subtract the ending energy from the starting energy and divide by 30. You can find the code for the pyjoulescope_driver statistics entry point here.
I also put together this quick example script which hopefully is close to what you have in mind:
# Copyright 2022-2023 Jetperch LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pyjoulescope_driver import Driver
import sys
import time
def _on_progress(fract, message):
# The MIT License (MIT)
# Copyright (c) 2016 Vladimir Ignatev
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
# OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fract = min(max(float(fract), 0.0), 1.0)
bar_len = 25
filled_len = int(round(bar_len * fract))
percents = int(round(100.0 * fract))
bar = '=' * filled_len + '-' * (bar_len - filled_len)
msg = f'[{bar}] {percents:3d}% {message:40s}\r'
sys.stdout.write(msg)
sys.stdout.flush()
def measure_energy(driver, device, duration=None):
"""Measure energy over a duration.
:param duration: The duration in seconds.
:return: The tuple of energy, duration.
"""
duration = 30 if duration is None else int(duration)
data = []
if duration <= 0:
return 0, 0
total_count = 10 * duration + 1 # need one extra since use first as a baseline
def _on_statistics_value(topic, value):
energy = value['accumulators']['energy']['value']
data.append(energy)
driver.subscribe(device + '/s/stats/value', 'pub', _on_statistics_value)
try:
while len(data) < total_count:
time.sleep(0.050)
duration_remaining = (total_count - len(data)) / 10
_on_progress(len(data) / total_count, f' {duration_remaining:.1f} seconds left')
except KeyboardInterrupt:
pass
finally:
driver.unsubscribe(device + '/s/stats/value', _on_statistics_value)
if len(data) > total_count:
data = data[:total_count]
actual_duration = (len(data) - 1) / 10.0
return data[-1] - data[0], actual_duration
def run():
duration = 30
with Driver() as d:
devices = d.device_paths()
if len(devices) != 1:
print('Found %d devices', len(devices))
return 1
device = devices[0]
d.open(device)
d.publish(device + '/s/i/range/mode', 'auto')
d.publish(device + '/s/stats/scnt', 100_000) # 10 Hz update rate
d.publish(device + '/s/stats/ctrl', 1)
energy, duration = measure_energy(d, device, duration=duration)
power_avg = energy / duration
print(f'\n{energy} J in {duration} s -> {power_avg} W average')
d.publish(device + '/s/stats/ctrl', 0)
d.close(device)
if __name__ == '__main__':
sys.exit(run())