Skip to content

Commit

Permalink
Add function for toggling status bits
Browse files Browse the repository at this point in the history
We'll need this for toggling the `command received` bit for most
plc commands.
  • Loading branch information
stefanscherzinger committed Aug 2, 2024
1 parent 2d50eda commit b8f34e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions schunk_egu_egk_gripper_dummy/src/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def __init__(self):
self.plc_output = "0x0048"
self.error_byte = 12
self.diagnostics_byte = 15
self.valid_status_bits = list(range(0, 10)) + [11, 12, 13, 14, 16, 17, 31]
self.valid_control_bits = list(range(0, 10)) + [11, 12, 13, 14, 16, 30, 31]
self.reserved_status_bits = [10, 15] + list(range(18, 31))
self.reserved_control_bits = [10, 15] + list(range(17, 30))
self.actual_position = "0x0230"
Expand Down Expand Up @@ -144,6 +146,15 @@ def get_status_bit(self, bit: int) -> int | bool:
byte_index, bit_index = divmod(bit, 8)
return 1 if self.plc_input_buffer[byte_index] & (1 << bit_index) != 0 else 0

def toggle_status_bit(self, bit: int) -> bool:
if bit < 0 or bit > 31:
return False
if bit in self.reserved_status_bits:
return False
byte_index, bit_index = divmod(bit, 8)
self.plc_input_buffer[byte_index] ^= 1 << bit_index
return True

def set_status_error(self, error: str) -> bool:
try:
self.plc_input_buffer[self.error_byte] = int(error, 16)
Expand Down
26 changes: 20 additions & 6 deletions schunk_egu_egk_gripper_dummy/tests/test_plc_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def test_dummy_rejects_reading_reserved_status_bits():

def test_dummy_supports_reading_and_writing_bits_in_plc_status():
dummy = Dummy()
valid_bits = list(range(0, 10)) + [11, 12, 13, 14, 16, 17, 31]
for bit in valid_bits:
for bit in dummy.valid_status_bits:
dummy.set_status_bit(bit=bit, value=True)
result = dummy.get_status_bit(bit=bit)
assert isinstance(result, int) # successful calls get the bit's value
Expand All @@ -44,8 +43,7 @@ def test_dummy_supports_reading_and_writing_bits_in_plc_status():
def test_dummy_only_touches_specified_status_bits():
dummy = Dummy()
before = dummy.get_plc_input()
valid_bits = list(range(0, 10)) + [11, 12, 13, 14, 16, 17, 31]
for bit in valid_bits:
for bit in dummy.valid_status_bits:
initial_value = dummy.get_status_bit(bit=bit)
dummy.set_status_bit(bit=bit, value=True)
dummy.set_status_bit(bit=bit, value=initial_value)
Expand Down Expand Up @@ -85,8 +83,7 @@ def test_dummy_rejects_writing_invalid_status_diagnostics():

def test_dummy_supports_reading_bits_in_plc_control():
dummy = Dummy()
valid_bits = list(range(0, 10)) + [11, 12, 13, 14, 16, 30, 31]
for bit in valid_bits:
for bit in dummy.valid_control_bits:
result = dummy.get_control_bit(bit=bit)
assert isinstance(result, int) # successful calls get the bit's value

Expand All @@ -98,6 +95,23 @@ def test_dummy_rejects_reading_reserved_control_bits():
assert not dummy.get_control_bit(bit)


def test_dummy_supports_toggling_status_bits():
dummy = Dummy()
for bit in dummy.valid_status_bits:
before = dummy.get_status_bit(bit)
dummy.toggle_status_bit(bit=bit)
after = dummy.get_status_bit(bit)
assert after != before
dummy.toggle_status_bit(bit=bit)
assert dummy.get_status_bit(bit=bit) == before


def test_dummy_rejects_toggling_reserved_status_bits():
dummy = Dummy()
for bit in dummy.reserved_status_bits:
assert not dummy.toggle_status_bit(bit)


def test_dummy_supports_reading_target_position():
dummy = Dummy()
target_pos = 0.0123
Expand Down

0 comments on commit b8f34e8

Please sign in to comment.