Skyward boardcore
Loading...
Searching...
No Matches
StateInitializer.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Authors: Marco Cella, 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
23#include "StateInitializer.h"
24
25using namespace Eigen;
26
27namespace Boardcore
28{
29
30StateInitializer::StateInitializer() { x_init << MatrixXf::Zero(13, 1); }
31
32void StateInitializer::eCompass(const Vector3f acc, const Vector3f mag)
33{
34 // ndr: since this method runs only when the rocket is stationary, there's
35 // no need to add the gravity vector because the accelerometers already
36 // measure it. This is not true if we consider the flying rocket.
37
38 Vector3f am(acc.cross(mag));
39
40 Matrix3f R;
41 // cppcheck-suppress constStatement
42 R << am.cross(acc), am, acc;
43 R.col(0).normalize();
44 R.col(1).normalize();
45 R.col(2).normalize();
46
47 Vector4f x_quat = SkyQuaternion::rotationMatrix2quat(R);
48
49 x_init(NAS::IDX_QUAT) = x_quat(0);
50 x_init(NAS::IDX_QUAT + 1) = x_quat(1);
51 x_init(NAS::IDX_QUAT + 2) = x_quat(2);
52 x_init(NAS::IDX_QUAT + 3) = x_quat(3);
53}
54
55void StateInitializer::triad(const Vector3f& acc, const Vector3f& mag,
56 const Vector3f& nedMag)
57{
58 // The gravity vector is expected to be read inversely because
59 // accelerometers read the binding reaction
60 Vector3f nedAcc(0.0f, 0.0f, -1.0f);
61
62 // Compute the reference triad
63 Vector3f R1 = nedAcc;
64 Vector3f R2 = nedAcc.cross(nedMag).normalized();
65 Vector3f R3 = R1.cross(R2);
66
67 // Compute the measured triad
68 Vector3f r1 = acc;
69 Vector3f r2 = acc.cross(mag).normalized();
70 Vector3f r3 = r1.cross(r2);
71
72 // Compose the reference and measured matrixes
73 Matrix3f M;
74 // cppcheck-suppress constStatement
75 M << R1, R2, R3;
76 Matrix3f m;
77 // cppcheck-suppress constStatement
78 m << r1, r2, r3;
79
80 // Compute the rotation matrix and the corresponding quaternion
81 Matrix3f A = m * M.transpose();
83
84 // Save the orientation in the state
85 x_init.block<4, 1>(NAS::IDX_QUAT, 0) = q;
86}
87
88Matrix<float, 13, 1> StateInitializer::getInitX() { return x_init; }
89
90} // namespace Boardcore
static constexpr uint16_t IDX_QUAT
Index of bias elements in the state.
Definition NAS.h:48
void triad(const Eigen::Vector3f &acc, const Eigen::Vector3f &mag, const Eigen::Vector3f &nedMag)
Triad algorithm to estimate the attitude before the liftoff.
Eigen::Matrix< float, 13, 1 > getInitX()
void eCompass(const Eigen::Vector3f acc, const Eigen::Vector3f mag)
Ecompass algorithm to estimate the attitude before the liftoff.
StateInitializer()
Initialize the state of the Extended Kalman Filter.
Vector4f rotationMatrix2quat(const Matrix3f &rtm)
This file includes all the types the logdecoder script will decode.