Skip to content

Commit

Permalink
Add functions to read target setpoints
Browse files Browse the repository at this point in the history
Also provide a mechanism to set actual position and velocity in the
internal data. These data will be requested periodically by the driver.
  • Loading branch information
stefanscherzinger committed Aug 2, 2024
1 parent 05817c3 commit 2d50eda
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions schunk_egu_egk_gripper_dummy/src/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
import json
import string
import struct


class Dummy(object):
Expand All @@ -20,6 +21,8 @@ def __init__(self):
self.diagnostics_byte = 15
self.reserved_status_bits = [10, 15] + list(range(18, 31))
self.reserved_control_bits = [10, 15] + list(range(17, 30))
self.actual_position = "0x0230"
self.actual_speed = "0x0238"

enum_config = os.path.join(
Path(__file__).resolve().parents[1], "config/enum.json"
Expand Down Expand Up @@ -171,6 +174,20 @@ def get_control_bit(self, bit: int) -> int | bool:
byte_index, bit_index = divmod(bit, 8)
return 1 if self.plc_output_buffer[byte_index] & (1 << bit_index) != 0 else 0

def get_target_position(self) -> float:
return struct.unpack("f", self.plc_output_buffer[4:8])[0]

def get_target_speed(self) -> float:
return struct.unpack("f", self.plc_output_buffer[8:12])[0]

def set_actual_position(self, position: float) -> None:
self.data[self.actual_position] = [
bytes(struct.pack("f", position)).hex().upper()
]

def set_actual_speed(self, speed: float) -> None:
self.data[self.actual_speed] = [bytes(struct.pack("f", speed)).hex().upper()]

def process_control_bits(self) -> None:

# Acknowledge
Expand Down
36 changes: 36 additions & 0 deletions schunk_egu_egk_gripper_dummy/tests/test_plc_communication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from src.dummy import Dummy
import struct
import pytest

# [1]: https://stb.cloud.schunk.com/media/IM0046706.PDF

Expand Down Expand Up @@ -94,3 +96,37 @@ def test_dummy_rejects_reading_reserved_control_bits():
for bit in dummy.reserved_control_bits:
assert isinstance(dummy.get_control_bit(bit), bool) # call fails
assert not dummy.get_control_bit(bit)


def test_dummy_supports_reading_target_position():
dummy = Dummy()
target_pos = 0.0123
dummy.plc_output_buffer[4:8] = bytes(struct.pack("f", target_pos))
assert pytest.approx(dummy.get_target_position()) == target_pos


def test_dummy_supports_reading_target_speed():
dummy = Dummy()
target_speed = 55.3
dummy.plc_output_buffer[8:12] = bytes(struct.pack("f", target_speed))
assert pytest.approx(dummy.get_target_speed()) == target_speed


def test_dummy_supports_writing_actual_position():
dummy = Dummy()
inst = "0x0230" # <actual_pos>
pos = 0.123
dummy.set_actual_position(pos)
read_pos = dummy.data[inst][0]
read_pos = struct.unpack("f", bytes.fromhex(read_pos))[0]
assert pytest.approx(read_pos) == pos


def test_dummy_supports_writing_actual_speed():
dummy = Dummy()
inst = "0x0238" # <actual_vel>
speed = 66.5
dummy.set_actual_speed(speed)
read_speed = dummy.data[inst][0]
read_speed = struct.unpack("f", bytes.fromhex(read_speed))[0]
assert pytest.approx(read_speed) == speed

0 comments on commit 2d50eda

Please sign in to comment.