Programming Essentials
During the process of creating an OpMode the OnBot Java tool prompted the selection of a sample code. In OnBot these samples act as templates; providing the outline and logical structure for different robotics use cases. In the previous section the sample code BlankLinearOpMode was selected. This sample code, seen in below, is the structural shell needed in order to have a working Linear OpMode.
Throughout Hello Robot we will primarily be focusing on modifying the code found during our initialization process and while loop that runs when the Play button is pressed on the Driver Hub. As such, most examples will begin at public void runOpMode()
.
When utilizing the samples provided in this tutorial, double check that the correct number of brackets and file names are added to your final program!
Let's take a quick tour of this template!
Initialization:
Our first section of code is our hardwareMap. This is where our attached components are called and defined between the program and the configuration file
Within this area of our OpMode is anything we want to run BEFORE we press Play on the Driver Hub, but AFTER we press to initialize. This might include defining variables, motor directions, or servo positions!
waitForStart();
Any code following this our waitForStart(); but before our while loop begins will be read ONCE when our play button is pressed! This might be used for resetting timers.
opModeIsActive()
Last is our while loop! This is where any code we want to actively run and/or repeat until we press STOP is entered.
You will complete the majority of your program here.
Adding Comments
Comments are lines of code intended to help you the programmer.
They can be used to explain the function of a section of code. This is especially helpful in collaborative programming environments. If code is handed from one programmer to another, comments communicate the intent of the code to the other programmer.
You can see a few premade comments already in our template written by the FIRST Tech Team to help get started!
To create a comment add //
before the comment to be made. This can also be used to temporarily remove a line of code as the robot will not read comments!
Common Errors
Bracket Mismatch
OnBot Java will attempt to notify you if there are either TOO MANY brackets within your code or NOT ENOUGH brackets by highlighting the final line in red as seen below:
The build errors may appear as below:
Keep in mind while checking your brackets that the error may be on a different line than the one reported! Take a look at the following example:
In this case, the missing bracket is on line 75 where my loop begins and should match with line 84's bracket. Once I have this corrected I can see the error clears:
Mismatched File/Class Names
While building your program you may encounter an error stating the class name is public and needs to be declared. This error can be common while copying and pasting from an example or tutorial and is the result of a mismatched name between the file name and public class.
Looking at the above example I can see my file is named "HelloRobot_TeleOp.java", but my class is "HelloWorld_TeleOp"!
To remedy this error, I could change my class name to match OR my file name. Which option is more ideal can be dependent on your end goal and how all your programs interact.
To change the name of a file, right click it on the list:
When renaming an OnBot Java file, the name is case sensitive and requires .java to be added to the end:
Alternatively, as previously mentioned, the public class name can be updated before building again to clear the error:
Always remember to Build your program after making major changes and before testing with your robot!
Last updated