Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/com/team2813/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
* on a roboRIO.
*/
public final class Constants {
public static double SIM_TIME_PERIOD =
0.02; // Update physics simulations every 20ms (like the actual bot).

public static final Transform3d FRONT_CAMERA_POSITION =
new Transform3d(
new Translation3d(Meters.of(-0.054564), Meters.of(0), Meters.of(0.501754)),
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/team2813/subsystems/Simulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.team2813.subsystems;

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

import edu.wpi.first.units.measure.Voltage;
import edu.wpi.first.wpilibj.RobotController;
import org.littletonrobotics.junction.LoggedRobot;

/** Utilities for working in simulation. */
public class Simulation {
/**
* The time period to use in simulation.
*
* <p>Currently set to the same time period as we use in the actual robot.
*/
public static final double TIME_PERIOD = LoggedRobot.defaultPeriodSecs;

/** Gets the voltage to apply to simulated motors. */
public static Voltage getMotorSupplyVoltage() {
return Volts.of(RobotController.getBatteryVoltage());
}

private Simulation() {
throw new AssertionError("Not instantiable");
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/team2813/subsystems/drive/ModuleIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

package com.team2813.subsystems.drive;

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

import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.swerve.SwerveModuleConstants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
Expand Down Expand Up @@ -83,8 +86,9 @@ public void updateInputs(ModuleIOInputs inputs) {
}

// Update simulation state
driveSim.setInputVoltage(MathUtil.clamp(driveAppliedVolts, -12.0, 12.0));
turnSim.setInputVoltage(MathUtil.clamp(turnAppliedVolts, -12.0, 12.0));
double supplyVoltage = Simulation.getMotorSupplyVoltage().in(Volts);
driveSim.setInputVoltage(MathUtil.clamp(driveAppliedVolts, -supplyVoltage, supplyVoltage));
turnSim.setInputVoltage(MathUtil.clamp(turnAppliedVolts, -supplyVoltage, supplyVoltage));
driveSim.update(0.02);
turnSim.update(0.02);

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/team2813/subsystems/hopper/HopperIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.sim.TalonFXSimState;
import com.team2813.Constants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.units.measure.Voltage;
Expand Down Expand Up @@ -56,9 +57,10 @@ public HopperIOSim() {
public void updateState(HopperIOInputs inputs) {
updateSimulation();

mainRollerMotorSimState.setSupplyVoltage(Volts.of(12));
followerRollerMotorSimState.setSupplyVoltage(Volts.of(12));
feederMotorSimState.setSupplyVoltage(Volts.of(12));
Voltage supplyVoltage = Simulation.getMotorSupplyVoltage();
mainRollerMotorSimState.setSupplyVoltage(supplyVoltage);
followerRollerMotorSimState.setSupplyVoltage(supplyVoltage);
feederMotorSimState.setSupplyVoltage(supplyVoltage);

inputs.mainRollerMotorVoltage = mainRollerMotor.getMotorVoltage().getValue();
inputs.mainRollerMotorRPS = mainRollerMotor.getRotorVelocity().getValue();
Expand All @@ -73,8 +75,8 @@ public void updateState(HopperIOInputs inputs) {
inputs.feederCurrent = feederMotor.getStatorCurrent().getValue();
}

public void updateSimulation() {
rollerSim.update(Constants.SIM_TIME_PERIOD);
private void updateSimulation() {
rollerSim.update(Simulation.TIME_PERIOD);

// Feed the velocity and acceleration of the roller simulation into the simulation motors to
// accurately model them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.sim.TalonFXSimState;
import com.team2813.Constants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.system.plant.DCMotor;
Expand Down Expand Up @@ -59,7 +60,7 @@ public IntakeExtensionIOSim() {
@Override
public void updateState(IntakeExtensionIOInputs inputs) {
// Continue supplying the simulated motor with 12V voltage.
extenderMotorSimState.setSupplyVoltage(12);
extenderMotorSimState.setSupplyVoltage(Simulation.getMotorSupplyVoltage());

// TalonFX simulation doesn't run the internal PID controller, so we simulate it ourselves
// using a WPILib PIDController with the same gains.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.sim.TalonFXSimState;
import com.team2813.Constants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.units.measure.Voltage;
Expand Down Expand Up @@ -35,14 +36,14 @@ public IntakeRollerIOSim() {
public void updateState(IntakeRollerIOInputs inputs) {
updateSimulation();

intakeSimState.setSupplyVoltage(Volts.of(12));
intakeSimState.setSupplyVoltage(Simulation.getMotorSupplyVoltage());

inputs.intakeMotorVoltage = intakeMotor.getMotorVoltage().getValue();
inputs.intakeMotorRPS = intakeMotor.getVelocity().getValue();
inputs.intakeMotorCurrent = intakeMotor.getStatorCurrent().getValue();
}

public void updateSimulation() {
private void updateSimulation() {
// TODO: Once we fetch from main, change this to the SIM_TIME in Constants.
intakeFlywheelSim.update(0.02);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/team2813/subsystems/kicker/KickerIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.sim.TalonFXSimState;
import com.team2813.Constants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.units.measure.Voltage;
Expand Down Expand Up @@ -44,12 +45,12 @@ public void updateState(KickerIOInputs inputs) {
}

private void updateSimulation() {
flywheelSim.update(Constants.SIM_TIME_PERIOD);
flywheelSim.update(Simulation.TIME_PERIOD);

TalonFXSimState simState = motor.getSimState();
simState.setRotorAcceleration(flywheelSim.getAngularAcceleration());
simState.setRotorVelocity(flywheelSim.getAngularVelocity());
simState.setSupplyVoltage(Volts.of(12));
simState.setSupplyVoltage(Simulation.getMotorSupplyVoltage());
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/team2813/subsystems/shooter/ShooterIOSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.sim.TalonFXSimState;
import com.team2813.Constants;
import com.team2813.subsystems.Simulation;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.units.measure.AngularVelocity;
Expand Down Expand Up @@ -49,8 +50,9 @@ public ShooterIOSim() {
public void updateState(ShooterIOInputs inputs) {
updateSimulation();

mainShooterSimState.setSupplyVoltage(Volts.of(12));
followerShooterSimState.setSupplyVoltage(Volts.of(12));
Voltage supplyVoltage = Simulation.getMotorSupplyVoltage();
mainShooterSimState.setSupplyVoltage(supplyVoltage);
followerShooterSimState.setSupplyVoltage(supplyVoltage);

inputs.mainShooterMotorVoltageVolts = mainShooterMotor.getMotorVoltage().getValue().in(Volts);
inputs.mainShooterMotorAngleRotations = mainShooterMotor.getPosition().getValue().in(Rotations);
Expand All @@ -67,9 +69,9 @@ public void updateState(ShooterIOInputs inputs) {
followerShooterMotor.getStatorCurrent().getValue().in(Amps);
}

public void updateSimulation() {
private void updateSimulation() {
// Update physics simulations every 20ms (like the actual bot).
shooterSim.update(Constants.SIM_TIME_PERIOD);
shooterSim.update(Simulation.TIME_PERIOD);

// Feed the velocity and acceleration of the roller simulation into the simulation motors to
// accurately model them.
Expand Down
Loading