-
Notifications
You must be signed in to change notification settings - Fork 4
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
Code for claw mech. RobotContainer updated with button bindings and s… #10
Closed
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
755dfcb
Code for claw mech. RobotContainer updated with button bindings and s…
Ippyb ee88ee8
per 7 claw mech with states, button release, and encoders
Ippyb ed326f2
initialized booleans
Ippyb 9fec24f
add 2nd motor
PinkBlueSky 046efc6
Removed duplicate Xbox
PinkBlueSky 21e07ec
Use only one encoder; add back the 2nd xbox controller
PinkBlueSky 34c2f1b
Edit controller bindings and restructure close/lift command
PinkBlueSky b608226
Restructure ClawSubsystem to have state vars; add new commands
PinkBlueSky f16d99c
Print encoder positions for the claw
PinkBlueSky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/frc/robot/commands/claw/CloseAndLiftClawCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package frc.robot.commands.claw; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.subsystems.claw.ClawSubsystem; | ||
|
||
public class CloseAndLiftClawCommand extends CommandBase { | ||
|
||
@SuppressWarnings({ "PMD.UnusedPrivateField", "PMD.SingularField" }) | ||
private final ClawSubsystem clawSubsystem; | ||
|
||
public CloseAndLiftClawCommand(ClawSubsystem clawSubsystem) { | ||
this.clawSubsystem = clawSubsystem; | ||
|
||
addRequirements(clawSubsystem); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
// Once this command is triggered, close the claw | ||
clawSubsystem.clawIsOpen = false; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
// Repeatedly check if the claw-closing is finished | ||
if (clawSubsystem.isMotorStalled()) { | ||
// If finished closing claw, lift the claw | ||
clawSubsystem.clawIsLifted = true; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
// If command has completed operation | ||
if (clawSubsystem.clawIsLifted) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/frc/robot/subsystems/claw/ClawSubsystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package frc.robot.subsystems.claw; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandScheduler; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
import com.ctre.phoenix.motorcontrol.can.TalonSRX; | ||
import edu.wpi.first.wpilibj.Solenoid; | ||
|
||
import com.ctre.phoenix.motorcontrol.ControlMode; | ||
import com.ctre.phoenix.motorcontrol.FeedbackDevice; | ||
|
||
import static frc.robot.Constants.ClawConstants.*; | ||
|
||
public class ClawSubsystem extends SubsystemBase { | ||
private TalonSRX rightMotor; | ||
private TalonSRX leftMotor; | ||
private Solenoid pfft; | ||
|
||
// Position variables for the motors | ||
private double rightPos; | ||
private double leftPos; | ||
|
||
// Set initial state: claw open and not lifted | ||
public boolean clawIsOpen = true; | ||
public boolean clawIsLifted = false; | ||
|
||
public ClawSubsystem() { | ||
CommandScheduler.getInstance().registerSubsystem(this); | ||
|
||
// Initialize motors and pneumatic | ||
rightMotor = new TalonSRX(rightMotorPort); | ||
leftMotor = new TalonSRX(leftMotorPort); | ||
|
||
pfft = new Solenoid(pfftPCMPort); | ||
|
||
// Configure the motors for the encoders | ||
rightMotor.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, 0, 0); | ||
rightMotor.setSelectedSensorPosition(0); | ||
|
||
leftMotor.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, 0, 0); | ||
leftMotor.setSelectedSensorPosition(0); | ||
|
||
// Set initial motor position vars | ||
rightPos = 0; | ||
leftPos = 0; | ||
|
||
} | ||
|
||
@Override | ||
public void periodic() { | ||
// If claw is meant to be open | ||
if (clawIsOpen) { | ||
|
||
// Set motor | ||
if (rightMotor.getSelectedSensorPosition() < clawOpenPosition) { | ||
rightMotor.set(ControlMode.PercentOutput, clawMotorSpeed); | ||
e3l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
leftMotor.set(ControlMode.PercentOutput, -clawMotorSpeed); | ||
} | ||
} else { | ||
// If claw is meant to be closed; give motors power until motors stall | ||
if (!isMotorStalled()) { | ||
rightMotor.set(ControlMode.PercentOutput, -clawMotorSpeed); | ||
leftMotor.set(ControlMode.PercentOutput, clawMotorSpeed); | ||
} | ||
} | ||
|
||
// If claw is meant to be lifted, turn pneumatic on | ||
if (clawIsLifted) { | ||
pfft.set(true); | ||
} else { | ||
// If not, turn pfft off | ||
pfft.set(false); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the claw motors are not moving (ie. if change in position is less | ||
* than a small delta value). | ||
* | ||
* @return true if stalled; false if moving | ||
*/ | ||
public boolean isMotorStalled() { | ||
double newRightPos = rightMotor.getSelectedSensorPosition(); | ||
double newLeftPos = leftMotor.getSelectedSensorPosition(); | ||
|
||
// If motors are barely changing position | ||
boolean isStalled = (Math.abs(newRightPos - rightPos) <= stallDelta) | ||
&& (Math.abs(newLeftPos - leftPos) <= stallDelta); | ||
|
||
// Save new motor positions | ||
rightPos = newRightPos; | ||
leftPos = newLeftPos; | ||
|
||
return isStalled; | ||
} | ||
|
||
@Override | ||
public void simulationPeriodic() { | ||
// This method will be called once per scheduler run during simulation | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just now realized that this opening logic may not be right; if we set the encoder position to 0 in the constructor of the clawSubsystem, then we don't know if at that point the claw is open/closed/halfway/whatever. Therefore we can't rely on a constant to check if the claw is open, because that might be different every time the code runs.