-
Notifications
You must be signed in to change notification settings - Fork 25
Add frc/system/NumericalIntegration.h #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
subprojects/robotpy-wpimath/semiwrap/controls/NumericalIntegration.yml
This file contains hidden or 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 @@ | ||
| defaults: | ||
| subpackage: system | ||
|
|
||
| extra_includes: | ||
| - frc_eigen.h | ||
| - frc/EigenCore.h | ||
| - pybind11/functional.h | ||
|
|
||
| functions: | ||
| RK4: | ||
| overloads: | ||
| F&&, T, units::second_t: | ||
| template_impls: | ||
| - [std::function<Eigen::MatrixXd(Eigen::MatrixXd)>, Eigen::MatrixXd] | ||
| F&&, T, U, units::second_t: | ||
| template_impls: | ||
| - ["std::function<Eigen::MatrixXd(Eigen::MatrixXd, Eigen::MatrixXd)>", Eigen::MatrixXd, Eigen::MatrixXd] | ||
| F&&, units::second_t, T, units::second_t: | ||
| template_impls: | ||
| - ["std::function<Eigen::MatrixXd(units::second_t, Eigen::MatrixXd)>", Eigen::MatrixXd] | ||
| RKDP: | ||
| overloads: | ||
| F&&, T, U, units::second_t, double: | ||
| template_impls: | ||
| - ["std::function<Eigen::MatrixXd(Eigen::MatrixXd, Eigen::MatrixXd)>", Eigen::MatrixXd, Eigen::MatrixXd] | ||
| F&&, units::second_t, T, units::second_t, double: | ||
| template_impls: | ||
| - ["std::function<Eigen::MatrixXd(units::second_t, Eigen::MatrixXd)>", Eigen::MatrixXd] | ||
55 changes: 55 additions & 0 deletions
55
subprojects/robotpy-wpimath/semiwrap/controls/NumericalJacobian.yml
This file contains hidden or 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,55 @@ | ||
| defaults: | ||
| subpackage: system | ||
|
|
||
| extra_includes: | ||
| - frc_eigen.h | ||
| - frc/EigenCore.h | ||
| - pybind11/functional.h | ||
| - pybind11/typing.h | ||
|
|
||
| functions: | ||
| NumericalJacobian: | ||
| overloads: | ||
| F&&, const Vectord<Cols>&: | ||
| ignore: true | ||
| F&&, const Eigen::VectorXd&: | ||
| template_impls: | ||
| - [std::function<Eigen::VectorXd(Eigen::VectorXd)>] | ||
| NumericalJacobianX: | ||
| overloads: | ||
| F&&, const Vectord<States>&, const Vectord<Inputs>&, Args&&...: | ||
| ignore: true | ||
| F&&, const Eigen::VectorXd&, const Eigen::VectorXd&, Args&&...: | ||
| # template_impls: | ||
| # - ["std::function<Eigen::VectorXd(Eigen::VectorXd, Eigen::VectorXd, py::args)>", py::args, Eigen::MatrixXd] | ||
| param_override: | ||
| args: | ||
| ignore: true | ||
| no_release_gil: true | ||
| cpp_code: | | ||
| [](py::typing::Callable<Eigen::VectorXd(Eigen::VectorXd, Eigen::VectorXd, py::args)> fn, | ||
| const Eigen::VectorXd& x, const Eigen::VectorXd& u, py::args args) { | ||
| return frc::NumericalJacobianX([=](const Eigen::VectorXd &ix, const Eigen::VectorXd &iu) { | ||
| py::object r = fn(ix, iu, *args); | ||
| return r.cast<Eigen::VectorXd>(); | ||
| }, x, u); | ||
| } | ||
| NumericalJacobianU: | ||
| overloads: | ||
| F&&, const Vectord<States>&, const Vectord<Inputs>&, Args&&...: | ||
| ignore: true | ||
| F&&, const Eigen::VectorXd&, const Eigen::VectorXd&, Args&&...: | ||
| # template_impls: | ||
| # - ["std::function<Eigen::VectorXd(Eigen::VectorXd, Eigen::VectorXd, py::args)>", py::args, Eigen::MatrixXd] | ||
| param_override: | ||
| args: | ||
| ignore: true | ||
| no_release_gil: true | ||
| cpp_code: | | ||
| [](py::typing::Callable<Eigen::VectorXd(Eigen::VectorXd, Eigen::VectorXd, py::args)> fn, | ||
| const Eigen::VectorXd& x, const Eigen::VectorXd& u, py::args args) { | ||
| return frc::NumericalJacobianU([=](const Eigen::VectorXd &ix, const Eigen::VectorXd &iu) { | ||
| py::object r = fn(ix, iu, *args); | ||
| return r.cast<Eigen::VectorXd>(); | ||
| }, x, u); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,202 @@ | ||
| import math | ||
|
|
||
| import wpimath.system | ||
| import wpimath.system.plant | ||
|
|
||
| import pytest | ||
| import numpy as np | ||
|
|
||
|
|
||
| def test_rk4_exponential(): | ||
virtuald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Test that integrating dx/dt = eˣ works""" | ||
| y0 = np.array([[0.0]]) | ||
|
|
||
| y1 = wpimath.system.RK4(lambda x: np.array([[math.exp(x[0, 0])]]), y0, 0.1) | ||
|
|
||
| assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_rk4_exponential_with_u(): | ||
| """Test that integrating dx/dt = eˣ works when we provide a u""" | ||
| y0 = np.array([[0.0]]) | ||
|
|
||
| y1 = wpimath.system.RK4( | ||
| lambda x, u: np.array([[math.exp(u[0, 0] * x[0, 0])]]), | ||
| y0, | ||
| np.array([[1.0]]), | ||
| 0.1, | ||
| ) | ||
|
|
||
| assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_rk4_time_varying(): | ||
| """ | ||
| Tests RK4 with a time varying solution. From | ||
| http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html: | ||
|
|
||
| dx/dt = x (2 / (eᵗ + 1) - 1) | ||
|
|
||
| The true (analytical) solution is: | ||
|
|
||
| x(t) = 12eᵗ/(eᵗ + 1)² | ||
| """ | ||
| y0 = np.array([[12.0 * math.exp(5.0) / math.pow(math.exp(5.0) + 1.0, 2.0)]]) | ||
|
|
||
| y1 = wpimath.system.RK4( | ||
| lambda t, x: np.array([[x[0, 0] * (2.0 / (math.exp(t) + 1.0) - 1.0)]]), | ||
| 5.0, | ||
| y0, | ||
| 1.0, | ||
| ) | ||
|
|
||
| expected = 12.0 * math.exp(6.0) / math.pow(math.exp(6.0) + 1.0, 2.0) | ||
| assert math.isclose(y1[0, 0], expected, abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_rkdp_zero(): | ||
| """Tests that integrating dx/dt = 0 works with RKDP""" | ||
| y1 = wpimath.system.RKDP( | ||
| lambda x, u: np.zeros((1, 1)), | ||
| np.array([[0.0]]), | ||
| np.array([[0.0]]), | ||
| 0.1, | ||
| ) | ||
|
|
||
| assert math.isclose(y1[0, 0], 0.0, abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_rkdp_exponential(): | ||
| """Tests that integrating dx/dt = eˣ works with RKDP""" | ||
| y0 = np.array([[0.0]]) | ||
|
|
||
| y1 = wpimath.system.RKDP( | ||
| lambda x, u: np.array([[math.exp(x[0, 0])]]), | ||
| y0, | ||
| np.array([[0.0]]), | ||
| 0.1, | ||
| ) | ||
|
|
||
| assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_rkdp_time_varying(): | ||
| """ | ||
| Tests RKDP with a time varying solution. From | ||
| http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html: | ||
|
|
||
| dx/dt = x(2/(eᵗ + 1) - 1) | ||
|
|
||
| The true (analytical) solution is: | ||
|
|
||
| x(t) = 12eᵗ/(eᵗ + 1)² | ||
| """ | ||
| y0 = np.array([[12.0 * math.exp(5.0) / math.pow(math.exp(5.0) + 1.0, 2.0)]]) | ||
|
|
||
| y1 = wpimath.system.RKDP( | ||
| lambda t, x: np.array([[x[0, 0] * (2.0 / (math.exp(t) + 1.0) - 1.0)]]), | ||
| 5.0, | ||
| y0, | ||
| 1.0, | ||
| 1e-12, | ||
| ) | ||
|
|
||
| expected = 12.0 * math.exp(6.0) / math.pow(math.exp(6.0) + 1.0, 2.0) | ||
| assert math.isclose(y1[0, 0], expected, abs_tol=1e-3) | ||
|
|
||
|
|
||
| def test_numerical_jacobian(): | ||
| """Test that we can recover A from ax_fn() pretty accurately""" | ||
| a = np.array( | ||
| [ | ||
| [1.0, 2.0, 4.0, 1.0], | ||
| [5.0, 2.0, 3.0, 4.0], | ||
| [5.0, 1.0, 3.0, 2.0], | ||
| [1.0, 1.0, 3.0, 7.0], | ||
| ] | ||
| ) | ||
|
|
||
| def ax_fn(x): | ||
| return a @ x | ||
|
|
||
| new_a = wpimath.system.numericalJacobian(ax_fn, np.zeros((4, 1))) | ||
| np.testing.assert_allclose(new_a, a, rtol=1e-6, atol=1e-5) | ||
|
|
||
|
|
||
| def test_numerical_jacobian_x_u_square(): | ||
| """Test that we can recover B from axbu_fn() pretty accurately""" | ||
| a = np.array( | ||
| [ | ||
| [1.0, 2.0, 4.0, 1.0], | ||
| [5.0, 2.0, 3.0, 4.0], | ||
| [5.0, 1.0, 3.0, 2.0], | ||
| [1.0, 1.0, 3.0, 7.0], | ||
| ] | ||
| ) | ||
| b = np.array([[1.0, 1.0], [2.0, 1.0], [3.0, 2.0], [3.0, 7.0]]) | ||
|
|
||
| def axbu_fn(x, u): | ||
| return a @ x + b @ u | ||
|
|
||
| x0 = np.zeros((4, 1)) | ||
| u0 = np.zeros((2, 1)) | ||
| new_a = wpimath.system.numericalJacobianX(axbu_fn, x0, u0) | ||
| new_b = wpimath.system.numericalJacobianU(axbu_fn, x0, u0) | ||
| np.testing.assert_allclose(new_a, a, rtol=1e-6, atol=1e-5) | ||
| np.testing.assert_allclose(new_b, b, rtol=1e-6, atol=1e-5) | ||
|
|
||
|
|
||
| def test_numerical_jacobian_x_u_rectangular(): | ||
| c = np.array( | ||
| [ | ||
| [1.0, 2.0, 4.0, 1.0], | ||
| [5.0, 2.0, 3.0, 4.0], | ||
| [5.0, 1.0, 3.0, 2.0], | ||
| ] | ||
| ) | ||
| d = np.array([[1.0, 1.0], [2.0, 1.0], [3.0, 2.0]]) | ||
|
|
||
| def cxdu_fn(x, u): | ||
| return c @ x + d @ u | ||
|
|
||
| x0 = np.zeros((4, 1)) | ||
| u0 = np.zeros((2, 1)) | ||
| new_c = wpimath.system.numericalJacobianX(cxdu_fn, x0, u0) | ||
| new_d = wpimath.system.numericalJacobianU(cxdu_fn, x0, u0) | ||
| np.testing.assert_allclose(new_c, c, rtol=1e-6, atol=1e-5) | ||
| np.testing.assert_allclose(new_d, d, rtol=1e-6, atol=1e-5) | ||
|
|
||
|
|
||
| def test_numerical_jacobian_x_passes_extra_args(): | ||
| a = np.array([[2.0, -1.0], [0.5, 3.0]]) | ||
| b = np.array([[1.0], [4.0]]) | ||
| x0 = np.zeros((2, 1)) | ||
| u0 = np.zeros((1, 1)) | ||
|
|
||
| seen = {} | ||
|
|
||
| def axbu_fn(x, u, scale, bias): | ||
| seen["args"] = (scale, bias) | ||
| return scale * (a @ x) + bias * (b @ u) | ||
|
|
||
| new_a = wpimath.system.numericalJacobianX(axbu_fn, x0, u0, 2.5, -3.0) | ||
|
|
||
| assert seen["args"] == (2.5, -3.0) | ||
| np.testing.assert_allclose(new_a, 2.5 * a, rtol=1e-6, atol=1e-5) | ||
|
|
||
|
|
||
| def test_numerical_jacobian_u_passes_extra_args(): | ||
| a = np.array([[1.0, 0.0], [0.0, -2.0]]) | ||
| b = np.array([[1.5], [-0.5]]) | ||
| x0 = np.zeros((2, 1)) | ||
| u0 = np.zeros((1, 1)) | ||
|
|
||
| seen = {} | ||
|
|
||
| def axbu_fn(x, u, scale, bias): | ||
| seen["args"] = (scale, bias) | ||
| return scale * (a @ x) + bias * (b @ u) | ||
|
|
||
| new_b = wpimath.system.numericalJacobianU(axbu_fn, x0, u0, 4.0, 0.25) | ||
|
|
||
| def test_todo(): | ||
| pass | ||
| assert seen["args"] == (4.0, 0.25) | ||
| np.testing.assert_allclose(new_b, 0.25 * b, rtol=1e-6, atol=1e-5) | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.