Skip to content

Commit

Permalink
Add test and fix reset
Browse files Browse the repository at this point in the history
  • Loading branch information
sciencewhiz committed Jan 6, 2023
1 parent 57fa451 commit 1f94532
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public double getSetpoint() {
* @return Whether the error is within the acceptable bounds.
*/
public boolean atSetpoint() {
return m_haveMeasurement && m_haveSetpoint
return m_haveMeasurement
&& m_haveSetpoint
&& Math.abs(m_positionError) < m_positionTolerance
&& Math.abs(m_velocityError) < m_velocityTolerance;
}
Expand Down Expand Up @@ -366,6 +367,7 @@ public void reset() {
m_prevError = 0;
m_totalError = 0;
m_velocityError = 0;
m_haveMeasurement = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package edu.wpi.first.math.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,4 +57,16 @@ void derivativeGainOutputTest() {

assertEquals(-0.01 / m_controller.getPeriod(), m_controller.calculate(0.0025, 0), 1e-5);
}

@Test
void atSetpointTest() {
m_controller.reset();
assertFalse(m_controller.atSetpoint());
m_controller.setTolerance(2);
assertFalse(m_controller.atSetpoint());
m_controller.setSetpoint(0);
assertFalse(m_controller.atSetpoint());
m_controller.calculate(0);
assertTrue(m_controller.atSetpoint());
}
}

0 comments on commit 1f94532

Please sign in to comment.