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.
A free open source IDE is available from SharpDevelop: http://www.icsharpcode.net/OpenSource/SD/default.asp
Lego has produced a pdf file of all the commands that can be sent to the CyberMaster or RCX with SPirit.ocx.
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();
You can pop up an error message telling the user to turn on the CyberMaster, then retry initialization.
if (!axSpirit1.PBAliveOrNot()) {
// Handle CyberMaster Problem
}
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.
axSpirit1.SetRwd("Motor 0 and Motor 1"); // Reverse motors 0
and 1
axSpirit1.SetFwd("Motor 2"); // Forward motor 2
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
axSpirit1.Off("012"); // All motors
axSpirit1.Off("1"); // right wheel only
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;
short freq = 500; // Frequency in hertz
short time = 100; // 1 second (time in centiseconds, NOT
milliseconds)
axSpirit1.PlayTone(freq, time);