Skip to content

Commit

Permalink
feat: (probable) sim functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mimizh2418 committed Jan 5, 2024
1 parent 2a08f6b commit e9f2276
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/team1540/advantagekitdemo/BuildConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public final class BuildConstants {
public static final String MAVEN_GROUP = "";
public static final String MAVEN_NAME = "advantageKitDemo";
public static final String VERSION = "unspecified";
public static final int GIT_REVISION = -1;
public static final String GIT_SHA = "UNKNOWN";
public static final String GIT_DATE = "UNKNOWN";
public static final String GIT_BRANCH = "UNKNOWN";
public static final String BUILD_DATE = "2024-01-03 22:28:19 PST";
public static final long BUILD_UNIX_TIME = 1704349699908L;
public static final int DIRTY = 129;
public static final int GIT_REVISION = 2;
public static final String GIT_SHA = "2a08f6b65c19ec91dfce3fb905ed68b2ab4608ec";
public static final String GIT_DATE = "2024-01-04 09:13:53 PST";
public static final String GIT_BRANCH = "main";
public static final String BUILD_DATE = "2024-01-04 16:52:55 PST";
public static final long BUILD_UNIX_TIME = 1704415975607L;
public static final int DIRTY = 1;

private BuildConstants(){}
}
24 changes: 22 additions & 2 deletions src/main/java/org/team1540/advantagekitdemo/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public enum SimulationMode {
REPLAY
}

public static final double DEADZONE_RADIUS = 0.1;

