Files
YAGSL/swervelib/simulation/SwerveModuleSimulation.java

101 lines
3.3 KiB
Java
Raw Normal View History

package swervelib.simulation;
2024-12-09 23:26:04 +00:00
import static edu.wpi.first.units.Units.Amps;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
2024-12-09 23:26:04 +00:00
import org.ironmaple.simulation.drivesims.SelfControlledSwerveDriveSimulation;
2025-02-22 06:15:56 +00:00
import swervelib.SwerveDrive;
2024-12-09 23:26:04 +00:00
import swervelib.parser.SwerveModulePhysicalCharacteristics;
/**
2024-12-09 23:26:04 +00:00
* Class that wraps around {@link org.ironmaple.simulation.drivesims.SwerveModuleSimulation}
*/
public class SwerveModuleSimulation
{
2025-02-22 06:15:56 +00:00
/**
* MapleSim module.
*/
public SelfControlledSwerveDriveSimulation.SelfControlledModuleSimulation mapleSimModule = null;
2023-02-22 20:50:16 -06:00
/**
2024-12-09 23:26:04 +00:00
* Configure the maple sim module
*
2025-02-22 06:15:56 +00:00
* @param simModule the {@link org.ironmaple.simulation.drivesims.SwerveModuleSimulation} object for
* simulation
2025-01-06 15:44:15 +00:00
* @param physicalCharacteristics Physical characteristics of the swerve drive from the JSON or built.
*/
2024-12-09 23:26:04 +00:00
public void configureSimModule(org.ironmaple.simulation.drivesims.SwerveModuleSimulation simModule,
SwerveModulePhysicalCharacteristics physicalCharacteristics)
{
2024-12-09 23:26:04 +00:00
this.mapleSimModule = new SelfControlledSwerveDriveSimulation.SelfControlledModuleSimulation(simModule);
this.mapleSimModule.withCurrentLimits(
Amps.of(physicalCharacteristics.driveMotorCurrentLimit),
Amps.of(physicalCharacteristics.angleMotorCurrentLimit));
}
/**
* Update the position and state of the module. Called from {@link swervelib.SwerveModule#setDesiredState} function
* when simulated.
*
* @param desiredState State the swerve module is set to.
*/
public void updateStateAndPosition(SwerveModuleState desiredState)
{
2024-12-09 23:26:04 +00:00
mapleSimModule.runModuleState(desiredState);
}
2025-02-22 06:15:56 +00:00
/**
* Runs a drive motor characterization on the sim module. This is called from
* {@link swervelib.SwerveDriveTest#runDriveMotorsCharacterizationOnSimModules(SwerveDrive, double, boolean)} to run
* sysId during simulation
*
* @param desiredFacing the desired facing of the module
* @param volts the voltage to run
*/
public void runDriveMotorCharacterization(Rotation2d desiredFacing, double volts)
{
mapleSimModule.runDriveMotorCharacterization(desiredFacing, volts);
}
/**
* Runs a drive motor characterization on the sim module. This method is called from
* {@link swervelib.SwerveDriveTest#runAngleMotorsCharacterizationOnSimModules(SwerveDrive, double)} to run sysId
* during simulation
*
* @param volts the voltage to run
*/
public void runAngleMotorCharacterization(double volts)
{
mapleSimModule.runSteerMotorCharacterization(volts);
}
/**
* Get the simulated swerve module position.
*
* @return {@link SwerveModulePosition} of the simulated module.
*/
public SwerveModulePosition getPosition()
{
2024-12-09 23:26:04 +00:00
return mapleSimModule.getModulePosition();
}
/**
* Get the {@link SwerveModuleState} of the simulated module.
*
* @return {@link SwerveModuleState} of the simulated module.
*/
public SwerveModuleState getState()
{
2024-12-09 23:26:04 +00:00
if (mapleSimModule == null)
{
return new SwerveModuleState();
}
SwerveModuleState state = mapleSimModule.getMeasuredState();
state.angle = state.angle.minus(Rotation2d.kZero);
return state;
}
}