Challenge- Create a MoveDistance Command
Challenge: The moveDistance() Method
moveDistance() MethodNow that you know how to use an Encoder, let's make a new method called moveDistance at the bottom of the XRPDrivetrain class. Remember when I said we'll use the MoveRobot command for later?
Conditions:
Answer Code
/*Example moveDistance method. This is a subsystem version. Imagine this is
a chunk from the XRPDrivetrain. */
private double targetDistance = 0;
private double averageDistance = 0;
// filler things
public void setDistanceTarget(double newDistance) {
targetDistance = newDistance;
}
// code that you want
public void periodic() {
averageDistance =
(Math.abs(m_leftEncoder.getDistance()) +
Math.abs(m_rightEncoder.getDistance())) / 2.0;
if (averageDistance < targetDistance) {
moveMotors(lSpeed, rSpeed);
} else {
moveMotors(0, 0);
/* if you want to, you can create a debounce variable
* that gets activated if you call the moveDistance() method, and
* resets the speed.
*/
}
}Answer Code (alternate)
/*Example moveDistance() method. This is a command version. */
private double distance = 0;
private RomiDrivetrain subsystem = RomiDrivetrain:GetInstance()
// filler things
// [stuff]
public void execute() {
// getRightDistanceInch() works too
if (subsystem.getLeftDistanceInch() < distance) {
moveMotors(lSpeed, rSpeed);
} else {
moveMotors(0, 0);
/* if you want to, you can create a debounce variable
* that gets activated if you call the moveDistance() method, and
* resets the speed (google up debounce variable)
*/
}
}
public void isFinished() {
// once this returns true, this returns true, making
// command scheduler end the command
return (subsystem.getLeftDistanceInch() >= distance);
}
Last updated