Skyward boardcore
Loading...
Searching...
No Matches
SoftAndHardIronCalibration.cpp
Go to the documentation of this file.
1/* Copyright (c) 2021-2022 Skyward Experimental Rocketry
2 * Authors: Riccardo Musso, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
24
26
27#include <iostream>
28
29using namespace Eigen;
30
31namespace Boardcore
32{
33
35
37{
38 // Let S a matrix of Nx7 composed as [x^2, y^2, z^2, x, y, z, 1]
39 // D need to be S^T * S
40 // To avoid storing all measurements we just need to incrementally add to D
41
42 Vector3f vector;
43 vector << data;
44 Vector<float, 7> S;
45 // cppcheck-suppress constStatement
46 S << vector.cwiseProduct(vector), vector, 1;
47
48 for (int i = 0; i < 7; i++)
49 for (int j = 0; j < 7; j++)
50 D(i, j) += S(i) * S(j);
51
52 return true;
53}
54
56{
57 // Compute eigen value and vectors of D
58 SelfAdjointEigenSolver<Matrix<float, 7, 7>> solver(D);
59 auto eigenValues = solver.eigenvalues();
60
61 // Find the smallest eigen value and vector
62 float minValue = eigenValues[0];
63 int minIdx = 0;
64
65 for (int i = 0; i < eigenValues.rows(); i++)
66 {
67 if (minValue > eigenValues[i])
68 {
69 minValue = eigenValues[i];
70 minIdx = i;
71 }
72 }
73 Eigen::Matrix<float, 7, 1> vec = solver.eigenvectors().col(minIdx);
74
75 // Invert the vector if necessary
76 float det = vec[0] * vec[1] * vec[2];
77 if (det)
78 {
79 vec *= -1;
80 det *= -1;
81 }
82
83 // Compute offset and gain
84 Vector3f offset{vec[3] / vec[0] / 2, vec[4] / vec[1] / 2,
85 vec[5] / vec[2] / 2};
86 Vector3f gain = (vec.block(0, 0, 3, 1) / cbrt(det)).cwiseSqrt();
87
88 return {gain, -offset};
89}
90
91} // namespace Boardcore
Six-parameter correction uses, for each axis, a coefficient to be multiplied and a constant to be add...
SixParametersCorrector computeResult()
Uses the recorded measurements to compute the correction parameters needed to correct sensor's data.
This file includes all the types the logdecoder script will decode.
Structure to handle magnetometer data.
Definition SensorData.h:249