Skip to content

Commit

Permalink
Added composite conversion factors.
Browse files Browse the repository at this point in the history
Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
  • Loading branch information
thenetworkgrinch committed Jul 29, 2024
1 parent 08f4c9a commit 05bce95
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/main/deploy/swerve/neo/modules/physicalproperties.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"conversionFactor": {
"drive": 0.047286787200699704,
"angle": 28.125
"conversionFactors": {
"drive": {
"factor": 0.047286787200699704
},
"angle": {
"factor": 28.125
}
},
"currentLimit": {
"drive": 40,
Expand Down
44 changes: 35 additions & 9 deletions src/main/java/swervelib/parser/json/ModuleJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import swervelib.parser.SwerveModuleConfiguration;
import swervelib.parser.SwerveModulePhysicalCharacteristics;
import swervelib.parser.json.modules.BoolMotorJson;
import swervelib.parser.json.modules.ConversionFactorsJson;
import swervelib.parser.json.modules.LocationJson;

/**
Expand All @@ -20,43 +21,47 @@ 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 = new MotorConfigDouble(0, 0);
public MotorConfigDouble conversionFactor = new MotorConfigDouble(0, 0);
/**
* Conversion Factors composition. Auto-calculates the conversion factors.
*/
public ConversionFactorsJson conversionFactors = new ConversionFactorsJson();
/**
* 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;
public boolean absoluteEncoderInverted = false;
/**
* The location of the swerve module from the center of the robot in inches.
*/
public LocationJson location;
public LocationJson location;
/**
* Should do cosine compensation when not pointing correct direction;.
*/
public boolean useCosineCompensator = true;
public boolean useCosineCompensator = true;

/**
* Create the swerve module configuration based off of parsed data.
Expand Down Expand Up @@ -85,6 +90,27 @@ public SwerveModuleConfiguration createModuleConfiguration(
}
}

// Setup deprecation notice.
// if (this.conversionFactor.drive != 0 && this.conversionFactor.angle != 0)
// {
// new Alert("Configuration",
// "\n'conversionFactor': {'drive': " + conversionFactor.drive + ", 'angle': " + conversionFactor.angle +
// "} \nis deprecated, please use\n" +
// "'conversionFactors': {'drive': {'factor': " + conversionFactor.drive + "}, 'angle': {'factor': " +
// conversionFactor.angle + "} }",
// AlertType.WARNING).set(true);
// }

// Override with composite conversion factor.
if (!conversionFactors.isAngleEmpty())
{
conversionFactor.angle = conversionFactors.angle.calculate();
}
if (!conversionFactors.isDriveEmpty())
{
conversionFactor.drive = conversionFactors.drive.calculate();
}

// Set the conversion factors to null if they are both 0.
if (this.conversionFactor != null)
{
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/swervelib/parser/json/PhysicalPropertiesJson.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package swervelib.parser.json;

import swervelib.parser.SwerveModulePhysicalCharacteristics;
import swervelib.parser.json.modules.ConversionFactorsJson;
import swervelib.telemetry.Alert;
import swervelib.telemetry.Alert.AlertType;

/**
* {@link swervelib.parser.SwerveModulePhysicalCharacteristics} parsed data. Used to configure the SwerveModule.
Expand All @@ -14,23 +17,27 @@ public class PhysicalPropertiesJson
* {@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 = new MotorConfigDouble(0, 0);
public MotorConfigDouble conversionFactor = new MotorConfigDouble(0, 0);
/**
* Conversion Factors composition. Auto-calculates the conversion factors.
*/
public ConversionFactorsJson conversionFactors = new ConversionFactorsJson();
/**
* The current limit in AMPs to apply to the motors.
*/
public MotorConfigInt currentLimit = new MotorConfigInt(40, 20);
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);
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;
public double wheelGripCoefficientOfFriction = 1.19;
/**
* The voltage to use for the smart motor voltage compensation, default is 12.
*/
public double optimalVoltage = 12;
public double optimalVoltage = 12;

/**
* Create the physical characteristics based off the parsed data.
Expand All @@ -39,6 +46,28 @@ public class PhysicalPropertiesJson
*/
public SwerveModulePhysicalCharacteristics createPhysicalProperties()
{
// Setup deprecation notice.
// if (conversionFactor.drive != 0 && conversionFactor.angle != 0 && conversionFactors.isDriveEmpty() &&
// conversionFactors.isAngleEmpty())
// {
// new Alert("Configuration",
// "\n'conversionFactor': {'drive': " + conversionFactor.drive + ", 'angle': " + conversionFactor.angle +
// "} \nis deprecated, please use\n" +
// "'conversionFactors': {'drive': {'factor': " + conversionFactor.drive + "}, 'angle': {'factor': " +
// conversionFactor.angle + "} }",
// AlertType.ERROR_TRACE).set(true);
// }

if (!conversionFactors.isAngleEmpty())
{
conversionFactor.angle = conversionFactors.angle.calculate();
}

if (!conversionFactors.isDriveEmpty())
{
conversionFactor.drive = conversionFactors.drive.calculate();
}

return new SwerveModulePhysicalCharacteristics(
conversionFactor,
wheelGripCoefficientOfFriction,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package swervelib.parser.json.modules;

import swervelib.math.SwerveMath;
import swervelib.telemetry.Alert;
import swervelib.telemetry.Alert.AlertType;

/**
* Angle motor conversion factors composite JSON parse class.
*/
public class AngleConversionFactorsJson
{

/**
* Gear ratio for the angle/steering/azimuth motor on the Swerve Module. Motor rotations to 1 wheel rotation.
*/
public double gearRatio = 0;
/**
* Calculated or given conversion factor.
*/
public double factor = 0;

/**
* Calculate the drive conversion factor.
*
* @return Drive conversion factor, if factor isn't set.
*/
public double calculate()
{
if (factor != 0 && gearRatio != 0)
{
new Alert("Configuration",
"The given angle conversion factor takes precedence over the composite conversion factor, please remove 'factor' if you want to use the composite factor instead.",
AlertType.WARNING).set(true);
}
if (factor == 0)
{
factor = SwerveMath.calculateDegreesPerSteeringRotation(gearRatio);
}
return factor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package swervelib.parser.json.modules;

/**
* Conversion Factors parsed JSON class
*/
public class ConversionFactorsJson
{

/**
* Drive motor conversion factors composition.
*/
public DriveConversionFactorsJson drive = new DriveConversionFactorsJson();
/**
* Angle motor conversion factors composition.
*/
public AngleConversionFactorsJson angle = new AngleConversionFactorsJson();

/**
* Check if the conversion factors are set for the drive motor.
*
* @return Empty
*/
public boolean isDriveEmpty()
{
return drive.factor == 0 && drive.diameter == 0 && drive.gearRatio == 0;
}

/**
* Check if the conversion factors are set for the angle motor.
*
* @return Empty
*/
public boolean isAngleEmpty()
{
return angle.factor == 0 && angle.gearRatio == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package swervelib.parser.json.modules;

import edu.wpi.first.math.util.Units;
import swervelib.math.SwerveMath;
import swervelib.telemetry.Alert;
import swervelib.telemetry.Alert.AlertType;

/**
* Drive motor composite JSON parse class.
*/
public class DriveConversionFactorsJson
{

/**
* Gear ratio for the drive motor rotations to turn the wheel 1 complete rotation.
*/
public double gearRatio = 0;
/**
* Diameter of the wheel in inches.
*/
public double diameter = 0;
/**
* Calculated conversion factor.
*/
public double factor = 0;

/**
* Calculate the drive conversion factor.
*
* @return Drive conversion factor, if factor isn't set.
*/
public double calculate()
{
if (factor != 0 && (diameter != 0 || gearRatio != 0))
{
new Alert("Configuration",
"The given drive conversion factor takes precedence over the composite conversion factor, please remove 'factor' if you want to use the composite factor instead.",
AlertType.WARNING).set(true);
}
if (factor == 0)
{
factor = SwerveMath.calculateMetersPerRotation(Units.inchesToMeters(this.diameter), this.gearRatio);
}
return factor;
}
}

0 comments on commit 05bce95

Please sign in to comment.