This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
forked from CircuitOfLifeRobotics/Robot2018-ElGrandeAvocado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GyroTurn.java
73 lines (57 loc) · 1.82 KB
/
GyroTurn.java
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
package com.team3925.frc2018.commands;
import com.team3925.frc2018.subsystems.Drivetrain;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.command.PIDCommand;
public class GyroTurn extends PIDCommand {
private static final double THRESHOLD_ERROR = 2;
private double angle;
private double zeroGyro;
private double finalTime, startTime;
public GyroTurn(double desiredAngle, double timeInMillis) {
super(0.035, 0, 0.075);
angle = desiredAngle;
finalTime = timeInMillis;
}
protected void initialize() {
zeroGyro();
startTime = Timer.getFPGATimestamp();
System.out.println("GyroTurnStarted");
}
protected void execute() {
super.setSetpoint(angle * getSmoothingFunction(Timer.getFPGATimestamp(), startTime + finalTime));
System.out.println("Gyro: " + getGyroHeading() + "\tSetpoint = " + super.getSetpoint());
// System.out.println(Gyro.getInstance().getFusedHeading());
}
protected boolean isFinished() {
return (Math.abs(super.getPIDController().getError()) < THRESHOLD_ERROR);
}
protected void end() {
Drivetrain.getInstance().setRaw(0, 0);
}
protected void interrupted() {
this.end();
}
@Override
protected double returnPIDInput() {
return (getGyroHeading());
}
@Override
protected void usePIDOutput(double output) {
Drivetrain.getInstance().setRaw(output, -output);
}
private double getSmoothingFunction(double currentTime, double finalTime) {
// Desmos Graph: https://www.desmos.com/calculator/3sls1cegnx
return (0.5 * (1 - (Math.cos(Math.PI * (currentTime / finalTime)))));
}
private void zeroGyro() {
zeroGyro = Drivetrain.getInstance().getGyroHeading();
}
private double getGyroHeading() {
double heading = Drivetrain.getInstance().getGyroHeading();
double finalAngle = heading;
if (heading > 180) {
finalAngle = heading - 360;
}
return finalAngle;
}
}