Search…
Test Bed - Blocks
The Blocks Programming Tool is a visual, programming tool that lets programmers use a web browser to create, edit and save their op modes. Blocks, like other scratch based programming tools, is a collection of preset code snippets that users can drag-and-drop into the appropriate code line. In this section users can learn how to create an op mode, as wells as the basics of programming the actuators and sensors featured on the test bed.
Follow the guide in order to get an in depth understanding of working with Blocks or navigate to the section that fits your needs:
Section
Goals of Section
Focuses on how to navigate the Blocks interface and create an op mode.
Breaks down the structure and key elelments needed for an op mode, as well as some of the essential components of Blocks and programming logic.
How to code servos and motors. This section walks through the basic logic of coding actuators, controlling actuators with a gamepad, and using telemetry.
How to code a digital device. The section focuses on the basic logic of coding a digital device, like a REV Touch Sensor.

Creating an Op Mode

Before diving in and creating your first op mode, you should consider the concept of naming conventions. When writing code the goal is to be as clear as possible about what is happening within the code. This is where the concept of naming conventions comes into play. Common naming conventions have been established by the programming world to denote variables, classes, functions, etc. Op modes share some similarities to classes. Thus the naming convention for op modes tends to follow the naming convention for classes; where the first letter of every word is capitalized.
This section assumes that you have already accessed the Blocks platform during the Hello Robot - Introduction to Programming. If you are unsure how to access blocks please revisit this section before proceeding.
To start, access the Robot Controller Console and go to the Blocks page. In the upper right-hand corner of there is a Create New Op Mode button, click it.
Clicking the Create New Op Mode button will open up the Create New Op Mode window. This window allows users to name their op modes and select a sample code to build off of. For this guide use the default BasicOpMode sample and name the op mod HelloRobot_TeleOp as shown in the image below.
Once the op mode has been named click 'OK' to proceed forward. Creating an op mode will open up the main Blocks programming page. Before moving on to programming, take some time to learn and understand the following key components of Blocks featured in the image below.
  1. 1.
    Save Op Mode - Click this button to save an op mode to the robot. It is important to save the op mode any time you stop working on a code, so that progress is not lost.
  2. 2.
    TeleOp/Autonomous - This section of blocks allows users to change between the two types of op modes: teleop and autonomous.
  3. 3.
    Categorized Blocks - This section of the screen is where the programming blocks are categorized and accessible. For instance, clicking Logic will open access to programming blocks like if/else statements.
  4. 4.
    Programming Space - This space is where blocks are added to build programs.
If a configuration has been made then the Actuators, Sensors, and Other Devices in the Categorized Blocks section should appear as drop down menus, where blocks that are unique to specific hardware can be accessed. If this is not the case a configuration file has not been made. For more information visit the Configuration page, before moving forward with programming.

Programming Essentials

During the process of creating an op mode 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 op mode.
An op mode 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 op mode to properly function.
Though this sample is given to users to reduce some of complexities of programming as they learn; it introduces some of the most important code blocks. It is also important to understand what is happening in the structure of the BasicOpMode, so that code blocks are put in the correct area.

Key Op Mode Blocks

