Lego — Binning the LineFollower Code

Binning: Arithmetic To Map Sensor Reading — Episode #11

J3
KidsTronics
8 min readMay 31, 2019

--

In this post, we’re gonna deal with binning and rewriting the LineFollower program from Episode 07.

Binning? jump to see my Lego’s youtube playlist — click here;

It's easy… instead of using nested switch blocks Color as Sensor — Compare — Reflected Light Intensity mode, use switch block in numeric mode. Missed the earth? Let me explain…keep on reading!

Motivation

The Line-Follower-Robot from Episode #07 uses nested Switch blocks to choose between three move steering blocks.

This approach is called a three-state controller because the program does one of the three things based on the sensor reading:

  1. Go straight
  2. Turn Left or
  3. Turn right

The main problem with this approach is that when the robot needs to turn, it always turns the same amount; whether the robot encounters a sharp or gentle turn, it uses the same independent fixed Steering value.

There are other two possibilities to solve this problem: One is binning that I will implement in this very post.

And another is called a proportional controller, which I will deal in the future post…

Data Binning

Is a data pre-processing technique used to reduce the effects of minor observations errors. The original data values which fall in a given small interval, a bin, are replaced by a value representative of that interval, often the central value. It is a form of quantization. Wikipedia

Solution

Switch Numeric modes let you add as many cases as you want, without having to nest.

The key for binning is to group all the expected sensor readings into a small set of numbers.

Let’s learn by doing it right away!

Remember this lesson 7?

Fig 1. As the robot cuts across the black line the light sensor experiences different values, right?
Fig 2 . Look at Port View for Color Sensor; The readings are: [Completely off the line: 72 ] [Slightly over the line: 59] [Over the edge of the line: 14 ] [Mostly over the line: 8 ] and [Completely over the line: 9]

The Tribot v1 uses the Color Sensor in Reflected Light Intensity mode to follow the edge of the line by adjusting the steering based on the light Sensor reading.

Ideally, we want a robot to follow the black line, right?

Wrong!

We think that this is on the line; however, when you’re building a line follower you’re not actually building the robot to follow the black line;

Rather, you’re making the robot follow on the edge between the black and the white: the so-called Good Zone:

Fig 3. Color Sensor reading at different positions as shown in Fig. 02 above; the less light reflection, the lower the sensor’s value.

In the Good Zone, we’re not completely on the black line; we’re not completely on the white line; we’re in the middle.

Why should we follow the edge of the line? Why we’re in between the black and the white as opposed to not directly on either?

If the Tribot were on the black line only, then your algorithm would have no way of knowing which way it’s strong;

Off the black line on both sides of the black line are white; no reference either;

However, if you’re falling in between the black and the white, if the sensor light value is getting too bright (values increasing beyond a threshold), you know the robot is moving away from the black line; If the sensor light value is getting too dark (values falling before a threshold), then you’re almost crossing the line (assuming the robot is on the left side of the line, as in the chart above, remember:)

Knowing these references give you chances on your algorithm to turn the robot in one specific direction and make the corrections for the ideal track— inside the good zone.

Let’s set a new target for Tribot!

let’s rewrite the previous code and see how to use the Switch in its Numerical Mode.

The Binning process:

Fig 4. Binning Process; extremes values are 8 and 72 and the formula is bin = (reading-8)/22

1 º Step: To calculate our bins, we need three numbers: the lowest and highest value you expect from the sensor, and the number of bins;

These are(see Fig.2):

2º Step: As shown in Fig.4 above this is a graphical representation of the binning process; To initialize your binning, slide the range to the left so that it starts at 0; In the program, you can do this by subtracting 8 (the lowest expected value);

3º Step: Then divide the range into three bins; we’ve numbered the bins 0, 1 and 2 because that will make the math work out easier; the size of the total range of value we expect is 64 / 3 = 21,333, but we will deal with whole numbers, so we will round up to 22 instead as our bin size:

*(it doesn’t have to be exact;)

4º Step: Now return to the actual positions and we get these ranges:

5ºStep: Apply this formula:

See the whole process of binning in the gif graph of Fig 4 above;

And there you have it!

Congratulation, now you have your binning process complete!

Let’s code; Here is our flowchart for Code I:

Fig 5. Flowchart for code I

Realize that now we are talking about bands, strips or zones. These are bins!

