Skip to content

Commit

Permalink
Merge pull request #18 from Merridew1/DrivetrainPID
Browse files Browse the repository at this point in the history
Drivetrain pid
  • Loading branch information
Merridew1 authored Dec 9, 2024
2 parents b847cc0 + 1a6590a commit 27ec070
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static final class DriveTrain {
public static double P = 0;
public static double I = 0;
public static double D = 0;
public static double MAX_VELOCITY = 0;
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/frc/robot/subsystems/drive/Drivetrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.littletonrobotics.junction.Logger;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
Expand All @@ -14,6 +15,10 @@
public class Drivetrain extends SubsystemBase {
private DrivetrainIO io;
private DrivetrainIOInputsAutoLogged inputs = new DrivetrainIOInputsAutoLogged();
PIDController driveTrainRightPidController = new PIDController(Constants.PID.DriveTrain.P,
Constants.PID.DriveTrain.I, Constants.PID.DriveTrain.D);
PIDController driveTrainLeftPIDController = new PIDController(0, 0, 0);
SimpleMotorFeedforward feedforward = new SimpleMotorFeedforward(0, 0);

/**
* Create Wrist Intake Subsystem
Expand All @@ -29,8 +34,11 @@ public void periodic() {
}

public void drive(Double lPower, double rPower) {
io.setDrivePower(driveTrainPidController.calculate(lPower),
driveTrainPidController.calculate(rPower));
io.setDrivePower(
driveTrainLeftPIDController.calculate(inputs.leftVelocity,
lPower * Constants.PID.DriveTrain.MAX_VELOCITY),
driveTrainRightPidController.calculate(inputs.rightVelocity,
rPower * Constants.PID.DriveTrain.MAX_VELOCITY));
Logger.recordOutput("Drivetrain/leftPower", lPower);
Logger.recordOutput("Drivetrain/rightPower", rPower);
}
Expand All @@ -39,8 +47,6 @@ public Command driveCommand(CommandXboxController controller) {
return run(() -> drive(controller.getLeftY(), controller.getRightY()));
}

PIDController driveTrainPidController = new PIDController(Constants.PID.DriveTrain.P,
Constants.PID.DriveTrain.I, Constants.PID.DriveTrain.D);

}

2 changes: 2 additions & 0 deletions src/main/java/frc/robot/subsystems/drive/DrivetrainIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface DrivetrainIO {
@AutoLog
public static class DrivetrainIOInputs {
public Rotation2d gyroYaw = new Rotation2d();
public double rightVelocity;
public double leftVelocity;
}

/** Updates the set of loggable inputs. */
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/frc/robot/subsystems/drive/DrivetrainReal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package frc.robot.subsystems.drive;

import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.controls.DutyCycleOut;
import com.ctre.phoenix6.hardware.TalonFX;
import edu.wpi.first.math.geometry.Rotation2d;
import frc.robot.Constants;
Expand All @@ -13,6 +15,8 @@ public class DrivetrainReal implements DrivetrainIO {
TalonFX left2 = new TalonFX(Constants.Motors.DriveTrain.BACK_LEFT_MOTOR_ID, "CANivore");
TalonFX right1 = new TalonFX(Constants.Motors.DriveTrain.FRONT_RIGHT_MOTOR_ID, "CANivore");
TalonFX right2 = new TalonFX(Constants.Motors.DriveTrain.BACK_RIGHT_MOTOR_ID, "CANivore");
StatusSignal<Double> rightVelocity = right1.getVelocity();
StatusSignal<Double> leftVelocity = left1.getVelocity();

/**
* Drivetrain Real
Expand All @@ -25,17 +29,20 @@ public DrivetrainReal() {
@Override
public void updateInputs(DrivetrainIOInputs inputs) {
inputs.gyroYaw = Rotation2d.fromDegrees(0);
inputs.leftVelocity = leftVelocity.getValueAsDouble();
inputs.rightVelocity = rightVelocity.getValueAsDouble();

}

/**
* Drive Voltage
*/
@Override
public void setDrivePower(double lPower, double rPower) {
left1.set(lPower);
left2.set(lPower);
right1.set(rPower);
right2.set(rPower);
left1.setControl(new DutyCycleOut(rPower));
left2.setControl(new DutyCycleOut(lPower));
right1.setControl(new DutyCycleOut(rPower));
right2.setControl(new DutyCycleOut(rPower));
}

}

0 comments on commit 27ec070

Please sign in to comment.