Files
YAGSL/swervelib/parser/SwerveDriveConfiguration.java

166 lines
4.7 KiB
Java
Raw Normal View History

2023-02-13 17:21:24 -06:00
package swervelib.parser;
2023-02-13 14:37:05 -06:00
import edu.wpi.first.math.geometry.Translation2d;
2024-12-09 23:26:04 +00:00
import edu.wpi.first.math.system.plant.DCMotor;
import java.util.function.Supplier;
2024-12-17 18:49:55 +00:00
import org.ironmaple.simulation.drivesims.COTS;
2024-12-09 23:26:04 +00:00
import org.ironmaple.simulation.drivesims.GyroSimulation;
2023-02-13 17:21:24 -06:00
import swervelib.SwerveModule;
2024-12-09 23:26:04 +00:00
import swervelib.imu.NavXSwerve;
import swervelib.imu.Pigeon2Swerve;
2023-02-13 17:21:24 -06:00
import swervelib.imu.SwerveIMU;
2024-12-09 23:26:04 +00:00
import swervelib.math.SwerveMath;
2023-02-13 14:37:05 -06:00
/**
* Swerve drive configurations used during SwerveDrive construction.
*/
2023-04-08 12:31:07 -05:00
public class SwerveDriveConfiguration
{
2023-02-13 14:37:05 -06:00
2024-08-24 17:27:03 -05:00
/**
* Number of modules on the robot.
*/
public final int moduleCount;
2023-04-08 12:31:07 -05:00
/**
* Swerve Module locations.
*/
2024-06-14 12:58:48 -05:00
public Translation2d[] moduleLocationsMeters;
2023-04-08 12:31:07 -05:00
/**
* Swerve IMU
*/
2024-06-14 12:58:48 -05:00
public SwerveIMU imu;
2023-04-08 12:31:07 -05:00
/**
* Swerve Modules.
*/
2024-06-14 12:58:48 -05:00
public SwerveModule[] modules;
2023-11-09 17:32:48 -06:00
/**
* Physical characteristics of the swerve drive from physicalproperties.json.
*/
2024-06-14 12:58:48 -05:00
public SwerveModulePhysicalCharacteristics physicalCharacteristics;
2023-02-13 14:37:05 -06:00
2023-04-08 12:31:07 -05:00
/**
* Create swerve drive configuration.
*
* @param moduleConfigs Module configuration.
* @param swerveIMU Swerve IMU.
* @param invertedIMU Invert the IMU.
* @param physicalCharacteristics {@link SwerveModulePhysicalCharacteristics} to store in association with self.
2023-04-08 12:31:07 -05:00
*/
public SwerveDriveConfiguration(
SwerveModuleConfiguration[] moduleConfigs,
SwerveIMU swerveIMU,
2023-11-09 17:32:48 -06:00
boolean invertedIMU,
SwerveModulePhysicalCharacteristics physicalCharacteristics)
2023-04-08 12:31:07 -05:00
{
this.moduleCount = moduleConfigs.length;
this.imu = swerveIMU;
2024-01-22 15:11:18 -06:00
swerveIMU.setInverted(invertedIMU);
2024-12-09 23:26:04 +00:00
this.modules = createModules(moduleConfigs);
2023-04-08 12:31:07 -05:00
this.moduleLocationsMeters = new Translation2d[moduleConfigs.length];
for (SwerveModule module : modules)
{
this.moduleLocationsMeters[module.moduleNumber] = module.configuration.moduleLocation;
2023-02-13 14:37:05 -06:00
}
2023-11-09 17:32:48 -06:00
this.physicalCharacteristics = physicalCharacteristics;
2023-04-08 12:31:07 -05:00
}
2023-02-13 14:37:05 -06:00
2023-04-08 12:31:07 -05:00
/**
* Create modules based off of the SwerveModuleConfiguration.
*
2024-12-09 23:26:04 +00:00
* @param swerves Swerve constants.
2023-04-08 12:31:07 -05:00
* @return Swerve Modules.
*/
2024-12-09 23:26:04 +00:00
public SwerveModule[] createModules(SwerveModuleConfiguration[] swerves)
2023-04-08 12:31:07 -05:00
{
SwerveModule[] modArr = new SwerveModule[swerves.length];
for (int i = 0; i < swerves.length; i++)
{
2024-12-09 23:26:04 +00:00
modArr[i] = new SwerveModule(i, swerves[i]);
2023-02-13 14:37:05 -06:00
}
2023-04-08 12:31:07 -05:00
return modArr;
}
2024-01-15 14:37:13 -06:00
/**
* Calculate the Drive Base Radius
2024-01-15 14:37:13 -06:00
*
* @return Drive base radius from center of robot to the farthest wheel in meters.
*/
public double getDriveBaseRadiusMeters()
{
Translation2d centerOfModules = moduleLocationsMeters[0];
2024-01-17 09:17:39 -06:00
//Calculate the Center by adding all module offsets together.
2024-01-22 15:11:18 -06:00
for (int i = 1; i < moduleLocationsMeters.length; i++)
{
centerOfModules = centerOfModules.plus(moduleLocationsMeters[i]);
2024-01-22 15:11:18 -06:00
}
2024-01-17 09:17:39 -06:00
//Return Largest Radius
return centerOfModules.getDistance(moduleLocationsMeters[0]);
2024-01-15 14:37:13 -06:00
}
2024-12-09 23:26:04 +00:00
/**
* Get the trackwidth of the swerve modules.
*
* @return Effective trackwdtih in Meters
*/
public double getTrackwidth()
{
SwerveModuleConfiguration fr = SwerveMath.getSwerveModule(modules, true, false);
SwerveModuleConfiguration fl = SwerveMath.getSwerveModule(modules, true, true);
return fr.moduleLocation.getDistance(fl.moduleLocation);
}
/**
* Get the tracklength of the swerve modules.
*
* @return Effective tracklength in Meters
*/
public double getTracklength()
{
SwerveModuleConfiguration br = SwerveMath.getSwerveModule(modules, false, false);
SwerveModuleConfiguration bl = SwerveMath.getSwerveModule(modules, false, true);
return br.moduleLocation.getDistance(bl.moduleLocation);
}
/**
* Get the {@link DCMotor} corresponding to the first module's configuration.
*
* @return {@link DCMotor} of the drive motor.
*/
public DCMotor getDriveMotorSim()
{
SwerveModuleConfiguration fl = SwerveMath.getSwerveModule(modules, true, true);
return fl.driveMotor.getSimMotor();
}
/**
* Get the {@link DCMotor} corresponding to the first module configuration.
*
* @return {@link DCMotor} of the angle motor.
*/
public DCMotor getAngleMotorSim()
{
SwerveModuleConfiguration fl = SwerveMath.getSwerveModule(modules, true, true);
return fl.angleMotor.getSimMotor();
}
/**
* Get the gyro simulation for the robot.
*
* @return {@link GyroSimulation} gyro simulation.
*/
public Supplier<GyroSimulation> getGyroSim()
{
if (imu instanceof Pigeon2Swerve)
{
2024-12-17 18:49:55 +00:00
return COTS.ofPigeon2();
2024-12-09 23:26:04 +00:00
} else if (imu instanceof NavXSwerve)
{
2024-12-17 18:49:55 +00:00
return COTS.ofNav2X();
2024-12-09 23:26:04 +00:00
}
2024-12-17 18:49:55 +00:00
return COTS.ofGenericGyro();
2024-12-09 23:26:04 +00:00
}
2023-02-13 14:37:05 -06:00
}