Session 2.2 Minimal “Time Based” Robot
Contents of the Robot.java file: (no other classes / “all in one”)
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.xrp.XRPMotor;
import edu.wpi.first.wpilibj.xrp.XRPRangefinder;
import edu.wpi.first.wpilibj.xrp.XRPServo;
import edu.wpi.first.wpilibj.XboxController;
/** The methods in this class are called automatically corresponding to each mode, as described in the TimedRobot documentation. */
public class Robot extends TimedRobot {
XRPMotor m_leftMotor = new XRPMotor(0);
XRPMotor m_rightMotor = new XRPMotor(1);
XRPServo m_armServo = new XRPServo(4); // Notice, port #4!!!!
XRPRangefinder myUltrasonicSensor = new XRPRangefinder(); // No port needed for this
XboxController myController = new XboxController(0);
/** This function is run when the robot is first started up and should be used for any initialization code. */
public Robot() {
m_leftMotor.setInverted(true);
}
/** This function is called periodically during operator control. About every 20ms */
@Override
public void teleopPeriodic() {
// Get the left and right stick values:
double leftValue = myController.getLeftY();
double rightValue = myController.getRightY();
// Pass those values to the motors as tank drive
m_leftMotor.set(leftValue);
m_rightMotor.set(rightValue);
// Control the arm:
if(myController.getAButtonReleased()){
m_armServo.setAngle(25);
}
if(myController.getBButtonReleased()){
m_armServo.setAngle(120);
}
// Get distance of the Rangefinder on-demand
if(myController.getXButtonReleased()){
double range = myUltrasonicSensor.getDistanceInches();
System.out.println("Object detected, it is "+range+" inches away.");
}
}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {
m_leftMotor.set(0);
m_rightMotor.set(0);
}
}
Keep in-mind that this design is not easily sustainable beyond a very simple and minimal robot. It would be difficult to extend this robot to have a good or flexible autonomous, and it could easily grow out of hand.
The recommended design pattern for competition FRC robots is the ‘Command Based’ framework, which we will go into in depth in the next lesson.
Published @ June 16, 2025 2:05 am