-
Notifications
You must be signed in to change notification settings - Fork 2
Fix rewired arm encoders #128
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
Open
ErikCald
wants to merge
4
commits into
main
Choose a base branch
from
Erik-FixArmEncoders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
150 changes: 150 additions & 0 deletions
150
src/main/java/frc/robot/commands/SyncArmEncodersV2.java
This file contains hidden or 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,150 @@ | ||
| // 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; | ||
|
|
||
| import edu.wpi.first.wpilibj.DriverStation; | ||
| import edu.wpi.first.wpilibj.Timer; | ||
| import edu.wpi.first.wpilibj2.command.CommandBase; | ||
| import edu.wpi.first.wpilibj2.command.Commands; | ||
| import edu.wpi.first.wpilibj2.command.WaitCommand; | ||
| import frc.robot.config.ArmConfig; | ||
| import frc.robot.subsystems.ArmSubsystem; | ||
| import frc.robot.subsystems.BlingSubsystem; | ||
|
|
||
| public class SyncArmEncodersV2 extends CommandBase { | ||
| private Timer m_smallTimer = new Timer(); | ||
| private Timer m_permanantTimer = new Timer(); | ||
| private int m_commandState = 0; | ||
| private int m_numSamples = 0; | ||
| private double m_sumBotSamples = 0; | ||
| private double m_sumTopSamples = 0; | ||
|
|
||
| /** Creates a new SyncSteerEncoders. */ | ||
| public SyncArmEncodersV2() { | ||
| addRequirements(ArmSubsystem.getInstance()); | ||
| } | ||
|
|
||
| // Called when the command is initially scheduled. | ||
| @Override | ||
| public void initialize() { | ||
| m_smallTimer.restart(); | ||
| m_permanantTimer.restart(); | ||
| m_numSamples = 0; | ||
| m_sumBotSamples = 0; | ||
| m_sumTopSamples = 0; | ||
| m_commandState = 0; | ||
|
|
||
| BlingSubsystem.getINSTANCE().noEncodersSynced(); | ||
| } | ||
|
|
||
| // Called every time the scheduler runs while the command is scheduled. | ||
| @Override | ||
| public void execute() { | ||
| if (m_smallTimer.get() > ArmConfig.ENCODER_SYNCING_PERIOD_V2) { | ||
| m_smallTimer.reset(); | ||
|
|
||
| if (m_commandState == 0 && m_permanantTimer.hasElapsed(1)) { | ||
| DriverStation.reportWarning( | ||
| String.format("Arm encoders are not synced, collecting arm encoder samples... (%.1fs)", m_permanantTimer.get()), | ||
| false); | ||
|
|
||
| m_commandState = 1; | ||
|
|
||
| } else if (m_commandState == 1) { | ||
| m_sumBotSamples += ArmSubsystem.getInstance().getAbsoluteBottom(); | ||
| m_sumTopSamples += ArmSubsystem.getInstance().getAbsoluteTop(); | ||
| m_numSamples++; | ||
|
|
||
| if (m_numSamples >= ArmConfig.NUM_SYNCING_SAMPLES) { | ||
| m_commandState = 2; | ||
| } | ||
| } else if (m_commandState == 2) { | ||
| ArmSubsystem.getInstance().resetEncoder( | ||
| m_sumBotSamples / m_numSamples, | ||
| m_sumTopSamples / m_numSamples | ||
| ); | ||
|
|
||
| if (ArmSubsystem.getInstance().areEncodersSynced() == false) { | ||
| DriverStation.reportWarning( | ||
| String.format("Arm encoders are not synced, attempting to sync them... (%.1fs)", m_permanantTimer.get()), | ||
| false); | ||
|
|
||
| } else { | ||
| m_commandState = 99; // End the command, the encoders are synced | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Called once the command ends or is interrupted. | ||
| @Override | ||
| public void end(boolean interrupted) { | ||
| if (m_commandState == 99) { | ||
| DriverStation.reportWarning( | ||
| String.format("Arm encoders are synced (%.1f) \n", m_permanantTimer.get()), | ||
| false); | ||
|
|
||
| BlingSubsystem.getINSTANCE().armEncodersSynced(); | ||
|
|
||
| new WaitCommand(1).andThen( | ||
| Commands.runOnce(() -> BlingSubsystem.getINSTANCE().armEncodersSynced()), | ||
| new WaitCommand(1), | ||
| Commands.runOnce(() -> BlingSubsystem.getINSTANCE().armEncodersSynced()) | ||
| ).schedule(); | ||
|
|
||
| ArmSubsystem.getInstance().sparkMaxBurnFlash(); | ||
| } | ||
|
|
||
| m_permanantTimer.stop(); | ||
| m_smallTimer.stop(); | ||
|
|
||
| if ( | ||
| /** Temporary checks for current encoder setup. TODO: Remove once encoder wrapping is solved.*/ | ||
| ArmSubsystem.getInstance().getBottomPosition() < Math.toRadians(5) || | ||
| ArmSubsystem.getInstance().getBottomPosition() > Math.toRadians(170) || | ||
| ArmSubsystem.getInstance().getTopPosition() < Math.toRadians(5) || | ||
| ArmSubsystem.getInstance().getTopPosition() > Math.toRadians(70) || | ||
| ArmSubsystem.getInstance().areEncodersSynced() == false || | ||
| m_commandState != 99 | ||
| ) { | ||
|
|
||
| DriverStation.reportError("Arm encoders were reset outside the proper range. " + | ||
| " Disabling the arm to prevent damage." + | ||
| " Make sure the code boots up with the arm in the reset position.", true); | ||
|
|
||
| // This line below will prevent any commands from being scheduled to the ArmSubsystem. | ||
| // This line is temporary while we haven't properly setup the absolute encoders properly yet. | ||
| // If it's blocking you from using the arm, you just need to put the arm in the reset position | ||
| // and reboot the code (which will properly reset the NEO encoders the absolute encoders). | ||
| Commands.run(() -> ArmSubsystem.getInstance(), ArmSubsystem.getInstance()).withInterruptBehavior(InterruptionBehavior.kCancelIncoming).ignoringDisable(true).schedule(); | ||
| } | ||
| } | ||
|
|
||
| // Returns true when the command should end. | ||
| @Override | ||
| public boolean isFinished() { | ||
| if (m_permanantTimer.get() > ArmConfig.ENCODER_SYNCING_TIMEOUT) { | ||
| DriverStation.reportError( | ||
| String.format("Arm encoders are not synced. SyncArmEncodersV2 spent %.1fs trying to sync them and has timed out", | ||
| m_permanantTimer.get()), | ||
| false); | ||
|
|
||
| return true; | ||
| } | ||
| return m_commandState == 99; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean runsWhenDisabled() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public InterruptionBehavior getInterruptionBehavior() { | ||
| DriverStation.reportWarning("Another ArmSubsystem command was scheduled while " + | ||
| "SyncArmEncodersV2 is still running. Cancelling the incoming command.", false); | ||
| return InterruptionBehavior.kCancelIncoming; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.