Program Listing for File ballistics.hpp

Return to documentation for file (src/tap/algorithms/ballistics.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_BALLISTICS_HPP_
#define TAPROOT_BALLISTICS_HPP_

#include <cmath>

#include "modm/math/geometry/vector.hpp"

namespace tap::algorithms::ballistics
{
struct AbstractKinematicState
{
    virtual modm::Vector3f projectForward(float dt) const = 0;

    inline static float quadraticKinematicProjection(float dt, float s, float v, float a)
    {
        return s + v * dt + 0.5f * a * powf(dt, 2.0f);
    }
};

struct SecondOrderKinematicState : public AbstractKinematicState
{
    inline SecondOrderKinematicState(
        modm::Vector3f position,
        modm::Vector3f velocity,
        modm::Vector3f acceleration)
        : position(position),
          velocity(velocity),
          acceleration(acceleration)
    {
    }

    modm::Vector3f position;      // m
    modm::Vector3f velocity;      // m/s
    modm::Vector3f acceleration;  // m/s^2

    inline modm::Vector3f projectForward(float dt) const override
    {
        return modm::Vector3f(
            quadraticKinematicProjection(dt, position.x, velocity.x, acceleration.x),
            quadraticKinematicProjection(dt, position.y, velocity.y, acceleration.y),
            quadraticKinematicProjection(dt, position.z, velocity.z, acceleration.z));
    }
};

bool computeTravelTime(
    const modm::Vector3f &targetPosition,
    float bulletVelocity,
    float *travelTime,
    float *turretPitch,
    const float pitchAxisOffset = 0);

bool findTargetProjectileIntersection(
    const AbstractKinematicState &targetInitialState,
    float bulletVelocity,
    uint8_t numIterations,
    float *turretPitch,
    float *turretYaw,
    float *projectedTravelTime,
    const float pitchAxisOffset = 0);

}  // namespace tap::algorithms::ballistics

#endif  // TAPROOT_BALLISTICS_HPP_