11package org .ghrobotics .frc2024 ;
22
3+ import edu .wpi .first .math .MathUtil ;
4+ import edu .wpi .first .math .controller .PIDController ;
5+ import edu .wpi .first .math .kinematics .ChassisSpeeds ;
36import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
47import edu .wpi .first .wpilibj2 .command .Command ;
8+ import edu .wpi .first .wpilibj2 .command .CommandScheduler ;
59import edu .wpi .first .wpilibj2 .command .Commands ;
610import edu .wpi .first .wpilibj2 .command .FunctionalCommand ;
711import edu .wpi .first .wpilibj2 .command .InstantCommand ;
1418import org .ghrobotics .frc2024 .commands .ArmPID ;
1519import org .ghrobotics .frc2024 .subsystems .Arm ;
1620import org .ghrobotics .frc2024 .subsystems .Climber ;
21+ import org .ghrobotics .frc2024 .subsystems .Drive ;
1722import org .ghrobotics .frc2024 .subsystems .Feeder ;
1823import org .ghrobotics .frc2024 .subsystems .Intake ;
1924import org .ghrobotics .frc2024 .subsystems .Shooter ;
@@ -26,6 +31,7 @@ public class Superstructure {
2631 private final Feeder feeder_ ;
2732 private final Climber climber_ ;
2833 private final RobotState robot_state_ ;
34+ private final Drive drive_ ;
2935 // private final ShootingPosition shootingPosition_ = new ShootingPosition();
3036
3137
@@ -36,14 +42,19 @@ public class Superstructure {
3642 public double armShootingAngle ;
3743 public double shootingDistance ;
3844
45+ public double y_velocity ;
46+
47+ private final PIDController pid_ = new PIDController (0.2 , 0 , 0 );
48+
3949 // Constructor
40- public Superstructure (Arm arm , Intake intake , Shooter shooter , Feeder feeder , RobotState robot_state , Climber climber ) {
50+ public Superstructure (Arm arm , Intake intake , Shooter shooter , Feeder feeder , RobotState robot_state , Climber climber , Drive drive ) {
4151 arm_ = arm ;
4252 intake_ = intake ;
4353 shooter_ = shooter ;
4454 feeder_ = feeder ;
4555 climber_ = climber ;
4656 robot_state_ = robot_state ;
57+ drive_ = drive ;
4758 }
4859
4960 public void periodic () {
@@ -57,10 +68,22 @@ public void periodic() {
5768
5869 SmartDashboard .putString ("Superstructure State" , state );
5970
71+ y_velocity = MathUtil .clamp (pid_ .calculate (LimelightHelpers .getTX ("limelight" ), -9.8 ), -0.5 , 0.5 );
72+ SmartDashboard .putNumber ("Vision Error" , pid_ .getPositionError ());
73+
6074 // Checks output current to see if note has intaked or not (current > 35 means intaked)
6175 if (intake_ .getRightOutputCurrent () > 45 ) {
6276 LimelightHelpers .setLEDMode_ForceOn ("limelight" );
6377 }
78+
79+ if (IO .align == true ) {
80+ drive_ .setSpeeds (new ChassisSpeeds (0 , y_velocity , 0 ), Drive .OutputType .OPEN_LOOP );
81+ }
82+ else {
83+ drive_ .setSpeeds (drive_ .getSpeeds ());
84+ }
85+
86+
6487 }
6588
6689 // Position Setter
@@ -135,6 +158,33 @@ public Command autoArm(double angle_deg) {
135158 );
136159 }
137160
161+ public Command align () {
162+ return new StartEndCommand (
163+ () -> IO .align = true ,
164+ () -> IO .align = false ,
165+ drive_
166+ );
167+ }
168+
169+ public Command autoAlign () {
170+ // pid_.setSetpoint(-9.8);
171+
172+
173+
174+ return new FunctionalCommand (
175+ () -> SmartDashboard .putNumber ("Started" , 0.1 ),
176+ () -> drive_ .setSpeeds (new ChassisSpeeds (0 , y_velocity , 0 ), Drive .OutputType .OPEN_LOOP ),
177+ (interrupted ) -> drive_ .setSpeeds (new ChassisSpeeds (0 , 0 , 0 ), Drive .OutputType .OPEN_LOOP ),
178+ () -> (pid_ .getPositionError () < 0.5 ),
179+ drive_
180+ );
181+ }
182+
183+ public void alignNote () {
184+
185+ drive_ .setSpeeds (new ChassisSpeeds (0 , y_velocity , 0 ), Drive .OutputType .OPEN_LOOP );
186+ }
187+
138188 /**
139189 * Feeder Setter
140190 * @param percent
@@ -188,6 +238,10 @@ public String getState() {
188238 return state ;
189239 }
190240
241+ public static class IO {
242+ public static boolean align ;
243+ }
244+
191245 public enum Position {
192246 STOW (55 , "STOW" ),
193247 SUBWOOFER (16.2 , "SUBWOOFER" ),
0 commit comments