2727import java .text .NumberFormat ;
2828import java .util .LinkedList ;
2929import java .util .List ;
30+ import java .util .function .BooleanSupplier ;
3031import java .util .function .DoubleSupplier ;
3132import java .util .function .Supplier ;
3233
33- import org .photonvision .PhotonUtils ;
34-
3534public class DriveCommands {
3635 private static final double DEADBAND = 0.1 ;
3736 private static final double ANGLE_KP = 5.0 ;
@@ -43,7 +42,6 @@ public class DriveCommands {
4342 private static final double WHEEL_RADIUS_MAX_VELOCITY = 0.25 ; // Rad/Sec
4443 private static final double WHEEL_RADIUS_RAMP_RATE = 0.05 ; // Rad/Sec^2
4544
46-
4745 private DriveCommands () {}
4846
4947 private static Translation2d getLinearVelocityFromJoysticks (double x , double y ) {
@@ -67,7 +65,8 @@ public static Command joystickDrive(
6765 Drive drive ,
6866 DoubleSupplier xSupplier ,
6967 DoubleSupplier ySupplier ,
70- DoubleSupplier omegaSupplier ) {
68+ DoubleSupplier omegaSupplier ,
69+ BooleanSupplier robotRelative ) {
7170 return Commands .run (
7271 () -> {
7372 // Get linear velocity
@@ -86,15 +85,19 @@ public static Command joystickDrive(
8685 linearVelocity .getX () * drive .getMaxLinearSpeedMetersPerSec (),
8786 linearVelocity .getY () * drive .getMaxLinearSpeedMetersPerSec (),
8887 omega * drive .getMaxAngularSpeedRadPerSec ());
88+
8989 boolean isFlipped =
9090 DriverStation .getAlliance ().isPresent ()
9191 && DriverStation .getAlliance ().get () == Alliance .Red ;
92+
9293 drive .runVelocity (
93- ChassisSpeeds .fromFieldRelativeSpeeds (
94- speeds ,
95- isFlipped
96- ? drive .getRotation ().plus (new Rotation2d (Math .PI ))
97- : drive .getRotation ()));
94+ robotRelative .getAsBoolean ()
95+ ? ChassisSpeeds .fromRobotRelativeSpeeds (speeds , Rotation2d .fromDegrees (0 ))
96+ : ChassisSpeeds .fromFieldRelativeSpeeds (
97+ speeds ,
98+ isFlipped
99+ ? drive .getRotation ().plus (new Rotation2d (Math .PI ))
100+ : drive .getRotation ()));
98101 },
99102 drive );
100103 }
@@ -108,7 +111,8 @@ public static Command joystickDriveAtAngle(
108111 Drive drive ,
109112 DoubleSupplier xSupplier ,
110113 DoubleSupplier ySupplier ,
111- Supplier <Rotation2d > rotationSupplier ) {
114+ Supplier <Rotation2d > rotationSupplier ,
115+ BooleanSupplier robotRelative ) {
112116
113117 // Create PID controller
114118 ProfiledPIDController angleController =
@@ -137,15 +141,19 @@ public static Command joystickDriveAtAngle(
137141 linearVelocity .getX () * drive .getMaxLinearSpeedMetersPerSec (),
138142 linearVelocity .getY () * drive .getMaxLinearSpeedMetersPerSec (),
139143 omega );
144+
140145 boolean isFlipped =
141146 DriverStation .getAlliance ().isPresent ()
142147 && DriverStation .getAlliance ().get () == Alliance .Red ;
148+
143149 drive .runVelocity (
144- ChassisSpeeds .fromFieldRelativeSpeeds (
145- speeds ,
146- isFlipped
147- ? drive .getRotation ().plus (new Rotation2d (Math .PI ))
148- : drive .getRotation ()));
150+ robotRelative .getAsBoolean ()
151+ ? ChassisSpeeds .fromRobotRelativeSpeeds (speeds , Rotation2d .fromDegrees (0 ))
152+ : ChassisSpeeds .fromFieldRelativeSpeeds (
153+ speeds ,
154+ isFlipped
155+ ? drive .getRotation ().plus (new Rotation2d (Math .PI ))
156+ : drive .getRotation ()));
149157 },
150158 drive )
151159
@@ -154,48 +162,54 @@ public static Command joystickDriveAtAngle(
154162 }
155163
156164 /**
157- * Field relative drive command where joystick controlls linear velocity and robot is continuously towards a point on the field.
158- * Useful for orbitting shooting targets
159- * @param orbitPose field-relative point to orbit
165+ * Field relative drive command where joystick controlls linear velocity and robot is continuously
166+ * towards a point on the field. Useful for orbitting shooting targets
167+ *
168+ * @param target Pose2d to look at
160169 */
161170 public static Command joystickOrbitDrive (
162171 Drive drive ,
163172 DoubleSupplier xSupplier ,
164173 DoubleSupplier ySupplier ,
165- Pose2d orbitPose ) {
166-
167- //create angle PID controller
168- ProfiledPIDController angleController =
169- new ProfiledPIDController (
170- 1 ,
171- 0 ,
172- 0.5 ,
173- new TrapezoidProfile .Constraints (ANGLE_MAX_VELOCITY , ANGLE_MAX_ACCELERATION ));
174-
175- angleController .enableContinuousInput (-Math .PI , Math .PI );
176- angleController .setTolerance (Units .degreesToRadians (5 ));
177-
178-
179- return Commands .run (
180- () -> {
181- Rotation2d rotationTarget = PhotonUtils .getYawToPose (drive .getPose (), orbitPose );//gets angle from robot position to tag position
182-
183- double radiansOff = drive .getPose ().getRotation ().getRadians () - rotationTarget .getRadians ();
184-
185- double orbitControllerOutput = angleController .calculate (radiansOff , 0 );
186- double orbitPIDXOut = xSupplier .getAsDouble ();
187- double orbitPIDYOut = ySupplier .getAsDouble ();
188-
189- drive .runVelocity (
190- ChassisSpeeds .fromFieldRelativeSpeeds (orbitPIDXOut , orbitPIDYOut , orbitControllerOutput , drive .getPose ().getRotation ())
191- );
192-
193-
194- }, drive )
174+ Pose2d target ,
175+ BooleanSupplier robotRelative ) {
176+
177+ return joystickDriveAtAngle (
178+ drive ,
179+ xSupplier ,
180+ ySupplier ,
181+ () ->
182+ Rotation2d .fromRadians (
183+ Math .atan2 (
184+ target .getY () - drive .getPose ().getY (),
185+ target .getX () - drive .getPose ().getX ())),
186+ robotRelative );
187+ }
195188
196- // Reset PID controller when command starts
197- .beforeStarting (() -> angleController .reset (drive .getRotation ().getRadians ()));
198- }
189+ // create angle PID controller
190+ /**
191+ * ProfiledPIDController angleController = new ProfiledPIDController( 1, 0, 0.5, new
192+ * TrapezoidProfile.Constraints(ANGLE_MAX_VELOCITY, ANGLE_MAX_ACCELERATION));
193+ *
194+ * <p>angleController.enableContinuousInput(-Math.PI, Math.PI);
195+ * angleController.setTolerance(Units.degreesToRadians(5));
196+ *
197+ * <p>return Commands.run( () -> { Rotation2d rotationTarget = Rotation2d.fromRadians( Math.atan2(
198+ * // find relative angle of robot to orbit pose target.getY() - drive.getPose().getY(),
199+ * target.getX() - drive.getPose().getX()));
200+ *
201+ * <p>double radiansOff = drive.getPose().getRotation().getRadians() -
202+ * rotationTarget.getRadians();
203+ *
204+ * <p>double orbitControllerOutput = angleController.calculate(radiansOff, 0); double orbitPIDXOut
205+ * = xSupplier.getAsDouble(); double orbitPIDYOut = ySupplier.getAsDouble();
206+ *
207+ * <p>drive.runVelocity( ChassisSpeeds.fromRobotRelativeSpeeds( orbitPIDXOut, orbitPIDYOut,
208+ * orbitControllerOutput, drive.getPose().getRotation())); }, drive)
209+ *
210+ * <p>// Reset PID controller when command starts .beforeStarting(() ->
211+ * angleController.reset(drive.getRotation().getRadians())); }
212+ */
199213 /** pid look at thingy */
200214 public static Command lookAt (
201215 Drive drive ,
0 commit comments