RobotAutoDriveToLine_Blocks Sample Program

This OpMode illustrates the concept of driving up to a line and then stopping. This is a Blocks sample program based on the Java sample program for driving to a line.

This is a tutorial on creating FIRST Tech Challenge (FTC) autonomous programs for the CENTERSTAGE game.

Visit the FTC docs page on Creating Op Modes in Blocks if you need help getting started and to create your first driver controlled program.

This OpMode illustrates the concept of driving and using a light sensor to stop on a white line.

Prerequisites/Assumptions

This tutorial assumes you’ve got some familiarity with Blocks and that you have a robot configuration in your driver station. Ideally you’ve completed a Blocks tutorial. This tutorial doesn’t explain how to program in blocks.

This tutorial assumes you have a robot with two driving wheels, normally referred to as a tank drive or pushbot drive.

A light sensor is required for this program, this robot has a REV Color Sensor V3.

A webcam is not required for this program.

RobotAutoDriveToLine_Blocks

The Sensor used here can be a REV Color Sensor V2 or V3. Make sure the white LED is turned on. The sensor can be plugged into any I2C port, and must be named “sensor_color” in the active configuration.

Depending on the height of your color sensor, you may want to set the sensor “gain”. The higher the gain, the greater the reflected light reading will be.

Use the SensorColor sample in this folder to determine the minimum gain value that provides an “Alpha” reading of 1.0 when you are on top of the white line. In this sample, we use a gain of 15 which works well with a Rev V2 color sensor.

  • For a Rev V3 color sensor on my robot a value of 30 provided an Alpha reading of 0.95.

Setting the correct WHITE_THRESHOLD value is key to stopping correctly. This should be set halfway between the bare-tile, and white-line “Alpha” values. The reflected light value can be read on the screen once the OpMode has been INIT, but before it is STARTED. Move the sensor on and off the white line and note the min and max readings.

Edit this code to make WHITE_THRESHOLD halfway between the min and max.

The RobotAutoDriveToLine_Blocks program is a not a long program. We’re not going to create it step by step. Instead, please copy the .blk file from the Pushbot GitHub repository and then I’ll explain how the program works.

Right click and select Download on GitHub file RobotAutoDriveToLine_Blocks .blk.

You can then connect to your robot and ‘upload’ the file to your robot, give the program the name RobotAutoDriveToLine_Blocks. Open this program in Blocks.

The first step in this program is to initialize variables and set one of the motors to reverse direction. The program then does into a loop to display the current brightness value. You can modify the set sensor_color.gain block to a value that creates an Alpha brightness near 1.

The above loop uses a getBrightness function. That function gets the current reading from the color sensor using getNormalizedColors. It then returns the Alpha value from that reading as the brightness level. See the SensorColors sample program that shows all the various color values you can get. In addition to Red, Blue and Green readings you can convert the color to a Hue, Saturation and Value (HSV) color format.

When the Start button is pressed the robot starts moving. It then goes into a loop waiting for the color sensor to detect the white line. When the getBrightness value exceeds the WHITE_THRESHOLD value the loop exits and the motors are set to zero power.

Here’s a YouTube video showing the robot running this program.

You should save the program and try running it. The robot should drive forward until it reaches a white line.

Next Steps

Change this function to detect a Red or Blue tape line. You would want to lower the GAIN value to perhaps 2, and then test actual red and blue tape lines to determine the correct thresholds to use. Test the tape lines with the SensorColor program. You might find it better to use the Hue value make it easier to separate the sensor readings from the grey tiles to the tape lines.

Detecting the tape lines might be useful for dropping the purple pixel on the spike mark in the CENTERSTAGE game. Most FTC games had tape lines on the field that can be used.

Check out the other Blocks sample programs:

Blocks Sample Programs

Getting Help

It is often possible to use Google (or other search engine) to get help or solve problems. There are lots of resources online. If you’re still stuck you can ask for help here.