©  Gene Harding 2018
Gene HardingProgramming LEGO® EV3 My Blockshttps://doi.org/10.1007/978-1-4842-3438-9_5

5. Using Lines

Gene Harding1 
(1)
South Bend, Indiana, USA
 

Learning Topics

My Blocks, unit conversion, feedback, looping constructs, proportional control, variables, and conditional constructs

Requirements

  1. 1.

    Find a black line when crossing it.

     
  2. 2.

    Follow the right or left edge of a black line a specified distance.

     
  3. 3.

    Find the point at which a black line makes an abrupt 90° change in direction.

     
  4. 4.

    Square up on a black line.

     
The black lines on FLL challenge mats can be used to precisely locate the robot in one direction (north–south or east–west), or in the case of lines intersecting at 90° angles, provide a precise waypoint in both directions (north–south and east–west). Figure 5-1 shows the three common line configurations seen on most competition mats: lines, line-Ts, and line-Ls. The EV3 color sensor can be used to detect and follow the high-contrast edge where a black line transitions to a white line.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig1_HTML.jpg
Figure 5-1

Line, line-T, and line-L

The color sensor has several different modes, most of which deal with detecting either a color , reflected light intensity, or ambient light intensity . Color sensing is covered here for completeness, but the focus in this chapter is dealing with black and white lines.

When used in a color-sensing mode the sensor can measure the following colors: no color (0), black (1), blue (2), green (3), yellow (4), red (5), white (6), or brown (7), outputting the numeric value listed in parentheses corresponding to the detected color. It can also compare the numeric value of the detected color to a given value, outputting both a logic result of the comparison and a numeric result for the color detected.

The color sensor can measure either reflected light intensity or ambient light intensity, outputting numeric values ranging from 0 (minimum reflected light) to 100 (maximum reflected light). When measuring reflected light intensity, the red light-emitting diode (LED) inside the sensor shines light downward, and the sensor detects how much light is reflected back. When measuring ambient light intensity , the LED remains off, so only ambient light is detected (i.e., light from other sources, such as lamps or overhead lights in the room or sun shining through the window). The sensor can also compare either reflected light intensity or ambient light intensity . In these modes it still outputs the numeric value of light intensity measurement, but also outputs a logic value corresponding to the result of the comparison.

In each of the Compare and Measure modes just listed, the sensor’s sample rate is 1 kHz, or 1,000 samples per second (EV3 User Guide, either Home or Education Edition).

Finally, the color sensor has Calibrate modes . If a guard is built around the sensor to prevent ambient light from entering, the Calibrate modes might not be needed. On the other hand, if a guard is not in place to shield it and only one sensor is used for line following, calibrating the sensor is very important. The next section addresses calibrating the color sensor for different ambient light conditions.

Calibrating the Color Sensor

Learning topics covered: calibration, feedback , looping constructs

In Reflected Light Intensity mode , the EV3 color sensor can return values ranging from 0 to 100. A 0 value indicates no reflected light, corresponding to a surface so dark that none of the LED’s light is reflected into the sensor. A value of 100 indicates “full” reflection, or a surface so bright that it reflects essentially all of the LED’s light into the sensor. In most situations, the range of sensor values will not be 0 to 100, but instead a smaller range. For example, suppose that a given competition mat with a certain amount of ambient lighting results in a light intensity value of 9 over a black line (dark surface) and a 93 over a white line (reflective surface). The problem is that different levels of ambient lighting can change these maximum and minimum values, so a given program can operate differently in different environments. My teams have seen their programs operate differently as the sun changed position in the sky, shining more or less light into the room through the windows.

Calibration can deal with this problem by redefining what constitutes a value of 0 or 100. For instance, in the scenario described earlier, the robot could be calibrated to treat a 9 as a 0 value, and a 93 as 100. By recalibrating the robot whenever the environment changes, you can reduce or eliminate the impact of a change in ambient light conditions. My teams found calibration to be critical when using a robot with no guard, or shielding, around the light sensor(s), so we always calibrated the robot just before doing a formal mission set run. We later built a guard to go around the light sensors to block ambient light. Although the guard seemed to eliminate the need for regular calibration, we continued the practice just to be safe.

There are three Calibrate modes , all dealing with reflected light intensity. The Minimum mode redefines what constitutes a 0 value; the Maximum mode redefines what constitutes a value of 100; and the Reset mode changes back to the default values. Let’s start with a basic calibration program.

