Page 179 - The Unofficial Guide to Lego Mindstorms Robots
P. 179
168
Other Nuggets
Spirit.ocx offers some interesting capabilities that go beyond even NQC. One of these is the ability to turn off the RCX, using
PBTurnOff. Interestingly, this function can be executed either i mmediately or inside a program. The following Visual Basic
code defines an RCX program that turns itself off:
Sub TurnOffProgram()
With DummySpiritForm.Spirit1
.InitComm
.SelectPrgm 2
.BeginOfTask 0
.PBTurnOff
.EndOfTask
Pause 1
.CloseComm
End With
End Sub
By itself, of course, it's a pretty silly program. You can imagi ne how it might be useful, though: shouldn't your robot turn itself
off when it's accomplished its mission, to conserve battery power?
You can also use Spirit.ocx to query the RCX for the cu rrent battery charge. The PBBattery function returns the current
battery voltage in milliVo lts (mV). For fresh batteri es, you should get a reading of 9000. The following example shows how
you can retrieve and splay the current battery power level:
di
Sub ShowBattery()
With DummySpiritForm.Spirit1
.InitComm
b attery = .P BBattery()
.CloseComm
MsgBox (bat tery)
End With
End Sub
Retrieving the Datalog
Back in Chapter 4, I describe d the datalog, a special set of data in the RCX that your programs can use. It's easy to create a
datalog and add values to it in NQC, but how to you get that data back? Although you can retrieve the datalog using a tool like
nqc or RCX Comm and Cen ter, it may not be in exactly the format you'd like. In this section I'll show you how to write a
program in Visual Basic t o extract the datalog from the RCX. You can modify this program to suit your needs.
The main part of the program is a subroutine, Sav eDatalog. It opens up communicatio ns with the RCX, extracts the datalog
values, and writes them into a file of comma-separat ed values. It's all pretty straightforward, except that the RCX will