Tidbits

| | |

Tidbits

Quick notes about embedded and fuzzy logic.

Here's a list of interesting bits about embedded systems and fuzzy logic.

This tip comes from Jack Ganssle (http://www.ganssle.com), through his email newsletter, The Embedded Muse. Many thanks to Chester Page as well.

Suffice it to say, we like this technique. So do others: similar constructions (lower-case in one instance) appear in other languages. If you come across more ideas for Reliable C, drop us a line at info@bytecraft.com and we'll spread the word.


Redefining C’s Operators
------------------------
Chester Page sent along a bit of wisdom that’s useful for many of us. I’ll 
just quote his email here:

Back in EM85, you noted that several people had suggested putting constants 
on the left side of compares, so that the compiler would honk if <=> was 
used instead of <==>, with the aim of reducing the risk of inadvertently 
altering a variable when intending to compare it.

Well, yeah...  Fine, if one of the comparands happens to be a constant.

There's a more general fix for this problem, which is so obvious that I 
expected that dozens of people would instantly bring it to your attention, 
and that you would pass it on. As I haven't seen mention of it in later 
EM's, I'm bringing it to your attention myself, just to be sure that you've 
seen it.

About the 4077th time I mashed a C program by typing <=> when I meant <==> 
-- no doubt from having been corrupted by writing a lot of code in BASIC, 
with its promiscuous use of <=> for both assignment and comparison -- I 
decided that I just didn't like using <==> in the first place -- both for 
aesthetic reasons and because it was too similar, visually, to <=> -- and 
that I would thereafter -- no doubt from having been corrupted by writing a 
lot of code in FORTRAN -- use only <EQ> and its siblings for 
comparisons.  Thanks to C's much-maligned <#define> statement, this was 
trivial to implement:

  #define EQ ==
  #define GE >=
  #define GT >
  #define LE <=
  #define LT <
  #define NE !=

This makes a comparison very conspicuously a comparison.

After having to maintain a lot of my own legacy code for a while, I decided 
that the most important function of source code is not to describe an 
algorithm to a compiler, but to describe the intent of the program to the 
next person who has to deal with it, and that making a program maintainable 
is more important than making it any faster or as smaller than necessary, 
and infinitely more important than being concise in the source 
implementation.  For this reason, I never, ever, write a comparison that 
does not contain an explicit comparison operator:

Always

  if ( i NE 0 )
  {
  }
and never

  if ( i )
  {
  }

and even

  if ( BooleanVariable EQ TRUE)
  {
  }

As I said, all this is obvious:  I know several other programmers who have, 
entirely on their own, come up with the identical fix.  But it is perhaps 
still worth passing on.

The latest Lord of the Rings film has battle scenes with very large numbers of animated warriors doing battle. The film's producers had a choice: casts of thousands, or a single animation warrior at Weta Digital by the name of Stephen Regelous. Stephen created a software package called Massive, that is responsible for the vast battling hordes in the latest Lord of the Rings movie.

There is an interview and description of how Stephen used Fuzzy Logic to model the behavior of a cast of characters at exn.ca. Like real soldiers, each animated soldier can have very similar programming, with around 100 fuzzy logic rules describing behavior in response to local environmental conditions. For example, each animated character will realistically react to their surroundings. A wall of soldiers marching forward in parallel encountering an obstacle will realistically have some of them make minor detours to avoid the obstacle. Just as in a real army, some soldiers of different ranks or function are programmed to behave differently.

There's a showcase of Massive at massivesoftware.com.

Byte Craft has seen fuzzy logic rules used to store behavioral data bases for games before, but never on a scale this size.