2023-02-13 17:21:24 -06:00
|
|
|
package swervelib.parser;
|
2023-02-13 14:37:05 -06:00
|
|
|
|
2023-11-09 17:32:48 -06:00
|
|
|
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
|
2023-02-13 14:37:05 -06:00
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
2023-02-13 17:21:24 -06:00
|
|
|
import swervelib.SwerveModule;
|
|
|
|
|
import swervelib.imu.SwerveIMU;
|
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
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Number of modules on the robot.
|
|
|
|
|
*/
|
2024-06-14 12:58:48 -05:00
|
|
|
public final int moduleCount;
|
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.
|
|
|
|
|
*
|
2023-12-05 16:25:42 -06:00
|
|
|
* @param moduleConfigs Module configuration.
|
|
|
|
|
* @param swerveIMU Swerve IMU.
|
|
|
|
|
* @param invertedIMU Invert the IMU.
|
|
|
|
|
* @param driveFeedforward The drive motor feedforward to use for the {@link SwerveModule}.
|
|
|
|
|
* @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,
|
|
|
|
|
SimpleMotorFeedforward driveFeedforward,
|
|
|
|
|
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);
|
2023-11-09 17:32:48 -06:00
|
|
|
this.modules = createModules(moduleConfigs, driveFeedforward);
|
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.
|
|
|
|
|
*
|
2023-11-09 17:32:48 -06:00
|
|
|
* @param swerves Swerve constants.
|
|
|
|
|
* @param driveFeedforward Drive feedforward created using
|
|
|
|
|
* {@link swervelib.math.SwerveMath#createDriveFeedforward(double, double, double)}.
|
2023-04-08 12:31:07 -05:00
|
|
|
* @return Swerve Modules.
|
|
|
|
|
*/
|
2023-11-09 17:32:48 -06:00
|
|
|
public SwerveModule[] createModules(SwerveModuleConfiguration[] swerves, SimpleMotorFeedforward driveFeedforward)
|
2023-04-08 12:31:07 -05:00
|
|
|
{
|
|
|
|
|
SwerveModule[] modArr = new SwerveModule[swerves.length];
|
|
|
|
|
for (int i = 0; i < swerves.length; i++)
|
|
|
|
|
{
|
2023-11-09 17:32:48 -06:00
|
|
|
modArr[i] = new SwerveModule(i, swerves[i], driveFeedforward);
|
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
|
|
|
|
|
|
|
|
/**
|
2024-01-18 11:03: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()
|
|
|
|
|
{
|
2024-01-18 11:03:13 -06:00
|
|
|
Translation2d centerOfModules = moduleLocationsMeters[0];
|
2024-01-17 09:17:39 -06:00
|
|
|
|
2024-01-18 11:03:13 -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++)
|
|
|
|
|
{
|
2024-01-18 11:03:13 -06:00
|
|
|
centerOfModules = centerOfModules.plus(moduleLocationsMeters[i]);
|
2024-01-22 15:11:18 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-17 09:17:39 -06:00
|
|
|
//Return Largest Radius
|
2024-01-18 11:03:13 -06:00
|
|
|
return centerOfModules.getDistance(moduleLocationsMeters[0]);
|
2024-01-15 14:37:13 -06:00
|
|
|
}
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|