http://cylonrobot.blogspot.com/feeds/posts/default

Monday, September 27, 2004

A bit of sample code

One of the reasons I have been pursuing Windows XP Embedded and the .NET Framework 2.0 as the brain of Cylon is to be able to build on top of a high level programming language that gives me much more power to write detailed algorithms. Here are two example methods from the CylonControl class that should give you an idea what it looks like to write robot control code in C#:

private void _Track()

{
if (!_Tracking) return;

Double delta = 3 * _HeadingDelta(_TrackHeading, _Heading);

// Govern down the turns so they aren't too severe
if (delta < -120) delta = -120;
if (delta > 120) delta = 120;

Steering = (SByte)delta;

if (_StopWhenTurnComplete)
{
if (Math.Abs(delta) < 15)
{
_Tracking = false;
_Status = "Turn complete.";
Stop();
}
}
}


private void _UpdateTelemetry()
{
while (!_Disposed)
{
lock (this)
{
lock (brainstem)
{
_Analog0 = (Int16)brainstem.Analog_ReadInt(0);
_Analog1 = (Int16)brainstem.Analog_ReadInt(1);
_Analog2 = (Int16)brainstem.Analog_ReadInt(2);
_Heading = brainstem.CMPS03_GetHeading(Degrees);
}

_Track();
}

Thread.Sleep(50);
}
}


Now what may not be evident here is just how much simpler you can make your robot logic once you have a powerful CPU & operating system that can use techniques such as multiple threads, locking, etc. One of the things I hope to find out over the next few years is just how much easier it is to write complex behavior mechanisms now that I don't have to rely upon state machines and complex time-sharing logic due to the limitations of a small CPU.

Operating system provided services such as networking, threads, asynchronous I/O, should bring to robotics the ability to develop much more sophisticated programs. For instance, my UI, telemetry, and tracking methods all run on different threads simplying what each method has to do and allowing me to get much more efficient use of resources such as the slower RS-232 link between the Brainstem and the Mini-ITX PC motherboard. A simple locking mechanism keeps the threads from each trying to access the same resource simultaneously.

In a future article I'll go into some detail about the threading model of Cylon and how the various components interact between each other and the hardware.