back home

Controlling the CyberMaster with C#

In order to program your Cybermaster in C# you will need Visual Studio .NET (or the free tools below) and the Spirit.ocx file (on the RCX or CyberMaster CD - they are both the same). I'm using C# because it's the most similar language to Java, though Spirit.ocx can be programmed with C++ or Visual basic as well.

Free C# Tools

A free open source IDE is available from SharpDevelop: http://www.icsharpcode.net/OpenSource/SD/default.asp

Complete Spirit.ocx Documentation

Lego has produced a pdf file of all the commands that can be sent to the CyberMaster or RCX with SPirit.ocx.

Setting Up Spirit.ocx

  1. Start Visual Studio .NET and create a new project.
  2. If Spirit.ocx is not registered you will need to make it visible. Select Project, Add Reference... Click on the COM tab and then browse to your Spirit.ocx file (it can be located anywhere).
  3. SpiritCtrl should now be visible in the Toolbox under the Components heading. Drag it onto a form.

Initialization Code

This code must be executed before you can attempt control. Make sure the CyberMaster is turned on.

axSpirit1.ComPortNo = SPIRITLib.COMPORTOPTIONS.COM2; // Choose com port here
axSpirit1.LinkType = SPIRITLib.LINKTYPEOPTIONS.Radio;
axSpirit1.PBrick = SPIRITLib.PBRICKOPTIONS.Spirit;
axSpirit1.InitComm(); // Set up the comms
axSpirit1.UnlockFirmware("Do you byte, when I knock?");
axSpirit1.UnlockPBrick();

Verifying the CyberMaster is Alive

You can pop up an error message telling the user to turn on the CyberMaster, then retry initialization.

if (!axSpirit1.PBAliveOrNot()) {
   // Handle CyberMaster Problem
}

Controlling Motors

Most motor commands accept Strings to identify the motors. The methods pull only numbers out of the strings and ignore the letters (but you can add text to make the commands easier to understand). Motors 0 and 1 drive the wheels and motor 2 controls the center output.

Setting Motor Direction

axSpirit1.SetRwd("Motor 0 and Motor 1"); // Reverse motors 0 and 1
axSpirit1.SetFwd("Motor 2"); // Forward motor 2

Turning the motors on

axSpirit1.On("0"); // Left wheel only
axSpirit1.On("1"); // Right wheel only
axSpirit1.On("2"); // Center output only
axSpirit1.On("01"); // Left and Right only

Turning Motors off

axSpirit1.Off("012"); // All motors
axSpirit1.Off("1"); // right wheel only

Reading Sensors

CyberMaster can only use passive sensors (touch sensor, thermometer, photoresistor).

int s1 = axSpirit1.Poll(9,0);
int s2 = axSpirit1.Poll(9,1);
int s3 = axSpirit1.Poll(9,2);

String valStr = s1 + " " + s2 + " " + s3;
textBox1.Text = valStr;

Playing Sound

short freq = 500; // Frequency in hertz
short time = 100; // 1 second (time in centiseconds, NOT milliseconds)
axSpirit1.PlayTone(freq, time);

back home