Skip to content

Commit

Permalink
DOCS-2231: Review input controller, generic, and gripper snippets (#2860
Browse files Browse the repository at this point in the history
)
  • Loading branch information
skyleilani authored May 13, 2024
1 parent 4f65f22 commit 084d70b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 90 deletions.
8 changes: 2 additions & 6 deletions docs/components/generic/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error): An error, if one occurred.

```go {class="line-numbers linkable-line-numbers"}
myGeneric, err := generic.FromRobot(robot, "my_generic_component")

resp, err := myGeneric.DoCommand(ctx, map[string]interface{}{"command": "example"})
resp, err := myGeneric.DoCommand(context.Background(), map[string]interface{}{"command": "example"})
```

For more information, see the [Go SDK Code](https://github.com/viamrobotics/api/blob/main/component/generic/v1/generic_grpc.pb.go).
Expand Down Expand Up @@ -270,9 +268,7 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error) : An error, if one occurred. Close will never return an error for a generic resource.

```go {class="line-numbers linkable-line-numbers"}
myGeneric, err := generic.FromRobot(robot, "my_generic")

err := myGeneric.Close(ctx)
err := myGeneric.Close(context.Background())
```

For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/resource#TriviallyCloseable).
Expand Down
20 changes: 3 additions & 17 deletions docs/components/gripper/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ await my_gripper.open()
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/components/gripper#Gripper).

```go
myGripper, err := gripper.FromRobot(robot, "my_gripper")

// Open the gripper.
err := myGripper.Open(context.Background(), nil)
```
Expand Down Expand Up @@ -172,8 +170,6 @@ grabbed = await my_gripper.grab()
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/components/gripper#Gripper).

```go
myGripper, err := gripper.FromRobot(robot, "my_gripper")

// Grab with the gripper.
grabbed, err := myGripper.Grab(context.Background(), nil)
```
Expand Down Expand Up @@ -221,8 +217,6 @@ await my_gripper.stop()
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/components/gripper#Gripper).

```go
myGripper, err := gripper.FromRobot(robot, "my_gripper")

// Stop the gripper.
err := myGripper.Stop(context.Background(), nil)
```
Expand Down Expand Up @@ -270,8 +264,6 @@ print('Moving:', moving)
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/resource#MovingCheckable).

```go
myGripper, err := gripper.FromRobot(robot, "my_gripper")

// Check whether the gripper is currently moving.
moving, err := myGripper.IsMoving(context.Background())
logger.Info("Is moving?")
Expand Down Expand Up @@ -325,14 +317,12 @@ if geometries:
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/resource#Shaped).

```go {class="line-numbers linkable-line-numbers"}
myGripper, err := gripper.FromRobot(robot, "my_gripper")

geometries, err := myGripper.Geometries(context.Background(), nil)

if len(geometries) > 0 {
// Get the center of the first geometry
elem := geometries[0]
fmt.Println("Pose of the first geometry's center point:", elem.center)
fmt.Println("Pose of the first geometry's center point:", elem.Pose())
}
```

Expand Down Expand Up @@ -382,9 +372,7 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error): An error, if one occurred.

```go {class="line-numbers linkable-line-numbers"}
myGripper, err := gripper.FromRobot(robot, "my_gripper")

resp, err := myGripper.DoCommand(ctx, map[string]interface{}{"command": "example"})
resp, err := myGripper.DoCommand(context.Background(), map[string]interface{}{"command": "example"})
```

For more information, see the [Go SDK Code](https://github.com/viamrobotics/api/blob/main/component/gripper/v1/gripper_grpc.pb.go).
Expand Down Expand Up @@ -427,9 +415,7 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error) : An error, if one occurred.

```go {class="line-numbers linkable-line-numbers"}
myGripper, err := gripper.FromRobot(robot, "my_gripper")

err := myGripper.Close(ctx)
err := myGripper.Close(context.Background())
```

For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/resource#Resource).
Expand Down
86 changes: 19 additions & 67 deletions docs/components/input-controller/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,62 +192,30 @@ For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/c

```go {class="line-numbers linkable-line-numbers"}

