Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add linting #37

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,33 @@ jobs:
java-version: 17
distribution: temurin
- name: Run formatter
run: ./gradlew spotlessCheck
run: ./gradlew spotlessCheck

lint:
name: "Linter"
runs-on: ubuntu-latest
# permissions:
# contents: read # for actions/checkout to fetch code
# security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'temurin'
- name: Run PMD
id: pmd
uses: pmd/pmd-github-action@967a81f8b657c87f7c3e96b62301cb1a48efef29
with:
rulesets: 'pmd-ruleset.xml'
# sourcePath: 'src/main/java'
analyzeModifiedFilesOnly: false
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: pmd-report.sarif
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ spotless {
endWithNewline()
}
}

apply plugin: "pmd"

pmd {
toolVersion = "7.3.0"
consoleOutput = true
sourceSets = [project.sourceSets["main"]]
reportsDir = file("${project.buildDir}/reports/pmd")
ruleSetFiles = files(file("$rootDir/pmd-ruleset.xml"))
ruleSets = []
}
125 changes: 125 additions & 0 deletions pmd-ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0"?>
<ruleset name="WPILibRuleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

<description>PMD Ruleset for WPILib</description>

<exclude-pattern>.*/LimeligtHelpers.java</exclude-pattern>

<rule ref="category/java/bestpractices.xml">
<exclude name="AccessorClassGeneration" />
<exclude name="AccessorMethodGeneration" />
<exclude name="AvoidPrintStackTrace" />
<exclude name="AvoidReassigningCatchVariables" />
<exclude name="AvoidReassigningParameters" />
<exclude name="AvoidUsingHardCodedIP" />
<exclude name="ConstantsInInterface" />
<exclude name="JUnitAssertionsShouldIncludeMessage" />
<exclude name="JUnitTestContainsTooManyAsserts" />
<exclude name="JUnitTestsShouldIncludeAssert" />
<exclude name="JUnit4TestShouldUseAfterAnnotation" />
<exclude name="JUnit4TestShouldUseBeforeAnnotation" />
<exclude name="JUnit4TestShouldUseTestAnnotation" />
<exclude name="LooseCoupling" />
<exclude name="PreserveStackTrace" />
<exclude name="ReplaceHashtableWithMap" />
<exclude name="ReplaceVectorWithList" />
<exclude name="SwitchStmtsShouldHaveDefault" />
<exclude name="SystemPrintln" />
<exclude name="UseVarargs" />
</rule>
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressRegex"
value=".*'.*Arguments\(\)'.*" />
</properties>
</rule>
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable">
<properties>
<property name="violationSuppressXPath" value="//Resource" />
</properties>
</rule>

<rule ref="category/java/design.xml">
<exclude name="AvoidThrowingRawExceptionTypes" />
<exclude name="AvoidThrowingNewInstanceOfSameException" />
<exclude name="CognitiveComplexity" />
<exclude name="CouplingBetweenObjects" />
<exclude name="CyclomaticComplexity" />
<exclude name="DataClass" />
<exclude name="ExceptionAsFlowControl" />
<exclude name="ExcessiveImports" />
<exclude name="ExcessiveParameterList" />
<exclude name="ExcessivePublicCount" />
<exclude name="GodClass" />
<exclude name="LawOfDemeter" />
<exclude name="LoosePackageCoupling" />
<exclude name="NPathComplexity" />
<exclude name="NcssCount" />
<exclude name="TooManyFields" />
<exclude name="TooManyMethods" />
</rule>

<rule ref="category/java/errorprone.xml">
<exclude name="AssignmentToNonFinalStatic" />
<exclude name="AvoidCatchingThrowable" />
<exclude name="AvoidDuplicateLiterals" />
<exclude name="AvoidLiteralsInIfCondition" />
<exclude name="CloseResource" />
<exclude name="ConstructorCallsOverridableMethod" />
<exclude name="DoNotTerminateVM" />
<exclude name="EmptyCatchBlock" />
<exclude name="FinalizeDoesNotCallSuperFinalize" />
<exclude name="JUnitSpelling" />
<exclude name="MissingSerialVersionUID" />
<exclude name="NonSerializableClass" />
<exclude name="NullAssignment" />
</rule>

<rule ref="category/java/multithreading.xml">
<exclude name="AvoidSynchronizedAtMethodLevel" />
<exclude name="AvoidUsingVolatile" />
<exclude name="DoNotUseThreads" />
<exclude name="UseConcurrentHashMap" />
</rule>

<rule ref="category/java/performance.xml">
<exclude name="AvoidInstantiatingObjectsInLoops" />
</rule>

<rule name="UnnecessaryCastRule" language="java"
message="Avoid unnecessary casts"
class="net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule"
externalInfoUrl="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryCastRule.java" />

