Files
YAGSL/swervelib/simulation/SwerveModuleSimulation.java

71 lines
2.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;
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
{
2024-12-09 23:26:04 +00:00
private 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
*
* @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);
}
/**
* 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;
}
}