Reflex Example 6: Subsumption and ReflexesIndex

The Problem

Suppose a BrainStem must perform a default task A.  It must monitor two inputs and perform task B when a 1-0 transition occurs on digital input 0 and perform task C when a 1-0 transition occurs on digital input 1.  Task C has priority over task B.  Task B takes a short time and after completion the Stem must perform default task A again.  Tasks B and C are implemented with TEA programs.  For the sake of simplicity, each "task" just prints a message.

This is a simple example of subsumption control.

The Solution

Two independent reflexes can perform the control logic for this application:

TEA program 1 implements task B and TEA program 2 implements task C.  TEA program 2 controls a reflex enabler which makes sure that task C gets priority over task B.

The Details

We need to describe this logic in terms that can be directly translated into BrainStem commands.  The digital input polling feature described in Example 4 may be used to check for transitions on inputs 0 and 1.

Reflex 15 Check reflex enabler 0.
  If reflex enabler 0 is on then issue reflex 100.
  Reflex 100 Launch TEA program 1.
  Reflex 16 Launch TEA program 2.

Reflex 15 is the reflex that is activated when the digital polling loop detects a transition on digital IO pin 0.  The value 15 corresponds to the device ID for detecting transtions on digital IO pin 0.  A reflex enabler acts as a "switch" for reflex 15.  When turned on, reflex 15 can activate reflex 100.  Reflex 16 is for a transition on digital IO pin 1.

Three TEA programs implement the tasks and interact with the reflexes.

Program 0 configures digital IO pins 0 and 1 to trigger reflexes when they see a 1-0 transition.  It also makes the Stem perform default task A and configures reflex enabler 0. Port 0x0380 configures reflex enabler 0.  Writing 0xE4 (128 | 100) to that port turns the enabler on and makes it issue raw input 100 when checked.

Program 1 performs task B.  It resets the polling on digital IO pin 0 by writing to port 0x0503 and exits.

Program 2 disables reflex B, performs task C, and waits for 10 seconds.  Then it performs default task A and enables reflex B again.  It resets the polling on digital IO pin 1 by writing to port 0x0507.  It also resets the polling on digital IO pin 0 by writing to port 0x0503 since a 1-0 transition may have occured during task C.  Then it exits.

The Commands

In the reflexive routine described above, there are really only three unique commands.  These three commands, complete with address byte and size byte, are listed below.  They are numbered from 0 to 2.

0 Check reflex enabler 0 and if it is enabled
then issue its raw input with a parameter of 0.
2 3 41 0 0
1 Launch TEA program 1.
2 3 21 3 1
2 Launch TEA program 2.
2 3 21 3 2

TEA Program 0 configures reflex enabler 0.  The cmdRFLXE_CFG command could be used to do the same thing.  A 1-0 transition on pin 0 will trigger reflex 15.  That reflex executes the cmdRFLXE_CHK command which checks reflex enabler 0.  If active, the command will propagate a new raw input to trigger reflex 100.  Reflex 100 executes a cmdVM_RUN command to perform task B.  A 1-0 transition on pin 1 will trigger reflex 16.  Reflex 16 executes a cmdVM_RUN command to perform task C.

These commands go in reflex command slots 0-2.  The series of cmdMSG_WR commands listed below will save the command data into the appropriate locations in the EEPROM.

Reflex CMD 0 2 12 128 0 2 3 41 0 0
Reflex CMD 1 2 12 128 1 2 3 21 3 1
Reflex CMD 2 2 12 128 2 2 3 21 3 2

The Vectors

Now that the reflex commands are in the EEPROM, we need to define the reflex vector data for reflexes 15, 100, and 16.  The binary contents of each reflex vector along with a description of what the contents mean is listed below:

Reflex Vector 15 Cmd 0, (last in vector)
[ 1 | 0000000 ] [ 1 | 000 | 0000 ]
128 128
Reflex Vector 100 Cmd 1, (last in vector)
[ 1 | 0000001 ] [ 1 | 000 | 0000 ]
129 128
Reflex Vector 16 Cmd 2, (last in vector)
[ 1 | 0000010 ] [ 1 | 000 | 0000 ]
130 128

This vector data goes into reflex vector slots 15, 100, and 16.  The series of cmdVEC_WR commands listed below will save the vector data into the appropriate locations in the EEPROM.

Vector 15 2 11 128 15 128 128
Vector 100 2 11 128 100 129 128
Vector 16 2 11 128 16 130 128

The Results

The reflexive control routine is now completely defined.  A text batch file can store all the commands for downloading the reflex data.  Copy and paste the following lines of numbers to a file called "ctrl.txt" in your "aUser" directory.  The Console can execute these commands automatically.  Just enter batch "ctrl.txt" at the prompt.

To test the routine, copy and paste the source of the three TEA programs to files "prg0.tea", "prg1.tea", and "prg2.tea" in your aUser directory.  Run the Console and compile the three TEA programs with the steep command.  Download them into their appropriate file slots (0,1,2) with the load command.

Run program 0 by entering "launch 2 0" at the Console prompt.  The message "Task A" should appear in the window.  During testing, you will also see status messages as the TEA programs start and stop.  Fake a 1-0 transition on digital input 0 by entering the following command:

You should see "Task B" in the window.  Now fake a 1-0 transition on digital input 1 by entering the following command:

You should see "Task C" in the window.  Reflex 15 should now be disabled.  Immediately enter the following command again:

You should not see "Task B" in the window because that reflex has been disabled.  When task C is completed, you should see "Task A" in the window.  Once this occurs, Reflex 15 is enabled again.  Enter:

and you should see "Task B" in the window.  Instead of faking the transitions with commands, try hooking up switches to digital inputs 0 and 1 and see what happens.  Connect a 1K to 10K pull-up resistor between the input and Vcc and connect a normally-open switch between the input and ground.  That makes the switch cause a 1-0 transition when pushed.

This example demonstrates how reflexes can be used to launch TEA programs and how TEA programs can control reflexes.  It also demonstrates a simple subsumption control strategy.

Here is a LEAF source file for the reflex routine in this example:

ex6_rflxe.leaf

Download "ex6_rflxe.leaf" here
/* ex6_rflxe.leaf */

#include <aCmd.tea>
#include <aGPReflexes.tea>

/* assume the BrainStem has address 2 */
#define MODULE 2

/* message ids */
#define mCHK 0
#define mRUN1 1
#define mRUN2 2

/* vector ids */
#define vRUN1 100

module[MODULE] {

  /************* REFLEX COMMANDS *************/

  message[mCHK] {
    MODULE, cmdRFLXE_CHK, 0, 0
  }

  message[mRUN1] {
    MODULE, cmdVM_RUN, 3, 1
  }

  message[mRUN2] {
    MODULE, cmdVM_RUN, 3, 2
  }

  /************* REFLEX VECTORS *************/

  vector[aGP_RFX_DTRANS_0] {
    mCHK
  }

  vector[vRUN1] {
    mRUN1
  }

  vector[aGP_RFX_DTRANS_1] {
    mRUN2
  }
}

Here is the BAG output from the LEAF compiler:

ex6_rflxe.bag

Download "ex6_rflxe.bag" here

// module 2 (3 messages, 3 vectors)

// message 0
2 12 128 0 2 3 41 0 0
// message 1
2 12 128 1 2 3 21 3 1
// message 2
2 12 128 2 2 3 21 3 2
// vector 15
2 11 128 15 128 128
// vector 16
2 11 128 16 130 128
// vector 100
2 11 128 100 129 128

This batch file contains the same commands for downloading the reflex that were listed for the "ctrl.txt" file, but in a different order.


version: 1.0, build 80903
© Copyright 1994-2008 Acroname, Inc., Boulder, Colorado.  All rights reserved.