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. 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. TeleOp/Autonomous - This section of blocks allows users to change between the two types of op modes: teleop and autonomous.

  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. 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

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.

If-then (if-else) statements are similar to the concept of cause and effect. If cause (or condition) happens, then perform effect.

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.

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

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.

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?

Try running this op mode on the test bed and consider the following question:

  • What is different from the previous run?

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.

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.

All gamepad related blocks are in the Gamepad Menu.

Add each button to the if/else if block as seen in the image below.

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.

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

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.

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.

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.

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.

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.

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.

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.

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. Click on the Variables menu. This will open a side window

  2. Select the Create variable... block

  3. A prompt from the FIRST Robot Controller will appear asking for a name for the variable. Name the variable touchStatus. Click okay

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:

For information on where to find motor specific blocks please revisit the motor section.

Last updated