Skip to content

Commit 4b456c9

Browse files
authored
DOCS-1871: Update camera, encoder, gantry examples (#540)
1 parent f1f44a7 commit 4b456c9

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

src/viam/components/component_base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
4747
"""
4848
Get all geometries associated with the Component, in their current configuration, in the frame of the Component.
4949
50+
::
51+
52+
geometries = await component.get_geometries()
53+
54+
if geometries:
55+
# Get the center of the first geometry
56+
print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
57+
5058
Returns:
5159
List[Geometry]: The geometries associated with the Component.
5260
"""

src/viam/components/encoder/encoder.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ async def reset_position(
3636
):
3737
"""
3838
Set the current position to be the new zero (home) position.
39+
40+
::
41+
42+
my_encoder = Encoder.from_robot(robot=robot, name='my_encoder')
43+
44+
# Reset the zero position of the encoder.
45+
await my_encoder.reset_position()
46+
3947
"""
4048
...
4149

@@ -54,6 +62,14 @@ async def get_position(
5462
The position will be either in relative units (ticks away from a zero position) for
5563
``PositionType.TICKS`` or absolute units (degrees along a circle) for ``PositionType.DEGREES``.
5664
65+
::
66+
67+
my_encoder = Encoder.from_robot(robot=robot, name='my_encoder')
68+
69+
# Get the position of the encoder in ticks
70+
position = await my_encoder.get_position(encoder.PositionTypeTicks)
71+
print("The encoder position is currently ", position[0], position[1])
72+
5773
Args:
5874
position_type (PositionType.ValueType): The desired output type of the position
5975
@@ -75,6 +91,13 @@ async def get_properties(
7591
"""
7692
Return a dictionary of the types of position reporting this encoder supports
7793
94+
::
95+
96+
my_encoder = Encoder.from_robot(robot=robot, name='my_encoder')
97+
98+
# Get whether the encoder returns position in ticks or degrees.
99+
properties = await my_encoder.get_properties()
100+
78101
Returns:
79102
Encoder.Properties: Map of position types to supported status.
80103
"""

src/viam/components/gantry/gantry.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ async def get_position(self, *, extra: Optional[Dict[str, Any]] = None, timeout:
2424
"""
2525
Get the position in millimeters.
2626
27+
::
28+
29+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
30+
31+
# Get the current positions of the axes of the gantry in millimeters.
32+
positions = await my_gantry.get_position()
33+
2734
Returns:
2835
List[float]: The position of the axes.
2936
"""
@@ -42,6 +49,20 @@ async def move_to_position(
4249
"""
4350
Move the gantry to a new position at the requested speeds.
4451
52+
::
53+
54+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
55+
56+
# Create a list of positions for the axes of the gantry to move to. Assume in
57+
# this example that the gantry is multi-axis, with 3 axes.
58+
examplePositions = [1, 2, 3]
59+
60+
exampleSpeeds = [3, 9, 12]
61+
62+
# Move the axes of the gantry to the positions specified.
63+
await my_gantry.move_to_position(
64+
positions=examplePositions, speeds=exampleSpeeds)
65+
4566
Args:
4667
positions (List[float]): List of positions for the axes to move to,
4768
in millimeters.
@@ -53,6 +74,12 @@ async def home(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optiona
5374
"""
5475
Home the gantry to find it's starting and ending positions
5576
77+
::
78+
79+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
80+
81+
await my_gantry.home()
82+
5683
Returns:
5784
bool : whether the gantry has run the homing sequence successfully
5885
"""
@@ -62,6 +89,13 @@ async def get_lengths(self, *, extra: Optional[Dict[str, Any]] = None, timeout:
6289
"""
6390
Get the lengths of the axes of the gantry in millimeters.
6491
92+
::
93+
94+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
95+
96+
# Get the lengths of the axes of the gantry in millimeters.
97+
lengths_mm = await my_gantry.get_lengths()
98+
6599
Returns:
66100
List[float]: The lengths of the axes.
67101
"""
@@ -71,6 +105,15 @@ async def get_lengths(self, *, extra: Optional[Dict[str, Any]] = None, timeout:
71105
async def stop(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs):
72106
"""
73107
Stop all motion of the gantry. It is assumed that the gantry stops immediately.
108+
109+
::
110+
111+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
112+
113+
# Stop all motion of the gantry. It is assumed that the gantry stops
114+
# immediately.
115+
await my_gantry.stop()
116+
74117
"""
75118
...
76119

@@ -79,6 +122,17 @@ async def is_moving(self) -> bool:
79122
"""
80123
Get if the gantry is currently moving.
81124
125+
::
126+
127+
my_gantry = Gantry.from_robot(robot=robot, name="my_gantry")
128+
129+
# Stop all motion of the gantry. It is assumed that the
130+
# gantry stops immediately.
131+
await my_gantry.stop()
132+
133+
# Print if the gantry is currently moving.
134+
print(my_gantry.is_moving())
135+
82136
Returns:
83137
bool: Whether the gantry is moving.
84138
"""

src/viam/resource/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def get_resource_name(cls, name: str) -> ResourceName:
4545
def from_robot(cls, robot: "RobotClient", name: str) -> Self:
4646
"""Get the Resource named ``name`` from the provided robot.
4747
48+
::
49+
50+
# Can be used with any resource, using an arm as an example
51+
my_arm = Arm.from_robot(robot, "my_arm")
52+
4853
Args:
4954
robot (RobotClient): The robot
5055
name (str): The name of the Resource
@@ -60,6 +65,11 @@ async def do_command(
6065
) -> Mapping[str, "ValueTypes"]:
6166
"""Send/Receive arbitrary commands to the Resource
6267
68+
::
69+
70+
command = {"cmd": "test", "data1": 500}
71+
result = component.do(command)
72+
6373
Args:
6474
command (Mapping[str, ValueTypes]): The command to execute
6575
@@ -92,5 +102,10 @@ async def close(self):
92102
Close must be idempotent. Later configuration may allow a resource to be "open" again.
93103
If a resource does not want or need a close function, it is assumed that the resource does not need to retun errors when future
94104
non-Close methods are called.
105+
106+
::
107+
108+
await component.close()
109+
95110
"""
96111
return

0 commit comments

Comments
 (0)