Our journey into line following began in the previous tutorial by using SIMPLE SCRIBBLER blocks to monitor the line sensors and and provide feedback with the user-defined LEDs. Once we had the right feedback, we created a simple line following program.
Here, we will look at the line sensor reading block. While the detect line block gives us data about whether the sensor is or isn’t over a line (with some variations on that theme), the more advanced SENSORS > LINE catgeory light sensor reading block gives us more detailed information. Take a look in that category and there are two blocks. The first block you have already seen, but the second block - the line sensor reading block – is new. With its drop-down menu open, it looks like this:
It provides a value of 0% to 100% that quantifies the line sensor reflectivity for the left (default) and right sensors. As you can see, this is keyed input block for insertion into other blocks.
Understanding and Measuring Line Reflectivity
- To better understand how this block works, construct the follow program.
This program uses blocks from the CONTROL, VARIABLES, MATH, SENSOR > LINE, and ACTIONS > COMMUNICATE categories. If you're unclear of what these COMMUNICATE blocks do, take some time to predict what will happen when this program runs. If your predictions were not accurate, spend some time to familiarize yourself with the blocks you didn't understand. Remember, you can also check the Scribbler 3 Block Reference Help if you need a block explained.
The first section of 6 blocks pauses for a second to start, and then gets the Terminal up and working. The message, "BlocklyProp Terminal Working" lasts for 2 seconds and then the screen is cleared for the feedback messages to begin and the program to continue. The next 7 blocks set up the title and the column categories for the left sensor and right sensor data, then waits half a second before continuing. The repeat forever block contains the last of the program, and it will repeat these blocks indefinitely. Variables are set for the left and right reflectivity value percentages and a message is printed in the appropriate column showing the sensor reading in real time.
- Print out the Scribbler Printable Line Following Tracks pdf if you don't have some on hand (make two copies of page 11 for later on).
- Save, compile, load to EEPROM, and run the program. This time the USB cable must remain connected between your computer and the S3 and your BlocklyProp workspace should still be open and visible.
The cable provides a link between the robot and the BlocklyProp Terminal software. When the program begins, the Terminal window will open. When the connection is established with the Terminal, you should see something like this (your numbers may differ):
- Place your robot on a flat surface on top of a piece of paper with a printed black line in its center.
- Now slide the S3 from the left to the right - back and forth across the line, very close to the paper.
Now we can begin the process of understanding the information. See how the percentage values change as S3’s line sensors move relative to the black line? If you want, conduct similar tests with a 3/4” black masking tape line, 3/4” black electrical tape line, or an approximately 3/4” wide Sharpie® marker line - all on white paper or poster board. You can also try white lines on a dark or black surface.
- Make a note of your readings for the various line types and surfaces for future reference on a chart.
An example of a suitable chart is below, with extra spaces for additional line types you might want to try:
*Example readings in the ink jet black / white paper column will be used in our example code illustrations (results shown use black ink jet lines printed from the Line Following Tracks pdf on 24lb, 97 brightness ink jet paper). When the sensors are over the black line the average line sensor reflectivity percentage is 10% (rounded low) - when the sensors are over the white paper surface, the average line sensor reflectivity is 61 (we’ll round that down to 60).
Putting Our Information to Work
Now we need to find a good middle point between our example low reading of 10% and high reading of 60%. The difference between them is 50%, so if we take half of that (25%) and add that to our low reading, we get a value of 35%. This will be our middle threshold for determining whether the S3 is over a line or not: readings < 35% should be over the line, readings ≥ 35% should be off the line (we use ≥ here so there is no numerical gap between the "over a line" and "off line" measurements). Remember that each S3 is unique. When making a line-following program that specifies thresholds like this one, you will have to find a "low" value, a "high" value, and a middle-point that works with your specific robot.
In the previous tutorial we used the SIMPLE SCRIBBLER category's detect line block for line sensing. There wasn't much we could customize with it - we chose between “is” over or “is not” over the black or white line. Now, using VARIABLES blocks, MATH blocks, and the advanced line sensor reading block (that we just used to measure the line sensor reflectivity), we can create a line sensing program optimized for our unique Scribbler Robot. Our variables are set up below (remember that creating a new variable creates both a set variable and use variable block):
Using Light Sensor Variables and Inserts with LED Feedback
To check the function of the variable blocks we just created, let’s add some LED feedback. Using what we've made so far, along with blocks from the CONTROL, FUNCTIONS, and ACTIONS > LEDs categories, construct the BlocklyProp code below:
You'll notice that we collapsed the line sensor variable blocks to make the program fit more easily into our tutorial. Collapsing is a feature you can use to keep your long, complex programs readable. If you need a refresh on how to do this, go back and re-read A Bit More on Program and Block Controls. To see the information we've hidden, take a look again at the image in the previous section.
- Save, compile, load to EEPROM, and run the program.
- Test the variables, inserts, and code by placing your S3 on a flat surface on top of a piece of paper with a printed black line in its center. Slide the S3 from the left to the right - back and forth.
Do you get the correct LED feedback as the robot moves relative to the black line? If not, recheck your work against our examples and try again. If yes, let's try following a line.
Creating an Advanced Line Following Program
In the following Try This and Your Turn sections, you'll be programming the S3 to follow lines using more advanced BlocklyProp blocks. It may require some experimentation with different settings and different blocks to get the program working. To be successful, and to avoid gettting overwhelmed, make sure you test your program after every change and keep track of what works and what doesn't. When you have things working as you want them to, be sure to save the program.
The most common approach to dealing with a large task you are unfamiliar with is by trying different ideas, failing, and then learning from your mistakes or mis-steps. You may also hear this process referred to as the "trial-and-error approach." BlocklyProp gives you all the tools you need, but you need to learn how to logically work through problems and solve them in small, manageable steps. If you have a pen and paper handy, writing your desired program steps in plain English first can also help you determine how to set it up in the Blockly workspace.
Using the code you created above along with blocks from the ACTIONS > MOTORS category, substitute the change LEDs blocks with motor speed blocks inside the functions (as shown below) to create a more advanced line following program in BlocklyProp.
- Save, compile, load to EEPROM, and disconnect your S3 from the programming cable.
- Piece together pages 11, 2, 8, 3, and another copy of 11 of the line following track from the pdf resource.
- Tape them together on a hard, flat, smooth surface as shown below and place your S3 on top of the black line. Run the program.
How did it work? How might we improve it? If you are like most experimenters, your S3 may have stopped as soon as it reached any gap in the black line. Remember that the S3 processes info very quickly, so as soon as both sensors simultaneously see a value over our middle threshold, it stops.
Placing the printed sheets edge to edge or even overlapping as they are printed will leave gaps in the line. That is because most printers don’t print to the edge of the paper. If we want to create a program that doesn't stop on these gaps, we're going to have to figure out how to program that in or physically work around them.
Here are some possible solutions:
- Remove the stop driving block in the “both sensors read a value above 35%” condition - that is, change our program.
- Eliminate the gaps by trimming the paper, or by filling the gaps using a Sharpie® or black masking tape.
- Improve our program by making it more tolerant of gaps in the line or programing it to find the line again. We’ll learn about that in the upcoming tutorial: More Advanced Line Following. Such a solution would allow us to use line follow tracks like page 7 from the pdf resource, unmodified.
- Try the first two solutions outlined above (in order).
- Run tests and observe how your S3 performs on the line following track for each. How did the solutions work? Which did you prefer?
- Using your code as a starting point, try a different block from the ACTIONS > MOTORS categories for the left and right return to the line condition (left and right functions). Can you make the S3 move more smoothly? What did you do?
- Combine the two previous examples to create an advanced line following program with LED feedback.
- Make your own line path with tape (black masking or black electrical tape - even white tape on dark service), or draw your own with black markers (about ¾” wide). Develop your own program using what you’ve learned.
Hint: You'll need to re-check reflectivity and possibly determine a new middle threshold value if you make a line from a different material or ink! You might have already done this in the beginning of this tutorial.
Special thanks to Parallax friend Whit Stodghill for his assistance in writing, editing, and testing material for these S3 tutorials.