|
| 1 | +import math |
| 2 | + |
1 | 3 | import wpimath.system |
2 | | -import wpimath.system.plant |
3 | 4 |
|
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +def test_rk4_exponential(): |
| 10 | + """Test that integrating dx/dt = eˣ works""" |
| 11 | + y0 = np.array([[0.0]]) |
| 12 | + |
| 13 | + y1 = wpimath.system.RK4(lambda x: np.array([[math.exp(x[0, 0])]]), y0, 0.1) |
| 14 | + |
| 15 | + assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) |
| 16 | + |
| 17 | + |
| 18 | +def test_rk4_exponential_with_u(): |
| 19 | + """Test that integrating dx/dt = eˣ works when we provide a u""" |
| 20 | + y0 = np.array([[0.0]]) |
| 21 | + |
| 22 | + y1 = wpimath.system.RK4( |
| 23 | + lambda x, u: np.array([[math.exp(u[0, 0] * x[0, 0])]]), |
| 24 | + y0, |
| 25 | + np.array([[1.0]]), |
| 26 | + 0.1, |
| 27 | + ) |
| 28 | + |
| 29 | + assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) |
| 30 | + |
| 31 | + |
| 32 | +def test_rk4_time_varying(): |
| 33 | + """ |
| 34 | + Tests RK4 with a time varying solution. From |
| 35 | + http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html: |
| 36 | +
|
| 37 | + dx/dt = x (2 / (eᵗ + 1) - 1) |
| 38 | +
|
| 39 | + The true (analytical) solution is: |
| 40 | +
|
| 41 | + x(t) = 12eᵗ/(eᵗ + 1)² |
| 42 | + """ |
| 43 | + y0 = np.array([[12.0 * math.exp(5.0) / math.pow(math.exp(5.0) + 1.0, 2.0)]]) |
| 44 | + |
| 45 | + y1 = wpimath.system.RK4( |
| 46 | + lambda t, x: np.array([[x[0, 0] * (2.0 / (math.exp(t) + 1.0) - 1.0)]]), |
| 47 | + 5.0, |
| 48 | + y0, |
| 49 | + 1.0, |
| 50 | + ) |
| 51 | + |
| 52 | + expected = 12.0 * math.exp(6.0) / math.pow(math.exp(6.0) + 1.0, 2.0) |
| 53 | + assert math.isclose(y1[0, 0], expected, abs_tol=1e-3) |
| 54 | + |
| 55 | + |
| 56 | +def test_rkdp_zero(): |
| 57 | + """Tests that integrating dx/dt = 0 works with RKDP""" |
| 58 | + y1 = wpimath.system.RKDP( |
| 59 | + lambda x, u: np.zeros((1, 1)), |
| 60 | + np.array([[0.0]]), |
| 61 | + np.array([[0.0]]), |
| 62 | + 0.1, |
| 63 | + ) |
| 64 | + |
| 65 | + assert math.isclose(y1[0, 0], 0.0, abs_tol=1e-3) |
| 66 | + |
| 67 | + |
| 68 | +def test_rkdp_exponential(): |
| 69 | + """Tests that integrating dx/dt = eˣ works with RKDP""" |
| 70 | + y0 = np.array([[0.0]]) |
| 71 | + |
| 72 | + y1 = wpimath.system.RKDP( |
| 73 | + lambda x, u: np.array([[math.exp(x[0, 0])]]), |
| 74 | + y0, |
| 75 | + np.array([[0.0]]), |
| 76 | + 0.1, |
| 77 | + ) |
| 78 | + |
| 79 | + assert math.isclose(y1[0, 0], math.exp(0.1) - math.exp(0.0), abs_tol=1e-3) |
| 80 | + |
| 81 | + |
| 82 | +def test_rkdp_time_varying(): |
| 83 | + """ |
| 84 | + Tests RKDP with a time varying solution. From |
| 85 | + http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html: |
| 86 | +
|
| 87 | + dx/dt = x(2/(eᵗ + 1) - 1) |
| 88 | +
|
| 89 | + The true (analytical) solution is: |
| 90 | +
|
| 91 | + x(t) = 12eᵗ/(eᵗ + 1)² |
| 92 | + """ |
| 93 | + y0 = np.array([[12.0 * math.exp(5.0) / math.pow(math.exp(5.0) + 1.0, 2.0)]]) |
| 94 | + |
| 95 | + y1 = wpimath.system.RKDP( |
| 96 | + lambda t, x: np.array([[x[0, 0] * (2.0 / (math.exp(t) + 1.0) - 1.0)]]), |
| 97 | + 5.0, |
| 98 | + y0, |
| 99 | + 1.0, |
| 100 | + 1e-12, |
| 101 | + ) |
| 102 | + |
| 103 | + expected = 12.0 * math.exp(6.0) / math.pow(math.exp(6.0) + 1.0, 2.0) |
| 104 | + assert math.isclose(y1[0, 0], expected, abs_tol=1e-3) |
| 105 | + |
| 106 | + |
| 107 | +def test_numerical_jacobian(): |
| 108 | + """Test that we can recover A from ax_fn() pretty accurately""" |
| 109 | + a = np.array( |
| 110 | + [ |
| 111 | + [1.0, 2.0, 4.0, 1.0], |
| 112 | + [5.0, 2.0, 3.0, 4.0], |
| 113 | + [5.0, 1.0, 3.0, 2.0], |
| 114 | + [1.0, 1.0, 3.0, 7.0], |
| 115 | + ] |
| 116 | + ) |
| 117 | + |
| 118 | + def ax_fn(x): |
| 119 | + return a @ x |
| 120 | + |
| 121 | + new_a = wpimath.system.numericalJacobian(ax_fn, np.zeros((4, 1))) |
| 122 | + np.testing.assert_allclose(new_a, a, rtol=1e-6, atol=1e-5) |
| 123 | + |
| 124 | + |
| 125 | +def test_numerical_jacobian_x_u_square(): |
| 126 | + """Test that we can recover B from axbu_fn() pretty accurately""" |
| 127 | + a = np.array( |
| 128 | + [ |
| 129 | + [1.0, 2.0, 4.0, 1.0], |
| 130 | + [5.0, 2.0, 3.0, 4.0], |
| 131 | + [5.0, 1.0, 3.0, 2.0], |
| 132 | + [1.0, 1.0, 3.0, 7.0], |
| 133 | + ] |
| 134 | + ) |
| 135 | + b = np.array([[1.0, 1.0], [2.0, 1.0], [3.0, 2.0], [3.0, 7.0]]) |
| 136 | + |
| 137 | + def axbu_fn(x, u): |
| 138 | + return a @ x + b @ u |
| 139 | + |
| 140 | + x0 = np.zeros((4, 1)) |
| 141 | + u0 = np.zeros((2, 1)) |
| 142 | + new_a = wpimath.system.numericalJacobianX(axbu_fn, x0, u0) |
| 143 | + new_b = wpimath.system.numericalJacobianU(axbu_fn, x0, u0) |
| 144 | + np.testing.assert_allclose(new_a, a, rtol=1e-6, atol=1e-5) |
| 145 | + np.testing.assert_allclose(new_b, b, rtol=1e-6, atol=1e-5) |
| 146 | + |
| 147 | + |
| 148 | +def test_numerical_jacobian_x_u_rectangular(): |
| 149 | + c = np.array( |
| 150 | + [ |
| 151 | + [1.0, 2.0, 4.0, 1.0], |
| 152 | + [5.0, 2.0, 3.0, 4.0], |
| 153 | + [5.0, 1.0, 3.0, 2.0], |
| 154 | + ] |
| 155 | + ) |
| 156 | + d = np.array([[1.0, 1.0], [2.0, 1.0], [3.0, 2.0]]) |
| 157 | + |
| 158 | + def cxdu_fn(x, u): |
| 159 | + return c @ x + d @ u |
| 160 | + |
| 161 | + x0 = np.zeros((4, 1)) |
| 162 | + u0 = np.zeros((2, 1)) |
| 163 | + new_c = wpimath.system.numericalJacobianX(cxdu_fn, x0, u0) |
| 164 | + new_d = wpimath.system.numericalJacobianU(cxdu_fn, x0, u0) |
| 165 | + np.testing.assert_allclose(new_c, c, rtol=1e-6, atol=1e-5) |
| 166 | + np.testing.assert_allclose(new_d, d, rtol=1e-6, atol=1e-5) |
| 167 | + |
| 168 | + |
| 169 | +def test_numerical_jacobian_x_passes_extra_args(): |
| 170 | + a = np.array([[2.0, -1.0], [0.5, 3.0]]) |
| 171 | + b = np.array([[1.0], [4.0]]) |
| 172 | + x0 = np.zeros((2, 1)) |
| 173 | + u0 = np.zeros((1, 1)) |
| 174 | + |
| 175 | + seen = {} |
| 176 | + |
| 177 | + def axbu_fn(x, u, scale, bias): |
| 178 | + seen["args"] = (scale, bias) |
| 179 | + return scale * (a @ x) + bias * (b @ u) |
| 180 | + |
| 181 | + new_a = wpimath.system.numericalJacobianX(axbu_fn, x0, u0, 2.5, -3.0) |
| 182 | + |
| 183 | + assert seen["args"] == (2.5, -3.0) |
| 184 | + np.testing.assert_allclose(new_a, 2.5 * a, rtol=1e-6, atol=1e-5) |
| 185 | + |
| 186 | + |
| 187 | +def test_numerical_jacobian_u_passes_extra_args(): |
| 188 | + a = np.array([[1.0, 0.0], [0.0, -2.0]]) |
| 189 | + b = np.array([[1.5], [-0.5]]) |
| 190 | + x0 = np.zeros((2, 1)) |
| 191 | + u0 = np.zeros((1, 1)) |
| 192 | + |
| 193 | + seen = {} |
| 194 | + |
| 195 | + def axbu_fn(x, u, scale, bias): |
| 196 | + seen["args"] = (scale, bias) |
| 197 | + return scale * (a @ x) + bias * (b @ u) |
| 198 | + |
| 199 | + new_b = wpimath.system.numericalJacobianU(axbu_fn, x0, u0, 4.0, 0.25) |
4 | 200 |
|
5 | | -def test_todo(): |
6 | | - pass |
| 201 | + assert seen["args"] == (4.0, 0.25) |
| 202 | + np.testing.assert_allclose(new_b, 0.25 * b, rtol=1e-6, atol=1e-5) |
0 commit comments