// Define a function that handles the controller.
func handleController(controller input.Controller) {

// Define a function to handle pressing the Start Menu Button "ButtonStart" on your controller, logging the start time.
printStartTime := func(ctx context.Context, event input.Event) {
logger.Info("Start Menu Button was pressed at this time: %v", event.Time)
}

// Define the EventType "ButtonPress" to serve as the trigger for printStartTime.
triggers := [1]input.EventType{input.ButtonPress}

// Get the controller's Controls.
controls, err := controller.Controls(context.Background(), nil)

// If the "ButtonStart" Control is found, register the function printStartTime to fire when "ButtonStart" has the event "ButtonPress" occur.
if slices.Contains(controls, Control.ButtonStart) {
err := controller.RegisterControlCallback(context.Background(), Control: input.ButtonStart, triggers, printStartTime, nil)
}
else {
logger.Fatalf("Oops! Couldn't find the start button control! Is your controller connected?")
}
// Define a function to handle pressing the Start Menu button, "ButtonStart", on your controller and logging the start time
printStartTime := func(ctx context.Context, event input.Event) {
logger.Info("Start Menu Button was pressed at this time: %v", event.Time)
}

func main() {
utils.ContextualMain(mainWithArgs, logging.NewLogger("client"))
}


func mainWithArgs(ctx context.Context, args []string, logger logging.Logger) (err error) {
// ... < INSERT CONNECTION CODE FROM MACHINE'S CODE SAMPLE TAB >

// Get the controller from the machine.
myController, err := input.FromRobot(myRobotWithController, "my_controller")

// Run the handleController function.
err := HandleController(myController)

// Delay closing your connection to your machine.
err = myRobotWithController.Start(ctx)
defer myRobotWithController.Close(ctx)

// Wait to exit mainWithArgs() until Context is Done.
<-ctx.Done()
// Define the EventType "ButtonPress" to serve as the trigger for printStartTime.
triggers := []input.EventType{input.ButtonPress}

// ... < INSERT ANY OTHER CODE FOR MAIN FUNCTION >
// Get the controller's Controls.
controls, err := controller.Controls(ctx, nil)

return nil
// If the "ButtonStart" Control is found, register the function printStartTime to fire when "ButtonStart" has the event "ButtonPress" occur.
if !slices.Contains(controls, input.ButtonStart) {
return errors.New("button `ButtonStart` not found; controller may be disconnected")
}
myController.RegisterControlCallback(context.Background(), input.ButtonStart, triggers, printStartTime, nil)
```

{{% /tab %}}
{{< /tabs >}}

### GetEvents

This method returns the current state of the controller as a map of [Event Objects](#event-object), representing the most recent event that has occured on each available [Control](#control-field).
This method returns the current state of the controller as a map of [Event Objects](#event-object), representing the most recent event that has occurred on each available [Control](#control-field).

{{< tabs >}}
{{% tab name="Python" %}}
Expand Down Expand Up @@ -291,14 +259,8 @@ print(f"Recent Events:\n{recent_events}")
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/components/input#Controller).

```go {class="line-numbers linkable-line-numbers"}
// Get the controller from the machine.
myController, err := input.FromRobot(myRobotWithController, "my_controller")

// Get the most recent Event for each Control.
recent_events, err := myController.Events(context.Background(), nil)

// Log the most recent Event for each Control.
logger.Info("Recent Events: %v", recent_events)
```

{{% /tab %}}
Expand Down Expand Up @@ -350,15 +312,8 @@ print(f"Controls:\n{controls}")
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/components/input#Controller).

```go {class="line-numbers linkable-line-numbers"}
// Get the controller from the machine.
myController, err := input.FromRobot(myRobotWithController, "my_controller")

// Get the list of Controls provided by the controller.
controls, err := myController.Controls(context.Background(), nil)

// Log the list of Controls provided by the controller.
logger.Info("Controls:")
logger.Info(controls)
```

{{% /tab %}}
Expand Down Expand Up @@ -416,14 +371,16 @@ For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/c
// Define a "Button is Pressed" event for the control ButtonStart.
buttonIsPressedEvent := input.Event{Time: time.Now(), Event: input.ButtonPress, Control: input.ButtonStart, Value: 1.0}

// Trigger the event on your controller.
err := myController.TriggerEvent(ctx Context.background(), buttonIsPressedEvent, nil)
triggerableController, ok := myController.(input.Triggerable)
if !ok {
logger.Fatalf("this controller does not support trigger events")
}

// Log any errors that occur.
// Trigger the event on your controller.
err = triggerableController.TriggerEvent(ctx, buttonIsPressedEvent, nil)
if err != nil {
logger.Fatalf("cannot trigger event on controller: %v", err)
}

```

{{% /tab %}}
Expand Down Expand Up @@ -533,9 +490,6 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error): An error, if one occurred.

```go {class="line-numbers linkable-line-numbers"}
// Get the controller from the machine.
myController, err := input.FromRobot(myRobotWithController, "my_controller")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myController.DoCommand(context.Background(), command)
```
Expand Down Expand Up @@ -580,9 +534,7 @@ For more information, see the [Python SDK Docs](https://python.viam.dev/autoapi/
- [(error)](https://pkg.go.dev/builtin#error) : An error, if one occurred.

```go {class="line-numbers linkable-line-numbers"}
myController, err := input.FromRobot(myRobotWithController, "my_controller")

err := myController.Close(ctx)
err := myController.Close(context.Background())
```

For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/resource#Resource).
Expand Down

0 comments on commit 084d70b

Please sign in to comment.