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.
|
|
|
|
|
*/
|
|
|
|
|
public DeviceJson drive;
|
|
|
|
|
/**
|
|
|
|
|
* Angle motor device configuration.
|
|
|
|
|
*/
|
|
|
|
|
public DeviceJson angle;
|
|
|
|
|
/**
|
|
|
|
|
* Absolute encoder device configuration.
|
|
|
|
|
*/
|
|
|
|
|
public DeviceJson encoder;
|
|
|
|
|
/**
|
|
|
|
|
* Defines which motors are inverted.
|
|
|
|
|
*/
|
|
|
|
|
public BoolMotorJson inverted;
|
|
|
|
|
/**
|
|
|
|
|
* Absolute encoder offset from 0 in degrees.
|
|
|
|
|
*/
|
|
|
|
|
public double absoluteEncoderOffset;
|
|
|
|
|
/**
|
|
|
|
|
* Absolute encoder inversion state.
|
|
|
|
|
*/
|
2023-02-23 22:16:09 -06:00
|
|
|
public boolean absoluteEncoderInverted = false;
|
|
|
|
|
/**
|
|
|
|
|
* The angle encoder pulse per revolution override. 1 for Neo encoder. 2048 for Falcons.
|
|
|
|
|
*/
|
|
|
|
|
public double angleEncoderPulsePerRevolution = 0;
|
2023-02-13 14:37:05 -06:00
|
|
|
/**
|
|
|
|
|
* The location of the swerve module from the center of the robot in inches.
|
|
|
|
|
*/
|
|
|
|
|
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.
|
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,
|
|
|
|
|
double maxSpeed,
|
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-08-09 16:20:15 -05:00
|
|
|
// 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;
|
|
|
|
|
|
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-02-20 20:59:31 -06:00
|
|
|
return new SwerveModuleConfiguration(
|
|
|
|
|
drive.createMotor(true),
|
|
|
|
|
angleMotor,
|
|
|
|
|
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,
|
|
|
|
|
maxSpeed,
|
|
|
|
|
physicalCharacteristics,
|
|
|
|
|
absoluteEncoderInverted,
|
|
|
|
|
inverted.drive,
|
2023-02-23 22:16:09 -06:00
|
|
|
inverted.angle,
|
2023-08-09 16:20:15 -05:00
|
|
|
angleEncoderPulsePerRevolution == 0 ? angleEncoderPulsePerRotation
|
2023-02-23 22:16:09 -06:00
|
|
|
: angleEncoderPulsePerRevolution,
|
2023-04-08 12:31:07 -05:00
|
|
|
name.replaceAll("\\.json", ""));
|
2023-02-13 14:37:05 -06:00
|
|
|
}
|
|
|
|
|
}
|