Skip to content

Driver API

Bases: ABC

Minimal interface implemented by vendor-specific drivers.

Source code in dexterous_hand/driver.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class HandDriver(ABC):
    """Minimal interface implemented by vendor-specific drivers."""

    @abstractmethod
    def connect(self) -> None:
        """Open communication with the hand."""

    @abstractmethod
    def disconnect(self) -> None:
        """Close communication with the hand."""

    @abstractmethod
    def read_joints(self) -> list[JointState]:
        """Return the current joint states."""

    @abstractmethod
    def command_positions(self, positions_rad: dict[str, float]) -> None:
        """Command target joint positions in radians."""

    @abstractmethod
    def stop(self) -> None:
        """Stop motion immediately where the hardware supports it."""

command_positions(positions_rad) abstractmethod

Command target joint positions in radians.

Source code in dexterous_hand/driver.py
23
24
25
@abstractmethod
def command_positions(self, positions_rad: dict[str, float]) -> None:
    """Command target joint positions in radians."""

connect() abstractmethod

Open communication with the hand.

Source code in dexterous_hand/driver.py
11
12
13
@abstractmethod
def connect(self) -> None:
    """Open communication with the hand."""

disconnect() abstractmethod

Close communication with the hand.

Source code in dexterous_hand/driver.py
15
16
17
@abstractmethod
def disconnect(self) -> None:
    """Close communication with the hand."""

read_joints() abstractmethod

Return the current joint states.

Source code in dexterous_hand/driver.py
19
20
21
@abstractmethod
def read_joints(self) -> list[JointState]:
    """Return the current joint states."""

stop() abstractmethod

Stop motion immediately where the hardware supports it.

Source code in dexterous_hand/driver.py
27
28
29
@abstractmethod
def stop(self) -> None:
    """Stop motion immediately where the hardware supports it."""

Bases: HandDriver

In-memory driver for tests, demos, and documentation examples.

Source code in dexterous_hand/driver.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class MockHandDriver(HandDriver):
    """In-memory driver for tests, demos, and documentation examples."""

    def __init__(self, joint_names: list[str]) -> None:
        self._connected = False
        self._positions = {name: 0.0 for name in joint_names}

    def connect(self) -> None:
        self._connected = True

    def disconnect(self) -> None:
        self._connected = False

    def read_joints(self) -> list[JointState]:
        self._require_connected()
        return [JointState(name=name, position_rad=value) for name, value in self._positions.items()]

    def command_positions(self, positions_rad: dict[str, float]) -> None:
        self._require_connected()
        unknown = set(positions_rad) - set(self._positions)
        if unknown:
            raise KeyError(f"Unknown joints: {sorted(unknown)}")
        self._positions.update(positions_rad)

    def stop(self) -> None:
        self._require_connected()

    def _require_connected(self) -> None:
        if not self._connected:
            raise RuntimeError("Driver is not connected")