
.EndIf
' …
.CloseComm
End With
End Sub
Once you've mastered
If
, the
While
loop is easy. Like
If
, it compares two values using an operator. The body of the
While
(until an
EndWhile
) is executed until the comparison is false. The
body of the following loop, for example, would be executed as long as the value of input 3 was less than 100:
While SENSOR_VALUE, SENSOR_3, LESS, CONSTANT, 100
' Loop body
EndWhile
Page 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
immediately 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 imagine 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 current battery charge. The
PBBattery
function returns the current battery voltage in milliVolts (mV). For fresh batteries, you should get a
reading of 9000. The following example shows how you can retrieve and display the current battery power level:
Sub ShowBattery()
With DummySpiritForm.Spirit1
.InitComm
battery = .PBBattery()
.CloseComm
MsgBox (battery)
End With
End Sub
Retrieving the Datalog
Back in Chapter 4, I described 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 Command Center, 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 to extract the datalog from the RCX. You can modify this program to suit your needs.
The main part of the program is a subroutine,
SaveDatalog
. It opens up communications with the RCX, extracts the datalog values, and writes them into a file of comma-separated values. It's
all pretty straightforward, except that the RCX will
Содержание MINDSTORMS Robots
Страница 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Страница 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Страница 25: ...Page 22 Attach the motor wires to output A and output C Next build the platform for the front bumpers ...
Страница 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Страница 41: ...Page 41 ...
Страница 43: ...Next build the support for the light sensor ...
Страница 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Страница 82: ......
Страница 84: ...Page 89 ...
Страница 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Страница 86: ...Page 90 Grabber Arm In Step 17 the half bushings go between the center block and the cams pear shaped pieces ...
Страница 87: ...Page 91 ...
Страница 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Страница 89: ...Page 93 Page 94 ...
Страница 90: ...Structural Support Page 95 ...
Страница 91: ...Idler Wheel Page 96 ...
Страница 92: ...Page 97 Drive Motor ...
Страница 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Страница 94: ...Grabber Arm Motor ...
Страница 95: ...Page 99 ...
Страница 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Страница 158: ......
Страница 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Страница 160: ...Page 176 ...
Страница 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...
Страница 162: ...The light sensor which is mounted on the bumper is attached to input 2 The touch sensor goes on input 1 Page 179 ...