|
| 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 | +#include <array> |
| 33 | + |
| 34 | +namespace andino_base { |
| 35 | + |
| 36 | +/// \brief Interface to the microcontroller running andino_firmware. |
| 37 | +/// It exposes the full set of commands supported by andino_firmware. |
| 38 | +class Mcu { |
| 39 | + public: |
| 40 | + /// @brief Holds encoder sensors reading. First value is the left encoder, second is the right one. |
| 41 | + using EncodersData = std::array<int, 2>; |
| 42 | + |
| 43 | + /// @brief Holds IMU sensor reading. |
| 44 | + struct ImuData { |
| 45 | + /// @brief Absolute orientation as a quaternion, in (x, y, z, w) order. |
| 46 | + std::array<double, 4> orientation{}; |
| 47 | + /// @brief Angular velocity [rad/s], in (x, y, z) order. |
| 48 | + std::array<double, 3> angular_velocity{}; |
| 49 | + /// @brief Linear acceleration [m/s^2], in (x, y, z) order. |
| 50 | + std::array<double, 3> linear_acceleration{}; |
| 51 | + }; |
| 52 | + |
| 53 | + /// @brief Holds encoders and IMU sensors reading. |
| 54 | + struct EncodersAndImuData { |
| 55 | + /// @brief The encoder values. |
| 56 | + EncodersData encoders_data{}; |
| 57 | + /// @brief The IMU sensor reading. |
| 58 | + ImuData imu_data{}; |
| 59 | + }; |
| 60 | + |
| 61 | + virtual ~Mcu() = default; |
| 62 | + |
| 63 | + /// @brief Checks if the microcontroller is connected. |
| 64 | + /// @return True if the microcontroller is connected, false otherwise. |
| 65 | + virtual bool is_connected() const = 0; |
| 66 | + |
| 67 | + /// @brief Resets the encoder sensors. |
| 68 | + virtual void reset_encoders() = 0; |
| 69 | + |
| 70 | + /// @brief Returns the encoder sensors data. |
| 71 | + /// @returns The encoder sensors data. |
| 72 | + virtual EncodersData read_encoders() = 0; |
| 73 | + |
| 74 | + /// @brief Checks if there is an IMU sensor available. |
| 75 | + /// @returns True if there is an IMU sensor available, false otherwise. |
| 76 | + virtual bool is_imu_available() = 0; |
| 77 | + |
| 78 | + /// @brief Returns the encoder sensors and IMU sensor data. |
| 79 | + /// @returns The encoder sensors and IMU sensor data. |
| 80 | + virtual EncodersAndImuData read_encoders_and_imu() = 0; |
| 81 | + |
| 82 | + /// @brief Sets the motors speed [ticks/s]. |
| 83 | + /// @param left_motor_speed Speed value for the left motor. |
| 84 | + /// @param right_motor_speed Speed value for the right motor. |
| 85 | + virtual void set_motors_speed(int left_motor_speed, int right_motor_speed) = 0; |
| 86 | + |
| 87 | + /// @brief Sets the motors PWM [duty range: 0-255]. |
| 88 | + /// @param left_motor_pwm PWM value for the left motor. |
| 89 | + /// @param right_motor_pwm PWM value for the right motor. |
| 90 | + virtual void set_motors_pwm(int left_motor_pwm, int right_motor_pwm) = 0; |
| 91 | + |
| 92 | + /// @brief Sets the PID tuning gains. |
| 93 | + /// @param kp Proportional gain. |
| 94 | + /// @param kd Derivative gain. |
| 95 | + /// @param ki Integral gain. |
| 96 | + /// @param ko Offset gain. |
| 97 | + virtual void set_pid_tuning_gains(float kp, float kd, float ki, float ko) = 0; |
| 98 | +}; |
| 99 | + |
| 100 | +} // namespace andino_base |
0 commit comments