From 072487531543e2f31793ddaf649b4df267baf6a8 Mon Sep 17 00:00:00 2001 From: Borui Su Date: Sat, 20 Sep 2025 12:49:10 -0700 Subject: [PATCH 1/2] Complete indexer command task --- tasks/lesson2-indexer-task/README.md | 2 +- tasks/lesson2-indexer-task/simgui-ds.json | 13 +++++++++---- .../main/java/frc/robot/RobotContainer.java | 8 ++++++++ .../java/frc/robot/subsystems/Indexer.java | 18 +++++++++++++++--- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/tasks/lesson2-indexer-task/README.md b/tasks/lesson2-indexer-task/README.md index dca26ae..724d2fb 100644 --- a/tasks/lesson2-indexer-task/README.md +++ b/tasks/lesson2-indexer-task/README.md @@ -41,7 +41,7 @@ This should return a command that: - Bind `indexForSeconds(1.5)` to button 1 on the joystick -## 4. Run and test in simulation +## 4. Run and test in simu8lation - Start your robot in simulation - Switch to teleoperated mode diff --git a/tasks/lesson2-indexer-task/simgui-ds.json b/tasks/lesson2-indexer-task/simgui-ds.json index 69b1a3c..2cec2ca 100644 --- a/tasks/lesson2-indexer-task/simgui-ds.json +++ b/tasks/lesson2-indexer-task/simgui-ds.json @@ -1,4 +1,9 @@ { + "Keyboard 0 Settings": { + "window": { + "visible": true + } + }, "keyboardJoysticks": [ { "axisConfig": [ @@ -20,10 +25,10 @@ "axisCount": 3, "buttonCount": 4, "buttonKeys": [ - 90, - 88, - 67, - 86 + 49, + 50, + 51, + 52 ], "povConfig": [ { diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java index 1500ca0..682d21f 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java @@ -5,6 +5,7 @@ package frc.robot; import edu.wpi.first.wpilibj2.command.Command; +import edu.wpi.first.wpilibj2.command.InstantCommand; import edu.wpi.first.wpilibj2.command.button.CommandJoystick; import frc.robot.subsystems.Indexer; @@ -14,6 +15,7 @@ public class RobotContainer { // Controller private final CommandJoystick joystick = new CommandJoystick(0); + /** The container for the robot. Contains subsystems, OI devices, and commands. */ public RobotContainer() { @@ -22,6 +24,12 @@ public RobotContainer() { private void configureBindings() { // TODO: Bind indexForSeconds(1.5) to joystick button 1 + joystick.button(1).onTrue( + new InstantCommand(() -> indexer.indexForSeconds(1.5)) + ); + + //joystick.button(1).onTrue(indexer.indexForSeconds(1.5)); + } public Command getAutonomousCommand() { diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java index e89f0e5..dffdd12 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java @@ -8,18 +8,20 @@ public class Indexer extends SubsystemBase { // TODO: Add indexing state variable here - + boolean indexing = false; /** Creates a new Indexer subsystem. */ public Indexer() {} /** Starts the indexing process. */ public void startIndexing() { // TODO: Set indexing state to true + indexing = true; } /** Stops the indexing process. */ public void stopIndexing() { // TODO: Set indexing state to false + indexing = false; } /** @@ -29,11 +31,21 @@ public void stopIndexing() { */ public boolean isIndexing() { // TODO: Return indexing state - return false; + return indexing; } // TODO: Implement indexForSeconds() command factory - + public void indexForSeconds(double seconds) { + this.startIndexing(); + int iseconds = (int)seconds; + try { + Thread.sleep((long)(seconds * 1000)); + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } + this.stopIndexing(); + + } @Override public void periodic() { // This method will always be called once per scheduler run From e612b120f98dc18ac4070d681f6cbc07688922fd Mon Sep 17 00:00:00 2001 From: Borui Su Date: Sat, 20 Sep 2025 16:54:04 -0700 Subject: [PATCH 2/2] fixed lesson 2 task --- .../main/java/frc/robot/RobotContainer.java | 7 +++---- .../java/frc/robot/subsystems/Indexer.java | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java index 682d21f..441bdac 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/RobotContainer.java @@ -15,7 +15,7 @@ public class RobotContainer { // Controller private final CommandJoystick joystick = new CommandJoystick(0); - + /** The container for the robot. Contains subsystems, OI devices, and commands. */ public RobotContainer() { @@ -24,9 +24,8 @@ public RobotContainer() { private void configureBindings() { // TODO: Bind indexForSeconds(1.5) to joystick button 1 - joystick.button(1).onTrue( - new InstantCommand(() -> indexer.indexForSeconds(1.5)) - ); + joystick.button(1).onTrue(indexer.indexForSeconds(1.5)); + //joystick.button(1).onTrue(indexer.indexForSeconds(1.5)); diff --git a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java index dffdd12..0faf005 100644 --- a/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java +++ b/tasks/lesson2-indexer-task/src/main/java/frc/robot/subsystems/Indexer.java @@ -4,7 +4,9 @@ package frc.robot.subsystems; +import edu.wpi.first.wpilibj2.command.Commands; import edu.wpi.first.wpilibj2.command.SubsystemBase; +import edu.wpi.first.wpilibj2.command.Command; public class Indexer extends SubsystemBase { // TODO: Add indexing state variable here @@ -35,17 +37,18 @@ public boolean isIndexing() { } // TODO: Implement indexForSeconds() command factory - public void indexForSeconds(double seconds) { - this.startIndexing(); - int iseconds = (int)seconds; - try { - Thread.sleep((long)(seconds * 1000)); - } catch (InterruptedException e){ - Thread.currentThread().interrupt(); - } - this.stopIndexing(); + + public Command indexForSeconds(double seconds) { + + return Commands.runOnce(this::startIndexing, this) + + .andThen(Commands.waitSeconds(1.5)) + .andThen(Commands.runOnce(this::stopIndexing, this)); + + } + @Override public void periodic() { // This method will always be called once per scheduler run