dobotkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dobotkit/__init__.py +112 -0
- dobotkit/_version.py +1 -0
- dobotkit/arm/__init__.py +46 -0
- dobotkit/arm/commands/__init__.py +21 -0
- dobotkit/arm/commands/_base.py +19 -0
- dobotkit/arm/commands/connection.py +32 -0
- dobotkit/arm/commands/effector_io.py +114 -0
- dobotkit/arm/commands/motion.py +60 -0
- dobotkit/arm/commands/sensors.py +38 -0
- dobotkit/arm/groups.py +162 -0
- dobotkit/arm/magicianlite.py +203 -0
- dobotkit/enums.py +69 -0
- dobotkit/exceptions.py +36 -0
- dobotkit/go/__init__.py +56 -0
- dobotkit/go/geometry.py +105 -0
- dobotkit/go/groups.py +164 -0
- dobotkit/go/magiciango.py +511 -0
- dobotkit/go/navigation.py +364 -0
- dobotkit/link.py +209 -0
- dobotkit/py.typed +0 -0
- dobotkit-0.1.0.dist-info/METADATA +322 -0
- dobotkit-0.1.0.dist-info/RECORD +24 -0
- dobotkit-0.1.0.dist-info/WHEEL +4 -0
- dobotkit-0.1.0.dist-info/licenses/LICENSE +21 -0
dobotkit/__init__.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""dobotkit -- complete pure-Python control for Dobot Magician Lite and Magician GO.
|
|
2
|
+
|
|
3
|
+
This module is the single public front door to the library. What it exposes:
|
|
4
|
+
|
|
5
|
+
* **Eager, light-weight symbols** -- the full exception hierarchy
|
|
6
|
+
(:mod:`dobotkit.exceptions`), every enum (:mod:`dobotkit.enums`), and
|
|
7
|
+
:data:`__version__`. These are pure-Python and pull in **no** device
|
|
8
|
+
dependencies, so ``import dobotkit`` stays cheap.
|
|
9
|
+
|
|
10
|
+
* **Lazy device classes** -- :class:`~dobotkit.arm.magicianlite.MagicianLite`
|
|
11
|
+
(DobotLink-mediated arm) and :class:`~dobotkit.go.magiciango.MagicianGO`
|
|
12
|
+
(DobotLink WebSocket car) are resolved through a module-level
|
|
13
|
+
:func:`__getattr__` (PEP 562). They are imported only on first *access*
|
|
14
|
+
(``dobotkit.MagicianLite``), so merely importing the package never drags in
|
|
15
|
+
``websockets``. A program that drives only the arm never pays for the GO's
|
|
16
|
+
navigation stack, and vice-versa.
|
|
17
|
+
|
|
18
|
+
The set of eager names re-exported here is kept in lock-step with the
|
|
19
|
+
``enums`` / ``exceptions`` modules by ``tests/test_integration.py``, which fails
|
|
20
|
+
if either module grows a public symbol that is not listed in :data:`__all__`.
|
|
21
|
+
|
|
22
|
+
Example::
|
|
23
|
+
|
|
24
|
+
import dobotkit # light: no websockets yet
|
|
25
|
+
|
|
26
|
+
with dobotkit.MagicianLite(port="COM3") as arm: # now DobotLink is contacted
|
|
27
|
+
arm.home()
|
|
28
|
+
arm.move_to(220, 0, 40, 0, wait=True)
|
|
29
|
+
"""
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
from typing import TYPE_CHECKING, Any
|
|
33
|
+
|
|
34
|
+
from ._version import __version__
|
|
35
|
+
from .enums import (
|
|
36
|
+
ColorPort,
|
|
37
|
+
ContinuousPathMode,
|
|
38
|
+
EndEffectorType,
|
|
39
|
+
GPIOType,
|
|
40
|
+
JOGMode,
|
|
41
|
+
LEDChannel,
|
|
42
|
+
PTPMode,
|
|
43
|
+
)
|
|
44
|
+
from .exceptions import (
|
|
45
|
+
DobotAlarmError,
|
|
46
|
+
DobotConnectionError,
|
|
47
|
+
DobotError,
|
|
48
|
+
DobotLinkError,
|
|
49
|
+
DobotProtocolError,
|
|
50
|
+
DobotTimeoutError,
|
|
51
|
+
DobotValueError,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if TYPE_CHECKING: # pragma: no cover - import only for type checkers, not at runtime
|
|
55
|
+
from .arm.magicianlite import MagicianLite
|
|
56
|
+
from .go.magiciango import MagicianGO
|
|
57
|
+
|
|
58
|
+
# Lazily-imported device classes -> (submodule, attribute). Kept in a table so
|
|
59
|
+
# __getattr__ and __dir__ agree and adding a device is a one-liner. Importing
|
|
60
|
+
# their modules is deferred to first attribute access (see __getattr__) so a
|
|
61
|
+
# bare ``import dobotkit`` never pulls in ``websockets``.
|
|
62
|
+
_LAZY = {
|
|
63
|
+
"MagicianLite": ("dobotkit.arm.magicianlite", "MagicianLite"),
|
|
64
|
+
"MagicianGO": ("dobotkit.go.magiciango", "MagicianGO"),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
__all__ = [
|
|
68
|
+
"__version__",
|
|
69
|
+
# enums (dobotkit.enums)
|
|
70
|
+
"ColorPort",
|
|
71
|
+
"ContinuousPathMode",
|
|
72
|
+
"EndEffectorType",
|
|
73
|
+
"GPIOType",
|
|
74
|
+
"JOGMode",
|
|
75
|
+
"LEDChannel",
|
|
76
|
+
"PTPMode",
|
|
77
|
+
# exceptions (dobotkit.exceptions)
|
|
78
|
+
"DobotAlarmError",
|
|
79
|
+
"DobotConnectionError",
|
|
80
|
+
"DobotError",
|
|
81
|
+
"DobotLinkError",
|
|
82
|
+
"DobotProtocolError",
|
|
83
|
+
"DobotTimeoutError",
|
|
84
|
+
"DobotValueError",
|
|
85
|
+
# lazy device classes
|
|
86
|
+
"MagicianLite",
|
|
87
|
+
"MagicianGO",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def __getattr__(name: str) -> Any:
|
|
92
|
+
"""Lazily resolve the device classes on first access (PEP 562).
|
|
93
|
+
|
|
94
|
+
Importing the device's module here -- rather than at the top of this file --
|
|
95
|
+
is what keeps ``import dobotkit`` from eagerly importing ``websockets``.
|
|
96
|
+
Any other unknown attribute raises ``AttributeError`` as usual.
|
|
97
|
+
"""
|
|
98
|
+
target = _LAZY.get(name)
|
|
99
|
+
if target is not None:
|
|
100
|
+
import importlib
|
|
101
|
+
|
|
102
|
+
module_name, attr = target
|
|
103
|
+
value = getattr(importlib.import_module(module_name), attr)
|
|
104
|
+
# Cache on the package so repeat access skips the import machinery.
|
|
105
|
+
globals()[name] = value
|
|
106
|
+
return value
|
|
107
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def __dir__() -> "list[str]":
|
|
111
|
+
"""Include the lazy device names in ``dir(dobotkit)`` / tab-completion."""
|
|
112
|
+
return sorted(set(globals()) | set(__all__))
|
dobotkit/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
dobotkit/arm/__init__.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""dobotkit.arm -- Magician Lite (4-axis arm) control over DobotLink.
|
|
2
|
+
|
|
3
|
+
This subpackage wraps the DobotLink ``Magician.*`` RPC surface
|
|
4
|
+
(:mod:`dobotkit.arm.commands`) and layers an ergonomic, safety-first API on
|
|
5
|
+
top of it. The exported name is:
|
|
6
|
+
|
|
7
|
+
* :class:`~dobotkit.arm.magicianlite.MagicianLite` -- the high-level,
|
|
8
|
+
Pythonic API (context manager, ``move_to``/``home``/``pick_and_place``,
|
|
9
|
+
``.io``/``.sensors``/``.effector`` groups).
|
|
10
|
+
|
|
11
|
+
Names are resolved through a module-level :func:`__getattr__` (PEP 562) so
|
|
12
|
+
that merely importing :mod:`dobotkit.arm` does not eagerly pull in
|
|
13
|
+
``websockets``; that dependency is imported only when the class is first
|
|
14
|
+
accessed.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from typing import TYPE_CHECKING, Any
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING: # pragma: no cover - for type checkers only, not imported at runtime
|
|
21
|
+
from .magicianlite import MagicianLite
|
|
22
|
+
|
|
23
|
+
# Public name -> (submodule, attribute). Deferred to first access so importing
|
|
24
|
+
# this subpackage never eagerly imports ``websockets``.
|
|
25
|
+
_LAZY = {
|
|
26
|
+
"MagicianLite": ("dobotkit.arm.magicianlite", "MagicianLite"),
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
__all__ = ["MagicianLite"]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def __getattr__(name: str) -> Any:
|
|
33
|
+
"""Lazily resolve the arm classes on first access (PEP 562)."""
|
|
34
|
+
target = _LAZY.get(name)
|
|
35
|
+
if target is not None:
|
|
36
|
+
import importlib
|
|
37
|
+
|
|
38
|
+
module_name, attr = target
|
|
39
|
+
value = getattr(importlib.import_module(module_name), attr)
|
|
40
|
+
globals()[name] = value # cache so repeat access skips the import machinery
|
|
41
|
+
return value
|
|
42
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def __dir__() -> "list[str]":
|
|
46
|
+
return sorted(set(globals()) | set(__all__))
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Thin dobotlink.Magician.* RPC wrappers for the Magician Lite arm.
|
|
2
|
+
|
|
3
|
+
:class:`ArmCommands` assembles the arm's RPC surface from category mixins so
|
|
4
|
+
independent command groups (connection, motion, effector/IO, sensors) can be
|
|
5
|
+
filled in in parallel. Each mixin inherits :class:`~._base._Base`, which owns
|
|
6
|
+
the DobotLink client, the port name, and the shared ``_call`` /
|
|
7
|
+
``_queued_index`` primitives; the MRO resolves ``_Base`` once.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from .connection import ConnectionMixin
|
|
12
|
+
from .motion import MotionMixin
|
|
13
|
+
from .effector_io import EffectorIoMixin
|
|
14
|
+
from .sensors import SensorMixin
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ArmCommands(ConnectionMixin, MotionMixin, EffectorIoMixin, SensorMixin):
|
|
18
|
+
"""1:1 wrappers over the arm's DobotLink RPC surface."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
__all__ = ["ArmCommands"]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Shared base for the arm's DobotLink RPC command mixins."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
from dobotkit.link import DobotLinkClient
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _Base:
|
|
8
|
+
"""Connection state and the shared ``_call`` / ``_queued_index`` primitives."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, client: DobotLinkClient, port_name: str) -> None:
|
|
11
|
+
self._client = client
|
|
12
|
+
self.port_name = port_name
|
|
13
|
+
|
|
14
|
+
def _call(self, func: str, *, call_timeout: Optional[float] = None, **params: Any) -> Any:
|
|
15
|
+
params["portName"] = self.port_name
|
|
16
|
+
return self._client.call(f"Magician.{func}", call_timeout=call_timeout, **params)
|
|
17
|
+
|
|
18
|
+
def _queued_index(self, resp: Any) -> int:
|
|
19
|
+
return int(resp.get("queuedCmdIndex", 0)) if isinstance(resp, dict) else int(resp or 0)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Connection and on-device command-queue RPC wrappers."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Any, List, cast
|
|
4
|
+
from dobotkit.arm.commands._base import _Base
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ConnectionMixin(_Base):
|
|
8
|
+
"""Connect/disconnect the arm and control its queued-command queue."""
|
|
9
|
+
|
|
10
|
+
# -- connection --
|
|
11
|
+
def search(self) -> List[Any]:
|
|
12
|
+
return cast(List[Any], self._client.call("Magician.SearchDobot"))
|
|
13
|
+
|
|
14
|
+
def connect(self) -> Any:
|
|
15
|
+
return self._call("ConnectDobot")
|
|
16
|
+
|
|
17
|
+
def disconnect(self) -> Any:
|
|
18
|
+
return self._call("DisconnectDobot")
|
|
19
|
+
|
|
20
|
+
# -- queue --
|
|
21
|
+
def queue_clear(self) -> Any:
|
|
22
|
+
return self._call("QueuedCmdClear")
|
|
23
|
+
|
|
24
|
+
def queue_start(self) -> Any:
|
|
25
|
+
return self._call("QueuedCmdStart")
|
|
26
|
+
|
|
27
|
+
def queue_stop(self) -> Any:
|
|
28
|
+
return self._call("QueuedCmdStop")
|
|
29
|
+
|
|
30
|
+
def current_index(self) -> int:
|
|
31
|
+
resp = self._call("GetQueuedCmdCurrentIndex")
|
|
32
|
+
return self._queued_index(resp)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""End-effector and digital/analog I/O RPC wrappers. Filled in later."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Any
|
|
4
|
+
from dobotkit.arm.commands._base import _Base
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class EffectorIoMixin(_Base):
|
|
8
|
+
"""RPC wrappers for end-effector control and digital/analog I/O."""
|
|
9
|
+
|
|
10
|
+
# -- end effectors --
|
|
11
|
+
def set_suction_cup(self, enable_ctrl: bool, on: bool, queued: bool = True) -> int:
|
|
12
|
+
"""Set suction cup state.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
enable_ctrl: Enable/disable control.
|
|
16
|
+
on: Turn on/off.
|
|
17
|
+
queued: Queue the command (default True).
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
Queued command index.
|
|
21
|
+
"""
|
|
22
|
+
return self._queued_index(self._call(
|
|
23
|
+
"SetEndEffectorSuctionCup", enableCtrl=bool(enable_ctrl), on=bool(on), isQueued=queued))
|
|
24
|
+
|
|
25
|
+
def set_gripper(self, enable_ctrl: bool, on: bool, queued: bool = True) -> int:
|
|
26
|
+
"""Set gripper state.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
enable_ctrl: Enable/disable control.
|
|
30
|
+
on: Open/close gripper.
|
|
31
|
+
queued: Queue the command (default True).
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
Queued command index.
|
|
35
|
+
"""
|
|
36
|
+
return self._queued_index(self._call(
|
|
37
|
+
"SetEndEffectorGripper", enableCtrl=bool(enable_ctrl), on=bool(on), isQueued=queued))
|
|
38
|
+
|
|
39
|
+
def set_servo_angle(self, index: int, angle: float, queued: bool = True) -> int:
|
|
40
|
+
"""Set servo angle.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
index: Servo index.
|
|
44
|
+
angle: Angle in degrees.
|
|
45
|
+
queued: Queue the command (default True).
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
Queued command index.
|
|
49
|
+
"""
|
|
50
|
+
return self._queued_index(self._call(
|
|
51
|
+
"SetServoAngle", index=int(index), value=float(angle), isQueued=queued))
|
|
52
|
+
|
|
53
|
+
# -- IO --
|
|
54
|
+
def set_io_multiplexing(self, address: int, multiplex: int) -> Any:
|
|
55
|
+
"""Set I/O multiplexing mode.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
address: I/O address.
|
|
59
|
+
multiplex: Multiplex mode.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
Command result.
|
|
63
|
+
"""
|
|
64
|
+
return self._call("SetIOMultiplexing", address=int(address), multiplex=int(multiplex))
|
|
65
|
+
|
|
66
|
+
def get_io_adc(self, address: int) -> Any:
|
|
67
|
+
"""Get I/O ADC value.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
address: I/O address.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
ADC reading dict with keys: port, value.
|
|
74
|
+
"""
|
|
75
|
+
return self._call("GetIOADC", address=int(address))
|
|
76
|
+
|
|
77
|
+
def get_io_di(self, address: int) -> Any:
|
|
78
|
+
"""Get I/O digital input value.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
address: I/O address.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
DI reading dict with keys: port, level.
|
|
85
|
+
"""
|
|
86
|
+
return self._call("GetIODI", address=int(address))
|
|
87
|
+
|
|
88
|
+
def set_io_do(self, address: int, level: int, queued: bool = False) -> Any:
|
|
89
|
+
"""Set I/O digital output value.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
address: I/O address.
|
|
93
|
+
level: Output level (0 or 1).
|
|
94
|
+
queued: Queue the command (default False).
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Command result.
|
|
98
|
+
"""
|
|
99
|
+
return self._call("SetIODO", address=int(address), level=int(level), isQueued=queued)
|
|
100
|
+
|
|
101
|
+
def set_io_pwm(self, address: int, frequency: float, duty: float, queued: bool = False) -> Any:
|
|
102
|
+
"""Set I/O PWM output.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
address: I/O address.
|
|
106
|
+
frequency: PWM frequency in Hz.
|
|
107
|
+
duty: Duty cycle (0.0-1.0).
|
|
108
|
+
queued: Queue the command (default False).
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
Command result.
|
|
112
|
+
"""
|
|
113
|
+
return self._call("SetIOPWM", address=int(address),
|
|
114
|
+
frequency=float(frequency), dutyCycle=float(duty), isQueued=queued)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Motion command RPC wrappers (PTP, CP, ARC, jog, ...)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Any
|
|
4
|
+
from dobotkit.arm.commands._base import _Base
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MotionMixin(_Base):
|
|
8
|
+
"""RPC wrappers for Magician arm motion commands (PTP, CP, ARC, jog, params)."""
|
|
9
|
+
|
|
10
|
+
# -- pose / motion --
|
|
11
|
+
def get_pose(self) -> Any:
|
|
12
|
+
"""Get current arm pose (x, y, z, r, jointAngle)."""
|
|
13
|
+
return self._call("GetPose")
|
|
14
|
+
|
|
15
|
+
def set_ptp_cmd(self, mode: int, x: float, y: float, z: float, r: float,
|
|
16
|
+
queued: bool = True, wait_finish: bool = False) -> int:
|
|
17
|
+
"""Send PTP (point-to-point) motion command; returns queued command index.
|
|
18
|
+
|
|
19
|
+
When ``wait_finish`` is True, ``isWaitForFinish=true`` is sent so
|
|
20
|
+
DobotLink blocks until the arm physically completes the move (matching
|
|
21
|
+
DobotLab's own behaviour), and the call is given a longer timeout since
|
|
22
|
+
a move can take several seconds.
|
|
23
|
+
"""
|
|
24
|
+
call_timeout = 20.0 if wait_finish else None
|
|
25
|
+
return self._queued_index(self._call(
|
|
26
|
+
"SetPTPCmd", call_timeout=call_timeout,
|
|
27
|
+
ptpMode=int(mode), x=float(x), y=float(y), z=float(z), r=float(r),
|
|
28
|
+
isQueued=queued, isWaitForFinish=wait_finish))
|
|
29
|
+
|
|
30
|
+
def set_home_params(self, x: float, y: float, z: float, r: float,
|
|
31
|
+
queued: bool = True) -> Any:
|
|
32
|
+
"""Set home position parameters."""
|
|
33
|
+
return self._call("SetHOMEParams", x=x, y=y, z=z, r=r, isQueued=queued)
|
|
34
|
+
|
|
35
|
+
def set_home_cmd(self, queued: bool = True, wait_finish: bool = False) -> int:
|
|
36
|
+
"""Send home command; returns queued command index.
|
|
37
|
+
|
|
38
|
+
When ``wait_finish`` is True, ``isWaitForFinish=true`` is sent so
|
|
39
|
+
DobotLink blocks until homing physically completes; homing is slow
|
|
40
|
+
(~15-20s), so a longer timeout is used.
|
|
41
|
+
"""
|
|
42
|
+
call_timeout = 40.0 if wait_finish else None
|
|
43
|
+
return self._queued_index(self._call(
|
|
44
|
+
"SetHOMECmd", call_timeout=call_timeout,
|
|
45
|
+
isQueued=queued, isWaitForFinish=wait_finish))
|
|
46
|
+
|
|
47
|
+
def set_ptp_common_params(self, velocity: float, acceleration: float) -> Any:
|
|
48
|
+
"""Set PTP common parameters (velocity and acceleration ratios)."""
|
|
49
|
+
return self._call("SetPTPCommonParams",
|
|
50
|
+
velocityRatio=velocity, accelerationRatio=acceleration)
|
|
51
|
+
|
|
52
|
+
def set_ptp_coordinate_params(self, velocity: float, acceleration: float) -> Any:
|
|
53
|
+
"""Set PTP coordinate parameters (velocity and acceleration in xyz and r)."""
|
|
54
|
+
return self._call("SetPTPCoordinateParams",
|
|
55
|
+
xyzVelocity=velocity, rVelocity=velocity,
|
|
56
|
+
xyzAcceleration=acceleration, rAcceleration=acceleration)
|
|
57
|
+
|
|
58
|
+
def set_wait_cmd(self, ms: int, queued: bool = True) -> int:
|
|
59
|
+
"""Send wait command with timeout in milliseconds; returns queued command index."""
|
|
60
|
+
return self._queued_index(self._call("SetWAITCmd", timeout=int(ms), isQueued=queued))
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Sensor RPC wrappers. Filled in later."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Any
|
|
4
|
+
from dobotkit.arm.commands._base import _Base
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SensorMixin(_Base):
|
|
8
|
+
"""Sensor and alarm RPC wrapper methods."""
|
|
9
|
+
|
|
10
|
+
# -- sensors --
|
|
11
|
+
def set_color_sensor(self, enable: int, port: int, version: int = 1) -> Any:
|
|
12
|
+
return self._call("SetColorSensor", enable=int(enable),
|
|
13
|
+
colorPort=int(port), version=int(version))
|
|
14
|
+
|
|
15
|
+
def get_color_sensor(self) -> Any:
|
|
16
|
+
return self._call("GetColorSensor")
|
|
17
|
+
|
|
18
|
+
def set_infrared_sensor(self, enable: int, port: int, version: int = 1) -> Any:
|
|
19
|
+
return self._call("SetInfraredSensor", enable=int(enable),
|
|
20
|
+
infraredPort=int(port), version=int(version))
|
|
21
|
+
|
|
22
|
+
def get_infrared_sensor(self, port: int) -> Any:
|
|
23
|
+
return self._call("GetInfraredSensor", infraredPort=int(port))
|
|
24
|
+
|
|
25
|
+
def get_seeed_distance(self, port: int) -> Any:
|
|
26
|
+
return self._call("GetSeeedDistanceSensor", port=int(port))
|
|
27
|
+
|
|
28
|
+
def get_seeed_temp(self, port: int) -> Any:
|
|
29
|
+
return self._call("GetSeeedTempSensor", port=int(port))
|
|
30
|
+
|
|
31
|
+
def get_seeed_light(self, port: int) -> Any:
|
|
32
|
+
return self._call("GetSeeedLightSensor", port=int(port))
|
|
33
|
+
|
|
34
|
+
def set_seeed_rgb(self, port: int, rgb: float) -> Any:
|
|
35
|
+
return self._call("SetSeeedRGBLed", port=int(port), rgb=float(rgb))
|
|
36
|
+
|
|
37
|
+
def get_alarms_state(self) -> Any:
|
|
38
|
+
return self._call("GetAlarmsState")
|
dobotkit/arm/groups.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Ergonomic facades over :class:`~dobotkit.arm.commands.ArmCommands`.
|
|
2
|
+
|
|
3
|
+
These are *thin* facades over ``ArmCommands`` (the DobotLink RPC wrapper).
|
|
4
|
+
They exist purely to give the high-level ``MagicianLite`` API readable,
|
|
5
|
+
intent-revealing accessors (``arm.effector.suck(True)``,
|
|
6
|
+
``arm.sensors.distance(2)``, ``arm.io.set_do(5, 1)``) without leaking the
|
|
7
|
+
verbose ``Magician.*`` RPC method names. Every method delegates to exactly one
|
|
8
|
+
``ArmCommands`` method and passes its return value through (after unwrapping
|
|
9
|
+
the single scalar the caller wants, where relevant).
|
|
10
|
+
|
|
11
|
+
Design choices baked into these facades:
|
|
12
|
+
|
|
13
|
+
* **Effector on/off pairs** (suction cup, gripper) default to
|
|
14
|
+
``enable=True`` -- the control circuit/air pump stays powered while ``on``
|
|
15
|
+
toggles the actuator (grab vs release). They default to ``queued=True`` so
|
|
16
|
+
they sequence correctly inside a motion program (pick -> move -> place).
|
|
17
|
+
* **Sensor reads that are routed through the MagicBox** (color, infrared,
|
|
18
|
+
Seeed distance/temp/light/RGB, ADC) are *guarded*: a missing MagicBox or
|
|
19
|
+
peripheral answers with :class:`~dobotkit.exceptions.DobotTimeoutError` or
|
|
20
|
+
:class:`~dobotkit.exceptions.DobotProtocolError`, which is caught here and
|
|
21
|
+
degraded to ``None`` + a ``RuntimeWarning`` instead of raising, so
|
|
22
|
+
teaching/beginner code keeps running. See :func:`_guard`.
|
|
23
|
+
"""
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import warnings
|
|
27
|
+
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar
|
|
28
|
+
|
|
29
|
+
from dobotkit.enums import GPIOType
|
|
30
|
+
from dobotkit.exceptions import DobotProtocolError, DobotTimeoutError
|
|
31
|
+
|
|
32
|
+
if TYPE_CHECKING: # pragma: no cover - imported only for type checking
|
|
33
|
+
from dobotkit.arm.commands import ArmCommands
|
|
34
|
+
|
|
35
|
+
__all__ = ["EffectorGroup", "SensorGroup", "IOGroup"]
|
|
36
|
+
|
|
37
|
+
_T = TypeVar("_T")
|
|
38
|
+
|
|
39
|
+
#: Warning message emitted when a MagicBox-routed peripheral call gets no
|
|
40
|
+
#: response. Bilingual so it reads in both the Korean teaching materials and
|
|
41
|
+
#: the English API docs.
|
|
42
|
+
_UNAVAILABLE = (
|
|
43
|
+
"주변장치 응답이 없습니다 — 매직박스/센서 연결을 확인하세요 "
|
|
44
|
+
"(no peripheral response; check the MagicBox and its device)"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _guard(call: Callable[[], _T]) -> Optional[_T]:
|
|
49
|
+
"""Run a MagicBox-routed peripheral call, degrading to ``None`` if absent."""
|
|
50
|
+
try:
|
|
51
|
+
return call()
|
|
52
|
+
except (DobotTimeoutError, DobotProtocolError):
|
|
53
|
+
warnings.warn(_UNAVAILABLE, RuntimeWarning, stacklevel=3)
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class _Group:
|
|
58
|
+
"""Base for the device groups: holds the shared ``ArmCommands`` reference."""
|
|
59
|
+
|
|
60
|
+
def __init__(self, cmds: "ArmCommands") -> None:
|
|
61
|
+
self.cmds = cmds
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class EffectorGroup(_Group):
|
|
65
|
+
"""Ergonomic accessors for the arm's end effectors."""
|
|
66
|
+
|
|
67
|
+
def suck(self, on: bool, *, enable: bool = True, queued: bool = True) -> Optional[int]:
|
|
68
|
+
"""Drive the suction cup (``on`` = grab, ``enable`` = pump power)."""
|
|
69
|
+
return self.cmds.set_suction_cup(enable, on, queued=queued)
|
|
70
|
+
|
|
71
|
+
def grip(self, on: bool, *, enable: bool = True, queued: bool = True) -> Optional[int]:
|
|
72
|
+
"""Drive the gripper (``on`` = close, ``enable`` = pump power)."""
|
|
73
|
+
return self.cmds.set_gripper(enable, on, queued=queued)
|
|
74
|
+
|
|
75
|
+
def servo(self, index: int, angle: float, *, queued: bool = True) -> Optional[int]:
|
|
76
|
+
"""Set the angle of servo ``index`` on the end effector."""
|
|
77
|
+
return self.cmds.set_servo_angle(index, angle, queued=queued)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class SensorGroup(_Group):
|
|
81
|
+
"""Ergonomic accessors for the arm's sensors.
|
|
82
|
+
|
|
83
|
+
Every read here is MagicBox-routed and therefore guarded: it returns
|
|
84
|
+
``None`` (with a ``RuntimeWarning``) instead of raising when the MagicBox
|
|
85
|
+
or the attached sensor is not connected. See :func:`_guard`.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
def adc(self, port: int) -> Optional[int]:
|
|
89
|
+
"""Select ADC mode on ``port`` and read its analog value."""
|
|
90
|
+
def _read() -> int:
|
|
91
|
+
self.cmds.set_io_multiplexing(port, int(GPIOType.ADC))
|
|
92
|
+
return int(self.cmds.get_io_adc(port)["value"])
|
|
93
|
+
|
|
94
|
+
return _guard(_read)
|
|
95
|
+
|
|
96
|
+
def di(self, port: int) -> Optional[int]:
|
|
97
|
+
"""Read the digital-input level of ``port``."""
|
|
98
|
+
def _read() -> int:
|
|
99
|
+
result = self.cmds.get_io_di(port)
|
|
100
|
+
return int(result.get("level", 0))
|
|
101
|
+
|
|
102
|
+
return _guard(_read)
|
|
103
|
+
|
|
104
|
+
def color(self, port: int) -> Optional[Any]:
|
|
105
|
+
"""Enable the color sensor on ``port`` and read its reading."""
|
|
106
|
+
def _read() -> Any:
|
|
107
|
+
self.cmds.set_color_sensor(1, port)
|
|
108
|
+
return self.cmds.get_color_sensor()
|
|
109
|
+
|
|
110
|
+
return _guard(_read)
|
|
111
|
+
|
|
112
|
+
def infrared(self, port: int) -> Optional[Any]:
|
|
113
|
+
"""Enable the infrared sensor on ``port`` and read its reading."""
|
|
114
|
+
def _read() -> Any:
|
|
115
|
+
self.cmds.set_infrared_sensor(1, port)
|
|
116
|
+
return self.cmds.get_infrared_sensor(port)
|
|
117
|
+
|
|
118
|
+
return _guard(_read)
|
|
119
|
+
|
|
120
|
+
def distance(self, port: int) -> Optional[Any]:
|
|
121
|
+
"""Read the Seeed distance sensor on ``port``."""
|
|
122
|
+
return _guard(lambda: self.cmds.get_seeed_distance(port))
|
|
123
|
+
|
|
124
|
+
def temp(self, port: int) -> Optional[Any]:
|
|
125
|
+
"""Read the Seeed temperature/humidity sensor on ``port``."""
|
|
126
|
+
return _guard(lambda: self.cmds.get_seeed_temp(port))
|
|
127
|
+
|
|
128
|
+
def light(self, port: int) -> Optional[Any]:
|
|
129
|
+
"""Read the Seeed light sensor on ``port``."""
|
|
130
|
+
return _guard(lambda: self.cmds.get_seeed_light(port))
|
|
131
|
+
|
|
132
|
+
def rgb(self, port: int, value: float) -> Optional[int]:
|
|
133
|
+
"""Set the Seeed RGB LED on ``port`` to ``value``."""
|
|
134
|
+
return _guard(lambda: self.cmds.set_seeed_rgb(port, value))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class IOGroup(_Group):
|
|
138
|
+
"""Ergonomic accessors for the arm's digital/analog I/O."""
|
|
139
|
+
|
|
140
|
+
def set_do(self, address: int, level: int) -> Any:
|
|
141
|
+
"""Set the digital-output level of I/O pin ``address``."""
|
|
142
|
+
return self.cmds.set_io_do(address, level)
|
|
143
|
+
|
|
144
|
+
def get_di(self, address: int) -> Optional[int]:
|
|
145
|
+
"""Read the digital-input level of I/O pin ``address``."""
|
|
146
|
+
def _read() -> int:
|
|
147
|
+
result = self.cmds.get_io_di(address)
|
|
148
|
+
return int(result.get("level", 0))
|
|
149
|
+
|
|
150
|
+
return _guard(_read)
|
|
151
|
+
|
|
152
|
+
def get_adc(self, address: int) -> Optional[int]:
|
|
153
|
+
"""Read the ADC value of I/O pin ``address``."""
|
|
154
|
+
return _guard(lambda: int(self.cmds.get_io_adc(address)["value"]))
|
|
155
|
+
|
|
156
|
+
def set_pwm(self, address: int, frequency: float, duty: float) -> Any:
|
|
157
|
+
"""Configure PWM (frequency Hz, duty cycle %) on I/O pin ``address``."""
|
|
158
|
+
return self.cmds.set_io_pwm(address, frequency, duty)
|
|
159
|
+
|
|
160
|
+
def set_multiplexing(self, address: int, multiplex: int) -> Any:
|
|
161
|
+
"""Assign a multiplex function to I/O pin ``address``."""
|
|
162
|
+
return self.cmds.set_io_multiplexing(address, multiplex)
|