The procedure to calibrate reflected light intensity is as follows:
  1. 1.

    Reset the sensor .

     
  2. 2.

    Place the robot so its light sensor is over a black line.

     
  3. 3.

    Read the sensor value and pass it to the Calibrate Minimum block.

     
  4. 4.

    Place the robot so its light sensor is over a white line.

     
  5. 5.

    Read the sensor value and pass it to the Calibrate Maximum block.

     
Start the program with a Color Sensor block in “Calibrate – Reflected Light Intensity – Reset” mode . Add a Wait block in “Brick Buttons – Compare” mode to wait for a button to be bumped. This allows for the user to place the robot over a black line before doing the minimum intensity calibration. Next, add a Color Sensor block in “Measure – Reflected Light Intensity” mode to read the value, and connect it with a data wire to a Color Sensor block in “Calibrate – Reflected Light Intensity – Minimum” mode to do the calibration. Add another Wait block in “Brick Buttons – Compare” mode to wait for a button to be bumped. This allows for the user to place the robot over a white line before doing the maximum intensity calibration. Next, add a Color Sensor block in “Measure – Reflected Light Intensity ” mode to read the value, and connect it with a data wire to a Color Sensor block in “Calibrate – Reflected Light Intensity – Maximum” mode to do the calibration. The program could be named Calibrate, and should look like Figure 5-2. (Note that this is just a program, not a My Block.)
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig2_HTML.jpg
Figure 5-2

Calibrate program

This approach works, but there is a significant issue with it: There is no way of being sure that the light sensor is placed so that it reads the desired minimum or maximum value. We address that issue by using feedback and a couple of loops.

Copy Calibrate to a new program and name it Calibrate2. We will use a loop to continuously poll the color sensor and display its value so we know when it is at a minimum or maximum. Replace the Wait blocks with Loop blocks . Set each loop exit mode to Brick Buttons, and pick the button of your choice. Note that when using the buttons in this manner, using the “bumped” option (2) is probably the most reliable choice. Sometimes one press or release can trigger the exit for multiple loops, even though it should not do so. Give a meaningful name to each loop, such as DisplayMin for the first loop and DisplayMax for the second. Place a Color Sensor block followed by two Display blocks (from the Action Blocks tab) inside each loop. Configure both Color Sensor blocks in “Measure – Reflected Light Intensity ” mode. The Display blocks will present a user prompt and display the current light intensity value.

Display blocks have several different modes, but the one we need now is “Text – Grid.” According to the EV3 help, this mode divides the brick’s display into a rectangular grid of 12 rows by 22 columns numbered 0 to 11 and 0 to 21, respectively. Each row is 10 pixels high, and each column is 8 pixels wide. Click on the small window at the upper right of the block where it says “MINDSTORMS.” This is the text to be displayed. Replace it with “Min?.” Set the Clear Screen parameter to True. This clears the entire screen each time the loop executes. Because this is the first item to be displayed, set the Row and Column parameters both to 0. The second Display block’s mode will be the same, but instead of typing a text value in the display box, select the “Wired” option. Notice that this generates a new input called Text in the Display block . Connect the Color Sensor output to this input. Now the program will display the sensor value each time it updates the minimum calibration. For the second Display block, set the Clear Screen parameter to False so it does not erase the “Min?” prompt. Set the Column to 12 or so and leave the Row set to 0 so the light intensity value is displayed on the same line, but after the “Min?” prompt. See Figure 5-3 for an illustration.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig3_HTML.jpg
Figure 5-3

Calibrate2 program

The second loop will be similar, but not the same as the first. To leave the Minimum value displayed, set the Clear Screen parameter of both Display blocks to False. Set the prompt text to “Max?,” Column to 0, and Row to 4 so it will display at the start of a new line. As with the DisplayMin loop, select the “Wired” option and wire the output of the Color Sensor block to the Text input of the second Display block. Set the Column parameter to 12 and Row to 4.

One tip here is very important: Add several spaces to the end of the “Max?” prompt so that the numeric value is cleared each time the loop executes. This is important to avoid confusing readouts. For instance, suppose the sensor is placed over a white area and returns a value of 100, then is moved to a shaded area where it returns a value of 25. If the old value is not cleared (with the extra spaces), the 2 will overwrite the 1 digit of 100, the 5 will overwrite the first 0 digit of 100, and the second 0 digit of 100 will remain, so it will look like the value being displayed is 250 instead of 25.

