Skip to content

Commit d9bb29a

Browse files
committed
[upstream_utils] Upgrade Sleipnir
1 parent 7a04d6a commit d9bb29a

File tree

65 files changed

+5669
-4415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5669
-4415
lines changed

benchmark/src/main/native/cpp/CartPoleBenchmark.hpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
#include "wpi/math/system/NumericalIntegration.hpp"
1313

14-
inline slp::VariableMatrix CartPoleDynamics(const slp::VariableMatrix& x,
15-
const slp::VariableMatrix& u) {
14+
inline slp::VariableMatrix<double> CartPoleDynamics(
15+
const slp::VariableMatrix<double>& x,
16+
const slp::VariableMatrix<double>& u) {
1617
constexpr double m_c = 5.0; // Cart mass (kg)
1718
constexpr double m_p = 0.5; // Pole mass (kg)
1819
constexpr double l = 0.5; // Pole length (m)
@@ -25,23 +26,23 @@ inline slp::VariableMatrix CartPoleDynamics(const slp::VariableMatrix& x,
2526

2627
// [ m_c + m_p m_p l cosθ]
2728
// M(q) = [m_p l cosθ m_p l² ]
28-
slp::VariableMatrix M{{m_c + m_p, m_p * l * cos(theta)},
29-
{m_p * l * cos(theta), m_p * std::pow(l, 2)}};
29+
slp::VariableMatrix<double> M{{m_c + m_p, m_p * l * cos(theta)},
30+
{m_p * l * cos(theta), m_p * std::pow(l, 2)}};
3031

3132
// [0 −m_p lθ̇ sinθ]
3233
// C(q, q̇) = [0 0 ]
33-
slp::VariableMatrix C{{0, -m_p * l * thetadot * sin(theta)}, {0, 0}};
34+
slp::VariableMatrix<double> C{{0, -m_p * l * thetadot * sin(theta)}, {0, 0}};
3435

3536
// [ 0 ]
3637
// τ_g(q) = [-m_p gl sinθ]
37-
slp::VariableMatrix tau_g{{0}, {-m_p * g * l * sin(theta)}};
38+
slp::VariableMatrix<double> tau_g{{0}, {-m_p * g * l * sin(theta)}};
3839

3940
// [1]
4041
// B = [0]
4142
constexpr Eigen::Matrix<double, 2, 1> B{{1}, {0}};
4243

4344
// q̈ = M⁻¹(q)(τ_g(q) − C(q, q̇)q̇ + Bu)
44-
slp::VariableMatrix qddot{4};
45+
slp::VariableMatrix<double> qddot{4};
4546
qddot.segment(0, 2) = qdot;
4647
qddot.segment(2, 2) = solve(M, tau_g - C * qdot + B * u);
4748
return qddot;
@@ -63,7 +64,7 @@ inline void BM_CartPole(benchmark::State& state) {
6364
constexpr Eigen::Vector<double, 4> x_final{
6465
{1.0, std::numbers::pi, 0.0, 0.0}};
6566

66-
slp::Problem problem;
67+
slp::Problem<double> problem;
6768

6869
// x = [q, q̇]ᵀ = [x, θ, ẋ, θ̇]ᵀ
6970
auto X = problem.decision_variable(4, N + 1);
@@ -95,11 +96,11 @@ inline void BM_CartPole(benchmark::State& state) {
9596

9697
// Dynamics constraints - RK4 integration
9798
for (int k = 0; k < N; ++k) {
98-
problem.subject_to(
99-
X.col(k + 1) ==
100-
wpi::math::RK4<decltype(CartPoleDynamics), slp::VariableMatrix,
101-
slp::VariableMatrix>(CartPoleDynamics, X.col(k),
102-
U.col(k), dt));
99+
problem.subject_to(X.col(k + 1) ==
100+
wpi::math::RK4<decltype(CartPoleDynamics),
101+
slp::VariableMatrix<double>,
102+
slp::VariableMatrix<double>>(
103+
CartPoleDynamics, X.col(k), U.col(k), dt));
103104
}
104105

105106
// Minimize sum squared inputs

docs/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ doxygen.sourceSets.main {
9797
exclude 'wpi/util/bit.hpp'
9898
exclude 'wpi/util/raw_ostream.hpp'
9999

100+
// Sleipnir
101+
exclude 'sleipnir/optimization/solver/interior_point.hpp'
102+
exclude 'sleipnir/optimization/solver/newton.hpp'
103+
exclude 'sleipnir/optimization/solver/sqp.hpp'
104+
100105
// apriltag
101106
exclude 'apriltag_pose.h'
102107

upstream_utils/sleipnir.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def copy_upstream_src(wpilib_root: Path):
1818

1919
# Copy Sleipnir files into allwpilib
2020
walk_cwd_and_copy_if(
21-
lambda dp, f: (has_prefix(dp, Path("include")) or has_prefix(dp, Path("src")))
22-
and f not in [".styleguide", ".styleguide-license"],
21+
lambda dp, f: (has_prefix(dp, Path("include")) or has_prefix(dp, Path("src"))),
2322
wpimath / "src/main/native/thirdparty/sleipnir",
2423
)
2524

@@ -49,8 +48,8 @@ def copy_upstream_src(wpilib_root: Path):
4948
def main():
5049
name = "sleipnir"
5150
url = "https://github.com/SleipnirGroup/Sleipnir"
52-
# main on 2025-09-19
53-
tag = "7f89d5547702a09e3617bc31fe5bafe6add04fab"
51+
# main on 2025-11-12
52+
tag = "bcc6039c4ef2b40375ce01c85a0732aa63716c11"
5453

5554
sleipnir = Lib(name, url, tag, copy_upstream_src)
5655
sleipnir.main()

upstream_utils/sleipnir_patches/0001-Use-fmtlib.patch

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,41 @@ Date: Wed, 29 May 2024 16:29:55 -0700
44
Subject: [PATCH 1/8] Use fmtlib
55

66
---
7-
include/.styleguide | 1 +
8-
include/sleipnir/util/assert.hpp | 5 +++--
9-
include/sleipnir/util/print.hpp | 31 ++++++++++++++++++-------------
10-
src/.styleguide | 1 +
11-
src/optimization/problem.cpp | 1 +
12-
5 files changed, 24 insertions(+), 15 deletions(-)
7+
include/sleipnir/optimization/problem.hpp | 1 +
8+
include/sleipnir/util/assert.hpp | 5 ++--
9+
include/sleipnir/util/print.hpp | 31 +++++++++++++----------
10+
3 files changed, 22 insertions(+), 15 deletions(-)
1311

14-
diff --git a/include/.styleguide b/include/.styleguide
15-
index 1b6652d3d5886cf8c9eca0d855c21031775bad7c..4f4c76204071f90bf49eddb8c2aceb583b5e09ba 100644
16-
--- a/include/.styleguide
17-
+++ b/include/.styleguide
18-
@@ -8,5 +8,6 @@ cppSrcFileInclude {
12+
diff --git a/include/sleipnir/optimization/problem.hpp b/include/sleipnir/optimization/problem.hpp
13+
index 3185466605b6604068e2807e461d07d8c856c505..95a33952a5a368c7c81491dbe849a8096357dc38 100644
14+
--- a/include/sleipnir/optimization/problem.hpp
15+
+++ b/include/sleipnir/optimization/problem.hpp
16+
@@ -15,6 +15,7 @@
1917

20-
includeOtherLibs {
21-
^Eigen/
22-
+ ^fmt/
23-
^gch/
24-
}
18+
#include <Eigen/Core>
19+
#include <Eigen/SparseCore>
20+
+#include <fmt/chrono.h>
21+
#include <gch/small_vector.hpp>
22+
23+
#include "sleipnir/autodiff/expression_type.hpp"
2524
diff --git a/include/sleipnir/util/assert.hpp b/include/sleipnir/util/assert.hpp
26-
index 75d8ffca32accbf66ffce30f073de1db2f42469b..53de01928b929793fa77885ec4a6d1a928bdc5a9 100644
25+
index 0846928c3da7a6047a3c271dd2d377a3b755eeab..5d432608def05b6dee6b7cbdb9a0b91a6ab5e1c2 100644
2726
--- a/include/sleipnir/util/assert.hpp
2827
+++ b/include/sleipnir/util/assert.hpp
29-
@@ -3,9 +3,10 @@
30-
#pragma once
28+
@@ -4,10 +4,11 @@
29+
30+
#ifdef SLEIPNIR_PYTHON
3131

32-
#ifdef JORMUNGANDR
3332
-#include <format>
3433
#include <source_location>
3534
#include <stdexcept>
36-
+
35+
3736
+#include <fmt/format.h>
37+
+
3838
/**
3939
* Throw an exception in Python.
4040
*/
41-
@@ -13,7 +14,7 @@
41+
@@ -15,7 +16,7 @@
4242
do { \
4343
if (!(condition)) { \
4444
auto location = std::source_location::current(); \
@@ -48,7 +48,7 @@ index 75d8ffca32accbf66ffce30f073de1db2f42469b..53de01928b929793fa77885ec4a6d1a9
4848
location.line(), location.function_name(), #condition)); \
4949
} \
5050
diff --git a/include/sleipnir/util/print.hpp b/include/sleipnir/util/print.hpp
51-
index fe430352dabf4cd6a890dc8007237c7a261dfd4b..055d5c9fa246201f1d8ae7ddca00b1159aeb2a57 100644
51+
index 797df849f63d960cf10eaf847415595961868ab0..a89b7d4f9864965443405a8e79cddd8dbfc54ad3 100644
5252
--- a/include/sleipnir/util/print.hpp
5353
+++ b/include/sleipnir/util/print.hpp
5454
@@ -4,10 +4,15 @@
@@ -76,8 +76,8 @@ index fe430352dabf4cd6a890dc8007237c7a261dfd4b..055d5c9fa246201f1d8ae7ddca00b115
7676
+ * Wrapper around fmt::print() that squelches write failure exceptions.
7777
*/
7878
template <typename... T>
79-
-inline void print(std::format_string<T...> fmt, T&&... args) {
80-
+inline void print(fmt::format_string<T...> fmt, T&&... args) {
79+
-void print(std::format_string<T...> fmt, T&&... args) {
80+
+void print(fmt::format_string<T...> fmt, T&&... args) {
8181
try {
8282
- std::print(fmt, std::forward<T>(args)...);
8383
+ fmt::print(fmt, std::forward<T>(args)...);
@@ -90,8 +90,8 @@ index fe430352dabf4cd6a890dc8007237c7a261dfd4b..055d5c9fa246201f1d8ae7ddca00b115
9090
+ * Wrapper around fmt::print() that squelches write failure exceptions.
9191
*/
9292
template <typename... T>
93-
-inline void print(std::FILE* f, std::format_string<T...> fmt, T&&... args) {
94-
+inline void print(std::FILE* f, fmt::format_string<T...> fmt, T&&... args) {
93+
-void print(std::FILE* f, std::format_string<T...> fmt, T&&... args) {
94+
+void print(std::FILE* f, fmt::format_string<T...> fmt, T&&... args) {
9595
try {
9696
- std::print(f, fmt, std::forward<T>(args)...);
9797
+ fmt::print(f, fmt, std::forward<T>(args)...);
@@ -104,8 +104,8 @@ index fe430352dabf4cd6a890dc8007237c7a261dfd4b..055d5c9fa246201f1d8ae7ddca00b115
104104
+ * Wrapper around fmt::println() that squelches write failure exceptions.
105105
*/
106106
template <typename... T>
107-
-inline void println(std::format_string<T...> fmt, T&&... args) {
108-
+inline void println(fmt::format_string<T...> fmt, T&&... args) {
107+
-void println(std::format_string<T...> fmt, T&&... args) {
108+
+void println(fmt::format_string<T...> fmt, T&&... args) {
109109
try {
110110
- std::println(fmt, std::forward<T>(args)...);
111111
+ fmt::println(fmt, std::forward<T>(args)...);
@@ -118,34 +118,11 @@ index fe430352dabf4cd6a890dc8007237c7a261dfd4b..055d5c9fa246201f1d8ae7ddca00b115
118118
+ * Wrapper around fmt::println() that squelches write failure exceptions.
119119
*/
120120
template <typename... T>
121-
-inline void println(std::FILE* f, std::format_string<T...> fmt, T&&... args) {
122-
+inline void println(std::FILE* f, fmt::format_string<T...> fmt, T&&... args) {
121+
-void println(std::FILE* f, std::format_string<T...> fmt, T&&... args) {
122+
+void println(std::FILE* f, fmt::format_string<T...> fmt, T&&... args) {
123123
try {
124124
- std::println(f, fmt, std::forward<T>(args)...);
125125
+ fmt::println(f, fmt, std::forward<T>(args)...);
126126
} catch (const std::system_error&) {
127127
}
128128
}
129-
diff --git a/src/.styleguide b/src/.styleguide
130-
index 1b6652d3d5886cf8c9eca0d855c21031775bad7c..4f4c76204071f90bf49eddb8c2aceb583b5e09ba 100644
131-
--- a/src/.styleguide
132-
+++ b/src/.styleguide
133-
@@ -8,5 +8,6 @@ cppSrcFileInclude {
134-
135-
includeOtherLibs {
136-
^Eigen/
137-
+ ^fmt/
138-
^gch/
139-
}
140-
diff --git a/src/optimization/problem.cpp b/src/optimization/problem.cpp
141-
index c3331197e2365934273f57422b79fa18c2b78a5b..09828cdb6d7cddff692b9d17603dc0c11cd5a3ec 100644
142-
--- a/src/optimization/problem.cpp
143-
+++ b/src/optimization/problem.cpp
144-
@@ -11,6 +11,7 @@
145-
146-
#include <Eigen/Core>
147-
#include <Eigen/SparseCore>
148-
+#include <fmt/chrono.h>
149-
#include <gch/small_vector.hpp>
150-
151-
#include "optimization/bounds.hpp"

upstream_utils/sleipnir_patches/0002-Use-wpi-SmallVector.patch

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,72 @@ Subject: [PATCH 2/8] Use wpi::SmallVector
55

66
---
77
include/sleipnir/autodiff/expression.hpp | 4 ++--
8-
include/sleipnir/autodiff/variable.hpp | 5 ++---
8+
include/sleipnir/autodiff/variable.hpp | 4 ++--
99
include/sleipnir/autodiff/variable_matrix.hpp | 4 ++--
10-
3 files changed, 6 insertions(+), 7 deletions(-)
10+
3 files changed, 6 insertions(+), 6 deletions(-)
1111

1212
diff --git a/include/sleipnir/autodiff/expression.hpp b/include/sleipnir/autodiff/expression.hpp
13-
index bb4d8c5641a5b3d633d372674e0a35f857889cd4..53a5f6d68d3153537840c4ff45fe5e5d8b0076b7 100644
13+
index f5919de6c9c0be044335ce7764ded545215f0486..46814576a3db9f472329b880b94b1ab98d218867 100644
1414
--- a/include/sleipnir/autodiff/expression.hpp
1515
+++ b/include/sleipnir/autodiff/expression.hpp
16-
@@ -30,7 +30,7 @@ inline constexpr bool USE_POOL_ALLOCATOR = true;
17-
struct Expression;
18-
19-
inline constexpr void inc_ref_count(Expression* expr);
20-
-inline constexpr void dec_ref_count(Expression* expr);
21-
+inline void dec_ref_count(Expression* expr);
16+
@@ -33,7 +33,7 @@ struct Expression;
17+
template <typename Scalar>
18+
constexpr void inc_ref_count(Expression<Scalar>* expr);
19+
template <typename Scalar>
20+
-constexpr void dec_ref_count(Expression<Scalar>* expr);
21+
+void dec_ref_count(Expression<Scalar>* expr);
2222

2323
/**
2424
* Typedef for intrusive shared pointer to Expression.
25-
@@ -733,7 +733,7 @@ inline constexpr void inc_ref_count(Expression* expr) {
26-
*
25+
@@ -801,7 +801,7 @@ constexpr void inc_ref_count(Expression<Scalar>* expr) {
2726
* @param expr The shared pointer's managed object.
2827
*/
29-
-inline constexpr void dec_ref_count(Expression* expr) {
30-
+inline void dec_ref_count(Expression* expr) {
28+
template <typename Scalar>
29+
-constexpr void dec_ref_count(Expression<Scalar>* expr) {
30+
+void dec_ref_count(Expression<Scalar>* expr) {
3131
// If a deeply nested tree is being deallocated all at once, calling the
3232
// Expression destructor when expr's refcount reaches zero can cause a stack
3333
// overflow. Instead, we iterate over its children to decrement their
3434
diff --git a/include/sleipnir/autodiff/variable.hpp b/include/sleipnir/autodiff/variable.hpp
35-
index f60236811eba45c67a9638e90d5101d877ecc2d0..264f0950f293c67d6e6c7e729887090c050e40e2 100644
35+
index c78af7224b2ef93ad50b238117583e01940c53ce..0a55b906130d7506c80eb150644ac44c222d1368 100644
3636
--- a/include/sleipnir/autodiff/variable.hpp
3737
+++ b/include/sleipnir/autodiff/variable.hpp
38-
@@ -47,7 +47,7 @@ class SLEIPNIR_DLLEXPORT Variable {
38+
@@ -61,7 +61,7 @@ class Variable : public SleipnirBase {
3939
/**
4040
* Constructs an empty Variable.
4141
*/
4242
- explicit constexpr Variable(std::nullptr_t) : expr{nullptr} {}
4343
+ explicit Variable(std::nullptr_t) : expr{nullptr} {}
4444

4545
/**
46-
* Constructs a Variable from a floating point type.
47-
@@ -77,8 +77,7 @@ class SLEIPNIR_DLLEXPORT Variable {
46+
* Constructs a Variable from a scalar type.
47+
@@ -116,7 +116,7 @@ class Variable : public SleipnirBase {
4848
*
4949
* @param expr The autodiff variable.
5050
*/
51-
- explicit constexpr Variable(detail::ExpressionPtr&& expr)
52-
- : expr{std::move(expr)} {}
53-
+ explicit Variable(detail::ExpressionPtr&& expr) : expr{std::move(expr)} {}
51+
- explicit constexpr Variable(detail::ExpressionPtr<Scalar>&& expr)
52+
+ explicit Variable(detail::ExpressionPtr<Scalar>&& expr)
53+
: expr{std::move(expr)} {}
5454

5555
/**
56-
* Assignment operator for double.
5756
diff --git a/include/sleipnir/autodiff/variable_matrix.hpp b/include/sleipnir/autodiff/variable_matrix.hpp
58-
index e1a419ca5356660b3c1c27230d1cb2a86977fb65..349a1550235516f9853609b61feded834ef2894b 100644
57+
index bb66bebc01413a291242886366ce329bb5f4b70a..7ddf02c0e2f66aff8da422b874cbe9772f9fd00d 100644
5958
--- a/include/sleipnir/autodiff/variable_matrix.hpp
6059
+++ b/include/sleipnir/autodiff/variable_matrix.hpp
61-
@@ -1120,14 +1120,14 @@ class SLEIPNIR_DLLEXPORT VariableMatrix {
60+
@@ -1281,14 +1281,14 @@ class VariableMatrix : public SleipnirBase {
6261
*
63-
* @return Begin iterator.
62+
* @return Const begin iterator.
6463
*/
6564
- const_iterator cbegin() const { return const_iterator{m_storage.cbegin()}; }
6665
+ const_iterator cbegin() const { return const_iterator{m_storage.begin()}; }
6766

6867
/**
69-
* Returns end iterator.
68+
* Returns const end iterator.
7069
*
71-
* @return End iterator.
70+
* @return Const end iterator.
7271
*/
7372
- const_iterator cend() const { return const_iterator{m_storage.cend()}; }
7473
+ const_iterator cend() const { return const_iterator{m_storage.end()}; }
7574

7675
/**
77-
* Returns number of elements in matrix.
76+
* Returns reverse begin iterator.

upstream_utils/sleipnir_patches/0003-Use-wpi-byteswap.patch

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@ Date: Tue, 28 Jan 2025 22:19:14 -0800
44
Subject: [PATCH 3/8] Use wpi::byteswap()
55

66
---
7-
include/.styleguide | 1 +
87
include/sleipnir/util/spy.hpp | 3 ++-
9-
2 files changed, 3 insertions(+), 1 deletion(-)
8+
1 file changed, 2 insertions(+), 1 deletion(-)
109

11-
diff --git a/include/.styleguide b/include/.styleguide
12-
index 4f4c76204071f90bf49eddb8c2aceb583b5e09ba..03938557c2600a7a1f72c6b93c935602f5acb2b2 100644
13-
--- a/include/.styleguide
14-
+++ b/include/.styleguide
15-
@@ -10,4 +10,5 @@ includeOtherLibs {
16-
^Eigen/
17-
^fmt/
18-
^gch/
19-
+ ^wpi/
20-
}
2110
diff --git a/include/sleipnir/util/spy.hpp b/include/sleipnir/util/spy.hpp
22-
index a2f94803e3744cee771669210d1af883160e9896..74dd7990b03783ce805a186920d5142caeb178c6 100644
11+
index f9143f2b925064e9df5c763823dcf3d435e7aa28..4b810e54a8038162e03cf08fc8eab52b67b2cdd5 100644
2312
--- a/include/sleipnir/util/spy.hpp
2413
+++ b/include/sleipnir/util/spy.hpp
2514
@@ -12,6 +12,7 @@
@@ -28,9 +17,9 @@ index a2f94803e3744cee771669210d1af883160e9896..74dd7990b03783ce805a186920d5142c
2817
#include <Eigen/SparseCore>
2918
+#include <wpi/util/bit.hpp>
3019

31-
#include "sleipnir/util/symbol_exports.hpp"
20+
namespace slp {
3221

33-
@@ -115,7 +116,7 @@ class SLEIPNIR_DLLEXPORT Spy {
22+
@@ -114,7 +115,7 @@ class Spy {
3423
*/
3524
void write32le(int32_t num) {
3625
if constexpr (std::endian::native != std::endian::little) {

0 commit comments

Comments
 (0)