Programming Essentials
Last updated
Last updated
During the process of creating an OpMode the Blocks tool prompted the selection of a sample code. In Blocks these samples act as templates; providing the blocks and logical structure for different robotics use cases. In the previous section the sample code BasicOpMode was selected. This sample code, seen in the image below, is the structural shell needed in order to have a working OpMode.
An OpMode can often be considered a set of instructions for a robot to follow in order to understand the world around it. The BasicOpMode provides the initial set of instructions that are needed in order for an OpMode to properly function.
Though this sample is given to users to reduce some of the complexities of programming as they learn; it introduces some of the most important code blocks. Let's take a closer look at some of them!
Comments are blocks 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.
When using the BasicOpMode template we can see there are three comments already clicked into place:
"Put loop blocks here" is similar to our last comment, but is for anything that needs to be repeated the entire time our program is running and will be halted when pressing the stop button.
A variable is a storage location with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. Variables can be numbers, characters, or even motors and servos.
Take a moment to think where else comment blocks may be useful in a program or to communicate with others.
If-then (if-else) statements are similar to the concept of cause and effect. If cause (or condition) happens, then perform effect.
In this case it could be read as "If the OpMode is active (or running) then do the following code."
Pre-added blocks like are comments written by the FIRST Tech Team to help with getting started using the provided template.
shows us where we will be establishing variables, resetting encoders, setting motor directions, and anything else that needs to happen when the code is first activated.
is where anything that will be used when hitting the play button on our Driver Hub should be added.
When the Robot Controller reaches the block it will stop and wait until it receives a Start command from the Driver Hub. Any code after this block will get executed only after the Start button has been pressed.
After the , there is a conditional if block that only gets executed if the OpMode is still active (i.e., a stop command hasn't been received).
You may notice there are two insistences of "opModeIsActive". This allows us to have two options at the start of our program becoming active. The first option has anything that needs to be run only ONCE to be added before our repeat. Then the that follows these blocks is an iterative or looping control structure.
As long as is true those blocks within our loop will remain active when applicable. This is where we will add a majority of our code!
Once the you press the Stop button, the clause is no longer true and the loop will exit.