Files
YAGSL/swervelib/parser/json/ModuleJson.java

134 lines
5.3 KiB
Java
Raw Normal View History

2023-02-13 17:21:24 -06:00
package swervelib.parser.json;
2023-02-13 14:37:05 -06:00
import edu.wpi.first.math.util.Units;
2023-02-13 17:21:24 -06:00
import swervelib.encoders.SwerveAbsoluteEncoder;
import swervelib.motors.SwerveMotor;
import swervelib.parser.PIDFConfig;
import swervelib.parser.SwerveModuleConfiguration;
import swervelib.parser.SwerveModulePhysicalCharacteristics;
import swervelib.parser.json.modules.BoolMotorJson;
import swervelib.parser.json.modules.LocationJson;
2023-02-13 14:37:05 -06:00
/**
2023-02-13 17:21:24 -06:00
* {@link swervelib.SwerveModule} JSON parsed class. Used to access the JSON data.
2023-02-13 14:37:05 -06:00
*/
public class ModuleJson
{
/**
* Drive motor device configuration.
*/
2023-11-09 17:32:48 -06:00
public DeviceJson drive;
2023-02-13 14:37:05 -06:00
/**
* Angle motor device configuration.
*/
2023-11-09 17:32:48 -06:00
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;
2023-02-13 14:37:05 -06:00
/**
* Absolute encoder device configuration.
*/
2023-11-09 17:32:48 -06:00
public DeviceJson encoder;
2023-02-13 14:37:05 -06:00
/**
* Defines which motors are inverted.
*/
2023-11-09 17:32:48 -06:00
public BoolMotorJson inverted;
2023-02-13 14:37:05 -06:00
/**
* Absolute encoder offset from 0 in degrees.
*/
2023-11-09 17:32:48 -06:00
public double absoluteEncoderOffset;
2023-02-13 14:37:05 -06:00
/**
* Absolute encoder inversion state.
*/
2023-11-09 17:32:48 -06:00
public boolean absoluteEncoderInverted = false;
2023-02-13 14:37:05 -06:00
/**
* The location of the swerve module from the center of the robot in inches.
*/
2023-11-09 17:32:48 -06:00
public LocationJson location;
2023-02-13 14:37:05 -06:00
/**
* 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 physicalCharacteristics Physical characteristics of the swerve module.
2023-04-08 12:31:07 -05:00
* @param name Module json filename.
2023-02-13 14:37:05 -06:00
* @return {@link SwerveModuleConfiguration} based on the provided data and parsed data.
*/
public SwerveModuleConfiguration createModuleConfiguration(
PIDFConfig anglePIDF,
PIDFConfig velocityPIDF,
2023-04-08 12:31:07 -05:00
SwerveModulePhysicalCharacteristics physicalCharacteristics,
String name)
2023-02-13 14:37:05 -06:00
{
SwerveMotor angleMotor = angle.createMotor(false);
SwerveAbsoluteEncoder absEncoder = encoder.createEncoder(angleMotor);
2023-02-13 14:37:05 -06:00
2023-08-09 15:05:33 -05:00
// If the absolute encoder is attached.
if (absEncoder == null)
{
absEncoder = angle.createIntegratedEncoder(angleMotor);
angleMotor.setAbsoluteEncoder(absEncoder);
}
2023-11-09 17:32:48 -06:00
// 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,
2023-11-09 17:32:48 -06:00
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,
physicalCharacteristics,
absoluteEncoderInverted,
inverted.drive,
inverted.angle,
2023-04-08 12:31:07 -05:00
name.replaceAll("\\.json", ""));
2023-02-13 14:37:05 -06:00
}
}