-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shoot from range using trajectory optimisation
- Loading branch information
1 parent
17258d1
commit 3416f51
Showing
54 changed files
with
12,909 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ modifiableFileExclude { | |
gradlew.bat | ||
src/main/java/com | ||
src/main/java/frc/robot/URCL.java | ||
sleipnir/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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/' | ||
} | ||
} | ||
} | ||
|
||
wpi.cpp.deps.wpilib(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.