Today, I propose a light C# library to perform Ev3 MultiThreading, with which you will be able to program your droid to perform multiple tasks at the same time. You can download and use the library source code under the MIT License from Smallrobots repositories on GitHub.

Summary

The need for a scheduler

The scheduler description

Three examples with videos

Credits

The need for a scheduler

With the LEGO MINDSTORMS EV3 Home Edition software, you can program the EV3 Brick to perform multiple concurrent actions easily enough using its graphical tools. However, It’s not so straightforward to do the same things using the C# and the .NET Framework with the Monobrick firmware library.

If you have a look at the first programs I’ve published on this blog, namely the Sup3rCar and the R3PTAR, you’ll notice that the scheduling of the periodic control tasks is delegated to the .NET Framework threading libraries. Pro: super-tested routines with less code to write and easy to maintain – you just ask the .NET Framework to wake up your thread when the sleep time elapses. Cons: there is no guarantee that each periodic routine is executed exactly each time its deadline is due.

The following example shows the routine that generates the sine wave that the R3PTAR follows to emulate slithering like a snake. Even if the thread is not woke up precisely each  waveThreadSampleTime milliseconds, no one will notice 🙂 !

Generally speaking, when the system under control is stable, only control performances are affected if a periodic control task experiences random delays at its execution start; the eventual decay of performances is acceptable or not depending on the particular application. However if the system under control is unstable, even a good control strategy will likely fail to stabilize the system if its timely execution is not guaranteed.

The Rolling Ev3rstorm clearly represents an unstable system: if the program on board the Ev3 Brick fails to stabilize the robot upright on the two wheels… well you can easily imagine what happens 🙂

During my first attempts to stabilize the Rolling Ev3rstorm, I used the .NET Threading API to schedule the tasks: no matter what values I did set for the controllers gains, it always fell down; that’s why I concluded that something was wrong with the control algorithm implementation and not with control algorithm itself.

The Ev3 MultiThreading scheduler description

The idea is then simple: instead of delegating the task scheduling to the .NET Framework, I designed a simple scheduler, implemented basically as an infinite loop, which starts the periodic tasks at the correct time.

The class diagram below shows the general idea:  a  Robot  owns a  TaskScheduler  which schedules  PeriodicTask (s). Please note that a the time of writing the proposed scheduler works only with periodic tasks.

Ev3 MultiThreading class diagram
Class diagram showing how to program the Ev3 for MultiThreading.

The scheduler works as follows:

  1. Check which is the next   PeriodicTask in the queue and determine its next due time;
  2. If the next due time is far to come, put the scheduling thread to sleep until about 200ms from next due time;
  3. Actively wait for the time to reach next due time;
  4. Start the scheduled task on time;
  5. After task execution re-schedule the task in the queue according to its next due time;
  6. If there is a conflict with an already scheduled periodic task, schedule the new due time after the conflicting periodic task to avoid task starvation.
  7. Repeat from point 1. until the scheduler gets stopped.

Point #3  will of course consume all the computing resources of the Ev3 Brick. But if all the droid activities are coded as periodic tasks, each of the task will be always executed on time.

Thing to note: the scheduler is not preemptive. This means that is your responsibility to write tasks that terminate correctly before it’s time for the next task to start. This also means that if a single task crashes, all the robot will crash. While this could be certainly considered a major issue in real robotic applications, for our little Lego Mindstorms Ev3 droid friends doesn’t matter too much.

See the code below for the  TaskScheduler :

Three examples with videos

The name I’ve given to this C# library is Ev3ControLib. Check the following post in this blog for some examples of droid programmed using this library:

  • The Rolling Ev3rstorm – a self balancing droid using the Ev3 Gyro Sensor chasing the Ev3 IR Beacon and firing at it when at firing range
  • The Formula Ev3 – a racing car which can be remotely piloted with the Ev3 IR Remote
  • The Sentin3l – an insect like droid remotely guided with the Ev3 IR Remote

Credits

In his work published on GitHub, Tomoaki Masuda presents a single periodic task executed with high timing accuracy using the concept of actively waiting for the due time when reaching its close proximity. In Ev3ControlLib library I extended this interesting concept to a periodic task scheduler to run multiple task at the same time on the Lego Mindstorms Ev3 Brick with the Monobrick firmware library.

Ev3 MultiThreading: a C# scheduler for droid tasks

Leave a Reply