Skip to content

Commit

Permalink
Add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
KangarooKoala committed Nov 4, 2024
1 parent 3de3c3e commit 12d7af9
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 39 deletions.
1 change: 1 addition & 0 deletions networktables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
27 changes: 27 additions & 0 deletions src/main/java/frc/robot/Controls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package frc.robot;

import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.commands.example.ExampleSetSpeedCommand;

public class Controls {
public static final boolean EXAMPLE_ENABLED = false;
public static final int DRIVE_CONTROLLER_PORT = 0;

private final CommandXboxController driverController;

// Normally, more descriptive names are better, but in this case, we care
// about making the subsystem access easy
private final Subsystems s;

public Controls(Subsystems s) {
driverController = new CommandXboxController(DRIVE_CONTROLLER_PORT);
this.s = s;
if (EXAMPLE_ENABLED) {
bindExampleControls();
}
}

private void bindExampleControls() {
driverController.a().onTrue(new ExampleSetSpeedCommand(s.exampleSubsystem, 0.1));
}
}
27 changes: 8 additions & 19 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
package frc.robot;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;

public class Robot extends TimedRobot {
private Command m_autonomousCommand;
public final Subsystems subsystems;
@SuppressWarnings("unused")
private final Controls controls;

private RobotContainer m_robotContainer;

@Override
public void robotInit() {
m_robotContainer = new RobotContainer();
public Robot() {
this.subsystems = new Subsystems();
this.controls = new Controls(subsystems);
}

@Override
Expand All @@ -33,13 +32,7 @@ public void disabledPeriodic() {}
public void disabledExit() {}

@Override
public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();

if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
}
public void autonomousInit() {}

@Override
public void autonomousPeriodic() {}
Expand All @@ -48,11 +41,7 @@ public void autonomousPeriodic() {}
public void autonomousExit() {}

@Override
public void teleopInit() {
if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}
}
public void teleopInit() {}

@Override
public void teleopPeriodic() {}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/frc/robot/RobotContainer.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/java/frc/robot/Subsystems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package frc.robot;

import frc.robot.subsystems.ExampleSubsystem;

public class Subsystems {
public static final boolean EXAMPLE_ENABLED = false;

public final ExampleSubsystem exampleSubsystem;

public Subsystems() {
if (EXAMPLE_ENABLED) {
this.exampleSubsystem = new ExampleSubsystem();
} else {
this.exampleSubsystem = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package frc.robot.commands.example;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.ExampleSubsystem;

public class ExampleSetSpeedCommand extends Command {
private final ExampleSubsystem exampleSubsystem;
private final double speed;

public ExampleSetSpeedCommand(ExampleSubsystem exampleSubsystem, double speed) {
this.exampleSubsystem = exampleSubsystem;
this.speed = speed;
}

@Override
public void initialize() {
exampleSubsystem.setSpeed(speed);
}

@Override
public boolean isFinished() {
return MathUtil.isNear(speed, exampleSubsystem.getSpeed(), 0.5);
}
}
15 changes: 15 additions & 0 deletions src/main/java/frc/robot/dummy/Motor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package frc.robot.dummy;

public class Motor {
private double speed = 0;

public Motor(int id) {}

public void set(double value) {
this.speed = 0;
}

public double get() {
return this.speed;
}
}
41 changes: 41 additions & 0 deletions src/main/java/frc/robot/subsystems/ExampleSubsystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package frc.robot.subsystems;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.dummy.Motor;

public class ExampleSubsystem extends SubsystemBase {
// Constants
public static final double MAX_SPEED = 7;
public static final double GEAR_RATIO = 10;

// Member variables
private final Motor motor;

// Constructor
public ExampleSubsystem() {
this.motor = new Motor(1);
}

// Methods
public void setSpeed(double speed) {
motor.set(speed / GEAR_RATIO);
}

public void fullSpeed() {
motor.set(MAX_SPEED);
}

public void fullReverse() {
motor.set(-MAX_SPEED);
}

public double getSpeed() {
return motor.get() * GEAR_RATIO;
}

// Factories
public Command setSpeedCmd(double speed) {
return runOnce(() -> setSpeed(speed));
}
}

0 comments on commit 12d7af9

Please sign in to comment.