Exploring Thresholds

BOGATECHThis unit introduces an extremely simple exercise that allows students to think more deeply about the concept of a threshold and how to use it to make a robot react to its environment.

It is based on my Teachers Introduction Course to LEGOĀ® MINDSTORMS NXT & EV3 at BOGATECHā€™s website. Here the programming examples are based on the LEGO MINDSTORMS EV3 Software, but at BOGATECH’s website you can find the NXT version and the differences between the two platforms, if you are interested.

The following exercises can even be done by students without any programming experience, with the goal of introducing the concept of a threshold and programming with logical structures. It is very interesting to observe that in this practical exercise, the challenge is not to program the Wait and Switch blocks, it is to determine the thresholds to be used in each block to make the robot detect the lines and their color correctly. Students could, for example, be given the program already written. Students typically understand these blocks very intuitively and with little explanation.

At the end the unit, an experiment is proposed to generate a graph based on the color sensorā€™s measurements. This graph perfectly illustrates the sensor behavior. This approach can be extrapolated to other sensors.

A little bit of theory

To better understand how a sensor works and how the EV3 makes decisions based on sensor values, we need to explain the concept of a threshold.

A very simple example is to use the classroom doorway to illustrate that depending on where we stand, we will either be inside or outside the classroom. But if we stand just in the middle of the doorway, where will we be? We will be at the doorā€™s threshold which separates the inside from the outside.

For another example, letā€™s imagine that we want to classify the students by separating the short from the tall ones. One way to do it is to take the tallest student and the shortest one, draw two lines on the blackboard at their heights and ask the class where to draw the line that will separate the short from the tall students? In other words, how can we calculate the threshold that will state the difference between a short and a tall person? Perhaps the easiest approach would be to find the mean of the two extremes, that is to simply add the two measurements and divide the result by 2, that is, (A+B) / 2.

We can also calculate the arithmetic mean of the whole class, by adding all the studentsā€™ heights and dividing the result by the number of students. That will typically give us a different result, but why? When should we apply this concept? We could also add different weights to the different measurements, but how and when should we do this?

To calculate the threshold required to differentiate between a pair of sensor measurements is very similar, but first we need to find out the values of the sensors we want to differentiate. As outlined below, we can use the View menu on the EV3 brick to read the value of the Color sensor when we place it on top of different colors. Similarly, we could read the volume level on a Sound sensor or the distance to an object in front of the Ultrasonic sensor.

Exercise overview

Schematics of the exercisesAs shown in this image, the exercise consists of finding the right thresholds to make the robot…

  1. Go forward and stop when it finds the black line, using only a Wait block.
  2. Do the same exercise but stopping at the green line.
  3. Go over or ā€œjumpā€ the green line and stop at the black line.
  4. Stop at any line (green or black) and, using a Switch based on the value of Reflected Light Intensity of the Color sensor, it has to say the color of the line using a Sound block.

Preparation

We need to tape a black line onto a white background. We also need another color line (e.g. green or blue) that will give aĀ reflected light intensity higher than black and lower than white. In the following exercises we will use several thresholds, but always between pairs of measurements corresponding to two different colors.

EV3 with one color sensorEach team will need a simple two-wheeled robot with a Color sensor (to be used as a light sensor) attached to the front of the robot, as shown in the image (e.g.Ā LEGO MINDSTORMS Education EV3 Software >Ā Robot Educator > Driving Base + Color Sensor Down).

Next, every student or group of students will need to fill in a table with their robotā€™s sensor readings on top of the white, the green, and the black colors (the readings might differ for each sensor) ā€“ by using the ā€œViewā€ option of the EV3 brick, selecting ā€œReflected Lightā€ and choosing the corresponding port of the color sensor ā€“ and calculating the thresholds between each pair of colors. At this point it is important that the students make some tests and realize that the reading of the Color sensorā€™s reflected light intensity on the same color or point can vary depending on differences in lighting in the room or the position of the robot.

Black Threshold Black-Green Green Threshold Green-White White
6% 7% 9% 42% 75%
Ā  Threshold Black-White Ā 
40%

Exercise 1: Stop at black

The first three exercises deal with varying the light intensity threshold of the Wait block to stop the robot at the desired lines.

Create a new program and add…

  • a ā€œMove Tankā€ block set to ā€œOnā€ and 40% power,
  • a Wait block set to Color Sensor > Compare > Reflected Light Intensity, and
  • a final ā€œMove Tankā€ block to stop the robot.
Stop the robot at the black line
Stop the robot at the black line

Suggestion: We can use a ā€œMove Tankā€ block to control both motors with only one block. Remember to check that the ports for the motors and color sensor are set correctly.

As we can observe, this first exercise will make the robot stop at the black line, and obviously we must use the threshold between black and white, for example 40%. Make sure that ā€œReflected Light Intensityā€ in the compare Color sensor option is selected.

Exercise 2: Stop at Green

For the second exercise, that is, to stop the robot at the green line, you might expect that we need to use the threshold between green and white, e.g. 42%. But if we do not change the previous threshold, will the robot stop at the green line? The answer is yes because the value of the reflected light intensity read by the color sensor on the green is 9%, a value which is still under the threshold between black and white, e.g. 40%.

Exercise 3: Ignore green, stop at black

The third exercise is more interesting because now the students need to think a little bit to put a threshold that will make the robot go over or ā€œjump” the green line without stopping, but then stop when it reaches the black line. To do this we need to put the threshold between black and green, e.g. 7%.

Exercise 4: Stop at any line, say its color

Finally, the fourth exercise, stopping the robot at any line and making it say its color, will allow the students to finish the reflection on the threshold concept, as we will see next.

To make this fourth and last exercise, we need to add a Switch block, also set to use the Color sensor. Now, the first Wait block will stop the robot at any colored line (in our case black or green) and the switch will find out the color of the line. Once the Wait and Switch blocks settings and operation are explained, the students need to find out the thresholds they need to put in each case to correctly do the exercise. If the students do not have any programming knowledge, it is important to let them make tests, make mistakes and think about what they do.

The students need to discover that to make the robot stop at any line, they need to set the value in the Wait block to the threshold between white and the lightest line, in our case the green line, 42%. In this way, if the robot stops at green, it will also stop at black. But in the Switch block, they need to put the threshold between the colors we want to differentiate, in our case green and black, 7%, as shown in the following image.

Stop the robot at any line and say its color
Stop the robot at any line and say its color

Note that inside the Switch, in the darker color branch we need to add a Sound block and choose File > Black to make the robot say this word, and in the corresponding branch to the lightest color we need to choose File > Green.

The question is: will the robot say the right color? The answer is: not yet. Let the students make tests and discover that if they put the robot directly on top of each line, the robot does not move, given that it is already on the line, but it says the color correctly. Why? It is important to let them build up their theories and discuss them among themselves.

Readings of an EV3 robot's color sensorThe reason is that the value of the Color sensor measurement during the color change is progressive. That is, the value does not change suddenly. This is due to the fact that the sensor progressively varies its position over the colors as the robot goes forward, and that produces a progressive change of the reflected light intensity value read by the Color sensor, as the following images demonstrate.

Given that the Color sensor reading of the reflected light intensity is progressive, when the robot stops, it does it at the threshold of the lightest line, thus, the robot will always, or almost always, say green.

The step from one color to another one is progressive, as well as the color sensor readings
The step from one color to another one is progressive, as well as the color sensor readings

What do we need to do to make the robot say the right line color? The students very quickly understand that they need to stop the robot just on top of each line so as the color sensor will read the color with all its intensity and, thus, it will say its name correctly. The solution is simple, we must make the robot go a little bit forward just after the wait block, instead of immediately stopping it, as shown in the image below.

Stop the robot at any line and say its color, final version
Stop the robot at any line and say its color, final version

But there is another way we can differentiate colors using the Color sensor in EV3. We can use the ā€œCompare Colorā€ mode in the Wait block and the ā€œMeasure Colorā€ mode in the Switch block. The Wait block allows specifying the set of colors we want to find and the Switch block allows adding several branches, one for each color we want to differentiate.

Note that we can also add a default branch with ā€œNo Colorā€, just in case the robot finds a different color from the ones we are interested in, and since this is the default branch, the robot will use it for those colors that do not match the other branches. But, will the program ever execute this default branch at all? The answer is no, because the Wait block will never stop at any color other than black or green! If you have some blue tape, for example, you can test this. Although in this specific problem having a default option in the Switch block is not really useful, it might be very interesting for other situations.

Finally, observe that after the Wait block is executed, we do not need to move the robot a little bit forward to ensure that it stops just on top of the line, as we previously did when using the sensor in the ā€œReflected Light Intensityā€ mode, because in the ā€œCompare Colorā€ mode the Wait block takes care of reading the color correctly.

Stop the robot at any line and say its color using the detected colors
Stop the robot at any line and say its color using the detected colors