The code now looks more elegant and easier to enhance, see for yourself!

Here is our code I solution using binning:

Fig 6. Code I

Now let’s expand the LineFollower v2 program from Episode 7 to turn slightly when the robot is near the line and more sharply when it’s farther from the line; We will get five cases (or bins:).

Lets set our steering values to vary between a sharp and gentle turn. If the robot is inside and near the good zone then let’s make it go almost straight; else, make a sharp turn. Let’s test it now!

Fig 7. Relationship between Bins x Steering behavior of the Tribot

All We need to do is change the value of the second Math block from 22 to 14 and add two more cases to the Switch block:

Fig 8. Flowchart for Code II

https://drive.google.com/file/d/1guaZGsDfZug3CHP2X57UZK2NocPgzmTm/view?usp=sharing

And here is the code II:

Fig 9. Code II

When we run the first code, it behaves much like the code from the Episode 7º with the difference that now we simplified the Switch block by adding the complexity of the Math blocks.

The second code the challenge was to give five cases, with two types of turn for each side.

And now we can have a lot more bins … without much complexity…

Comparing with the last code from the Episode 7º we apply less effort to expand the code.

It results in one code much more elegant, right?

See the video for my lab:

That’s all folks!

In the next post, we’re gonna study a proportional line follower. Be tuned!

Bye!

Download All Files For This Project

Related Posts:

01º Lego Episode — Our Startup’s Journey — Invaders and Invasions?

02º Lego Episode — Timmyton Lego-Learning-By-Playing — L2BP Series

04º Lego Episode — Lego MotionsTribot v 1.0Seeing Your Creation Move — Move Steering Block

05º Lego Episode — Lego Motions Move Tribot Around — And Backward…Five Programs Files

06º Lego Episode — Lego SensorsTouch N Color — Two out of five human senses — Touch N Sight

07º Lego Episode — Lego Sensor LineFollower — Line Follower Tribot v1.0

08º Lego Episode — Maze Solving Robot v1 — Lego Solution Right-Wall-Follower-Robot

09° Lego Episode — Gettle_&_Sound_Bots — How gentle can a robot be? What is the audible range of the human ear? How deep can we dive?

10° Lego Episode — Data Logging — Data Collection and the EV3

11º Lego Episode — Binning the LineFollower Code — Binning: Arithmetic To Map Sensor Reading

8th — kidSerie J3 Follower Line Robot v1.0 — The Simplest Follower Line Robot

12º Lego Episode — A Proportional LineFollower Robot — Advanced Math To Improve Your Robot’s Steering

13º LEGO Theory — Theory of Multitasking — A very Useful Programming Technique

14º LEGO formula — Normalizing Data — Converting Data to Use The Same Range

15º Lego Episode — PID — The Ultimate Line Follower — Algorithm for your EV3 PID Line Follower Robot

16° Lego Meets Pixy Episode — How to Connect Your Inexpensive Camera Module to Lego

18° Lego Episode — GEARS & WORMS — Geartrains & Worm & Clutch Gears

23° Lego Episode — Differential Explained — How Differential Works?

24° Lego Episode — PitBot — A Star Is Born — Working at The First Structure in Our Sparring Robot

25° Lego Episode — PitBot Is Agressive? Well, No Worries! — Making PitBot bite!

26° Lego Episode — Dancing Good w/ PitBot — All The Secret for Replicate This Awesome Robot

27 ° LEGO Episode — Sumo Arena is Ready! — Here is the playing arena for Arduino x Lego

28 ° LEGO Episode — Pick Pitbot Up! — Our Robot Are Leaving Body & Paint Shop

28 ° LEGO — B — Episode — Pitbot Battery & Sensor Setup — Preparing The infrastructure for running Arduino code

29 ° LEGO Episode — Bridging All Sensors Together — Pitbot — Collecting All Codes for the Final Act of Giving Behaviors to Robot

Credits & References

Book: The Art of Lego Mindstorms EV3 Programming by Terry Griffin

EV3 Large and NXT Motors — The Differences Explained

Building Instructions & Program Descriptions

LEGO® 9V Technic Motors compared characteristics

Robotics for Children (& Parents (& Grandparents))

Tribot v 1.0 .pdf File

Introducing LEGO Digital Designer

--

--

J3
KidsTronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️