INSTINCT Code Coverage Report


Directory: src/
File: Navigation/INS/Units.hpp
Date: 2025-06-02 15:19:59
Exec Total Coverage
Lines: 42 106 39.6%
Functions: 10 10 100.0%
Branches: 24 132 18.2%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 /// @file Units.hpp
10 /// @brief Units used by INS
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2024-10-21
13
14 #pragma once
15
16 #include <cstddef>
17 #include <string>
18
19 #include "Navigation/Constants.hpp"
20 #include "Navigation/Transformations/Units.hpp"
21 #include "util/Eigen.hpp"
22 #include "util/Json.hpp"
23
24 namespace NAV
25 {
26
27 namespace Units
28 {
29
30 /// Possible units to specify an accelerometer with
31 enum class ImuAccelerometerUnits : uint8_t
32 {
33 m_s2, ///< [m/s^2]
34 g, ///< [g]
35 COUNT, ///< Amount of items in the enum
36 };
37
38 /// Possible units to specify an gyroscope bias with
39 enum class ImuGyroscopeUnits : uint8_t
40 {
41 rad_s, ///< [rad/s]
42 deg_s, ///< [deg/s]
43 COUNT, ///< Amount of items in the enum
44 };
45
46 /// Possible units to specify an accelerometer noise
47 enum class ImuAccelerometerNoiseUnits : uint8_t
48 {
49 m_s2_sqrts, ///< [m/s^2/sqrt(s)] (Standard deviation)
50 m_s2_sqrth, ///< [m/s^2/sqrt(h)] (Standard deviation)
51 COUNT, ///< Amount of items in the enum
52 };
53
54 /// Possible units to specify an gyro noise
55 enum class ImuGyroscopeNoiseUnits : uint8_t
56 {
57 rad_s_sqrts, ///< [rad/s/sqrt(s)] (Standard deviation)
58 rad_s_sqrth, ///< [rad/s/sqrt(h)] (Standard deviation)
59 deg_s_sqrts, ///< [deg/s/sqrt(s)] (Standard deviation)
60 deg_s_sqrth, ///< [deg/s/sqrt(h)] (Standard deviation)
61 COUNT, ///< Amount of items in the enum
62 };
63
64 /// Possible units to specify an accelerometer IRW
65 enum class ImuAccelerometerIRWUnits : uint8_t
66 {
67 m_s3_sqrts, ///< [m/s^3/sqrt(s)] (Standard deviation)
68 m_s3_sqrth, ///< [m/s^3/sqrt(h)] (Standard deviation)
69 COUNT, ///< Amount of items in the enum
70 };
71
72 /// Possible units to specify an gyro RW
73 enum class ImuGyroscopeIRWUnits : uint8_t
74 {
75 rad_s2_sqrts, ///< [rad/s^2/sqrt(s)] (Standard deviation)
76 rad_s2_sqrth, ///< [rad/s^2/sqrt(h)] (Standard deviation)
77 deg_s2_sqrts, ///< [deg/s^2/sqrt(s)] (Standard deviation)
78 deg_s2_sqrth, ///< [deg/s^2/sqrt(h)] (Standard deviation)
79 COUNT, ///< Amount of items in the enum
80 };
81
82 /// Possible units to specify an accelerometer noise in a filter
83 enum class ImuAccelerometerFilterNoiseUnits : uint8_t
84 {
85 m_s2_sqrtHz, ///< [m / s^2 / √(Hz)]
86 mg_sqrtHz, ///< [mg / √(Hz)]
87 COUNT, ///< Amount of items in the enum
88 };
89
90 /// Possible units to specify an gyro noise in a filter
91 enum class ImuGyroscopeFilterNoiseUnits : uint8_t
92 {
93 rad_s_sqrtHz, ///< [rad / s /√(Hz)]
94 rad_hr_sqrtHz, ///< [rad / hr /√(Hz)]
95 deg_s_sqrtHz, ///< [deg / s /√(Hz)]
96 deg_hr_sqrtHz, ///< [deg / hr /√(Hz)]
97 COUNT, ///< Amount of items in the enum
98 };
99
100 /// Possible units for the accelerometer dynamic bias
101 enum class ImuAccelerometerFilterBiasUnits : uint8_t
102 {
103 m_s2, ///< [m / s^2]
104 microg, ///< [µg]
105 COUNT, ///< Amount of items in the enum
106 };
107
108 /// Possible units for the gyroscope dynamic bias
109 enum class ImuGyroscopeFilterBiasUnits : uint8_t
110 {
111 rad_s, ///< [1/s]
112 rad_h, ///< [1/h]
113 deg_s, ///< [°/s]
114 deg_h, ///< [°/h]
115 COUNT, ///< Amount of items in the enum
116 };
117
118 /// @brief Converts the provided data into a json object
119 /// @param[out] j Json object which gets filled with the info
120 /// @param[in] data Data to convert into json
121 void to_json(json& j, const ImuAccelerometerUnits& data);
122 /// @brief Converts the provided json object into the data object
123 /// @param[in] j Json object with the needed values
124 /// @param[out] data Object to fill from the json
125 void from_json(const json& j, ImuAccelerometerUnits& data);
126
127 /// @brief Converts the provided data into a json object
128 /// @param[out] j Json object which gets filled with the info
129 /// @param[in] data Data to convert into json
130 void to_json(json& j, const ImuGyroscopeUnits& data);
131 /// @brief Converts the provided json object into the data object
132 /// @param[in] j Json object with the needed values
133 /// @param[out] data Object to fill from the json
134 void from_json(const json& j, ImuGyroscopeUnits& data);
135
136 /// @brief Converts the provided data into a json object
137 /// @param[out] j Json object which gets filled with the info
138 /// @param[in] data Data to convert into json
139 void to_json(json& j, const ImuAccelerometerNoiseUnits& data);
140 /// @brief Converts the provided json object into the data object
141 /// @param[in] j Json object with the needed values
142 /// @param[out] data Object to fill from the json
143 void from_json(const json& j, ImuAccelerometerNoiseUnits& data);
144
145 /// @brief Converts the provided data into a json object
146 /// @param[out] j Json object which gets filled with the info
147 /// @param[in] data Data to convert into json
148 void to_json(json& j, const ImuGyroscopeNoiseUnits& data);
149 /// @brief Converts the provided json object into the data object
150 /// @param[in] j Json object with the needed values
151 /// @param[out] data Object to fill from the json
152 void from_json(const json& j, ImuGyroscopeNoiseUnits& data);
153
154 /// @brief Converts the provided data into a json object
155 /// @param[out] j Json object which gets filled with the info
156 /// @param[in] data Data to convert into json
157 void to_json(json& j, const ImuAccelerometerIRWUnits& data);
158 /// @brief Converts the provided json object into the data object
159 /// @param[in] j Json object with the needed values
160 /// @param[out] data Object to fill from the json
161 void from_json(const json& j, ImuAccelerometerIRWUnits& data);
162
163 /// @brief Converts the provided data into a json object
164 /// @param[out] j Json object which gets filled with the info
165 /// @param[in] data Data to convert into json
166 void to_json(json& j, const ImuGyroscopeIRWUnits& data);
167 /// @brief Converts the provided json object into the data object
168 /// @param[in] j Json object with the needed values
169 /// @param[out] data Object to fill from the json
170 void from_json(const json& j, ImuGyroscopeIRWUnits& data);
171
172 /// @brief Converts the provided data into a json object
173 /// @param[out] j Json object which gets filled with the info
174 /// @param[in] data Data to convert into json
175 void to_json(json& j, const ImuAccelerometerFilterNoiseUnits& data);
176 /// @brief Converts the provided json object into the data object
177 /// @param[in] j Json object with the needed values
178 /// @param[out] data Object to fill from the json
179 void from_json(const json& j, ImuAccelerometerFilterNoiseUnits& data);
180
181 /// @brief Converts the provided data into a json object
182 /// @param[out] j Json object which gets filled with the info
183 /// @param[in] data Data to convert into json
184 void to_json(json& j, const ImuGyroscopeFilterNoiseUnits& data);
185 /// @brief Converts the provided json object into the data object
186 /// @param[in] j Json object with the needed values
187 /// @param[out] data Object to fill from the json
188 void from_json(const json& j, ImuGyroscopeFilterNoiseUnits& data);
189
190 /// @brief Converts the provided data into a json object
191 /// @param[out] j Json object which gets filled with the info
192 /// @param[in] data Data to convert into json
193 void to_json(json& j, const ImuAccelerometerFilterBiasUnits& data);
194 /// @brief Converts the provided json object into the data object
195 /// @param[in] j Json object with the needed values
196 /// @param[out] data Object to fill from the json
197 void from_json(const json& j, ImuAccelerometerFilterBiasUnits& data);
198
199 /// @brief Converts the provided data into a json object
200 /// @param[out] j Json object which gets filled with the info
201 /// @param[in] data Data to convert into json
202 void to_json(json& j, const ImuGyroscopeFilterBiasUnits& data);
203 /// @brief Converts the provided json object into the data object
204 /// @param[in] j Json object with the needed values
205 /// @param[out] data Object to fill from the json
206 void from_json(const json& j, ImuGyroscopeFilterBiasUnits& data);
207
208 } // namespace Units
209
210 /// @brief Converts the value depending on the unit provided
211 /// @param[in] value Value to convert
212 /// @param[in] unit Unit the value is in
213 /// @return Value in unit of the first item in the Unit enum
214 template<typename Derived>
215 58447 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuAccelerometerUnits unit)
216 {
217
2/4
✓ Branch 0 taken 58446 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
58447 switch (unit)
218 {
219 58446 case Units::ImuAccelerometerUnits::m_s2:
220 58446 return value;
221 case Units::ImuAccelerometerUnits::g:
222 return value * InsConst::G_NORM;
223 case Units::ImuAccelerometerUnits::COUNT:
224 break;
225 }
226 1 return value;
227 }
228
229 /// @brief Converts the value depending on the unit provided
230 /// @param[in] value Value to convert
231 /// @param[in] unit Unit the value is in
232 /// @return Value in unit of the first item in the Unit enum
233 template<typename Derived>
234 58437 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuGyroscopeUnits unit)
235 {
236
1/4
✓ Branch 0 taken 58437 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
58437 switch (unit)
237 {
238 58437 case Units::ImuGyroscopeUnits::rad_s:
239 58437 return value;
240 case Units::ImuGyroscopeUnits::deg_s:
241 return deg2rad(value);
242 case Units::ImuGyroscopeUnits::COUNT:
243 break;
244 }
245 return value;
246 }
247
248 /// @brief Converts the value depending on the unit provided
249 /// @param[in] value Value to convert
250 /// @param[in] unit Unit the value is in
251 /// @return Value in unit of the first item in the Unit enum
252 template<typename Derived>
253 92654 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuAccelerometerNoiseUnits unit)
254 {
255
1/4
✓ Branch 0 taken 92658 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
92654 switch (unit)
256 {
257 92658 case Units::ImuAccelerometerNoiseUnits::m_s2_sqrts:
258 92658 return value;
259 case Units::ImuAccelerometerNoiseUnits::m_s2_sqrth:
260 return value / 60.0;
261 case Units::ImuAccelerometerNoiseUnits::COUNT:
262 break;
263 }
264 return value;
265 }
266
267 /// @brief Converts the value depending on the unit provided
268 /// @param[in] value Value to convert
269 /// @param[in] unit Unit the value is in
270 /// @return Value in unit of the first item in the Unit enum
271 template<typename Derived>
272 92653 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuGyroscopeNoiseUnits unit)
273 {
274
1/6
✓ Branch 0 taken 92665 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
92653 switch (unit)
275 {
276 92665 case Units::ImuGyroscopeNoiseUnits::rad_s_sqrts:
277 92665 return value;
278 case Units::ImuGyroscopeNoiseUnits::rad_s_sqrth:
279 return value / 60.0;
280 case Units::ImuGyroscopeNoiseUnits::deg_s_sqrts:
281 return deg2rad(value);
282 case Units::ImuGyroscopeNoiseUnits::deg_s_sqrth:
283 return deg2rad(value) / 60.0;
284 case Units::ImuGyroscopeNoiseUnits::COUNT:
285 break;
286 }
287 return value;
288 }
289
290 /// @brief Converts the value depending on the unit provided
291 /// @param[in] value Value to convert
292 /// @param[in] unit Unit the value is in
293 /// @return Value in unit of the first item in the Unit enum
294 template<typename Derived>
295 34217 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuAccelerometerIRWUnits unit)
296 {
297
2/4
✓ Branch 0 taken 34213 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
34217 switch (unit)
298 {
299 34213 case Units::ImuAccelerometerIRWUnits::m_s3_sqrts:
300 34213 return value;
301 case Units::ImuAccelerometerIRWUnits::m_s3_sqrth:
302 return value / 60.0;
303 case Units::ImuAccelerometerIRWUnits::COUNT:
304 break;
305 }
306 4 return value;
307 }
308
309 /// @brief Converts the value depending on the unit provided
310 /// @param[in] value Value to convert
311 /// @param[in] unit Unit the value is in
312 /// @return Value in unit of the first item in the Unit enum
313 template<typename Derived>
314 34220 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuGyroscopeIRWUnits unit)
315 {
316
1/6
✓ Branch 0 taken 34220 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
34220 switch (unit)
317 {
318 34220 case Units::ImuGyroscopeIRWUnits::rad_s2_sqrts:
319 34220 return value;
320 case Units::ImuGyroscopeIRWUnits::rad_s2_sqrth:
321 return value / 60.0;
322 case Units::ImuGyroscopeIRWUnits::deg_s2_sqrts:
323 return deg2rad(value);
324 case Units::ImuGyroscopeIRWUnits::deg_s2_sqrth:
325 return deg2rad(value) / 60.0;
326 case Units::ImuGyroscopeIRWUnits::COUNT:
327 break;
328 }
329 return value;
330 }
331
332 /// @brief Converts the value depending on the unit provided
333 /// @param[in] value Value to convert
334 /// @param[in] unit Unit the value is in
335 /// @return Value in unit of the first item in the Unit enum
336 template<typename Derived>
337 49997 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuAccelerometerFilterNoiseUnits unit)
338 {
339
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49997 switch (unit)
340 {
341 case Units::ImuAccelerometerFilterNoiseUnits::m_s2_sqrtHz:
342 return value;
343 49997 case Units::ImuAccelerometerFilterNoiseUnits::mg_sqrtHz:
344
3/6
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49997 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 49997 times.
✗ Branch 8 not taken.
99994 return value * 1e-3 * InsConst::G_NORM;
345 case Units::ImuAccelerometerFilterNoiseUnits::COUNT:
346 break;
347 }
348 return value;
349 }
350
351 /// @brief Converts the value depending on the unit provided
352 /// @param[in] value Value to convert
353 /// @param[in] unit Unit the value is in
354 /// @return Value in unit of the first item in the Unit enum
355 template<typename Derived>
356 49997 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuGyroscopeFilterNoiseUnits unit)
357 {
358
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49997 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
49997 switch (unit)
359 {
360 case Units::ImuGyroscopeFilterNoiseUnits::rad_s_sqrtHz:
361 return value;
362 case Units::ImuGyroscopeFilterNoiseUnits::rad_hr_sqrtHz:
363 return value / 3600.0;
364 case Units::ImuGyroscopeFilterNoiseUnits::deg_s_sqrtHz:
365 return deg2rad(value);
366 49997 case Units::ImuGyroscopeFilterNoiseUnits::deg_hr_sqrtHz:
367
3/6
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49997 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 49997 times.
✗ Branch 8 not taken.
49997 return deg2rad(value) / 3600.0;
368 case Units::ImuGyroscopeFilterNoiseUnits::COUNT:
369 break;
370 }
371 return value;
372 }
373
374 /// @brief Converts the value depending on the unit provided
375 /// @param[in] value Value to convert
376 /// @param[in] unit Unit the value is in
377 /// @return Value in unit of the first item in the Unit enum
378 template<typename Derived>
379 49997 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuAccelerometerFilterBiasUnits unit)
380 {
381
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49997 switch (unit)
382 {
383 case Units::ImuAccelerometerFilterBiasUnits::m_s2:
384 return value;
385 49997 case Units::ImuAccelerometerFilterBiasUnits::microg:
386
3/6
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49997 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 49997 times.
✗ Branch 8 not taken.
99994 return value * 1e-6 * InsConst::G_NORM;
387 case Units::ImuAccelerometerFilterBiasUnits::COUNT:
388 break;
389 }
390 return value;
391 }
392
393 /// @brief Converts the value depending on the unit provided
394 /// @param[in] value Value to convert
395 /// @param[in] unit Unit the value is in
396 /// @return Value in unit of the first item in the Unit enum
397 template<typename Derived>
398 49997 [[nodiscard]] typename Derived::PlainObject convertUnit(const Eigen::MatrixBase<Derived>& value, Units::ImuGyroscopeFilterBiasUnits unit)
399 {
400
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49997 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
49997 switch (unit)
401 {
402 case Units::ImuGyroscopeFilterBiasUnits::rad_s:
403 return value;
404 case Units::ImuGyroscopeFilterBiasUnits::rad_h:
405 return value / 3600.0;
406 case Units::ImuGyroscopeFilterBiasUnits::deg_s:
407 return deg2rad(value);
408 49997 case Units::ImuGyroscopeFilterBiasUnits::deg_h:
409
3/6
✓ Branch 1 taken 49997 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49997 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 49997 times.
✗ Branch 8 not taken.
49997 return deg2rad(value) / 3600.0;
410 case Units::ImuGyroscopeFilterBiasUnits::COUNT:
411 break;
412 }
413 return value;
414 }
415
416 /// @brief Converts the unit into a string
417 /// @param[in] unit Unit
418 [[nodiscard]] std::string to_string(Units::ImuAccelerometerUnits unit);
419 /// @brief Converts the unit into a string
420 /// @param[in] unit Unit
421 [[nodiscard]] std::string to_string(Units::ImuGyroscopeUnits unit);
422 /// @brief Converts the unit into a string
423 /// @param[in] unit Unit
424 [[nodiscard]] std::string to_string(Units::ImuAccelerometerNoiseUnits unit);
425 /// @brief Converts the unit into a string
426 /// @param[in] unit Unit
427 [[nodiscard]] std::string to_string(Units::ImuGyroscopeNoiseUnits unit);
428 /// @brief Converts the unit into a string
429 /// @param[in] unit Unit
430 [[nodiscard]] std::string to_string(Units::ImuAccelerometerIRWUnits unit);
431 /// @brief Converts the unit into a string
432 /// @param[in] unit Unit
433 [[nodiscard]] std::string to_string(Units::ImuGyroscopeIRWUnits unit);
434 /// @brief Converts the unit into a string
435 /// @param[in] unit Unit
436 [[nodiscard]] std::string to_string(Units::ImuAccelerometerFilterNoiseUnits unit);
437 /// @brief Converts the unit into a string
438 /// @param[in] unit Unit
439 [[nodiscard]] std::string to_string(Units::ImuGyroscopeFilterNoiseUnits unit);
440 /// @brief Converts the unit into a string
441 /// @param[in] unit Unit
442 [[nodiscard]] std::string to_string(Units::ImuAccelerometerFilterBiasUnits unit);
443 /// @brief Converts the unit into a string
444 /// @param[in] unit Unit
445 [[nodiscard]] std::string to_string(Units::ImuGyroscopeFilterBiasUnits unit);
446
447 } // namespace NAV
448