FTC programming contains lots of concepts that are very specific to robotics and non regular programming. In this tutorial I plan to cover some of those concepts and basics. This tutorial volition show how to create bones OpModes for Teleop. I'll assume you have some bones knowledge of the FTC hardware.

Table of Contents

  • Table of Contents
  • Getting Prepare
  • Creating your first OpMode
  • Actually Doing Stuff with Teleop
  • Joysticks

Getting Ready Upwards

Y'all should have Android Studio and the FTC SDK downloaded. Here is a good tutorial on how to practice that. Open up the SDK within Android Studio, You may meet a notification like this:

Update Gradle

Click Update to make certain you're on the latest version. Another error may pop up similar below. Yous should click the links for Update Build Tools.

Update Build Tools

A status bar similar below shows the status of your programme compiling or uploading to the telephone.

Android Studio Status Bar

Next you should disable Instant Run in Android Studio because sometimes it stops new OpModes from showing upwards on the phones, and occasionally finish OpModes from updating with new lawmaking. Get to File->Settings in the toolbar and search for instant run and make certain it'south unchecked.

Android Studio Instant Run

Creating your first OpMode

The SDK FIRST developed is a library of code that you lot tin can use to control your robot. Basically information technology'southward just a folder with many files that accept code inside of them.

On the left in Android Studio there is a listing of the files inside of the SDK. It's similar to a file manager, but specifically for Android lawmaking. The OpModes you'll write become inside of TeamCode -> java -> org.firstinspires.ftc.teamcode. To create a new OpMode right click on the org.firstinspires.ftc.teamcode folder and select new -> Java Form. A window should pop upwardly, only all you'll need to enter is the proper noun of your OpMode. I chose MyFirstOpMode. A file with something similar to the following lawmaking should show upwards:

                                                    bundle                  org.firstinspires.ftc.teamcode                  ;                  /**                                      * Created by nicholas on ix/5/17.                                      */                  public                  class                  MyFirstOpMode                  {                  }                              

Within the SDK, Beginning made a class called OpMode. Classes are blueprints, and objects are new things created from those blueprint templates. Inside of the OpMode class are functions and variables that you can use to run your lawmaking on the robot. To utilize these functions and variables FIRST made, we extend from Starting time's OpMode class. This ways that our MyFirstOpMode class has all of the stuff Get-go's OpMode has, and our new stuff. An important thing to know is that the stuff in our class won't modify the stuff in Get-go'due south pattern. extend copies the stuff from Commencement's class blueprint into our blueprint. We use extend like this:

                                                    public                  class                  MyFirstOpMode                  extends                  OpMode                  {                  }                              

A red line like a grammar error should prove upwardly under the public form… line. This means that Android Studio has plant an error in our program. Android Studio doesn't grab all errors, merely only ones that finish the program from running in the outset place. It doesn't catch errors in developer logic :(

If you lot hover over red lines, you should encounter the fault. In this case, the fault is Course 'MyFirstOpMode' must either exist declared abstract or abstract method 'loop()' in 'OpMode'. Don't worry! Errors are hard to read, only you'll learn how. Basically information technology's saying that your form needs to implement the loop() method for this to work. Allow's implement that:

                                                    public                  class                  MyFirstOpMode                  extends                  OpMode                  {                  public                  void                  loop                  ()                  {                  }                  }                              

Now there should be a new error, simply this fourth dimension it mentions implement and init(). We know what that ways!

                                                    public                  form                  MyFirstOpMode                  extends                  OpMode                  {                  public                  void                  init                  ()                  {                  }                  public                  void                  loop                  ()                  {                  }                  }                              

Now no reddish underlines should bear witness upwards. Corking job reader! :)

Really Doing Stuff with Teleop

How exercise we actually do things Nick? I want to command my robot… And so, let'southward work on Teleop get-go. To tell the SDK that this OpMode is a Teleop OpMode and not an Autonomous OpMode, we put @TeleOp higher up the class like so:

                                                    @TeleOp                  public                  form                  MyFirstOpMode                  extends                  OpMode                  {                  public                  void                  init                  ()                  {                  }                  public                  void                  loop                  ()                  {                  }                  }                              

The mode OpMode works is that offset the code within init() is run. So, the lawmaking within of loop() is run over and over endlessly until you press stop on the Commuter Station or the autonomous menstruum ends.

Let's write our Teleop for a tank drive robot with 1 motor on each side. In the code, we'll name them left and right. We create ii variables with the type DcMotor inside the MyFirstOpMode form, but within of whatsoever functions. You put them inside of the course rather than the functions because when a variable is created within of the role it is removed when the lawmaking inside of the office stops running, and it can't be accessed by other code exterior of the function. Variables inside of the grade can be accessed inside of whatever function and will be removed when the OpMode finishes. As y'all already know, we desire to create the variables left and right within of the grade and then that any code inside of the OpMode tin access the motors.

