Skip to content

Commit 84e38ff

Browse files
authored
generate_calib_param dictionary added (#134)
* generate_calib_param dictionary added * pytest fix * suggested fix - calib_params nomenclature - better description for docstring * suggest fix - calib param dataclass formation done in eeprom class - removed redundant methods and tests
1 parent 0cd8d59 commit 84e38ff

File tree

7 files changed

+53
-266
lines changed

7 files changed

+53
-266
lines changed

src/edgepi/calibration/edgepi_calibration.py

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@
33
able to generate new calibration parameters using measurements tools.
44
'''
55

6-
from struct import pack
6+
from edgepi.calibration.eeprom_constants import ModuleNames
7+
from edgepi.calibration.calibration_constants import NumOfCh
78

8-
from edgepi.calibration.eeprom_constants import (
9-
ModuleNames,
10-
MemoryAddr
11-
)
12-
from edgepi.calibration.calibration_constants import(
13-
NumOfCh,
14-
CalibParam,
15-
# ReferenceV
16-
)
17-
18-
19-
# TODO: calibration class should only handle the calibration process and separate the data storage
209

2110
class EdgePiCalibration():
2211
'''
@@ -33,122 +22,9 @@ class EdgePiCalibration():
3322
def __init__(self, module: ModuleNames):
3423
self.module = module.value
3524
self.num_of_ch = NumOfCh[module.name].value
36-
self.calib_dict_gen = {ModuleNames.DAC.value : self.__generat_dac_calib_dict}
3725
self.full_scale_range = None
3826
self.full_scale_code = None
3927

40-
# TODO: to be used for prepare a value to be upkoaded to eeprom
41-
# pylint: disable=unused-private-member
42-
def __from_value_to_memory(self, param: float = None):
43-
# TODO: Create range check for calibration param, unittest,change packing into float and
44-
# chekc precision instead of int
45-
param = int(param*10**9)
46-
value = pack("i", param)
47-
return list(value)
48-
49-
def __from_memory_to_value(self, memory: list = None) -> float:
50-
# TODO: Create range check for calibration param, unittest
51-
value = pack("BBBB", memory[0], memory[1], memory[2], memory[3])
52-
value = int.from_bytes(value, "little", signed=True)
53-
return float(format(value*10**-9, '.9f'))
54-
55-
def __generat_dac_calib_dict(self, calib_param: list = None) :
56-
'''
57-
Function to generate DAC calibration parameter dictionary based on the list of imported cal-
58-
ibration parameter from DAC module.
59-
60-
Arg:
61-
calib_param (list): list contains the parameter read from eeprom and passed from each mo
62-
-dule
63-
Return:
64-
calib_dict (dict):
65-
'''
66-
calib_dict={}
67-
for ch in range(self.num_of_ch):
68-
offset = ch * MemoryAddr.CH_OFFSET.value
69-
packed_gain = self.__from_memory_to_value(calib_param[offset:offset+4])
70-
packed_offset = self.__from_memory_to_value(calib_param[offset+4:offset+8])
71-
calib_dict[ch] = CalibParam(gain = packed_gain, offset=packed_offset)
72-
73-
return calib_dict
74-
75-
# TODO: ADC Calib to be added
76-
# def __generat_adc_calib_dict(self, calib_param: list = None) :
77-
# '''
78-
# Function to generate ADC calibration parameter dictionary based on the list of imported
79-
# calibration parameter from ADC module.
80-
81-
# Arg:
82-
# calib_param (list): list contains the parameter read from eeprom and passed from each
83-
# module
84-
# Return:
85-
# calib_dict (dict):
86-
# '''
87-
88-
# return 2
89-
90-
# TODO: RTD Calib to be added
91-
# def __generat_rtd_calib_dict(self, calib_param: list = None) :
92-
# '''
93-
# Function to generate RTD calibration parameter dictionary based on the list of imported
94-
# calibration parameter from RTD module.
95-
96-
# Arg:
97-
# calib_param (list): list contains the parameter read from eeprom and passed from each
98-
# module
99-
# Return:
100-
# calib_dict (dict):
101-
# '''
102-
103-
# return 3
104-
105-
# TODO: TC Calib to be added
106-
# def __generat_tc_calib_dict(self, calib_param: list = None) :
107-
# '''
108-
# Function to generate TC calibration parameter dictionary based on the list of imported
109-
# calibration parameter from TC module.
110-
111-
# Arg:
112-
# calib_param (list): list contains the parameter read from eeprom and passed from each
113-
# module
114-
# Return:
115-
# calib_dict (dict):
116-
# '''
117-
118-
# return 4
119-
120-
def get_calibration_dict(self, calib_param: list = None):
121-
'''
122-
Function to generate calibration parameter dictionary based on the list of imported
123-
calibration parameter from each module.
124-
125-
Arg:
126-
calib_param (list): list contains the parameter read from eeprom and passed from each
127-
module
128-
Return:
129-
calib_dict (dict):
130-
'''
131-
gen_calib_dict = self.calib_dict_gen[self.module]
132-
return gen_calib_dict(calib_param)
133-
134-
def generate_calib_param_dict(self):
135-
'''
136-
Function to generate dictionary of calibration parameters
137-
Args:
138-
N/A
139-
Return:
140-
ch_to_calib_dict (dict): dictionary mapped channel to CalibParam data class
141-
ie) {1 : CalibParam(gain, offset)
142-
.
143-
.
144-
.
145-
nth_channel : CalibParam(gain, offset)}
146-
'''
147-
ch_to_calib_dict = {}
148-
for ch in range(self.num_of_ch):
149-
ch_to_calib_dict[ch] = CalibParam(gain=1, offset=0)
150-
return ch_to_calib_dict
151-
15228
def generate_measurements_dict(self, num_of_points: int = None):
15329
'''
15430
Function to generate dictionary of measurements to generate calibration parameter. It will

src/edgepi/calibration/edgepi_eeprom.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ def get_edgepi_reserved_data(self):
9999
# pylint: disable=no-member
100100
self.eeprom_layout.ParseFromString(self.__read_edgepi_reserved_memory())
101101
eeprom_data = EdgePiEEPROMData()
102-
eeprom_data.dac_calib_parms=eeprom_data.message_to_list(self.eeprom_layout.dac)
103-
eeprom_data.adc_calib_parms=eeprom_data.message_to_list(self.eeprom_layout.adc)
104-
eeprom_data.rtd_calib_parms=eeprom_data.message_to_list(self.eeprom_layout.rtd)
105-
eeprom_data.tc_calib_parms=eeprom_data.message_to_list(self.eeprom_layout.tc)
102+
eeprom_data.dac_calib_parms=eeprom_data.message_to_dict(self.eeprom_layout.dac)
103+
eeprom_data.adc_calib_parms=eeprom_data.message_to_dict(self.eeprom_layout.adc)
104+
eeprom_data.rtd_calib_parms=eeprom_data.message_to_dict(self.eeprom_layout.rtd)
105+
eeprom_data.tc_calib_parms=eeprom_data.message_to_dict(self.eeprom_layout.tc)
106106
eeprom_data.config_key=eeprom_data.keys_to_str(self.eeprom_layout.config_key)
107107
eeprom_data.data_key=eeprom_data.keys_to_str(self.eeprom_layout.data_key)
108108
eeprom_data.serial= self.eeprom_layout.serial_number

src/edgepi/calibration/eeprom_constants.py

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from enum import Enum
44
from dataclasses import dataclass
55
from edgepi.calibration.eeprom_mapping_pb2 import EepromLayout
6+
from edgepi.calibration.calibration_constants import CalibParam
67

78
class EEPROMInfo(Enum):
89
"""
@@ -21,53 +22,6 @@ class ModuleNames(Enum):
2122
RTD = 0x2
2223
TC = 0x3
2324

24-
# TODO: to be deleted once protobuf is implemented
25-
class DACParamAddr(Enum):
26-
"""
27-
EdgePi DAC Calibration Parameter Addresses
28-
Each parameter, gain and offset, are 4 bytes long 0x200~0x23F
29-
Ex) CH0_gain = 0x200~0x203, CH0_offset = 0x204~0x207
30-
"""
31-
CH0 = 0x200
32-
CH1 = 0x208
33-
CH2 = 0x210
34-
CH3 = 0x218
35-
CH4 = 0x220
36-
CH5 = 0x228
37-
CH6 = 0x230
38-
CH7 = 0x238
39-
LEN = 63
40-
# TODO: to be deleted once protobuf is implemented
41-
class ADCParamAddr(Enum):
42-
"""
43-
EdgePi DAC Calibration Parameter Addresses
44-
Each parameter, gain and offset, are 4 bytes long
45-
"""
46-
CH0 = 0x240
47-
CH1 = 0x248
48-
CH2 = 0x240
49-
CH3 = 0x248
50-
CH4 = 0x240
51-
CH5 = 0x248
52-
CH6 = 0x240
53-
CH7 = 0x248
54-
DIFF1 =0x250
55-
DIFF2 =0x258
56-
DIFF3 =0x260
57-
DIFF4 =0x268
58-
# TODO: to be deleted once protobuf is implemented
59-
class MemoryAddr(Enum):
60-
"""
61-
Memory offset values
62-
"""
63-
START = 0x000
64-
DAC = 0x200
65-
ADC = 0x240
66-
TC = 0x280
67-
RTD = 0x2C0
68-
END = 0x3FF
69-
CH_OFFSET = 0x8
70-
7125
class EdgePiMemoryInfo(Enum):
7226
"""
7327
Information regarding Edgepi reserved memory space
@@ -123,19 +77,19 @@ class EdgePiEEPROMData:
12377
model: str = None
12478
client_id: str = None
12579

126-
def message_to_list(self, data_to_unpack: EepromLayout = None):
80+
def message_to_dict(self, data_to_unpack: EepromLayout = None):
12781
"""
12882
Function to unpack message to list
12983
Args:
13084
data_to_unpack: EepromLayout message modules
13185
Returns:
13286
calib_list: 1-D array
13387
"""
134-
calib_list=[]
135-
for ch in data_to_unpack.calibs:
136-
calib_list.append(ch.gain)
137-
calib_list.append(ch.offset)
138-
return calib_list
88+
calib_dict={}
89+
for indx, ch in enumerate(data_to_unpack.calibs):
90+
calib_dict[indx] = CalibParam(gain=ch.gain,
91+
offset=ch.offset)
92+
return calib_dict
13993

14094
def keys_to_str(self, data_to_unpack: EepromLayout = None):
14195
"""

src/edgepi/dac/edgepi_dac.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
from edgepi.gpio.edgepi_gpio import EdgePiGPIO
2222
from edgepi.gpio.gpio_configs import GpioConfigs
2323
from edgepi.calibration.edgepi_eeprom import EdgePiEEPROM
24-
from edgepi.calibration.eeprom_constants import MemoryAddr,DACParamAddr, ModuleNames
25-
from edgepi.calibration.edgepi_calibration import EdgePiCalibration
26-
27-
28-
2924

3025
class EdgePiDAC(spi):
3126
"""A EdgePi DAC device"""
@@ -59,12 +54,12 @@ def __init__(self):
5954
self.log.info("Initializing DAC Bus")
6055
super().__init__(bus_num=6, dev_id=3, mode=1, max_speed=1000000)
6156

57+
# Read edgepi reserved data and generate calibration parameter dictionary
6258
eeprom = EdgePiEEPROM()
63-
calib = EdgePiCalibration(ModuleNames.DAC)
64-
calib_param = eeprom.sequential_read(MemoryAddr.DAC.value, DACParamAddr.LEN.value)
65-
calib_dict = calib.get_calibration_dict(calib_param)
59+
eeprom_data = eeprom.get_edgepi_reserved_data()
60+
dac_calib_params = eeprom_data.dac_calib_parms
6661

67-
self.dac_ops = DACCommands(calib_dict)
62+
self.dac_ops = DACCommands(dac_calib_params)
6863
self.gpio = EdgePiGPIO(GpioConfigs.DAC.value)
6964

7065
self.__dac_power_state = {

src/test_edgepi/unit_tests/test_calibration/test_edgepi_calibration.py

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
# pylint: disable=protected-access
55

66
from unittest import mock
7+
import os
8+
PATH = os.path.dirname(os.path.abspath(__file__))
79
import sys
810
sys.modules['periphery'] = mock.MagicMock()
911

1012
import pytest
1113
from edgepi.calibration.edgepi_calibration import EdgePiCalibration
12-
from edgepi.calibration.calibration_constants import CalibParam
1314
from edgepi.calibration.eeprom_constants import ModuleNames
1415

16+
def read_binfile():
17+
"""Read the dummy serializedFile and return byte string"""
18+
with open(PATH+"/serializedFile","rb") as fd:
19+
b_string = fd.read()
20+
return b_string
21+
1522
# @pytest.fixture(name="calib")
1623
# def fixture_test_dac():
1724
# yield EdgePiCalibration()
@@ -25,19 +32,6 @@ def test_init_class(module_name, result):
2532
assert edge_calib.module == result[0]
2633
assert edge_calib.num_of_ch == result[1]
2734

28-
@pytest.mark.parametrize("module_name, result", [(ModuleNames.DAC, [1, 0]),
29-
(ModuleNames.ADC, [1, 0]),
30-
(ModuleNames.RTD, [1, 0]),
31-
(ModuleNames.TC, [1, 0])])
32-
def test_generate_calib_param_dict(module_name, result):
33-
edge_calib = EdgePiCalibration(module_name)
34-
calib_dict = edge_calib.generate_calib_param_dict()
35-
for _, value in calib_dict.items():
36-
assert value.gain == result[0]
37-
assert value.offset == result[1]
38-
dict_length = len(calib_dict.values())
39-
assert dict_length == edge_calib.num_of_ch
40-
4135
@pytest.mark.parametrize("module_name, num_of_points, result", [(ModuleNames.DAC, 10, 0),
4236
(ModuleNames.ADC, 5, 0),
4337
(ModuleNames.RTD, 2, 0),
@@ -103,41 +97,3 @@ def test_record_measurements(module_name, num_of_points, values_to_record):
10397
assert value_dict['input_unit'] != 0
10498
assert value_dict['expected_out'] != 0
10599
assert value_dict['actual_out'] != 0
106-
107-
@pytest.mark.parametrize("module_name, memory, result",
108-
[(ModuleNames.DAC,[105,60,232,254], -0.018334615)])
109-
def test_from_memory_to_value(module_name, memory, result):
110-
edge_calib = EdgePiCalibration(module_name)
111-
assert edge_calib._EdgePiCalibration__from_memory_to_value(memory) == result
112-
113-
@pytest.mark.parametrize("module_name, value, result",
114-
[(ModuleNames.DAC, -0.018334615,[105,60,232,254]),
115-
(ModuleNames.DAC, -0.018334615234,[105,60,232,254]),
116-
(ModuleNames.DAC, -0.018334615999,[105,60,232,254]),
117-
(ModuleNames.DAC, 0.0,[0,0,0,0])])
118-
def test_from_value_to_memory(module_name, value, result):
119-
edge_calib = EdgePiCalibration(module_name)
120-
assert edge_calib._EdgePiCalibration__from_value_to_memory(value) == result
121-
122-
@pytest.mark.parametrize("module_name, calib_param, result",
123-
[(ModuleNames.DAC,
124-
[0,225,245,5,0,225,245,5,
125-
0,194,235,11,0,194,235,11,
126-
0,163,225,17,0,163,225,17,
127-
0,132,215,23,0,132,215,23,
128-
0,101,205,29,0,101,205,29,
129-
0,70,195,35,0,70,195,35,
130-
0,39,185,41,0,39,185,41,
131-
0,8,175,47,0,8,175,47],
132-
{0:CalibParam(gain=0.1,offset=0.1),
133-
1:CalibParam(gain=0.2,offset=0.2),
134-
2:CalibParam(gain=0.3,offset=0.3),
135-
3:CalibParam(gain=0.4,offset=0.4),
136-
4:CalibParam(gain=0.5,offset=0.5),
137-
5:CalibParam(gain=0.6,offset=0.6),
138-
6:CalibParam(gain=0.7,offset=0.7),
139-
7:CalibParam(gain=0.8,offset=0.8)})])
140-
def test_get_calibration_dict(module_name, calib_param, result):
141-
edge_calib = EdgePiCalibration(module_name)
142-
calib_dict = edge_calib.get_calibration_dict(calib_param)
143-
assert calib_dict == result

src/test_edgepi/unit_tests/test_calibration/test_eeprom_constants.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ def test_edgepi_eeprom_data():
2020
memory_map = EepromLayout()
2121
memory_map.ParseFromString(read_binfile())
2222
eeprom_data = EdgePiEEPROMData()
23-
eeprom_data.dac_calib_parms=eeprom_data.message_to_list(memory_map.dac)
24-
eeprom_data.adc_calib_parms=eeprom_data.message_to_list(memory_map.adc)
25-
eeprom_data.rtd_calib_parms=eeprom_data.message_to_list(memory_map.rtd)
26-
eeprom_data.tc_calib_parms=eeprom_data.message_to_list(memory_map.tc)
23+
eeprom_data.dac_calib_parms=eeprom_data.message_to_dict(memory_map.dac)
24+
eeprom_data.adc_calib_parms=eeprom_data.message_to_dict(memory_map.adc)
25+
eeprom_data.rtd_calib_parms=eeprom_data.message_to_dict(memory_map.rtd)
26+
eeprom_data.tc_calib_parms=eeprom_data.message_to_dict(memory_map.tc)
2727
eeprom_data.config_key=eeprom_data.keys_to_str(memory_map.config_key)
2828
eeprom_data.data_key=eeprom_data.keys_to_str(memory_map.data_key)
2929
eeprom_data.serial=memory_map.serial_number
3030
eeprom_data.model=memory_map.model
3131
eeprom_data.client_id=memory_map.client_id
32-
assert eeprom_data.dac_calib_parms == eeprom_data.message_to_list(memory_map.dac)
33-
assert eeprom_data.adc_calib_parms==eeprom_data.message_to_list(memory_map.adc)
34-
assert eeprom_data.rtd_calib_parms==eeprom_data.message_to_list(memory_map.rtd)
35-
assert eeprom_data.tc_calib_parms==eeprom_data.message_to_list(memory_map.tc)
32+
assert eeprom_data.dac_calib_parms == eeprom_data.message_to_dict(memory_map.dac)
33+
assert eeprom_data.adc_calib_parms==eeprom_data.message_to_dict(memory_map.adc)
34+
assert eeprom_data.rtd_calib_parms==eeprom_data.message_to_dict(memory_map.rtd)
35+
assert eeprom_data.tc_calib_parms==eeprom_data.message_to_dict(memory_map.tc)
3636
assert eeprom_data.config_key.certificate == memory_map.config_key.certificate
3737
assert eeprom_data.config_key.private == memory_map.config_key.private_key
3838
assert eeprom_data.data_key.certificate == memory_map.data_key.certificate

0 commit comments

Comments
 (0)