forked from equinor/flotilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPose.cs
229 lines (196 loc) · 5.34 KB
/
Pose.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
#nullable disable
using System.ComponentModel.DataAnnotations;
using Api.Mqtt.MessageModels;
using Api.Services.Models;
using Microsoft.EntityFrameworkCore;
namespace Api.Database.Models
{
[Owned]
public class Orientation
{
public Orientation()
{
X = 0;
Y = 0;
Z = 0;
W = 1;
}
public Orientation(Orientation copy)
{
X = copy.X;
Y = copy.Y;
Z = copy.Z;
W = copy.W;
}
public Orientation(float x = 0, float y = 0, float z = 0, float w = 1)
{
X = x;
Y = y;
Z = z;
W = w;
}
[Required]
public float X { get; set; }
[Required]
public float Y { get; set; }
[Required]
public float Z { get; set; }
[Required]
public float W { get; set; }
public override bool Equals(object obj)
{
if (obj is not Orientation orientation)
{
return false;
}
const float Tolerance = 1e-6F;
if (MathF.Abs(orientation.X - X) > Tolerance)
{
return false;
}
if (MathF.Abs(orientation.Y - Y) > Tolerance)
{
return false;
}
if (MathF.Abs(orientation.Z - Z) > Tolerance)
{
return false;
}
if (MathF.Abs(orientation.W - W) > Tolerance)
{
return false;
}
return true;
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
[Owned]
public class Position
{
public Position()
{
X = 0;
Y = 0;
Z = 0;
}
public Position(Position copy)
{
X = copy.X;
Y = copy.Y;
Z = copy.Z;
}
public Position(float x = 0, float y = 0, float z = 0)
{
X = x;
Y = y;
Z = z;
}
[Required]
public float X { get; set; }
[Required]
public float Y { get; set; }
[Required]
public float Z { get; set; }
public override bool Equals(object obj)
{
if (obj is not Position position)
{
return false;
}
const float Tolerance = 1e-6F;
if (MathF.Abs(position.X - X) > Tolerance)
{
return false;
}
if (MathF.Abs(position.Y - Y) > Tolerance)
{
return false;
}
if (MathF.Abs(position.Z - Z) > Tolerance)
{
return false;
}
return true;
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
[Owned]
public class Pose
{
public Pose()
{
Position = new Position();
Orientation = new Orientation();
}
public Pose(Pose copy)
{
Position = new Position(copy.Position);
Orientation = new Orientation(copy.Orientation);
}
public Pose(IsarPoseMqtt isarPose)
{
Position = new Position(isarPose.Position.X, isarPose.Position.Y, isarPose.Position.Z);
Orientation = new Orientation(
isarPose.Orientation.X,
isarPose.Orientation.Y,
isarPose.Orientation.Z,
isarPose.Orientation.W
);
}
public Pose(
float x_pos,
float y_pos,
float z_pos,
float x_ori,
float y_ori,
float z_ori,
float w
)
{
Position = new Position(x_pos, y_pos, z_pos);
Orientation = new Orientation(x_ori, y_ori, z_ori, w);
}
public Pose(EnuPosition enuPosition, float echoAngle)
{
Position = new Position(enuPosition.East, enuPosition.North, enuPosition.Up);
Orientation = AxisAngleToQuaternion(echoAngle);
}
public Pose(Position position, Orientation orientation)
{
Position = position;
Orientation = orientation;
}
[Required]
public Position Position { get; set; }
[Required]
public Orientation Orientation { get; set; }
// Since this is a ground robot the only quaternion vector
// that makes sense is up (0, 0, 1)
// Echo representes North at 0deg and increases this value clockwise
// Our representation has East at 0deg with rotations anti-clockwise
public Orientation AxisAngleToQuaternion(float echoAngle)
{
float angle;
echoAngle %= 2F * MathF.PI;
if (echoAngle < 0)
{
echoAngle += 2F * MathF.PI;
}
angle = (450 * MathF.PI / 180) - echoAngle;
var quaternion = new Orientation
{
X = 0,
Y = 0,
Z = MathF.Sin(angle / 2),
W = MathF.Cos(angle / 2),
};
return quaternion;
}
}
}