FTC Programming

Setting Up The File

The very beginning of the FTC code.

What is a TeleOp?

TeleOp is known as the driver control period of the match, which follows the autonomous section (we'll talk about autonomous later). The TeleOp section lasts for 2 minutes and 30 seconds and this is where a team can score as many points as possible, based on what the game objective is in a particular season.

How to create a new file?

Once you have gone onto OnBot on your computer, click on the "OnBotJava" tab at the top. Then you can start adding a new file by clicking the on the left-hand side. Here you can name your file whatever you see fit. Start by naming the file "MyFirstTeleOp" and make sure "Not an OpMode" is selected and no option is checked at the bottom. Delete all the code except the first line. Begin by typing the following code in your TeleOp code file:

@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")

The float variable is used to represent numbers that have a decimal place in them. It is similar to the int variable type, but can hold up to 7 digits of precision.

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

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", "rightDrive", "armMotor". This is done by typing this:

@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {
    private ElapsedTime runtime = new ElapsedTime(); // this makes a timer variable 
    private DcMotor leftDrive = null;
    private DcMotor rightDrive = null;
    private DcMotor armMotor = null;
   // note that all motors are equal to null, which basically means they don't do anything quite yet
}

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 what 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:

@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {

   private ElapsedTime runtime = new ElapsedTime();
   private DcMotor leftDrive = null;
   private DcMotor rightDrive = null;
   private DcMotor armMotor = null;
   
   @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
   }
}

Programming what the robot will do

Finally, we are now able to start giving our robot instructions on what to do when something happens on the gamepad. To start out the section of this part of the code, type this:

@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {

   private ElapsedTime runtime = new ElapsedTime(); // this makes a timer variable
   private DcMotor leftDrive = null;
   private DcMotor rightDrive = null;
   private DcMotor armMotor = null;
   
   @Override
   public void runOpMode() {
   
      // initialization code goes here
      
      waitForStart();
      runtime.reset();
      
      while (opModeIsActive()) {
          // code goes here 
      }
   }
}

Notice how there are 3 chttps://stempump.com/resources/course/page.php?course=FTC%20Programming&id=0 braces '}' at the end of the code. This is very important that you do not have more or less than what is required for the code to function properly. Most errors in code are due to a misplaced chttps://stempump.com/resources/course/page.php?course=FTC%20Programming&id=0 brace. Just keep in mind that everytime there is an opening chttps://stempump.com/resources/course/page.php?course=FTC%20Programming&id=0 brace '{', a closing one must match with it.

chevron_up