Skip to content

Instantly share code, notes, and snippets.

@tervay
Created January 31, 2018 16:29
Show Gist options
  • Save tervay/192c0b5d2ce6a449cf7fef377b9078dc to your computer and use it in GitHub Desktop.
Save tervay/192c0b5d2ce6a449cf7fef377b9078dc to your computer and use it in GitHub Desktop.
public interface Subsystem {
/**
* Called once when the robot enters the disabled state.
*/
void initDisabled();
/**
* Called once when the robot enters the teleop state.
*/
void initTeleop();
/**
* Called roughly every 20ms, or at 50Hz, while in the disabled state.
*/
void runDisabled();
/**
* Called roughly every 20ms, or at 50Hz, while in the teleop state.
*/
void runTeleop();
// The display method isn't necessary, but I like it. To each their own.
// You should call this in your robots periodic methods. Or not. Whatever you want.
// I use this so that the other functional code doesn't get overly cluttered with
// print statements, but you will likely need them anyway for debugging.
/**
* Called to display things via the Dashboard or via print commands. Not necessary to use.
*/
void display();
}
// Example usage
public class DriveTrain implements Subsystem {
private Talon left, right;
public DriveTrain() {
// The ports under the Constants file are just where the Talons (or whatever speed controller)
// are plugged into the RoboRIO.
left = new Talon(Constants.DT_LEFT_PORT);
right = new Talon(Constants.DT_RIGHT_PORT);
}
// well, don't drive in disabled. :)
public void initDisabled() {
left.set(0.0);
right.set(0.0);
}
public void initTeleop() {
// don't need to do anything here
}
public void runDisabled() {
// don't need to do anything here
}
public void runTeleop() {
// Controls can be whatever you want them to be. This is just an example.
double forwardSpeed = Robot.joystick.getLeftJoystickY(); // between -1 and 1
double turnRightValue = Robot.joystick.getRightTrigger(); // between 0 and 1
double turnLeftValue = Robot.joystick.getLeftTrigger(); // between 0 and 1
// I will warn you that these signs may be off. you may need to adjust these if you use this.
// Again, this is example code, and you should write your own controls to your driver's preference!
left.set(clamp(forwardSpeed - turnRightValue, -1.0, 1.0));
right.set(clamp(forwardSpeed - turnLeftValue, -1.0, 1.0));
}
/**
* Clamps a value to a certain range.
*/
private static double clamp(double value, double minLimit, double maxLimit) {
return Math.max(minLimit, Math.min(value, maxLimit));
}
public void display() {
SmartDashboard.putNumber("DT Left Speed", left.get());
SmartDashboard.putNumber("DT Right Speed", right.get());
}
}
public class Robot extends IterativeRobot() {
// Called once when the robot turns on
public void disabledInit() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment