Updated YAGSL to next-gen

This commit is contained in:
thenetworkgrinch
2023-11-09 17:32:48 -06:00
parent 0b02b3c504
commit 6aaf512b38
21 changed files with 573 additions and 517 deletions

View File

@@ -18,42 +18,45 @@ public class ModuleJson
/**
* Drive motor device configuration.
*/
public DeviceJson drive;
public DeviceJson drive;
/**
* Angle motor device configuration.
*/
public DeviceJson angle;
public DeviceJson angle;
/**
* Conversion factor for the module, if different from the one in swervedrive.json
* <p>
* Conversion factor applied to the motor controllers PID loops. Can be calculated with
* {@link swervelib.math.SwerveMath#calculateDegreesPerSteeringRotation(double, double)} for angle motors or
* {@link swervelib.math.SwerveMath#calculateMetersPerRotation(double, double, double)} for drive motors.
*/
public MotorConfigDouble conversionFactor;
/**
* Absolute encoder device configuration.
*/
public DeviceJson encoder;
public DeviceJson encoder;
/**
* Defines which motors are inverted.
*/
public BoolMotorJson inverted;
public BoolMotorJson inverted;
/**
* Absolute encoder offset from 0 in degrees.
*/
public double absoluteEncoderOffset;
public double absoluteEncoderOffset;
/**
* Absolute encoder inversion state.
*/
public boolean absoluteEncoderInverted = false;
/**
* The angle encoder pulse per revolution override. 1 for Neo encoder. 2048 for Falcons.
*/
public double angleEncoderPulsePerRevolution = 0;
public boolean absoluteEncoderInverted = false;
/**
* The location of the swerve module from the center of the robot in inches.
*/
public LocationJson location;
public LocationJson location;
/**
* Create the swerve module configuration based off of parsed data.
*
* @param anglePIDF The PIDF values for the angle motor.
* @param velocityPIDF The velocity PIDF values for the drive motor.
* @param maxSpeed The maximum speed of the robot in meters per second.
* @param physicalCharacteristics Physical characteristics of the swerve module.
* @param name Module json filename.
* @return {@link SwerveModuleConfiguration} based on the provided data and parsed data.
@@ -61,21 +64,12 @@ public class ModuleJson
public SwerveModuleConfiguration createModuleConfiguration(
PIDFConfig anglePIDF,
PIDFConfig velocityPIDF,
double maxSpeed,
SwerveModulePhysicalCharacteristics physicalCharacteristics,
String name)
{
SwerveMotor angleMotor = angle.createMotor(false);
SwerveAbsoluteEncoder absEncoder = encoder.createEncoder(angleMotor);
// Override angle encoder pulse per rotation based on the encoder and motor type.
int encoderPulseOverride = encoder.getPulsePerRotation(physicalCharacteristics.angleEncoderPulsePerRotation);
int motorPulseOverride = angle.getPulsePerRotation(physicalCharacteristics.angleEncoderPulsePerRotation);
int angleEncoderPulsePerRotation =
motorPulseOverride != physicalCharacteristics.angleEncoderPulsePerRotation ? motorPulseOverride
: encoderPulseOverride;
// If the absolute encoder is attached.
if (absEncoder == null)
{
@@ -83,22 +77,57 @@ public class ModuleJson
angleMotor.setAbsoluteEncoder(absEncoder);
}
// Set the conversion factors to null if they are both 0.
if (this.conversionFactor != null)
{
if (this.conversionFactor.angle == 0 && this.conversionFactor.drive == 0)
{
this.conversionFactor = null;
}
}
if (this.conversionFactor == null && physicalCharacteristics.conversionFactor == null)
{
throw new RuntimeException("No Conversion Factor configured! Please create SwerveDrive using \n" +
"SwerveParser.createSwerveDrive(driveFeedforward, maxSpeed, angleMotorConversionFactor, driveMotorConversion)\n" +
"OR\n" +
"SwerveParser.createSwerveDrive(maxSpeed, angleMotorConversionFactor, driveMotorConversion)\n" +
"OR\n" +
"Set the conversion factor in physicalproperties.json OR the module JSON file." +
"REMEMBER: You can calculate the conversion factors using SwerveMath.calculateMetersPerRotation AND SwerveMath.calculateDegreesPerSteeringRotation\n");
} else if (physicalCharacteristics.conversionFactor != null && this.conversionFactor == null)
{
this.conversionFactor = physicalCharacteristics.conversionFactor;
} else if (physicalCharacteristics.conversionFactor !=
null) // If both are defined, override 0 with the physical characterstics input.
{
this.conversionFactor.angle = this.conversionFactor.angle == 0 ? physicalCharacteristics.conversionFactor.angle
: this.conversionFactor.angle;
this.conversionFactor.drive = this.conversionFactor.drive == 0 ? physicalCharacteristics.conversionFactor.drive
: this.conversionFactor.drive;
}
if (this.conversionFactor.drive == 0 || this.conversionFactor.angle == 0)
{
throw new RuntimeException(
"Conversion factors cannot be 0, please configure conversion factors in physicalproperties.json or the module JSON files.");
}
System.out.println(conversionFactor.drive);
return new SwerveModuleConfiguration(
drive.createMotor(true),
angleMotor,
conversionFactor,
absEncoder,
absoluteEncoderOffset,
Units.inchesToMeters(Math.round(location.x) == 0 ? location.front : location.x),
Units.inchesToMeters(Math.round(location.y) == 0 ? location.left : location.y),
anglePIDF,
velocityPIDF,
maxSpeed,
physicalCharacteristics,
absoluteEncoderInverted,
inverted.drive,
inverted.angle,
angleEncoderPulsePerRevolution == 0 ? angleEncoderPulsePerRotation
: angleEncoderPulsePerRevolution,
name.replaceAll("\\.json", ""));
}
}