client

package
v0.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2022 License: AGPL-3.0 Imports: 35 Imported by: 12

README

Go client

The Go client for the Viam RDK can function as an SDK to connect to a robot.

Install

To install the Go SDK, run

	go get go.viam.com/rdk/robot/client

Basic Usage

To connect to a robot as a client, you should instantiate a client.

	package main

	import (
		"context"

		"github.com/edaniels/golog"
		"go.viam.com/rdk/robot/client"
		"go.viam.com/rdk/utils"
		"go.viam.com/utils/rpc"
	)

	func main() {
		logger := golog.NewDebugLogger("client")
		// this instantiates a robot client that is connected to the robot at <address>
		robot, err := client.New(
			context.Background(),
			"<address of robot>",
			logger,
			// credentials can be found in the Viam app if the robot is Viam managed
			// otherwise, credential keys can also be set through the config
			client.WithDialOptions(rpc.WithCredentials(rpc.Credentials{
				Type:    utils.CredentialsTypeRobotLocationSecret,
				Payload: "<robot secret>",
			})),
		)
		if err != nil {
			logger.Fatal(err)
		}
	}

If the robot is managed by Viam, you can also navigate to the robot page on app.viam.com, select the CONNECT tab, and copy the boilerplate code from the section labeled Golang SDK.

You can then query resources and also grab a resource by its name.

	logger.Info("Resources:")
  	logger.Info(robot.ResourceNames())

	// grab a motor by its name and query for its position
	m1, err := motor.FromRobot(robot, "motor1")
	if err != nil {
		logger.Fatal(err)
	}
	position, err := m1.Position(context.Background(), map[string]interface{}{})
	if err != nil {
		logger.Error(err)
	}

Remember to close the client at the end!

	robot.Close(context.Background())

Documentation

Overview

Package client contains a gRPC based robot.Robot client.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingClientRegistration is used when there is no resource client registered for the subtype.
	ErrMissingClientRegistration = errors.New("resource client registration doesn't exist")
)

Functions

func ExtractDialOptions

func ExtractDialOptions(opts ...RobotClientOption) []rpc.DialOption

ExtractDialOptions extracts RPC dial options from the given options, if any exist.

Types

type RobotClient

type RobotClient struct {
	// contains filtered or unexported fields
}

RobotClient satisfies the robot.Robot interface through a gRPC based client conforming to the robot.proto contract.

func New

func New(ctx context.Context, address string, logger golog.Logger, opts ...RobotClientOption) (*RobotClient, error)

New constructs a new RobotClient that is served at the given address. The given context can be used to cancel the operation.

func (*RobotClient) Changed

func (rc *RobotClient) Changed() <-chan bool

Changed watches for whether the remote has changed.

func (*RobotClient) Close

func (rc *RobotClient) Close(ctx context.Context) error

Close cleanly closes the underlying connections and stops the refresh goroutine if it is running.

func (*RobotClient) Connected

func (rc *RobotClient) Connected() bool

Connected exposes whether a robot client is connected to the remote.

func (*RobotClient) DiscoverComponents

func (rc *RobotClient) DiscoverComponents(ctx context.Context, qs []discovery.Query) ([]discovery.Discovery, error)

DiscoverComponents takes a list of discovery queries and returns corresponding component configurations.

func (*RobotClient) FrameSystemConfig

func (rc *RobotClient) FrameSystemConfig(
	ctx context.Context,
	additionalTransforms []*referenceframe.LinkInFrame,
) (framesystemparts.Parts, error)

FrameSystemConfig returns the info of each individual part that makes up the frame system.

func (*RobotClient) Logger

func (rc *RobotClient) Logger() golog.Logger

Logger returns the logger being used for this robot.

func (*RobotClient) OperationManager

func (rc *RobotClient) OperationManager() *operation.Manager

OperationManager returns nil.

func (*RobotClient) ProcessManager

func (rc *RobotClient) ProcessManager() pexec.ProcessManager

