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

climb pivot + algae flywheel #35

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands.drive;
package frc.robot.commands;

import edu.wpi.first.wpilibj2.command.Command;

/* You should consider using the more terse Command factories API instead https://docs.wpilib.org/en/stable/docs/software/commandbased/organizing-command-based.html#defining-commands */
public class XState extends Command {
/** Creates a new XState. */
public XState() {
public class ClimbPivot extends Command {
/** Creates a new ClimbPivot. */
private final ClimbPivot climbPivot;

Check warning on line 11 in src/main/java/frc/robot/commands/ClimbPivot.java

View workflow job for this annotation

GitHub Actions / Lint

It is somewhat confusing to have a field name matching the declaring class name

It is somewhat confusing to have a field name matching the declaring type name. This probably means that type and/or field names should be chosen more carefully. AvoidFieldNameMatchingTypeName (Priority: 3, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_errorprone.html#avoidfieldnamematchingtypename
private final double targetPosition = 0;

Check warning on line 12 in src/main/java/frc/robot/commands/ClimbPivot.java

View workflow job for this annotation

GitHub Actions / Lint

This final field could be made static

If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object at runtime. FinalFieldCouldBeStatic (Priority: 3, Ruleset: Design) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_design.html#finalfieldcouldbestatic

Check warning on line 12 in src/main/java/frc/robot/commands/ClimbPivot.java

View workflow job for this annotation

GitHub Actions / Lint

The field initializer for 'targetPosition' is never used (overwritten on line 16)

Reports assignments to variables that are never used before the variable is overwritten, or goes out of scope. Unused assignments are those for which 1. The variable is never read after the assignment, or 2. The assigned value is always overwritten by other assignments before the next read of the variable. The rule tracks assignements to fields of `this`, and static fields of the current class. This may cause some false positives in timing-sensitive concurrent code, which the rule cannot detect. The rule may be suppressed with the standard `@SuppressWarnings("unused")` tag. The rule subsumes {% rule "UnusedLocalVariable" %}, and {% rule "UnusedFormalParameter" %}. Those violations are filtered out by default, in case you already have enabled those rules, but may be enabled with the property `reportUnusedVariables`. Variables whose name starts with `ignored` or `unused` are filtered out, as is standard practice for exceptions. Limitations: * The rule currently cannot know which method calls throw exceptions, or which exceptions they throw. In the body of a try block, every method or constructor call is assumed to throw. This may cause false-negatives. The only other language construct that is assumed to throw is the `throw` statement, in particular, things like `assert` statements, or NullPointerExceptions on dereference are ignored. * The rule cannot resolve assignments across constructors, when they're called with the special `this(...)` syntax. This may cause false-negatives. Both of those limitations may be partly relaxed in PMD 7. UnusedAssignment (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_bestpractices.html#unusedassignment

public ClimbPivot(CoralPivot climbPivot, double targetPosition) {
this.climbPivot = climbPivot;
this.targetPosition = targetPosition;
addRequirements(climbPivot);
// Use addRequirements() here to declare subsystem dependencies.
}

// Called when the command is initially scheduled.
@Override
public void initialize() {}
public void initialize() {
climbPivot.setPivotPosition(targetPosition);
}

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {}

// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {}

public void end(boolean interrupted) {
climbPivot.stop();

}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
double error = Math.abs(climbPivot.getCurrentPosition() - targetPosition);
return error < 1.0;
}
}
45 changes: 45 additions & 0 deletions src/main/java/frc/robot/commands/drive/DealgaefierPivot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands.drive;

import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.pivotforcoralorsomeshit.CoralPivot;

public class CoralPivot extends Command {
/** Creates a new CoralPivot. */
private final CoralPivot coralPivot;

Check warning on line 12 in src/main/java/frc/robot/commands/drive/DealgaefierPivot.java

View workflow job for this annotation

GitHub Actions / Lint

It is somewhat confusing to have a field name matching the declaring class name

It is somewhat confusing to have a field name matching the declaring type name. This probably means that type and/or field names should be chosen more carefully. AvoidFieldNameMatchingTypeName (Priority: 3, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_errorprone.html#avoidfieldnamematchingtypename
private final double targetPosition = 0;

Check warning on line 13 in src/main/java/frc/robot/commands/drive/DealgaefierPivot.java

View workflow job for this annotation

GitHub Actions / Lint

This final field could be made static

If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object at runtime. FinalFieldCouldBeStatic (Priority: 3, Ruleset: Design) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_design.html#finalfieldcouldbestatic

Check warning on line 13 in src/main/java/frc/robot/commands/drive/DealgaefierPivot.java

View workflow job for this annotation

GitHub Actions / Lint

The field initializer for 'targetPosition' is never used (overwritten on line 17)

Reports assignments to variables that are never used before the variable is overwritten, or goes out of scope. Unused assignments are those for which 1. The variable is never read after the assignment, or 2. The assigned value is always overwritten by other assignments before the next read of the variable. The rule tracks assignements to fields of `this`, and static fields of the current class. This may cause some false positives in timing-sensitive concurrent code, which the rule cannot detect. The rule may be suppressed with the standard `@SuppressWarnings("unused")` tag. The rule subsumes {% rule "UnusedLocalVariable" %}, and {% rule "UnusedFormalParameter" %}. Those violations are filtered out by default, in case you already have enabled those rules, but may be enabled with the property `reportUnusedVariables`. Variables whose name starts with `ignored` or `unused` are filtered out, as is standard practice for exceptions. Limitations: * The rule currently cannot know which method calls throw exceptions, or which exceptions they throw. In the body of a try block, every method or constructor call is assumed to throw. This may cause false-negatives. The only other language construct that is assumed to throw is the `throw` statement, in particular, things like `assert` statements, or NullPointerExceptions on dereference are ignored. * The rule cannot resolve assignments across constructors, when they're called with the special `this(...)` syntax. This may cause false-negatives. Both of those limitations may be partly relaxed in PMD 7. UnusedAssignment (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.9.0/pmd_rules_java_bestpractices.html#unusedassignment

public CoralPivot(CoralPivot coralPivot, double targetPosition) {
this.coralPivot = coralPivot;
this.targetPosition = targetPosition;
addRequirements(coralPivot);
// Use addRequirements() here to declare subsystem dependencies.
}

// Called when the command is initially scheduled.
@Override
public void initialize() {
coralPivot.setPivotPosition(targetPosition);
}

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {}

// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
coralPivot.stop();

}

// Returns true when the command should end.
@Override
public boolean isFinished() {
double error = Math.abs(coralPivot.getCurrentPosition() - targetPosition);
return error < 1.0;
}
}
42 changes: 42 additions & 0 deletions src/main/java/frc/robot/subsystems/flywheel/Flywheel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.flywheel;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.subsystems.flywheel.FlywheelConstants;

public class FlywheelSubsystem extends SubsystemBase {
/** Creates a new FlywheelSubsystem. */
private FlywheelInterface flywheelInterface;

private FlywheelInputsAutoLogged inputs = new FlywheelInputsAutoLogged();

public Flywheel() {

}

public double getFlywheelSpeed() {
return flywheelInterface.getFlywheelSpeed();
}

public double getVolts() {
return flywheelInterface.getVolts();
}

public void setFlywheelSpeed(double speed) {
flywheelInterface.setFlywheelSpeed(speed);
}

public void setVolts(double volts) {
flywheelInterface.setVolts(volts);
}

@Override
public void periodic() {
// This method will be called once per scheduler run
flywheelInterface.updateInputs(inputs);
Logger.processInputs("Flywheel/", inputs);
}
}
16 changes: 16 additions & 0 deletions src/main/java/frc/robot/subsystems/flywheel/FlywheelConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.flywheel;

import com.ctre.phoenix6.hardware.TalonFX;

/** Add your docs here. */
public class FlywheelConstants {
public static final int FLYWHEEL_MOTOR_ID = 0-9;
public static final double FLYWHEEL_GEAR_RATIO = 0;
public static final double FLYWHEEL_P = 0;
public static final double FLYWHEEL_I = 0;
public static final double FLYWHEEL_D = 0;
}
30 changes: 30 additions & 0 deletions src/main/java/frc/robot/subsystems/flywheel/FlywheelInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.flywheel;
import org.littletonrobotics.junction.AutoLog;
/** Add your docs here. */
public class FlywheelInterface {
@AutoLog
public static class FlywheelInterface {
public double leaderMotorPosition = 0.0;
}


public default void updateInputs(FlywheelInterface inputs) {}


public default double getFlywheelSpeed() {
return 0.0;
}

public default void setFlywheelSpeed(double speed) {}

public default void setVolts(double volts) {}

public default double getVolts() {
return 0.0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.flywheel;

/** Add your docs here. */
public class PhysicalFlywheel {}
51 changes: 51 additions & 0 deletions src/main/java/frc/robot/subsystems/flywheel/SimulatedFlywheel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.flywheel;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.simulation.FlywheelSim;

/** Add your docs here. */
public class SimulatedFlywheel implements FlywheelInterface {
private SimulatedFlywheel simulatedflywheel =
new SimulatedFlywheel(
DCMotor.getFalcon500(2),
FlywheelConstants.FLYWHEEL_GEAR_RATIO,
true,
0.0);
private PIDController simPID;
private double currentVolts;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id log this value to akit by adding something like flywheelAppliedVolts to the interface, and then setting this value equal to that in updateInputs.


public SimulatedFlywheel(DCMotor dcMotor, double flywheelGearRatio, boolean b, double d) {
simPID =
new PIDController(
FlywheelConstants.FLYWHEEL_P,
FlywheelConstants.FLYWHEEL_I,
FlywheelConstants.FLYWHEEL_D);
}

public void updateInputs(FlywheelInputs inputs) {
inputs.leaderMotorPosition = getFlywheelPosition();
inputs.followerMotorPosition = getFlywheelPosition();
}

public void setFlywheelspeed(double speed) {
setVolts(simPID.calculate(getFlywheelSpeed(), speed));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure your units are and will be consistent.

also your setVolts method uses the pid to calculate the pid calculating speed. That's kinda not good.

}

public double getFlywheelSpeed() {
return flywheelSim.getAngularVelocityRPM();
}

public void setVolts(double volts) {
currentVolts = simPID.calculate(volts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are setting straight voltage i wouldn't use pid.

elevatorSim.setInputVoltage(currentVolts);
}

public double getVolts() {
return currentVolts;
}
}
57 changes: 57 additions & 0 deletions src/main/java/frc/robot/subsystems/pivot/ClimbPivot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.pivot;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.can.TalonFX;
import com.ctre.phoenix.motorcontrol.TalonFXConfiguration;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;

public class ClimbPivot extends SubsystemBase {
/** Creates a new ClimbPivot. */
public TalonFX climbPivotMotor = new TalonFX(Constants.CLIMB_PIVOT_MOTOR);
private static final int TICKS_PER_ROTATION = 2048;
private static final double GEAR_RATIO = 10.0;

public ClimbPivot() {
TalonFXConfiguration config = new TalonFXConfiguration();
config.slot0.kP = 0.0;
config.slot0.kI = 0.0;
config.slot0.kD = 0.0;
config.slot0.kF = 0.0;
config.motionCruiseVelocity = 15000;
climb.motionAcceleration = 6000;
climbPivotMotor.configAllSettings(config);
climbPivotMotor.setNeutralMode(NeutralMode.Brake);
climbPivotMotor.setSelectedSensorPosition(0);
}

public void setPivotPosition(double degrees) {
double ticks = degreesToTicks(degrees);
climbPivotMotor.set(ControlMode.MotionMagic, ticks);
}

public double getCurrentPosition() {
return ticksToDegrees(climbPivotMotor.getSelectedSensorPosition());
}

public void stop() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait I'm not sure what the mechanism looks like, but what is stop doing? If we have one method used for setting a position, it seems weird having another method for just like turning off the motor.

Rather than that it would make more sense to me that the commands that call this would just use setPivotPosition to set it to its like inactive position.

climbPivotMotor.set(ControlMode.PercentOutput, 0);
}

private double degreesToTicks(double degrees) {
return degrees / 360.0 * TICKS_PER_ROTATION * GEAR_RATIO;
}

private double ticksToDegrees(double ticks) {
return ticks / (TICKS_PER_ROTATION * GEAR_RATIO) * 360.0;
}

@Override
public void periodic() {
System.out.println("Current Position: " + getCurrentPosition());
}
}
57 changes: 57 additions & 0 deletions src/main/java/frc/robot/subsystems/pivot/DealgaefierPivot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.pivot;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.can.TalonFX;
import com.ctre.phoenix.motorcontrol.TalonFXConfiguration;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;

public class CoralPivot extends SubsystemBase {
/** Creates a new Cpivot. */
public TalonFX dealgaefierPivotMotor = new TalonFX(Constants.CORAL_PIVOT_MOTOR);
private static final int TICKS_PER_ROTATION = 2048;
private static final double GEAR_RATIO = 10.0;

public CoralPivot() {
TalonFXConfiguration config = new TalonFXConfiguration();
config.slot0.kP = 0.0;
config.slot0.kI = 0.0;
config.slot0.kD = 0.0;
config.slot0.kF = 0.0;
config.motionCruiseVelocity = 15000;
config.motionAcceleration = 6000;
dealgaefierPivotMotor.configAllSettings(config);
dealgaefierPivotMotor.setNeutralMode(NeutralMode.Brake);
dealgaefierPivotMotor.setSelectedSensorPosition(0);
}

public void setPivotPosition(double degrees) {
double ticks = degreesToTicks(degrees);
dealgaefierPivotMotor.set(ControlMode.MotionMagic, ticks);
}

public double getCurrentPosition() {
return ticksToDegrees(dealgaefierPivotMotor.getSelectedSensorPosition());
}

public void stop() {
dealgaefierPivotMotor.set(ControlMode.PercentOutput, 0);
}

private double degreesToTicks(double degrees) {
return degrees / 360.0 * TICKS_PER_ROTATION * GEAR_RATIO;
}

private double ticksToDegrees(double ticks) {
return ticks / (TICKS_PER_ROTATION * GEAR_RATIO) * 360.0;
}

@Override
public void periodic() {
System.out.println("Current Position: " + getCurrentPosition());
}
}
12 changes: 12 additions & 0 deletions src/main/java/frc/robot/subsystems/pivot/PivotConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.pivot;

/** Add your docs here. */
public class PivotConstants {
public static final TalonFX CORAL_PIVOT_MOTOR = 0-9;
public static final TalonFX CLIMB_PIVOT_MOTOR = 0-9;
public static final TalonFX FLYWHEEL_MOTOR = 0-9;
}
Loading