Note in Figure 5-3 that the program chain has been split into two rows. Arranging programs in this manner often improves readability, so let’s try it. Select the second loop and the blocks that follow it, and drag them down and to the left so they are under the first loop. Then, “grab” the Sequence Plug Exit of the top right Color Sensor block and connect it to the Sequence Plug Entry of the second loop. Remember to add appropriate comments, then save and try out the Calibrate program.

Finding a Black Line

Learning topics covered: My Blocks, feedback , looping constructs

Before the robot can follow a line, it must first find the line. Although this process is fairly straightforward, some patterns used on the competition mats make it a bit more complicated. It is now quite common for mats to have patterns (separate from the lines) with dark areas that look black (or are black) and light areas that look white (or are white). Because of this, my teams generally found it effective to use the following steps to find a black line on the mat:
  1. 1.

    Use dead reckoning to get close to the line.

     
  2. 2.

    Look for the white space before the black line.

     
  3. 3.

    Look for the black line itself.

     
The first step bypasses most or all of the areas on the mat that might be mistaken for the black line or its surrounding white space. The second step is especially useful when there is a dark pattern near the line, and the dark pattern could be mistaken for the black line itself. Figure 5-4 shows a black line with three patterns nearby that might appear black to a robot’s light sensor. By looking for the white line first, the robot will bypass the star, circle, or chevron if any of those is in its path. Using dead reckoning followed by the white space search greatly increases the probability of finding the desired black line.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig4_HTML.jpg
Figure 5-4

Dark areas adjacent to black line

Dead reckoning navigation was covered in the two preceding chapters. A color sensor in Reflected Light Intensity mode can detect the black line and its surrounding white lines. Let’s begin by making a program to find the white area, starting with the loop used in the earlier forward My Blocks. Copy the “wiggle-correction” loop from Forward2 and paste it into a new program. Because the goal is not to move a specified distance, that part of the code is not needed, and the loop exit criterion must be modified. Change the Loop block mode from “Motor Rotation – Compare – Degrees” to “Color Sensor – Compare – Reflected Light Intensity” and make sure the Port is set to “1”. A high Threshold Value is needed to detect white, probably somewhere between 85 and 95 because a white surface should reflect almost all of the light, and a properly calibrated sensor should return a value close to 100. Name the loop something meaningful like FindWhite. Add a Move Steering block after the loop to stop both motors, and test the program. If a mat is not available, place a white sheet of paper on a relatively dark surface, such as a woodgrain table, and verify that the robot stops when it reaches the paper. The program should look something like Figure 5-5.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig5_HTML.jpg
Figure 5-5

Program to find white area

A second loop is needed to find the black line, so copy the loop FindWhite, paste it right after the first one, and rename it FindBlack. Because the goal is to find black, which reflects very little light, the loop exit criterion will be to a light intensity less than a low value, probably somewhere between 5 and 15. Change the Compare Type parameter for the loop to 5 (≤) and enter a low threshold value. Add a black line to the white piece of paper and test the program.

Because this program might be needed many times across multiple mission sets, it makes sense to convert it to a My Block, so select everything but the Start block and open the My Block Builder. Reasonable numeric inputs could include minimum threshold with a default value of 5, maximum threshold with a default value of 95, power for the motors with a default of 20, and port number with a default of 1. Although the reason might not be apparent until later in this chapter, it is often useful to be able to specify the port number when using sensors. Click the Port window at the upper right of each Loop block and select the Wired option, then connect the PortNumber input of the My Block to the appropriate input of the Loop block. The final My Block should look something like Figure 5-6. Test it to make sure it works inside a program and remember to add comments before moving on.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig6_HTML.jpg
Figure 5-6

My Block to find a black line

Following a Black Line

Learning topics covered: My Blocks, unit conversion , feedback , looping constructs , proportional control

A color sensor can provide feedback to a Move Steering block so that the robot will follow the edge of a black line. Refer to Figure 5-7 as we explore this concept. First, note that the reflected light intensity over the center of the black line is at or close to 0, over the center of the white line it is about 100, and at the junction of the two it is about 50. Second, remember that the Steering input to a Move Steering block causes the robot to turn right when it is positive, turn left when negative, and go straight when zero. Thus, if the robot is following the edge of the black line and going straight, the +50 Reflected Light Intensity value from the color sensor must be translated to a 0 value for the Move Steering block. A Math block can do this operation quite easily by subtracting 50 from the Reflected Light Intensity value.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig7_HTML.jpg
Figure 5-7

