Skip to content
Rande Johnson edited this page Dec 21, 2013 · 5 revisions

Programming the control system for the robots can be done in several programming languages, including Java. Java is a very common language used throughout industry, everywhere from Android mobile devices to many back-end servers and services. It has been the most popular or second-most popular programming language for many years, and is used in all kinds of situations, including small Android mobile devices to massively large servers. Java programs can be run on most platforms with no modification.

Advantages

There are several advantages to using Java for robot control:

  • Programming FRC robots requires very little code, and most of the code can be shared for both tele-operation and autonomous mode. You can even test most of the autonomous code in teleoperation.
  • You can develop and deploy Java programs for FRC robots on any platform (e.g., Windows, OS X, and Linux). The robots can only be driven via the Driver Station on Windows.
  • You can use the Eclipse IDE, which is far superior to NetBeans. You can even debug your program in Eclipse while it is running on the robot.
  • Multiple developers can collaborate on a single Java program, especially when the code is stored on GitHub. This is standard practice for the software development industry.

Tools

  • JDK - The Java Development Kit (JDK) contains the compiler and other tools necessary when developing Java programs. This is different than the Java Runtime Environment (JRE), which is often installed on computer to run Java programs created by someone else.
  • Eclipse - Eclipse is a very popular Integrated Development Environment (IDE) used for Java development. Many professional Java developers use Eclipse.
  • Git - Git is a tool for managing source code. Software developers place their source code into a Git repository, and then Git keeps track of all changes (commits) to the code and makes it possible to share and merge changes from other developers. Also, because Git records all the history of changes to the source code, we don't need to make "backup copies" of our code in some working state. Git will do that automatically for us, as long as we use Git correctly. Git will take some time to master, but doing so is very worthwhile as it is an extremely useful tool used by many professional developers. Git is a command-line tool, though there are a number of graphical user interfaces available.
  • GitHub.com - GitHub.com is a website that exposes Git repositories in the cloud, allowing multiple developers to easily share code, review and merge changes, see the entire history of the code, and to browse the code online. Our team's area already contains two Git repositories: 2014 and files. GitHub.com offers some tools that Git does not, like issues and wikis.
  • Command-line - Many software developers heavily use the command line, especially when using Git. This may seem counter intuitive and difficult at first, but there are quite a few advantages:
    1. The command line tools are extremely powerful and flexible on OS X, Unix and Linux. The Windows command line tool is very subpar, but there are Linux-like tools available; the Git Bash command line on Windows will be sufficient for our purposes. On Mac, the built-in command line app is called "Terminal".
    2. It is easy to document a series of command line steps, and it is just as easy to follow along and copy/paste the commands.
    3. Multiple commands that are commonly used can be put into a script.

Install the tools

There are several things that must be done one time before you can start programming on your own computer. Few if any of these are pre-installed on most computers, so unless you know otherwise you will likely need to install all of these.

  1. Install the JDK - If you do not yet have JDK 6 or 7 installed, download and run the official JDK 7 platform installer.
  2. Install Eclipse - If you don't have Eclipse installed, download and run the latest Eclipse Standard installer.
  3. Install Git - The easiest way to install Git is to follow these instructions.

Set up Git and GitHub

Git makes it possible for every developer on the project to have a complete copy of the source code (and the entire history of that source code). GitHub makes it easy for these developers to share their changes and to merge all those changes into the shared upstream repository on GitHub. Unfortunately, that means there are a lot of moving parts. Fortunately, once it's set up you can focus on your own steps, which aren't too bad at all.

We've already set up the shared upstream repository at https://github.com/frc-4931/2014, and it even has some initial code. Anybody can pull the recent changes into their local repository, but nobody can push their changes to the upstream. Instead, they create a fork of the upstream repository on GitHub, push their changes to their fork, and then create a pull-request with the changes. Everyone can then see all of these pull-requests, and when a pull-request is reviewed and deemed acceptable an administrator merges the changes in that pull-request into the upstream repository. Again, everyone can then pull the changes into the local repository.

  1. Fork the FRC4931 repository - Go to https://github.com/frc-4931/2014 and be sure you're logged in. Click on the "Fork" button (near the upper right corner), and when prompted choose your username. This gives you a copy (a fork) of the team's official "upstream" repository, and this is where you will share your changes. At this point, it also is a complete copy of the team's official "upstream" repository.
  2. Get a local copy of your fork - This is called "cloning" in Git, and it's actually pretty simple. First, go to your fork (e.g., https://github.com/yourusername/2014) and copy the "clone URL" on the right side of that page. Then go to your command line and type each one of these commands (one-by-one):
$ cd the/path/where/you/want/to/develop
$ git clone https://github.com/yourusername/2014.git frc4931-2014

