Skip to content

Commit d3aa85e

Browse files
committed
align to speaker command
1 parent 0875847 commit d3aa85e

File tree

3 files changed

+83
-58
lines changed

3 files changed

+83
-58
lines changed

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import edu.wpi.first.math.numbers.N3;
2828

2929
public final class Constants {
30+
3031
public static final double stickDeadband = 0.1;
3132

3233
public static final class IntakeArm{ //TODO: This must be tuned to specific robot
@@ -202,27 +203,27 @@ public static final class AutoConstants { //TODO: The below constants are used i
202203
}
203204

204205
public static class Vision {
205-
public static final String kRightCameraName = "Arducam_OV9281_USB_Camera";
206-
public static final String kLeftCameraName = "YOUR CAMERA NAME";
207-
// Cam mounted facing forward, half a meter forward of center, half a meter up
208-
// from center.
209-
public static final Transform3d kRightRobotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5),
210-
new Rotation3d(0, 0, 0));
211-
public static final Transform3d kLeftRobotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5),
212-
new Rotation3d(0, 0, 0));
213-
214-
// The layout of the AprilTags on the field
215-
public static final AprilTagFieldLayout kTagLayout = AprilTagFields.kDefaultField.loadAprilTagLayoutField();
216-
217-
// The standard deviations of our vision estimated poses, which affect
218-
// correction rate
219-
// (Fake values. Experiment and determine estimation noise on an actual robot.)
220-
public static final Matrix<N3, N1> kRightSingleTagStdDevs = VecBuilder.fill(4, 4, 8);
221-
public static final Matrix<N3, N1> kRightMultiTagStdDevs = VecBuilder.fill(0.5, 0.5, 1);
222-
223-
public static final Matrix<N3, N1> kLeftSingleTagStdDevs = VecBuilder.fill(4, 4, 8);
224-
public static final Matrix<N3, N1> kLeftMultiTagStdDevs = VecBuilder.fill(0.5, 0.5, 1);
225-
226-
public static final Pose2d target = new Pose2d(1, 1, new Rotation2d(Units.degreesToRadians(0)));
227-
}
206+
public static final String kRightCameraName = "Arducam_OV9281_USB_Camera";
207+
public static final String kLeftCameraName = "YOUR CAMERA NAME";
208+
// Cam mounted facing forward, half a meter forward of center, half a meter up
209+
// from center.
210+
public static final Transform3d kRightRobotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5),
211+
new Rotation3d(0, 0, 0));
212+
public static final Transform3d kLeftRobotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5),
213+
new Rotation3d(0, 0, 0));
214+
215+
// The layout of the AprilTags on the field
216+
public static final AprilTagFieldLayout kTagLayout = AprilTagFields.kDefaultField.loadAprilTagLayoutField();
217+
218+
// The standard deviations of our vision estimated poses, which affect
219+
// correction rate
220+
// (Fake values. Experiment and determine estimation noise on an actual robot.)
221+
public static final Matrix<N3, N1> kRightSingleTagStdDevs = VecBuilder.fill(4, 4, 8);
222+
public static final Matrix<N3, N1> kRightMultiTagStdDevs = VecBuilder.fill(0.5, 0.5, 1);
223+
224+
public static final Matrix<N3, N1> kLeftSingleTagStdDevs = VecBuilder.fill(4, 4, 8);
225+
public static final Matrix<N3, N1> kLeftMultiTagStdDevs = VecBuilder.fill(0.5, 0.5, 1);
226+
227+
public static final Pose2d target = new Pose2d(1, 1, new Rotation2d(Units.degreesToRadians(0)));
228+
}
228229
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
package frc.robot.commands.SwerveCommands;
6+
7+
import static frc.robot.Constants.IntakeArm.minimumError;
8+
import static frc.robot.Constants.Swerve.minimumErrorTurn;
9+
10+
import edu.wpi.first.math.controller.PIDController;
11+
import edu.wpi.first.math.geometry.Rotation2d;
12+
import edu.wpi.first.math.geometry.Translation2d;
13+
import edu.wpi.first.math.kinematics.SwerveModuleState;
14+
import edu.wpi.first.wpilibj2.command.Command;
15+
import frc.Utils.vision.Vision;
16+
import frc.robot.commands.SwerveCommands.*;
17+
import frc.robot.subsystems.SwerveSubsystem;
18+
19+
public class AlignToSpeaker extends Command {
20+
/** Creates a new TurnToDegree. */
21+
SwerveSubsystem swerve = SwerveSubsystem.getInstance();
22+
private PIDController pid = new PIDController(0, 0, 0);
23+
private Vision vision = Vision.getInstance();
24+
double angleFromTarget = vision.GetAngleFromTarget().getDegrees();
25+
26+
public AlignToSpeaker() {
27+
28+
}
29+
30+
// Called when the command is initially scheduled.
31+
@Override
32+
public void initialize() {}
33+
34+
// Called every time the scheduler runs while the command is scheduled.
35+
@Override
36+
public void execute() {
37+
angleFromTarget = vision.GetAngleFromTarget().getDegrees();
38+
optimize();
39+
double rotation = pid.calculate(angleFromTarget, 180);
40+
swerve.drive(new Translation2d(), rotation, true, false, false);
41+
}
42+
43+
// Called once the command ends or is interrupted.
44+
@Override
45+
public void end(boolean interrupted) {}
46+
47+
// Returns true when the command should end.
48+
@Override
49+
public boolean isFinished() {
50+
return Math.abs(vision.GetAngleFromTarget().getDegrees() - 180) < minimumErrorTurn ;
51+
}
52+
53+
public void optimize() {
54+
double delta = 180 - angleFromTarget;
55+
if (Math.abs(delta) > 90){
56+
angleFromTarget = delta > 90 ? (angleFromTarget -= 180) : (angleFromTarget += 180);
57+
}
58+
}
59+
}

src/main/java/frc/robot/commands/SwerveCommands/TurnToDegree.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)