mirror of
https://github.com/BroncBotz3481/YAGSL
synced 2026-07-02 07:21:39 +00:00
Updated swervelib
This commit is contained in:
32
swervelib/parser/json/ControllerPropertiesJson.java
Normal file
32
swervelib/parser/json/ControllerPropertiesJson.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.PIDFConfig;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveControllerConfiguration;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveDriveConfiguration;
|
||||
|
||||
/**
|
||||
* {@link frc.robot.subsystems.swervedrive2.swervelib.SwerveController} parsed class. Used to access the JSON data.
|
||||
*/
|
||||
public class ControllerPropertiesJson
|
||||
{
|
||||
|
||||
/**
|
||||
* The minimum radius of the angle control joystick to allow for heading adjustment of the robot.
|
||||
*/
|
||||
public double angleJoystickRadiusDeadband;
|
||||
/**
|
||||
* The PID used to control the robot heading.
|
||||
*/
|
||||
public PIDFConfig heading;
|
||||
|
||||
/**
|
||||
* Create the {@link SwerveControllerConfiguration} based on parsed and given data.
|
||||
*
|
||||
* @param driveConfiguration {@link SwerveDriveConfiguration} parsed configuration.
|
||||
* @return {@link SwerveControllerConfiguration} object based on parsed data.
|
||||
*/
|
||||
public SwerveControllerConfiguration createControllerConfiguration(SwerveDriveConfiguration driveConfiguration)
|
||||
{
|
||||
return new SwerveControllerConfiguration(driveConfiguration, heading, angleJoystickRadiusDeadband);
|
||||
}
|
||||
}
|
||||
100
swervelib/parser/json/DeviceJson.java
Normal file
100
swervelib/parser/json/DeviceJson.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.encoders.CANCoderSwerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.encoders.SparkMaxEncoderSwerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.encoders.SwerveAbsoluteEncoder;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.imu.NavXSwerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.imu.Pigeon2Swerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.imu.PigeonSwerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.imu.SwerveIMU;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.motors.SparkMaxSwerve;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.motors.SwerveMotor;
|
||||
|
||||
/**
|
||||
* Device JSON parsed class. Used to access the JSON data.
|
||||
*/
|
||||
public class DeviceJson
|
||||
{
|
||||
|
||||
/**
|
||||
* The device type, e.g. pigeon/pigeon2/sparkmax/talonfx/navx
|
||||
*/
|
||||
public String type;
|
||||
/**
|
||||
* The CAN ID or pin ID of the device.
|
||||
*/
|
||||
public int id;
|
||||
/**
|
||||
* The CAN bus name which the device resides on if using CAN.
|
||||
*/
|
||||
public String canbus = "";
|
||||
|
||||
/**
|
||||
* Create a {@link SwerveAbsoluteEncoder} from the current configuration.
|
||||
*
|
||||
* @return {@link SwerveAbsoluteEncoder} given.
|
||||
*/
|
||||
public SwerveAbsoluteEncoder createEncoder()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case "integrated":
|
||||
case "attached":
|
||||
return null;
|
||||
case "cancoder":
|
||||
return new CANCoderSwerve(id, canbus != null ? canbus : "");
|
||||
default:
|
||||
throw new RuntimeException(type + " is not a recognized absolute encoder type.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SwerveIMU} from the given configuration.
|
||||
*
|
||||
* @return {@link SwerveIMU} given.
|
||||
*/
|
||||
public SwerveIMU createIMU()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case "navx":
|
||||
return new NavXSwerve();
|
||||
case "pigeon":
|
||||
return new PigeonSwerve(id);
|
||||
case "pigeon2":
|
||||
return new Pigeon2Swerve(id, canbus != null ? canbus : "");
|
||||
default:
|
||||
throw new RuntimeException(type + " is not a recognized absolute encoder type.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SwerveMotor} from the given configuration.
|
||||
*
|
||||
* @param isDriveMotor If the motor being generated is a drive motor.
|
||||
* @return {@link SwerveMotor} given.
|
||||
*/
|
||||
public SwerveMotor createMotor(boolean isDriveMotor)
|
||||
{
|
||||
if (type.equals("sparkmax"))
|
||||
{
|
||||
return new SparkMaxSwerve(id, isDriveMotor);
|
||||
}
|
||||
throw new RuntimeException(type + " is not a recognized absolute encoder type.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SwerveAbsoluteEncoder} from the data port on the motor controller.
|
||||
*
|
||||
* @param motor The motor to create the absolute encoder from.
|
||||
* @return {@link SwerveAbsoluteEncoder} from the motor controller.
|
||||
*/
|
||||
public SwerveAbsoluteEncoder createIntegratedEncoder(SwerveMotor motor)
|
||||
{
|
||||
if (type.equals("sparkmax"))
|
||||
{
|
||||
return new SparkMaxEncoderSwerve(motor);
|
||||
}
|
||||
throw new RuntimeException("Could not create absolute encoder from data port of " + type + " id " + id);
|
||||
}
|
||||
}
|
||||
76
swervelib/parser/json/ModuleJson.java
Normal file
76
swervelib/parser/json/ModuleJson.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
import edu.wpi.first.math.util.Units;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.encoders.SwerveAbsoluteEncoder;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.motors.SwerveMotor;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.PIDFConfig;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveModuleConfiguration;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveModulePhysicalCharacteristics;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.json.modules.BoolMotorJson;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.json.modules.LocationJson;
|
||||
|
||||
/**
|
||||
* {@link frc.robot.subsystems.swervedrive2.swervelib.SwerveModule} JSON parsed class. Used to access the JSON data.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public boolean absoluteEncoderInverted = false;
|
||||
/**
|
||||
* 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.
|
||||
* @return {@link SwerveModuleConfiguration} based on the provided data and parsed data.
|
||||
*/
|
||||
public SwerveModuleConfiguration createModuleConfiguration(PIDFConfig anglePIDF, PIDFConfig velocityPIDF,
|
||||
double maxSpeed,
|
||||
SwerveModulePhysicalCharacteristics physicalCharacteristics)
|
||||
{
|
||||
SwerveMotor angleMotor = angle.createMotor(false);
|
||||
SwerveAbsoluteEncoder absEncoder = encoder.createEncoder();
|
||||
|
||||
// If the absolute encoder is attached.
|
||||
if (absEncoder == null)
|
||||
{
|
||||
absEncoder = angle.createIntegratedEncoder(angleMotor);
|
||||
angleMotor.setAbsoluteEncoder(absEncoder);
|
||||
}
|
||||
|
||||
return new SwerveModuleConfiguration(drive.createMotor(true), angleMotor, absEncoder,
|
||||
absoluteEncoderOffset, Units.inchesToMeters(location.x),
|
||||
Units.inchesToMeters(location.y), anglePIDF, velocityPIDF, maxSpeed,
|
||||
physicalCharacteristics, absoluteEncoderInverted, inverted.drive,
|
||||
inverted.angle);
|
||||
}
|
||||
}
|
||||
20
swervelib/parser/json/PIDFPropertiesJson.java
Normal file
20
swervelib/parser/json/PIDFPropertiesJson.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.PIDFConfig;
|
||||
|
||||
/**
|
||||
* {@link frc.robot.subsystems.swervedrive2.swervelib.SwerveModule} PID with Feedforward for the drive motor and angle
|
||||
* motor.
|
||||
*/
|
||||
public class PIDFPropertiesJson
|
||||
{
|
||||
|
||||
/**
|
||||
* The PIDF with Integral Zone used for the drive motor.
|
||||
*/
|
||||
public PIDFConfig drive;
|
||||
/**
|
||||
* The PIDF with Integral Zone used for the angle motor.
|
||||
*/
|
||||
public PIDFConfig angle;
|
||||
}
|
||||
128
swervelib/parser/json/PhysicalPropertiesJson.java
Normal file
128
swervelib/parser/json/PhysicalPropertiesJson.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
import edu.wpi.first.math.util.Units;
|
||||
import frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveModulePhysicalCharacteristics;
|
||||
|
||||
/**
|
||||
* {@link frc.robot.subsystems.swervedrive2.swervelib.parser.SwerveModulePhysicalCharacteristics} parsed data. Used to
|
||||
* configure the SwerveModule.
|
||||
*/
|
||||
public class PhysicalPropertiesJson
|
||||
{
|
||||
|
||||
/**
|
||||
* Wheel diameter in inches.
|
||||
*/
|
||||
public double wheelDiameter;
|
||||
/**
|
||||
* Gear ratio for the motors, number of times the motor has to spin before the wheel rotates a single time.
|
||||
*/
|
||||
public MotorConfigDouble gearRatio;
|
||||
/**
|
||||
* Encoder pulse per rotation for non-integrated encoders. 1 for integrated encoders.
|
||||
*/
|
||||
public MotorConfigInt encoderPulsePerRotation = new MotorConfigInt(1, 1);
|
||||
/**
|
||||
* The current limit in AMPs to apply to the motors.
|
||||
*/
|
||||
public MotorConfigInt currentLimit = new MotorConfigInt(40, 20);
|
||||
/**
|
||||
* The minimum number of seconds to take for the motor to go from 0 to full throttle.
|
||||
*/
|
||||
public MotorConfigDouble rampRate = new MotorConfigDouble(0.25, 0.25);
|
||||
/**
|
||||
* The grip tape coefficient of friction on carpet. Used to calculate the practical maximum acceleration.
|
||||
*/
|
||||
public double wheelGripCoefficientOfFriction = 1.19;
|
||||
/**
|
||||
* Angle motor free speed rotations per minute.
|
||||
*/
|
||||
public double angleMotorFreeSpeedRPM;
|
||||
|
||||
/**
|
||||
* Create the physical characteristics based off the parsed data.
|
||||
*
|
||||
* @param optimalVoltage Optimal voltage to compensate for and use to calculate drive motor feedforward.
|
||||
* @return {@link SwerveModulePhysicalCharacteristics} based on parsed data.
|
||||
*/
|
||||
public SwerveModulePhysicalCharacteristics createPhysicalProperties(double optimalVoltage)
|
||||
{
|
||||
return new SwerveModulePhysicalCharacteristics(gearRatio.drive, gearRatio.angle, angleMotorFreeSpeedRPM,
|
||||
Units.inchesToMeters(wheelDiameter), wheelGripCoefficientOfFriction,
|
||||
optimalVoltage,
|
||||
currentLimit.drive, currentLimit.angle, rampRate.drive,
|
||||
rampRate.angle, encoderPulsePerRotation.drive,
|
||||
encoderPulsePerRotation.angle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to store doubles for motor configuration.
|
||||
*/
|
||||
class MotorConfigDouble
|
||||
{
|
||||
|
||||
/**
|
||||
* Drive motor.
|
||||
*/
|
||||
public double drive;
|
||||
/**
|
||||
* Angle motor.
|
||||
*/
|
||||
public double angle;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public MotorConfigDouble()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*
|
||||
* @param angle Angle data.
|
||||
* @param drive Drive data.
|
||||
*/
|
||||
public MotorConfigDouble(double angle, double drive)
|
||||
{
|
||||
this.angle = angle;
|
||||
this.drive = drive;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to store ints for motor configuration.
|
||||
*/
|
||||
class MotorConfigInt
|
||||
{
|
||||
|
||||
/**
|
||||
* Drive motor.
|
||||
*/
|
||||
public int drive;
|
||||
/**
|
||||
* Angle motor.
|
||||
*/
|
||||
public int angle;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public MotorConfigInt()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor with values.
|
||||
*
|
||||
* @param drive Drive data.
|
||||
* @param angle Angle data.
|
||||
*/
|
||||
public MotorConfigInt(int drive, int angle)
|
||||
{
|
||||
this.angle = angle;
|
||||
this.drive = drive;
|
||||
}
|
||||
}
|
||||
30
swervelib/parser/json/SwerveDriveJson.java
Normal file
30
swervelib/parser/json/SwerveDriveJson.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json;
|
||||
|
||||
/**
|
||||
* {@link frc.robot.subsystems.swervedrive2.swervelib.SwerveDrive} JSON parsed class. Used to access parsed data from
|
||||
* the swervedrive.json file.
|
||||
*/
|
||||
public class SwerveDriveJson
|
||||
{
|
||||
|
||||
/**
|
||||
* Maximum robot speed in feet per second.
|
||||
*/
|
||||
public double maxSpeed;
|
||||
/**
|
||||
* Optimal voltage to compensate to and base feedforward calculations off of.
|
||||
*/
|
||||
public double optimalVoltage;
|
||||
/**
|
||||
* Robot IMU used to determine heading of the robot.
|
||||
*/
|
||||
public DeviceJson imu;
|
||||
/**
|
||||
* Invert the IMU of the robot.
|
||||
*/
|
||||
public boolean invertedIMU;
|
||||
/**
|
||||
* Module JSONs in order clockwise order starting from front left.
|
||||
*/
|
||||
public String[] modules;
|
||||
}
|
||||
17
swervelib/parser/json/modules/BoolMotorJson.java
Normal file
17
swervelib/parser/json/modules/BoolMotorJson.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json.modules;
|
||||
|
||||
/**
|
||||
* Inverted motor JSON parsed class. Used to access the JSON data.
|
||||
*/
|
||||
public class BoolMotorJson
|
||||
{
|
||||
|
||||
/**
|
||||
* Drive motor inversion state.
|
||||
*/
|
||||
public boolean drive;
|
||||
/**
|
||||
* Angle motor inversion state.
|
||||
*/
|
||||
public boolean angle;
|
||||
}
|
||||
19
swervelib/parser/json/modules/LocationJson.java
Normal file
19
swervelib/parser/json/modules/LocationJson.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package frc.robot.subsystems.swervedrive2.swervelib.parser.json.modules;
|
||||
|
||||
/**
|
||||
* Location JSON parsed class. Used to access the JSON data. Module locations, in inches, as distances to the center of
|
||||
* the robot. Positive x is torwards the robot front, and * +y is torwards robot left.
|
||||
*/
|
||||
|
||||
public class LocationJson
|
||||
{
|
||||
|
||||
/**
|
||||
* Location of the swerve module in inches from the center of the robot horizontally.
|
||||
*/
|
||||
public double x;
|
||||
/**
|
||||
* Location of the swerve module in inches from the center of the robot vertically.
|
||||
*/
|
||||
public double y;
|
||||
}
|
||||
Reference in New Issue
Block a user