-
Notifications
You must be signed in to change notification settings - Fork 110
/
slam.go
217 lines (194 loc) · 6.52 KB
/
slam.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Package slam implements simultaneous localization and mapping.
// This is an Experimental package.
// For more information, see the [SLAM service docs].
//
// [SLAM service docs]: https://docs.viam.com/services/slam/
package slam
import (
"bytes"
"context"
"io"
"github.com/pkg/errors"
"go.opencensus.io/trace"
pb "go.viam.com/api/service/slam/v1"
"go.viam.com/rdk/data"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/robot"
"go.viam.com/rdk/spatialmath"
)
// TBD 05/04/2022: Needs more work once GRPC is included (future PR).
func init() {
resource.RegisterAPI(API, resource.APIRegistration[Service]{
RPCServiceServerConstructor: NewRPCServiceServer,
RPCServiceHandler: pb.RegisterSLAMServiceHandlerFromEndpoint,
RPCServiceDesc: &pb.SLAMService_ServiceDesc,
RPCClient: NewClientFromConn,
})
data.RegisterCollector(data.MethodMetadata{
API: API,
MethodName: position.String(),
}, newPositionCollector)
data.RegisterCollector(data.MethodMetadata{
API: API,
MethodName: pointCloudMap.String(),
}, newPointCloudMapCollector)
}
// SubtypeName is the name of the type of service.
const (
SubtypeName = "slam"
MappingModeNewMap = MappingMode(iota)
MappingModeLocalizationOnly
MappingModeUpdateExistingMap
)
func (t MappingMode) String() string {
switch t {
case MappingModeNewMap:
return "mapping mode"
case MappingModeLocalizationOnly:
return "localizing only mode"
case MappingModeUpdateExistingMap:
return "updating mode"
default:
return "unspecified mode"
}
}
// SensorTypeCamera is a camera sensor.
const (
SensorTypeCamera = SensorType(iota)
SensorTypeMovementSensor
)
func (t SensorType) String() string {
switch t {
case SensorTypeCamera:
return "camera"
case SensorTypeMovementSensor:
return "movement sensor"
default:
return "unsupported sensor type"
}
}
// API is a variable that identifies the slam resource API.
var API = resource.APINamespaceRDK.WithServiceType(SubtypeName)
// MappingMode describes what mapping mode the slam service is in, including
// creating a new map, localizing on an existing map or updating an existing map.
type MappingMode uint8
// SensorType describes what sensor type the sensor is, including
// camera or movement sensor.
type SensorType uint8
// SensorInfo holds information about the sensor name and sensor type.
type SensorInfo struct {
Name string
Type SensorType
}
// Properties returns various information regarding the current slam service,
// including whether the slam process is running in the cloud and its mapping mode.
type Properties struct {
CloudSlam bool
MappingMode MappingMode
InternalStateFileType string
SensorInfo []SensorInfo
}
// Named is a helper for getting the named service's typed resource name.
func Named(name string) resource.Name {
return resource.NewName(API, name)
}
// FromRobot is a helper for getting the named SLAM service from the given Robot.
func FromRobot(r robot.Robot, name string) (Service, error) {
return robot.ResourceFromRobot[Service](r, Named(name))
}
// FromDependencies is a helper for getting the named SLAM service from a collection of
// dependencies.
func FromDependencies(deps resource.Dependencies, name string) (Service, error) {
return resource.FromDependencies[Service](deps, Named(name))
}
// Service describes the functions that are available to the service.
//
// The Go SDK implements helper functions that concatenate streaming
// responses. Some of the following examples use corresponding
// helper methods instead of interface methods.
// For more information, see the [SLAM service docs].
//
// Position example:
//
// // Get the current position of the specified source component
// // in the SLAM map as a Pose.
// pos, name, err := mySLAMService.Position(context.Background())
//
// PointCloudMap example (using PointCloudMapFull helper method):
//
// // Get the point cloud map in standard PCD format.
// pcdMapBytes, err := PointCloudMapFull(
// context.Background(), mySLAMService, true)
//
// InternalState example (using InternalStateFull helper method):
//
// // Get the internal state of the SLAM algorithm required
// // to continue mapping/localization.
// internalStateBytes, err := InternalStateFull(
// context.Background(), mySLAMService)
//
// Properties example:
//
// // Get the properties of your current SLAM session
// properties, err := mySLAMService.Properties(context.Background())
//
// [SLAM service docs]: https://docs.viam.com/services/slam/
type Service interface {
resource.Resource
Position(ctx context.Context) (spatialmath.Pose, error)
PointCloudMap(ctx context.Context, returnEditedMap bool) (func() ([]byte, error), error)
InternalState(ctx context.Context) (func() ([]byte, error), error)
Properties(ctx context.Context) (Properties, error)
}
// HelperConcatenateChunksToFull concatenates the chunks from a streamed grpc endpoint.
func HelperConcatenateChunksToFull(f func() ([]byte, error)) ([]byte, error) {
var fullBytes []byte
for {
chunk, err := f()
if errors.Is(err, io.EOF) {
return fullBytes, nil
}
if err != nil {
return nil, err
}
fullBytes = append(fullBytes, chunk...)
}
}
// PointCloudMapFull concatenates the streaming responses from PointCloudMap into a full point cloud.
func PointCloudMapFull(ctx context.Context, slamSvc Service, returnEditedMap bool) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "slam::PointCloudMapFull")
defer span.End()
callback, err := slamSvc.PointCloudMap(ctx, returnEditedMap)
if err != nil {
return nil, err
}
return HelperConcatenateChunksToFull(callback)
}
// InternalStateFull concatenates the streaming responses from InternalState into
// the internal serialized state of the slam algorithm.
func InternalStateFull(ctx context.Context, slamSvc Service) ([]byte, error) {
ctx, span := trace.StartSpan(ctx, "slam::InternalStateFull")
defer span.End()
callback, err := slamSvc.InternalState(ctx)
if err != nil {
return nil, err
}
return HelperConcatenateChunksToFull(callback)
}
// Limits returns the bounds of the slam map as a list of referenceframe.Limits.
func Limits(ctx context.Context, svc Service, useEditedMap bool) ([]referenceframe.Limit, error) {
data, err := PointCloudMapFull(ctx, svc, useEditedMap)
if err != nil {
return nil, err
}
dims, err := pointcloud.GetPCDMetaData(bytes.NewReader(data))
if err != nil {
return nil, err
}
return []referenceframe.Limit{
{Min: dims.MinX, Max: dims.MaxX},
{Min: dims.MinY, Max: dims.MaxY},
}, nil
}