-
Notifications
You must be signed in to change notification settings - Fork 110
/
sensor.go
182 lines (172 loc) · 4.11 KB
/
sensor.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
package protoutils
import (
"github.com/golang/geo/r3"
geo "github.com/kellydunn/golang-geo"
"google.golang.org/protobuf/types/known/structpb"
"go.viam.com/rdk/spatialmath"
)
const (
typeAngularVelocity = "angular_velocity"
typeVector3 = "vector3"
typeEuler = "euler"
typeQuat = "quat"
typeGeopoint = "geopoint"
typeOrientationVector = "orientation_vector_radians"
typeOrientationVectorDegrees = "orientation_vector_degrees"
typeAxisAngle = "r4aa"
)
func goToProto(v interface{}) (*structpb.Value, error) {
switch x := v.(type) {
case spatialmath.AngularVelocity:
v = map[string]interface{}{
"x": x.X,
"y": x.Y,
"z": x.Z,
"_type": typeAngularVelocity,
}
case r3.Vector:
v = map[string]interface{}{
"x": x.X,
"y": x.Y,
"z": x.Z,
"_type": typeVector3,
}
case *spatialmath.EulerAngles:
v = map[string]interface{}{
"roll": x.Roll,
"pitch": x.Pitch,
"yaw": x.Yaw,
"_type": typeEuler,
}
case *spatialmath.Quaternion:
v = map[string]interface{}{
"r": x.Real,
"i": x.Imag,
"j": x.Jmag,
"k": x.Kmag,
"_type": typeQuat,
}
case *spatialmath.OrientationVector:
v = map[string]interface{}{
"theta": x.Theta,
"ox": x.OX,
"oy": x.OY,
"oz": x.OZ,
"_type": typeOrientationVector,
}
case *spatialmath.OrientationVectorDegrees:
v = map[string]interface{}{
"theta": x.Theta,
"ox": x.OX,
"oy": x.OY,
"oz": x.OZ,
"_type": typeOrientationVectorDegrees,
}
case *spatialmath.R4AA:
v = map[string]interface{}{
"theta": x.Theta,
"rx": x.RX,
"ry": x.RY,
"rz": x.RZ,
"_type": typeAxisAngle,
}
case spatialmath.Orientation:
deg := x.OrientationVectorDegrees()
v = map[string]interface{}{
"theta": deg.Theta,
"ox": deg.OX,
"oy": deg.OY,
"oz": deg.OZ,
"_type": typeOrientationVectorDegrees,
}
case *geo.Point:
v = map[string]interface{}{
"lat": x.Lat(),
"lng": x.Lng(),
"_type": typeGeopoint,
}
}
return structpb.NewValue(v)
}
// ReadingGoToProto converts go readings to proto readings.
func ReadingGoToProto(readings map[string]interface{}) (map[string]*structpb.Value, error) {
m := map[string]*structpb.Value{}
for k, v := range readings {
vv, err := goToProto(v)
if err != nil {
return nil, err
}
m[k] = vv
}
return m, nil
}
// ReadingProtoToGo converts proto readings to go readings.
func ReadingProtoToGo(readings map[string]*structpb.Value) (map[string]interface{}, error) {
m := map[string]interface{}{}
for k, v := range readings {
m[k] = cleanSensorType(v.AsInterface())
}
return m, nil
}
func cleanSensorType(v interface{}) interface{} {
switch x := v.(type) {
case map[string]interface{}:
switch x["_type"] {
case typeAngularVelocity:
return spatialmath.AngularVelocity{
X: x["x"].(float64),
Y: x["y"].(float64),
Z: x["z"].(float64),
}
case typeVector3:
return r3.Vector{
X: x["x"].(float64),
Y: x["y"].(float64),
Z: x["z"].(float64),
}
case typeEuler:
return &spatialmath.EulerAngles{
Roll: x["roll"].(float64),
Pitch: x["pitch"].(float64),
Yaw: x["yaw"].(float64),
}
case typeQuat:
return &spatialmath.Quaternion{
x["r"].(float64),
x["i"].(float64),
x["j"].(float64),
x["k"].(float64),
}
case typeOrientationVector:
return &spatialmath.OrientationVector{
Theta: x["theta"].(float64),
OX: x["ox"].(float64),
OY: x["oy"].(float64),
OZ: x["oz"].(float64),
}
case typeOrientationVectorDegrees:
return &spatialmath.OrientationVectorDegrees{
Theta: x["theta"].(float64),
OX: x["ox"].(float64),
OY: x["oy"].(float64),
OZ: x["oz"].(float64),
}
case typeAxisAngle:
return &spatialmath.R4AA{
Theta: x["theta"].(float64),
RX: x["rx"].(float64),
RY: x["ry"].(float64),
RZ: x["rz"].(float64),
}
case typeGeopoint:
return geo.NewPoint(
x["lat"].(float64),
x["lng"].(float64),
)
default:
return v
}
default:
return v
}
}