|
| 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 | +from wpilib import Timer |
| 9 | + |
| 10 | +from wpimath.geometry import Pose2d, Rotation2d, Translation2d |
| 11 | +from wpimath.kinematics import ( |
| 12 | + SwerveModuleState, |
| 13 | + SwerveModulePosition, |
| 14 | + SwerveDrive4Kinematics, |
| 15 | + SwerveDrive4Odometry, |
| 16 | +) |
| 17 | +from wpimath.controller import ( |
| 18 | + ProfiledPIDControllerRadians, |
| 19 | + PIDController, |
| 20 | + HolonomicDriveController, |
| 21 | +) |
| 22 | +from wpimath.trajectory import ( |
| 23 | + TrapezoidProfileRadians, |
| 24 | + Trajectory, |
| 25 | + TrajectoryConfig, |
| 26 | + TrajectoryGenerator, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +from util import * # type: ignore |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + from .util import * |
| 34 | + |
| 35 | +import pytest |
| 36 | + |
| 37 | +import commands2 |
| 38 | + |
| 39 | + |
| 40 | +def test_swervecontrollercommand(): |
| 41 | + timer = Timer() |
| 42 | + angle = Rotation2d(0) |
| 43 | + |
| 44 | + swerve_module_states = ( |
| 45 | + SwerveModuleState(0, Rotation2d(0)), |
| 46 | + SwerveModuleState(0, Rotation2d(0)), |
| 47 | + SwerveModuleState(0, Rotation2d(0)), |
| 48 | + SwerveModuleState(0, Rotation2d(0)), |
| 49 | + ) |
| 50 | + |
| 51 | + swerve_module_positions = ( |
| 52 | + SwerveModulePosition(0, Rotation2d(0)), |
| 53 | + SwerveModulePosition(0, Rotation2d(0)), |
| 54 | + SwerveModulePosition(0, Rotation2d(0)), |
| 55 | + SwerveModulePosition(0, Rotation2d(0)), |
| 56 | + ) |
| 57 | + |
| 58 | + rot_controller = ProfiledPIDControllerRadians( |
| 59 | + 1, |
| 60 | + 0, |
| 61 | + 0, |
| 62 | + TrapezoidProfileRadians.Constraints(3 * math.pi, math.pi), |
| 63 | + ) |
| 64 | + |
| 65 | + x_tolerance = 1 / 12.0 |
| 66 | + y_tolerance = 1 / 12.0 |
| 67 | + angular_tolerance = 1 / 12.0 |
| 68 | + |
| 69 | + wheel_base = 0.5 |
| 70 | + track_width = 0.5 |
| 71 | + |
| 72 | + kinematics = SwerveDrive4Kinematics( |
| 73 | + Translation2d(wheel_base / 2, track_width / 2), |
| 74 | + Translation2d(wheel_base / 2, -track_width / 2), |
| 75 | + Translation2d(-wheel_base / 2, track_width / 2), |
| 76 | + Translation2d(-wheel_base / 2, -track_width / 2), |
| 77 | + ) |
| 78 | + |
| 79 | + odometry = SwerveDrive4Odometry( |
| 80 | + kinematics, |
| 81 | + Rotation2d(0), |
| 82 | + swerve_module_positions, |
| 83 | + Pose2d(0, 0, Rotation2d(0)), |
| 84 | + ) |
| 85 | + |
| 86 | + def set_module_states(states): |
| 87 | + nonlocal swerve_module_states |
| 88 | + swerve_module_states = states |
| 89 | + |
| 90 | + def get_robot_pose() -> Pose2d: |
| 91 | + odometry.update(angle, swerve_module_positions) |
| 92 | + return odometry.getPose() |
| 93 | + |
| 94 | + with ManualSimTime() as sim: |
| 95 | + subsystem = commands2.Subsystem() |
| 96 | + waypoints: List[Pose2d] = [] |
| 97 | + waypoints.append(Pose2d(0, 0, Rotation2d(0))) |
| 98 | + waypoints.append(Pose2d(1, 5, Rotation2d(3))) |
| 99 | + config = TrajectoryConfig(8.8, 0.1) |
| 100 | + trajectory = TrajectoryGenerator.generateTrajectory(waypoints, config) |
| 101 | + |
| 102 | + end_state = trajectory.sample(trajectory.totalTime()) |
| 103 | + |
| 104 | + command = commands2.SwerveControllerCommand( |
| 105 | + trajectory=trajectory, |
| 106 | + pose=get_robot_pose, |
| 107 | + kinematics=kinematics, |
| 108 | + controller=HolonomicDriveController( |
| 109 | + PIDController(0.6, 0, 0), |
| 110 | + PIDController(0.6, 0, 0), |
| 111 | + rot_controller, |
| 112 | + ), |
| 113 | + outputModuleStates=set_module_states, |
| 114 | + requirements=(subsystem,), |
| 115 | + ) |
| 116 | + |
| 117 | + timer.restart() |
| 118 | + |
| 119 | + command.initialize() |
| 120 | + while not command.isFinished(): |
| 121 | + command.execute() |
| 122 | + angle = trajectory.sample(timer.get()).pose.rotation() |
| 123 | + |
| 124 | + for i in range(0, len(swerve_module_positions)): |
| 125 | + swerve_module_positions[i].distance += ( |
| 126 | + swerve_module_states[i].speed * 0.005 |
| 127 | + ) |
| 128 | + swerve_module_positions[i].angle = swerve_module_states[i].angle |
| 129 | + |
| 130 | + sim.step(0.005) |
| 131 | + |
| 132 | + timer.stop() |
| 133 | + command.end(True) |
| 134 | + |
| 135 | + assert end_state.pose.X() == pytest.approx(get_robot_pose().X(), x_tolerance) |
| 136 | + |
| 137 | + assert end_state.pose.Y() == pytest.approx(get_robot_pose().Y(), y_tolerance) |
| 138 | + |
| 139 | + assert end_state.pose.rotation().radians() == pytest.approx( |
| 140 | + get_robot_pose().rotation().radians(), |
| 141 | + angular_tolerance, |
| 142 | + ) |
0 commit comments