LEGO Theory — Multitasking

A very Useful Programming Technique — Episode #13

J3
KidsTronics
6 min readJun 8, 2019

--

Using multiple sequences — treads — allows your program to perform more than one task simultaneously :) videos? click here now!

In this post, we’re gonna explore some concepts related to the ways of how to use multiple sequences in our program: Multitasking!

Why should we be worried about multitasking?

In a competition, like FLL, instead of using fixed configuration for our robot, we could collect information from the track during the race and set the parameters at runtime.

Think about a line follower robot: Will track illumination influence the performance of our robot? Will the material that they made the track influence robot’s performance in the same way that does impact playing tennis on a hard, Grass or Clay Court?

Collecting data at runtime can be our competitive differential, and may represent a place on the podium! Who knows? Just get ready!

For Data Logging see Episode #10 ;)

Let’s get it on!

To start it off, let’s play with a very simple sequence: 1, 2 and 3.

Fig 1. code: 13_Simple_Sequence_Synch_v1 — Counting 1,2 and 3, right? Think again…

Are we counting 1, 2 and 3? Nope…instead, it is 1,3 and 2. Why is that?

The key issue resides in the fact that is the variable two, which is passed via data wire, that triggers the second treads. At that very moment, that is, after the three variable is presented on the screen, is that the two pops up.

The next alert is about loop: it must be finished first before variables can be passed by. In the program below, inside the loop, we are counting one through five and trying to display the digits on the screen. But in the end, just digit 4 appears on screen…Why is that occurs?

Fig 2. code: 13_Simple_Sequence_Synch_v1

The threads above are only initialized after the loop exits. And it comes out on index 4. Stay alert, okay?

The next exercise is a simple way to data logging the sensor readings at various distances from the black line: 13_ReadingBlackLine_v1:

Fig 3. Tribot moves over the line and we collect the data to File1

https://youtu.be/9v1-3VYyF8

Fig 4. Code: 13_ReadingBlackLine_v1; Reading black line program

The data is stored in a file named File1. The loop block is configured to run until the Motor B Rotation Sensor reads greater than one rotation, which will move the Tribot far enough to accumulate all the data we want (Fig.3).

Here is the resulting graphic:

Fig 5. Graph from 13_ReadingBlackLine_v1; The symmetry is why we don’t try to make the Tribot follow the center of the line

The two nice straight lines formed by the readings as they change those 70 to 8 tells us that the reading is proportional to the distance from the edge, which is why we can use those readings as a reliable basis for the proportional line follower.

Reading At Runtime — Proportional Algorithm

Now the last exercise: let’s collect data as Tribot gives an entire revolution on the track.

Fig 6. Tribot around the circuit :D while we’re collecting data saving data to File2
Fig 7. Code: 13_Proportional_Lap_Reading_v1
Fig 8. The graphical result from the code 13_Proportional_Lap_Reading_v1

Look at that! Wow! Yeap Tribot works hard; I could not even imagine this because the Tribot runs smoothly all the way, could you?

The error line (red) is the mirror of the sensor reading (orange); Motor B (gray) basically takes care of the outer (of the curve) wheel; Motor B is the one that works hard and it reacts proportionally to the recorded error (60%), while Motor C (black) only works hard at the most critical point of the curve (Fig.6); When the speed of the two motors is the same (well at the end of the lap), then its curves (Motors A & B) overlap (Fig. 8).

When We Must (Not) Use Multitasking

. Use a second sequence (treads) only when it’s really necessary;. Edit the program slowly;. Avoid trying to control the same motor or sensor from more than one treads;. Use Variables instead of data wires to pass information between treads;. Be specially careful with data wires that pass into or out Loop blocks and Switch blocks;. Multiple sequences are most appropriate when the tasks they perform are independent;

And that’s all folks!

Thanks for being here!

Until PID ….but to get there in the next episode we’re gonna deal with normalization o/

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

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

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

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

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

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!