Skip to content

Commit a09d9a0

Browse files
committed
code used for today's tag alignment demo
1 parent 596b383 commit a09d9a0

File tree

4 files changed

+76
-16
lines changed

4 files changed

+76
-16
lines changed

src/main/java/frc/robot/Constants.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ public static final class DriveConstants {
9191

9292
public static final int kEncoderDepth = 4;
9393
public static final int kEncoderMeasurementPeriod = 16;
94-
public static final int kSteerSmartCurrentLimit = 60;
94+
public static final int kDriveSmartCurrentLimit = 60; // TODO: find a good value
95+
public static final int kDrivePeakCurrentLimit = kDriveSmartCurrentLimit + 15;
96+
public static final int kSteerSmartCurrentLimit = 60; // TODO: find a good value
9597
public static final int kSteerPeakCurrentLimit = kSteerSmartCurrentLimit + 15;
9698
// The amount of time to go from 0 to full power in seconds
9799
public static final double kRampRate = .1;
98100

99101
// DriveCommand.java Constants
100-
public static final double kDriveP = 6; // up to 1.0?
101-
// public static final double kDriveP = 0.4; // up to 1.0?
102+
public static final double kDriveP = 5; // TODO: find a good value
102103
public static final double kDriveI = 0;
103104
public static final double kDriveD = 0;
104105
// public static final double kDriveMaxAcceleration = 2 * kDriveMaxSpeed; //
@@ -125,7 +126,7 @@ public static final class RobotConstants {
125126
* the pose of the robot.
126127
*/
127128
public static Transform3d kRobotToCamera1 = new Transform3d(new Translation3d(0.0, 0.0, 0.2),
128-
new Rotation3d(0, Units.degreesToRadians(-20), 0));
129+
new Rotation3d(0, Units.degreesToRadians(-10), 0));
129130

130131
/**
131132
* The {@code Transform3d} expressing the pose of the second camera relative to

src/main/java/frc/robot/Robot.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,39 +90,45 @@ public void bindDriveControls() {
9090
DriveCommand.moveForward(m_driveSubsystem, 1.8, distanceTolerance, angleTolerance),
9191
DriveCommand.moveForward(m_driveSubsystem, -1.8, distanceTolerance, angleTolerance)));
9292

93-
Transform2d robotToTarget = new Transform2d(1, 0, Rotation2d.fromDegrees(180));
93+
Transform2d robotToTarget = new Transform2d(1.8, 0, Rotation2d.fromDegrees(180));
9494
double angleOfCoverageInDegrees = 90;
9595
double distanceThresholdInMeters = 4;
96-
m_driverController.button(Button.kTriangle)
97-
.whileTrue(
98-
alignTest(1, robotToTarget, distanceTolerance, angleTolerance));
9996
// m_driverController.button(Button.kTriangle)
10097
// .whileTrue(
101-
// AlignCommand.turnToClosestTag(
102-
// m_driveSubsystem, m_poseEstimationSubystem, angleOfCoverageInDegrees,
103-
// distanceThresholdInMeters,
104-
// distanceTolerance, angleTolerance));
98+
// alignTest(1, robotToTarget, distanceTolerance, angleTolerance));
99+
100+
m_driverController.button(Button.kTriangle)
101+
.whileTrue(
102+
AlignCommand.turnToClosestTag(
103+
m_driveSubsystem, m_poseEstimationSubystem, angleOfCoverageInDegrees,
104+
distanceThresholdInMeters,
105+
distanceTolerance, angleTolerance));
105106

106107
m_driverController.button(Button.kLeftBumper)
107108
.whileTrue(
108109
AlignCommand.moveToClosestTag(
109110
m_driveSubsystem, m_poseEstimationSubystem, angleOfCoverageInDegrees,
110111
distanceThresholdInMeters, robotToTarget,
111112
distanceTolerance, angleTolerance));
112-
113113
m_driverController.button(Button.kRightBumper)
114114
.whileTrue(
115115
tourCommand(
116116
m_driveSubsystem, m_poseEstimationSubystem, distanceTolerance,
117117
angleTolerance,
118-
robotToTarget, 12, 15, 14, 16, 17, 18, 19, 20, 21, 22));
118+
robotToTarget, 1, 6, 7, 8, 2, 8, 7, 6, 1));
119+
// m_driverController.button(Button.kRightBumper)
120+
// .whileTrue(
121+
// tourCommand(
122+
// m_driveSubsystem, m_poseEstimationSubystem, distanceTolerance,
123+
// angleTolerance,
124+
// robotToTarget, 12, 15, 14, 16, 17, 18, 19, 20, 21, 22));
119125
}
120126

121-
private Command alignTest(int tagIDs, Transform2d robotToTarget, double distanceTolerance, double angleTolerance) {
127+
Command alignTest(int tagID, Transform2d robotToTarget, double distanceTolerance, double angleTolerance) {
122128
return sequence(
123129
AlignCommand.moveTo(
124130
m_driveSubsystem, m_poseEstimationSubystem,
125-
kFieldLayout.getTagPose(1).get().toPose2d().plus(robotToTarget),
131+
kFieldLayout.getTagPose(tagID).get().toPose2d().plus(robotToTarget),
126132
distanceTolerance, angleTolerance),
127133
AlignCommand.moveTo(
128134
m_driveSubsystem, m_poseEstimationSubystem,

src/main/java/frc/robot/SwerveModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ public SwerveModule(int canId, int drivePort, int steerPort) {
4949
config.openLoopRampRate(kRampRate).closedLoopRampRate(kRampRate);
5050
// Helps with encoder precision (not set in stone)
5151
config.encoder.uvwAverageDepth(kEncoderDepth).uvwMeasurementPeriod(kEncoderMeasurementPeriod);
52+
config.smartCurrentLimit(kDriveSmartCurrentLimit).secondaryCurrentLimit(kDrivePeakCurrentLimit);
53+
m_driveMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
54+
55+
config = new SparkMaxConfig();
56+
config.idleMode(IdleMode.kBrake).voltageCompensation(12);
57+
config.openLoopRampRate(kRampRate).closedLoopRampRate(kRampRate);
58+
// Helps with encoder precision (not set in stone)
59+
config.encoder.uvwAverageDepth(kEncoderDepth).uvwMeasurementPeriod(kEncoderMeasurementPeriod);
5260
config.smartCurrentLimit(kSteerSmartCurrentLimit).secondaryCurrentLimit(kSteerPeakCurrentLimit);
5361
m_steerMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
62+
5463
m_steerController.enableContinuousInput(0, 360);
5564
if (RobotBase.isSimulation()) {
5665
m_driveMotorModel = new DCMotorSim(

src/main/java/frc/robot/subsystems/PoseEstimationSubsystem.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,50 @@ public Pose2d getEstimatedPose() {
148148
return m_poseEstimator.getEstimatedPosition();
149149
}
150150

151+
/**
152+
* Finds the {@code Pose2d} of the {@code AprilTag} that is closest (in terms of
153+
* angular displacement) to the specified {@code Pose2d}.
154+
*
155+
* @param pose a {@code Pose2d}
156+
* @param distanceThresholdInMeters the maximum distance (in meters) within
157+
* which {@code AprilTag}s are considered
158+
* @return the {@code Pose2d} of the {@code AprilTag} that is closest (in terms
159+
* of angular displacement) to the specified {@code Pose2d}
160+
*/
161+
public Pose2d closestTagPose(Pose2d pose, double distanceThresholdInMeters) {
162+
var i = closestTagID(pose, distanceThresholdInMeters);
163+
return i == null ? null : kFieldLayout.getTagPose(i).get().toPose2d();
164+
}
165+
166+
/**
167+
* Determines the ID of the {@code AprilTag} that is closest (in terms of
168+
* angular displacement) to the specified {@code Pose2d} ({@code null} if no
169+
* such {@code AprilTag}).
170+
*
171+
* @param pose a {@code Pose2d}
172+
* @param distanceThresholdInMeters the maximum distance (in meters) within
173+
* which {@code AprilTag}s are considered
174+
* @return the ID of the {@code AprilTag} that is closest (in terms of angular
175+
* displacement) to the specified {@code Pose2d} ({@code null} if no
176+
* such {@code AprilTag})
177+
*/
178+
public static Integer closestTagID(Pose2d pose, double distanceThresholdInMeters) {
179+
var s = kFieldLayout.getTags().stream()
180+
// consider only the tags facing toward the robot
181+
.filter(
182+
t -> Math.abs(
183+
t.pose.getTranslation().toTranslation2d().minus(pose.getTranslation()).getAngle()
184+
.minus(t.pose.toPose2d().getRotation()).getDegrees()) > 90)
185+
.filter(t -> Math.abs(translationalDisplacement(pose, t.pose.toPose2d())) < distanceThresholdInMeters)
186+
// only tags sufficently close
187+
.map(t -> Map.entry(t.ID, Math.abs(angularDisplacement(pose, t.pose.toPose2d()).getDegrees())));
188+
Optional<Entry<Integer, Double>> closest = s.reduce((e1, e2) -> e1.getValue() < e2.getValue() ? e1 : e2);
189+
if (closest.isPresent()) {
190+
return closest.get().getKey();
191+
} else
192+
return null;
193+
}
194+
151195
/**
152196
* Finds the {@code Pose2d} of the {@code AprilTag} that is closest to the robot
153197
* ({@code null} if no such {@code AprilTag}).

0 commit comments

Comments
 (0)