Comments are blocks of code that benefit the human user. They are used by programmers 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. Blocks like
are comments written by the FIRST Tech Team to inform the user what will happen when blocks are added directly beneath the comment.
For instance, any programming blocks that are placed after the
comment (and before the
block) will be executed when the op mode is first selected by a user at the Driver Station. Typically, blocks put in this section are meant to create and define variables between the initialization and start phases of the op mode.
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.
When the Robot Controller reaches the block
it will stop and wait until it receives a Start command from the Driver Station. A Start command will not be sent until the user pushes the Start button on the Driver Station. Any code after the
block will get executed after the Start button has been pressed.
After the
, there is a conditional if block
that only gets executed if the op mode is still active (i.e., a stop command hasn't been received).
If-then (if-else) statements are similar to the concept of cause and effect. If cause (or condition) happens, then perform effect.
Any blocks that are placed after the
comment and before the
will be executed sequentially by the Robot Controller after the Start button has been pressed.
The
is an iterative or looping control structure.
This control will perform the steps listed under the “do” portion of the block as long as the condition
is true. What this means is that the statements included in the “do” portion of the block will repeatedly be executed as long as the op mode HelloRobot_TeleOp is running.
Once the user presses the Stop button, the
clause is no longer true and the
loop will stop repeating itself.

Functions and Methods

The previous section did not go into a detailed discussion of the purple function (or method) blocks. Functions and methods are similar procedures in programming that are more advance than what will be covered in this guide.
For now the most important thing to know is that occasionally methods within the SDK libraries will need to be called in order to perform a certain task. For instance, the
line calls the method opModeIsActive, which is the procedure in the SDK that is able to tell when the robot was been started or stopped.
When your programming skills have advanced take sometime to visit the concepts of functions and methods and explore how they can help you enhance your code.

Programming Actuators

Servo Basics

The goal of this section is to cover some of the basics of programming a servo within Blocks. By the end of this section users should be able to control a servo with a gamepad, as well as understand some of the key programming needs of the servo.
This section is considering the Smart Robot Servo in its default mode. If your servo has been changed to function in continuous mode or with angular limits it will not behave the same using the code examples below. You can learn more about the Smart Robot Servo or changing the Servo's mode via the SRS Programmer by clicking the hyperlinks.
With a typical servo, you can specify a target position for the servo. The servo will turn its motor shaft to move to the target position, and then maintain that position, even if moderate forces are applied to try and disturb its position.
For both Blocks and OnBot Java, you can specify a target position that ranges from 0 to 1 for a servo. For a servo with a 270° range, if the input range was from 0 to 1 then a signal input of 0 would cause the servo to turn to point -135°. For a signal input of 1, the servo would turn to +135°. Inputs between the minimum and maximum have corresponding angles evenly distributed between the minimum and maximum servo angle. This is important to keep in mind as you learn how to code servos.
Since this section will focus on servos it is important to understand how to access servos within Blocks. At the top of the Categorize Blocks section there is a drop down menu for Actuators. When the menu is selected it will drop down two choices: DcMotor or Servo. Selecting Servo will open a side window filled with various servo related blocks.

Programming a Servo

From the Servo menu select the block
The block above will change names depending on the name of the servo in a configuration file. If there are multiple motors in a configuration file the arrow next to test_servo will drop down a menu of all the servos in a configuration.
Add this block to the op mode code within the
. Click on the number block to change from
to
.
Select Save Op Mode in the upper right corner in the Robot Controller Console.
Try running this op mode on the test bed two times and consider the following questions:
  • Did the servo move during the first run?
  • Did the servo move during the second run?
If the servo did not move switch from
back to
and try again.
The intent of the
is to set the position of the servo. If the servo is already in the set position when a code is run, it will not change positions. Lets try adding another
block and see what changes.
Drag an additional
block into the op mode code under the
comment.
Try running this op mode on the test bed and consider the following question:
  • What is different from the previous run?
The
that was added in the step above changes the servo position to 0 during the initialization phase, so when the op mode is run the servo will always move to position 1. For some applications starting the servo in a known state, like at position zero, is beneficial to the operation of a mechanism. Setting the servo to the known state in the initialization ensures it is in the correct position when the op mode runs.

Programming a Servo with a Gamepad

The focus of this example is to assign certain servo positions to buttons on the gamepad. For this example the known state will stay at position 0, so that after initialization the servo will be a the -135 degree position of the servo range. The following list shows what buttons correspond with which servo position.
If you are using a PS4 Controller, like the Etpark Wired Controller for PS4 (REV-39-1865), see the Using Gamepads section to determine how the gamepad code used in this section translates to the PS4 Gamepad.
Button
Degree Position
Code Position
Y
-135
0
X
0
0.5
B
0
0.5
A
135
1
The best way to switch the servo position will be to use a conditional if/ else if statement. An if statement considers whether a conditional statement is true or false. If the conditional statement is true a defined action (like the servo moving) is performed. If the conditional statement is false the action is not performed.
An if/ else ifstatement takes in multiple different conditional statements. If the first conditional statement is found to be false then the second conditional state is analyzed. Each statement in the if/ else if will be analyzed one by one until a statement is found true or all statements are found false. For this example, there will be three conditions that will need to be checked.
From the Logic Menu in Blocks select the
block and drag in into the op mode's while loop.
Click on the blue and white Settings icon for the
block. This will display a pop-up menu that lets you modify the
block.
Drag an
block from the left side of the pop-up menu and snap it into place under the
block. Drag a second
block from the left side and snap it into place on the right side under the first
block.
There are three different paths in this if/else if block. Each one corresponds with one of the three chosen servo positions 0, 0.5, and 1. However, there are four different buttons that will be used for this example. Both button B and button X should be able to move the servo to position 0.5. In order to do this the logical operator or needs to be used.
The logical operator or considers two operands if either (or both) are true the or statement is true. If both operands are false the or statement is false.
From the Logic Menu in Blocks select the
block.
Add this block to the If/else if block, as shown in the image below. Use the drop down menu on the block to change it from an
block to an
block.
All gamepad related blocks are in the Gamepad Menu.
Add each button to the if/else if block as seen in the image below.
Add
blocks to each section of the If/else if block. Set the servo position to correspond with the assigned gamepad button.
There are three different paths in thisif/else if statement. If the first conditional statement is true (the Y button is pressed) the servo moves to code position 0 and the other conditional statements are ignored. If the first condition is false (the Y button is not pressed) the second condition is analyzed. Recall that this behavior repeats until a condition is met or all conditions have been tested and found false.

Servos and Telemetry

Telemetry is the process of collecting and transmitting data. In Robotics telemetry is used to output internal data from actuators and sensors to the Driver Station. This data can then be analyzed by users to make decisions that can improve code.
The most useful telemetry from the servo is the the position of the servo along its 270 degree range. In order to get that information the following line needs to be used.
In order to access the telemetry blocks select the Utilities drop down. The utilities drop down is in alphabetical order, so telemetry is towards the bottom of the drop down options. Select the
block from the telemetry menu.
Drag the
block and place it beneath theif/else if block set.
From the Servo menu pullout the block
Drag the Block and attach it to the number parameter on the telemetry blocks.
Change the key parameter to "Servo Position"
When the op mode is run the telemetry block will display the current position information will be displayed with the Servo Position Key. The number that corresponds with the current position will change as the servo shaft position changes.

Motor Basics

Modify your op mode to add the motor related code. This can be done by clearing out your current code modifications or adding the motor related code to your current op mode.
The goal of this section is to cover some of the basics of programming a motor within Blocks. By the end of this section users should be able to control a motor using a gamepad, as well as understand some of the basics of working with motor encoders.
Since this section will focus on motors it is important to understand how to access motors within Blocks. At the top of the Categorize Blocks section there is a drop down menu for Actuators. When the menu is selected it will drop down two choices: DcMotor or Servo. Selecting DC Motor will open a side window filled with various motor related blocks.

Driving Motors

From the Dc Motor menu in Blocks select the block
.
The block above will change names depending on the name of the motor in a configuration file. If there are multiple motors in a configuration file the arrow next to test_motor will drop down a menu of all the motors in a configuration.
Add this block to the op mode code within the while loop.
Select Save Op Mode in the upper right corner in the Robot Controller Console.
Try running this op mode on the test bed and consider the following questions:
  • How fast is the motor running?
  • What happens if you change the power from 1 to 0.3?
  • What happens if you change the power to -1?
The level of power sent to the motor is dependent on the numerical number assigned to the motor. The change from 1 to 0.3 decreased the motors speed from 100% of duty cycle to 30% of duty cycle. Meanwhile, the change to -1 allowed the motor to rotate at 100% duty cycle in the opposite direction. So, power can be fluctuated to drive a motor forward or backwards.
However, the
block will run the motor in the assigned direction until something in the code stops the motor or causes a change in direction.
To better understand motors and the concept of duty cycle check out the our Motors and Choosing an Actuator documentation.

Driving Motors with the Gamepad

In the previous section you learned how to set the motor to run at a specific power level in a specific direction. However, in some applications, it may be necessary to control the motor with a gamepad, to easily change the direction or power level of a mechanism.
From the Gamepad Menu in Blocks select the
Block.
Drag the
block so it snaps in place onto the right side of the
block. This set of blocks will continually loop and read the value of gamepad #1’s left joystick (the y position) and set the motor power to the Y value of the left joystick.
Note that for the Logitech F310 gamepads, the Y value of a joystick ranges from -1, when a joystick is in its topmost position, to +1, when a joystick is in its bottommost position. If the motor is not running in the intended direction adding a negative symbol, or negation operator, to the line of code will change the direction of the motor in relation to the gamepad.
From the Math Menu in Blocks select the
block in the image below.
Drag the negative symbol block so it snaps in place between the
and
blocks.

Motors and Telemetry

Recall that telemetry is the process of collecting and transmitting data. In Robotics telemetry is used to output internal data from actuators and sensors to the Driver Station. This data can then be analyzed by users to make decisions that can improve code.
In order to gain telemetry data from the motor, motor encoders need to be used. REV DC Motors, like the Core Hex Motor, are equipped with internal encoders that relay information in the form of counts.
In order to access the telemetry blocks select the Utilities drop down. The utilities drop down is in alphabetical order, so telemetry is towards the bottom of the drop down options. Select the
block from the telemetry menu.
Drag the
block and place it beneath the
block set.
From the DC Motor menu pullout the block
. Drag the Block and attach it to the number parameter on the telemetry blocks.
Change the key parameter to "Counts Per Revolution: "
When the op mode is run the telemetry block will display the current position information will be displayed with the Counts Per Revolution Key. The number that corresponds with the current position will change as the motor shaft position is changed.
For more information on programming encoders check out the Using Encoders page. For more information the counts per revolution metric and how to use it check out the Encoders page.

Programming Sensors

Touch Sensor Basics

Modify your op mode to add the digital device related code. This can be done by clearing out your current code modifications or adding the digital device code to your op mode.
The goal of this section is to cover some of the basics of programming a digital device, or Touch Sensor, within Blocks.
Since this section will focus on digital devices it is important to understand how to access digital device specific blocks. At the top of the Categorize Blocks section there is a drop down menu for Other Devices. When the menu is selected it will drop down an option for Digital Devices. Selecting Digital Devices will open a side window filled with various digital device related blocks. The one that will most commonly be used is
.
Before programming with a Touch Sensor or other digital device it is important to understand what a digital device is and what the common applications for digital devices are. Visit the Digital Sensors page for more info.

Programming a Digital Device

The information from digital devices comes in two states, also known as binary states. The most common way to utilize this information is to use a conditional statement like an if/else statement.
From the Logic Menu in Blocks select the
block.
Drag the
block and place it beneath the
comment.
Select a
block from the Digital Devices menu and add it to the if/do/else block as shown in the image below.
The
block stores the binary FALSE/TRUE information from the touch sensor and acts as the condition for the
statement. If
is true, any code placed in the do portion of the block will be activated. If
is false anything placed in the else portion of the clock will be activated
The FALSE/TRUEstate of a REV Touch Sensor corresponds with whether or not the button on the Touch Sensor is pressed. When the button is not pressed the state of the Touch Sensor is True. When the button is pressed the state of the Touch Sensor is False,
To help remember how the physical and digital states of the sensor correspond in the next few sections lets use some comments.
Comment blocks can be found in the Miscellaneous menu.
Place one comment block in the do portion of the
block and change the comment to say
Add another comment to the else portion of the block and change that comment to say
, as shown in the image below.
The next step in the process is to use telemetry to display the status of the Touch Sensor on the Driver Station phone. To do this, lets create a string variable called touchStatus.
String refers to data that consists of a sequence of characters.
  1. 1.
    Click on the Variables menu. This will open a side window
  2. 2.
    Select the Create variable... block
  3. 3.
    A prompt from the FIRST Robot Controller will appear asking for a name for the variable. Name the variable touchStatus. Click okay
This process created a variable named touchStatus. Currently touchStatus is undefined, in order to define it the
block needs to be used. This block can be found in the Variables menu now that the variable has been created.
Drag a
block and place it beneath the
comment block. Add another
block to the
block set under the
comment.
The
block allows you to define the touchStatus variable. Depending on what the status is of the touch sensor is, touchStatuswill be set to a different string. For this select the string
block from the Text menu, as seen in the image below.
Attach a string block to both
blocks. Fill the
blocks with a status message that relates to the state of the Touch Sensor. For instance,
and
.
To display this information on the Driver Station phone telemetry must be used. In order to access the telemetry blocks select the Utilities drop down. The utilities drop down is in alphabetical order, so telemetry is towards the bottom of the drop down options. Select the
block from the telemetry menu.
Drag the
block and place it beneath the
block set.
From the Variables menu select the
block. Drag the Block and attach it to the text parameter on the telemetry block.
Change the key parameter to "Button Status: "
When this program is run the touchStatus telemetry will appear on the Driver Station phone. The touchStatus information will change based on the state of the Touch Sensor button.

Digital Devices as Limit Switches

One of the most common uses for a digital device like a touch sensor is to use it as a limit switch. The intent of a limit switch is to stop a mechanism, like an arm or lift, before it exceeds its physical limitations. In this application power needs to be cut from the motor when the limit is met.
The concept of a limit switch involves many of the same steps from the previous section on programming a digital device. For that reason lets pick up from the following block set:
The
block establishes a conditional environment for the limit switch. If the touch sensor is not pressed the motor can run, however, if it is pressed the motor can not run. To add this to the code the
block needs to be used.
For information on where to find motor specific blocks please revisit the motor section.
Add a
block under the
comment. Change the power to 0.3. Add another
block under the
comment. Change the power to 0.
This
block introduces the basics of a limit switch. Like with most sensors, its good to have telemetry that updates the Driver Station on the status of the sensor. Consider the code from the previous section, or the following code as potential ideas for telemetry.
Export as PDF
Copy link
On this page
Creating an Op Mode
Programming Essentials
Programming Actuators
Servo Basics
Motor Basics
Programming Sensors
Touch Sensor Basics