Program Listing for File chassis_subsystem_interface.hpp

Return to documentation for file (src/tap/control/chassis/chassis_subsystem_interface.hpp)

/*
 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
 *
 * This file is part of Taproot.
 *
 * Taproot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Taproot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Taproot.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TAPROOT_CHASSIS_SUBSYSTEM_INTERFACE_HPP_
#define TAPROOT_CHASSIS_SUBSYSTEM_INTERFACE_HPP_

#include "tap/algorithms/math_user_utils.hpp"
#include "tap/motor/dji_motor.hpp"

#include "../subsystem.hpp"
#include "modm/math/matrix.hpp"

namespace tap::control::chassis
{
class ChassisSubsystemInterface : public Subsystem
{
public:
    ChassisSubsystemInterface(Drivers* drivers) : Subsystem(drivers) {}

    virtual inline int getNumChassisMotors() const = 0;

    virtual inline bool allMotorsOnline() const = 0;

    virtual modm::Matrix<float, 3, 1> getActualVelocityChassisRelative() const = 0;

    static void getVelocityWorldRelative(
        modm::Matrix<float, 3, 1>& chassisRelativeVelocity,
        float chassisHeading)
    {
        modm::Matrix<float, 3, 3> transform;
        float headingCos = cosf(chassisHeading);
        float headingSin = sinf(chassisHeading);
        headingCos = tap::algorithms::compareFloatClose(headingCos, 0.0f, 1e-6) ? 0.0f : headingCos;
        headingSin = tap::algorithms::compareFloatClose(headingSin, 0.0f, 1e-6) ? 0.0f : headingSin;

        transform[0][0] = headingCos;
        transform[1][0] = headingSin;
        transform[2][0] = 0;
        transform[0][1] = -headingSin;
        transform[1][1] = headingCos;
        transform[2][1] = 0;
        transform[0][2] = 0;
        transform[1][2] = 0;
        transform[2][2] = 1;
        chassisRelativeVelocity = transform * chassisRelativeVelocity;
    }
};
}  // namespace tap::control::chassis

#endif  // TAPROOT_CHASSIS_SUBSYSTEM_INTERFACE_HPP_