1313import edu .wpi .first .math .controller .PIDController ;
1414import edu .wpi .first .math .geometry .Rotation2d ;
1515import edu .wpi .first .util .sendable .SendableRegistry ;
16+ import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
1617import edu .wpi .first .wpilibj2 .command .Command ;
1718
1819public class AlignToApriltag extends Command {
@@ -21,34 +22,82 @@ public class AlignToApriltag extends Command {
2122 public final Drivetrain drivetrain ;
2223 private Limelight limelight ;
2324
24- public final PIDController rotationPID = new PIDController (thetaPIDController [0 ], thetaPIDController [1 ],
25- thetaPIDController [2 ]);
25+ public final PIDController rotationPID = new PIDController (
26+ SmartDashboard .getNumber ("apriltag align kp" ,
27+ thetaPIDController [0 ]),
28+ SmartDashboard .getNumber ("apriltag align ki" ,
29+ thetaPIDController [1 ]),
30+ SmartDashboard .getNumber ("apriltag align kd" ,
31+ thetaPIDController [2 ]));
2632
2733 public AlignToApriltag (Drivetrain drivetrain , Limelight limelight ) {
28- this .limelight = limelight ;
34+ this .limelight = limelight ;
2935 this .drivetrain = drivetrain ;
3036 this .teleopDrive = (TeleopDrive ) drivetrain .getDefaultCommand ();
3137
3238 rotationPID .enableContinuousInput (-180 , 180 );
3339 Rotation2d targetAngle = Rotation2d .fromDegrees (drivetrain .getHeading ())
34- .minus (Rotation2d .fromRadians (limelight .getRotateAngleRad ()));
35- rotationPID .setSetpoint (MathUtil .inputModulus (targetAngle .getDegrees (), -180 , 180 ));
36- rotationPID .setTolerance (positionTolerance [2 ], velocityTolerance [2 ]);
40+ .plus (Rotation2d .fromRadians (
41+ limelight .getRotateAngleRadMT2 ()));
42+ rotationPID .setSetpoint (MathUtil .inputModulus (targetAngle .getDegrees (),
43+ -180 , 180 ));
44+ rotationPID .setTolerance (
45+ SmartDashboard .getNumber ("apriltag align pos tolerance" ,
46+ positionTolerance [2 ]),
47+ SmartDashboard .getNumber ("apriltag align vel tolerance" ,
48+ velocityTolerance [2 ]));
3749 SendableRegistry .addChild (this , rotationPID );
3850 addRequirements (drivetrain );
3951 }
4052
4153 @ Override
4254 public void execute () {
55+ double kp = SmartDashboard .getNumber ("apriltag align kp" ,
56+ rotationPID .getP ());
57+ double ki = SmartDashboard .getNumber ("apriltag align ki" ,
58+ rotationPID .getI ());
59+ double kd = SmartDashboard .getNumber ("apriltag align kd" ,
60+ rotationPID .getD ());
61+
62+ if (kp != rotationPID .getP ())
63+ rotationPID .setP (kp );
64+ if (ki != rotationPID .getI ())
65+ rotationPID .setI (ki );
66+ if (kd != rotationPID .getD ())
67+ rotationPID .setD (kd );
68+
69+ double posTolerance =
70+ SmartDashboard .getNumber ("apriltag align pos tolerance" ,
71+ rotationPID .getPositionTolerance ());
72+ double velTolerance =
73+ SmartDashboard .getNumber ("apriltag align vel tolerance" ,
74+ rotationPID .getVelocityTolerance ());
75+
76+ if (posTolerance != rotationPID .getPositionTolerance ()
77+ || velTolerance != rotationPID .getVelocityTolerance ())
78+ rotationPID .setTolerance (posTolerance , velTolerance );
79+
80+ SmartDashboard .putNumber ("apriltag align pos error (rad)" ,
81+ rotationPID .getPositionError ());
82+ SmartDashboard .putNumber ("apriltag align vel error (rad/s)" ,
83+ rotationPID .getVelocityError ());
84+
4385 Rotation2d targetAngle = Rotation2d .fromDegrees (drivetrain .getHeading ())
44- .minus (Rotation2d .fromRadians (limelight .getRotateAngleRad ()));
45- rotationPID .setSetpoint (MathUtil .inputModulus (targetAngle .getDegrees (), -180 , 180 ));
86+ .plus (Rotation2d .fromRadians (
87+ limelight .getRotateAngleRadMT2 ()));
88+ rotationPID .setSetpoint (MathUtil .inputModulus (targetAngle .getDegrees (),
89+ -180 , 180 ));
90+ double rotationDrive = rotationPID .calculate (drivetrain .getHeading ());
91+ if (!limelight .seesTag ()) {
92+ rotationDrive = 0 ;
93+ }
94+
4695 if (teleopDrive == null )
47- drivetrain .drive (0 , 0 , rotationPID . calculate ( drivetrain . getHeading ()) );
96+ drivetrain .drive (0 , 0 , rotationDrive );
4897 else {
4998 double [] driverRequestedSpeeds = teleopDrive .getRequestedSpeeds ();
5099 drivetrain .drive (driverRequestedSpeeds [0 ], driverRequestedSpeeds [1 ],
51- rotationPID . calculate ( drivetrain . getHeading ()) );
100+ rotationDrive );
52101 }
53102 }
54103
0 commit comments