-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
124 lines (110 loc) · 3.05 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Package gantry contains a gRPC based gantry client.
package gantry
import (
"context"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/gantry/v1"
"go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/referenceframe"
)
// client implements GantryServiceClient.
type client struct {
name string
conn rpc.ClientConn
client pb.GantryServiceClient
logger golog.Logger
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) Gantry {
c := pb.NewGantryServiceClient(conn)
return &client{
name: name,
conn: conn,
client: c,
logger: logger,
}
}
func (c *client) Position(ctx context.Context, extra map[string]interface{}) ([]float64, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
resp, err := c.client.GetPosition(ctx, &pb.GetPositionRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return nil, err
}
return resp.PositionsMm, nil
}
func (c *client) Lengths(ctx context.Context, extra map[string]interface{}) ([]float64, error) {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return nil, err
}
lengths, err := c.client.GetLengths(ctx, &pb.GetLengthsRequest{
Name: c.name,
Extra: ext,
})
if err != nil {
return nil, err
}
return lengths.LengthsMm, nil
}
func (c *client) MoveToPosition(
ctx context.Context,
positionsMm []float64,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
worldStateMsg, err := referenceframe.WorldStateToProtobuf(worldState)
if err != nil {
return err
}
_, err = c.client.MoveToPosition(ctx, &pb.MoveToPositionRequest{
Name: c.name,
PositionsMm: positionsMm,
WorldState: worldStateMsg,
Extra: ext,
})
return err
}
func (c *client) Stop(ctx context.Context, extra map[string]interface{}) error {
ext, err := protoutils.StructToStructPb(extra)
if err != nil {
return err
}
_, err = c.client.Stop(ctx, &pb.StopRequest{Name: c.name, Extra: ext})
return err
}
func (c *client) ModelFrame() referenceframe.Model {
// TODO(erh): this feels wrong
return nil
}
func (c *client) CurrentInputs(ctx context.Context) ([]referenceframe.Input, error) {
res, err := c.Position(ctx, nil)
if err != nil {
return nil, err
}
return referenceframe.FloatsToInputs(res), nil
}
func (c *client) GoToInputs(ctx context.Context, goal []referenceframe.Input) error {
return c.MoveToPosition(ctx, referenceframe.InputsToFloats(goal), &referenceframe.WorldState{}, nil)
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return generic.DoFromConnection(ctx, c.conn, c.name, cmd)
}
func (c *client) IsMoving(ctx context.Context) (bool, error) {
resp, err := c.client.IsMoving(ctx, &pb.IsMovingRequest{Name: c.name})
if err != nil {
return false, err
}
return resp.IsMoving, nil
}