Line following concept

Now, think about what happens if the robot drifts to the left: The sensor is over more of the white line and less of the black line, which means the Reflected Light Intensity value increases to somewhere in the range of 51 to 100. After subtracting 50 the value is still positive, and a positive Steering value causes the robot to steer to the right, back toward the edge of the black line. Likewise, if the robot drifts to the right, the Reflected Light Intensity value decreases, and after subtracting 50 the value will be negative, which will cause the robot to steer left, back toward the edge of the black line.

Remember the proportional control technique described in Chapter 3 for wiggle control? This is the same control technique because the farther the robot drifts from the edge of the black line, the higher the steering correction to move it back. In this case, however, there is one more thing to consider: sensor placement. It might make sense to think of the robot as following the color sensor, so the sensor must be ahead of the driving wheels by at least a couple of inches, and preferably 3" to 5" inches or so. If the color sensor is placed too close to the driving wheels, it will be difficult or impossible to get the robot to follow the line smoothly.

Now it is time to start writing the line following program, which will follow a line for a specified distance. The first part of the program will be the same as the Forward2 My Block: Reset rotation sensors B and C, and convert distance from inches to degrees. Next will be a loop with the same exit criterion. Inside the loop will be a Color Sensor block in “Measure – Reflected Light Intensity”mode, followed by a Math block that subtracts 50 before passing that value to the Steering parameter input of a Move Steering block in On mode, as shown in Figure 5-8. Try the program and see how it works. Experiment with setting the light sensor slightly to the left and right of the edge of the black line. What happens? The robot will probably oscillate several times before settling and following the line smoothly, or it might not settle at all. Try increasing the power from 20 to 30. At some point as the power (speed) is increased, the oscillations will not settle out. The problem is that the “gain” in the equation is too high. Because there is no gain parameter in the math equation, the value is effectively gain = 1. If a smaller value is used, the correction would be slower and the oscillations would be gentler.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig8_HTML.jpg
Figure 5-8

Initial line following program

Change the Math block mode to Advanced. Inputs “a” and “b” will be as before, but “c” will be used for the gain and “d” will be used to set which side of the black line to follow. The Steering parameter calculation will be
$$ Steering=left(a-b
ight)ast cast left(-d
ight) $$
(9)
Where“a” is the reflected light intensity (data wire from Color Sensor block ),“b” is 50 (the amount to shift),“c” is the gain, and“d” is LineSide (1 for right side of black line, –1 for left side).

To start, set c = 0.5 and d = 1.0, change the power back to 20, then run the program again. Change d to –1.0 and verify that the robot follows the left edge of the black line. Spend some time experimenting with values for gain (c input) and the Power input to the Move Steering block to get a feel for how they affect the line following performance. For a given speed, higher values of gain will allow the robot to track the edge of the line even if it is not initially aligned with it, but it will also tend to oscillate. Thus, there is a trade-off between smoothness and ability to track the line when approaching from an angle.

It is now time to make the My Block, with numeric inputs for LineSide, Port, and Distance. (The Port input will be useful after we add the second color sensor in the next section.) Although Gain and Power could also be set as inputs, it is probably a better strategy to make one version of the LineFollow program for going smoothly once the robot is aligned, and another version for dealing with poor initial alignment. That way the Gain and Power parameters can be tuned to optimize performance in each of those situations, and then left alone. The LineFollow My Block should look something like Figure 5-9.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig9_HTML.jpg
Figure 5-9

LineFollow My Block

Finding Black Line Intersections (Ts and Ls)

Learning topics covered: My Blocks, feedback , looping constructs

The concept of finding a line intersection is illustrated in Figure 5-10, which depicts the left color sensor being used to follow the left side of the black line as the right color sensor looks for the line intersecting from the right. Similarly, if looking for an intersecting line coming from the left, the right sensor would do the line following and the left sensor the detecting.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig10_HTML.jpg
Figure 5-10

Using two color sensors to find line intersections

Performance will be more consistent if the color sensors are matched. Run the Calibrate2 My Block with the Port set to 1 in all four of the Color Sensor blocks, making a note of the minimum and maximum calibration values. Repeat with the Port set to 3 (or whichever port your other color sensor is using). If both values are within a few percent of each other, it should be fine. If not, it might be worth some effort to track down a color sensor that is a closer match to one of them.

