2023-02-13 17:21:24 -06:00
package swervelib.parser.json ;
2023-02-13 14:37:05 -06:00
2024-12-09 23:26:04 +00:00
import com.revrobotics.spark.SparkMax ;
2023-02-13 14:37:05 -06:00
import edu.wpi.first.math.util.Units ;
2024-12-09 23:26:04 +00:00
import swervelib.encoders.SparkMaxEncoderSwerve ;
2023-02-13 17:21:24 -06:00
import swervelib.encoders.SwerveAbsoluteEncoder ;
2025-01-06 15:44:15 +00:00
import swervelib.encoders.ThriftyNovaEncoderSwerve ;
2023-02-13 17:21:24 -06:00
import swervelib.motors.SwerveMotor ;
2025-01-06 15:44:15 +00:00
import swervelib.motors.ThriftyNovaSwerve ;
2023-02-13 17:21:24 -06:00
import swervelib.parser.PIDFConfig ;
import swervelib.parser.SwerveModuleConfiguration ;
import swervelib.parser.SwerveModulePhysicalCharacteristics ;
import swervelib.parser.json.modules.BoolMotorJson ;
2024-07-29 15:14:25 -05:00
import swervelib.parser.json.modules.ConversionFactorsJson ;
2023-02-13 17:21:24 -06:00
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 .
* /
2024-07-29 15:14:25 -05:00
public DeviceJson drive ;
2023-02-13 14:37:05 -06:00
/ * *
* Angle motor device configuration .
* /
2024-07-29 15:14:25 -05:00
public DeviceJson angle ;
/ * *
* Conversion Factors composition . Auto - calculates the conversion factors .
* /
public ConversionFactorsJson conversionFactors = new ConversionFactorsJson ( ) ;
2023-02-13 14:37:05 -06:00
/ * *
* Absolute encoder device configuration .
* /
2024-07-29 15:14:25 -05:00
public DeviceJson encoder ;
2023-02-13 14:37:05 -06:00
/ * *
* Defines which motors are inverted .
* /
2024-07-29 15:14:25 -05:00
public BoolMotorJson inverted ;
2023-02-13 14:37:05 -06:00
/ * *
* Absolute encoder offset from 0 in degrees .
* /
2024-07-29 15:14:25 -05:00
public double absoluteEncoderOffset ;
2023-02-13 14:37:05 -06:00
/ * *
* Absolute encoder inversion state .
* /
2024-07-29 15:14:25 -05: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 .
* /
2024-07-29 15:14:25 -05:00
public LocationJson location ;
2024-01-26 11:29:15 -06:00
/ * *
* Should do cosine compensation when not pointing correct direction ; .
* /
2024-07-29 15:14:25 -05:00
public boolean useCosineCompensator = true ;
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 .
* /
2023-02-20 20:59:31 -06:00
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 ) ;
2023-08-09 13:15:02 -05:00
SwerveAbsoluteEncoder absEncoder = encoder . createEncoder ( angleMotor ) ;
2023-02-13 14:37:05 -06:00
2023-11-09 17:32:48 -06:00
// Set the conversion factors to null if they are both 0.
2024-12-09 23:26:04 +00:00
if ( ! conversionFactors . works ( ) & & physicalCharacteristics . conversionFactor = = null )
2023-11-09 17:32:48 -06:00
{
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 " ) ;
2024-12-09 23:26:04 +00:00
} else if ( physicalCharacteristics . conversionFactor . works ( ) & & ! conversionFactors . works ( ) )
2023-11-09 17:32:48 -06:00
{
2024-12-09 23:26:04 +00:00
conversionFactors = physicalCharacteristics . conversionFactor ;
} else if ( physicalCharacteristics . conversionFactor . works ( ) )
// If both are defined, override 0 with the physical characterstics input.
2023-11-09 17:32:48 -06:00
{
2024-12-09 23:26:04 +00:00
conversionFactors . angle = conversionFactors . isAngleEmpty ( ) ? physicalCharacteristics . conversionFactor . angle
: conversionFactors . angle ;
conversionFactors . drive = conversionFactors . isDriveEmpty ( ) ? physicalCharacteristics . conversionFactor . drive
: conversionFactors . drive ;
2023-11-09 17:32:48 -06:00
}
2024-12-09 23:26:04 +00:00
if ( conversionFactors . isDriveEmpty ( ) | | conversionFactors . isAngleEmpty ( ) )
2023-11-09 17:32:48 -06:00
{
throw new RuntimeException (
" Conversion factors cannot be 0, please configure conversion factors in physicalproperties.json or the module JSON files. " ) ;
}
2024-09-03 14:46:24 -05:00
// Backwards compatibility, auto-optimization.
2024-12-09 23:26:04 +00:00
if ( conversionFactors . angle . factor = = 360 & & absEncoder ! = null & &
2025-01-06 15:44:15 +00:00
( absEncoder instanceof SparkMaxEncoderSwerve & & angleMotor . getMotor ( ) instanceof SparkMax ) )
{
angleMotor . setAbsoluteEncoder ( absEncoder ) ;
} else if ( ( absEncoder instanceof ThriftyNovaEncoderSwerve & & angleMotor instanceof ThriftyNovaSwerve ) )
2024-09-03 14:46:24 -05:00
{
angleMotor . setAbsoluteEncoder ( absEncoder ) ;
}
2023-02-20 20:59:31 -06:00
return new SwerveModuleConfiguration (
drive . createMotor ( true ) ,
angleMotor ,
2024-12-09 23:26:04 +00:00
conversionFactors ,
2023-02-20 20:59:31 -06:00
absEncoder ,
absoluteEncoderOffset ,
2023-03-09 21:48:47 -06:00
Units . inchesToMeters ( Math . round ( location . x ) = = 0 ? location . front : location . x ) ,
Units . inchesToMeters ( Math . round ( location . y ) = = 0 ? location . left : location . y ) ,
2023-02-20 20:59:31 -06:00
anglePIDF ,
velocityPIDF ,
physicalCharacteristics ,
absoluteEncoderInverted ,
inverted . drive ,
2023-02-23 22:16:09 -06:00
inverted . angle ,
2024-01-26 11:29:15 -06:00
name . replaceAll ( " \\ .json " , " " ) ,
useCosineCompensator ) ;
2023-02-13 14:37:05 -06:00
}
}