This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathPID_Drive.java
More file actions
114 lines (94 loc) · 3.03 KB
/
Copy pathPID_Drive.java
File metadata and controls
114 lines (94 loc) · 3.03 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot.commands;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.RobotContainer;
import frc.robot.subsystems.DrivetrainSubsystem;
import frc.robot.subsystems.Intake;
public class PID_Drive extends CommandBase {
/** Creates a new PID_Drive. */
public double setpoint;
public double Kp;
public double Ki;
public double Kd;
public double error;
public double lastError;
public double integral;
public double derivative;
public double avgPos;
public double motorPower;
public Boolean done = false;
public DrivetrainSubsystem drive;
public PID_Drive(DrivetrainSubsystem drivetrainSubsystem, double s) {
// Use addRequirements() here to declare subsystem dependencies.
addRequirements(drivetrainSubsystem);
RobotContainer.getDriveTrainSubsystem().resetEncoders();
setpoint = s;
Kp = 0.07; //controls speed - DOES NOT EFFECT DISTANCE
Ki = 0;
Kd = 0.01; //effects stopping smoothness
error = 0;
lastError = 0;
integral = 0;
derivative = 0;
avgPos = RobotContainer.getDriveTrainSubsystem().getAverageEncoderDistance();
motorPower = 0;
//drivetrainSubsystem.zeroHeading();
System.out.println("STARTING");
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
avgPos = RobotContainer.getDriveTrainSubsystem().getAverageEncoderDistance();
error = setpoint - avgPos;
integral = 0;
RobotContainer.getDriveTrainSubsystem().resetEncoders();
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
//Intake.intake(-0.1);
if (Math.abs(error) > (Math.abs(setpoint) / 1.5)){
error = Math.abs(setpoint) - Math.abs(DrivetrainSubsystem.getAverageEncoderDistance());
integral = integral + error;
derivative = error - lastError;
motorPower = (Kp * error) + (Ki * integral) + (Kd * derivative);
if (motorPower > .65){
motorPower = .65;
}
if (motorPower < .20){
motorPower = 0;
}
if (setpoint < 0){
motorPower = -motorPower;
}
if (motorPower == 0){
done = true;
}
System.out.println(error);
//DrivetrainSubsystem.arcadeDrive((-motorPower), 0);
DrivetrainSubsystem.TankDrive((-motorPower - (motorPower / 40)), (-motorPower));
}
else{
//System.out.println("Done");
done = true;
//end(true);
}
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
//Intake.intake(0);
RobotContainer.getDriveTrainSubsystem().TankDrive(0, 0);
//DrivetrainSubsystem.resetEncoders();
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
if (done){
return true;
}
return false;
}
}