first). All is well if you are writing a
fairly non-complex app such as
blinking some LEDs, driving around
in circles, etc., but once you get
into a more complex application
where many things are potentially
happening at the same time, then
things get a little screwy. This is
because there is no facility in these
languages (nor should there be) to
monitor or do many things at the
same time, or at least give the
appearance of doing so.
Consequently, we typically fall
back to writing some form of case
statement or loop to perform all the
necessary tasks one right after the
other and therein lies the problem.
We do these things one right after the
other, in a serial fashion. In our Mini
Sumo example, this is not good if we
are in “attack mode” and we run
across the line without being able to
get back around the loop to check
the sensors. I know this is a rather
obtuse example but nevertheless, it is
difficult to write code like this in a
serial fashion and it often ends up
So what is the solution? Most
computers you are familiar with today
have an operating system, for example
MS Windows, which handles all the
housekeeping required and gives us
the impression that many things are
happening at the same time.
• The ability to split our application
into separate independent tasks.
• The ability to prioritize these tasks.
• The requirement that the highest priority available task runs (Pre-emptive
• The ability to pass messages
THE MINI SUMO
With the aid of FreeRTOS, we are
no longer constricted to writing serial
code. We can divide our application
up into separate independent tasks
and FreeRTOS will give us the illusion
that each of these tasks is running at
the same time. It does this by rapidly
switching between tasks (giving each
one a small amount of time to run —
a tick), making sure that if a higher
priority task is ready to run, it will
stop the current task and
start the higher priority
task. This is the part which
is called Pre-emptive
further, we should think a
bit about those functions
which are important for a
Mini Sumo robot to have
and their associated priority. For example, these
the function behind each task is
understandable. The first two tasks
— Wait_For_Start and Start_Strategy
— only exist long enough for the
operator to press the start button and
for Start_Strategy to move the bot out
of the way of an immediate attack
and to get all the other tasks running.
After that, they are deleted, which
frees up some memory. This leaves us
with all the other independent tasks
Note that all of these tasks are
running concurrently (or giving the
illusion thereof), allowing us to
concentrate on various strategies of
attack and defense, all the while in the
background we have a set of tasks
running which are checking the sensors. This approach of using FreeRTOS
gives us the ability to program much
more complex behaviors than might
be possible with using a language
‘only’ on top of the chip.
All is fine but, so far, there is little
organization to all of these tasks. This
■ FIGURE 2. Independent Mini Sumo Tasks.
So, why not put an operating
system on our Mega32 with its 32K
of Flash memory and 2K of SRAM?
Exactly! Why not? Sometime ago, I
found an excellent operating system
ideal for this purpose — FreeRTOS.
There are two important words
joined together here. The first is
‘free’ (yes, this operating system
will cost you not a penny) and the
second word is ‘RTOS’ which stands
for Real Time Operating System. This
terrific little operating system is
perfectly sized to fit inside the
Mega32 or even a smaller chip such
as the Mega16 and may be obtained
FreeRTOS provides us with several sets of much-needed functionality:
• Don’t fall out of the ring.
• Find the opponent.
• Push the opponent out
of the ring.
And they should
happen in that order of
We can now design
our application by breaking these basic functions
into separate tasks. As you
can see from Figure 2, I
have broken the app into
about 10 separate tasks.
going into a lot of detail,
on button press, wait 5 seconds then run
rotate left/right and back up 6 inches.
create and run all the other tasks.
aimlessly wander randomly around the ring.
back away from line and turn towards the
center of the ring.
drive forward away from line.
Head straight at opponent and keep going.
Check to see if front line sensor on.
Check to see if rear line sensor on.
Check to see if target in range.
August 2006 83