Tech: Programming Guide

Updated: Jun 28, 2018

This resource is a guide to programming in the FIRST Robotics Competition, in Java. It will introduce the classes and methods used.


Normally, when a Java program runs, it looks for main(), runs that method and that's how it knows what to do. In FRC, the robot has a RoboRIO which runs different methods at different times, based on the part of competition. It knows what part to run and when because the Driver Station gives it instructions.


There's a robotInit() method that replaces the constructor, and methods like autonomousPeriodic() and teleopInit() instead of main().


public class Robot extends IterativeRobot


We code the Robot class, which extends IterativeRobot. You should override each method in IterativeRobot , but technically you don't need to since it's a concrete class.


Each part of competition (autonomous, teleop, etc.) has 3 methods. In teleop it would be teleopInit(), teleopPeriodic(), and teleopDisabled(). When teleop starts, teleopInit() is run, then teleopPeriodic() is looped for the entire game, and at the end, or if the robot ever gets disabled, teleopDisabled() is run. Generally, in teleopInit() sensors are reset or some data is retrieved as a starting (reference) point. teleopPeriodic() does most of the work for joystick control or encoders, etc.


To code in general, FRC provides many classes under the WPILib (WIP Robotics Library).

https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599696-what-is-wpilib

You can find all the classes and documentation here, but you only need a handful:

http://first.wpi.edu/FRC/roborio/release/docs/java/


The class used to drive is DifferentialDrive. It takes in two SpeedControllerGroup objects, one for the left side, and another for the right side. A SpeedControllerGroup can have either two or three Talon or Spark objects.

In this example, three Talon objects are constructed for the left and right side. The parameter is the PWM port. Those are the ports on the RoboRIO that you connect to your motor controller (Talon/Spark) with a PWM cable (black/red/white).


Your motor controller objects (Talon/Spark) are used to construct two SpeedControllerGroup objects. These two objects become the DifferentialDrive object.


http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/drive/DifferentialDrive.html


The DifferentialDrive class is really just used for two types of drive:

m_drive.arcadeDrive(-1, 0);

m_drive.tankDrive(-0.5, 0.5);


Both of these examples do the same thing, turn left, but arcadeDrive asks for how much to go left/right and forwards/backwards, while tankDrive asks how much the left side goes forward and how much the right side goes forward.


The motors can actually be accessed directly by using set(power) where power is a double from -1 to 1. This can be used with motors not part of the drive system, such as flywheels or an arm.


There's a lot more classes to mess with, so I'll just link their docs:

http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/ADXRS450_Gyro.html

http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/drive/MecanumDrive.html

http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/Joystick.html

http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/XboxController.html


There's also an awesome motor/encoder system that isn't in WPILib, which has motor controllers like TalonSRX which handle encoders well.


Oh yeah and we published our 2017-2018 PowerUp code. One of the best ways to learn is to take code that exists and modify it to see how it works (and how it breaks). But don't rely on existing code! When it comes to making your team's code, start from scratch. Good luck!

https://github.com/pranavpatil1/Team3749-FRC2018/tree/master/TestRobot/src/org/usfirst/frc/team3749/robot

142 views

Join the robotics community.

arrow&v

DNHS FIRST Robotics Team 3749

  • White Instagram Icon
  • LinkedIn
  • White Twitter Icon
  • Facebook Clean

© 2018 by Team 3749