11/*----------------------------------------------------------------------------*/
2- /* Copyright (c) 2018 FIRST. All Rights Reserved. */
2+ /* Copyright (c) 2017- 2018 FIRST. All Rights Reserved. */
33/* Open Source Software - may be modified and shared by FRC teams. The code */
44/* must be accompanied by the FIRST BSD license file in the root directory of */
55/* the project. */
66/*----------------------------------------------------------------------------*/
77
88package frc .robot .commands ;
99
10+ import frc .robot .lib .Limelight ;
11+ import frc .robot .subsystems .Drivetrain ;
1012import edu .wpi .first .wpilibj .Joystick ;
1113import edu .wpi .first .wpilibj .command .Command ;
1214import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
13- import frc .robot .subsystems .Drivetrain ;
14-
15- public class TeleopDrive extends Command {
16- Drivetrain dt ;
17- Joystick leftJoy , rightJoy ;
18-
19- double prevSpeed = 0 , prevLeft = 0 , prevRight = 0 ;
2015
21- double outreachSpeed = 0.3 ;
22-
23- /**
24- * Handles all the teleoperated driving functionality
25- *
26- * @param dt the Drivetrain object to use, passing it in is useful for testing
27- * purposes
28- */
29- public TeleopDrive (Drivetrain dt , Joystick leftJoy , Joystick rightJoy ) {
16+ public class Drive extends Command {
17+ private Drivetrain dt ;
18+ private Joystick leftJoy , rightJoy ;
19+ private Limelight lime ;
20+ private Limelight .Mode limelightMode = Limelight .Mode .TARGET ;
21+ private double adjustment = 0 ;
22+ private double minError = 0.05 ; // TODO: Test minimum values
23+ private double prevSpeed = 0 , prevLeft = 0 , prevRight = 0 ;
24+ private double outreachSpeed = 0.3 ;
25+
26+ public Drive (Drivetrain dt , Limelight lime , Joystick leftJoy , Joystick rightJoy ) {
3027 requires (dt );
3128 this .dt = dt ;
29+ this .lime = lime ;
30+ this .dt = dt ;
3231 this .leftJoy = leftJoy ;
3332 this .rightJoy = rightJoy ;
3433
@@ -45,12 +44,22 @@ public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) {
4544 }
4645 }
4746
47+ // Called just before this Command runs the first time
48+ @ Override
49+ protected void initialize () {
50+ }
51+
52+ // Called repeatedly when this Command is scheduled to run
4853 @ Override
4954 protected void execute () {
50- if (SmartDashboard .getBoolean ("Arcade Drive " , true )) {
51- arcadeDrive ();
55+ if (SmartDashboard .getBoolean ("Using Limelight " , false )) {
56+ autoAllign ();
5257 } else {
53- tankDrive ();
58+ if (SmartDashboard .getBoolean ("Arcade Drive" , true )) {
59+ arcadeDrive ();
60+ } else {
61+ tankDrive ();
62+ }
5463 }
5564 }
5665
@@ -230,16 +239,49 @@ private void charDrive(double left, double right) {
230239 dt .disableVoltageCompensation ();
231240 }
232241
242+ private void autoAllign () {
243+ if (limelightMode == Limelight .Mode .DIST ) {
244+ adjustment = lime .distanceAssist ();
245+ dt .drive (adjustment , adjustment );
246+ if (Math .abs (adjustment ) < minError ) {
247+ SmartDashboard .putBoolean ("Finished Alligning" , true );
248+ }
249+ }
250+ else if (limelightMode == Limelight .Mode .STEER ) {
251+ adjustment = lime .distanceAssist ();
252+ dt .drive (adjustment , -adjustment );
253+ if (Math .abs (adjustment ) < minError ) {
254+ SmartDashboard .putBoolean ("Finished Alligning" , true );
255+ }
256+ } else {
257+ double [] params = lime .autoTarget ();
258+ dt .drive (params [0 ], params [1 ]);
259+ double maxInput = Math .max (Math .abs (params [0 ]), Math .abs (params [1 ]));
260+ if (maxInput < minError ) {
261+ SmartDashboard .putBoolean ("Finished Alligning" , true );
262+ }
263+ }
264+ }
265+
266+ // Make this return true when this Command no longer needs to run execute()
233267 @ Override
234268 protected boolean isFinished () {
235- return false ;
269+ if (!SmartDashboard .getBoolean ("Use Limelight" , false ) || SmartDashboard .getBoolean ("Finished Alligning" , false )) {
270+ SmartDashboard .putBoolean ("Use Limelight" , false );
271+ SmartDashboard .putBoolean ("Finished Alligning" , false );
272+ return true ;
273+ } else {
274+ return false ;
275+ }
236276 }
237277
278+ // Called once after isFinished returns true
238279 @ Override
239280 protected void end () {
240- dt .stop ();
241281 }
242282
283+ // Called when another command which requires one or more of the same
284+ // subsystems is scheduled to run
243285 @ Override
244286 protected void interrupted () {
245287 end ();
0 commit comments