Basic Use Of The 2010 FRC Controller For Java Programming

laboredbumbaileySoftware and s/w Development

Jun 7, 2012 (5 years and 3 months ago)

688 views

Basic Use Of The
2010 FRC Controller
For
Java Programming
FIRST FRC Workshops
January 23, 2010
Introduction
The purpose of this workshop is to get you familiar with your FRC control system and how to program

it using Java. When you are done, you will have a completely configured control system and be able to

drive your robot with a simple control program written in Java.
Not sure what all the Java code means? Please look at the slides from the
Java for FRC
from the

Kickoff and
Basics of Java Programming For FRC
for details.
Configure Your Robot Hardware
Make sure that your two hobby servos are plugged into PWM 1 and PWM 2 on the Digital Sidecar.

Make sure that you have the two required jumpers plugged in next to PWM 1 & 2, otherwise the servos

will not work.
Make sure you pull the jumpers out before you wire Jaguars or Victors into PWM 1 or PWM 2. If you

don't pull the jumpers out, you can burn out your speed controllers.
Configure Your Driver's Station
Now we will configure your driver's station, in case you haven't done it yet. Turn on your driver's

station and wait until it boots. It will end up running the driver's station program once it finishes

booting.
Plug in your two joysticks and your eStop button into the USB hub and plug the USB hub into your

driver's station.
Go to the Setup Tab and enter your team number.
January 23, 2010: Keith M. Hughes
In my case, my “team” is 9901, a fake team number. Use your real team number. Your Voltage display

will not contain any numbers. And I don't have an eStop button, which is why it says “NO STOP

BUTTON”. Your's should say you have the eStop button.
Configure NetBeans
This section only needs to be done once.
First you need to tell NetBeans about the FRC java plugins.
Go to the
Tools
menu and choose
Plugins
.
Click on the
Settings
tab.
Click the
Add
button and fill in the dialog box with the following values.
Hit
OK
.
January 23, 2010: Keith M. Hughes
Now go to the
Available Plugins
tab and look for all the FRC entries. The following is an example,

perhaps there are more.
Select all the FRC entries and click the
Install
button.
Now NetBeans knows about FRC. Close the Plugins dialog.
NetBeans now needs to be told your team number so that it can talk to your cRIO.
1.
Go to the
Tools
Menu and selection
Options
.
2.
Select the
Miscellaneous
tab (on the right).
3.
Click on the
FRC Configuration
sub-tab.
4.
Type in your team number in the appropriate place.
5.
Hit the
OK
button.
Create Your Robot Project
Go to the
File
menu and choose
New Project
. This will bring up the New Project wizard.
Select the
FRC Java
category, then
SimpleRobotTemplateProject
. Hit the
Next>
button.
This will bring up the
Name and Location
dialog.
January 23, 2010: Keith M. Hughes
Suppose I am team 9901. I picked the name
FRC2010Team9901
for my project and for my robot. If I

use this naming convention, I will have a copy of the previous year's robot program each year in case I

want to look back at it. You should pick a good name for your project.
All you need to fill in is
Project Name
and
Robot Class
. I also changed
Project Package
to be

something specific to my team, this isn't strictly necessary. But why don't you name yours the same

way as I did mine, but with your team number instead of 9901, just so everything works as you type.
Hit the
Finish
button,
You now have a new project called
FRC2010Team9901
(but with your team number rather than

9901). You can see this in the
Project Explorer
window on the upper, left side of NetBeans. The

picture below shows what you will see. Depending on what you do, you may have more than 1.
Click on the various + until the project explorer looks something like the following.
January 23, 2010: Keith M. Hughes
Your Program
Now let's type in your first FRC Java program. Click on
FRC2010Team9901.java
in the
Project

Explorer
window.
Look in the center window, where you will find the skeleton of your Java code. Scroll until you find

the section that looks like this:
import edu.wpi.first.wpilibj.SimpleRobot;
Your code must be exactly as given below, except for your team number in the places you see 9901.

