This page grows one section per session. Bookmark it. The long version, with every piece of syntax in the repository and where it appears, is Java Syntax.
packagefrc.robot;// which folder this file is inimportedu.wpi.first.wpilibj.TimedRobot;// bring in one class by namepublicclassRobotextendsTimedRobot{// a class; the file is Robot.javaprivateintm_loopCount=0;// attribute: lives as long as the object@Override// replaces a method TimedRobot already haspublicvoidrobotPeriodic(){// method definition, returns nothing (void)doubleseconds=m_loopCount*0.02;// local variable: lives until the method endsCommandScheduler.getInstance().run();// method call}}
Type
Holds
Example
int
Whole numbers
int count = 3;
double
Decimals
double speed = 0.5;
boolean
true / false
boolean on = true;
String
Text
String name = "Romi";
Operator
Meaning
Example
Result
+ - * /
Math
3 * 0.02
0.06
/ on two ints
Whole-number division
7 / 2
3
%
Remainder
130 % 50
30
++
Add one
count++;
+=
Add and store
count += 5;
+ with text
Glue text and numbers
"Loop " + 50
"Loop 50"
=
Store
count = 0;
==
Are they equal?
count == 50
true or false
if(m_loopCount%50==0){// run the block only when the condition is trueDataLogManager.log("One second");}
Every statement ends with ;. Every { has a }. Comments start with //.
publicclassSignalLightextendsSubsystemBase{// one class per file: SignalLight.javaprivatefinalOnBoardIOm_onboardIO=newOnBoardIO(...);// attribute, set once (final)privatebooleanm_yellowOn=false;// attribute, changes over timepublicbooleanisYellowOn(){// method that returns a booleanreturnm_yellowOn;}publicvoidsetGreen(booleanon){// method with a parameter, returns nothingm_onboardIO.setGreenLed(on);}}
SignalLightlight=newSignalLight();// build an object from the classlight.isYellowOn();// call a method on an objectRslLogic.shouldBeOn(true,0.25);// call a static method on a class (no object)
Word
Meaning
private
Only this class can use it
public
Anyone can use it
final
Assigned once, never changes
static
Belongs to the class, not to an object
Constructor
public ClassName() { ... }, runs once at new
Operator
Meaning
<<=>>=
Compare numbers
==!=
Equal, not equal
!a
Not
a && b
Both
a \|\| b
Either
if(!enabled){returntrue;// return ends the method right here}doublet=timeSeconds%1.0;returnt<0.5;// a comparison is already a boolean
publicstaticdoubleforwardSpeed(doublerawAxis){// takes a double, returns a doublereturn-applyDeadband(rawAxis);// call another method, flip the sign}Math.abs(x)// size without signx*0.5// scaleDriveConstants.kDeadband// a named constant: public static final double kDeadband = 0.1;
publicclassArcadeDriveCommandextendsCommand{// build on WPILib's CommandprivatefinalDoubleSupplierm_forwardAxis;// attribute filled by the constructorpublicArcadeDriveCommand(RomiDrivetraindrivetrain,DoubleSupplierforwardAxis){m_forwardAxis=forwardAxis;// parameter -> attributeaddRequirements(drivetrain);}@Overridepublicvoidexecute(){doubleraw=m_forwardAxis.getAsDouble();// ask the supplier for a fresh value}}
doubleinchesPerCount=(Math.PI*kWheelDiameterInch)/kCountsPerRevolution;// parentheses firstUnits.inchesToMeters(inches)// named conversion, no magic 0.0254Rotation2d.fromDegrees(90.0)// build an object with a static methodm_odometry.getPoseMeters()// returns a Pose2d (x, y, rotation)assertEquals(expected,actual,1e-6);// decimals: always give a tolerance
classRomiDrivetrainTest{@BeforeEachvoidsetUp(){...}// before every test@AfterEachvoidtearDown(){m_drivetrain.close();}// after every test: clean up}publicclassRomiDrivetrainextendsSubsystemBaseimplementsAutoCloseable{@Overridepublicvoidclose(){...}// the promise: I can clean myself up}
doubledegrees=m_gyro.getAngleZ();if(DriveConstants.kGyroReversed){// a boolean constant as a switchdegrees=-degrees;}
for(inti=0;i<sides;i++){// start at 0, keep going while i < sides, add one each passroutine=routine.andThen(newDriveDistance(0.5,sideInch,m_drivetrain));}