Skip to content

Instantly share code, notes, and snippets.

@crussell52
Last active January 29, 2022 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crussell52/3fba53d8a9c9e6dd49ee74c72d3101aa to your computer and use it in GitHub Desktop.
Save crussell52/3fba53d8a9c9e6dd49ee74c72d3101aa to your computer and use it in GitHub Desktop.
HumanInput module
// pseudo-code to describe a HumanInput class for the robot.
//
// The idea behind HumanInput is to isolate all logic that interprets human input
// into a single location. This allows Commands to extract an interpreted value
// to send into a subsystem instead of a raw value.
//
// All Input manipulation (filtering, smoothing, etc) becomes the responsibility of the
// HumanInput class instead of Commands or Subsystems. The benefit is that you can alter
// the manipulations without ever touching your Command or Subsystem code, reducing the
// chance of side effect.
class HumanInput {
// Input sources are injected via the constructor.
public HumanInput(Gamepad gp, BanksBlock bb) {
this.gamePad = gp
this.banksBlock = bb
}
// Get methods are introduced for extracting interpreted values.
public CurvatureDriveVal getCurvatureDriveVal() {
double power = Math.pow(gp.LeftStick().Vertical(), 3)
double angle = gamePad.RightStick().Horizontal()
bool qt = gamePad.Button1()
return new CurvatureDriveVal(power, angle, qt)
}
// Some methods are really simple, but we can make them complex
// later -- if we need to -- while keeping the risk of side effects low.
public double getIntakePower() {
return banksBlock.leftSlider() // Direct translation
}
}
// Somewhere else....
class DriveCommand extends CommandBase {
// This comand is influenced by human input, so inject it into the constructor.
// It operates the DriveTrain, so inject that also.
public DriveCommmand(HumanInput humanInput, DriveTrain driveTrain) {
this.humanInput = humanInput
this.driveTrain = driveTrain
// Take control of drive train
addRequirement(driveTrain)
}
public void execute() {
// The command focuses on orchestrating the robot operation. The Subsystem
// can stay focused on sending the control values to the components.
in = humanInput.getDriveCurvatureVal();
driveTrain.curvature(in.power(), in.angle(), in.isQuickTurn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment