Skip to content

Commit

Permalink
Arm Sim
Browse files Browse the repository at this point in the history
  • Loading branch information
manthan-acharya committed Feb 3, 2024
1 parent 0ddb8bb commit 62465e6
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 44 deletions.
28 changes: 22 additions & 6 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import frc.robot.subsystems.apriltagvision.AprilTagVisionConstants;
import frc.robot.subsystems.apriltagvision.AprilTagVisionIONorthstar;
import frc.robot.subsystems.drive.*;
import frc.robot.subsystems.superstructure.Arm.Arm;
import frc.robot.subsystems.superstructure.Arm.ArmIO;
import frc.robot.subsystems.superstructure.Arm.ArmIOKrakenFOC;
import frc.robot.subsystems.superstructure.Arm.ArmIOSim;
import frc.robot.subsystems.superstructure.intake.Intake;
import frc.robot.subsystems.superstructure.intake.IntakeIO;
import frc.robot.subsystems.superstructure.intake.IntakeIOSim;
Expand Down Expand Up @@ -58,6 +62,7 @@ public class RobotContainer {
private AprilTagVision aprilTagVision;
private Shooter shooter;
private Intake intake;
private Arm arm;

// Controller
private final CommandXboxController controller = new CommandXboxController(0);
Expand All @@ -84,6 +89,7 @@ public RobotContainer() {
new AprilTagVisionIONorthstar(AprilTagVisionConstants.cameraNames[0]));
shooter = new Shooter(new ShooterIOSparkMax());
intake = new Intake(new IntakeIOSparkMax());
arm = new Arm(new ArmIOKrakenFOC());
}
}
}
Expand All @@ -98,6 +104,7 @@ public RobotContainer() {
new ModuleIOSim(DriveConstants.moduleConfigs[3]));
shooter = new Shooter(new ShooterIOSim());
intake = new Intake(new IntakeIOSim());
arm = new Arm(new ArmIOSim());
}
default -> {
// Replayed robot, disable IO implementations
Expand All @@ -110,6 +117,7 @@ public RobotContainer() {
new ModuleIO() {});
shooter = new Shooter(new ShooterIO() {});
intake = new Intake(new IntakeIO() {});
arm = new Arm(new ArmIO() {});
}
}

Expand All @@ -135,6 +143,10 @@ public RobotContainer() {
intake = new Intake(new IntakeIO() {});
}

if (arm == null) {
arm = new Arm(new ArmIO() {});
}