Capitalization matters, those curly braces need to be there, the semicolons are important.
package org.team9901;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
public class FRC2010Team9901 extends SimpleRobot {
private RobotDrive robotDrive = new RobotDrive(1,2);
private Joystick stick = new Joystick(1);
public FRC2010Team9901() {
getWatchdog().setExpiration(0.1);
}
public void autonomous() {
}
public void operatorControl() {
January 23, 2010: Keith M. Hughes
getWatchdog().setEnabled(true);
while (isEnabled() && isOperatorControl()) {
getWatchdog().feed();
robotDrive.arcadeDrive(stick);
}

}
}
We now have a complete Java program which will drive your robot in Arcade Mode. Let's get this code

into the cRIO.
Downloading Code Into the cRIO
To run your program, you need to deploy your code to the cRIO.
Hit the
Run
button. It is a green triangle on the toolbar near the top of the screen.

This will compile your program and deploy it to the cRIO and start it running. The following window

will pop open.
You will know your program is running when you see
Information: No user-supplied robotMain()
January 23, 2010: Keith M. Hughes
at the bottom of the window.
If you don't see something like this, try it again. If it still doesn't work:

Make sure you have a network cable from your laptop to Ethernet Port 1 on the cRIO.

Make sure your laptop's network connection is set to the proper IP address (10.xx.yy.6, where

xx and yy are from your team number).
Now remove the plug the network cable from your laptop and plug it into your driver's station.
Make sure your driver's station is in Teleoperated Mode.
Then press the
Enable
button on the driver's station.
January 23, 2010: Keith M. Hughes
You should find that you can move both servos from the joystick plugged into USB 1.
You now have Arcade Mode driving! Congrats!
Tank Mode
Now you will modify the program to run in Tank Mode, which will use two joysticks. Parts of your

program will remain the same, parts will be different.
Try and change only the parts that are different from your own program rather than trying to retype the

entire program. This will help you learn what different parts do.
Please notice what ends with a comma, what ends with a semicolon, how capitalization is used, and

where the curly braces
{}
are.
Remember about your team number!
package org.team9901;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
public class FRC2010Team9901 extends SimpleRobot {
private RobotDrive robotDrive = new RobotDrive(1, 2);
private Joystick stickLeft = new Joystick(1);
private Joystick stickRight = new Joystick(2);
public FRC2010Team9901() {
getWatchdog().setExpiration(0.1);
}
public void autonomous() {
}
public void operatorControl() {
getWatchdog().setEnabled(true);
while (isEnabled() && isOperatorControl()) {
getWatchdog().feed();
robotDrive.tankDrive(stickLeft, stickRight);
}
}
}
Try running this. You should find now that both joysticks work. The joystick in USB 1 will drive the

January 23, 2010: Keith M. Hughes
servo in PWM 1 and the joystick in USB 2 will drive the servo in PWM 2.
Notice how similar this was to your previous program. What was the same? What changed?
Autonomous Mode
OK, let's try an autonomous mode. We'll start off with something simple, we'll have the robot move

forward for two seconds, then stop. In your case, your servos will move to some position, hold that

position for 2 seconds, then move to another position. But if you had Jaguars attached to PWM 1 and

PWM 2, your robot would move forward.
Try and change only the parts that are different from your own program rather than trying to retype the

entire program.
Please notice what ends with a comma, what ends with a semicolon, how capitalization is used, and

where the curly braces
{}
are.
package org.team9901;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
public class FRC2010Team9901 extends SimpleRobot {
private RobotDrive robotDrive = new RobotDrive(1, 2);
private Joystick stickLeft = new Joystick(1);
private Joystick stickRight = new Joystick(2);
public FRC2010Team9901() {
getWatchdog().setExpiration(0.1);
}
public void autonomous() {
robotDrive.drive(0.5, 0.0);
Timer.delay(2.0);
robotDrive.drive(0.0, 0.0);
}
public void operatorControl() {
getWatchdog().setEnabled(true);
while (isEnabled() && isOperatorControl()) {
getWatchdog().feed();
robotDrive.tankDrive(stickLeft, stickRight);
}
}
}
January 23, 2010: Keith M. Hughes
Now let's try running your autonomous mode. There are a couple of switches on your Driver's Station

screen, one for Teleoperated Mode and one for Autonomous Mode. It should be in Teleoperated Mode

