This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Joystick deadzones and toggle condition (#144)
Co-authored-by: Ryan Cahoon <[email protected]>
- Loading branch information
1 parent
984cc28
commit c1bde3b
Showing
10 changed files
with
240 additions
and
27 deletions.
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
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
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
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,46 @@ | ||
package com.team766.framework3; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.function.BooleanSupplier; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ConditionsTest { | ||
private static class ValueProxy implements BooleanSupplier { | ||
boolean value = false; | ||
|
||
public boolean getAsBoolean() { | ||
return value; | ||
} | ||
} | ||
|
||
@Test | ||
public void testToggle() { | ||
var v = new ValueProxy(); | ||
|
||
var t = new Conditions.Toggle(v); | ||
|
||
assertFalse(t.getAsBoolean()); | ||
assertFalse(t.getAsBoolean()); | ||
|
||
v.value = true; | ||
|
||
assertTrue(t.getAsBoolean()); | ||
assertTrue(t.getAsBoolean()); | ||
|
||
v.value = false; | ||
|
||
assertTrue(t.getAsBoolean()); | ||
assertTrue(t.getAsBoolean()); | ||
|
||
v.value = true; | ||
|
||
assertFalse(t.getAsBoolean()); | ||
assertFalse(t.getAsBoolean()); | ||
|
||
v.value = false; | ||
|
||
assertFalse(t.getAsBoolean()); | ||
assertFalse(t.getAsBoolean()); | ||
} | ||
} |
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,52 @@ | ||
package com.team766.hal; | ||
|
||
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 com.team766.TestCase3; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public abstract class JoystickAbstractTest extends TestCase3 { | ||
protected JoystickReader joystick; | ||
|
||
protected abstract void setAxis(int axis, double value); | ||
|
||
@Test | ||
public void testDeadzone() { | ||
// Deadzone should start at 0.0, so the condition should be true even if the value is 0. | ||
setAxis(0, 0.0); | ||
setAxis(1, 0.0); | ||
assertEquals(0.0, joystick.getAxis(0)); | ||
assertEquals(0.0, joystick.getAxis(1)); | ||
assertTrue(joystick.isAxisMoved(0)); | ||
assertTrue(joystick.isAxisMoved(1)); | ||
|
||
// Same result if the deadzone is explicitly set to 0.0. | ||
joystick.setAllAxisDeadzone(0.0); | ||
assertTrue(joystick.isAxisMoved(0)); | ||
assertTrue(joystick.isAxisMoved(1)); | ||
|
||
// Test with the deadzone larger than the axis values. | ||
joystick.setAllAxisDeadzone(0.6); | ||
assertFalse(joystick.isAxisMoved(0)); | ||
assertFalse(joystick.isAxisMoved(1)); | ||
|
||
// Calling setAxisDeadzone after setAllAxisDeadzone should set the deadzone for that axis | ||
// but maintain the deadzone for all other axes. | ||
setAxis(0, 0.5); | ||
setAxis(1, 0.3); | ||
joystick.setAxisDeadzone(1, 0.2); | ||
assertEquals(0.0, joystick.getAxis(0)); | ||
assertEquals(0.3, joystick.getAxis(1)); | ||
assertFalse(joystick.isAxisMoved(0)); | ||
assertTrue(joystick.isAxisMoved(1)); | ||
|
||
// Calling setAllAxisDeadzone should override previously-set per-axis deadzones. | ||
joystick.setAllAxisDeadzone(0.5); | ||
assertEquals(0.5, joystick.getAxis(0)); | ||
assertEquals(0.0, joystick.getAxis(1)); | ||
assertTrue(joystick.isAxisMoved(0)); | ||
assertFalse(joystick.isAxisMoved(1)); | ||
} | ||
} |
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,18 @@ | ||
package com.team766.hal.mock; | ||
|
||
import com.team766.hal.JoystickAbstractTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
public class MockJoystickTest extends JoystickAbstractTest { | ||
private MockJoystick driver; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
joystick = driver = new MockJoystick(); | ||
} | ||
|
||
@Override | ||
protected void setAxis(int axis, double value) { | ||
driver.setAxisValue(axis, value); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/com/team766/hal/wpilib/WpilibJoystickTest.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,24 @@ | ||
package com.team766.hal.wpilib; | ||
|
||
import com.team766.hal.JoystickAbstractTest; | ||
import edu.wpi.first.wpilibj.simulation.JoystickSim; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
|
||
// TODO: AdvantageKit breaks this test. Try re-enabling this after upgrade to 2025. | ||
@Disabled | ||
public class WpilibJoystickTest extends JoystickAbstractTest { | ||
private JoystickSim driver; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
joystick = new Joystick(0); | ||
driver = new JoystickSim(0); | ||
} | ||
|
||
@Override | ||
protected void setAxis(int axis, double value) { | ||
driver.setRawAxis(axis, value); | ||
updateDriverStationData(); | ||
} | ||
} |