-
Notifications
You must be signed in to change notification settings - Fork 110
/
server.go
168 lines (153 loc) · 4.22 KB
/
server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Package base contains a gRPC based arm service server.
package base
import (
"context"
"github.com/pkg/errors"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/base/v1"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/subtype"
)
// subtypeServer implements the BaseService from base.proto.
type subtypeServer struct {
pb.UnimplementedBaseServiceServer
s subtype.Service
}
// NewServer constructs a base gRPC service server.
func NewServer(s subtype.Service) pb.BaseServiceServer {
return &subtypeServer{s: s}
}
// getBase returns the base specified or nil.
func (s *subtypeServer) getBase(name string) (Base, error) {
resource := s.s.Resource(name)
if resource == nil {
return nil, errors.Errorf("no base with name (%s)", name)
}
base, ok := resource.(Base)
if !ok {
return nil, errors.Errorf("resource with name (%s) is not a base", name)
}
return base, nil
}
// MoveStraight moves a robot's base in a straight line by a given distance, expressed in millimeters
// and a given speed, expressed in millimeters per second.
func (s *subtypeServer) MoveStraight(
ctx context.Context,
req *pb.MoveStraightRequest,
) (*pb.MoveStraightResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
mmPerSec := 500.0 // TODO(erh): this is probably the wrong default
reqMmPerSec := req.GetMmPerSec()
if reqMmPerSec != 0 {
mmPerSec = reqMmPerSec
}
err = base.MoveStraight(ctx, int(req.DistanceMm), mmPerSec, req.Extra.AsMap())
if err != nil {
return nil, err
}
return &pb.MoveStraightResponse{}, nil
}
// Spin spins a robot's base by an given angle, expressed in degrees, and a given
// angular speed, expressed in degrees per second.
func (s *subtypeServer) Spin(
ctx context.Context,
req *pb.SpinRequest,
) (*pb.SpinResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
degsPerSec := 64.0
reqDegsPerSec := req.GetDegsPerSec()
if reqDegsPerSec != 0 {
degsPerSec = reqDegsPerSec
}
err = base.Spin(ctx, req.GetAngleDeg(), degsPerSec, req.Extra.AsMap())
if err != nil {
return nil, err
}
return &pb.SpinResponse{}, nil
}
func (s *subtypeServer) SetPower(
ctx context.Context,
req *pb.SetPowerRequest,
) (*pb.SetPowerResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
err = base.SetPower(
ctx,
protoutils.ConvertVectorProtoToR3(req.GetLinear()),
protoutils.ConvertVectorProtoToR3(req.GetAngular()),
req.Extra.AsMap(),
)
if err != nil {
return nil, err
}
return &pb.SetPowerResponse{}, nil
}
func (s *subtypeServer) SetVelocity(
ctx context.Context,
req *pb.SetVelocityRequest,
) (*pb.SetVelocityResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
err = base.SetVelocity(
ctx,
protoutils.ConvertVectorProtoToR3(req.GetLinear()),
protoutils.ConvertVectorProtoToR3(req.GetAngular()),
req.Extra.AsMap(),
)
if err != nil {
return nil, err
}
return &pb.SetVelocityResponse{}, nil
}
// Stop stops a robot's base.
func (s *subtypeServer) Stop(
ctx context.Context,
req *pb.StopRequest,
) (*pb.StopResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
if err = base.Stop(ctx, req.Extra.AsMap()); err != nil {
return nil, err
}
return &pb.StopResponse{}, nil
}
// IsMoving queries of a component is in motion.
func (s *subtypeServer) IsMoving(ctx context.Context, req *pb.IsMovingRequest) (*pb.IsMovingResponse, error) {
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
moving, err := base.IsMoving(ctx)
if err != nil {
return nil, err
}
return &pb.IsMovingResponse{IsMoving: moving}, nil
}
// DoCommand receives arbitrary commands.
func (s *subtypeServer) DoCommand(ctx context.Context,
req *commonpb.DoCommandRequest,
) (*commonpb.DoCommandResponse, error) {
base, err := s.getBase(req.GetName())
if err != nil {
return nil, err
}
return protoutils.DoFromResourceServer(ctx, base, req)
}