Challenge- Create a DriveTrain!

As the challenge name suggests, you'll be creating your own DriveTrain! It is a tank drive subsystem for simplicity's sake, and shouldn't be too difficult to code.

Note: you still need to make your Commands and bind it through Robot Container later. You won't

Write a GetInstance() Method

Your code should:

Sample Code + Explanation
/* class header */
/* This is what I alluded to in Hint #1. */
private static TankDriveSubsystem subsystem;

/* this should only run once- for creating the subsystem object above. */
public TankDriveSubsystem() {
    /* [code] */
}

/* Returns a DriveSubsystem instance. */
public static TankDriveSubsystem getInstance() {
    if (subsystem != null) {
      subsystem = new TankDriveSubsystem(); /* create the aforementioned instance */
    }
    
    return subsystem;
}

Write a moveRobot() Method

Now that you learned about how to set motors, at the bottom of the RomiDrivetrain class, make a helper method called moveRobot().

Conditions:

For the last point, although you can use math.abs() or a bunch of if-statements, there's another way we can do this, but using one of the built-in libraries in WPILIB VSC.

WPILIB's MathUtil class has a clamp method, clamp(number, lowestBound, highestBound).

Whatever's in the number parameter will never be smaller than the lowestBound and greater than the highestBound.

For example, if I were to do int num = MathUtil.clamp(3, 4, 6), num will be some value between 4 and 6 inclusively.

Make sure to import it! (Put this in your class header: import edu.wpi.first.math.MathUtil)

Once you have what you believe to be your answer, compare your code with the answer code below.

Answer Code
/* make sure that you import MathUtil!!!
*/
import edu.wpi.first.math.MathUtil;

// Moves the Romi robot.
public void moveRobot(double lSpeed, double rSpeed){
  double checkedLSpeed = MathUtil.clamp(lSpeed, -1, 1);
  double checkedLSpeed = MathUtil.clamp(rSpeed, -1, 1);
  
  m_leftMotor.set(checkedLSpeed);
  m_rightMotor.set(checkedRSpeed);
}
Alternate Answer Code
// Moves the Romi robot.
public void moveRobot(double lSpeed, double rSpeed){
  double checkedLSpeed = lSpeed;
  double checkedRSpeed = rSpeed;
  
  /* look at how cleaner the other one is!!!!!! */
  if (lSpeed > 1) {
    lSpeed = 1;
  
  } else if (lSpeed < -1) {
    lSpeed = -1;
  }
  
  if (rSpeed > 1) {
    rSpeed = 1;
  
  } else if (rSpeed < -1) {
    rSpeed = -1;
    
  }
  
  
  m_leftMotor.set(checkedLSpeed);
  m_rightMotor.set(checkedRSpeed);
}

Last updated