Learner

Installing RoboRio Programming Tools

Before we get into installation of the software tools, we will go over some basics.

Exercise: Drive in a Square Pattern

Now lets expand on the previous example. This OpMode shows changing direction, use of a for loop and time delays to drive in a square pattern by programming only two of the moves needed (drive straight then turn).

Exercise: Drive in a Square Pattern

// simple autonomous program that drives bot in a square pattern then ends.
// this code assumes it will end before the period is over but if the period ended while
// still driving, this code would just stop.
 
package com.qualcomm.ftcrobotcontroller.local.opmodes;
 
import com.qualcomm.ftcrobotcontroller.local.lib.Logging;
import com.qualcomm.ftcrobotcontroller.local.lib.Util;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;

Exercise: Drive in a Square Pattern

// simple autonomous program that drives bot in a square pattern then ends.
// this code assumes it will end before the period is over but if the period ended while
// still driving, this code would just stop.
 
package com.qualcomm.ftcrobotcontroller.local.opmodes;
 
import com.qualcomm.ftcrobotcontroller.local.lib.Logging;
import com.qualcomm.ftcrobotcontroller.local.lib.Util;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;

Exercise: Drive Forward and Stop

So lets do some actual robot programming. Assuming you have constructed a simple robot chassis with two DC motors driving two wheels, one on the left and one on the right side of the robot, we will also assume you have configured the controller phone for this hardware and named the motors left_motor and right_motor. Given all this, we can write a simple LinearOpMode to drive the robot forward for 2 seconds and then stop:

Exceptions

When running a Java program, if the JVM detects an error it will generate an error condition called an Exception. An Exception is actually an object that contains information about the error and is available for your code to capture and handle as needed. Generating an exception is called throwing, since all Exception objects are subclasses of the Java Throwable object.

Collections

A Collection is an object that stores lists of other objects allowing the group of stored objects to be manipulated in many powerful ways. A Collection may sound like an array or ArrayList and while a Collection is quite different than an array, ArrayList is in fact one implementation of the Collection concept. Java has a large number of specific implementations of the Collection concept you can use. Here are the most commonly used types of Collection:

Arrays

An array is a special object used to store a list of variables of the same data type. An array is defined like this:

This statement defines and then creates an array of 3 integer variables (or elements) which will be addressed as a list. The new keyword defines the size of the array. We can then put values in the array and access them with an index value (position) in the array. Arrays are indexed starting at zero:

Enums

Lots of times when programming we need to assign constant values to track the various states of a data item. For example, in a program we have an integer variable that indicates the day of the week. We can define a convention where the integer value zero is assigned to mean Sunday, the value of 1 to mean Monday, 2 to mean Tuesday and so on. When coding our program we have to remember that 2 means Tuesday. This tracking of numeric values and what they mean for various variables can get cumbersome and error prone in more complex programs.

The Number Classes

We explored Java's primitive data types in an earlier lesson. While we use primitive numeric data types directly most of the time, there are times when we need to treat a numeric primitive as an object. For this reason Java provides the Number Classes. There is a subclass of Number for each numeric primitive:

Pages