All pages
Powered by GitBook
1 of 16

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Tackling the Basics Directory - Blocks

Quick Jump Back to a Section

Creating an OpMode - Blocks

Programming Essentials

Programming Servos

Programming Motors

Programming Touch Sensors

Part 1: Tackling the Basics

Now that we have our Control System all set up and ready to program it's time to get a full robot running, right?

While we will be getting motors moving and sensors sensing during this section, it's important that we first start small. In this section, we'll be working with a simple test bed as we breakdown how to program some of the components that can be connected to the Control Hub.

By tackling these components individually we'll be able to explore more of their capabilities, common uses, and discuss errors that may occur while working with a full robot.

During Hello Robot you will encounter sections called "Quick Check!" These pauses are intend to be moments to think deeper on a topic or to self-check your understanding as you progress. It's is expected that the completion of Hello Robot may take multiple days, meetings, or classes.

Quick Check!

As mentioned, during this section we will be focus first on the concept of testing. Why do you think testing might be important in robotics?

Why do you think testing might be important in robotics?

Testing before a design is put into use, or as it is being constructed, helps to proactively identify, isolate, and correct potential issues.

Think about it this way:

Imagine spending all day working on building an arm for your robot so it can climb. The design is a little complex, but after an hour or so you have it connected to your robot and everything program.

You go to turn it on for the first time and.... the motor does not move. You can't tell because of where the motor sits if it is damaged or if something is tightened too much preventing it from moving. The rest of the day is spent taking the arm back off to check and repair.

Now think about how things may have gone if we tested the arm before it was attached to the robot. We don't need everything else to move, just a test code to move its motor. Might save us some time right?

Building a Test Bed

One of the best practices to get into the routine of is testing all your components individually when they are first received. That's where out test bed comes into play. For our test bed we will be sticking to the basics with our components connected directly to our Control Hub rather than something like a Servo Power Module or Expansion Hub. If desired, we could add some mechanical parts, such as a servo horn or wheel, to aid with visualizing our testing, but this is not required.

in this tutorial we'll be using our test bed to learn about programming basics, however it is highly encourage to maintain a test bed for future testing.

Remember when testing a component there may be multiple points of failure such as the port, wire, program, or device itself. Utilizing a test bed helps to narrow down those failure points by making it easier to test and compare in a system's simplest state.

Click to learn more about how a test bed may used in real world applications!

A test bed is a testing environment for hardware and software components, commonly used in the engineering world. Test bed applications includes a broad range of different equipment and measurement testing. In some cases a test bed is a piece of equipment for testing a specific product, in other cases it is a system of components that create a testing environment. Regardless, the end goal of a test bed is to ensure a component is working before it is used for its intended purpose.

To create our test bed for this tutorial you will need the following. The names we used in our configuration are included:

Component
Configuration Name

1

Control Hub

2

Core Hex Motor

test_motor

The design of a test bed depends on the use case and available resources. For instance, one of the design requirements for the test bed featured here was accessibility. Notice that the placement of the hardware components on the Extrusion allows for the actuators, sensors, and Control Hub to be removed or swapped out with ease.

Be sure to complete your configuration on the Driver Hub once you have assembled your test bed.

There are other minor, but important, design considerations to make for a test bed. For example, when adding an actuator to a test bed consider the following questions:

  • What level of constraint does the actuator need? One of the benefits of creating a test bed for motors, or other actuators, is that the motors can be properly constrained during the testing process. In this case providing basic motion support and constraint is valuable.

  • How will you be able to tell the behavior of the actuator? The example test bed uses a wheel with a zip tie to help users visualize the behavior of the motor. Tape or other markers can be used, as well.

Well a test bed is recommended, in the case of time restrictions, space, or other limitations, individual components may be added or removed during each section of Hello Robot. Make sure moving components, such as motors or servos are ALWAYS secured while running, even at low speeds.

Creating an OpMode - Blocks

The time has come to create our first OpMode. We want to make sure to choose a clear and unique name each time we make a program. This will help us to find it again later or to communicate with teammates who may also be driving the robot.