<!-- Custom Rules -->
<rule name="UseRequireNonNull"
message="Use Objects.requireNonNull() instead of throwing a NullPointerException yourself."
language="java" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<description>Use Objects.requireNonNull() instead of throwing a
NullPointerException yourself.</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//IfStatement[child::Expression//NullLiteral]/Statement//ThrowStatement/Expression/PrimaryExpression/PrimaryPrefix/AllocationExpression/ClassOrInterfaceType[@Image='NullPointerException']
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Example {
public Example(Object example) {
if (example == null) {
throw new NullPointerException();
}
}
}
]]>
</example>
</rule>
</ruleset>
16 changes: 8 additions & 8 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static final class PivotConstants {
public static final double SHOOT_TRAP_ANGLE = 0;
public static final double PIVOT_ACCEPTABLE_ERROR = 0.015;

public static double[][] SPEAKER_PIVOT_POSITION = {
public static final double[][] SPEAKER_PIVOT_POSITION = {
// Distance, Angle (rotations)
{1.3, 0.029},
{1.5, 0.037265625},
Expand All @@ -438,7 +438,7 @@ public static final class PivotConstants {
{4.9, 0.1135}
};

public static double[][] PASS_PIVOT_POSITION = {
public static final double[][] PASS_PIVOT_POSITION = {
// Distance, Angle (rotations)
{10.680643009839416, 0.037400390625},
{9.11398136590441, 0.038},
Expand Down Expand Up @@ -482,24 +482,24 @@ public static final class ShooterConstants {
public static final double AUTO_SHOOT_P = 5; // 7 --> 4.5 --> 5
public static final double AUTO_SHOOT_I = 0.0;
public static final double AUTO_SHOOT_D = 0.0;
public static Constraints AUTO_SHOOT_CONSTRAINTS =
public static final Constraints AUTO_SHOOT_CONSTRAINTS =
new Constraints(DriveConstants.MAX_ANGULAR_SPEED_RADIANS_PER_SECOND, 2);

public static final double AUTO_SHOOT_MOVE_P = 10.0;
public static final double AUTO_SHOOT_MOVE_I = 0.0;
public static final double AUTO_SHOOT_MOVE_D = 0.0;
public static Constraints AUTO_SHOOT_MOVE_CONSTRAINTS = new Constraints(10, 5);
public static final Constraints AUTO_SHOOT_MOVE_CONSTRAINTS = new Constraints(10, 5);

public static final double AUTO_LINEUP_ROTATION_P = 3;
public static final double AUTO_LINEUP_ROTATION_I = 0.0;
public static final double AUTO_LINEUP_ROTATION_D = 0.0;
public static Constraints AUTO_LINEUP_ROTATION_CONSTRAINTS =
public static final Constraints AUTO_LINEUP_ROTATION_CONSTRAINTS =
new Constraints(DriveConstants.MAX_ANGULAR_SPEED_RADIANS_PER_SECOND, 2);

public static final double AUTO_LINEUP_TRANSLATION_P = 10; // 4.5, 4.0
public static final double AUTO_LINEUP_TRANSLATION_I = 0.0;
public static final double AUTO_LINEUP_TRANSLATION_D = 0.0;
public static Constraints AUTO_LINEUP_TRANSLATION_CONSTRAINTS = new Constraints(1, 1);
public static final Constraints AUTO_LINEUP_TRANSLATION_CONSTRAINTS = new Constraints(1, 1);

// TODO: calc
public static final double NOTE_LAUNCH_VELOCITY_METERS_PER_SECOND = 10.8;
Expand Down Expand Up @@ -552,13 +552,13 @@ public static final class TrajectoryConstants {
public static final double AUTO_ALIGN_TRANSLATIONAL_I = 0;
public static final double AUTO_ALIGN_TRANSLATIONAL_D = 0;

public static Constraints AUTO_ALIGN_TRANSLATION_CONSTRAINTS = new Constraints(5, 2);
public static final Constraints AUTO_ALIGN_TRANSLATION_CONSTRAINTS = new Constraints(5, 2);

public static final double AUTO_ALIGN_ROTATIONAL_P = 3;
public static final double AUTO_ALIGN_ROTATIONAL_I = 0;
public static final double AUTO_ALIGN_ROTATIONAL_D = 0;

public static Constraints AUTO_ALIGN_ROTATIONAL_CONSTRAINTS =
public static final Constraints AUTO_ALIGN_ROTATIONAL_CONSTRAINTS =
new Constraints(DriveConstants.MAX_ANGULAR_SPEED_RADIANS_PER_SECOND, 2);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void configureButtonBindings() {

Trigger driverRightBumper = new Trigger(driverController::getRightBumper);
Trigger driverRightDirectionPad = new Trigger(() -> driverController.getPOV() == 90);
Trigger driverDownDirectionPad = new Trigger(() -> driverController.getPOV() == 180);
// Trigger driverDownDirectionPad = new Trigger(() -> driverController.getPOV() == 180);
Trigger driverLeftDirectionPad = new Trigger(() -> driverController.getPOV() == 270);

// autodrive
Expand All @@ -233,8 +233,8 @@ private void configureButtonBindings() {
Trigger operatorLeftTrigger = new Trigger(() -> operatorController.getLeftTriggerAxis() > 0.2);
Trigger operatorLeftBumper = new Trigger(operatorController::getLeftBumper);
// amp and speaker
Trigger operatorBButton = new Trigger(operatorController::getBButton);
Trigger operatorRightBumper = new Trigger(operatorController::getRightBumper);
// Trigger operatorBButton = new Trigger(operatorController::getBButton);
// Trigger operatorRightBumper = new Trigger(operatorController::getRightBumper);
Trigger operatorRightTrigger =
new Trigger(() -> operatorController.getRightTriggerAxis() > 0.2);
Trigger driverRightTrigger = new Trigger(() -> driverController.getRightTriggerAxis() > 0.2);
Expand All @@ -246,13 +246,13 @@ private void configureButtonBindings() {
DoubleSupplier operatorRightStickY = operatorController::getRightY;
// unused
Trigger operatorUpDirectionPad = new Trigger(() -> operatorController.getPOV() == 0);
Trigger operatorLeftDirectionPad = new Trigger(() -> operatorController.getPOV() == 270);
// Trigger operatorLeftDirectionPad = new Trigger(() -> operatorController.getPOV() == 270);
Trigger operatorDownDirectionPad = new Trigger(() -> operatorController.getPOV() == 180);
Trigger driverLeftTrigger = new Trigger(() -> driverController.getLeftTriggerAxis() > 0.2);
Trigger driverLeftBumper = new Trigger(driverController::getLeftBumper);
Trigger driverBButton = new Trigger(driverController::getBButton);
Trigger driverYButton = new Trigger(driverController::getYButton);
DoubleSupplier operatorLeftStickY = operatorController::getLeftY;
// Trigger driverYButton = new Trigger(driverController::getYButton);
// DoubleSupplier operatorLeftStickY = operatorController::getLeftY;

// DRIVER BUTTONS

Expand Down
27 changes: 4 additions & 23 deletions src/main/java/frc/robot/commands/shooter/AutoSpinUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ public class AutoSpinUp extends DriveCommandBase {
private final DriveSubsystem driveSubsystem;
private final ShooterSubsystem shooterSubsystem;
private final PivotSubsystem pivotSubsystem;
private final VisionSubsystem visionSubsystem;
private final LEDSubsystem leds;

private final DoubleSupplier leftX, leftY, rightX;
private final BooleanSupplier isFieldRelative;

private double headingError = 0;

private boolean isRed = false;
private double desiredHeading = 0;
private double desiredHeading;
private Translation2d speakerPos;

/** Creates a new SpinUpForSpeaker. */
Expand All @@ -53,7 +49,6 @@ public AutoSpinUp(
this.driveSubsystem = driveSubsystem;
this.shooterSubsystem = shooterSubsystem;
this.pivotSubsystem = pivotSubsystem;
this.visionSubsystem = visionSubsystem;
this.leftX = leftX;
this.leftY = leftY;
this.rightX = rightX;
Expand All @@ -66,14 +61,8 @@ public AutoSpinUp(
@Override
public void initialize() {
Optional<Alliance> alliance = DriverStation.getAlliance();
// if alliance is detected
if (alliance.isPresent()) {
// and if it's red, we're red
isRed = alliance.get() == Alliance.Red;
} else {
// otherwise default to red alliance
isRed = true;
}
boolean isRed = alliance.get() == Alliance.Red;

// SmartDashboard.putBoolean("red", isRed);
speakerPos =
isRed
Expand All @@ -89,15 +78,10 @@ public void execute() {

// get positions of various things
Translation2d robotPos = driveSubsystem.getPose().getTranslation();
// distance (for speaker lookups)
double distance = robotPos.getDistance(speakerPos);
// arctangent for desired heading
desiredHeading =
Math.atan2((robotPos.getY() - speakerPos.getY()), (robotPos.getX() - speakerPos.getX()));

// current
headingError = desiredHeading - driveSubsystem.getOdometryRotation2d().getRadians();

// allow the driver to drive slowly (NOT full speed - will mess up shooter)
driveSubsystem.drive(
deadband(leftY.getAsDouble()) * DriveConstants.MAX_SPEED_METERS_PER_SECOND,
Expand Down Expand Up @@ -129,10 +113,7 @@ public boolean isFinished() {
}

public boolean isInRange() {
if (desiredHeading > 25) {
return true;
}
return false;
return (desiredHeading > 25);
}

private double deadband(double val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class VisionSubsystem extends SubsystemBase {
* The pose estimates from the limelights in the following order {shooterLimelight,
* frontLeftLimelight, frontRightLimelight}
*/
private PoseEstimate[] limelightEstimates;
private final PoseEstimate[] limelightEstimates;

public VisionSubsystem() {
limelightEstimates = new PoseEstimate[3];
Expand Down