-
Notifications
You must be signed in to change notification settings - Fork 109
/
server.go
177 lines (156 loc) · 5.1 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
169
170
171
172
173
174
175
176
177
// Package motor contains a gRPC based motor service server
package motor
import (
"context"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/motor/v1"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
type serviceServer struct {
pb.UnimplementedMotorServiceServer
coll resource.APIResourceCollection[Motor]
}
// NewRPCServiceServer constructs a motor gRPC service server.
// It is intentionally untyped to prevent use outside of tests.
func NewRPCServiceServer(coll resource.APIResourceCollection[Motor]) interface{} {
return &serviceServer{coll: coll}
}
// SetPower sets the percentage of power the motor of the underlying robot should employ between 0-1.
func (server *serviceServer) SetPower(
ctx context.Context,
req *pb.SetPowerRequest,
) (*pb.SetPowerResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
return &pb.SetPowerResponse{}, motor.SetPower(ctx, req.GetPowerPct(), req.Extra.AsMap())
}
// GoFor requests the motor of the underlying robot to go for a certain amount based off
// the request.
func (server *serviceServer) GoFor(
ctx context.Context,
req *pb.GoForRequest,
) (*pb.GoForResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
return &pb.GoForResponse{}, motor.GoFor(ctx, req.GetRpm(), req.GetRevolutions(), req.Extra.AsMap())
}
// GetPosition reports the position of the motor of the underlying robot
// based on its encoder. If it's not supported, the returned data is undefined.
// The unit returned is the number of revolutions which is intended to be fed
// back into calls of GoFor.
func (server *serviceServer) GetPosition(
ctx context.Context,
req *pb.GetPositionRequest,
) (*pb.GetPositionResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
pos, err := motor.Position(ctx, req.Extra.AsMap())
if err != nil {
return nil, err
}
return &pb.GetPositionResponse{Position: pos}, nil
}
// GetProperties returns a message of booleans indicating which optional features the robot's motor supports.
func (server *serviceServer) GetProperties(
ctx context.Context,
req *pb.GetPropertiesRequest,
) (*pb.GetPropertiesResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
features, err := motor.Properties(ctx, req.Extra.AsMap())
if err != nil {
return nil, err
}
return FeatureMapToProtoResponse(features)
}
// Stop turns the motor of the underlying robot off.
func (server *serviceServer) Stop(
ctx context.Context,
req *pb.StopRequest,
) (*pb.StopResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
return &pb.StopResponse{}, motor.Stop(ctx, req.Extra.AsMap())
}
// IsPowered returns whether or not the motor of the underlying robot is currently on.
func (server *serviceServer) IsPowered(
ctx context.Context,
req *pb.IsPoweredRequest,
) (*pb.IsPoweredResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
isOn, powerPct, err := motor.IsPowered(ctx, req.Extra.AsMap())
if err != nil {
return nil, err
}
return &pb.IsPoweredResponse{IsOn: isOn, PowerPct: powerPct}, nil
}
// GoTo requests the motor of the underlying robot to go a specific position.
func (server *serviceServer) GoTo(
ctx context.Context,
req *pb.GoToRequest,
) (*pb.GoToResponse, error) {
operation.CancelOtherWithLabel(ctx, req.GetName())
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
return &pb.GoToResponse{}, motor.GoTo(ctx, req.GetRpm(), req.GetPositionRevolutions(), req.Extra.AsMap())
}
// ResetZeroPosition sets the current position of the motor specified by the request
// (adjusted by a given offset) to be its new zero position.
func (server *serviceServer) ResetZeroPosition(
ctx context.Context,
req *pb.ResetZeroPositionRequest,
) (*pb.ResetZeroPositionResponse, error) {
motorName := req.GetName()
motor, err := server.coll.Resource(motorName)
if err != nil {
return nil, err
}
return &pb.ResetZeroPositionResponse{}, motor.ResetZeroPosition(ctx, req.GetOffset(), req.Extra.AsMap())
}
// IsMoving queries of a component is in motion.
func (server *serviceServer) IsMoving(ctx context.Context, req *pb.IsMovingRequest) (*pb.IsMovingResponse, error) {
motor, err := server.coll.Resource(req.GetName())
if err != nil {
return nil, err
}
moving, err := motor.IsMoving(ctx)
if err != nil {
return nil, err
}
return &pb.IsMovingResponse{IsMoving: moving}, nil
}
// DoCommand receives arbitrary commands.
func (server *serviceServer) DoCommand(ctx context.Context,
req *commonpb.DoCommandRequest,
) (*commonpb.DoCommandResponse, error) {
motor, err := server.coll.Resource(req.GetName())
if err != nil {
return nil, err
}
return protoutils.DoFromResourceServer(ctx, motor, req)
}