- Basic robot structure using
TimedRobot - Setting up a differential drive with two motors
- Autonomous mode with timed driving
- Teleoperated mode with joystick control
- Proper motor inversion for drivetrain
Complete Example
- Java
- C++
What This Example Demonstrates
Robot Structure
The robot extendsTimedRobot, which automatically calls methods at 20ms intervals:
autonomousInit()- Called once when autonomous startsautonomousPeriodic()- Called every 20ms during autonomousteleopPeriodic()- Called every 20ms during teleoperated mode
Differential Drive
TheDifferentialDrive class simplifies controlling a tank-style drivetrain:
- Takes two motor controllers (left and right)
- Provides
arcadeDrive()method for single-stick driving - Forward/backward on one axis, turning on another
Motor Inversion
One side of the drivetrain must be inverted so both sides drive forward with positive values. This is done withsetInverted(true) on the right motor.
Running in Simulation
- Deploy the code to your robot or simulator
- Enable autonomous mode to see the 2-second forward drive
- Switch to teleoperated mode and use an Xbox controller:
- Left stick Y-axis: forward/backward
- Right stick X-axis: turning
Source Location
- Java:
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/gettingstarted/Robot.java - C++:
wpilibcExamples/src/main/cpp/examples/GettingStarted/cpp/Robot.cpp