A graphical exploration of thresholds

Finally, to better understand the Color sensor operation, its readings, and the use of thresholds, we can create an ā€œExperimentā€ in the MINDSTORMS EV3 Software. Experiments allow us to record data from one or more sensors over time and generate a graph of the recorded data automatically.

The software allows us to record data in a number of ways, including:

  • Dynamically, by having the robot connected to the computer via USB or Bluetooth, the graphs are generated in real time, making it easy for the students to relate the movement of the robot with the resulting graph to understand it better.
  • Programmatically, by using specific data logging blocks. After the experiment is run we can access the data to visualize and study it in the computer.

The advantage of the first method is that the graphs are seen in real time and you can change the configuration in the same window where the experiment is displayed. But if you cannot have the computer connected via USB or Bluetooth, or if you need to run an experiment or record data after an event is triggered by a sensor, or given specific conditions, then you may need to generate an experiment using the data logging blocks programmatically.

For the pedagogical purpose of this unit, we will do both methods, but we will start by doing the programmatic approach first, so as not to unveil the ā€œmagicā€ until the end.

A little bit of theory

Before running any experiment, it is very instructive to ask the students to sketch the graph the robot will generate. We can help them by drawing a straight line at the height of the white color, at 60%, and another one at the height of the black one, at 30% (these values are just an example).

Colored line reading graph

Very soon the students will realize that the color change does not happen abruptly going from white to black, as the vertical red line shows, and they propose the inclined yellow line. This is a very important step because they relate the concept with the graph. In fact, in reality, the sensor readings draw a curve like the orange curves show (this effect is more noticeable when using the light sensor of the NXT brick).

Exercise 1: Programmatic graphical exploration

To record the data read by the Color sensor programmatically, we just need to use a Move Tank block with enough rotations to go over the black and the green lines, with a power of 40%, and two Data Logging blocks. In the first Data Logging block we need to choose the number of samples per second, e.g. 15, and the Color sensor with its port using the ā€œReflected Light Intensityā€ mode. Note that the duration, or mode, is set to ā€œOnā€ because we want to collect the data for as long as the robot moves, and stop it when the robot stops.

Color sensor readingsā€™ recording experiment
Color sensor readingsā€™ recording experiment

To retrieve an Experiment from the EV3 brick, we have to create a New Experiment window, turn the Oscilloscope mode ā€œOffā€ and click the ā€œUploadā€ arrow icon in the bottom right corner of the window to access the brick memory as shown in the images below. Note that you might get several ā€œ.rdfā€ Experiment files, with the same ā€œrootā€ name but with a different number, depending on the number of times you have repeated the same experiment.

Toggle Oscilloscope mode on or off to dynamically capture data from the sensors
Toggle Oscilloscope mode on or off to dynamically capture data from the sensors
Experiment window showing the Data Log file manager window and the ā€œUploadā€ arrow icon to open it in the bottom right corner
Experiment window showing the Data Log file manager window
and the ā€œUploadā€ arrow icon to open it in the bottom right corner

If we run the robot over both colored lines, we will see that the graph shows two steep valleys. At this point it is important that the students interpret the results. For example, can you tell from the following graph whether the robot crossed the green or black lines first?

Experiment window
Experiment window

In this case, the first and longer peak corresponds to the black color and the second to the green.

What else can we notice about these graphs? In addition to observing small variations of the white color, one question might be: why is the graph so jagged, rather than being more rounded? Were we wrong with the previous predictions? If we were not wrong, then how can we obtain more precise or meaningful results, where we will better see the shape of the curve? The answer is that to obtain more precision and more meaningful curves we have to increment the number of samples per second. Alternatively, or in addition, we can reduce the speed of the robot.

For the following graph, the experiment ran with 30 samples per second (double of the previous one) and a motor power of 20% (half of the previous one).

Second experiment window
Second experiment window

In this experiment we can see color sensor readings through the experiment much more clearly. We can also see that the readings along the top of the graph, that were all of the same color, are not always the same. Why does this happen? It is interesting to make the students think about what factors they believe have an influence on these color sensor reading variations. Some factors to consider may include dirt or other marks on the surface, lighting conditions, and the sensor reading errors. Note that the reflected light readings along the darker areas are more stable than along the lighter areas.

Finally, we can see and understand visually where the robot stops at each line, which is at the threshold between white and the green line, represented by the yellow line in the previous image. Thus, when reaching the black line the robot will stop a little bit before the border of the line, and when reaching the green line, it will stop right on the border of the line, since it is precisely the threshold between green and white, as we have mentioned before.