right now. Switch it to Autonomous Mode.
Now press the
Enable
button.
You should have the servos move to one position for two seconds and then switch to another position.

Does it make sense why it moved to one position for 2 seconds then moved to another?
Now change it to repeat what it did 4 times with a 2 second pause. You are only going to change the

code between
public
void autonomous()
and the matching
}
.
public void autonomous() {
getWatchdog().setEnabled(false);
for (int i = 1; i <= 4; i = i + 1) {
robotDrive.drive(0.5, 0.0);
Timer.delay(2.0);
robotDrive.drive(0.0, 0.0);
Timer.delay(2.0);
}
}
Run your program and see if it repeats itself 4 times.
How would you make it repeat 10 times?
Working With Buttons
January 23, 2010: Keith M. Hughes
Now let's see how to work with buttons. Let's make the robot stop if the trigger on the left joystick is

pressed. The robot will stay stopped until the trigger is released, at which time you can make the servos

move again.
Notice you are only changing
the code between
public
void operatorControl()
and the

matching
}
.
public void operatorControl() {
getWatchdog().setEnabled(true);
while (isEnabled() && isOperatorControl()) {
getWatchdog().feed();
if (stickLeft.getTrigger()) {
robotDrive.drive(0.0, 0.0);
} else {
robotDrive.tankDrive(stickLeft, stickRight);
}
}
}
Can you see that the watchdog will be fed even if the trigger is pressed?
Instead of the trigger, let's try another button.
public void operatorControl() {
getWatchdog().setEnabled(true);
while (isEnabled() && isOperatorControl()) {
getWatchdog().feed();
if (stickLeft.getRawButton(3)) {
robotDrive.drive(0.0, 0.0);
} else {
robotDrive.tankDrive(stickLeft, stickRight);
}
}
}
Not sure which button 3 is? Try all of them, starting from the top of the joystick, until you find out

which is button 3. Just move the joystick around as you try various buttons until you find the one that

makes the servos stop moving.
A Pseudo-Arm
Let's make one of our servos into an arm and make it do a little dance when button 3 is pushed. Notice

we are changing the time for the watchdog.
g
etWatchdog().setExpiration(0.5);
January 23, 2010: Keith M. Hughes
You will need to pull out one of your servos out of the PWM port it is plugged into and plug it into

PWM 3. You must also move the servo jumper from where the PWM cable was to PWM 3. Your drive

system will continue to talk to the old port, but you won't see anything happen since nothing is plugged

into that PWM port any more. This is OK, you just have two servos and we need one of them for a fake

arm.
Here is the whole program. As usual, try and just change what is different from your previous program.

This will help you understand what different pieces of your program do.
package org.team9901;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
public
class
FRC2010Team9901
extends SimpleRobot {
private RobotDrive robotDrive = new RobotDrive(1,2);
private Servo arm = new Servo(3);
private Joystick stickLeft = new Joystick(1);
private Joystick stickRight = new Joystick(2);
public
FRC2010Team9901
() {
getWatchdog().setExpiration(0.5);
}
public
void autonomous()
{
getWatchdog().setEnabled(false);
for (int i = 1; i <= 4; i = i + 1) {
robotDrive.drive(0.5, 0.0);
Timer.delay(2.0);
robotDrive.drive(0.0, 0.0);
Timer.delay(2.0);
}
}
public void moveArm() {
for (int i = 0; i < 4; i++) {
getWatchdog().feed();
arm.set(1.0);
Timer.delay(0.4);
getWatchdog().feed();
arm.set(0.0);
Timer.delay(0.4);
getWatchdog().feed();
arm.set(0.5);
}
}
public void operatorControl() {
getWatchdog().setEnabled(true);
January 23, 2010: Keith M. Hughes
while (isEnabled() && isOperatorControl()) {
if (stickLeft.getRawButton(3)) {
moveArm();
} else {
getWatchdog().feed();
robotDrive.tankDrive(stickLeft, stickRight);
}
}
}
}
Download the program into your robot and see what it does.
Notice we're feeding the watchdog in
moveArm()
.
Your arm probably won't use a hobby servo, but a bigger, more powerful motor. You would then

replace
Servo
with
Jaguar
or
Victor
in the code above.
January 23, 2010: Keith M. Hughes