In the programming world, there are common naming conventions that have been established to denote variables, classes, functions, etc. OpModes share some similarities to classes, a program-code-template. Thus the naming convention for OpModes tends to follow the naming convention for classes, which has the first letter of every word is capitalized.

To start, in the REV Hardware Client, select the "Program and Manage" menu tab. In the upper left-hand corner of there is a "Create New OpMode" button, click it:

Clicking the "Create New OpMode" button will open a new window to name and, if applicable, select a sample template for a program. For this guide use the default "BasicOpMode" sample and name the OpMode HelloRobot_TeleOp as shown in the image below.

3

Smart Robot Servo

test_servo

4

REV Touch Sensor

test_touch

5

Color Sensor V3

test_color

6

Battery

Once the OpMode has been named click 'OK' to proceed forward.

Creating an OpMode 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 OpMode - Click this button to save an OpMode to the robot. It is important to save the OpMode any time you stop working on a code, so that progress is not lost. Blocks does not have an autosave feature!

  2. TeleOp/Autonomous - This section of blocks allows users to change between the two types of OpMode: 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. Blocks not currently in the use may be dragged off to the side to be clicked back in later or deleted.

  5. Greeting Message - This intro information message may appear when creating a new, empty OpMode. Clicking the ? icon will close this message.

Blocks includes a nifty tool to view how our code would appear if converted to Java. You can click the button on the far right side to open or close this viewer.

While this feature is designed to aid in the transition between programming platforms, some edits may be required for the Java code to properly compiled if added to an OnBot Java OpMode.

While there are standardized naming conventions in programming, at the end of the day you will want to pick something that makes sense to YOU or your team. This might include having your name, team name, a school class period, or similar in your name.

Your OpMode name should not be the same as a created variable name.

Remember a needs to be completed first before programming! Some blocks or dropdown menus may be hidden from the side menu until a configuration is made active.

Opening Java Viewer:

Programming Servo Telemetry

Telemetry is the process of collecting and transmitting data. In robotics ,telemetry is used to output internal data from the actuators and sensors to the Driver Hub. It is a way for the robot to communicate back to you the programmer what the robot thinks its doing or seeing. This information can then be used to improve your code, make adjustments to a mechanism, or to strategize when driving around the field if competing.

Telemetry blocks in Blocks can be found under the Utilities dropdown menu:

The most useful telemetry from the servo is the position of the servo along its 270° range.

From the telemetry menu, select the block.

Drag the block and place it beneath the if/else if block set. In this scenario, we want our telemetry to be constantly providing output rather than waiting for a designated check.

Programming Motors Basics

Let's start by getting a motor spinning automatically when we hit play on our program!

From the DcMotor menu in Blocks select the block .

Add this block to the OpMode within the while loop. In this scenario we want our motor to continually run so long as our OpMode is active:

Select Save OpMode in the upper lefthand corner of the programming interface.

Try running this OpMode on the test bed and consider the following questions:

Color Sensor Telemetry

We are going to add several telemetry blocks within our program, but let's start by having our robot tell us how much red, green, or blue it sees when looking at an object with our Color Sensor.

For this we will be using the block from the Telemetry menu. We will need three in total, one for each color, which will be entered in the "key".

The "precision" on the block will be changed to 3. Precision sets the number of decimal places!

To "number" we will add the appropriate block from the "Color" menu:

To add telemetry for hue, saturation, and value we will repeat most of the previous process. However, for "number" we will snap in the matching variable block.

Programming Motor Telemetry

Recall that telemetry is the process of collecting and transmitting data. There is a lot of useful information our motors could send back to us, but to start let's have it output the power based on the joystick's movement.

Similar to what we did for our servo program, let's add a block from the telemetry menu to the end of our loop:

From the DcMotor menu pullout the block . Drag the block and attach it to the number parameter on the telemetry blocks.

Change the key parameter to "Motor Power"

When the OpMode is run the telemetry block will display the current power based on the joysticks movement. Give it a try!

configuration
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"

Give your program a go!

What is Telemetry?

Using Telemetry with Servos