ProcessManager returns a useless process manager for the sake of satisfying the robot.Robot interface. Maybe it should not be part of the interface!

func (*RobotClient) Refresh

func (rc *RobotClient) Refresh(ctx context.Context) (err error)

Refresh manually updates the underlying parts of the robot based on its metadata response.

func (*RobotClient) RefreshEvery

func (rc *RobotClient) RefreshEvery(ctx context.Context, every time.Duration)

RefreshEvery refreshes the robot on the interval given by every until the given context is done.

func (*RobotClient) RemoteByName

func (rc *RobotClient) RemoteByName(name string) (robot.Robot, bool)

RemoteByName returns a remote robot by name. It is assumed to exist on the other end. Right now this method is unimplemented.

func (*RobotClient) RemoteNames

func (rc *RobotClient) RemoteNames() []string

RemoteNames returns the names of all known remotes.

func (*RobotClient) ResourceByName

func (rc *RobotClient) ResourceByName(name resource.Name) (interface{}, error)

ResourceByName returns resource by name.

func (*RobotClient) ResourceNames

func (rc *RobotClient) ResourceNames() []resource.Name

ResourceNames returns all resource names.

func (*RobotClient) ResourceRPCSubtypes

func (rc *RobotClient) ResourceRPCSubtypes() []resource.RPCSubtype

ResourceRPCSubtypes returns a list of all known resource subtypes.

func (*RobotClient) SessionManager added in v0.2.5

func (rc *RobotClient) SessionManager() session.Manager

SessionManager returns nil.

func (*RobotClient) SetParentNotifier

func (rc *RobotClient) SetParentNotifier(f func())

SetParentNotifier set the notifier function, robot client will use that the relay changes.

func (*RobotClient) Status

func (rc *RobotClient) Status(ctx context.Context, resourceNames []resource.Name) ([]robot.Status, error)

Status takes a list of resource names and returns their corresponding statuses. If no names are passed in, return all statuses.

func (*RobotClient) StopAll

func (rc *RobotClient) StopAll(ctx context.Context, extra map[resource.Name]map[string]interface{}) error

StopAll cancels all current and outstanding operations for the robot and stops all actuators and movement.

func (*RobotClient) TransformPose

func (rc *RobotClient) TransformPose(
	ctx context.Context,
	query *referenceframe.PoseInFrame,
	destination string,
	additionalTransforms []*referenceframe.LinkInFrame,
) (*referenceframe.PoseInFrame, error)

TransformPose will transform the pose of the requested poseInFrame to the desired frame in the robot's frame system.

type RobotClientOption

type RobotClientOption interface {
	// contains filtered or unexported methods
}

RobotClientOption configures how we set up the connection. Cribbed from https://github.com/grpc/grpc-go/blob/aff571cc86e6e7e740130dbbb32a9741558db805/dialoptions.go#L41

func WithCheckConnectedEvery

func WithCheckConnectedEvery(checkConnectedEvery time.Duration) RobotClientOption

WithCheckConnectedEvery returns a RobotClientOption for how often to check connection to the robot.

func WithDialOptions

func WithDialOptions(opts ...rpc.DialOption) RobotClientOption

WithDialOptions returns a RobotClientOption which sets the options for making gRPC connections to other servers.

func WithDisableSessions added in v0.2.5

func WithDisableSessions() RobotClientOption

WithDisableSessions returns a RobotClientOption that disables session support.

func WithReconnectEvery

func WithReconnectEvery(reconnectEvery time.Duration) RobotClientOption

WithReconnectEvery returns a RobotClientOption for how often to reconnect the robot.

func WithRefreshEvery

func WithRefreshEvery(refreshEvery time.Duration) RobotClientOption

WithRefreshEvery returns a RobotClientOption for how often to refresh the status/parts of the robot.

func WithRemoteName added in v0.2.2

func WithRemoteName(remoteName string) RobotClientOption

WithRemoteName returns a RobotClientOption setting the name of the remote robot.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL