1 package org
.usfirst
.frc
.team3501
.robot
.commands
.driving
;
3 import org
.usfirst
.frc
.team3501
.robot
.Robot
;
4 import org
.usfirst
.frc
.team3501
.robot
.subsystems
.DriveTrain
;
5 import org
.usfirst
.frc
.team3501
.robot
.utils
.PIDController
;
7 import edu
.wpi
.first
.wpilibj
.Preferences
;
8 import edu
.wpi
.first
.wpilibj
.command
.Command
;
9 import edu
.wpi
.first
.wpilibj
.smartdashboard
.SmartDashboard
;
12 * This command makes the robot drive a specified distance using encoders on the
13 * robot and using a feedback loop
15 * parameters: distance the robot will move in inches motorVal: the motor input
16 * to set the motors to
18 public class DriveDistance
extends Command
{
19 private DriveTrain driveTrain
= Robot
.getDriveTrain();
20 private double maxTimeOut
= 10;
21 private double target
;
22 private double zeroAngle
;
23 private Preferences prefs
;
24 private PIDController driveController
;
26 private double driveStraightGyroP
;
28 public DriveDistance(double distance
, double maxTimeOut
) {
30 this.target
= distance
;
31 driveStraightGyroP
= SmartDashboard
.getNumber(driveTrain
.DRIVE_GYRO_P_Val
,
32 driveTrain
.PID_ERROR
);
34 this.driveController
= driveTrain
.getDriveController();
35 this.driveController
.setDoneRange(0.5);
36 this.driveController
.setMaxOutput(1.1);
37 this.driveController
.setMinDoneCycles(5);
41 protected void initialize() {
42 this.driveTrain
.resetEncoders();
43 this.driveController
.setSetPoint(this.target
);
47 protected void execute() {
48 double xVal
= driveStraightGyroP
* (driveTrain
.getAngle() - zeroAngle
);
49 double yVal
= driveController
.calcPID(driveTrain
.getAvgEncoderDistance());
51 double leftDrive
= yVal
- xVal
;
52 double rightDrive
= yVal
+ xVal
;
53 this.driveTrain
.setMotorValues(leftDrive
, rightDrive
);
56 "PID VALS: " + driveController
.getP() + " " + driveController
.getI()
57 + " " + driveController
.getD());
59 System
.out
.println(driveTrain
.getAvgEncoderDistance());
63 protected boolean isFinished() {
64 return timeSinceInitialized() >= maxTimeOut
65 || this.driveController
.isDone();
69 protected void end() {
73 protected void interrupted() {