That will take a few minutes to complete (it's downloading all of the files from your clone). It creates a directory called frc4931-2014, and in it you will find all of the same files and folders in your fork on GitHub.

  1. Tell your local clone about upstream - Each local Git repository can talk to multiple remote repositories, and when you clone your repository contains an "origin" remote for your fork. The following commands add a remote called "upstream" to the teams' official repository, and then fetches all of the contents of this "upstream" repository. Run each of these one-by-one, exactly as they are written:
   $ cd frc4931-2014
   $ git remote add upstream https://github.com/frc-4931/2014.git
   $ git fetch upstream

Next we have to do a little housekeeping. Again, run each of these commands one-by-one, exactly as they are written:

   $ git branch --set-upstream master upstream/master
   $ git pull upstream master
   $ git status

The second command will result in quite a few output lines, but the last command should result in something like the following output:

   # On branch master
   nothing to commit, working directory clean

At this point, your local repository is completely set up.

Set up Eclipse

The next step is to import the code into Eclipse. Start Eclipse, and when it prompts for a workspace location choose the same frc4931-2014 directory that we created above when we used git clone. Then:

  1. If a "Welcome" tab appears, click the "Workspace" button in the upper right corner.
  2. Choose "Window->Open Perspective" and choose "Java".
  3. Open Eclipse preferences (via "File->Preferences" on OS X or "Window->Preferences" on other operating systems), and expand "Team->Git->Projects" in the left-hand tree, and select "Projects" in the tree. On the left, uncheck the "Automatically ignore derived resources by adding them to .gitignore" option and click "OK".
  4. Choose "File->Import" and in the dialog expand "General" and choose "Existing projects into workspace". Choose "Next" to start the import.
  5. An "Import" dialog will appear, and the radio button next to "Select a root directory" should be selected, and to the right there will be a "Browse..." button. Click that button. A file chooser dialog will appear, and it should have our frc4931-2014 directory already selected (if not, find it on your file system and select it). Then choose "Open". That file chooser dialog will close, and the "Import" dialog will change to have at least 6 projects listed and selected.
  6. Click the "Finish" button in the "Import" dialog to finish the import. You should see those 6 (or more) projects in the "Package Explorer" tab of your workspace.

Develop the Java projects

Once all of this setup work has been done, it should not have to be done again. That means that from now on, you can edit and compile Java code on your computer, and deploy it to the robot.

At any given time, you should only be working on a single project at a time. All the Java projects are independent of each other, and represent different scenarios. The current projects are just prototypes, and we'll probably create a single project for each specific robot that Team 4931 builds. That project can evolve as the robot evolves.

Remember that we can always "go back in time" to see the state of the Java code at a past date/time, so nothing is ever lost. With Git, we do not need to make copies of the code to serve as a backup - Git does all that for us.

Let's look at the PrototypeCommandDriveRobot project. This prototype project allows the Drivers Station to control a robot that has two motors (on ports 1 and 2) and a single arcade-style joystick (on port 1) with buttons 3 and 4 increasing and decreasing the maximum available speed when the joystick position is maxed out. It is written in the "command style", where the code contains 1 or more "commands" (or groups of commands) for each "subsystem", and each command (or group of commands) can be bound to something on the operator interface (e.g., joysticks, buttons, etc.).

Expand the PrototypeCommandDriveRobot project and its src folder to see packages for the commands, subsystems, robot and operator interface.

Compile a project

To compile the source code in a project, simply select the project (e.g., PrototypeCommandDriveRobot) in the "Package Explorer" tab, and then choose Run->External Tools->Compile. (Or, look in the toolbar for the pull-down button with a white arrow in a green circle and a red toolbox under the green circle. The Compile choice will be in that pull-down menu, too.) The following should be displayed in the "Console" tab:

Buildfile: /Users/rhauch/Documents/FRC/frc4931-2014/PrototypeCommandDriveRobot/build.xml
Trying to override old definition of task classloader
clean:
clean:
-actual-compile-compile:
    [mkdir] Created dir: /Users/rhauch/Documents/FRC/frc4931-2014/PrototypeCommandDriveRobot/build
    [mkdir] Created dir: /Users/rhauch/Documents/FRC/frc4931-2014/PrototypeCommandDriveRobot/build/classes
     [echo] [crio-compile] ./src, /Users/rhauch/sunspotfrcsdk/lib/wpilibj.jar:/Users/rhauch/sunspotfrcsdk/lib/networktables-crio.jar, /Users/rhauch/sunspotfrcsdk/lib/squawk.jar -> ./build/classes
    [javac] Compiling 8 source files to /Users/rhauch/Documents/FRC/frc4931-2014/PrototypeCommandDriveRobot/build/classes
-post-compile:
compile:
BUILD SUCCESSFUL
Total time: 1 second

To compile another project, simply select it and run the same Compile build tool. Do not select any of the non-Java projects, as they might mess up the build commands.

Deploying a project

Before you can deploy the compiled code to the robot, your computer has to be configured with a static IP address that is on the same subnet as the robot and the WiFi base station. By FRC convention, the static IP addresses of our equipment will incorporates the team's number (e.g., 4931):

  • 10.49.31.2 for the robot
  • 10.49.31.5 for the Classmate computer with the Drivers Station (provided by FRC in the kit of parts)
  • '10.49.31.6' for the computer used to image the cRIO (rarely used, revert to another IP when done)
  • 10.49.31.x for all other computers; each programmer should pick for x a unique value greater than 20

See this page for more details.

Once your computer is using a static IP address, then deploying the project is very easy:

  • Select the project (e.g., PrototypeCommandDriveRobot) in the "Package Explorer" tab, and then choose Run->External Tools->Deploy.

Making changes

See the Java Development Steps page for step-by-step instructions to modify the code and publish your changes.