For the block "key" is a text box we are able to change to help define what the value is being read out to the Driver Hub's screen. Think of it like an answer key or one used on a map to identify symbols.

Only certain blocks can be added to the second parameter based on what is being requested. In this case the parameter number means the data must be a numeric value.

How fast is the motor running?

  • What happens if you change the power from 1 to 5? What about 100?

  • What happens if you change the power from 1 to 0.3?

  • This is a good time to experiment with different values to see how our motor reacts. You might notice that setting our power to 5 or even 100 does not make the motor spin any fast than when set to 1. But setting our power to 0.3 significantly slows our motor's speed, right?

    Now what happens if you change the power from 1 to -1?

    From our perspective, a power level of 1 probably doesn't sound very strong. However, to our robot the power being set to 1 translates to the motor running at 100%. That would mean setting the power to 0.3 requests the motor to spin at 30% of power.

    When we set our power to a negative power, the motor is told to reverse direction while maintaining that power. So if we set our power to -1 then our motor will still run at 100%, but in the opposite direction than when set to 1.

    Spinning a Motor

    Not seeing your motor listed? Be sure the correct configuration has been activated!

    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.

    Quick Check!

    Setting Direction and Power

    The direction a motor spins may be determined by the power OR may be designated during the initialization process.

    Save your OpMode and give it a try! How do the values change depending on what color object the sensor is looking at?

    While working on your code, you may have noticed something called "alpha". The alpha value of a surface tells how transparent or opaque it may be.

    Using a similar method as before, we can add a telemetry call for the alpha value to see on our Driver Hub.

    Adding Hue, Saturation, and Value Telemetry

    Quick Check!

    This feature requires the Color Sensor's LED to be switched on!

    Alpha Telemetry

    Motors and Telemetry

    Full Program

    Programming Motors

    Modify your OpMode to add the motor related code. For now your completed servo code can be dragged to the side of your work space. You may alternatively choose to create a second program.

    What is a Motor?

    Just like servos, a motor is a form of actuator. You may picture a dozen different things when you think of a motor, from those used to spin the wheels of a car to the large turbines that allow a plane to fly. For our robots, we will be focusing on DC motors. These are a type of electrical motor that use direct current, or DC, to rotate and produce the mechanical force needed to move an attached mechanism.

    For this tutorial, either a Core Hex Motor or HD Hex Motor may be used as long as they have been properly configured on the Driver Hub.

    Most standard motors are able to continuously rotate in either direction with an adjustable speed or power. Some motors may also include a built in encoder, which allows them to move to a specified position, similar to a servo, or to collect data like the number of completed rotations!

    To access the motor snippets in Blocks we need to look under the Actuators dropdown menu:

    You may notice there are several options for blocks under the DcMotor menu. For Hello Robot we will be using those found in the DcMotor menu itself and under Dual.

    As the name suggests, the blocks found under Dual are intended for the use of two motors. We will learn more about them in !

    In the next few sections, we will be learning to program our motor to first move automatically in different directions then in response to our gamepad inputs. In our final section we will take a look at using telemetry with our motor's built in encoder.


    Below is a sneak peek of our final full code:

    Programming a Motor with a Gamepad

    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 most applications, it will be necessary to control the motor with a gamepad, to easily change the direction or power level of a mechanism.

    We are able to use a button to set the motor to a specific power or we can program it so it changes based on the direction of one of the gamepad's joysticks!


    From the Gamepad Menu in Blocks select the Block.

    When using Blocks we are able to snap some blocks together.

    Looking at the block you might notice it is the perfect shape to snap into the end of block, over the 1, like a puzzle piece!

    This set of blocks will now 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.

    Save your OpMode and test it out with your gamepad! Think about the following questions while testing:

    • What happens when you move the left joystick up a small amount versus a large amount?

    • What happens if you move the joystick to the left or right along the X-axis?

    • What happens if you move the joystick at a diagonal or rotate it 360 degrees?

    You may notice that when moving along the X-axis nothing happens at the moment. However, once the joystick hits an angle near the Y-axis vertices the motor may start to jitter and spin.

    When you tested your program, did the motor spin the expected direction while moving the joystick up or down?

    In the FTC SDK for most controllers 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.

    That may be a little confusing to control, but we can add a negative symbol, or negation operator, to the line of code to 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:

    Detecting Color

    We've asked our robot to gather a lot of data with the color sensor. Now let's have it use that information to output an actual color name rather than just a value!

    Detecting Common Colors

    Recall when we learned about using if/else statements while working with the touch sensor.

    Let's first set up the skeleton of our if/else statement for determining different colors:

    Detecting color if/else statement

    Once we've added our block we'll click the gear to add the needed "else if" pieces to have enough for all our colors. To each we will add a block from the Logic menu. Next we can add our variable and a block to each side of this logic statement. We want for each if/else our robot to check if the read hue is LESS THAN a set number!

    Is the seen hue less than _?

    Last we can add our telemetry for our robot to read out information to the Driver Hub based on what it detects:

    Each check will be for a certain color that is within the specified range. "Key" can be changed to "Color" on the telemetry blocks. We can add the colors in first:

    Next we will add our values for the hue ranges. For example, a color that's hue is between 90-149 should appear as green.

    The exact hue values may need to be adjusted slightly, but those used above are based on the default conversion of HSV to RGB when using hue to identify color.

    You'll notice that "red" is detected for values under 30 and above 350. This is intentional as red is the beginning and end of the RGB spectrum!

    Let's snap our If/Else statement into our loop below the "Alpha" telemetry call.

    Save your OpMode and give it a try! You can adjust the values as you need to better reflect the colors available or changes due to lighting in the room.

    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!

    Comments are blocks of code intended to help you the programmer.

    Programming Servo Basics

    Let's start by reviewing 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.

    Let's start by programming our servo to rotate to the default 1 position!

    From the Servo menu, we will primarily be using the block

    Add this block to the op mode code within the .

    Click on the number block to change from to .

    Programming Motor Basics

    Programming a Motor with a Gamepad

    Programming Motor Telemetry

    If you do not see the DcMotor menu under Actuators double check your configuration includes a motor and is currently active on the Driver Hub!

    Let's get Programming!

    Part 2
    Added telemetry blocks
    If/else statement with colors added
    Completed if/else statement
    If/Else statement added to the full program.
    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.

    Pre-added blocks like are comments written by the FIRST Tech Team to help with getting started using the provided template.

    When using the BasicOpMode template we can see there are three comments already clicked into place:

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

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

    Take a moment to think where else comment blocks may be useful in a program or to communicate with others.

    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.

    Key OpMode Blocks

    Comments

    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.

    Where else could we use comment blocks?

    Below is an example of comment blocks used in our 2023-24 Starter Bot Programming Demo:

    Here you can see a comment block has been added to label where the code for the drivetrain is AND to help instruct a driver on how to control the robot!

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

    Select Save OpMode in the upper lefthand corner in the programming interface.

    Let's give our program a try. Take a moment to observe what happens.

    When running our program for the first time, we should have seen our servo move itself to position 1 and maintain that position. But what happens if we run it again? Does the servo move?

    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.

    In this case, we do not want our servo to reset to 0 every time our code repeats. Because of this where do you think we would snap in our block?

    Recall when we discussed the section marked by the comment during Programming Essentials. Since we only want our servo to reset ONCE we will request it do so during the initialization process when the code is first activated, but before play is pressed.

    Go ahead and click a block into place to match the code below:

    In many 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 OpMode runs.

    Take a moment to think about where setting the servo to a known state during initialization may be helpful before moving to the next section!

    Locating the Servo Blocks

    The block above will change names depending on the name of the servo in a configuration file. If there are multiple servos in a configuration file the arrow next to test_servo will drop down a menu of all the servos in a configuration.

    Different block options will appear when using a continuous rotation servo.

    Programming Position Movements

    Quick Check!

    Running our program a second time

    Likely, on a second run our servo did not move since it is already at the correct position. Now check what happens if you first manually rotate the servo while the robot is disabled. Once the code is activated again by pressing play we should see it move again!

    Note: Servos are designed to maintain their position so long as the robot's program is enabled. Trying to forcibly move the servo while ON may damage it and is not recommended.

    If your servo did not move as expected, double check your wiring and port are correct compared to your configuration.

    Resetting Back to Zero

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

    • What is different from the previous run?

    Quick Check!

    Adjusting Y-axis Direction

    Programming Servos

    What is a 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 or changing the Servo's mode via the by clicking the hyperlinks.

    A servo is a form of actuator or a device designed for moving. With a typical servo, you can specify a target position. 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 Hello Robot we will be using the Smart Robot Servo, which is able to switch between a continuous and angular mode.

    • Continuous mode allows for the servo to rotate a full 360°, either direction, indefinitely similar to a standard motor.

    • Angular mode sets the servo to move to specified positions within a 270° range of motion.

    Let's take a look at how to program our servo while it is on angular mode:

    While most common servos have a range of 180° for motion, the Smart Robot Servo has a range of 270° due to its ability to switch between modes. When programming this means our 0 and 1 position might be a little different than what you'd expect.

    Looking at the image above we can see that on default when asking our servo to move to its position 0 it will be at -135° . On the opposite end, moving to its position 1 takes our servo to +135° . Therefore if we wanted to return to 0° we would need to program it to move to its position 0.5.

    Let's review quick our basic positions:

    Programmed Position
    Degrees

    Based on what we've learned so far, think about the following two questions:

    1. If we wanted our servo to move to -67.5° what position would we program it move to?

    2. If we have programmed our servo to move to position 0.7, what would that equal in degrees?

    In the next few sections, we will be learning to program our servo to first move automatically to different requested positions then in response to our gamepad's input.


    Below is a sneak peek of our final full code:

    Programming Touch Sensors

    This section applies to the use of the REV or . Requirements may vary when using other 3rd party touch sensors.

    The REV Touch Sensor must be configured to digital port 1, 3, 5, or 7.

    It is recommended to create a new OpMode while following this tutorial. Ours is named HelloRobot_TouchSensor!

    The touch sensor block is now found under the "Sensors" dropdown as seen below:

    Touch Sensor Basics

    Let's start by breaking down how a touch sensor works at its core!

    Remember what sensors and motors are available in your program are determined by your configuration! Double check the correct configuration is active if you do not see a device list.

    The information collected by a touch sensor comes in two states, also known as binary states. This information is perfect to use with a conditional statement like an if/else statement.

    The block collects the binary TRUE/FALSE state from the touch sensor and acts as the condition for the if/else statement.

    Let's take a look at our touch sensor block paired with our block:

    Take a moment to think what this code is asking the robot to do. We could read this line of code as "If the touch sensor is pressed do ____, else if the touch sensor is not pressed do _____."

    It's always helpful for us to be able to see what the robot thinks its doing on our Driver Hub's screen. To do this, let's request the robot shares some telemetry data while our program is active.

    We can access the "Telemetry" blocks under our "Utilities" dropdown on the menu. Look for the block to be added in each section of the if/else statement.

    What happens if you run the program right now?

    When on the default "Telemetry" block the information provided is not helpful for the robot to communicate with us. Therefore we need to change "key" and "text" to match the desired information.

    The "key" should be something related to which sensor, motor, or other device we are receiving information from. Meanwhile "text" will tell us what is happening based on the state of our touch sensor and our if/else statement.

    Let's give our code another try to see what happens on the Driver Hub's Screen. Did you see something like the following?

    Remember its up to us to decide what our telemetry readout says. With that in mind we could change it so our robot says "Hello World" when the button is pressed:

    At the moment, our robot does not have any senses to help navigate the world around it like you might. However, that's the key advantage to adding sensors to our design.

    For the touch sensor, one of the most common uses is for it to act as a . This will help the robot know when it needs to halt the movement of a mechanism, like an arm or lift, that's at its limit similar to how your nerves help to tell your brain to do the same.

    We can test this idea by adding on to our existing if/else statement. This time we are going to ask our motor to move until our sensor is pressed using the block:

    In the above example the if/else is checking first for if the touch sensor is pressed. The full statement could be read as "If the touch sensor is pressed set the motor's power to 0 else, if it is not pressed, set the power to 0.3".

    There may be situations where we want our program to read if the touch is NOT pressed first. Let's take a quick look at how that would function using the block from the "Logic" menu.

    Give it a try!

    Programming Color Sensors

    It is recommended to create a new OpMode while following this tutorial. Ours is named HelloRobot_ColorSensor!

    The color and light sensor menus are found under the "Sensors" dropdown as seen below: Additional blocks to set or call colors are within the "Color" menu under Utilities:

    Color Sensor Basics:

    While a touch sensor features a physical switch to gather information, a color sensor makes use of reflected light. By doing so it collects different data to determine how much light it is seeing, the distance to a surface, and of course what color is in front of it.

    But what makes up a color?

    For our robot we're going to focus on a few key components: hue, saturation, and value. With these we can use something known as the HSV color model to have the robot translate what its seeing into a recognizable color.

    HSV is a form of a cylindrical RGB color model used to do things like create color pickers for digital painting programs, to edit photos, and for programming vision code.

    Hue, saturation, and value all will play a part in helping our robot tell us what color it detects and allow us to make adjustments for something like a uniquely colored game piece!

    Detecting Light vs. Dark

    Before we tackle colors, let's start with having our robot use the color sensor to tell us how much light is being reflected.

    To start, let's grab a block to add to our loop. Our "key" should be set to "Light detected":

    To the "number" place we will pull a block from the color sensor menu:

    Time to test your program to see what your color sensor detects! While testing think about the following questions:

    • Is the number higher when less or more light is detected?

    • What happens when the color sensor looks at different color surfaces?

    • Does the value change when turning the color sensor's LED light on or off?

    Let's start by establishing a few variables in our program.

    We'll be going over is in more detail during Part 2: Robot Control, but for this example we are using them to help our robot translate the data it records more clearly. Our variables will be called "color", "hue", "saturation", "value", and "normalizedColors".

    We've discussed how most of these are related to the HSV color model, but what about normalizedColors?

    Color Normalization is another technique within vision programming intended to help compensate for differences caused by lighting and shadows when looking at colors. This also affects shades of a color. For example, there are a ton of different shades of blue, such as cyan, navy, and aquamarine, but to our robot these will all be referenced as blue.

    Now that we've named our variables, we need to set them to different values.

    From our variable menu we need a block. From the dropdown menu, we can change it to "normalizedColors". Next we will snap it in place with a block from the Color Sensor menu below our light detecting telemetry:

    Next, let's go ahead and add set blocks for all our variables:

    To each we can connect their corresponding block from the Color menu under Utilities:

    Next we need to change our variable name from the default of "myColor".

    Notice that "color" is matched with NormalizedColors using the matching variable while the rest have the variable set to "color".

    From here we can add our telemetry blocks to see what values the color sensor detects!

    Does the value change if there is a shadow or if the lighting in the room changes?

    Quick Check!

    What happened?

    Likely, the numbers and differences you saw while testing are different than those we'd see ourselves. There are many factors that might change the color sensor's readings including the lighting in the room and surface material.

    However, one thing that is the same is that 1 should be the least amount of light, such as when your hand is covering the sensor, and 0 is the most amount of light being seen.

    Establishing Variables

    Defining Variables

    what a variable
    NormalizedColors is at the bottom of the list.

    0

    -135°

    0.5

    0°

    1

    135°

    A servo horn attachment connected to your Smart Robot Servo may effect where 0° appears. We recommend using a SRS programmer to set the servo to zero before adding attachments. This may also be done using the code learned in this section!

    Quick Check

    Click to reveal the answers
    1. Breaking it down we can see that -67.5° would be half of our movement between 0° and -135° . Therefore, we will set our position to halfway between 0 and 0.5 equaling 0.25.

    2. This second question requires a little more math. Let's think about how far our servo would move for each 0.1 of a position. Our total movement is 270° so if divided by 10 we know 0.1 = 27°. Going from there we can do the math starting from the 0° we know position 0.5 brings us to. Therefore, moving to position 0.7 should be 54°.

    For Hello Robot we will only be programming using positions. Understanding their translation to degrees is still important, however, to help think through designing a mechanism. Degrees may also be preferred when using a direct to program the servo.

    Let's get Programming!

    Smart Robot Servo
    SRS Programmer

    Adding Telemetry

    Take a moment to think about how else telemetry data could be used with your robot before moving on to the next section!

    Touch Sensor as a Limit Switch

    Reversing it

    limit switch
    Touch Sensor
    Limit Switch
    Programming Servo Basics
    Using a Gamepad with a Servo
    Programming Servo Telemetry
    pulse input

    Using a Gamepad with a Servo

    Programming a Servo with a Gamepad

    Having our robot able to rotate the servo automatically can be incredibly useful, especially when writing an autonomous program, but what if I want to control the positions with my gamepad?

    Let's take a look at how we can add input commands to our code!

    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, selecting the appropriate button from the dropdown in Blocks may be easier to follow when looking back at your code. The buttons are also interchangeable when programming in Blocks. (ex: Y in code = Triangle pressed on controller)

    Button

    Degree Position

    Blocks for adding controller inputs can be found in the "Gamepad" menu:

    One of the most common logic statements used in programming is an if/else statement, also known as an if/then statement. This block can be found under the "Logic" menu in Blocks:

    In its most simple format we will be asking our robot to check IF something is happening and if the answer is yes, or true in our robot's mind, THEN it will DO what has been asked.

    During this section we are going to be asking "If the Y button is pressed on our controller then move our servo to position 0."

    If our servo will move to position 0 when the previous statement is TRUE, what do expect to happen when the answer is FALSE (or the Y button is not pressed)?

    Our if/else statement can come in many forms that includes multiple different conditional statements. Blocks allows for our base block to be easily added to add as many conditional statements as we need by clicking the blue gear on our block.

    Adding an block by clicking and dragging to our existing if statement converts it into becoming an if/else if statement. Using our previous example we can see how this may look in Blocks:

    Now our statement is checking first if Y is being pressed to move to position 0, but has added now the option to look for something else, such as another button being pressed.

    Let's add to our existing logic statement the ability to move our servo to position 1 when A is pressed on our controller. Give it a try first before revealing the answer below!

    How would our full logic statement be read once our new blocks are added?

    To add all of our gamepad inputs we need to further extend our if/else if statement:

    Now there are three different paths in our if/else if block that our robot may follow based on each input request. We've previously added our ability to move to position 0 and 1, but what about 0.5?

    You may have noticed in our gamepad chart at the beginning of this section that we are going to have two buttons able to move our servo to position 0.5. This is so we can practice using a logical operator like the block in our program!

    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 dropdown menu on the block to change it from an block to an block.

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

    Now to finish by adding our 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 this if/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. This means the order we add our pathways DOES matter. If X and A are pressed at the same time, the robot will will try to prioritize the X button first.

    Give it a try!

    Code Position

    Y/Triangle

    -135

    0

    X/Square

    0

    0.5

    B/Circle

    0

    0.5

    A/Cross

    135

    1

    Introducing If/Else Statements

    Quick Check!

    What will happen when the answer is FALSE?

    At the moment, we have not asked our robot to do anything specific when our statement is false. This means for now our servo will not move or change while our Y button is not pressed.

    If/Else If Statements

    Quick Check!

    Reveal the answer!

    Programming our servo to move to position 1 when A is pressed will look very similar to our existing code:

    Now our statement reads: "If the Y button is pressed then move the servo to position 0, else if the A button is pressed then move the servo to position 1."

    If you have not already, test the code we have written thus far! Our logic statement should be added to our and previous removed.

    • What happens when both buttons are pressed at the same time?

    Adding Logic Operators

    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.

    Similar the logical operator and considers two operands requiring both to be true for the whole statement to be true.

    Our previously added blocks for A and Y inputs can be temporarily moved to the side in the workspace to be readded as applicable.

    Click Save OpMode and give your program a try!

    Full Program