Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
fix encoder offset
Browse files Browse the repository at this point in the history
  • Loading branch information
jack60612 committed Feb 28, 2024
1 parent 6dc7570 commit e570248
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/frc/robot/subsystems/ArmSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class ArmSubsystem extends SubsystemBase {
private boolean m_frontLimitState = false;
private final Debouncer m_frontLimitDebouncer = new Debouncer(0.2, Debouncer.DebounceType.kBoth);

// offset to match the absolute encoder with the main encoder
private double m_curOffset = 0.0;

/** Creates a new ArmSubsystem. */
public ArmSubsystem(ShooterState shooterState) {
m_shooterState = shooterState;
Expand Down Expand Up @@ -192,8 +195,9 @@ public void MoveArmToPosition(double radians) {
final_radians = Math.min(final_radians, kMaxArmAngleRadians);
if (final_radians != m_goal.position) {
m_newGoal = true;
resetOffset();
}
m_goal = new TrapezoidProfile.State(radians, 0); // set the goal
m_goal = new TrapezoidProfile.State(radians + m_curOffset, 0); // set the goal
}

/*
Expand All @@ -215,6 +219,14 @@ public void matchEncoders() {
m_MainEncoder.setPosition(m_AbsoluteEncoder.getPosition());
}

private void SetOffsetWithEncoder() {
m_curOffset = getError();
}

private void resetOffset() {
m_curOffset = 0.0;
}

/**
* Calculates the error between the position reported by the absolute encoder and the main
* encoder.
Expand Down Expand Up @@ -264,7 +276,7 @@ public void periodic() {
m_newGoal = false; // reset the new goal flag, so that we dont try resyncing encoders again
double cur_error = getError();
if (!HelperFunctions.inDeadzone(cur_error, Units.degreesToRadians(3))) {
matchEncoders();
SetOffsetWithEncoder();
}
}
}
Expand Down

0 comments on commit e570248

Please sign in to comment.