The type of a variable is what the variable is. For instance there is an int type for integers, a double type for decimal numbers, a Money type for monetary values, etc.

                                                    @TeleOp                  public                  form                  MyFirstOpMode                  extends                  OpMode                  {                  DcMotor                  left                  ;                  DcMotor                  right                  ;                  public                  void                  init                  ()                  {                  }                  public                  void                  loop                  ()                  {                  }                  }                              

Inside of init(), we want to tell the plan that this motor variable corresponds to that motor configured on the robot, and this other motor variable corresponds to that other motor configured on the robot. We practise this by setting the variables equal to hardwareMap.dcMotor.get("Name of motor configured on phones");.

                                                    @TeleOp                  public                  class                  MyFirstOpMode                  extends                  OpMode                  {                  DcMotor                  left                  ;                  DcMotor                  right                  ;                  public                  void                  init                  ()                  {                  left                  =                  hardwareMap                  .                  dcMotor                  .                  get                  (                  "left"                  );                  correct                  =                  hardwareMap                  .                  dcMotor                  .                  get                  (                  "right"                  );                  }                  public                  void                  loop                  ()                  {                  }                  }                              

Now we become to the fun stuff. :) Actually moving the robot. Variables with the type DcMotor are objects created from the DcMotor grade that take functions that can operate the motors. We volition employ motorname.setPower() to change the power of the motors. motorname.setPower() takes an input from -1 to 1. -1 is full speed in reverse, ane is total speed frontward, .5 is half speed forward, -.25 is quarter speed backwards, etc. An important thing to know is that motors keep going at the aforementioned speed you last told them to get at. Then what nosotros can do is within of loop() set the motor powers over and over at different speeds to control the robot.

                                                    @TeleOp                  public                  grade                  MyFirstOpMode                  extends                  OpMode                  {                  DcMotor                  left                  ;                  DcMotor                  right                  ;                  public                  void                  init                  ()                  {                  left                  =                  hardwareMap                  .                  dcMotor                  .                  go                  (                  "left"                  );                  right                  =                  hardwareMap                  .                  dcMotor                  .                  get                  (                  "right"                  );                  }                  public                  void                  loop                  ()                  {                  right                  .                  setPower                  (                  i                  );                  left                  .                  setPower                  (-                  one                  );                  }                  }                              

Nosotros set the left motor to -one because it is flipped over when compared to the right motor, then going reverse on the left actually moves the robot forward. If you run this code, your robot should go forward, but we can't control information technology. :( For this we need joysticks.

Joysticks

In FTC, you can use ii gamepads continued to the Driver Station. Inside of your OpMode, those gamepads are variables named gamepad1 and gamepad2 with the type of the Gamepad class. The Gamepad class has variables on information technology which correspond to whether a button is pressed and the joysticks. The Gamepad joystick variables are gamepad1.left_stick_x, gamepad1.left_stick_y, gamepad.right_stick_x, and gamepad.right_stick_y. Those variables are also on the second gamepad. They are decimal numbers (doubles) which range from -1 to 1 and correspond to either the x axis or the y centrality of the joystick movement.

For a bones tank drive, we will have the y centrality on the left and correct sticks of gamepad1 directly control the speed of the motors since motor.setPower() takes an input from -ane to 1 and the gamepads output values from -1 to 1.

                                                    @TeleOp                  public                  class                  MyFirstOpMode                  extends                  OpMode                  {                  DcMotor                  left                  ;                  DcMotor                  right                  ;                  public                  void                  init                  ()                  {                  left                  =                  hardwareMap                  .                  dcMotor                  .                  get                  (                  "left"                  );                  correct                  =                  hardwareMap                  .                  dcMotor                  .                  get                  (                  "right"                  );                  }                  public                  void                  loop                  ()                  {                  right                  .                  setPower                  (                  gamepad1                  .                  right_stick_y                  );                  left                  .                  setPower                  (-                  gamepad1                  .                  left_stick_y                  );                  }                  }                              

We can apply the negative sign to the value from gamepad1.left_stick_y to contrary its management and ensure the left motor moves the robot in the same management as the right motor like mentioned earlier.

That's it! You should be able to printing the green play button in the toolbar and upload code to your Robot Controller and run this code on your tank drive. You volition need to configure what motor port corresponds to what name on the phones when you lot hook up the hardware. You lot volition besides demand to turn on USB Debugging on the phone and permit the computer to connect to the phone start.

Check out other posts in this series

  • FTC Bones Teleop
  • Android Studio FTC Tools: Making common tasks easier and faster

Join me in learning about how computers work

Only enter your email into the box below. You'll as well receive my free 12 page guide, Getting Started with Operating System Development, straight to your inbox just for subscribing.

Getting Started with Operating System Development Ebook