This is the first in a series of posts I'll be writing about the Gyro Boy robot. The program supplied with the robot works, but is almost impossible to read. Tim challenged my to write a literate Gyro Boy program and I think it is a great opportunity to show of some of the power of visual programming that few teams ever realize. Note that this is more a reinterpretation than a strict translation. You'll never find two programmers who agree 100% on how a project should be implemented.
Attached is a shot of my top level Gyro Boy program. It took me hours to read the LEGO version of this program. A couple of minutes should be adequate to understand what my version is doing. Comments would have gone a long way in making the LEGO version easier to understand, but I hope to show how hierarchy and modularity are equally, if not more important.
The most noticeable thing when looking at the image are the large comments. In Literate Programming comments are not used to describe the code, which needs no comments because it is easy to understand, but rather to document the design and usage. The top comment block describes what this program does (how you use it) at a high level. After reading the comment you should be able to use Gyro Boy.
Below the main comment are two loops; Balance and Command. EV3 lets you label loops for the Loop Interrupt block, but I think the labels are great for documentation too. The top loop balances the robot. The bottom loop uses the color and ultrasonic sensors to tell the robot what to do. Everything is presented at a high level for quick understanding. To find detail you must delve down into the next layer of MyBlocks.
To be continued...