Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JCompass.hh
Go to the documentation of this file.
1#ifndef __JDETECTOR__JCOMPASS__
2#define __JDETECTOR__JCOMPASS__
3
4#include <istream>
5#include <ostream>
6#include <cmath>
7#include <limits>
8
9#include "JLang/JEquals.hh"
10#include "JDB/JAHRS.hh"
12#include "JIO/JSerialisable.hh"
15
16
17/**
18 * \author mdejong
19 */
20
21namespace JDETECTOR {}
22namespace JPP { using namespace JDETECTOR; }
23
24namespace JDETECTOR {
25
26 using JLANG::JEquals;
27 using JDATABASE::JAHRS;
31 using JIO::JReader;
32 using JIO::JWriter;
33
34
35 /**
36 * Data structure for compass in three dimensions.
37 *
38 * \image html compass.png "Definitions of yaw, pitch and roll."
39 *
40 * Note that the z-axis of the %KM3NeT reference system points up.\n
41 * So, the yaw angle, measured by the compass is measured from North to East
42 * (since in the compass system z-axis points down).\n
43 * Note also that the sign of the angle JCompass::pitch is maintained and
44 * the signs of the angles JCompass::yaw and JCompass::roll inverted
45 * when converting to/from a rotation matrix or quaternion.
46 *
47 * This class implements the JMATH::JMath and JLANG::JEquals interfaces.
48 */
49 class JCompass :
50 public JEquals<JCompass>
51 {
52 public:
53 /**
54 * Default constructor.
55 */
57 yaw (0.0),
58 pitch(0.0),
59 roll (0.0)
60 {}
61
62
63 /**
64 * Constructor.
65 *
66 * \param yaw yaw angle [rad]
67 * \param pitch pitch angle [rad]
68 * \param roll roll angle [rad]
69 */
70 JCompass(const double yaw,
71 const double pitch,
72 const double roll) :
73 yaw (yaw),
74 pitch(pitch),
75 roll (roll)
76 {}
77
78
79 /**
80 * Constructor.
81 *
82 * \param data AHRS data
83 * \param calibration AHRS calibration
84 */
85 JCompass(const JAHRS& data, const JAHRSCalibration& calibration)
86 {
87 using namespace std;
88 using namespace JPP;
89
90 double A0 = data.AHRS_A0;
91 double A1 = data.AHRS_A1;
92 double A2 = data.AHRS_A2;
93
94 double H0 = data.AHRS_H0;
95 double H1 = data.AHRS_H1;
96 double H2 = data.AHRS_H2;
97
98 A0 -= calibration.ACC_OFFSET_X;
99 A1 -= calibration.ACC_OFFSET_Y;
100 A2 -= calibration.ACC_OFFSET_Z;
101
102 JMatrix3D(calibration.ACC_ROT_XX, calibration.ACC_ROT_XY, calibration.ACC_ROT_XZ,
103 calibration.ACC_ROT_YX, calibration.ACC_ROT_YY, calibration.ACC_ROT_YZ,
104 calibration.ACC_ROT_ZX, calibration.ACC_ROT_ZY, calibration.ACC_ROT_ZZ).transform(A0, A1, A2);
105
106 H0 -= 0.5 * (calibration.MAG_XMIN + calibration.MAG_XMAX);
107 H1 -= 0.5 * (calibration.MAG_YMIN + calibration.MAG_YMAX);
108 H2 -= 0.5 * (calibration.MAG_ZMIN + calibration.MAG_ZMAX);
109
110 JMatrix3D(calibration.MAG_ROT_XX, calibration.MAG_ROT_XY, calibration.MAG_ROT_XZ,
111 calibration.MAG_ROT_YX, calibration.MAG_ROT_YY, calibration.MAG_ROT_YZ,
112 calibration.MAG_ROT_ZX, calibration.MAG_ROT_ZY, calibration.MAG_ROT_ZZ).transform(H0, H1, H2);
113
114 // from CLB->DOM (rotation around X) + from nautical (X-North, Y-East, Z-down) to UTM-like (X: East, Y: North, Z:up)
115
116 double prev0 = A0;
117 A0 = -A1;
118 A1 = prev0;
119 A2 = A2;
120
121 prev0 = H0;
122 H0 = -H1;
123 H1 = prev0;
124 H2 = H2;
125
126 this->roll = atan2(A1, A2);
127 this->pitch = atan2(-A0, sqrt(A1*A1 + A2*A2));
128 this->yaw = atan2(H0 * cos(pitch) + H1 * sin(pitch) * sin(roll) + H2 * sin(pitch) * cos(roll), H1 * cos(pitch) - H2 * sin(pitch) );
129 }
130
131
132 /**
133 * Constructor.
134 *
135 * \param Q quaternion
136 */
138 yaw (0.0),
139 pitch(0.0),
140 roll (0.0)
141 {
142 using namespace std;
143
144 this->yaw = atan2(2.0*(Q.getA()*Q.getD() + Q.getB()*Q.getC()), 1.0 - 2.0*(Q.getC()*Q.getC() + Q.getD()*Q.getD()));
145
146 double tp = 2.0*(Q.getA()*Q.getC() - Q.getB()*Q.getD());
147 this->pitch = 2.0*atan2(sqrt(1.0 + tp), sqrt(1.0 - tp) ) - M_PI / 2.0;
148
149 this->roll = atan2(2.0*(Q.getA()*Q.getB() + Q.getC()*Q.getD()), 1.0 - 2.0*(Q.getB()*Q.getB() + Q.getC()*Q.getC()));
150 }
151
152
153 /**
154 * Get compass.
155 *
156 * \return this compass
157 */
158 const JCompass& getCompass() const
159 {
160 return static_cast<const JCompass&>(*this);
161 }
162
163
164 /**
165 * Set compass.
166 *
167 * \param compass compass
168 */
169 void setCompass(const JCompass& compass)
170 {
171 static_cast<JCompass&>(*this) = compass;
172 }
173
174
175 /**
176 * Get rotation matrix.
177 * This is active rotation matrix taken from passive meausurements.
178 *
179 * \return rotation matrix
180 */
182 {
183 using namespace JPP;
184
185 const JRotation3D Rx = JRotation3X(+this->getRoll());
186 const JRotation3D Ry = JRotation3Y(+this->getPitch());
187 const JRotation3D Rz = JRotation3Z(+this->getYaw());
188
189 return (Rz * Ry * Rx);
190 }
191
192
193 /**
194 * Get quaternion.
195 *
196 * \return quaternion
197 */
199 {
200 using namespace JPP;
201
202 const double cy = cos(0.5 * yaw);
203 const double sy = sin(0.5 * yaw);
204 const double cp = cos(0.5 * pitch);
205 const double sp = sin(0.5 * pitch);
206 const double cr = cos(0.5 * roll);
207 const double sr = sin(0.5 * roll);
208
209 return JQuaternion3D(cr * cp * cy + sr * sp * sy,
210 sr * cp * cy - cr * sp * sy,
211 cr * sp * cy + sr * cp * sy,
212 cr * cp * sy - sr * sp * cy);
213 }
214
215
216 /**
217 * Get yaw compass.
218 *
219 * \return yaw compass
220 */
221 double getYaw() const
222 {
223 return yaw;
224 }
225
226
227 /**
228 * Get pitch compass.
229 *
230 * \return pitch compass
231 */
232 double getPitch() const
233 {
234 return pitch;
235 }
236
237
238 /**
239 * Get roll compass.
240 *
241 * \return roll compass
242 */
243 double getRoll() const
244 {
245 return roll;
246 }
247
248
249 /**
250 * Check equality.
251 *
252 * \param compass compass
253 * \param precision numerical precision
254 * \return true if compass's are equal; else false
255 */
256 bool equals(const JCompass& compass,
257 const double precision = std::numeric_limits<double>::min()) const
258 {
259 return (fabs(getYaw() - compass.getYaw()) <= precision &&
260 fabs(getPitch() - compass.getPitch()) <= precision &&
261 fabs(getRoll() - compass.getRoll()) <= precision);
262 }
263
264
265 /**
266 * Correct compass for magnetic declination and meridian convergence angle.
267 *
268 * \param declination magnetic declination [rad]
269 * \param meridian meridian convergence angle [rad]
270 */
271 void correct(const double declination, const double meridian)
272 {
273 this->yaw -= declination;
274 this->yaw += meridian;
275 }
276
277
278 /**
279 * Read compasss from input.
280 *
281 * \param in input stream
282 * \param compass compasss
283 * \return input stream
284 */
285 friend inline std::istream& operator>>(std::istream& in, JCompass& compass)
286 {
287 in >> compass.yaw >> compass.pitch >> compass.roll;
288
289 return in;
290 }
291
292
293 /**
294 * Write compasss to output.
295 *
296 * \param out output stream
297 * \param compass compass
298 * \return output stream
299 */
300 friend inline std::ostream& operator<<(std::ostream& out, const JCompass& compass)
301 {
302 out << compass.getYaw() << ' ' << compass.getPitch() << ' ' << compass.getRoll();
303
304 return out;
305 }
306
307
308 /**
309 * Read compasss from input.
310 *
311 * \param in reader
312 * \param compass compasss
313 * \return reader
314 */
315 friend inline JReader& operator>>(JReader& in, JCompass& compass)
316 {
317 in >> compass.yaw;
318 in >> compass.pitch;
319 in >> compass.roll;
320
321 return in;
322 }
323
324
325 /**
326 * Write compasss to output.
327 *
328 * \param out writer
329 * \param compass compasss
330 * \return writer
331 */
332 friend inline JWriter& operator<<(JWriter& out, const JCompass& compass)
333 {
334 out << compass.yaw;
335 out << compass.pitch;
336 out << compass.roll;
337
338 return out;
339 }
340
341
342 protected:
343 double yaw;
344 double pitch;
345 double roll;
346 };
347}
348
349#endif
Data structure for compass in three dimensions.
Definition JCompass.hh:51
double getYaw() const
Get yaw compass.
Definition JCompass.hh:221
const JCompass & getCompass() const
Get compass.
Definition JCompass.hh:158
friend std::istream & operator>>(std::istream &in, JCompass &compass)
Read compasss from input.
Definition JCompass.hh:285
void setCompass(const JCompass &compass)
Set compass.
Definition JCompass.hh:169
JCompass(const JQuaternion3D &Q)
Constructor.
Definition JCompass.hh:137
friend JWriter & operator<<(JWriter &out, const JCompass &compass)
Write compasss to output.
Definition JCompass.hh:332
bool equals(const JCompass &compass, const double precision=std::numeric_limits< double >::min()) const
Check equality.
Definition JCompass.hh:256
friend JReader & operator>>(JReader &in, JCompass &compass)
Read compasss from input.
Definition JCompass.hh:315
JCompass(const JAHRS &data, const JAHRSCalibration &calibration)
Constructor.
Definition JCompass.hh:85
JRotation3D getRotation() const
Get rotation matrix.
Definition JCompass.hh:181
friend std::ostream & operator<<(std::ostream &out, const JCompass &compass)
Write compasss to output.
Definition JCompass.hh:300
void correct(const double declination, const double meridian)
Correct compass for magnetic declination and meridian convergence angle.
Definition JCompass.hh:271
double getRoll() const
Get roll compass.
Definition JCompass.hh:243
JCompass(const double yaw, const double pitch, const double roll)
Constructor.
Definition JCompass.hh:70
JQuaternion3D getQuaternion() const
Get quaternion.
Definition JCompass.hh:198
JCompass()
Default constructor.
Definition JCompass.hh:56
double getPitch() const
Get pitch compass.
Definition JCompass.hh:232
Data structure for unit quaternion in three dimensions.
double getB() const
Get b value.
double getD() const
Get d value.
double getC() const
Get c value.
double getA() const
Get a value.
Rotation around X-axis.
Rotation around Y-axis.
Rotation around Z-axis.
Interface for binary input.
Interface for binary output.
void transform(double &__x, double &__y, double &__z) const
Transform.
file Auxiliary data structures and methods for detector calibration.
Definition JAnchor.hh:12
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Calibration.
Definition JHead.hh:330
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84