generated from StuyPulse/Phil
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSwerveDriveDriveToNote.java
82 lines (63 loc) · 3.08 KB
/
SwerveDriveDriveToNote.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/************************ PROJECT JIM *************************/
/* Copyright (c) 2024 StuyPulse Robotics. All rights reserved.*/
/* This work is licensed under the terms of the MIT license. */
/**************************************************************/
package com.stuypulse.robot.commands.swerve;
import static com.stuypulse.robot.constants.Settings.NoteDetection.*;
import com.stuypulse.robot.constants.Settings.Swerve;
import com.stuypulse.robot.subsystems.vision.NoteVision;
import com.stuypulse.robot.subsystems.odometry.Odometry;
import com.stuypulse.robot.subsystems.swerve.SwerveDrive;
import com.stuypulse.robot.util.HolonomicController;
import com.stuypulse.stuylib.control.angle.feedback.AnglePIDController;
import com.stuypulse.stuylib.control.feedback.PIDController;
import com.stuypulse.stuylib.streams.booleans.BStream;
import com.stuypulse.stuylib.streams.booleans.filters.BDebounceRC;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
public class SwerveDriveDriveToNote extends Command {
private final SwerveDrive swerve;
private final Odometry odometry;
private final NoteVision vision;
private final HolonomicController controller;
private final BStream aligned;
public SwerveDriveDriveToNote(){
this.swerve = SwerveDrive.getInstance();
this.odometry = Odometry.getInstance();
this.vision = NoteVision.getInstance();
controller = new HolonomicController(
new PIDController(Translation.P,Translation.I,Translation.D),
new PIDController(Translation.P, Translation.I, Translation.D),
new AnglePIDController(Rotation.P, Rotation.I, Rotation.D)
);
SmartDashboard.putData("Note Detection/Controller", controller);
aligned = BStream.create(this::isAligned).filtered(new BDebounceRC.Rising(DEBOUNCE_TIME));
addRequirements(swerve);
}
private boolean isAligned() {
return controller.isDone(THRESHOLD_X.get(), THRESHOLD_Y.get(), THRESHOLD_ANGLE.get());
}
@Override
public void execute() {
Translation2d targetTranslation = odometry.getTranslation().plus(
new Translation2d(Swerve.CENTER_TO_INTAKE_FRONT, 0).rotateBy(odometry.getRotation()));
Rotation2d targetRotation = vision.getEstimatedNotePose().minus(targetTranslation).getAngle();
Pose2d targetPose = new Pose2d(targetTranslation, targetRotation);
if (vision.hasNoteData()) {
// drive to note
swerve.setChassisSpeeds(controller.update(targetPose, odometry.getPose()));
}
else {
// only rotate toward saved note pose
swerve.setChassisSpeeds(controller.update(targetPose, new Pose2d(targetTranslation, odometry.getRotation())));
}
SmartDashboard.putBoolean("Note Detection/Is Aligned", aligned.get());
}
@Override
public boolean isFinished() {
return aligned.get();
}
}