Skip to content

Commit

Permalink
Add shoot from range using trajectory optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
spacey-sooty committed Jul 5, 2024
1 parent 17258d1 commit 7e47e4d
Show file tree
Hide file tree
Showing 55 changed files with 12,934 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
java-version: 17
distribution: temurin
- uses: gradle/gradle-build-action@v3
- name: Install roboRIO toolchain
run: ./gradlew installRoboRioToolchain
- uses: actions/setup-python@v5
with:
python-version: 3.8
Expand Down
1 change: 1 addition & 0 deletions .styleguide
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ modifiableFileExclude {
gradlew.bat
src/main/java/com
src/main/java/frc/robot/URCL.java
sleipnir/
}
42 changes: 42 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "java"
id "cpp"
id "edu.wpi.first.GradleRIO" version "2024.3.2"
id 'edu.wpi.first.GradleJni' version '0.10.1'
id 'com.github.spotbugs' version '6.0.18' apply false
id 'com.diffplug.spotless' version '6.25.0' apply false
}
Expand All @@ -12,6 +14,15 @@ java {

def ROBOT_MAIN_CLASS = "frc.robot.Main"

// Set up exports properly
nativeUtils {
exportsConfigs {
// Only export explicit symbols from driver library
ShooterTrajoptJNI {
}
}
}

// Define my targets (RoboRIO) and artifacts (deployable files)
// This is added by GradleRIO's backing project DeployUtils.
deploy {
Expand Down Expand Up @@ -88,6 +99,37 @@ if (!project.hasProperty('noSimGUI')) {

wpi.sim.addDriverstation()

model {
components {
ShooterTrajoptJNI(JniNativeLibrarySpec) {
targetPlatform wpi.platforms.roborio

enableCheckTask true
javaCompileTasks << compileJava
jniCrossCompileOptions << JniCrossCompileOptions(wpi.platforms.roborio)

sources {
cpp {
source {
srcDirs 'src/main/native/cpp'
include '**/*.cpp'
}

exportedHeaders {
srcDir 'src/main/native/include'
}
}
}

binaries.all {
lib project: ':sleipnir', library: 'Sleipnir', linkage: 'shared'
}

nativeUtils.useRequiredLibrary(it, "driver_shared")
}
}
}

// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar')
// in order to make them all available at runtime. Also adding the manifest so WPILib
// knows where to look for our Robot Class.
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ pluginManagement {

Properties props = System.getProperties();
props.setProperty("org.gradle.internal.native.headers.unresolved.dependencies.ignore", "true");

include 'sleipnir'
rootProject.name = '2024-Offseason'
21 changes: 21 additions & 0 deletions sleipnir/LICENSE_small_vector.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Gene Harvey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions sleipnir/SLEIPNIR_LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (c) Sleipnir contributors

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 changes: 31 additions & 0 deletions sleipnir/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import edu.wpi.first.toolchain.NativePlatforms

apply plugin: 'cpp'
apply plugin: 'edu.wpi.first.GradleRIO'

model {
components {
Sleipnir(NativeLibrarySpec) {
targetPlatform NativePlatforms.desktop
targetPlatform NativePlatforms.roborio

sources {
cpp {
source {
srcDir 'src/cpp'
include '**/*.cpp', '**/*.cc'
}

exportedHeaders {
srcDirs 'src/include', 'src/cpp/'
}
}
}
binaries.all {
cppCompiler.args "-I${file('src/main/cpp').absolutePath}"
}

wpi.cpp.deps.wpilib(it)
}
}
}
115 changes: 115 additions & 0 deletions sleipnir/src/cpp/autodiff/VariableMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) Sleipnir contributors

#include "sleipnir/autodiff/VariableMatrix.hpp"

#include <Eigen/QR>

namespace Eigen {

template <>
struct NumTraits<sleipnir::Variable> : NumTraits<double> {
using Real = sleipnir::Variable;
using NonInteger = sleipnir::Variable;
using Nested = sleipnir::Variable;

enum {
IsComplex = 0,
IsInteger = 0,
IsSigned = 1,
RequireInitialization = 1,
ReadCost = 1,
AddCost = 3,
MulCost = 3
};
};

} // namespace Eigen

// For Variable equality operator
#include "sleipnir/optimization/Constraints.hpp"

namespace sleipnir {

VariableMatrix Solve(const VariableMatrix& A, const VariableMatrix& B) {
// m x n * n x p = m x p
Assert(A.Rows() == B.Rows());

if (A.Rows() == 1 && A.Cols() == 1) {
// Compute optimal inverse instead of using Eigen's general solver
return B(0, 0) / A(0, 0);
} else if (A.Rows() == 2 && A.Cols() == 2) {
// Compute optimal inverse instead of using Eigen's general solver
//
// [a b]⁻¹ ___1___ [ d −b]
// [c d] = ad − bc [−c a]

const auto& a = A(0, 0);
const auto& b = A(0, 1);
const auto& c = A(1, 0);
const auto& d = A(1, 1);

sleipnir::VariableMatrix Ainv{{d, -b}, {-c, a}};
auto detA = a * d - b * c;
Ainv /= detA;

return Ainv * B;
} else if (A.Rows() == 3 && A.Cols() == 3) {
// Compute optimal inverse instead of using Eigen's general solver
//
// [a b c]⁻¹
// [d e f]
// [g h i]
// 1 [ei − fh ch − bi bf − ce]
// = --------------------------------- [fg − di ai − cg cd − af]
// aei − afh − bdi + bfg + cdh − ceg [dh − eg bg − ah ae − bd]

const auto& a = A(0, 0);
const auto& b = A(0, 1);
const auto& c = A(0, 2);
const auto& d = A(1, 0);
const auto& e = A(1, 1);
const auto& f = A(1, 2);
const auto& g = A(2, 0);
const auto& h = A(2, 1);
const auto& i = A(2, 2);

sleipnir::VariableMatrix Ainv{
{e * i - f * h, c * h - b * i, b * f - c * e},
{f * g - d * i, a * i - c * g, c * d - a * f},
{d * h - e * g, b * g - a * h, a * e - b * d}};
auto detA =
a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
Ainv /= detA;

return Ainv * B;
} else {
using MatrixXv = Eigen::Matrix<Variable, Eigen::Dynamic, Eigen::Dynamic>;

MatrixXv eigenA{A.Rows(), A.Cols()};
for (int row = 0; row < A.Rows(); ++row) {
for (int col = 0; col < A.Cols(); ++col) {
eigenA(row, col) = A(row, col);
}
}

MatrixXv eigenB{B.Rows(), B.Cols()};
for (int row = 0; row < B.Rows(); ++row) {
for (int col = 0; col < B.Cols(); ++col) {
eigenB(row, col) = B(row, col);
}
}

MatrixXv eigenX = eigenA.householderQr().solve(eigenB);

VariableMatrix X{A.Cols(), B.Cols()};
for (int row = 0; row < X.Rows(); ++row) {
for (int col = 0; col < X.Cols(); ++col) {
X(row, col) = eigenX(row, col);
}
}

return X;
}
}

} // namespace sleipnir
58 changes: 58 additions & 0 deletions sleipnir/src/cpp/optimization/Inertia.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <cstddef>

#include "sleipnir/util/Concepts.hpp"

namespace sleipnir {

/**
* Represents the inertia of a matrix (the number of positive, negative, and
* zero eigenvalues).
*/
class Inertia {
public:
size_t positive = 0;
size_t negative = 0;
size_t zero = 0;

constexpr Inertia() = default;

/**
* Constructs the Inertia type with the given number of positive, negative,
* and zero eigenvalues.
*
* @param positive The number of positive eigenvalues.
* @param negative The number of negative eigenvalues.
* @param zero The number of zero eigenvalues.
*/
constexpr Inertia(size_t positive, size_t negative, size_t zero)
: positive{positive}, negative{negative}, zero{zero} {}

/**
* Constructs the Inertia type with the inertia of the given LDLT
* decomposition.
*
* @tparam Solver Eigen sparse linear system solver.
* @param solver The LDLT decomposition of which to compute the inertia.
*/
template <EigenSolver Solver>
explicit Inertia(const Solver& solver) {
const auto& D = solver.vectorD();
for (int row = 0; row < D.rows(); ++row) {
if (D(row) > 0.0) {
++positive;
} else if (D(row) < 0.0) {
++negative;
} else {
++zero;
}
}
}

bool operator==(const Inertia&) const = default;
};

} // namespace sleipnir
Loading

0 comments on commit 7e47e4d

Please sign in to comment.