-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathservo.py
114 lines (76 loc) · 3.53 KB
/
servo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import abc
from typing import Any, Final, Mapping, Optional
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from ..component_base import ComponentBase
class Servo(ComponentBase):
"""
Servo represents a physical servo.
This acts as an abstract base class for any drivers representing specific
servo implementations. This cannot be used on its own. If the ``__init__()`` function is
overridden, it must call the ``super().__init__()`` function.
::
from viam.components.servo import Servo
For more information, see `Servo component <https://docs.viam.com/components/servo/>`_.
"""
SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "servo"
)
@abc.abstractmethod
async def move(self, angle: int, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs):
"""
Move the servo to the provided angle.
::
my_servo = Servo.from_robot(robot=machine, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Move the servo from its origin to the desired angle of 90 degrees.
await my_servo.move(90)
Args:
angle (int): The desired angle of the servo in degrees.
For more information, see `Servo component <https://docs.viam.com/components/servo/>`_.
"""
...
@abc.abstractmethod
async def get_position(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> int:
"""
Get the current angle (degrees) of the servo.
::
my_servo = Servo.from_robot(robot=machine, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Get the current set angle of the servo.
pos1 = await my_servo.get_position()
# Move the servo from its origin to the desired angle of 20 degrees.
await my_servo.move(20)
# Get the current set angle of the servo.
pos2 = await my_servo.get_position()
Returns:
int: The current angle of the servo in degrees.
For more information, see `Servo component <https://docs.viam.com/components/servo/>`_.
"""
...
@abc.abstractmethod
async def stop(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs):
"""
Stop the servo. It is assumed that the servo stops immediately.
::
my_servo = Servo.from_robot(robot=machine, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Stop the servo. It is assumed that the servo stops moving immediately.
await my_servo.stop()
For more information, see `Servo component <https://docs.viam.com/components/servo/>`_.
"""
...
@abc.abstractmethod
async def is_moving(self) -> bool:
"""
Get if the servo is currently moving.
::
my_servo = Servo.from_robot(robot=machine, name="my_servo")
print(await my_servo.is_moving())
Returns:
bool: Whether the servo is moving.
For more information, see `Servo component <https://docs.viam.com/components/servo/>`_.
"""
...