autoChooser = new LoggedDashboardChooser<>("Auto Choices");
// Set up feedforward characterization
autoChooser.addOption(
Expand Down Expand Up @@ -194,12 +206,16 @@ private void configureButtonBindings() {
// () -> -controller.getLeftX(),
// () -> -controller.getRightX()));
// controller.a().onTrue(DriveCommands.toggleCalculateShotWhileMovingRotation(drive));
controller
.a()
.onTrue(Commands.either(intake.stopCommand(), intake.intakeCommand(), intake::running));
controller
.x()
.onTrue(Commands.either(intake.stopCommand(), intake.ejectCommand(), intake::running));
controller.a().onTrue(arm.home());
arm.setDefaultCommand(arm.runToSetpoint());
// controller
// .a()
// .onTrue(Commands.either(intake.stopCommand(), intake.intakeCommand(),
// intake::running));
// controller
// .x()
// .onTrue(Commands.either(intake.stopCommand(), intake.ejectCommand(),
// intake::running));
controller
.b()
.onTrue(
Expand Down
156 changes: 143 additions & 13 deletions src/main/java/frc/robot/subsystems/superstructure/Arm/Arm.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,154 @@
package frc.robot.subsystems.superstructure.Arm;

import static frc.robot.subsystems.superstructure.SuperstructureConstants.ArmConstants.*;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismRoot2d;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj.util.Color8Bit;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.util.EqualsUtil;
import frc.robot.util.LoggedTunableNumber;
import java.util.function.BooleanSupplier;
import org.littletonrobotics.junction.Logger;

public class Arm extends SubsystemBase {
private final ArmIO io;
//prob change this maybe i think
private final ArmIO.ArmIOInputs inputs = new ArmIO.ArmIOInputs();
private final LoggedTunableNumber armP = new LoggedTunableNumber("Arm/P", 6.0);
private final LoggedTunableNumber armI = new LoggedTunableNumber("Arm/I", 3.0);
private final LoggedTunableNumber armD = new LoggedTunableNumber("Arm/D", 2.0);
private final LoggedTunableNumber armF = new LoggedTunableNumber("Arm/F", 8.0);
private final LoggedTunableNumber armMaxVelocity = new LoggedTunableNumber("Arm/MaxVelocity", 6.3);
private final LoggedTunableNumber armMaxAcceleration = new LoggedTunableNumber("Arm/MaxAcceleration", 2.8);
private static final LoggedTunableNumber kP =
new LoggedTunableNumber("Arm/Kp", controllerConstants.kP());
private static final LoggedTunableNumber kI =
new LoggedTunableNumber("Arm/Ki", controllerConstants.kI());
private static final LoggedTunableNumber kD =
new LoggedTunableNumber("Arm/Kd", controllerConstants.kD());
private static final LoggedTunableNumber kS =
new LoggedTunableNumber("Arm/Ks", controllerConstants.ffkS());
private static final LoggedTunableNumber kV =
new LoggedTunableNumber("Arm/Kv", controllerConstants.ffkV());
private static final LoggedTunableNumber kA =
new LoggedTunableNumber("Arm/Ka", controllerConstants.ffkA());
private static final LoggedTunableNumber kG =
new LoggedTunableNumber("Arm/Kg", controllerConstants.ffkG());
private static final LoggedTunableNumber armVelocity =
new LoggedTunableNumber("Arm/Velocity", profileConstraints.cruiseVelocityRadPerSec());
private static final LoggedTunableNumber armAcceleration =
new LoggedTunableNumber("Arm/Acceleration", profileConstraints.accelerationRadPerSec2());
private static final LoggedTunableNumber armTolerance =
new LoggedTunableNumber("Arm/Tolerance", positionTolerance.getRadians());
private static final LoggedTunableNumber armDesiredSetpoint =
new LoggedTunableNumber("Arm/SetpointDegrees", 0.0);

private final ArmIO armIO;
private final ArmIOInputsAutoLogged inputs = new ArmIOInputsAutoLogged();
private BooleanSupplier disableSupplier = () -> false;
private BooleanSupplier coastSupplier = () -> false;

private final Mechanism2d armMechanism;
private final MechanismRoot2d armRoot;
private final MechanismLigament2d armMeasured;

// private final MechanismLigament2d armSetpoint;

public Arm(ArmIO io) {
System.out.println("[Init] Creating Arm");
this.armIO = io;
io.setBrakeMode(true);

// Create a mechanism
armMechanism = new Mechanism2d(2, 3, new Color8Bit(Color.kAntiqueWhite));
armRoot = armMechanism.getRoot("Arm Joint", armOrigin2d.getX(), armOrigin2d.getY());
armMeasured =
new MechanismLigament2d(
"Arm Measured",
armLength,
inputs.armAnglePosition.getDegrees(),
2.0,
new Color8Bit(Color.kBlack));
armRoot.append(armMeasured);
// armSetpoint =
// new MechanismLigament2d(
// "Arm Setpoint",
// armLength,
// inputs.armReferencePosition.getDegrees(),
// 10.0,
// new Color8Bit(Color.kGreen));
// armRoot.append(armSetpoint);
}

@Override
public void periodic() {
// Process inputs
armIO.updateInputs(inputs);
Logger.processInputs("Arm", inputs);

// Update controllers
LoggedTunableNumber.ifChanged(
hashCode(), () -> armIO.setPID(kP.get(), kI.get(), kD.get()), kP, kI, kD);
LoggedTunableNumber.ifChanged(
hashCode(), () -> armIO.setFF(kS.get(), kV.get(), kA.get(), kG.get()));
LoggedTunableNumber.ifChanged(
hashCode(),
constraints -> armIO.setProfileConstraints(constraints[0], constraints[1]),
armVelocity,
armAcceleration);

if (DriverStation.isDisabled()) {
armIO.stop();
}

if (coastSupplier.getAsBoolean()) armIO.setBrakeMode(false);

// Logs
armMeasured.setAngle(inputs.armAnglePosition);
// armSetpoint.setAngle(inputs.armReferencePosition);
Logger.recordOutput("Arm/SetpointAngle", inputs.armReferencePosition);
Logger.recordOutput("Arm/MeasuredAngle", inputs.armAnglePosition);
Logger.recordOutput("Arm/VelocityRadsPerSec", inputs.armVelocityRadsPerSec);
Logger.recordOutput("Arm/Mechanism", armMechanism);
}

public Command runToSetpoint() {
return run(
() -> {
if (inputs.homed) {
Rotation2d setpoint =
Rotation2d.fromDegrees(
MathUtil.clamp(
armDesiredSetpoint.get(), minAngle.getDegrees(), maxAngle.getDegrees()));
setSetpoint(setpoint);
}
});
}

public Command home() {
return run(() -> armIO.setVoltage(-1.0))
.until(
() -> {
boolean homed = EqualsUtil.epsilonEquals(inputs.armVelocityRadsPerSec, 0.0, 0.01);
System.out.println(homed);
return homed;
})
.andThen(() -> armIO.setHome());
}

public Arm(ArmIO io) {
System.out.println("[Init] Creating Arm");
this.io = io;
public void setSetpoint(Rotation2d setpoint) {
if (disableSupplier.getAsBoolean() || !inputs.homed) return;
armIO.setSetpoint(setpoint.getRadians());
}

public void setBrakeMode(boolean enabled) {
armIO.setBrakeMode(enabled);
}

}
public void setOverrides(BooleanSupplier disableSupplier, BooleanSupplier coastSupplier) {
this.disableSupplier = disableSupplier;
this.coastSupplier = coastSupplier;
}

public void stop() {
armIO.stop();
}
}
65 changes: 47 additions & 18 deletions src/main/java/frc/robot/subsystems/superstructure/Arm/ArmIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,51 @@

public interface ArmIO {

@AutoLog
class ArmIOInputs {

public double armVelocityRadsPerSec = 0.0;
public double armAppliedVolts = 0.0;
public double[] armCurrentAmps = new double[] {};
public double [] armTempCelcius = new double[] {};
public Rotation2d armInternalPosition = new Rotation2d();
public Rotation2d armAnglePosition = new Rotation2d();

}
default void updateInputs(ArmIOInputs inputs) {}
default void setSetpoint(Rotation2d setpoint) {}
default void setBrakeMode(boolean enabled) {}
default void stop() {}



@AutoLog
class ArmIOInputs {
public Rotation2d armAnglePosition = new Rotation2d();
public Rotation2d armReferencePosition = new Rotation2d();
public double armVelocityRadsPerSec = 0.0;
public boolean homed = false;
public boolean atSetpoint = false;
public double[] armAppliedVolts = new double[] {};
public double[] armCurrentAmps = new double[] {};
public double[] armTorqueCurrentAmps = new double[] {};
public double[] armTempCelcius = new double[] {};
}

default void updateInputs(ArmIOInputs inputs) {}

/** Run to setpoint angle in radians */
default void setSetpoint(double setpointRads) {}

/** Run motors at voltage */
default void setVoltage(double volts) {}

/** Set brake mode enabled */
default void setBrakeMode(boolean enabled) {}

/** Set FF values */
default void setFF(double s, double v, double a, double g) {}

/** Set PID values */
default void setPID(double p, double i, double d) {}

/** Set MotionMagic constraints */
default void setProfileConstraints(
double cruiseVelocityRadsPerSec, double accelerationRadsPerSec2) {}

/**
* Stops motors
*
* @params none!
*/
default void stop() {}

/**
* Set current position to home
*
* @params none!
*/
default void setHome() {}
}
Loading

0 comments on commit 62465e6

Please sign in to comment.