Skip to content

Commit

Permalink
NT-PID -> main (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatribotsProgramming authored Feb 25, 2024
2 parents 747efbb + f1c04ea commit d17e80a
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 446 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void simulationPeriodic() {
REVPhysicsSim.getInstance().run();
Robot.alliance = DriverStation.getAlliance();

for (Neo neo : NeoMotorConstants.MOTOR_LIST) {
for (Neo neo : NeoMotorConstants.MOTOR_MAP.values()) {
neo.tick();
}
}
Expand Down
69 changes: 4 additions & 65 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.event.EventLoop;
import edu.wpi.first.wpilibj.smartdashboard.Field2d;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.Commands;
Expand All @@ -24,6 +23,7 @@
import frc.robot.commands.drive.Drive;
import frc.robot.commands.misc.leds.LPI;
import frc.robot.commands.subsytemHelpers.AlignmentCmds;
import frc.robot.commands.subsytemHelpers.NTPIDTuner;
import frc.robot.commands.subsytemHelpers.PieceControl;
import frc.robot.commands.subsytemHelpers.ShooterCmds;
import frc.robot.subsystems.*;
Expand All @@ -37,14 +37,11 @@
import frc.robot.util.constants.Constants.DriveConstants;
import frc.robot.util.constants.Constants.FieldConstants;
import frc.robot.util.constants.Constants.NTConstants;
import frc.robot.util.constants.Constants.NeoMotorConstants;
import frc.robot.util.constants.Constants.OIConstants;
import frc.robot.util.mod.PatriBoxController;
import frc.robot.util.motors.Neo;
import frc.robot.util.testing.CalibrationControl;
import frc.robot.util.testing.HDCTuner;
import frc.robot.util.testing.PIDNotConstants;
import frc.robot.util.testing.PIDTunerCommands;
import monologue.Annotations.Log;
import monologue.Logged;

Expand All @@ -71,7 +68,6 @@ public class RobotContainer implements Logged {
private ChoreoStorage choreoPathStorage;
private PathPlannerStorage pathPlannerStorage;
private CalibrationControl calibrationControl;
private PIDTunerCommands PIDTuner;
private AlignmentCmds alignmentCmds;

public static HDCTuner HDCTuner;
Expand Down Expand Up @@ -114,13 +110,9 @@ public RobotContainer() {
AutoConstants.HDC.getThetaController());

Neo.incinerateMotors();
new NTPIDTuner().schedule();

shooterCmds = new ShooterCmds(shooter, pivot);

PIDTuner = new PIDTunerCommands(new PIDNotConstants[] {
swerve.getDrivingPidNotConstants(),
swerve.getTurningPidNotConstants()
});

alignmentCmds = new AlignmentCmds(swerve, climb, shooterCmds);

Expand Down Expand Up @@ -269,36 +261,6 @@ private void configureSimulationBindings(PatriBoxController controller) {
.onTrue(pieceControl.shootWhenReady(swerve::getPose, swerve::getRobotRelativeVelocity));
}

private void configurePIDTunerBindings(PatriBoxController controller) {
controller.pov(0, 270, testButtonBindingLoop)
.onTrue(PIDTuner.incrementSubsystemCommand());

controller.pov(0, 90, testButtonBindingLoop)
.onTrue(PIDTuner.decreaseSubsystemCommand());

controller.pov(0, 0, testButtonBindingLoop)
.onTrue(PIDTuner.increaseCurrentPIDCommand(.1));

controller.pov(0, 180, testButtonBindingLoop)
.onTrue(PIDTuner.increaseCurrentPIDCommand(-.1));

controller.rightBumper(testButtonBindingLoop)
.onTrue(PIDTuner.PIDIncrementCommand());

controller.leftBumper(testButtonBindingLoop)
.onTrue(PIDTuner.PIDDecreaseCommand());

controller.a(testButtonBindingLoop)
.onTrue(PIDTuner.logCommand());

controller.x(testButtonBindingLoop)
.onTrue(PIDTuner.multiplyPIDCommand(2));

controller.b(testButtonBindingLoop)
.onTrue(PIDTuner.multiplyPIDCommand(.5));

}

private void configureCalibrationBindings(PatriBoxController controller) {
controller.leftBumper(testButtonBindingLoop).onTrue(pieceControl.stopAllMotors());
controller.rightBumper(testButtonBindingLoop).onTrue(calibrationControl.updateMotorsCommand());
Expand Down Expand Up @@ -354,27 +316,9 @@ private void configureHDCBindings(PatriBoxController controller) {
}

public Command getAutonomousCommand() {
return driver.getYButton() ? choreoChooser.getSelected() : pathPlannerStorage.getSelectedAuto();
return pathPlannerStorage.getSelectedAuto();
}

@Log.NT
public static SendableChooser<Command> choreoChooser = new SendableChooser<>();
// PathPlannerPath starting = PathPlannerPath.fromChoreoTrajectory("S W3-1S C1");
private void setupChoreoChooser() {
// // TODO: Autos currently start at C1-5, we need to integrate the other paths
// // with the center line schenanigans to make full autos
// choreoChooser.setDefaultOption("Do Nothing", Commands.none());
// choreoChooser.addOption("W3-1 C1-5",
// swerve.resetOdometryCommand(
// () -> starting.getPreviewStartingHolonomicPose()
// .plus(new Transform2d(
// new Translation2d(),
// Rotation2d.fromDegrees(180))))
// .andThen(
// AutoBuilder.followPath(starting)
// .andThen(choreoPathStorage.generateCenterLineComplete(1, 5, false))));
}

public void onDisabled() {
swerve.stopDriving();
pieceControl.stopAllMotors().schedule();
Expand Down Expand Up @@ -487,14 +431,9 @@ private void fixPathPlannerCommands() {
registerPathToPathCommands();
pathPlannerStorage.configureAutoChooser();
}

private void incinerateMotors() {
for (Neo neo : NeoMotorConstants.MOTOR_LIST) {
neo.burnFlash();
}
}

private void initializeComponents() {

Pose3d initialShooterPose = new Pose3d(
NTConstants.PIVOT_OFFSET_METERS.getX(),
0,
Expand Down
165 changes: 165 additions & 0 deletions src/main/java/frc/robot/commands/subsytemHelpers/NTPIDTuner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package frc.robot.commands.subsytemHelpers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;

import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.util.constants.Constants.NeoMotorConstants;
import frc.robot.util.motors.Neo;

public class NTPIDTuner extends Command {

NetworkTable table = NetworkTableInstance.getDefault().getTable("Calibration");
Map<List<Neo>, Map<NetworkTableEntry, Double>> entries = new HashMap<>();

public NTPIDTuner() {
// Initialize the motor group map
Map<String, List<Neo>> motorGroups = NeoMotorConstants.initializeMotorGroupMap();

// Initialize the NetworkTableEntries for the PID values of each motor group
initializeMotorEntries(motorGroups);
}

@Override
public boolean runsWhenDisabled() {
return true;
}

private void initializeMotorEntries(Map<String, List<Neo>> motorGroups) {
// Loop through each motor group
for (int i = 0; i < motorGroups.size(); i++) {

// Get the name of the current motor group
String groupName = motorGroups.keySet().toArray()[i].toString();

// Get the NetworkTableEntries for the PID values of the current motor group
NetworkTableEntry pEntry = table.getEntry(groupName + "/0-P");
NetworkTableEntry iEntry = table.getEntry(groupName + "/1-I");
NetworkTableEntry dEntry = table.getEntry(groupName + "/2-D");
NetworkTableEntry ffEntry = table.getEntry(groupName + "/3-FF");
NetworkTableEntry iZoneEntry = table.getEntry(groupName + "/4-IZone");

// Get the first motor of the current motor group
List<Neo> motorGroup = motorGroups.get(groupName);
Neo motor = motorGroup.get(0);

// Get the PID values of the first motor
double pValue = motor.getP();
double iValue = motor.getI();
double dValue = motor.getD();
double ffValue = motor.getFF();
double iZoneValue = motor.getIZone();

// Set the PID values in the NetworkTableEntries
pEntry.setDouble(pValue);
iEntry.setDouble(iValue);
dEntry.setDouble(dValue);
ffEntry.setDouble(ffValue);
iZoneEntry.setDouble(iZoneValue);

// Create a map of NetworkTableEntries and their corresponding PID values
Map<NetworkTableEntry, Double> motorEntries = new HashMap<>();
motorEntries.put(pEntry, pValue);
motorEntries.put(iEntry, iValue);
motorEntries.put(dEntry, dValue);
motorEntries.put(ffEntry, ffValue);
motorEntries.put(iZoneEntry, iZoneValue);

// Add the motor group and its corresponding NetworkTableEntries to the entries map
entries.put(motorGroup, motorEntries);
}
}

@Override
public void execute() {
// Loop through all of the entries and update the values
for (Map.Entry<List<Neo>, Map<NetworkTableEntry, Double>> entry : entries.entrySet()) {
updateValues(entry);
}
}

private void updateValues(Map.Entry<List<Neo>, Map<NetworkTableEntry, Double>> entry) {
for (Map.Entry<NetworkTableEntry, Double> entry2 : entry.getValue().entrySet()) {
double NTValue = entry2.getKey().getDouble(entry2.getValue());
if (NTValue != entry2.getValue()) {
String entryPath = entry2.getKey().getName();
// We are given a full path like "/Calibration//FrontLeftTurn/3-FF"
// We just want that "FF" part
String lastPart = entryPath.substring(entryPath.lastIndexOf("-") + 1);

if (lastPart.equals("IZone")) {
if (NTValue > 99) {
NTValue = Double.POSITIVE_INFINITY;
} else if (NTValue < 0) {
NTValue = 0;
}
if (NTValue == entry2.getValue()) {
return;
}
entry2.setValue(NTValue);
}

// Update the last known value for future comparisons
entry2.setValue(NTValue);
updateController(entry.getKey(), lastPart, NTValue);
}
}
}

private void updateController(List<Neo> motorList, String name, double NTValue) {
// Find out if it was P, I, D, FF, or IZone and update the value
for (Neo motor : motorList) {
switch (name) {
case "P":
motor.setP(NTValue);
break;
case "I":
motor.setI(NTValue);
break;
case "D":
motor.setD(NTValue);
break;
case "FF":
motor.setFF(NTValue);
break;
case "IZone":
motor.setIZone(NTValue);
break;
}
}
System.err.println("Updated " + getKeyByValue(NeoMotorConstants.MOTOR_GROUPS, motorList) + "'s " + name + " to " + NTValue);
}

/**
* Retrieves the key associated with a given value in a map.
*
* @param map the map to search in
* @param value the value to search for
* @return the key associated with the given value, or null if not found
*/
private <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Entry<T, E> entry : map.entrySet()) {
if (Objects.equals(value, entry.getValue())) {
return entry.getKey();
}
}
return null;
}

// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}

// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
8 changes: 0 additions & 8 deletions src/main/java/frc/robot/subsystems/Climb.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import frc.robot.util.constants.Constants.FieldConstants;
import frc.robot.util.constants.Constants.NTConstants;
import frc.robot.util.motors.Neo;
import frc.robot.util.testing.PIDNotConstants;
import monologue.Logged;
import monologue.Annotations.Log;

public class Climb extends SubsystemBase implements Logged {

private final Neo leftMotor;
private final Neo rightMotor;
private final PIDNotConstants climbPID;

@Log
private double posLeft = 0, posRight = 0, targetPosRight = 0, targetPosLeft = 0;
Expand All @@ -38,7 +36,6 @@ public Climb() {
rightMotor = new Neo(ClimbConstants.RIGHT_CLIMB_CAN_ID, !FieldConstants.IS_SIMULATION);

configureMotors();
climbPID = new PIDNotConstants(leftMotor.getPID(), leftMotor.getPIDController());
}

private void configureMotors() {
Expand Down Expand Up @@ -72,11 +69,6 @@ public void periodic() {
);
}


public PIDNotConstants getPidNotConstants() {
return this.climbPID;
}

public void setPosition(Pair<Double, Double> positionPair) {
setPosition(positionPair.getFirst(), positionPair.getSecond());
}
Expand Down
Loading

0 comments on commit d17e80a

Please sign in to comment.