Skip to content

Setting up an Auton

  • Lesson
  • 2 min read
  • 1 of 2 in this chapter

How to create a new Autonomous file

Once again, let's create a new OpMode. Create a new Java Class in Android Studio and a new OpMode in OnBot.

Start by naming the file "MyFirstAuton" and make sure "Not an OpMode" is selected and no option is checked at the bottom in OnBot. Make sure you do not delete the very first line of code.

Begin by typing the following code in your Autonomous code file:

Java
@Autonomous(name="MyFirstAuton", group="Linear Opmode")

Next, you are going to set your code and place it into a class that can be accessed later. This is done by typing this:

Java
public class MyFirstAuton extends LinearOpMode {
 // this space is here because this is where you will define your motors
 } 

We are also going to be using a Linear OpMode here, but the implementation is a tad different.

Setting up motors

Next, we are going to set what motors will be used on our robot and in our code. The motors we will be using are leftDrive and rightDrive. This is done by typing this:

Java
@Autonomous(name="MyFirstAuton", group="Linear Opmode")
 public class MyFirstAuton extends LinearOpMode {
 
 private ElapsedTime runtime = new ElapsedTime();
 private DcMotor leftDrive;
 private DcMotor rightDrive;
 
 // Make sure power is between -1 and 1
 double power = 1;

The power variable is set above and will serve to hold the motor power we will be running the robot's motors at.

Setting up the code to initialize our robot

Initialization is critical to code, even though the robot does not do anything yet. What initialization does is tell what motors should do based on the port and the wiring that is on the robot.

In order to set up the code to do this, type the following:

Java
 double power = 1; // from above
 
 @Override
 public void runOpMode() {
 
 //Initialization code goes here... 
 
 waitForStart(); // the robot initializes once the "INIT" button is pressed on the phone
 runtime.reset(); // this sets that timer variable we made earlier to 0, which is useful for autonomous programming
  //Autonomous robot instructions go here...
 }

Programming what the robot will do

Finally, we are now able to start giving our robot instructions on what to do during the Autonomous period. You have the Autonomous file all set up!