-
Notifications
You must be signed in to change notification settings - Fork 110
/
servo.go
91 lines (78 loc) · 2.79 KB
/
servo.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
package servo
import (
"context"
pb "go.viam.com/api/component/servo/v1"
"go.viam.com/rdk/data"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/robot"
)
func init() {
resource.RegisterAPI(API, resource.APIRegistration[Servo]{
Status: resource.StatusFunc(CreateStatus),
RPCServiceServerConstructor: NewRPCServiceServer,
RPCServiceHandler: pb.RegisterServoServiceHandlerFromEndpoint,
RPCServiceDesc: &pb.ServoService_ServiceDesc,
RPCClient: NewClientFromConn,
})
data.RegisterCollector(data.MethodMetadata{
API: API,
MethodName: position.String(),
}, newPositionCollector)
}
// SubtypeName is a constant that identifies the component resource API string "servo".
const SubtypeName = "servo"
// API is a variable that identifies the component resource API.
var API = resource.APINamespaceRDK.WithComponentType(SubtypeName)
// A Servo represents a physical servo connected to a board.
//
// Move example:
//
// // Move the servo from its origin to the desired angle of 30 degrees.
// myServoComponent.Move(context.Background(), 30, nil)
//
// Position example:
//
// // Get the current set angle of the servo.
// pos1, err := myServoComponent.Position(context.Background(), nil)
//
// // Move the servo from its origin to the desired angle of 20 degrees.
// myServoComponent.Move(context.Background(), 20, nil)
//
// // Get the current set angle of the servo.
// pos2, err := myServoComponent.Position(context.Background(), nil)
//
// logger.Info("Position 1: ", pos1)
// logger.Info("Position 2: ", pos2)
type Servo interface {
resource.Resource
resource.Actuator
// Move moves the servo to the given angle (0-180 degrees).
// This will block until done or a new operation cancels this one.
Move(ctx context.Context, angleDeg uint32, extra map[string]interface{}) error
// Position returns the current set angle (degrees) of the servo.
Position(ctx context.Context, extra map[string]interface{}) (uint32, error)
}
// Named is a helper for getting the named Servo's typed resource name.
func Named(name string) resource.Name {
return resource.NewName(API, name)
}
// FromRobot is a helper for getting the named servo from the given Robot.
func FromRobot(r robot.Robot, name string) (Servo, error) {
return robot.ResourceFromRobot[Servo](r, Named(name))
}
// NamesFromRobot is a helper for getting all servo names from the given Robot.
func NamesFromRobot(r robot.Robot) []string {
return robot.NamesByAPI(r, API)
}
// CreateStatus creates a status from the servo.
func CreateStatus(ctx context.Context, s Servo) (*pb.Status, error) {
position, err := s.Position(ctx, nil)
if err != nil {
return nil, err
}
isMoving, err := s.IsMoving(ctx)
if err != nil {
return nil, err
}
return &pb.Status{PositionDeg: position, IsMoving: isMoving}, nil
}