|
| 1 | +// BSD 3-Clause License |
| 2 | +// |
| 3 | +// Copyright (c) 2026, Ekumen Inc. |
| 4 | +// All rights reserved. |
| 5 | +// |
| 6 | +// Redistribution and use in source and binary forms, with or without |
| 7 | +// modification, are permitted provided that the following conditions are met: |
| 8 | +// |
| 9 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | +// list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +// this list of conditions and the following disclaimer in the documentation |
| 14 | +// and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// 3. Neither the name of the copyright holder nor the names of its |
| 17 | +// contributors may be used to endorse or promote products derived from |
| 18 | +// this software without specific prior written permission. |
| 19 | +// |
| 20 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +#pragma once |
| 31 | + |
| 32 | +namespace andino { |
| 33 | + |
| 34 | +/// @brief This class allows to use an IMU sensor by configuring it and then getting its |
| 35 | +/// orientation, angular velocity and linear acceleration readings. It is backed by an Adafruit |
| 36 | +/// BNO055 sensor. |
| 37 | +class Imu { |
| 38 | + public: |
| 39 | + /// @brief Absolute orientation as a quaternion. |
| 40 | + struct Orientation { |
| 41 | + /// @brief Constructs a zeroed-out Orientation. |
| 42 | + Orientation() = default; |
| 43 | + |
| 44 | + /// @brief Constructs an Orientation from its components. |
| 45 | + Orientation(double orientation_x, double orientation_y, double orientation_z, |
| 46 | + double orientation_w) |
| 47 | + : x(orientation_x), y(orientation_y), z(orientation_z), w(orientation_w) { |
| 48 | + } |
| 49 | + |
| 50 | + /// Quaternion x component. |
| 51 | + double x{0.0}; |
| 52 | + /// Quaternion y component. |
| 53 | + double y{0.0}; |
| 54 | + /// Quaternion z component. |
| 55 | + double z{0.0}; |
| 56 | + /// Quaternion w component. |
| 57 | + double w{0.0}; |
| 58 | + }; |
| 59 | + |
| 60 | + /// @brief A 3-axis vector reading (e.g. angular velocity or linear acceleration). |
| 61 | + struct Vector3 { |
| 62 | + /// @brief Constructs a zeroed-out Vector3. |
| 63 | + Vector3() = default; |
| 64 | + |
| 65 | + /// @brief Constructs a Vector3 from its components. |
| 66 | + Vector3(double vector_x, double vector_y, double vector_z) |
| 67 | + : x(vector_x), y(vector_y), z(vector_z) { |
| 68 | + } |
| 69 | + |
| 70 | + /// X axis component. |
| 71 | + double x{0.0}; |
| 72 | + /// Y axis component. |
| 73 | + double y{0.0}; |
| 74 | + /// Z axis component. |
| 75 | + double z{0.0}; |
| 76 | + }; |
| 77 | + |
| 78 | + /// @brief Initializes the IMU sensor. |
| 79 | + /// |
| 80 | + /// @return True if the IMU sensor was successfully initialized, false otherwise. |
| 81 | + bool begin() const; |
| 82 | + |
| 83 | + /// @brief Gets the absolute orientation. |
| 84 | + /// |
| 85 | + /// @return Absolute orientation as a quaternion. |
| 86 | + Orientation get_orientation() const; |
| 87 | + |
| 88 | + /// @brief Gets the angular velocity. |
| 89 | + /// |
| 90 | + /// @return Angular velocity vector [rad/s]. |
| 91 | + Vector3 get_angular_velocity() const; |
| 92 | + |
| 93 | + /// @brief Gets the linear acceleration. |
| 94 | + /// |
| 95 | + /// @return Linear acceleration vector [m/s^2]. |
| 96 | + Vector3 get_linear_acceleration() const; |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace andino |
0 commit comments