-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriveDistance.java
More file actions
97 lines (83 loc) · 2.83 KB
/
DriveDistance.java
File metadata and controls
97 lines (83 loc) · 2.83 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package frc.robot.commands.auto;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.Drivetrain;
import frc.robot.subsystems.Gyro;
public class DriveDistance extends Command {
Drivetrain drivetrain;
Gyro gyro;
Pose2d target;
double xDistance;
double yDistance;
double theta;
double xDisplacement;
double yDisplacement;
double angleDisplacement;
double xyThreshold = 0.6;
double xyMaxSpeed;
double rotateMaxSpeed = 1.5;
public DriveDistance(double xyMaxSpeed, Drivetrain drivetrain, Gyro gyro, double xDistance, double yDistance, double theta) {
this.drivetrain = drivetrain;
this.gyro = gyro;
this.xDistance = xDistance;
this.yDistance = yDistance;
this.theta = theta;
this.xyMaxSpeed = xyMaxSpeed;
addRequirements(drivetrain);
}
@Override
public void initialize() {
System.out.println("drivedistance initialized");
Pose2d start = drivetrain.getPose2d();
this.target = new Pose2d((start.getX() + xDistance), (start.getY() + yDistance), new Rotation2d(theta));
}
@Override
public void execute() {
// TODO: add conversion to inches if needed
Pose2d currentPos = drivetrain.getPose2d();
this.xDisplacement = target.getX() - currentPos.getX();
double xSpeed = xDisplacement * 0.6;
if (xSpeed > 0) {
xSpeed = Math.min(xSpeed, xyMaxSpeed);
} else {
xSpeed = Math.max(xSpeed, -xyMaxSpeed);
}
// if (Math.abs(xDisplacement) <= xyThreshold) {
// xSpeed = 0.0;
// }
SmartDashboard.putNumber("xDisplacement", xDisplacement);
this.yDisplacement = target.getY() - currentPos.getY();
double ySpeed = yDisplacement * 0.6;
if (ySpeed > 0) {
ySpeed = Math.min(ySpeed, xyMaxSpeed);
} else {
ySpeed = Math.max(ySpeed, -xyMaxSpeed);
}
// if (Math.abs(yDisplacement) <= xyThreshold) {
// ySpeed = 0.0;
// }
SmartDashboard.putNumber("yDisplacement", yDisplacement);
this.angleDisplacement = (gyro.getRotation2d().minus(target.getRotation())).getRadians();
double rotateSpeed = angleDisplacement * 1.0;
if (rotateSpeed > 0) {
rotateSpeed = Math.min(rotateSpeed, rotateMaxSpeed);
} else {
rotateSpeed = Math.max(rotateSpeed, -rotateMaxSpeed);
}
SmartDashboard.putNumber("angleDisplacement", angleDisplacement);
// System.out.println("x displacement: " + xDisplacement);
// System.out.println("y displacement: " + yDisplacement);
// System.out.println("angle displacement: ", angleDisplacement);
drivetrain.drive(xSpeed, ySpeed, -rotateSpeed * 1.0);
}
@Override
public void end(boolean interrupted) {
drivetrain.drive(0.0, 0.0, 0.0);
}
@Override
public boolean isFinished() {
return (Math.abs(xDisplacement) <= xyThreshold && Math.abs(yDisplacement) <= xyThreshold && angleDisplacement <= 0.4);
}
}