public static final class DrivetrainConstants {
public static final int FL_ID = 1;
public static final int BL_ID = 2;
Expand All @@ -53,7 +55,7 @@ public static final class DrivetrainConstants {
public static final double WHEEL_DIAMETER_METERS = Units.inchesToMeters(4.0);
public static final double TRACK_WIDTH_METERS = 26.5;

public static final double ROBOT_WEIGHT_KG = Units.lbsToKilograms(118); // omg its tem 118 teh robnots
public static final double ROBOT_MASS_KG = Units.lbsToKilograms(118); // omg its tem 118 teh robnots
public static final double ROBOT_MOI = 2.54; // omg its tem 254 teh chezy pofs

public static final double VELOCITY_KP = 3.2925;
Expand All @@ -79,10 +81,28 @@ public static final class ElevatorConstants {
public static final double KP = 1;
public static final double KI = 0;
public static final double KD = 0;
public static final TrapezoidProfile.Constraints MOTION_CONSTRAINTS = new TrapezoidProfile.Constraints(2.5, 12.5);
public static final TrapezoidProfile.Constraints MOTION_CONSTRAINTS =
new TrapezoidProfile.Constraints(2.5, 12.5);

public static final double KS = 0;
public static final double KG = 0.38;
public static final double KV = 0.19;
}

public static final class IntakeConstants {
public static final double WRIST_GEARING = 50;
public static final double INTAKE_LENGTH_METERS = Units.inchesToMeters(17);
public static final double INTAKE_MASS_KG = Units.lbsToKilograms(8);
public static final double INTAKE_MOI = INTAKE_MASS_KG * INTAKE_LENGTH_METERS * INTAKE_LENGTH_METERS / 3;

public static final double WRIST_KP = 1;
public static final double WRIST_KI = 0;
public static final double WRIST_KD = 0;
public static final TrapezoidProfile.Constraints MOTION_CONSTRAINTS =
new TrapezoidProfile.Constraints(1.8, 7.2);

public static final double WRIST_KS = 0;
public static final double WRIST_KG = 0.87;
public static final double WRIST_KV = 5.64;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/team1540/advantagekitdemo/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;
import org.team1540.advantagekitdemo.util.SuperstructureVisualizer;

/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
Expand Down Expand Up @@ -99,6 +100,7 @@ public void robotPeriodic() {
// This must be called from the robot's periodic block in order for anything in
// the Command-based framework to work.
CommandScheduler.getInstance().run();
SuperstructureVisualizer.periodic();
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/org/team1540/advantagekitdemo/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import org.team1540.advantagekitdemo.commands.ArcadeDriveCommand;
import org.team1540.advantagekitdemo.commands.ElevatorManualCommand;
import org.team1540.advantagekitdemo.commands.WristManualCommand;
import org.team1540.advantagekitdemo.subsystems.drivetrain.Drivetrain;
import org.team1540.advantagekitdemo.subsystems.drivetrain.DrivetrainIOReal;
import org.team1540.advantagekitdemo.subsystems.drivetrain.DrivetrainIOSim;
import org.team1540.advantagekitdemo.subsystems.elevator.Elevator;
import org.team1540.advantagekitdemo.subsystems.elevator.ElevatorIO;
import org.team1540.advantagekitdemo.subsystems.elevator.ElevatorIOSim;
import org.team1540.advantagekitdemo.subsystems.intake.Intake;
import org.team1540.advantagekitdemo.subsystems.intake.IntakeIO;
import org.team1540.advantagekitdemo.subsystems.intake.IntakeIOSim;

/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
Expand All @@ -30,18 +39,25 @@
*/
public class RobotContainer {
// Controller
private final CommandXboxController controller = new CommandXboxController(0);
private final CommandXboxController driver = new CommandXboxController(0);
private final CommandXboxController copilot = new CommandXboxController(1);

final Drivetrain drivetrain;
final Elevator elevator;
final Intake intake;

/**
* The container for the robot. Contains subsystems, OI devices, and commands.
*/
public RobotContainer() {
if (Robot.isReal()) {
drivetrain = new Drivetrain(new DrivetrainIOReal());
elevator = new Elevator(new ElevatorIO() {});
intake = new Intake(new IntakeIO() {});
} else {
drivetrain = new Drivetrain(new DrivetrainIOSim());
elevator = new Elevator(new ElevatorIOSim());
intake = new Intake(new IntakeIOSim());
}
configureButtonBindings();
}
Expand All @@ -53,6 +69,9 @@ public RobotContainer() {
* edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/
private void configureButtonBindings() {
drivetrain.setDefaultCommand(new ArcadeDriveCommand(drivetrain, driver));
elevator.setDefaultCommand(new ElevatorManualCommand(elevator, copilot));
intake.setDefaultCommand(new WristManualCommand(intake, copilot));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.team1540.advantagekitdemo.commands;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import org.team1540.advantagekitdemo.Constants;
import org.team1540.advantagekitdemo.subsystems.drivetrain.Drivetrain;

public class ArcadeDriveCommand extends Command {
private final Drivetrain drivetrain;

private final CommandXboxController xBoxController;

private final SlewRateLimiter leftRateLimiter = new SlewRateLimiter(5);
private final SlewRateLimiter rightRateLimiter = new SlewRateLimiter(5);

public ArcadeDriveCommand(Drivetrain drivetrain, CommandXboxController xBoxController) {
this.drivetrain = drivetrain;
this.xBoxController = xBoxController;
addRequirements(drivetrain);
}

public void execute() {
double throttle = MathUtil.applyDeadband(-xBoxController.getLeftY(), Constants.DEADZONE_RADIUS);
double turn = MathUtil.applyDeadband(xBoxController.getRightX(), Constants.DEADZONE_RADIUS);
double left = leftRateLimiter.calculate(
MathUtil.clamp(throttle + turn,-1, 1)
);
double right = rightRateLimiter.calculate(
MathUtil.clamp(throttle - turn, -1, 1)
);

drivetrain.drivePercent(left, right);
}

@Override
public void end(boolean interrupted) {
drivetrain.stop();
}

@Override
public boolean isFinished() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.team1540.advantagekitdemo.commands;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import org.team1540.advantagekitdemo.subsystems.elevator.Elevator;

public class ElevatorManualCommand extends Command {
private final Elevator elevator;
private final CommandXboxController controller;

public ElevatorManualCommand(Elevator elevator, CommandXboxController controller) {
this.elevator = elevator;
this.controller = controller;
addRequirements(elevator);
}

@Override
public void execute() {
elevator.setPercent(controller.getRightTriggerAxis() - controller.getLeftTriggerAxis());
}

@Override
public void end(boolean interrupted) {
elevator.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.team1540.advantagekitdemo.commands;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import org.team1540.advantagekitdemo.subsystems.intake.Intake;

public class WristManualCommand extends Command {
private final Intake intake;
private final CommandXboxController controller;

public WristManualCommand(Intake intake, CommandXboxController controller) {
this.intake = intake;
this.controller = controller;
addRequirements(intake);
}

@Override
public void execute() {
intake.setWristPercent(controller.getRightX());
}

@Override
public void end(boolean interrupted) {
intake.stopWrist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DrivetrainIOSim implements DrivetrainIO {
DCMotor.getFalcon500(2),
GEAR_RATIO,
ROBOT_MOI,
ROBOT_WEIGHT_KG,
ROBOT_MASS_KG,
WHEEL_DIAMETER_METERS / 2,
TRACK_WIDTH_METERS,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import edu.wpi.first.wpilibj.RobotState;
import edu.wpi.first.wpilibj2.command.ProfiledPIDSubsystem;
import org.littletonrobotics.junction.Logger;
import org.team1540.advantagekitdemo.util.SuperstructureVisualizer;

import static org.team1540.advantagekitdemo.Constants.ElevatorConstants.*;

Expand All @@ -25,6 +26,8 @@ public void periodic() {
super.periodic();
io.updateInputs(inputs);
Logger.processInputs("Elevator", inputs);

SuperstructureVisualizer.setElevatorPosition(getPositionMeters());
}

public void setPercent(double percentOutput) {
Expand All @@ -42,8 +45,7 @@ public void setPosition(double positionMeters) {
}

public void stop() {
if (RobotState.isDisabled()) setVoltage(0);
else setPosition(getPositionMeters());
setPercent(0);
}

public double getPositionMeters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.littletonrobotics.junction.AutoLog;

public interface ElevatorIO {


@AutoLog
class ElevatorIOInputs {
public double positionMeters = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.team1540.advantagekitdemo.subsystems.intake;

import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.wpilibj.RobotState;
import edu.wpi.first.wpilibj2.command.ProfiledPIDSubsystem;
import org.littletonrobotics.junction.Logger;
import org.team1540.advantagekitdemo.util.SuperstructureVisualizer;

import static org.team1540.advantagekitdemo.Constants.IntakeConstants.*;

public class Intake extends ProfiledPIDSubsystem {
private final IntakeIO io;
private final IntakeIOInputsAutoLogged inputs = new IntakeIOInputsAutoLogged();

private final ArmFeedforward wristFeedforward = new ArmFeedforward(WRIST_KS, WRIST_KG, WRIST_KV);

public Intake(IntakeIO io) {
super(new ProfiledPIDController(WRIST_KP, WRIST_KI, WRIST_KD, MOTION_CONSTRAINTS));
this.io = io;
}

@Override
public void periodic() {
super.periodic();
io.updateInputs(inputs);
Logger.processInputs("Intake", inputs);

SuperstructureVisualizer.setWristPosition(getWristPosition());
}

public void setIntakePercent(double percentOutput) {
io.setIntakeVoltage(12 * percentOutput);
}

public void setWristPercent(double percentOutput) {
disable();
setWristVoltage(12 * percentOutput);
}

public void setWristPosition(Rotation2d position) {
enable();
setGoal(position.getRotations());
}

private void setWristVoltage(double volts) {
io.setWristVoltage(volts);
}

public void stopIntake() {
setIntakePercent(0);
}

public void stopWrist() {
setWristPercent(0);
}

public Rotation2d getWristPosition() {
return inputs.wristPosition;
}

@Override
protected void useOutput(double output, TrapezoidProfile.State setpoint) {
setWristVoltage(output + wristFeedforward.calculate(getWristPosition().getRadians(), setpoint.velocity));
}

@Override
protected double getMeasurement() {
return inputs.wristPosition.getRotations();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.team1540.advantagekitdemo.subsystems.intake;

import edu.wpi.first.math.geometry.Rotation2d;
import org.littletonrobotics.junction.AutoLog;

public interface IntakeIO {
@AutoLog
class IntakeIOInputs {
public Rotation2d wristPosition = new Rotation2d();
public double wristAppliedVolts = 0;
public double wristCurrentAmps = 0;

public double intakeAppliedVolts = 0;
public double intakeCurrentAmps = 0;
}

default void updateInputs(IntakeIOInputs inputs) {}

default void setWristVoltage(double volts) {}

default void setIntakeVoltage(double volts) {}
}
Loading

0 comments on commit e9f2276

Please sign in to comment.