Closed StanKatsyukCT closed 3 years ago
Hi @StanKatsyukCT, please try adding ppk2_test.set_source_voltage(3300)
to device setup.
I think this might be causing your issues.
Like this:
ppk2_test = PPK2_API(ppk2_port)
ppk2_test.get_modifiers()
ppk2_test.use_source_meter() # set source meter mode
ppk2_test.set_source_voltage(3300) # set source voltage to 3.3V
ppk2_test.toggle_DUT_power("OFF") # ensure DUT power is disabled before starting
ppk2_test.toggle_DUT_power("ON") # enable DUT power
Thank you and please report back!
Hi @NejcKle , thanks for the suggestion. I tried this but im still not getting the same readings as NRF Connect.
In your experience, what should I set the following to?:
As I understand it, the sample_size
param is to set how many bytes are read from serial read? Is this the same value that NRF is hardcoded to do at 10k?
Hi @StanKatsyukCT, the ppk2 actually streams data at a fixed rate of 100k samples per second.
As the sample rate is fixed, the sleep_time
between reads should be set to 10ms or 1ms, so no samples are missed.
We are doing averaging over a fixed sized window to get the same readings as the nRF Connect app reports. Please check out https://github.com/IRNAS/ppk2-api-python/issues/5 for more information.
The sample_size
param seen in code is the size of each sampled value in bytes and should not be changed!
Hope this helps!
@NejcKle thanks for the suggestion. Is the window_size
variable in your code the same as the "Sampling Parameters" in NRF Connects Power Profiler app?
I am currently experimenting with a 1000
window size, but I'm still not able to get the same readings as I do via the NRF app.
My updated code:
` """ Basic usage of PPK2 Python API. The basic ampere mode sequence is:
ppk2s_connected = PPK2_API.list_devices() ppk2_port = "/dev/tty.usbmodemD325C91691932" boot_up_sleep_time = 45 window_size = 1000
if(len(ppk2s_connected) == 1): ppk2_port = ppk2s_connected[0] print(f'Found PPK2 at {ppk2_port}') else: print(f'Too many connected PPK2\'s: {ppk2s_connected}') exit()
ppk2_test = PPK2_API(ppk2_port) ppk2_test.get_modifiers() ppk2_test.use_source_meter() # set source meter mode ppk2_test.set_source_voltage(3300) # set source voltage to 3.3V ppk2_test.toggle_DUT_power("OFF") # ensure DUT power is disabled before starting ppk2_test.toggle_DUT_power("ON") # enable DUT power
print(f'Sleeping for {boot_up_sleep_time} seconds') time.sleep(boot_up_sleep_time) # sleep to allow device to fully boot ppk2_test.start_measuring()
def _average_samples(list, window_size): """Average samples based on window size""" chunks = [list[val:val + window_size] for val in range(0, len(list), window_size)] avgs = [] for chunk in chunks: avgs.append(sum(chunk) / len(chunk)) return avgs
while True: read_data = ppk2_test.get_data() if read_data != b'': samples = _average_samples(ppk2_test.get_samples(read_data), 1000) # optionally average samples print(samples[0]) time.sleep(.01) # lower time between sampling -> less samples read in one sampling period
ppk2_test.stop_measuring() `
For reference, the NRF Power Profiler shows about ≈ 50uA but via the python code above i get ≈ 20uA
Thanks again for all your insight
@StanKatsyukCT yes, the window_size
variable is the same as the "Sampling Parameters" in the nRF Connect App.
Could you try averaging samples in a time window? In the screenshot you provided the data gets averaged over a period of 7 seconds. Something like this:
start_time = time.time()
sampling_duration = 7 # set window size in seconds
sampling_period_samples = []
while True:
read_data = ppk2_test.get_data()
if read_data != b'':
samples = _average_samples(ppk2_test.get_samples(read_data), 1000) # optionally average samples
if time.time() - start_time < sampling_duration:
sampling_period_samples += samples # append to list
else:
print(f"Average of sampling period: {sum(sampling_period_samples) / len(sampling_period_samples)}")
sampling_period_samples = [] # clear list
start_time = time.time()
time.sleep(.01) # lower time between sampling -> less samples read in one sampling period
@NejcKle Im still not getting the same readings :/
Let me run everything im doing by you just to make sure im not missing something:
The most recent code I have is:
"""
Basic usage of PPK2 Python API.
The basic ampere mode sequence is:
1. read modifiers
2. set use_source_meter
3. read stream of data
"""
import time
from src.ppk2_api import PPK2_API
ppk2s_connected = PPK2_API.list_devices()
ppk2_port = "/dev/tty.usbmodemD325C91691932"
boot_up_sleep_time = 45
if(len(ppk2s_connected) == 1):
ppk2_port = ppk2s_connected[0]
print(f'Found PPK2 at {ppk2_port}')
else:
print(f'Too many connected PPK2\'s: {ppk2s_connected}')
exit()
ppk2_test = PPK2_API(ppk2_port)
ppk2_test.get_modifiers()
ppk2_test.use_source_meter() # set source meter mode
ppk2_test.set_source_voltage(3100) # set source voltage to 3.3V
ppk2_test.toggle_DUT_power("OFF") # ensure DUT power is disabled before starting
ppk2_test.toggle_DUT_power("ON") # enable DUT power
def setup():
print(f'Sleeping for {boot_up_sleep_time} seconds')
time.sleep(boot_up_sleep_time) # sleep to allow device to fully boot
print('starting test')
ppk2_test.start_measuring()
def _average_samples(list, window_size):
"""Average samples based on window size"""
chunks = [list[val:val + window_size] for val in range(0, len(list), window_size)]
avgs = []
for chunk in chunks:
avgs.append(sum(chunk) / len(chunk))
return avgs
def run_test():
setup()
start_time = time.time()
sampling_duration = 7 # set window size in seconds
sampling_period_samples = []
while True:
read_data = ppk2_test.get_data()
if read_data != b'':
samples = _average_samples(ppk2_test.get_samples(read_data), 1000) # optionally average samples
if time.time() - start_time < sampling_duration:
sampling_period_samples += samples # append to list
else:
print(f"Average of sampling period: {sum(sampling_period_samples) / len(sampling_period_samples)}")
sampling_period_samples = [] # clear list
start_time = time.time()
time.sleep(.01) # lower time between sampling -> less samples read in one sampling period
run_test()
However, that outputs two values that look promising and then it seems like it outputs junk data
Found PPK2 at /dev/cu.usbmodemD325C91691932
Sleeping for 45 seconds
starting test
Average of sampling period: 71.21186607958448
Average of sampling period: 51.604008113477775
Average of sampling period: 7117199.324881784
Average of sampling period: 10396420.503219703
Average of sampling period: 10398523.612243453
Average of sampling period: 10396456.484739268
Average of sampling period: 10397220.137242936
Im not sure if I am overloading the PPK2 with serial reads and it locks up?
also, maybe you can help me understand the variables a bit more:
window_size
the number of serial reads we are doing?sampling_duration
the time for which we are sampling for? If #2 is true, do we still need sleeps in between samples?
Thanks in advance
Hi @StanKatsyukCT, sorry for the late response, we've been busy with urgent matters.
window_size
variable is not the number of serial reads we are doing, it is the number of samples we are averaging. So, for a window_size
of 1000
you would get n/1000
average values. The nRF Connect App does exactly this when displaying the graph, you will notice how the graph smooths out as the window_size
increases.sampling_duration
is the period in minutes/seconds/milliseconds/etc.
in which we average all datapoints. This is equivalent to the size of the window in the nRF Connect App, 7 seconds in your example image. So the average
value displayed on your image is the average of 7 seconds of readings. This is what the sampling_duration
variable specifies.Could you log the ppk2_test.get_samples(read_data)
samples directly to a file and include it here?
I was able to get it working, there was an issue in the logic for processing the buffer output of the PPK2. Thanks for all your help @NejcKle and team.
Would love to see both the issue, and the solution =)
I have the same problem. If you remove the sleep to obtain the maximum polling frequency, then zero values appear, which distort the final result.
ppk2_test = PPK2_API("/dev/ttyACM0") # serial port will be different for you
ppk2_test.get_modifiers()
ppk2_test.use_source_meter() # set source meter mode
ppk2_test.set_source_voltage(3300) # set source voltage in mV
ppk2_test.toggle_DUT_power('ON')
ppk2_test.start_measuring() # start measuring
# read measured values in a for loop like this:
start_time = time.time()
while time.time() - start_time <= 1:
read_data = ppk2_test.get_data()
samples = ppk2_test.get_samples(read_data)
ave = sum(samples) / len(samples)
print(len(samples), ave)
ppk2_test.stop_measuring()
result fragment: 1 -1.2596387693469735 1 -1.2596387693469735 1 41.078069146083955 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735 7 41.15273882494715 1 -1.2596387693469735 1 -1.2596387693469735 1 -1.2596387693469735
I was able to get it working, there was an issue in the logic for processing the buffer output of the PPK2. Thanks for all your help @NejcKle and team.
Hi @StanKatsyukCT, I request you to upload your final code, just for a reference.
Thanks in advance.
Hi @StanKatsyukCT .. what was the issue in the logic for processing the buffer output of the PPK2 ? I am having the same issue where on the python script I get a very high value compared to what I get on gui ?
Hello team! Firstly, thanks for spending time on this API, really appreciate all your effort!
I am running into an issue where I can't seem to get the same current readings from the ppk2-api as I do from NRF connect.
My NRF connect setup:![image](https://user-images.githubusercontent.com/55717075/120562561-81dd5980-c3bb-11eb-9fd0-e21b22064cd4.png)
Note the reading of around 52uA (which is expected)
The
example.py
code im running in python is:The output of the code is
As you can see, the uA I am getting is much higher than the readings I get via NRF connect. Am I missing some param I need to adjust to get the readings to be more accurate?
Thank you in advance,