Vision Command Smart Sensor:

Vision Command (VC) doesn't allow for very complex RCX programming. It only provides for sound and motor commands. It would be nice if VC could be treated as a smart sensor available from a conventional RCX program. The following is a description of a fairly simple method to do just that. Many thanks to Rainer Balzerowski who did the pioneering work in this area.

First you need to understand how VC works with the RCX. When you click the RUN button in VC, it creates, downloads, and runs a small program in slot 5 of the RCX. The program name is always myprog.txt. Some of the RCX commands are actually expressed in the program as macros. For example the Spin Left command is macro bb_SpinLeft. If you are only using VC as a smart sensor you really don't care about commands like Spin Left since you will be writing your own control program. This is how you take control away from VC. If you modify the macro definition file in the correct place, you can fool VC into sending a program to the RCX that will jump to your control program waiting in another slot. By putting the modified RCX command into the VC AutoPilot code stack you are sure to transfer control immediately.

Edit RCX2MLT.h in the following way (might be a good idea to make a backup copy of it first):

C:\Program Files\LEGO MINDSTORMS\Vision Command\script\Include\RCX2MLT.h

Original bb_SpinLeft Macro:

macro bb_SpinLeft(nLeft, nRight, nTime) {
	dir nRight nLeft
	on [nLeft nRight]
	wait nTime
	float [nLeft nRight]
}

Modified bb_SpinLeft Macro:

macro bb_SpinLeft(nLeft, nRight, nTime) {
	slot 1          //This one instruction makes VC start what ever program is in slot 1
	dir nRight nLeft
	on [nLeft nRight]
	wait nTime
	float [nLeft nRight]
}

Now you need to write a VC program to use it as a smart sensor:

  1. Select a pattern. Pattern 6 is a good start since it uses all 8 regions.
  2. Make sure the Spin Left command is first in the AutoPilot stack. You also need to set the time for the command to 0.1 sec.
  3. Under every region of the pattern, make sure there is a RCX command that doesn't involve time. Good examples are the Set Power or Brake. You are doing this to make sure VC will send a message to the RCX telling it that the region has become active. If time is involved, VC waits for the RCX to execute the command and you don't want it to waste time.

Here is an example of a VC program using pattern 6:

 

 

If you are curious, here is the LEGO Script Program VC will make from this program:

C:\Program Files\LEGO MINDSTORMS\Vision Command\script\myprog.txt

Using LEGO Script from the RCX2.0 SDK or NQC 2.2+ write your control program. As a region become active, VC will send out a message with that region's number. This number is available within the program as the keyword "message". Here is the smallest LEGO Script program I could think of that would demonstrate the principle. It will continually show the last active region number on the RCX display. It must be loaded into slot 1 prior to running VC. VC and the ATLClient compete for the IR tower so they can't both run at the same time.

program test{
  main{
  display message
  forever{}
  }
}

Here is the same thing in NQC:

task main()
{
  while(true)
  {
  SetUserDisplay(Message(),0);
  }
}

There you have it. A smart sensor that can be used to detect motion or color.

Dean Husby suggested that you can add a little more flexibility with another modification. Modify the SpinLeft macro as shown below to allow you to select the slot number from within VC. With the modification, you select slot number by entering .1s in the SpinLeft command for slot 1, .2s for slot 2, .3s for slot 3, and .4s for slot 4. Slot 5 already has the VC program so it doesn't make sense to jump to that. You will notice that VC actually passes .1s as the value 10 etc. Longer times will ignore the whole slot jumping process and the SpinLeft command behaves like it did originally.

Modified bb_SpinLeft Macro:

macro bb_SpinLeft(nLeft, nRight, nTime) {
	select nTime{   // The following lines check the value of nTime and jump to the appropriate slot.
          when 10 {slot 1}
          when 20 {slot 2}
          when 30 {slot 3}
          when 40 {slot 4}
        }
	dir nRight nLeft
	on [nLeft nRight]
	wait nTime
	float [nLeft nRight]
}

 

Click Here to Return to the Vision Command Page