Skyward boardcore
Loading...
Searching...
No Matches
RotatedIMU.cpp
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Author: Niccolò Betto
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 "RotatedIMU.h"
24
25using namespace Eigen;
26
27namespace Boardcore
28{
29
31 : sampleImu(std::move(sampleImuFunction))
32{
33}
34
35void RotatedIMU::addAccTransformation(const Matrix3f& t) { accT = t * accT; }
36void RotatedIMU::addGyroTransformation(const Matrix3f& t) { gyroT = t * gyroT; }
37void RotatedIMU::addMagTransformation(const Matrix3f& t) { magT = t * magT; }
38
40{
41 accT = Matrix3f::Identity();
42 gyroT = Matrix3f::Identity();
43 magT = Matrix3f::Identity();
44}
45
46Matrix3f RotatedIMU::rotateAroundX(float angle)
47{
48 Matrix3f rotation;
49 angle = angle * EIGEN_PI / 180.f;
50
51 // clang-format off
52 rotation = Matrix3f{{1, 0, 0},
53 {0, cosf(angle), -sinf(angle)},
54 {0, sinf(angle), cosf(angle)}};
55 // clang-format on
56
57 return rotation;
58}
59
60Matrix3f RotatedIMU::rotateAroundY(float angle)
61{
62 Matrix3f rotation;
63 angle = angle * EIGEN_PI / 180.f;
64
65 // clang-format off
66 rotation = Matrix3f{{cosf(angle), 0, sinf(angle)},
67 {0, 1, 0},
68 {-sinf(angle), 0, cosf(angle)}};
69 // clang-format on
70
71 return rotation;
72}
73
74Matrix3f RotatedIMU::rotateAroundZ(float angle)
75{
76 Matrix3f rotation;
77 angle = angle * EIGEN_PI / 180.f;
78
79 // clang-format off
80 rotation = Matrix3f{{cosf(angle), -sinf(angle), 0},
81 {sinf(angle), cosf(angle), 0},
82 {0, 0, 1}};
83 // clang-format on
84
85 return rotation;
86}
87
89{
90 auto imuData = sampleImu();
91
92 AccelerometerData& accData = imuData;
93 GyroscopeData& gyroData = imuData;
94 MagnetometerData& magData = imuData;
95
96 Vector3f rotatedAcc = accT * static_cast<Vector3f>(accData);
97 Vector3f rotatedGyro = gyroT * static_cast<Vector3f>(gyroData);
98 Vector3f rotatedMag = magT * static_cast<Vector3f>(magData);
99
100 auto acc = AccelerometerData{rotatedAcc};
101 acc.accelerationTimestamp = accData.accelerationTimestamp;
102
103 auto gyro = GyroscopeData{rotatedGyro};
104 gyro.angularSpeedTimestamp = gyroData.angularSpeedTimestamp;
105
106 auto mag = MagnetometerData{rotatedMag};
107 mag.magneticFieldTimestamp = magData.magneticFieldTimestamp;
108
109 return {acc, gyro, mag};
110}
111
112} // namespace Boardcore
RotatedIMU(SampleIMUFunction sampleFunction)
Construct a new Rotated IMU object.
static Eigen::Matrix3f rotateAroundZ(float angle)
Creates a rotation matrix around the Z axis.
void addGyroTransformation(const Eigen::Matrix3f &t)
Multiplies the current gyroscope transformation.
static Eigen::Matrix3f rotateAroundX(float angle)
Creates a rotation matrix around the X axis.
void resetTransformations()
Resets all the transformations to the original (Identity) ones.
IMUData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
void addAccTransformation(const Eigen::Matrix3f &t)
Multiplies the current accelerometer transformation.
std::function< IMUData()> SampleIMUFunction
Definition RotatedIMU.h:42
static Eigen::Matrix3f rotateAroundY(float angle)
Creates a rotation matrix around the Y axis.
void addMagTransformation(const Eigen::Matrix3f &t)
Multiplies the current magnetometer transformation.
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318
Structure to handle accelerometer data.
Definition SensorData.h:121
Structure to handle gyroscope data.
Definition SensorData.h:207
Structure to handle magnetometer data.
Definition SensorData.h:249