55# import numpy as np
66# import matplotlib.pyplot as plt
77# import matplotlib
8- from ppk2_api .ppk2_api import PPK2_API
8+ from ppk2_api .ppk2_api import PPK2_MP as PPK2_API
99
1010class PowerProfiler ():
1111 def __init__ (self , serial_port = None , source_voltage_mV = 3300 , filename = None ):
@@ -14,28 +14,37 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
1414 self .measurement_thread = None
1515 self .ppk2 = None
1616
17- try :
17+ print (f"Initing power profiler" )
18+
19+ # try:
20+ if serial_port :
21+ self .ppk2 = PPK2_API (serial_port )
22+ else :
23+ serial_port = self .discover_port ()
24+ print (f"Opening serial port: { serial_port } " )
1825 if serial_port :
1926 self .ppk2 = PPK2_API (serial_port )
20- else :
21- serial_port = self .discover_port ()
22- if serial_port :
23- self .ppk2 = PPK2_API (serial_port )
27+
28+ try :
2429 ret = self .ppk2 .get_modifiers () # try to read modifiers, if it fails serial port is probably not correct
30+ print (f"Initialized ppk2 api: { ret } " )
2531 except Exception as e :
32+ print (f"Error initializing power profiler: { e } " )
2633 ret = None
2734 raise e
2835
2936 if not ret :
3037 self .ppk2 = None
31- # raise Exception(f"Error when initing PowerProfiler with serial port {serial_port}")
38+ raise Exception (f"Error when initing PowerProfiler with serial port { serial_port } " )
3239 else :
3340 self .ppk2 .use_source_meter ()
3441
3542 self .source_voltage_mV = source_voltage_mV
3643
3744 self .ppk2 .set_source_voltage (self .source_voltage_mV ) # set to 3.3V
3845
46+ print (f"Set power profiler source voltage: { self .source_voltage_mV } " )
47+
3948 self .measuring = False
4049 self .current_measurements = []
4150
@@ -73,12 +82,19 @@ def delete_power_profiler(self):
7382 self .measuring = False
7483 self .stop = True
7584
85+ print ("Deleting power profiler" )
86+
7687 if self .measurement_thread :
88+ print (f"Joining measurement thread" )
7789 self .measurement_thread .join ()
7890 self .measurement_thread = None
7991
8092 if self .ppk2 :
93+ print (f"Disabling ppk2 power" )
8194 self .disable_power ()
95+ del self .ppk2
96+
97+ print (f"Deleted power profiler" )
8298
8399 def discover_port (self ):
84100 """Discovers ppk2 serial port"""
@@ -111,8 +127,7 @@ def measurement_loop(self):
111127 if self .measuring : # read data if currently measuring
112128 read_data = self .ppk2 .get_data ()
113129 if read_data != b'' :
114- #samples = self.ppk2.get_samples(read_data)
115- samples = self ._average_samples (self .ppk2 .get_samples (read_data ), 1024 ) # optionally average samples
130+ samples = self .ppk2 .get_samples (read_data )
116131 self .current_measurements += samples # can easily sum lists, will append individual data
117132 time .sleep (0.001 ) # TODO figure out correct sleep duration
118133
@@ -129,8 +144,8 @@ def start_measuring(self):
129144 """Start measuring"""
130145 if not self .measuring : # toggle measuring flag only if currently not measuring
131146 self .current_measurements = [] # reset current measurements
132- self .ppk2 .start_measuring () # send command to ppk2
133147 self .measuring = True # set internal flag
148+ self .ppk2 .start_measuring () # send command to ppk2
134149 self .measurement_start_time = time .time ()
135150
136151 def stop_measuring (self ):
@@ -149,6 +164,9 @@ def get_min_current_mA(self):
149164 def get_max_current_mA (self ):
150165 return max (self .current_measurements ) / 1000
151166
167+ def get_num_measurements (self ):
168+ return len (self .current_measurements )
169+
152170 def get_average_current_mA (self ):
153171 """Returns average current of last measurement in mA"""
154172 if len (self .current_measurements ) == 0 :
@@ -159,7 +177,7 @@ def get_average_current_mA(self):
159177
160178 def get_average_power_consumption_mWh (self ):
161179 """Return average power consumption of last measurement in mWh"""
162- average_current_mA = self .get_average_current_mA () # convert microamperes to milliamperes
180+ average_current_mA = self .get_average_current_mA ()
163181 average_power_mW = (self .source_voltage_mV / 1000 ) * average_current_mA # divide by 1000 as source voltage is in millivolts - this gives us milliwatts
164182 measurement_duration_h = self .get_measurement_duration_s () / 3600 # duration in seconds, divide by 3600 to get hours
165183 average_consumption_mWh = average_power_mW * measurement_duration_h
@@ -174,9 +192,4 @@ def get_average_charge_mC(self):
174192 def get_measurement_duration_s (self ):
175193 """Returns duration of measurement"""
176194 measurement_duration_s = (self .measurement_stop_time - self .measurement_start_time ) # measurement duration in seconds
177- return measurement_duration_s
178-
179- # pp = PowerProfiler("/dev/ttyACM1")
180- # pp.start_measuring()
181- # time.sleep(10)
182- # pp.stop_measuring()
195+ return measurement_duration_s
0 commit comments