Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions components/gripper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ func (c *client) Grab(ctx context.Context, extra map[string]interface{}) (bool,
return resp.Success, nil
}

func (c *client) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (HoldingStatus, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return HoldingStatus{}, err
}
resp, err := c.client.IsHoldingSomething(ctx, &pb.IsHoldingSomethingRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return HoldingStatus{}, err
}
return HoldingStatus{
IsHoldingSomething: resp.IsHoldingSomething,
Meta: resp.Meta.AsMap(),
}, nil
}

func (c *client) Stop(ctx context.Context, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions components/gripper/fake/gripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func (g *Gripper) Stop(ctx context.Context, extra map[string]interface{}) error
return nil
}

// IsHoldingSomething always returns a status in which the gripper is not holding something and
// no additional information is supplied.
func (g *Gripper) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error) {
return gripper.HoldingStatus{}, nil
}

// IsMoving is always false for a fake gripper.
func (g *Gripper) IsMoving(ctx context.Context) (bool, error) {
return false, nil
Expand Down
10 changes: 10 additions & 0 deletions components/gripper/gripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func Named(name string) resource.Name {
return resource.NewName(API, name)
}

// HoldingStatus represents whether the gripper is currently holding onto
// an object as well as any additional contextual information (stored in `Meta`).
type HoldingStatus struct {
IsHoldingSomething bool
Meta map[string]interface{}
}

// A Gripper represents a physical robotic gripper.
// For more information, see the [gripper component docs].
//
Expand Down Expand Up @@ -74,6 +81,9 @@ type Gripper interface {
// returns true if we grabbed something.
// This will block until done or a new operation cancels this one.
Grab(ctx context.Context, extra map[string]interface{}) (bool, error)

// IsHoldingSomething returns whether the gripper is currently holding onto an object.
IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (HoldingStatus, error)
}

// FromRobot is a helper for getting the named Gripper from the given Robot.
Expand Down
22 changes: 20 additions & 2 deletions components/gripper/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/gripper/v1"
"go.viam.com/utils/protoutils"

"go.viam.com/rdk/operation"
"go.viam.com/rdk/protoutils"
rprotoutils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
Expand Down Expand Up @@ -77,6 +78,23 @@ func (s *serviceServer) IsMoving(ctx context.Context, req *pb.IsMovingRequest) (
return &pb.IsMovingResponse{IsMoving: moving}, nil
}

// IsHoldingSomething queries if the gripper has managed to grab something.
func (s *serviceServer) IsHoldingSomething(ctx context.Context, req *pb.IsHoldingSomethingRequest) (*pb.IsHoldingSomethingResponse, error) {
gripper, err := s.coll.Resource(req.GetName())
if err != nil {
return nil, err
}
holdingStatus, err := gripper.IsHoldingSomething(ctx, req.Extra.AsMap())
if err != nil {
return nil, err
}
meta, err := protoutils.StructToStructPb(holdingStatus.Meta)
if err != nil {
return nil, err
}
return &pb.IsHoldingSomethingResponse{IsHoldingSomething: holdingStatus.IsHoldingSomething, Meta: meta}, nil
}

// DoCommand receives arbitrary commands.
func (s *serviceServer) DoCommand(ctx context.Context,
req *commonpb.DoCommandRequest,
Expand All @@ -85,7 +103,7 @@ func (s *serviceServer) DoCommand(ctx context.Context,
if err != nil {
return nil, err
}
return protoutils.DoFromResourceServer(ctx, gripper, req)
return rprotoutils.DoFromResourceServer(ctx, gripper, req)
}

func (s *serviceServer) GetGeometries(ctx context.Context, req *commonpb.GetGeometriesRequest) (*commonpb.GetGeometriesResponse, error) {
Expand Down
27 changes: 18 additions & 9 deletions testutils/inject/gripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
// Gripper is an injected gripper.
type Gripper struct {
gripper.Gripper
name resource.Name
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
OpenFunc func(ctx context.Context, extra map[string]interface{}) error
GrabFunc func(ctx context.Context, extra map[string]interface{}) (bool, error)
StopFunc func(ctx context.Context, extra map[string]interface{}) error
IsMovingFunc func(context.Context) (bool, error)
CloseFunc func(ctx context.Context) error
GeometriesFunc func(ctx context.Context) ([]spatialmath.Geometry, error)
KinematicsFunc func(ctx context.Context) (referenceframe.Model, error)
name resource.Name
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
OpenFunc func(ctx context.Context, extra map[string]interface{}) error
GrabFunc func(ctx context.Context, extra map[string]interface{}) (bool, error)
StopFunc func(ctx context.Context, extra map[string]interface{}) error
IsHoldingSomethingFunc func(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error)
IsMovingFunc func(context.Context) (bool, error)
CloseFunc func(ctx context.Context) error
GeometriesFunc func(ctx context.Context) ([]spatialmath.Geometry, error)
KinematicsFunc func(ctx context.Context) (referenceframe.Model, error)
}

// NewGripper returns a new injected gripper.
Expand Down Expand Up @@ -49,6 +50,14 @@ func (g *Gripper) Grab(ctx context.Context, extra map[string]interface{}) (bool,
return g.GrabFunc(ctx, extra)
}

// IsHoldingSomething calls the injected IsHoldingSomething or the real version.
func (g *Gripper) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error) {
if g.IsHoldingSomethingFunc == nil {
return g.Gripper.IsHoldingSomething(ctx, extra)
}
return g.IsHoldingSomethingFunc(ctx, extra)
}

// Stop calls the injected Stop or the real version.
func (g *Gripper) Stop(ctx context.Context, extra map[string]interface{}) error {
if g.StopFunc == nil {
Expand Down
Loading