Programming Essentials
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!
Key OpMode Blocks
Comments
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.
Call waitForStart
Call opModeIsActive
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."
Last updated