-
Notifications
You must be signed in to change notification settings - Fork 109
/
motor.go
124 lines (109 loc) · 4.5 KB
/
motor.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 inject
import (
"context"
"go.viam.com/rdk/components/motor"
)
// Motor is an injected motor.
type Motor struct {
motor.Motor
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
SetPowerFunc func(ctx context.Context, powerPct float64, extra map[string]interface{}) error
GoForFunc func(ctx context.Context, rpm, rotations float64, extra map[string]interface{}) error
GoToFunc func(ctx context.Context, rpm, position float64, extra map[string]interface{}) error
ResetZeroPositionFunc func(ctx context.Context, offset float64, extra map[string]interface{}) error
PositionFunc func(ctx context.Context, extra map[string]interface{}) (float64, error)
PropertiesFunc func(ctx context.Context, extra map[string]interface{}) (map[motor.Feature]bool, error)
StopFunc func(ctx context.Context, extra map[string]interface{}) error
IsPoweredFunc func(ctx context.Context, extra map[string]interface{}) (bool, float64, error)
}
// SetPower calls the injected Power or the real version.
func (m *Motor) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error {
if m.SetPowerFunc == nil {
return m.Motor.SetPower(ctx, powerPct, extra)
}
return m.SetPowerFunc(ctx, powerPct, extra)
}
// GoFor calls the injected GoFor or the real version.
func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
if m.GoForFunc == nil {
return m.Motor.GoFor(ctx, rpm, revolutions, extra)
}
return m.GoForFunc(ctx, rpm, revolutions, extra)
}
// GoTo calls the injected GoTo or the real version.
func (m *Motor) GoTo(ctx context.Context, rpm, positionRevolutions float64, extra map[string]interface{}) error {
if m.GoToFunc == nil {
return m.Motor.GoTo(ctx, rpm, positionRevolutions, extra)
}
return m.GoToFunc(ctx, rpm, positionRevolutions, extra)
}
// ResetZeroPosition calls the injected Zero or the real version.
func (m *Motor) ResetZeroPosition(ctx context.Context, offset float64, extra map[string]interface{}) error {
if m.ResetZeroPositionFunc == nil {
return m.Motor.ResetZeroPosition(ctx, offset, extra)
}
return m.ResetZeroPositionFunc(ctx, offset, extra)
}
// Position calls the injected Position or the real version.
func (m *Motor) Position(ctx context.Context, extra map[string]interface{}) (float64, error) {
if m.PositionFunc == nil {
return m.Motor.Position(ctx, extra)
}
return m.PositionFunc(ctx, extra)
}
// Properties calls the injected Properties or the real version.
func (m *Motor) Properties(ctx context.Context, extra map[string]interface{}) (map[motor.Feature]bool, error) {
if m.PropertiesFunc == nil {
return m.Motor.Properties(ctx, extra)
}
return m.PropertiesFunc(ctx, extra)
}
// Stop calls the injected Off or the real version.
func (m *Motor) Stop(ctx context.Context, extra map[string]interface{}) error {
if m.StopFunc == nil {
return m.Motor.Stop(ctx, extra)
}
return m.StopFunc(ctx, extra)
}
// IsPowered calls the injected IsPowered or the real version.
func (m *Motor) IsPowered(ctx context.Context, extra map[string]interface{}) (bool, float64, error) {
if m.IsPoweredFunc == nil {
return m.Motor.IsPowered(ctx, extra)
}
return m.IsPoweredFunc(ctx, extra)
}
// DoCommand calls the injected DoCommand or the real version.
func (m *Motor) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
if m.DoFunc == nil {
return m.Motor.DoCommand(ctx, cmd)
}
return m.DoFunc(ctx, cmd)
}
// LocalMotor is an injected motor that supports additional features provided by RDK
// (e.g. GoTillStop).
type LocalMotor struct {
Motor
GoTillStopFunc func(ctx context.Context, rpm float64, stopFunc func(ctx context.Context) bool) error
IsMovingFunc func(context.Context) (bool, error)
}
// GoTillStop calls the injected GoTillStop or the real version.
func (m *LocalMotor) GoTillStop(
ctx context.Context, rpm float64,
stopFunc func(ctx context.Context) bool,
) error {
if m.GoTillStopFunc == nil {
stoppableMotor, ok := m.Motor.Motor.(motor.LocalMotor)
if !ok {
return motor.NewGoTillStopUnsupportedError("(name unavailable)")
}
return stoppableMotor.GoTillStop(ctx, rpm, stopFunc)
}
return m.GoTillStopFunc(ctx, rpm, stopFunc)
}
// IsMoving calls the injected IsMoving or the real version.
func (m *LocalMotor) IsMoving(ctx context.Context) (bool, error) {
if m.IsMovingFunc == nil {
return m.Motor.Motor.(motor.LocalMotor).IsMoving(ctx)
}
return m.IsMovingFunc(ctx)
}