|
| 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 | +from typing import TYPE_CHECKING, List, Tuple |
| 6 | +import math |
| 7 | + |
| 8 | +import wpimath.controller as controller |
| 9 | +import wpimath.trajectory as trajectory |
| 10 | +import wpimath.geometry as geometry |
| 11 | +import wpimath.kinematics as kinematics |
| 12 | +from wpimath.trajectory import TrapezoidProfile as DimensionlessProfile |
| 13 | +from wpimath.trajectory import TrapezoidProfileRadians as RadiansProfile |
| 14 | + |
| 15 | +from wpilib import Timer |
| 16 | + |
| 17 | +from util import * # type: ignore |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from .util import * |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +import commands2 |
| 25 | + |
| 26 | + |
| 27 | +class TrapezoidProfileRadiansFixture: |
| 28 | + def __init__(self): |
| 29 | + constraints: RadiansProfile.Constraints = RadiansProfile.Constraints( |
| 30 | + 3 * math.pi, math.pi |
| 31 | + ) |
| 32 | + self._profile: RadiansProfile = RadiansProfile(constraints) |
| 33 | + self._goal_state = RadiansProfile.State(3, 0) |
| 34 | + |
| 35 | + self._state = self._profile.calculate( |
| 36 | + 0, self._goal_state, RadiansProfile.State(0, 0) |
| 37 | + ) |
| 38 | + |
| 39 | + self._timer = Timer() |
| 40 | + |
| 41 | + def profileOutput(self, state: RadiansProfile.State) -> None: |
| 42 | + self._state = state |
| 43 | + |
| 44 | + def currentState(self) -> RadiansProfile.State: |
| 45 | + return self._state |
| 46 | + |
| 47 | + def getGoal(self) -> RadiansProfile.State: |
| 48 | + return self._goal_state |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture() |
| 52 | +def get_trapezoid_profile_radians() -> TrapezoidProfileRadiansFixture: |
| 53 | + return TrapezoidProfileRadiansFixture() |
| 54 | + |
| 55 | + |
| 56 | +class TrapezoidProfileFixture: |
| 57 | + def __init__(self): |
| 58 | + constraints: DimensionlessProfile.Constraints = ( |
| 59 | + DimensionlessProfile.Constraints(3 * math.pi, math.pi) |
| 60 | + ) |
| 61 | + self._profile: DimensionlessProfile = DimensionlessProfile(constraints) |
| 62 | + self._goal_state = DimensionlessProfile.State(3, 0) |
| 63 | + |
| 64 | + self._state = self._profile.calculate( |
| 65 | + 0, self._goal_state, DimensionlessProfile.State(0, 0) |
| 66 | + ) |
| 67 | + |
| 68 | + self._timer = Timer() |
| 69 | + |
| 70 | + def profileOutput(self, state: DimensionlessProfile.State) -> None: |
| 71 | + self._state = state |
| 72 | + |
| 73 | + def currentState(self) -> DimensionlessProfile.State: |
| 74 | + return self._state |
| 75 | + |
| 76 | + def getGoal(self) -> DimensionlessProfile.State: |
| 77 | + return self._goal_state |
| 78 | + |
| 79 | + |
| 80 | +@pytest.fixture() |
| 81 | +def get_trapezoid_profile_dimensionless() -> TrapezoidProfileFixture: |
| 82 | + return TrapezoidProfileFixture() |
| 83 | + |
| 84 | + |
| 85 | +def test_trapezoidProfileDimensionless( |
| 86 | + scheduler: commands2.CommandScheduler, get_trapezoid_profile_dimensionless |
| 87 | +): |
| 88 | + with ManualSimTime() as sim: |
| 89 | + subsystem = commands2.Subsystem() |
| 90 | + |
| 91 | + fixture_data = get_trapezoid_profile_dimensionless |
| 92 | + |
| 93 | + command = commands2.TrapezoidProfileCommand[ |
| 94 | + DimensionlessProfile, DimensionlessProfile.State |
| 95 | + ]( |
| 96 | + fixture_data._profile, |
| 97 | + fixture_data.profileOutput, |
| 98 | + fixture_data.getGoal, |
| 99 | + fixture_data.currentState, |
| 100 | + subsystem, |
| 101 | + ) |
| 102 | + |
| 103 | + fixture_data._timer.restart() |
| 104 | + |
| 105 | + command.initialize() |
| 106 | + |
| 107 | + count = 0 |
| 108 | + while not command.isFinished(): |
| 109 | + command.execute() |
| 110 | + count += 1 |
| 111 | + sim.step(0.005) |
| 112 | + |
| 113 | + fixture_data._timer.stop() |
| 114 | + command.end(True) |
| 115 | + |
| 116 | + |
| 117 | +def test_trapezoidProfileRadians( |
| 118 | + scheduler: commands2.CommandScheduler, get_trapezoid_profile_radians |
| 119 | +): |
| 120 | + with ManualSimTime() as sim: |
| 121 | + subsystem = commands2.Subsystem() |
| 122 | + |
| 123 | + fixture_data = get_trapezoid_profile_radians |
| 124 | + |
| 125 | + command = commands2.TrapezoidProfileCommand[ |
| 126 | + RadiansProfile, RadiansProfile.State |
| 127 | + ]( |
| 128 | + fixture_data._profile, |
| 129 | + fixture_data.profileOutput, |
| 130 | + fixture_data.getGoal, |
| 131 | + fixture_data.currentState, |
| 132 | + subsystem, |
| 133 | + ) |
| 134 | + |
| 135 | + fixture_data._timer.restart() |
| 136 | + |
| 137 | + command.initialize() |
| 138 | + |
| 139 | + count = 0 |
| 140 | + while not command.isFinished(): |
| 141 | + command.execute() |
| 142 | + count += 1 |
| 143 | + sim.step(0.005) |
| 144 | + |
| 145 | + fixture_data._timer.stop() |
| 146 | + command.end(True) |
0 commit comments