Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import numpy |
| 4 | import sys, os |
| 5 | import casadi |
| 6 | from numpy.testing import assert_array_equal, assert_array_almost_equal |
| 7 | import unittest |
| 8 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 9 | from frc971.control_loops.swerve import bigcaster_dynamics |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 10 | from frc971.control_loops.swerve import dynamics |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 11 | from frc971.control_loops.swerve import nocaster_dynamics |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 12 | from frc971.control_loops.swerve import physics_test_utils as utils |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 13 | |
| 14 | |
Austin Schuh | 6927bc3 | 2024-07-14 17:24:56 -0700 | [diff] [blame] | 15 | class TestSwervePhysics(unittest.TestCase): |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 16 | I = numpy.zeros((8, 1)) |
| 17 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 18 | def wrap(self, python_module): |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 19 | self.swerve_physics = utils.wrap(python_module.swerve_physics) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 20 | self.contact_patch_velocity = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 21 | utils.wrap_module(python_module.contact_patch_velocity, i) |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 22 | for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 23 | ] |
| 24 | self.wheel_ground_velocity = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 25 | utils.wrap_module(python_module.wheel_ground_velocity, i) |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 26 | for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 27 | ] |
| 28 | self.wheel_slip_velocity = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 29 | utils.wrap_module(python_module.wheel_slip_velocity, i) |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 30 | for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 31 | ] |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 32 | self.wheel_force = [ |
| 33 | utils.wrap_module(python_module.wheel_force, i) for i in range(4) |
| 34 | ] |
| 35 | self.module_angular_accel = [ |
| 36 | utils.wrap_module(python_module.module_angular_accel, i) |
| 37 | for i in range(4) |
| 38 | ] |
| 39 | self.F = [utils.wrap_module(python_module.F, i) for i in range(4)] |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 40 | self.mounting_location = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 41 | utils.wrap_module(python_module.mounting_location, i) |
| 42 | for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 43 | ] |
| 44 | |
| 45 | self.slip_angle = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 46 | utils.wrap_module(python_module.slip_angle, i) for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 47 | ] |
| 48 | self.slip_ratio = [ |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 49 | utils.wrap_module(python_module.slip_ratio, i) for i in range(4) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 50 | ] |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 51 | self.Ms = [utils.wrap_module(python_module.Ms, i) for i in range(4)] |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 52 | |
| 53 | def setUp(self): |
| 54 | self.wrap(dynamics) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 55 | |
| 56 | def test_contact_patch_velocity(self): |
| 57 | """Tests that the contact patch velocity makes sense.""" |
| 58 | for i in range(4): |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 59 | contact_patch_velocity = self.contact_patch_velocity[i] |
| 60 | wheel_ground_velocity = self.wheel_ground_velocity[i] |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 61 | |
| 62 | # No angular velocity should result in just linear motion. |
| 63 | for velocity in [ |
| 64 | numpy.array([[1.5], [0.0]]), |
| 65 | numpy.array([[0.0], [1.0]]), |
| 66 | numpy.array([[-1.5], [0.0]]), |
| 67 | numpy.array([[0.0], [-1.0]]), |
| 68 | numpy.array([[2.0], [-1.7]]), |
| 69 | ]: |
| 70 | for theta in [0.0, 1.0, -1.0, 100.0]: |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 71 | patch_velocity = contact_patch_velocity( |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 72 | utils.state_vector(velocity=velocity, theta=theta), |
| 73 | self.I) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 74 | |
| 75 | assert_array_equal(patch_velocity, velocity) |
| 76 | |
| 77 | # Now, test that spinning the robot results in module velocities. |
| 78 | # We are assuming that each module is on a square robot. |
| 79 | module_center_of_mass_angle = i * numpy.pi / 2.0 + numpy.pi / 4.0 |
| 80 | for theta in [-module_center_of_mass_angle, 0.0, 1.0, -1.0, 100.0]: |
| 81 | for omega in [0.65, -0.1]: |
| 82 | # Point each module to the center to make the math easier. |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 83 | patch_velocity = contact_patch_velocity( |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 84 | utils.state_vector( |
| 85 | velocity=numpy.array([[0.0], [0.0]]), |
| 86 | theta=theta, |
| 87 | omega=omega, |
| 88 | module_angle=module_center_of_mass_angle, |
| 89 | ), |
| 90 | self.I, |
| 91 | ) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 92 | |
| 93 | assert_array_almost_equal( |
| 94 | patch_velocity, |
Austin Schuh | 6927bc3 | 2024-07-14 17:24:56 -0700 | [diff] [blame] | 95 | (dynamics.ROBOT_WIDTH / numpy.sqrt(2.0) - |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 96 | dynamics.CASTER) * omega * numpy.array([ |
| 97 | [-numpy.sin(theta + module_center_of_mass_angle)], |
| 98 | [numpy.cos(theta + module_center_of_mass_angle)], |
| 99 | ]), |
| 100 | ) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 101 | |
| 102 | # Point the wheel along +x, rotate it by theta, then spin it. |
| 103 | # Confirm the velocities come out right. |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 104 | patch_velocity = contact_patch_velocity( |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 105 | utils.state_vector( |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 106 | velocity=numpy.array([[0.0], [0.0]]), |
| 107 | theta=-module_center_of_mass_angle, |
| 108 | module_omega=omega, |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 109 | module_angle=(theta + module_center_of_mass_angle), |
| 110 | ), |
| 111 | self.I, |
| 112 | ) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 113 | |
| 114 | assert_array_almost_equal( |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 115 | patch_velocity, |
| 116 | -dynamics.CASTER * omega * |
| 117 | numpy.array([[-numpy.sin(theta)], [numpy.cos(theta)]]), |
| 118 | ) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 119 | |
| 120 | # Now, test that the rotation back to wheel coordinates works. |
| 121 | # The easiest way to do this is to point the wheel in a direction, |
| 122 | # move in that direction, and confirm that there is no lateral velocity. |
| 123 | for robot_angle in [0.0, 1.0, -5.0]: |
| 124 | for module_angle in [0.0, 1.0, -5.0]: |
| 125 | wheel_patch_velocity = numpy.array( |
| 126 | wheel_ground_velocity( |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 127 | utils.state_vector( |
| 128 | velocity=numpy.array([ |
| 129 | [numpy.cos(robot_angle + module_angle)], |
| 130 | [numpy.sin(robot_angle + module_angle)], |
| 131 | ]), |
| 132 | theta=robot_angle, |
| 133 | module_angle=module_angle, |
| 134 | ), |
| 135 | self.I, |
| 136 | )) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 137 | |
| 138 | assert_array_almost_equal(wheel_patch_velocity, |
| 139 | numpy.array([[1], [0]])) |
| 140 | |
| 141 | def test_slip_angle(self): |
| 142 | """Tests that the slip_angle calculation works.""" |
| 143 | velocity = numpy.array([[1.5], [0.0]]) |
| 144 | |
| 145 | for i in range(4): |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 146 | for wrap in range(-1, 2): |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 147 | for theta in [0.0, 0.6, -0.4]: |
| 148 | module_angle = numpy.pi * wrap + theta |
| 149 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 150 | # We have redefined the angle to be the sin of the angle. |
| 151 | # That way, when the module flips directions, the slip angle also flips |
| 152 | # directions to keep it stable. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 153 | computed_angle = self.slip_angle[i]( |
| 154 | utils.state_vector(velocity=velocity, |
| 155 | module_angle=numpy.pi * wrap + |
| 156 | theta), |
| 157 | self.I, |
| 158 | )[0, 0] |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 159 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 160 | expected = numpy.sin(numpy.pi * wrap + theta) |
| 161 | |
| 162 | self.assertAlmostEqual( |
| 163 | expected, |
| 164 | computed_angle, |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 165 | msg=f"Trying wrap {wrap} theta {theta}", |
| 166 | ) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 167 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 168 | def test_wheel_torque(self): |
| 169 | """Tests that the per module self aligning forces have the right signs.""" |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 170 | # Point all the modules in a little bit. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 171 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 172 | velocity=numpy.array([[1.0], [0.0]]), |
| 173 | module_angles=[-0.001, -0.001, 0.001, 0.001], |
| 174 | ) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 175 | xdot_equal = self.swerve_physics(X, self.I) |
| 176 | |
| 177 | self.assertGreater(xdot_equal[2, 0], 0.0) |
| 178 | self.assertAlmostEqual(xdot_equal[3, 0], 0.0, places=1) |
| 179 | self.assertGreater(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 180 | |
| 181 | self.assertGreater(xdot_equal[6, 0], 0.0) |
| 182 | self.assertAlmostEqual(xdot_equal[7, 0], 0.0, places=1) |
| 183 | self.assertGreater(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 184 | |
| 185 | self.assertLess(xdot_equal[10, 0], 0.0) |
| 186 | self.assertAlmostEqual(xdot_equal[11, 0], 0.0, places=1) |
| 187 | self.assertLess(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 188 | |
| 189 | self.assertLess(xdot_equal[14, 0], 0.0) |
| 190 | self.assertAlmostEqual(xdot_equal[15, 0], 0.0, places=1) |
| 191 | self.assertLess(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 192 | |
| 193 | # Shouldn't be spinning. |
| 194 | self.assertAlmostEqual(xdot_equal[21, 0], 0.0, places=2) |
| 195 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 196 | # Now, make the bot want to go left by going to the other side. |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 197 | # The wheels will be going too fast based on our calcs, so they should be decelerating. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 198 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 199 | velocity=numpy.array([[1.0], [0.0]]), |
| 200 | module_angles=[0.01, 0.01, 0.01, 0.01], |
| 201 | ) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 202 | xdot_left = self.swerve_physics(X, self.I) |
| 203 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 204 | self.assertLess(xdot_left[2, 0], -0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 205 | self.assertLess(xdot_left[3, 0], 0.0) |
| 206 | self.assertLess(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 207 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 208 | self.assertLess(xdot_left[6, 0], -0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 209 | self.assertLess(xdot_left[7, 0], 0.0) |
| 210 | self.assertLess(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 211 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 212 | self.assertLess(xdot_left[10, 0], -0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 213 | self.assertLess(xdot_left[11, 0], 0.0) |
| 214 | self.assertLess(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 215 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 216 | self.assertLess(xdot_left[14, 0], -0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 217 | self.assertLess(xdot_left[15, 0], 0.0) |
| 218 | self.assertLess(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 219 | |
| 220 | self.assertGreater(xdot_left[19, 0], 0.0001) |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 221 | self.assertGreater(xdot_left[20, 0], 0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 222 | # Shouldn't be spinning. |
| 223 | self.assertAlmostEqual(xdot_left[21, 0], 0.0) |
| 224 | |
| 225 | # And now do it to the right too. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 226 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 227 | velocity=numpy.array([[1.0], [0.0]]), |
| 228 | module_angles=[-0.01, -0.01, -0.01, -0.01], |
| 229 | ) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 230 | xdot_right = self.swerve_physics(X, self.I) |
| 231 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 232 | self.assertGreater(xdot_right[2, 0], 0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 233 | self.assertLess(xdot_right[3, 0], 0.0) |
| 234 | self.assertGreater(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 235 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 236 | self.assertGreater(xdot_right[6, 0], 0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 237 | self.assertLess(xdot_right[7, 0], 0.0) |
| 238 | self.assertGreater(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 239 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 240 | self.assertGreater(xdot_right[10, 0], 0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 241 | self.assertLess(xdot_right[11, 0], 0.0) |
| 242 | self.assertGreater(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 243 | |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 244 | self.assertGreater(xdot_right[14, 0], 0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 245 | self.assertLess(xdot_right[15, 0], 0.0) |
| 246 | self.assertGreater(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 247 | |
| 248 | self.assertGreater(xdot_right[19, 0], 0.0001) |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 249 | self.assertLess(xdot_right[20, 0], -0.05) |
| 250 | # Shouldn't be spinning. |
| 251 | self.assertAlmostEqual(xdot_right[21, 0], 0.0) |
| 252 | |
| 253 | def test_wheel_torque_backwards_nocaster(self): |
| 254 | """Tests that the per module self aligning forces have the right signs when going backwards.""" |
| 255 | self.wrap(nocaster_dynamics) |
| 256 | # Point all the modules in a little bit, going backwards. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 257 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 258 | velocity=numpy.array([[1.0], [0.0]]), |
| 259 | module_angles=[ |
| 260 | numpy.pi - 0.001, |
| 261 | numpy.pi - 0.001, |
| 262 | numpy.pi + 0.001, |
| 263 | numpy.pi + 0.001, |
| 264 | ], |
| 265 | drive_wheel_velocity=-1, |
| 266 | ) |
| 267 | xdot_equal = self.swerve_physics(X, self.I) |
| 268 | |
| 269 | self.assertGreater(xdot_equal[2, 0], 0.0, msg="Steering backwards") |
| 270 | self.assertAlmostEqual(xdot_equal[3, 0], 0.0, places=1) |
| 271 | self.assertGreater(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 272 | |
| 273 | self.assertGreater(xdot_equal[6, 0], 0.0, msg="Steering backwards") |
| 274 | self.assertAlmostEqual(xdot_equal[7, 0], 0.0, places=1) |
| 275 | self.assertGreater(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 276 | |
| 277 | self.assertLess(xdot_equal[10, 0], 0.0, msg="Steering backwards") |
| 278 | self.assertAlmostEqual(xdot_equal[11, 0], 0.0, places=1) |
| 279 | self.assertLess(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 280 | |
| 281 | self.assertLess(xdot_equal[14, 0], 0.0, msg="Steering backwards") |
| 282 | self.assertAlmostEqual(xdot_equal[15, 0], 0.0, places=1) |
| 283 | self.assertLess(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 284 | |
| 285 | # Shouldn't be spinning. |
| 286 | self.assertAlmostEqual(xdot_equal[21, 0], 0.0, places=2) |
| 287 | |
| 288 | # Now, make the bot want to go left by going to the other side. |
| 289 | # The wheels will be going too fast based on our calcs, so they should be decelerating. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 290 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 291 | velocity=numpy.array([[1.0], [0.0]]), |
| 292 | module_angles=[numpy.pi + 0.01] * 4, |
| 293 | drive_wheel_velocity=-1, |
| 294 | ) |
| 295 | xdot_left = self.swerve_physics(X, self.I) |
| 296 | |
| 297 | self.assertLess(xdot_left[2, 0], -0.05) |
| 298 | self.assertGreater(xdot_left[3, 0], 0.0) |
| 299 | self.assertLess(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 300 | |
| 301 | self.assertLess(xdot_left[6, 0], -0.05) |
| 302 | self.assertGreater(xdot_left[7, 0], 0.0) |
| 303 | self.assertLess(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 304 | |
| 305 | self.assertLess(xdot_left[10, 0], -0.05) |
| 306 | self.assertGreater(xdot_left[11, 0], 0.0) |
| 307 | self.assertLess(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 308 | |
| 309 | self.assertLess(xdot_left[14, 0], -0.05) |
| 310 | self.assertGreater(xdot_left[15, 0], 0.0) |
| 311 | self.assertLess(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 312 | |
| 313 | self.assertGreater(xdot_left[19, 0], 0.0001) |
| 314 | self.assertGreater(xdot_left[20, 0], 0.05) |
| 315 | # Shouldn't be spinning. |
| 316 | self.assertAlmostEqual(xdot_left[21, 0], 0.0) |
| 317 | |
| 318 | # And now do it to the right too. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 319 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 320 | velocity=numpy.array([[1.0], [0.0]]), |
| 321 | drive_wheel_velocity=-1, |
| 322 | module_angles=[-0.01 + numpy.pi] * 4, |
| 323 | ) |
| 324 | xdot_right = self.swerve_physics(X, self.I) |
| 325 | |
| 326 | self.assertGreater(xdot_right[2, 0], 0.05) |
| 327 | self.assertGreater(xdot_right[3, 0], 0.0) |
| 328 | self.assertGreater(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 329 | |
| 330 | self.assertGreater(xdot_right[6, 0], 0.05) |
| 331 | self.assertGreater(xdot_right[7, 0], 0.0) |
| 332 | self.assertGreater(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 333 | |
| 334 | self.assertGreater(xdot_right[10, 0], 0.05) |
| 335 | self.assertGreater(xdot_right[11, 0], 0.0) |
| 336 | self.assertGreater(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 337 | |
| 338 | self.assertGreater(xdot_right[14, 0], 0.05) |
| 339 | self.assertGreater(xdot_right[15, 0], 0.0) |
| 340 | self.assertGreater(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 341 | |
| 342 | self.assertGreater(xdot_right[19, 0], 0.0001) |
| 343 | self.assertLess(xdot_right[20, 0], -0.05) |
| 344 | # Shouldn't be spinning. |
| 345 | self.assertAlmostEqual(xdot_right[21, 0], 0.0) |
| 346 | |
| 347 | def test_wheel_torque_backwards_caster(self): |
| 348 | """Tests that the per module self aligning forces have the right signs when going backwards with a lot of caster.""" |
| 349 | self.wrap(bigcaster_dynamics) |
| 350 | # Point all the modules in a little bit, going backwards. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 351 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 352 | velocity=numpy.array([[1.0], [0.0]]), |
| 353 | module_angles=[ |
| 354 | numpy.pi - 0.001, |
| 355 | numpy.pi - 0.001, |
| 356 | numpy.pi + 0.001, |
| 357 | numpy.pi + 0.001, |
| 358 | ], |
| 359 | drive_wheel_velocity=-1, |
| 360 | ) |
| 361 | xdot_equal = self.swerve_physics(X, self.I) |
| 362 | |
| 363 | self.assertLess(xdot_equal[2, 0], 0.0, msg="Steering backwards") |
| 364 | self.assertAlmostEqual(xdot_equal[3, 0], 0.0, places=1) |
| 365 | self.assertLess(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 366 | |
| 367 | self.assertLess(xdot_equal[6, 0], 0.0, msg="Steering backwards") |
| 368 | self.assertAlmostEqual(xdot_equal[7, 0], 0.0, places=1) |
| 369 | self.assertLess(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 370 | |
| 371 | self.assertGreater(xdot_equal[10, 0], 0.0, msg="Steering backwards") |
| 372 | self.assertAlmostEqual(xdot_equal[11, 0], 0.0, places=1) |
| 373 | self.assertGreater(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 374 | |
| 375 | self.assertGreater(xdot_equal[14, 0], 0.0, msg="Steering backwards") |
| 376 | self.assertAlmostEqual(xdot_equal[15, 0], 0.0, places=1) |
| 377 | self.assertGreater(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 378 | |
| 379 | # Shouldn't be spinning. |
| 380 | self.assertAlmostEqual(xdot_equal[21, 0], 0.0, places=2) |
| 381 | |
| 382 | # Now, make the bot want to go left by going to the other side. |
| 383 | # The wheels will be going too fast based on our calcs, so they should be decelerating. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 384 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 385 | velocity=numpy.array([[1.0], [0.0]]), |
| 386 | module_angles=[numpy.pi + 0.01] * 4, |
| 387 | drive_wheel_velocity=-1, |
| 388 | ) |
| 389 | xdot_left = self.swerve_physics(X, self.I) |
| 390 | |
| 391 | self.assertGreater(xdot_left[2, 0], -0.05) |
| 392 | self.assertGreater(xdot_left[3, 0], 0.0) |
| 393 | self.assertGreater(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 394 | |
| 395 | self.assertGreater(xdot_left[6, 0], -0.05) |
| 396 | self.assertGreater(xdot_left[7, 0], 0.0) |
| 397 | self.assertGreater(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 398 | |
| 399 | self.assertGreater(xdot_left[10, 0], -0.05) |
| 400 | self.assertGreater(xdot_left[11, 0], 0.0) |
| 401 | self.assertGreater(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 402 | |
| 403 | self.assertGreater(xdot_left[14, 0], -0.05) |
| 404 | self.assertGreater(xdot_left[15, 0], 0.0) |
| 405 | self.assertGreater(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 406 | |
| 407 | self.assertGreater(xdot_left[19, 0], 0.0001) |
| 408 | self.assertGreater(xdot_left[20, 0], 0.05) |
| 409 | # Shouldn't be spinning. |
| 410 | self.assertAlmostEqual(xdot_left[21, 0], 0.0) |
| 411 | |
| 412 | # And now do it to the right too. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 413 | X = utils.state_vector( |
Austin Schuh | 27694fa | 2024-07-20 16:29:49 -0700 | [diff] [blame] | 414 | velocity=numpy.array([[1.0], [0.0]]), |
| 415 | drive_wheel_velocity=-1, |
| 416 | module_angles=[-0.01 + numpy.pi] * 4, |
| 417 | ) |
| 418 | xdot_right = self.swerve_physics(X, self.I) |
| 419 | |
| 420 | self.assertLess(xdot_right[2, 0], 0.05) |
| 421 | self.assertGreater(xdot_right[3, 0], 0.0) |
| 422 | self.assertLess(self.Ms[0](X, self.I)[0, 0], 0.0) |
| 423 | |
| 424 | self.assertLess(xdot_right[6, 0], 0.05) |
| 425 | self.assertGreater(xdot_right[7, 0], 0.0) |
| 426 | self.assertLess(self.Ms[1](X, self.I)[0, 0], 0.0) |
| 427 | |
| 428 | self.assertLess(xdot_right[10, 0], 0.05) |
| 429 | self.assertGreater(xdot_right[11, 0], 0.0) |
| 430 | self.assertLess(self.Ms[2](X, self.I)[0, 0], 0.0) |
| 431 | |
| 432 | self.assertLess(xdot_right[14, 0], 0.05) |
| 433 | self.assertGreater(xdot_right[15, 0], 0.0) |
| 434 | self.assertLess(self.Ms[3](X, self.I)[0, 0], 0.0) |
| 435 | |
| 436 | self.assertGreater(xdot_right[19, 0], 0.0001) |
| 437 | self.assertLess(xdot_right[20, 0], -0.05) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 438 | # Shouldn't be spinning. |
| 439 | self.assertAlmostEqual(xdot_right[21, 0], 0.0) |
| 440 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 441 | def test_wheel_forces(self): |
| 442 | """Tests that the per module forces have the right signs.""" |
| 443 | for i in range(4): |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 444 | wheel_force = self.wheel_force[i] |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 445 | |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 446 | X = utils.state_vector() |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 447 | robot_equal = wheel_force(X, self.I) |
| 448 | xdot_equal = self.swerve_physics(X, self.I) |
| 449 | self.assertEqual(robot_equal[0, 0], 0.0) |
| 450 | self.assertEqual(robot_equal[1, 0], 0.0) |
| 451 | self.assertEqual(xdot_equal[2 + 4 * i], 0.0) |
| 452 | self.assertEqual(xdot_equal[3 + 4 * i], 0.0) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 453 | |
| 454 | # Robot is moving faster than the wheels, it should decelerate. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 455 | X = utils.state_vector(dx=0.01) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 456 | robot_faster = wheel_force(X, self.I) |
| 457 | xdot_faster = self.swerve_physics(X, self.I) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 458 | self.assertLess(robot_faster[0, 0], -0.1) |
| 459 | self.assertEqual(robot_faster[1, 0], 0.0) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 460 | self.assertGreater(xdot_faster[3 + 4 * i], 0.0) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 461 | |
| 462 | # Robot is now going slower than the wheels. It should accelerate. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 463 | X = utils.state_vector(dx=-0.01) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 464 | robot_slower = wheel_force(X, self.I) |
| 465 | xdot_slower = self.swerve_physics(X, self.I) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 466 | self.assertGreater(robot_slower[0, 0], 0.1) |
| 467 | self.assertEqual(robot_slower[1, 0], 0.0) |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 468 | self.assertLess(xdot_slower[3 + 4 * i], 0.0) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 469 | |
| 470 | # Positive lateral velocity -> negative force. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 471 | robot_left = wheel_force(utils.state_vector(dy=0.01), self.I) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 472 | self.assertEqual(robot_left[0, 0], 0.0) |
| 473 | self.assertLess(robot_left[1, 0], -0.1) |
| 474 | |
| 475 | # Negative lateral velocity -> positive force. |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 476 | robot_right = wheel_force(utils.state_vector(dy=-0.01), self.I) |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 477 | self.assertEqual(robot_right[0, 0], 0.0) |
| 478 | self.assertGreater(robot_right[1, 0], 0.1) |
| 479 | |
| 480 | |
justinT21 | 07d41ed | 2024-07-31 21:12:31 -0700 | [diff] [blame] | 481 | if __name__ == "__main__": |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 482 | unittest.main() |