Exercise 2: Dynamic graphical exploration

As mentioned before, if we keep the robot connected to the computer via USB or Bluetooth, we can do the experiment dynamically to watch how the graphs are generated in real time, allowing the students to relate visually the movement of the robot with the graph being generated to understand it better.

To dynamically watch the generation of the graph, we can turn ā€œOnā€ the ā€œOscilloscopeā€ mode in a new Experiment window (this is done by default when opening a new experiment window). Let the students play within the Experiment window generating graphs for the Color sensor, watching how the reflected light reading changes as they push the robot by hand over the different colors on the field.

Suggestion: Disconnect any extra sensors from the EV3 brick before creating a new Experiment, leaving only the Color sensor connected. When a new Experiment is created, the software automatically fills the Sensor Setup configuration for each of the connected sensors. Alternatively, you can remove the sensors you are not interested in by clicking in the red cross on the right of each entry. Finally, make sure that the Color sensor is connected to the right port (default port 3 in our case) and that the mode is set to ā€œReflected Light Intensityā€. Note that if you have previously configured the Port View settings for any sensors (in the lower right of the EV3 window), then the Sensor Setup will be filled with the same parameters used there.

Experiment and sensor setup parameters
Experiment and sensor setup parameters

Finally, to fully understand the exercise and how thresholds work with the Color sensor over different color lines, let us unveil the ā€œmagicā€…

The EV3 Software has a very useful and easy to use feature called “Graph Programming”. This allows us to dynamically run a program within an Experiment establishing up to three different area or zones, divided by two thresholds, running specific code in each zone. Just click on the Graph Programming tab, in the bottom left of an Experiment window, select the Color sensor, and you will see three different zone markers (star, rectangle and circle). After selecting the checkboxes of the three zones to make them available, you will see two horizontal lines dividing the screen. JustĀ type inĀ the desired thresholds in the left textbox of each line, in our case, 42 and 7, as shown in the image below.

Next, we need to select the programming blocks we want to run in the three different areas (above, between, and below the two thresholds). What do we want the robot to do? The idea is to make the robot say the color of the line continuously, as shown below, so that it will say different color names while it is pushed through the field, crossing the two thresholds.

Experiment window with ā€œGraph Programmingā€
Experiment window with ā€œGraph Programmingā€

Note that in the first area, the top one, the Color sensor is supposed to be on top of white, thus the robot needs to say ā€œWhiteā€. In the middle area the robot detects green, thus the robot will have to say ā€œGreenā€, and in the bottom area the robot will detect a reflected light intensity near black, thus the robot will have to say ā€œBlackā€.

Finally, run the Experiment by turning the oscilloscope mode ā€œOnā€ and put the robot on top of white. The robot will say ā€œWhiteā€ continuously. Now gently push the robot towards the black line. Guess what will happenā€¦ If you go slowly you will hear the robot say ā€œGreenā€!, but if you continue pushing it towards black, it will start saying ā€œBlackā€ until you remove it from the line. Now you can also place the robot near green and push the robot to check that it will always say greenā€¦ This reveals, in a very practical manner, the Color sensor readingsā€™ progression in reflected light intensity mode and its effect when using thresholds to find out line colors.

To finish the unit, if you would like toĀ  reproduce the smoothness of the programmatic experiment graph dynamically, you just need to run a ā€œMove Tankā€ block in the robot while running the Experiment with the robot connected to the computer. This will ensure that the speed of the robot is constant through the whole experiment, thus, ensuring that the graphs are generated at a constant speed.

Acquired knowledge

With this exercise the students have learnt to interpret a graph, to better understand how sensors operate, as well as the concept of a threshold, and how to use a threshold to program a robot to make decisions in response to the environment. Also important is the mental and intellectual effort needed when trying to deduce the results before doing an exercise, and then to later prove them practically. This helps the students to assimilate the acquired knowledge much more effectively and puts their mental gymnastics into practice.

The following two tabs change content below.
Josep Fargas
I have been working with LEGO Education robots for many years and have been involved in many schools and conferences. I started the BOGATECH Robotics Workshop to offer activities and support to students, education centres, and teachers. My course, "Robo-telling Stories", was selected as one of the top seven European Union finalists of the inGenious Teachers Competition. One of my main interests is to collaborate with other teachers to research how to teach curricular and non-curricular subjects more creatively.