Note: (especially returning members) The RobotDrive class was deprecated. We might start using DifferentialDrive. EDIT: We didn't change this year, but starting next year, we should look into it.
The CANTalon class was also deprecated. ctre TalonSRX will be used.
Open Eclipse. Click "New -> Project ->WPILIB robot java development ->Example Robot Java Project
->Getting Started. Open the Robot.java file from the project explorer (left side).
It should look like this.
By doing this, you've created a Java project that contains source code and related files for building a program. A Java project may have several classes (e.g. Wheels class, Grabber class, etc.) and the Java builder will incrementally compile Java source files as they are changed. You can access methods created in different classes of the project by implementing them as an interface or extending them which we will discuss more later.
Code snippet
(website to create this: https://tohtml.com/java/ )
import edu.wpi.first.wpilibj.Joystick;
The import statement will import the files that are in the WPILIB library. You must import the classes that you wish to use in order to use the methods in the class.
Most of the things we need for the robot is in wpilibj library. Follow the example above to import the class.
Sometimes we need to import other libraries, too. For example, the TalonSRX class is from: com.ctre.phoenix.motorcontrol.can.TalonSRX
For most Java built in libraries, you don't need to import, such as java.lang.Math
public class Robot extends IterativeRobot {
private DifferentialDrive m_robotDrive
= new DifferentialDrive(new Spark(0), new Spark(1));
The class named Robot is the "main program". Which is why Robot.java file is the "main file".
The word "private" means that the instance field is not accessible by other files. It is only accessible by the methods inside that class. By standard, ALL instance objects should be private.
We are declaring an object called m_robotDrive. This is an instance object. The type of it is DifferentialDrive. That is the name of the class it is from. This part so far is declaring the variable. Then, to initialize it, we put the keyword "new" and call the constructor along with the parameters. In the example, two motors are being put in. One is motor from port 0, and another is motor from port 1. (We don't physically have Spark motors, but we have TalonSRX instead. It is declared the same way as this.)
Take a look at this for more info on creating objects: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
private Joystick m_stick = new Joystick(0);
private Timer m_timer = new Timer();
Instance objects. Notice that Joystick has the parameter of an integer of 0, while Timer doesn't require parameters.
Check the documentation (WPILIB API, Java API) on how to call the constructor.
If you hover your mouse over most words in Eclipse, you can see the documentation for that method or class.
Now you learned about the instance field,
Import Encoder class, then construct an instance object of Encoder with channels 1 and 2.
Import XboxController class, then construct an instance object of XboxController with port 0.
You must be using Eclipse or VS Code. (Sorry, BlueJ would not work from this chapter and on. You can still type in it, but it doesn't have the necessary files to compile.) You must do the setup tasks in chapter 2. You will also need WPILIB API documentation to refer to (link at the frontpage).
Created: Dexin in February 2018
Updated: Dexin, 12/9/18