2023-02-15 22:18:27 -06:00
|
|
|
package swervelib.simulation;
|
|
|
|
|
|
2024-12-09 23:26:04 +00:00
|
|
|
import static edu.wpi.first.units.Units.Amps;
|
|
|
|
|
|
2023-02-15 22:18:27 -06:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.math.kinematics.SwerveModulePosition;
|
2023-08-29 21:03:58 -05:00
|
|
|
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;
|
2023-02-15 22:18:27 -06:00
|
|
|
|
|
|
|
|
/**
|
2024-12-09 23:26:04 +00:00
|
|
|
* Class that wraps around {@link org.ironmaple.simulation.drivesims.SwerveModuleSimulation}
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
|
|
|
|
public class SwerveModuleSimulation
|
|
|
|
|
{
|
|
|
|
|
|
2024-12-09 23:26:04 +00:00
|
|
|
private SelfControlledSwerveDriveSimulation.SelfControlledModuleSimulation mapleSimModule = null;
|
2023-02-22 20:50:16 -06:00
|
|
|
|
2023-02-15 22:18:27 -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
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
2024-12-09 23:26:04 +00:00
|
|
|
public void configureSimModule(org.ironmaple.simulation.drivesims.SwerveModuleSimulation simModule,
|
|
|
|
|
SwerveModulePhysicalCharacteristics physicalCharacteristics)
|
2023-02-15 22:18:27 -06:00
|
|
|
{
|
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));
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2023-08-29 21:03:58 -05:00
|
|
|
public void updateStateAndPosition(SwerveModuleState desiredState)
|
2023-02-15 22:18:27 -06:00
|
|
|
{
|
2024-12-09 23:26:04 +00:00
|
|
|
mapleSimModule.runModuleState(desiredState);
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
2023-02-15 22:18:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-29 21:03:58 -05:00
|
|
|
* Get the {@link SwerveModuleState} of the simulated module.
|
2023-02-15 22:18:27 -06:00
|
|
|
*
|
2023-08-29 21:03:58 -05:00
|
|
|
* @return {@link SwerveModuleState} of the simulated module.
|
2023-02-15 22:18:27 -06:00
|
|
|
*/
|
2023-08-29 21:03:58 -05:00
|
|
|
public SwerveModuleState getState()
|
2023-02-15 22:18:27 -06:00
|
|
|
{
|
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);
|
2023-02-15 22:18:27 -06:00
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|