At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Robotics » LEGO Mindstorms

Using spirit.ocx in Visual C++

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 Project

This 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":

Press "Insert" and then close the dialog. For your own interest, check out the CSpirit class that is generated. Now, open up your main dialog in the resource editor. You will notice there is a LEGO icon in the controls toolbar! Click and drag a LEGO control on to your dialog. Now, bring up the properties and uncheck the "Visible" property.

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 Programming

Since 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
  // ...
}

Motors

Motors 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.

Sensors

Sensors 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	3
Now, 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();

Conclusion

There 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.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -