Skip to content

Commit

Permalink
Add SysId to example projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Jan 20, 2024
1 parent 6e35ca8 commit c8e2d83
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 1,302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.commands.DriveCommands;
import frc.robot.commands.FeedForwardCharacterization;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.GyroIO;
import frc.robot.subsystems.drive.GyroIOPigeon2;
Expand Down Expand Up @@ -111,15 +111,27 @@ public RobotContainer() {
.withTimeout(5.0));
autoChooser = new LoggedDashboardChooser<>("Auto Choices", AutoBuilder.buildAutoChooser());

// Set up feedforward characterization
// Set up SysId routines
autoChooser.addOption(
"Drive FF Characterization",
new FeedForwardCharacterization(
drive, drive::runCharacterizationVolts, drive::getCharacterizationVelocity));
"Drive SysId (Quasistatic Forward)",
drive.sysIdQuasistatic(SysIdRoutine.Direction.kForward));
autoChooser.addOption(
"Flywheel FF Characterization",
new FeedForwardCharacterization(
flywheel, flywheel::runVolts, flywheel::getCharacterizationVelocity));
"Drive SysId (Quasistatic Reverse)",
drive.sysIdQuasistatic(SysIdRoutine.Direction.kReverse));
autoChooser.addOption(
"Drive SysId (Dynamic Forward)", drive.sysIdDynamic(SysIdRoutine.Direction.kForward));
autoChooser.addOption(
"Drive SysId (Dynamic Reverse)", drive.sysIdDynamic(SysIdRoutine.Direction.kReverse));
autoChooser.addOption(
"Flywheel SysId (Quasistatic Forward)",
flywheel.sysIdQuasistatic(SysIdRoutine.Direction.kForward));
autoChooser.addOption(
"Flywheel SysId (Quasistatic Reverse)",
flywheel.sysIdQuasistatic(SysIdRoutine.Direction.kReverse));
autoChooser.addOption(
"Flywheel SysId (Dynamic Forward)", flywheel.sysIdDynamic(SysIdRoutine.Direction.kForward));
autoChooser.addOption(
"Flywheel SysId (Dynamic Reverse)", flywheel.sysIdDynamic(SysIdRoutine.Direction.kReverse));

// Configure the button bindings
configureButtonBindings();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package frc.robot.subsystems.drive;

import static edu.wpi.first.units.Units.*;

import com.pathplanner.lib.auto.AutoBuilder;
import com.pathplanner.lib.pathfinding.Pathfinding;
import com.pathplanner.lib.util.HolonomicPathFollowerConfig;
Expand All @@ -30,7 +32,9 @@
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.util.LocalADStarAK;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -49,6 +53,7 @@ public class Drive extends SubsystemBase {
private final GyroIO gyroIO;
private final GyroIOInputsAutoLogged gyroInputs = new GyroIOInputsAutoLogged();
private final Module[] modules = new Module[4]; // FL, FR, BL, BR
private final SysIdRoutine sysId;

private SwerveDriveKinematics kinematics = new SwerveDriveKinematics(getModuleTranslations());
private Rotation2d rawGyroRotation = new Rotation2d();
Expand Down Expand Up @@ -100,6 +105,23 @@ MAX_LINEAR_SPEED, DRIVE_BASE_RADIUS, new ReplanningConfig()),
(targetPose) -> {
Logger.recordOutput("Odometry/TrajectorySetpoint", targetPose);
});

// Configure SysId
sysId =
new SysIdRoutine(
new SysIdRoutine.Config(
null,
null,
null,
(state) -> Logger.recordOutput("Drive/SysIdState", state.toString())),
new SysIdRoutine.Mechanism(
(voltage) -> {
for (int i = 0; i < 4; i++) {
modules[i].runCharacterization(voltage.in(Volts));
}
},
null,
this));
}

public void periodic() {
Expand Down Expand Up @@ -200,20 +222,14 @@ public void stopWithX() {
stop();
}

/** Runs forwards at the commanded voltage. */
public void runCharacterizationVolts(double volts) {
for (int i = 0; i < 4; i++) {
modules[i].runCharacterization(volts);
}
/** Returns a command to run a quasistatic test in the specified direction. */
public Command sysIdQuasistatic(SysIdRoutine.Direction direction) {
return sysId.quasistatic(direction);
}

/** Returns the average drive velocity in radians/sec. */
public double getCharacterizationVelocity() {
double driveVelocityAverage = 0.0;
for (var module : modules) {
driveVelocityAverage += module.getCharacterizationVelocity();
}
return driveVelocityAverage / 4.0;
/** Returns a command to run a dynamic test in the specified direction. */
public Command sysIdDynamic(SysIdRoutine.Direction direction) {
return sysId.dynamic(direction);
}

/** Returns the module states (turn angles and drive velocities) for all of the modules. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

package frc.robot.subsystems.flywheel;

import static edu.wpi.first.units.Units.*;

import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.Constants;
import org.littletonrobotics.junction.AutoLogOutput;
import org.littletonrobotics.junction.Logger;
Expand All @@ -24,6 +28,7 @@ public class Flywheel extends SubsystemBase {
private final FlywheelIO io;
private final FlywheelIOInputsAutoLogged inputs = new FlywheelIOInputsAutoLogged();
private final SimpleMotorFeedforward ffModel;
private final SysIdRoutine sysId;

/** Creates a new Flywheel. */
public Flywheel(FlywheelIO io) {
Expand All @@ -45,6 +50,16 @@ public Flywheel(FlywheelIO io) {
ffModel = new SimpleMotorFeedforward(0.0, 0.0);
break;
}

// Configure SysId
sysId =
new SysIdRoutine(
new SysIdRoutine.Config(
null,
null,
null,
(state) -> Logger.recordOutput("Flywheel/SysIdState", state.toString())),
new SysIdRoutine.Mechanism((voltage) -> runVolts(voltage.in(Volts)), null, this));
}

@Override
Expand Down Expand Up @@ -72,6 +87,16 @@ public void stop() {
io.stop();
}

/** Returns a command to run a quasistatic test in the specified direction. */
public Command sysIdQuasistatic(SysIdRoutine.Direction direction) {
return sysId.quasistatic(direction);
}

/** Returns a command to run a dynamic test in the specified direction. */
public Command sysIdDynamic(SysIdRoutine.Direction direction) {
return sysId.dynamic(direction);
}

/** Returns the current velocity in RPM. */
@AutoLogOutput
public double getVelocityRPM() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void updateInputs(FlywheelIOInputs inputs) {
@Override
public void setVoltage(double volts) {
closedLoop = false;
appliedVolts = 0.0;
appliedVolts = volts;
sim.setInputVoltage(volts);
}

Expand Down
Loading

0 comments on commit c8e2d83

Please sign in to comment.