0% found this document useful (0 votes)
72 views37 pages

FTC - Java Programming

This document summarizes an FTC workshop on Java programming. It discusses the history of Java, what is needed to program in Java for FTC including Android Studio and tutorial files. It introduces the user interface of Android Studio and provides important definitions. It then provides an example of teleop mode code, explaining how to create an op mode class, define properties and methods, and register the op mode. Finally, it suggests structural ideas for extending the code from teleop to autonomous modes using object oriented principles like inheritance, abstraction and hierarchy.

Uploaded by

dan8818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views37 pages

FTC - Java Programming

This document summarizes an FTC workshop on Java programming. It discusses the history of Java, what is needed to program in Java for FTC including Android Studio and tutorial files. It introduces the user interface of Android Studio and provides important definitions. It then provides an example of teleop mode code, explaining how to create an op mode class, define properties and methods, and register the op mode. Finally, it suggests structural ideas for extending the code from teleop to autonomous modes using object oriented principles like inheritance, abstraction and hierarchy.

Uploaded by

dan8818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Kettering FTC Workshop

FTC – JAVA PROGRAMMING

Workshop 2015
Eric Weber
FRC: 1322, FTC: 5954 & 7032

EW - 2015
Kettering FTC Workshop

Java History
• First appeared in 1995
• Sun Microsystems creates and maintains the core
language
• Community involvement is very high in the
development
• Appears in many small devices
• Want college credit in Computer Science?
– Java is the standard language for AP CS courses
• Most importantly, it is currently the gateway into
other languages
– Know java? You know C, C++, C#, Python, Ruby, Pascal
and many others with minimal understanding

EW - 2015
Kettering FTC Workshop

What you will need?


• Android Studio
– http://developer.android.com/sdk/index.html#top
• FTC_App
– https://github.com/ftctechnh/ftc_app/archive/master.
zip
• Tutorial for setting up phones
– https://github.com/ftctechnh/ftc_app/blob/master/d
oc/tutorial/FTCTraining_Manual.pdf

EW - 2015
Kettering FTC Workshop

If you haven’t followed the instructions


on
http://paws.kettering.edu/~webe3546/

Start Downloading
The process will take a long time to
complete

4
EW - 2015
Kettering FTC Workshop

Introducing the UI

Project Tab Compiler

Source Code Editor

EW - 2015
Kettering FTC Workshop

Important Definitions
• IDE (Integrated Development Environment):
– Android Studio itself is an IDE. It contains a source
code editor, compiler, and a debugger all in one.
• OP Modes: define how our robots behave
– Teleop and Autonomous modes are now called OP
Modes
• Keywords: Reserved words that Java requires, and
cannot be used as an unique name

EW - 2015
Kettering FTC Workshop

TELEOP EXAMPLE CODE

Will work with the robot being built during this workshop.

EW - 2015
Kettering FTC Workshop

Code Objective:

Don’t write this down yet.


We will cover this line by
line.

EW - 2015
Kettering FTC Workshop

Teleop Mode Example:


• This code is strictly for a simple tele OP mode
• Not optimal for programming a robot with an
autonomous mode
• Equivalent to a ‘Hello World’ for the robot that is
being built in the class rooms upstairs

EW - 2015
Kettering FTC Workshop

Creating an OP Mode:
1) In the Project Tree Navigate:
- FTC APP -> FtcRobotController -> src -> main ->
com.qualcomm.ftcrobotcontroller -> opmodes
3) Right Click on the Folder
4) Go to “New” -> “Java Class”
5) Give it a name
6) Click “OK”

EW - 2015
Kettering FTC Workshop

Edit Class Definition:


