Skip to content

Commit

Permalink
Add methods to read and write errors and diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanscherzinger committed Jul 31, 2024
1 parent d2bb958 commit 6f9f89f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion schunk_egu_egk_gripper_dummy/config/data.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"0x0040": [
"800000800000A25800000000000000EF"
"800000800000A25800000000D90000EF"
],
"0x0048": [
"05000000000000000000000000000000"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions schunk_egu_egk_gripper_dummy/src/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def __init__(self):
self.data = None
self.plc_input = "0x0040"
self.plc_output = "0x0048"
self.error_byte = 12
self.diagnostics_byte = 15
self.reserved_status_bits = [10, 15] + list(range(18, 31))

enum_config = os.path.join(
Expand Down Expand Up @@ -122,3 +124,25 @@ def get_status_bit(self, bit: int) -> int | bool:
return False
byte_index, bit_index = divmod(bit, 8)
return 1 if self.plc_input_buffer[byte_index] & (1 << bit_index) != 0 else 0

def set_status_error(self, error: str) -> bool:
try:
self.plc_input_buffer[self.error_byte] = int(error, 16)
return True
except ValueError:
return False

def get_status_error(self) -> str:
return hex(self.plc_input_buffer[self.error_byte]).replace("0x", "").upper()

def set_status_diagnostics(self, diagnostics: str) -> bool:
try:
self.plc_input_buffer[self.diagnostics_byte] = int(diagnostics, 16)
return True
except ValueError:
return False

def get_status_diagnostics(self) -> str:
return (
hex(self.plc_input_buffer[self.diagnostics_byte]).replace("0x", "").upper()
)
39 changes: 34 additions & 5 deletions schunk_egu_egk_gripper_dummy/tests/test_plc_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,46 @@ def test_dummy_only_touches_specified_bits():
assert dummy.get_plc_input() == before


def test_dummy_supports_reading_and_writing_status_error():
dummy = Dummy()
error_codes = ["AA", "bb", "0xcc"]
expected = ["AA", "BB", "CC"]
for error, expected in zip(error_codes, expected):
dummy.set_status_error(error)
assert dummy.get_status_error() == expected


def test_dummy_rejects_writing_invalid_status_error():
dummy = Dummy()
invalid_codes = ["zz", "-1", "aaa"]
for error in invalid_codes:
assert not dummy.set_status_error(error)


def test_dummy_supports_reading_and_writing_status_diagnostics():
dummy = Dummy()
diagnostics_code = "EF"
dummy.set_status_diagnostics(diagnostics_code)
assert dummy.get_status_diagnostics() == diagnostics_code


def test_dummy_rejects_writing_invalid_status_diagnostics():
dummy = Dummy()
invalid_codes = ["zz", "-1", "aaa"]
for code in invalid_codes:
assert not dummy.set_status_diagnostics(code)


# See p. 24 in
# Booting and establishing operational readiness [1]


@pytest.mark.skip()
def test_dummy_starts_in_error_state():
dummy = Dummy()
query = {"inst": dummy.plc_input, "count": 1}
data = dummy.get_data(query)[0]
assert data[0:2] == "80"
assert data[30:] == "D9" # ERR_FAST_STOP
assert dummy.get_status_bit(0) == 0 # not ready for operation
assert dummy.get_status_bit(7) == 1 # there's an error
assert dummy.get_status_error() == "D9" # ERR_FAST_STOP
assert dummy.get_status_diagnostics() == "EF" # ERR_COMM_LOST


@pytest.mark.skip()
Expand Down

0 comments on commit 6f9f89f

Please sign in to comment.