| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
|
spirit.ocx is the ActiveX control that is supplied with LEGO Mindstorms that is used to communicate with the RCX. Since the control is ActiveX-based, it is very easily accessed from Visual Basic and Visual Basic for Applications. There are many documents on how to access it from VB, but how about Visual C++?
The ProjectThis is also relatively simple. Start a new MFC project, and make sure your program is dialog-based. Once you have finished ironing out the details of your project and have a new project created, go to "Project, Add to Project, Components and Controls". A dialog box will pop up, double-click on "Registered ActiveX controls". A folder of ActiveX controls will appear - scroll across to find "Spirit Control":
Bring up ClassWizard, and switch to the Member Variables. Now, select your Spirit control and add a variable of type CSpirit. Now to actually set up the communications and variables, and do a little programming.
The ProgrammingSince spirit.ocx is also used to control the CyberMaster Technic kit, you have to set up a few variables: the COM port, the link type and the "brick type". Here is the code I normally use:
m_cRCX.SetComPortNo(1); // COM port 1
m_cRCX.SetLinkType(0); // 0 = IR
m_cRCX.SetPBrick(1); // 1 = RCX, 0 = CyberMaster
m_cRCX.InitComm(); // Set up the comms
// Is the RCX responding?
if (!m_cRCX.PBAliveOrNot()) {
// ...
// Handle RCX Problem
// ...
}
MotorsMotors are pretty easy to control using the Spirit control, mainly because you use strings to denote which motors you want.
m_cRCX.SetFwd("Motor 0 and Motor 2");
m_cRCX.On("02");
Sleep(100); // CSpirit::Wait not used,
m_cRCX.Off("02"); // since it is only downloadable.
Notice how the first line actually uses an English phrase. This is because the routine simply reads the first two numbers between 0 and 2 in the string. This applies to all motor commands, I used it only once as an example.
SensorsSensors are a little more complicated since you have to understand IF-THEN statements. First things first - we should make a couple of equates to make our lives much easier:#define RCX_VARIABLE 0 #define RCX_CONSTANT 2 #define RCX_SENSOR 9 #define RCX_SENSOR_1 0 #define RCX_SENSOR_2 1 #define RCX_SENSOR_3 2 #define RCX_GREATER 0 #define RCX_LESS 1 #define RCX_EQUAL 2 #define RCX_NOTEQUAL 3Now, the CSpirit::If and CSpirit::While functions take 5 parameters. The first two determine the first value of the comparison split into type and value, then the comparison operator, then the last two are the second value. For instance, if you want to compare whether the value from sensor 3 equals 4:
bool sensor3 = m_cRCX.If(RCX_SENSOR, RCX_SENSOR_3,
RCX_EQUALS, RCX_CONSTANT, 4)
If you would like to loop as long as sensor one doesn't equal 1 (a touch sensor):
m_cRCX.While(RCX_SENSOR, RCX_SENSOR1,
RCX_NOTEQUAL, RCX_CONSTANT,1);
//...
m_cRCX.EndWhile();
ConclusionThere is a lot more to the spirit.ocx control, but most of it can be figured out from the class definition, or from the LEGO Mindstorms SDK documentation (see http://www.legomindstorms.com/ for more details). You can create programs within your C++ program and download them to the RCX to be executed at a later date, you can poll any sensor or motor for its value, allowing you to use your computer as a huge datalog, you can check the battery level, play notes, turn the RCX off - just about anything! Have fun...
Submitted: 24/08/2000 Article content copyright © James Matthews, 2000.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -