Skip to content

Commit 1980d85

Browse files
authored
[RSDK-10924] add IsHoldingSomething to gripper API (#5052)
1 parent 4e463d7 commit 1980d85

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

components/gripper/client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func (c *client) Grab(ctx context.Context, extra map[string]interface{}) (bool,
7474
return resp.Success, nil
7575
}
7676

77+
func (c *client) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (HoldingStatus, error) {
78+
ext, err := protoutils.StructToStructPb(extra)
79+
if err != nil {
80+
return HoldingStatus{}, err
81+
}
82+
resp, err := c.client.IsHoldingSomething(ctx, &pb.IsHoldingSomethingRequest{
83+
Name: c.name,
84+
Extra: ext,
85+
})
86+
if err != nil {
87+
return HoldingStatus{}, err
88+
}
89+
return HoldingStatus{
90+
IsHoldingSomething: resp.IsHoldingSomething,
91+
Meta: resp.Meta.AsMap(),
92+
}, nil
93+
}
94+
7795
func (c *client) Stop(ctx context.Context, extra map[string]interface{}) error {
7896
ext, err := protoutils.StructToStructPb(extra)
7997
if err != nil {

components/gripper/fake/gripper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ func (g *Gripper) Stop(ctx context.Context, extra map[string]interface{}) error
109109
return nil
110110
}
111111

112+
// IsHoldingSomething always returns a status in which the gripper is not holding something and
113+
// no additional information is supplied.
114+
func (g *Gripper) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error) {
115+
return gripper.HoldingStatus{}, nil
116+
}
117+
112118
// IsMoving is always false for a fake gripper.
113119
func (g *Gripper) IsMoving(ctx context.Context) (bool, error) {
114120
return false, nil

components/gripper/gripper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func Named(name string) resource.Name {
3636
return resource.NewName(API, name)
3737
}
3838

39+
// HoldingStatus represents whether the gripper is currently holding onto
40+
// an object as well as any additional contextual information (stored in `Meta`).
41+
type HoldingStatus struct {
42+
IsHoldingSomething bool
43+
Meta map[string]interface{}
44+
}
45+
3946
// A Gripper represents a physical robotic gripper.
4047
// For more information, see the [gripper component docs].
4148
//
@@ -74,6 +81,9 @@ type Gripper interface {
7481
// returns true if we grabbed something.
7582
// This will block until done or a new operation cancels this one.
7683
Grab(ctx context.Context, extra map[string]interface{}) (bool, error)
84+
85+
// IsHoldingSomething returns whether the gripper is currently holding onto an object.
86+
IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (HoldingStatus, error)
7787
}
7888

7989
// FromRobot is a helper for getting the named Gripper from the given Robot.

components/gripper/server.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77

88
commonpb "go.viam.com/api/common/v1"
99
pb "go.viam.com/api/component/gripper/v1"
10+
"go.viam.com/utils/protoutils"
1011

1112
"go.viam.com/rdk/operation"
12-
"go.viam.com/rdk/protoutils"
13+
rprotoutils "go.viam.com/rdk/protoutils"
1314
"go.viam.com/rdk/referenceframe"
1415
"go.viam.com/rdk/resource"
1516
"go.viam.com/rdk/spatialmath"
@@ -77,6 +78,23 @@ func (s *serviceServer) IsMoving(ctx context.Context, req *pb.IsMovingRequest) (
7778
return &pb.IsMovingResponse{IsMoving: moving}, nil
7879
}
7980

81+
// IsHoldingSomething queries if the gripper has managed to grab something.
82+
func (s *serviceServer) IsHoldingSomething(ctx context.Context, req *pb.IsHoldingSomethingRequest) (*pb.IsHoldingSomethingResponse, error) {
83+
gripper, err := s.coll.Resource(req.GetName())
84+
if err != nil {
85+
return nil, err
86+
}
87+
holdingStatus, err := gripper.IsHoldingSomething(ctx, req.Extra.AsMap())
88+
if err != nil {
89+
return nil, err
90+
}
91+
meta, err := protoutils.StructToStructPb(holdingStatus.Meta)
92+
if err != nil {
93+
return nil, err
94+
}
95+
return &pb.IsHoldingSomethingResponse{IsHoldingSomething: holdingStatus.IsHoldingSomething, Meta: meta}, nil
96+
}
97+
8098
// DoCommand receives arbitrary commands.
8199
func (s *serviceServer) DoCommand(ctx context.Context,
82100
req *commonpb.DoCommandRequest,
@@ -85,7 +103,7 @@ func (s *serviceServer) DoCommand(ctx context.Context,
85103
if err != nil {
86104
return nil, err
87105
}
88-
return protoutils.DoFromResourceServer(ctx, gripper, req)
106+
return rprotoutils.DoFromResourceServer(ctx, gripper, req)
89107
}
90108

91109
func (s *serviceServer) GetGeometries(ctx context.Context, req *commonpb.GetGeometriesRequest) (*commonpb.GetGeometriesResponse, error) {

testutils/inject/gripper.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import (
1212
// Gripper is an injected gripper.
1313
type Gripper struct {
1414
gripper.Gripper
15-
name resource.Name
16-
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
17-
OpenFunc func(ctx context.Context, extra map[string]interface{}) error
18-
GrabFunc func(ctx context.Context, extra map[string]interface{}) (bool, error)
19-
StopFunc func(ctx context.Context, extra map[string]interface{}) error
20-
IsMovingFunc func(context.Context) (bool, error)
21-
CloseFunc func(ctx context.Context) error
22-
GeometriesFunc func(ctx context.Context) ([]spatialmath.Geometry, error)
23-
KinematicsFunc func(ctx context.Context) (referenceframe.Model, error)
15+
name resource.Name
16+
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
17+
OpenFunc func(ctx context.Context, extra map[string]interface{}) error
18+
GrabFunc func(ctx context.Context, extra map[string]interface{}) (bool, error)
19+
StopFunc func(ctx context.Context, extra map[string]interface{}) error
20+
IsHoldingSomethingFunc func(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error)
21+
IsMovingFunc func(context.Context) (bool, error)
22+
CloseFunc func(ctx context.Context) error
23+
GeometriesFunc func(ctx context.Context) ([]spatialmath.Geometry, error)
24+
KinematicsFunc func(ctx context.Context) (referenceframe.Model, error)
2425
}
2526

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

53+
// IsHoldingSomething calls the injected IsHoldingSomething or the real version.
54+
func (g *Gripper) IsHoldingSomething(ctx context.Context, extra map[string]interface{}) (gripper.HoldingStatus, error) {
55+
if g.IsHoldingSomethingFunc == nil {
56+
return g.Gripper.IsHoldingSomething(ctx, extra)
57+
}
58+
return g.IsHoldingSomethingFunc(ctx, extra)
59+
}
60+
5261
// Stop calls the injected Stop or the real version.
5362
func (g *Gripper) Stop(ctx context.Context, extra map[string]interface{}) error {
5463
if g.StopFunc == nil {

0 commit comments

Comments
 (0)