The LineFollow My Block from the previous section is the basis for the LineTFind My Block . The inputs will be a little different, so a new My Block must be created from scratch. Start by copying LineFollow to a new program, then deleting the three blocks before the loop . These blocks will not be needed because the robot will keep going until it finds the intersecting line, regardless of the distance. The loop exit condition must also be modified to look for a light condition. Change its mode from “Motor Rotation – Compare – Degrees” to “Color Sensor – Compare – Reflected Light Intensity ”, and set the condition to 5 (≤).

There are now two sensor ports being used, but the side of the line to follow will determine which port is used to follow the line and which is used to find the intersecting line, so no port input(s) are needed. Create the LineTFind My Block with one numeric input called LineSide. Insert a Switch block before the loop, set its mode to “Numeric,” and connect the LineSide input to it. On my robot, the left color sensor uses port 1 and the right sensor uses port 3, so if the input is 1 the robot will use port 3 to follow the right side of the line and port 1 to find the intersecting line. If the input is –1 the port assignments must be reversed.

We need two new variables to hold the port values, so create those variables, name them “FollowPrt” and “FindPort,” or something that makes sense to you, and place one of each in the top part of the Switch block (in “Write – Numeric” mode), and one of each in the bottom part of the Switch block. (The reason for naming the first one “FollowPrt” instead of “FollowPort” is so the full name will be displayed when the variable appears in the program. If it is named “FollowPort” then “FollowP…” appears as the variable name because there is not enough space for the full name. If you cannot see the full variable name, just abbreviate it until you can. You can delete unused variables in the Project Properties window.)

The top value of the Switch block can stay at 1, but the bottom part must be changed from 0 to –1. Note that the “dot” selection next to the 1 at the top indicates the default, so if the input is any value other than –1 the Switch block will execute the top portion. When the input is 1, FollowPrt should be 3 and FindPort should be 1, and vice versa when the input is –1.

Next, place a copy of FollowPrt, in “Read – Numeric” mode, just before the Color Sensor block . Change the block’s Port input from constant to wired, and connect FollowPrt to that input. Similarly, place a copy of FindPort just before the end of the loop and wire it to the Loop block’s Port input. The LineTFind My Block is shown in Figure 5-11. Don’t forget to add comments to it and test it to make sure it works properly.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig11_HTML.jpg
Figure 5-11

LineTFind My Block

Squaring Up on Lines

Learning topics covered: My Blocks, feedback , looping constructs

There might occasionally be times when it is useful to “square up” the robot so that it is perpendicular to a line. This is possible with two light sensors. The assumption for this My Block is that each light sensor is located the same distance forward of the driving axle, as shown in Figure 5-10. The concept is that the left sensor controls the left wheel and the right sensor independently controls the right wheel. They do this in a manner such that each sensor ends up positioned over the edge of the black line, as shown in Figure 5-12.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig12_HTML.jpg
Figure 5-12

Squaring up on a line

Remember how the LineFollow My Block was set up? The Reflected Light Intensity value from the color sensor was shifted from a range of 0 to 100 to a range of –50 to +50 by subtracting 50, then it was multiplied by a gain value before being passed to the Steering input of a Move Steering block . A similar approach will work here, but a pair of Large Motor blocks will be used to independently control each motor, and the numeric values will go to the Power inputs (in lieu of the Steering input of a Move Steering block). Thus, when a sensor is over black the motor will move that wheel backward, and when the sensor is over white the motor will rotate that wheel forward. Two loops will execute in parallel, one for the left side and one for the right side. Ideally, after a couple of seconds or so each motor will settle into place with its corresponding light sensor over the edge of the black line. Each loop’s exit criterion will be time, two to three seconds or so. Because this My Block will have no inputs, it will be a very small block. The name must therefore be very short to be visible, so let’s call it something like “LnSqu.” It is illustrated in Figure 5-13.
../images/461934_1_En_5_Chapter/461934_1_En_5_Fig13_HTML.jpg
Figure 5-13

LnSqu (line-squaring) My Block

Conclusion

You should now have a suite of several My Blocks that use lines to improve navigation accuracy. Using lines can make a very substantial difference, often the difference between success and failure on a given mission or set of missions. Walls can be used in a similar fashion to aid navigation, and that is the subject of the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.190.167