Skip to content

Commit 044a245

Browse files
authored
DOCS-1872: Add gripper and input controller methods (#542)
1 parent 4b456c9 commit 044a245

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/viam/components/gripper/gripper.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ async def open(
2828
):
2929
"""
3030
Open the gripper.
31+
32+
::
33+
34+
my_gripper = Gripper.from_robot(robot=robot, name="my_gripper")
35+
36+
# Open the gripper.
37+
await my_gripper.open()
3138
"""
3239
...
3340

@@ -42,6 +49,13 @@ async def grab(
4249
"""
4350
Instruct the gripper to grab.
4451
52+
::
53+
54+
my_gripper = Gripper.from_robot(robot=robot, name="my_gripper")
55+
56+
# Grab with the gripper.
57+
grabbed = await my_gripper.grab()
58+
4559
Returns:
4660
bool: Indicates if the gripper grabbed something.
4761
"""
@@ -57,6 +71,13 @@ async def stop(
5771
):
5872
"""
5973
Stop the gripper. It is assumed the gripper stops immediately.
74+
75+
::
76+
77+
my_gripper = Gripper.from_robot(robot=robot, name="my_gripper")
78+
79+
# Stop the gripper.
80+
await my_gripper.stop()
6081
"""
6182
...
6283

@@ -65,6 +86,14 @@ async def is_moving(self) -> bool:
6586
"""
6687
Get if the gripper is currently moving.
6788
89+
::
90+
91+
my_gripper = Gripper.from_robot(robot=robot, name="my_gripper")
92+
93+
# Check whether the gripper is currently moving.
94+
moving = await my_gripper.is_moving()
95+
print('Moving:', moving)
96+
6897
Returns:
6998
bool: Whether the gripper is moving.
7099
"""

src/viam/components/input/input.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ async def get_controls(self, *, extra: Optional[Dict[str, Any]] = None, timeout:
146146
"""
147147
Returns a list of Controls provided by the Controller
148148
149+
::
150+
151+
# Get the controller from the machine.
152+
my_controller = Controller.from_robot(
153+
robot=myRobotWithController, name="my_controller")
154+
155+
# Get the list of Controls provided by the controller.
156+
controls = await my_controller.get_controls()
157+
158+
# Print the list of Controls provided by the controller.
159+
print(f"Controls: {controls}")
160+
149161
Returns:
150162
List[Control]: List of controls provided by the Controller
151163
"""
@@ -159,6 +171,18 @@ async def get_events(
159171
Returns the most recent Event for each input
160172
(which should be the current state)
161173
174+
::
175+
176+
# Get the controller from the machine.
177+
my_controller = Controller.from_robot(
178+
robot=myRobotWithController, name="my_controller")
179+
180+
# Get the most recent Event for each Control.
181+
recent_events = await my_controller.get_events()
182+
183+
# Print out the most recent Event for each Control.
184+
print(f"Recent Events: {recent_events}")
185+
162186
Returns:
163187
Dict[Control, Event]: The most recent event for each input
164188
"""
@@ -178,6 +202,46 @@ def register_control_callback(
178202
Register a function that will fire on given EventTypes for a given
179203
Control
180204
205+
::
206+
207+
# Define a function to handle pressing the Start Menu Button "BUTTON_START" on
208+
# your controller, printing out the start time.
209+
def print_start_time(event):
210+
print(f"Start Menu Button was pressed at this time: {event.time}")
211+
212+
213+
# Define a function that handles the controller.
214+
async def handle_controller(controller):
215+
# Get the list of Controls on the controller.
216+
controls = await controller.get_controls()
217+
218+
# If the "BUTTON_START" Control is found, register the function
219+
# print_start_time to fire when "BUTTON_START" has the event "ButtonPress"
220+
# occur.
221+
if Control.BUTTON_START in controls:
222+
controller.register_control_callback(
223+
Control.BUTTON_START, [EventType.BUTTON_PRESS], print_start_time)
224+
else:
225+
print("Oops! Couldn't find the start button control! Is your "
226+
"controller connected?")
227+
exit()
228+
229+
while True:
230+
await asyncio.sleep(1.0)
231+
232+
233+
async def main():
234+
# ... < INSERT CONNECTION CODE FROM MACHINE'S CODE SAMPLE TAB >
235+
236+
# Get your controller from the machine.
237+
my_controller = Controller.from_robot(
238+
robot=myRobotWithController, name="my_controller")
239+
240+
# Run the handleController function.
241+
await handleController(my_controller)
242+
243+
# ... < INSERT ANY OTHER CODE FOR MAIN FUNCTION >
244+
181245
Args:
182246
control (Control): The control to register the function for
183247
triggers (List[EventType]): The events that will
@@ -197,6 +261,16 @@ async def trigger_event(
197261
) -> None: # Explicitly return None for typechecking, as this is technically a NoReturn default implementation
198262
"""Directly send an Event (such as a button press) from external code
199263
264+
::
265+
266+
# Define a "Button is Pressed" event for the control BUTTON_START.
267+
button_is_pressed_event = Event(
268+
time(), EventType.BUTTON_PRESS, Control.BUTTON_START, 1.0)
269+
270+
# Trigger the event on your controller. Set this trigger to timeout if it has
271+
# not completed in 7 seconds.
272+
await myController.trigger_event(event=my_event, timeout=7.0)
273+
200274
Args:
201275
event (Event): The event to trigger
202276
"""

0 commit comments

Comments
 (0)