Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ def __init__(self, name: str, index: int, subindex: int = 0):
#: Factor between physical unit and integer value
self.factor: float = 1
#: Minimum allowed value
self.min: Optional[int] = None
self.min: Optional[Union[int, float]] = None
#: Maximum allowed value
self.max: Optional[int] = None
self.max: Optional[Union[int, float]] = None
#: Default value at start-up
self.default: Optional[int] = None
#: Is the default value relative to the node-ID (only applies to COB-IDs)
Expand Down Expand Up @@ -480,10 +480,10 @@ def encode_raw(self, value: Union[int, float, str, bytes, bytearray]) -> bytes:
if self.data_type in NUMBER_TYPES:
if self.min is not None and value < self.min:
logger.warning(
"Value %d is less than min value %d", value, self.min)
"Value %s is less than min value %s", value, self.min)
if self.max is not None and value > self.max:
logger.warning(
"Value %d is greater than max value %d",
"Value %s is greater than max value %s",
value, self.max)
try:
return self.STRUCT_TYPES[self.data_type].pack(value)
Expand Down
10 changes: 10 additions & 0 deletions canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ def build_variable(
min_string = eds.get(section, "LowLimit")
if var.data_type in datatypes.SIGNED_TYPES:
var.min = _signed_int_from_hex(min_string, _calc_bit_length(var.data_type))
elif var.data_type in datatypes.FLOAT_TYPES:
try:
var.min = int(min_string, 0)
except ValueError:
var.min = float(min_string)
else:
var.min = int(min_string, 0)
except ValueError:
Expand All @@ -353,6 +358,11 @@ def build_variable(
max_string = eds.get(section, "HighLimit")
if var.data_type in datatypes.SIGNED_TYPES:
var.max = _signed_int_from_hex(max_string, _calc_bit_length(var.data_type))
elif var.data_type in datatypes.FLOAT_TYPES:
try:
var.max = int(max_string, 0)
except ValueError:
var.max = float(max_string)
else:
var.max = int(max_string, 0)
except ValueError:
Expand Down
36 changes: 36 additions & 0 deletions test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def test_record(self):

def test_record_with_limits(self):
cases = [
(0x3002, 4.5, None), # _ REAL32 fractional limit
(0x3020, 0, 127), # _ INTEGER8 hex limits
(0x3021, 2, 10), # _ UNSIGNED8 hex limits
(0x3022, 100, 1000), # _ UNSIGNED16 decimal limits
Expand All @@ -152,6 +153,39 @@ def test_record_with_limits(self):
self.assertEqual(var.min, expected_min)
self.assertEqual(var.max, expected_max)

def test_roundtrip_float_limits(self):
for data_type in (canopen.objectdictionary.REAL32, canopen.objectdictionary.REAL64):
for limits, expected_min, expected_max in [
("LowLimit=-1.5\nHighLimit=2.5e1\n", -1.5, 25.0),
("LowLimit=0x10\nHighLimit=0x20\n", 16, 32),
("LowLimit=-2\nHighLimit=10\n", -2, 10),
("LowLimit=-0o10\nHighLimit=0o20\n", -8, 16),
("", None, None),
]:
with self.subTest(data_type=data_type, limits=limits):
data = (
"[DeviceInfo]\n[2000]\nParameterName=Floating point value\n"
f"DataType={data_type}\nAccessType=rw\nDefaultValue=0.5\n{limits}"
)
with io.StringIO(data) as source:
source.name = "mock.eds"
od = canopen.import_od(source)
self.assertEqual(od[0x2000].min, expected_min)
self.assertEqual(od[0x2000].max, expected_max)
self.assertEqual(od[0x2000].default, 0.5)
self.assertIs(type(od[0x2000].min), type(expected_min))
self.assertIs(type(od[0x2000].max), type(expected_max))
for doc_type in "eds", "dcf":
with io.StringIO() as dest:
canopen.export_od(od, dest, doc_type)
dest.name = f"mock.{doc_type}"
dest.seek(0)
restored = canopen.import_od(dest)
self.assertEqual(restored[0x2000].min, expected_min)
self.assertEqual(restored[0x2000].max, expected_max)
self.assertIs(type(restored[0x2000].min), type(expected_min))
self.assertIs(type(restored[0x2000].max), type(expected_max))

def test_signed_int_from_hex(self):
for data_type, test_cases in self.test_data.items():
for test_case in test_cases:
Expand Down Expand Up @@ -179,6 +213,8 @@ def test_build_variable_range_warnings(self):
("2001", "DefaultValue", "SOMETHING"), # BOOLEAN non-numeric
("2003", "DefaultValue", "SOMETHING"), # INTEGER16 non-numeric
("2006", "ParameterValue", ""), # UNSIGNED16 empty
("2008", "LowLimit", "SOMETHING"), # REAL32 non-numeric
("2011", "HighLimit", ""), # REAL64 empty
]
for index, option, value in cases:
with self.subTest(index=index, option=option, value=value):
Expand Down
17 changes: 17 additions & 0 deletions test/test_od.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ def test_real64(self):
self.assertEqual(var.decode_raw(b"\x00\x00\x00\x00\x00\x4a\x93\x40"), 1234.5)
self.assertEqual(var.decode_raw(b"\x06\x81\x95\x43\x0b\x42\x8f\xc0"), -1000.2555)

def test_encode_raw_limit_warning_values(self):
for data_type, minimum, maximum, value, expected in [
(od.REAL64, -1.5, 2.5, -1.75, "Value -1.75 is less than min value -1.5"),
(od.REAL64, -1.5, 2.5, 2.75, "Value 2.75 is greater than max value 2.5"),
(od.INTEGER16, -1, 2, -2, "Value -2 is less than min value -1"),
(od.INTEGER16, -1, 2, 3, "Value 3 is greater than max value 2"),
]:
with self.subTest(data_type=data_type, value=value):
var = od.ODVariable("Limited value", 0x2000)
var.data_type = data_type
var.min = minimum
var.max = maximum
with self.assertLogs("canopen.objectdictionary", level="WARN") as cm:
encoded = var.encode_raw(value)
self.assertEqual(cm.records[0].getMessage(), expected)
self.assertEqual(var.decode_raw(encoded), value)

def test_visible_string(self):
var = od.ODVariable("Test VISIBLE_STRING", 0x1000)
var.data_type = od.VISIBLE_STRING
Expand Down