Skip to content

Commit

Permalink
used suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
Advay17 committed Sep 15, 2024
1 parent 4c76958 commit 622f04e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
}
},
],
"java.test.defaultConfig": "WPIlibUnitTests"
"java.test.defaultConfig": "WPIlibUnitTests",
"java.debug.settings.onBuildFailureProceed": true
}
21 changes: 21 additions & 0 deletions simgui-ds.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,26 @@
"buttonCount": 0,
"povCount": 0
}
],
"robotJoysticks": [
{
"guid": "78696e70757401000000000000000000",
"useGamepad": true
},
{
"useGamepad": true
},
{
"useGamepad": true
},
{
"useGamepad": true
},
{
"useGamepad": true
},
{
"useGamepad": true
}
]
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;

/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
Expand Down Expand Up @@ -57,7 +58,6 @@ public void disabledPeriodic() {}
@Override
public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();

// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RobotContainer {
// The robot's subsystems and commands are defined here...
public static final XRPDrivetrain m_xrpDrivetrain = new XRPDrivetrain();
public static final Servo m_servo = new Servo();
private final CommandXboxController m_controller = new CommandXboxController(0);
public final CommandXboxController m_controller = new CommandXboxController(0);



Expand All @@ -33,7 +33,9 @@ public RobotContainer() {
configureButtonBindings();
//The default command is the command that executes if no other command is using the specified subsystem.
//This causes the xrp to drive based on the left joystick y and right joystick x of the controller by default.
m_xrpDrivetrain.setDefaultCommand(DriveCommands.arcadeDriveCommand(m_controller.getLeftY(), m_controller.getRightX()));
//It uses lambdas to make the parameters suppliers, which means that instead of them being constant, they rerun each time the command is scheduled.
//The values are negated because the axes are flipped for some reason.
m_xrpDrivetrain.setDefaultCommand(DriveCommands.arcadeDriveCommand(()-> -m_controller.getLeftY(), () -> -m_controller.getRightX()));
//TODO: Task 4-Comment out the above line by adding // to the left of it. Then, set the default command to be your tankDriveCommand.
//HINT: In tank drive, the left wheel is controlled by the y axis of the left joystick and the y axis of the right joystick.
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/frc/robot/commands/DriveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import frc.robot.Robot;
import frc.robot.RobotContainer;
import frc.robot.subsystems.XRPDrivetrain;

import java.util.function.Supplier;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.FunctionalCommand;
import edu.wpi.first.wpilibj2.command.InstantCommand;
Expand Down Expand Up @@ -121,17 +124,21 @@ public boolean isFinished() {

/**
* A command that sets the speed of the robot based on an arcade control scheme, using an Instant Command.
* @param forwardSpeed Speed the robot goes forward.
* @param turnSpeed Speed the robot turns.
* Because this value is something that changes based on controller input, we want the values to be rechecked periodically.
* For this, we use what is called a supplier, which is any function which returns a double.
* By using a supplier, instead of the drive speed being a constant 0, the command instead runs the supplier each time it runs to determine the correct speed.
* @param forwardSpeed Speed the robot goes forward. Is a supplier, and therefore must be a method or a lambda.
* @param turnSpeed Speed the robot turns. Is a supplier, and therefore must be a method or a lambda.
* @return Command to arcade drive the XRP
*/
public static Command arcadeDriveCommand(double forwardSpeed, double turnSpeed){
public static Command arcadeDriveCommand(Supplier<Double> forwardSpeed, Supplier<Double> turnSpeed){
System.out.println("runs");
//This uses an InstantCommand, which shouldn't be a class. An Instant Command immediately executes, and only takes in fields for what it should do
//and the required subsystems.
//Useful for simple commands.
return new InstantCommand(
//Tells the XRP to drive at the given speeds
()->RobotContainer.m_xrpDrivetrain.arcadeDrive(forwardSpeed, turnSpeed),
//Tells the XRP to drive at the given speeds by using .get(), which gets the value returned by the supplier
()->RobotContainer.m_xrpDrivetrain.arcadeDrive(forwardSpeed.get(), turnSpeed.get()),
RobotContainer.m_xrpDrivetrain
);
}
Expand Down

0 comments on commit 622f04e

Please sign in to comment.