55package frc .robot .commands ;
66
77import com .pathplanner .lib .auto .AutoBuilder ;
8+ import edu .wpi .first .math .geometry .Pose2d ;
9+ import edu .wpi .first .math .geometry .Rotation2d ;
10+ import edu .wpi .first .math .geometry .Translation2d ;
811import edu .wpi .first .wpilibj2 .command .Command ;
912import frc .robot .Constants ;
1013import frc .robot .Constants .LED_STATE ;
14+ import frc .robot .Constants .NOTE_POSITIONS ;
1115import frc .robot .Constants .NoteState ;
1216import frc .robot .subsystems .drive .Drive ;
1317import frc .robot .subsystems .intake .Intake ;
1418import frc .robot .subsystems .led .LED ;
1519import frc .robot .subsystems .pivot .Pivot ;
1620import frc .robot .subsystems .shooter .Shooter ;
21+ import frc .robot .util .AllianceFlipUtil ;
22+ import frc .robot .util .FieldConstants ;
23+ import java .util .HashMap ;
1724import org .littletonrobotics .junction .Logger ;
1825
1926public class AlignToNoteAuto extends Command {
@@ -24,9 +31,15 @@ public class AlignToNoteAuto extends Command {
2431 Pivot pivot ;
2532 Intake intake ;
2633 Shooter shooter ;
27- Command pathCommand ;
34+ Command generatedPathCommand ;
35+ Command targetNotePathCommand ;
36+ Translation2d targetNoteLocation ;
37+ Rotation2d targetNoteRotation ;
38+
39+ private boolean useGeneratedPathCommand ;
2840
2941 private boolean finished ;
42+ private HashMap <NOTE_POSITIONS , Translation2d > noteLocations = new HashMap <>();
3043
3144 public AlignToNoteAuto (LED led , Drive drive , Shooter shooter , Intake intake , Pivot pivot ) {
3245 this .shooter = shooter ;
@@ -35,6 +48,21 @@ public AlignToNoteAuto(LED led, Drive drive, Shooter shooter, Intake intake, Piv
3548 this .led = led ;
3649 this .drive = drive ;
3750 finished = false ;
51+
52+ noteLocations .put (NOTE_POSITIONS .C5 , FieldConstants .StagingLocations .centerlineTranslations [0 ]);
53+ noteLocations .put (NOTE_POSITIONS .C4 , FieldConstants .StagingLocations .centerlineTranslations [1 ]);
54+ noteLocations .put (NOTE_POSITIONS .C3 , FieldConstants .StagingLocations .centerlineTranslations [2 ]);
55+ noteLocations .put (NOTE_POSITIONS .C2 , FieldConstants .StagingLocations .centerlineTranslations [3 ]);
56+ noteLocations .put (NOTE_POSITIONS .C1 , FieldConstants .StagingLocations .centerlineTranslations [4 ]);
57+ noteLocations .put (
58+ NOTE_POSITIONS .B1 ,
59+ AllianceFlipUtil .apply (FieldConstants .StagingLocations .spikeTranslations [2 ]));
60+ noteLocations .put (
61+ NOTE_POSITIONS .B2 ,
62+ AllianceFlipUtil .apply (FieldConstants .StagingLocations .spikeTranslations [1 ]));
63+ noteLocations .put (
64+ NOTE_POSITIONS .B3 ,
65+ AllianceFlipUtil .apply (FieldConstants .StagingLocations .spikeTranslations [0 ]));
3866 // Use addRequirements() here to declare subsystem dependencies.
3967 addRequirements (drive , shooter , led );
4068 }
@@ -43,22 +71,43 @@ public AlignToNoteAuto(LED led, Drive drive, Shooter shooter, Intake intake, Piv
4371 @ Override
4472 public void initialize () {
4573 Logger .recordOutput ("auto pickup init" , "true" );
74+
4675 led .setState (LED_STATE .FLASHING_RED );
4776 intake .runRollers (12 );
4877 shooter .setFeedersRPM (500 );
4978 pivot .setPivotGoal (Constants .PivotConstants .INTAKE_SETPOINT_DEG );
50- // this.pathCommand = drive.alignToNote();
51- // pathCommand.schedule();
52- pathCommand = AutoBuilder .followPath (drive .generatePathToNote ());
53- pathCommand .initialize ();
79+ targetNoteLocation = noteLocations .get (drive .getTargetNote ());
80+ useGeneratedPathCommand =
81+ drive .getCachedNoteLocation ().getDistance (targetNoteLocation ) < 1
82+ || drive .getCachedNoteLocation () != null ;
83+ // useGeneratedPathCommand = false;
84+ generatedPathCommand = AutoBuilder .followPath (drive .generatePathToNote ());
85+ if (useGeneratedPathCommand ) {
86+
87+ generatedPathCommand .initialize ();
88+ } else {
89+ targetNoteRotation =
90+ new Rotation2d (
91+ targetNoteLocation .getX () - drive .getPose ().getX (),
92+ targetNoteLocation .getY () - drive .getPose ().getY ());
93+ targetNotePathCommand =
94+ drive .generateTrajectory (
95+ new Pose2d (targetNoteLocation , targetNoteRotation ), 3 , 2.45 , 100 , 180 , 0.5 );
96+ targetNotePathCommand .initialize ();
97+ }
5498 }
5599
56100 // Called every time the scheduler runs while the command is scheduled.
57101 @ Override
58102 public void execute () {
59-
103+ Logger . recordOutput ( "useGeneratedPathCommand" , useGeneratedPathCommand );
60104 finished = shooter .seesNote () == NoteState .SENSOR ;
61- pathCommand .execute ();
105+ if (useGeneratedPathCommand ) {
106+ generatedPathCommand .execute ();
107+ } else {
108+ targetNotePathCommand .execute ();
109+ }
110+
62111 Logger .recordOutput ("path is finished" , finished );
63112 }
64113
@@ -75,8 +124,9 @@ public void end(boolean interrupted) {
75124 @ Override
76125 public boolean isFinished () {
77126 Logger .recordOutput ("isFinished align note" , shooter .seesNote ());
127+ // return false;
78128 return shooter .seesNote () == NoteState .SENSOR
79- || shooter .seesNote () == NoteState .CURRENT
80- || finished ;
129+ || shooter .seesNote () == NoteState .CURRENT
130+ || finished ;
81131 }
82132}
0 commit comments