• Once we have created our OP Mode, we need to
edit a line immediately.
• Please add “extends OpMode” between the name
of class and the “{“

EW - 2015
Kettering FTC Workshop

Anyone Notice This?

• Then you may auto complete with the


selected word below
• Press the “Tab” key to allow completion.
• A benefit of using an IDE allows easier
functionality

EW - 2015
Kettering FTC Workshop

Important Notes:
• First we are defining a public class
– Classes defines data formatting and procedures
– Public defines how it may be accessed
• In this case, anywhere
• Second we have a unique name for these classes
– Must be unique and not be a keyword
• Third we are extending a parent class (Inheritance)
– We are directly adding onto a class already made
– We also gain the functionality of this class

EW - 2015
Kettering FTC Workshop

Define Properties
• Next we will enter in the following below
• These are what are called properties or fields
– From here on out, we will refer to them as properties

EW - 2015
Kettering FTC Workshop

Important Notes:
• Properties allow us to define data to be used
• In this case we are defining:
– 1 DcMotorController (a class soon to be an object)
– 2 DcMotor (another class soon to be an object)
• Later we will be able to effect the values of the DC
Motors
• In non-OOP languages, these are also known as
variables (RobotC)
• If you want more, you have to define more

EW - 2015
Kettering FTC Workshop

Our first Method


• Methods allow us to perform tasks
• Please enter the next lines after our properties:

EW - 2015
Kettering FTC Workshop

Important Notes:
• First off, @Override allows us to over write a
previous method from OpMode.
– This is one of two methods that MUST be overridden.
– This will always be the first method called once the
ARM button is pressed on the robot controller.
• Second, we are assigning actual objects to the
properties we already have defined

EW - 2015
Kettering FTC Workshop

Important Notes:
• Third, what is hardwareMap?
– It is an object that contains all the hardware mapping
as defined by the configuration files on your Robot
Controller app
– Everything stated by your robot configuration file will
be here. This makes setting up your configuration
correctly and translate it EXACTLY into your java
code.

EW - 2015
Kettering FTC Workshop

Our Second Method


• The second method we must override is the loop()
method.
• Please enter the next lines after our previous
method.

EW - 2015
Kettering FTC Workshop

Important Notes:
• This method is called every time the robot cycles
(approx. 20ms give or take)
• Not where to apply a loop
• Since a part of OpMode, this will be consistent
with autonomous OpModes as well

EW - 2015
Kettering FTC Workshop

Final step: Register your OpMode


• We need to finalize the app by registering our OpMode with the
rest of the program.
• Navigate through the project tab to: ftc_app-master ->
FtcRobotController -> src -> main -> java -> com -> qualcomm ->
ftcrobotcontroller -> opmodes -> FtcOpModeRegister
• Under the register method, type: manager.register(“Tutorial”,
Tutorial.class);

EW - 2015
Kettering FTC Workshop

Important Notes:
• To be able to select your OpMode, it needs to be
added to a list.
• I have already trimmed down the OpModes that
were used as tutorials.
• NullOp will do nothing.
– Good to keep due to any issues that arise.

EW - 2015
Kettering FTC Workshop

Important Definitions:
• Class: Defines data format and procedures
• Properties: Variables defined by the class
• Methods: Procedures that work on inputs or properties
• Inheritance: The ability to extend a class to include more
functionality (methods) or data (properties)
• Overriding: The ability to take a method and change it to
give different functionality
• Constructor: As an object is created, a special method is
always called immediately.

EW - 2015
Kettering FTC Workshop

STRUCTURAL SUGGESTIONS

Ideas to extend your code from Teleop to Autonomous modes

24
EW - 2015
Kettering FTC Workshop

Hierarchy
• Object Oriented Programming’s Greatest
asset is reusability and extensibility.
• Better to define a robot by its actions, then
control it through those actions.
Core
Functions

Teleop Auto
Mode Test Modes
Zone

EW - 2015
Kettering FTC Workshop

Key Ideas on Inheritance


• Inheritance Properties:
– Extending a base class forces the base class to exist,
giving us those methods as well
• Think drive systems, arms, sensors, or timers required
• But know about Private, Public, and Protected
– We only need to override the operation modes we
want.
– For instance, leave initialization for the base class, only
work with loop() in the actual OpModes.

EW - 2015
Kettering FTC Workshop

Important Keys to Note:


• Methods with inheritance
• Overriding (Virtual Methods)
• OpModes
• Encapsulation

EW - 2015
Kettering FTC Workshop

NOW AN OPEN DISCUSSION.

Questions and Suggestions?


What would you like to see

28
EW - 2015
Kettering FTC Workshop

AUTONOMOUS MODE

A method to accomplish tasks in Autonomous mode

29
EW - 2015
Kettering FTC Workshop

State Machines
• For this style of programming, State Machines are
the suggested method.
• Review of State Machines:
– Idea of states: Set of instructions unique to a phase of
a program
– States define what the robot is to do
– Redefine outputs
– Read inputs to trigger next state

EW - 2015
Kettering FTC Workshop

State Machines
• Requirements of a state machine:
– A state variable (usually an enumeration)
– A state selector (always a case-switch operator)
– State triggers (sensors or timers)
– An initial state

Drive Check
Initialize Stop
Forward Encoders

EW - 2015
Kettering FTC Workshop

Enumerations:
• Enumerations are unique names with values
defined behind them
• Common examples include compass directions
(values of NORTH, SOUTH, EAST, and WEST)
• Place above the actual OpMode

EW - 2015
Kettering FTC Workshop

Switch and Case Structure:


• Allows for multiple cases or states to make different
operations
• Selector can take Enumeration’s, Integer’s commonly
Switch Structure – Refers to all
cases
Selector – Selects whatever value
is entered
Case – Different states, as selected
by the selector
Break- Escapes out of structure
Default- If no valid case exists,
default will always be used

EW - 2015
Kettering FTC Workshop

Triggers:
• Usually done by an if statement
– If statements are like case’s, but can easier to define
with logical statements
• Causes a change in our state variable

EW - 2015
Kettering FTC Workshop

Putting it Together:
• We get the following OpMode:

EW - 2015
Kettering FTC Workshop

FINAL REMARKS &


QUESTIONS

EW - 2015
Kettering FTC Workshop

Thank You

Now get out their and program!

EW - 2015

You might also like