Skip to content

Commit 7e667cd

Browse files
committed
[wpimath] Add Sleipnir Java bindings
1 parent 1e50471 commit 7e667cd

File tree

76 files changed

+11650
-201
lines changed

Some content is hidden

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

76 files changed

+11650
-201
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020
id 'net.ltgt.errorprone' version '4.3.0' apply false
2121
id 'com.gradleup.shadow' version '9.0.0' apply false
2222
id 'com.diffplug.spotless' version '7.2.1' apply false
23-
id 'com.github.spotbugs' version '6.2.3' apply false
23+
id 'com.github.spotbugs' version '6.4.2' apply false
2424
}
2525

2626
wpilibVersioning.buildServerMode = project.hasProperty('buildServer')

wpimath/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ cc_library(
8181
java_library(
8282
name = "wpimath-java",
8383
srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]),
84+
javacopts = [
85+
"-Xep:UnicodeInCode:OFF",
86+
],
8487
visibility = ["//visibility:public"],
8588
deps = [
8689
"//wpiunits",

wpimath/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ include(DownloadAndCheck)
77

88
file(
99
GLOB wpimath_jni_src
10-
src/main/native/cpp/jni/ArmFeedforwardJNI.cpp
1110
src/main/native/cpp/jni/DAREJNI.cpp
1211
src/main/native/cpp/jni/EigenJNI.cpp
13-
src/main/native/cpp/jni/Ellipse2dJNI.cpp
1412
src/main/native/cpp/jni/Exceptions.cpp
1513
src/main/native/cpp/jni/Pose3dJNI.cpp
1614
src/main/native/cpp/jni/StateSpaceUtilJNI.cpp
1715
src/main/native/cpp/jni/TrajectoryUtilJNI.cpp
16+
src/main/native/cpp/jni/autodiff/GradientJNI.cpp
17+
src/main/native/cpp/jni/autodiff/HessianJNI.cpp
18+
src/main/native/cpp/jni/autodiff/JacobianJNI.cpp
19+
src/main/native/cpp/jni/autodiff/VariableJNI.cpp
20+
src/main/native/cpp/jni/autodiff/VariableMatrixJNI.cpp
21+
src/main/native/cpp/jni/optimization/ProblemJNI.cpp
1822
)
1923

2024
# Java bindings
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.math.autodiff;
6+
7+
/**
8+
* Expression type.
9+
*
10+
* <p>Used for autodiff caching.
11+
*/
12+
public enum ExpressionType {
13+
/** There is no expression. */
14+
NONE(0),
15+
/** The expression is a constant. */
16+
CONSTANT(1),
17+
/** The expression is composed of linear and lower-order operators. */
18+
LINEAR(2),
19+
/** The expression is composed of quadratic and lower-order operators. */
20+
QUADRATIC(3),
21+
/** The expression is composed of nonlinear and lower-order operators. */
22+
NONLINEAR(4);
23+
24+
/** ExpressionType value. */
25+
public final int value;
26+
27+
ExpressionType(int value) {
28+
this.value = value;
29+
}
30+
31+
/**
32+
* Converts integer to its corresponding enum value.
33+
*
34+
* @param x The integer.
35+
* @return The enum value.
36+
*/
37+
public static ExpressionType fromInteger(int x) {
38+
return switch (x) {
39+
case 0 -> ExpressionType.NONE;
40+
case 1 -> ExpressionType.CONSTANT;
41+
case 2 -> ExpressionType.LINEAR;
42+
case 3 -> ExpressionType.QUADRATIC;
43+
case 4 -> ExpressionType.NONLINEAR;
44+
default -> null;
45+
};
46+
}
47+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.math.autodiff;
6+
7+
import org.ejml.data.DMatrixSparseCSC;
8+
import org.ejml.ops.DConvertMatrixStruct;
9+
import org.ejml.simple.SimpleMatrix;
10+
11+
/**
12+
* This class calculates the gradient of a variable with respect to a vector of variables.
13+
*
14+
* <p>The gradient is only recomputed if the variable expression is quadratic or higher order.
15+
*/
16+
public class Gradient implements AutoCloseable {
17+
private long m_handle;
18+
private int m_rows;
19+
20+
/**
21+
* Constructs a Gradient object.
22+
*
23+
* @param variable Variable of which to compute the gradient.
24+
* @param wrt Variable with respect to which to compute the gradient.
25+
*/
26+
public Gradient(Variable variable, Variable wrt) {
27+
this(variable, new VariableMatrix(wrt));
28+
}
29+
30+
/**
31+
* Constructs a Gradient object.
32+
*
33+
* @param variable Variable of which to compute the gradient.
34+
* @param wrt Vector of variables with respect to which to compute the gradient.
35+
*/
36+
public Gradient(Variable variable, VariableMatrix wrt) {
37+
assert wrt.cols() == 1;
38+
39+
m_handle = GradientJNI.create(variable.getHandle(), wrt.getHandles());
40+
m_rows = wrt.rows();
41+
}
42+
43+
/**
44+
* Constructs a Gradient object.
45+
*
46+
* @param variable Variable of which to compute the gradient.
47+
* @param wrt Vector of variables with respect to which to compute the gradient.
48+
*/
49+
public Gradient(Variable variable, VariableBlock wrt) {
50+
this(variable, new VariableMatrix(wrt));
51+
}
52+
53+
@Override
54+
public void close() {
55+
if (m_handle != 0) {
56+
GradientJNI.destroy(m_handle);
57+
m_handle = 0;
58+
}
59+
}
60+
61+
/**
62+
* Returns the gradient as a VariableMatrix.
63+
*
64+
* <p>This is useful when constructing optimization problems with derivatives in them.
65+
*
66+
* @return The gradient as a VariableMatrix.
67+
*/
68+
public VariableMatrix get() {
69+
return VariableMatrix.fromHandles(m_rows, 1, GradientJNI.get(m_handle));
70+
}
71+
72+
/**
73+
* Evaluates the gradient at wrt's value.
74+
*
75+
* @return The gradient at wrt's value.
76+
*/
77+
public SimpleMatrix value() {
78+
var triplet = GradientJNI.value(m_handle);
79+
return new SimpleMatrix(DConvertMatrixStruct.convert(triplet, (DMatrixSparseCSC) null));
80+
}
81+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.math.autodiff;
6+
7+
import edu.wpi.first.math.jni.WPIMathJNI;
8+
import org.ejml.data.DMatrixSparseTriplet;
9+
10+
/** Gradient JNI functions. */
11+
public final class GradientJNI extends WPIMathJNI {
12+
private GradientJNI() {
13+
// Utility class.
14+
}
15+
16+
/**
17+
* Constructs a Gradient object.
18+
*
19+
* @param variable Variable of which to compute the Gradient.
20+
* @param wrt Vector of variables with respect to which to compute the Gradient.
21+
*/
22+
static native long create(long variable, long[] wrt);
23+
24+
/**
25+
* Destructs a Gradient.
26+
*
27+
* @param handle Gradient handle.
28+
*/
29+
static native void destroy(long handle);
30+
31+
/**
32+
* Returns the Gradient as an array of Variable handles.
33+
*
34+
* <p>This is useful when constructing optimization problems with derivatives in them.
35+
*
36+
* @param handle Gradient handle.
37+
* @return The Gradient as an array of Variable handles.
38+
*/
39+
static native long[] get(long handle);
40+
41+
/**
42+
* Evaluates the Gradient at wrt's value.
43+
*
44+
* @param handle Gradient handle.
45+
* @return The Gradient at wrt's value.
46+
*/
47+
static native DMatrixSparseTriplet value(long handle);
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.math.autodiff;
6+
7+
import org.ejml.data.DMatrixSparseCSC;
8+
import org.ejml.ops.DConvertMatrixStruct;
9+
import org.ejml.simple.SimpleMatrix;
10+
11+
/**
12+
* This class calculates the Hessian of a variable with respect to a vector of variables.
13+
*
14+
* <p>The gradient tree is cached so subsequent Hessian calculations are faster, and the Hessian is
15+
* only recomputed if the variable expression is nonlinear.
16+
*/
17+
public class Hessian implements AutoCloseable {
18+
private long m_handle;
19+
private int m_rows;
20+
21+
/**
22+
* Constructs a Hessian object.
23+
*
24+
* @param variable Variable of which to compute the Hessian.
25+
* @param wrt Variable with respect to which to compute the Hessian.
26+
*/
27+
public Hessian(Variable variable, Variable wrt) {
28+
this(variable, new VariableMatrix(wrt));
29+
}
30+
31+
/**
32+
* Constructs a Hessian object.
33+
*
34+
* @param variable Variable of which to compute the Hessian.
35+
* @param wrt Vector of variables with respect to which to compute the Hessian.
36+
*/
37+
public Hessian(Variable variable, VariableMatrix wrt) {
38+
assert wrt.cols() == 1;
39+
40+
m_handle = HessianJNI.create(variable.getHandle(), wrt.getHandles());
41+
m_rows = wrt.rows();
42+
}
43+
44+
/**
45+
* Constructs a Hessian object.
46+
*
47+
* @param variable Variable of which to compute the Hessian.
48+
* @param wrt Vector of variables with respect to which to compute the Hessian.
49+
*/
50+
public Hessian(Variable variable, VariableBlock wrt) {
51+
this(variable, new VariableMatrix(wrt));
52+
}
53+
54+
@Override
55+
public void close() {
56+
if (m_handle != 0) {
57+
HessianJNI.destroy(m_handle);
58+
m_handle = 0;
59+
}
60+
}
61+
62+
/**
63+
* Returns the Hessian as a VariableMatrix.
64+
*
65+
* <p>This is useful when constructing optimization problems with derivatives in them.
66+
*
67+
* @return The Hessian as a VariableMatrix.
68+
*/
69+
public VariableMatrix get() {
70+
return VariableMatrix.fromHandles(m_rows, m_rows, HessianJNI.get(m_handle));
71+
}
72+
73+
/**
74+
* Evaluates the Hessian at wrt's value.
75+
*
76+
* @return The Hessian at wrt's value.
77+
*/
78+
public SimpleMatrix value() {
79+
var triplet = HessianJNI.value(m_handle);
80+
return new SimpleMatrix(DConvertMatrixStruct.convert(triplet, (DMatrixSparseCSC) null));
81+
}
82+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.math.autodiff;
6+
7+
import edu.wpi.first.math.jni.WPIMathJNI;
8+
import org.ejml.data.DMatrixSparseTriplet;
9+
10+
/** Hessian JNI functions. */
11+
public final class HessianJNI extends WPIMathJNI {
12+
private HessianJNI() {
13+
// Utility class.
14+
}
15+
16+
/**
17+
* Constructs a Hessian object.
18+
*
19+
* @param variable Variable of which to compute the Hessian.
20+
* @param wrt Vector of variables with respect to which to compute the Hessian.
21+
*/
22+
static native long create(long variable, long[] wrt);
23+
24+
/**
25+
* Destructs a Hessian.
26+
*
27+
* @param handle Hessian handle.
28+
*/
29+
static native void destroy(long handle);
30+
31+
/**
32+
* Returns the Hessian as an array of Variable handles.
33+
*
34+
* <p>This is useful when constructing optimization problems with derivatives in them.
35+
*
36+
* @param handle Hessian handle.
37+
* @return The Hessian as an array of Variable handles.
38+
*/
39+
static native long[] get(long handle);
40+
41+
/**
42+
* Evaluates the Hessian at wrt's value.
43+
*
44+
* @param handle Hessian handle.
45+
* @return The Hessian at wrt's value.
46+
*/
47+
static native DMatrixSparseTriplet value(long handle);
48+
}

0 commit comments

Comments
 (0)