Reflex Example 2: A2D SamplesIndex

The Problem

Suppose a BrainStem must take samples from a device with an analog output, scale them from 0-255, and send them to the host at half-second intervals.

The Solution

Here is some logic to implement this function:

The Details

We need to describe this logic in terms that can be directly translated into BrainStem commands.  Doing this requires the use of analog device input, parameter modification, and a reflex timer.  This revised logic is shown below:

Reflex 100 Take a reading from analog input 0 and issue Reflex 0 with that reading as input.
  Reflex 0 Modify parameter (copy A2D reading) in debug packet and send it to host.
  Set Timer 1 for 0.5 seconds
Reflex 20 Issue Reflex 100.

Reflex 0 is the reflex that is activated when a conversion is ready for A2D input 0.  The value 0 corresponds to the device ID for A2D input 0.  Reflex 20 is reflex that is activated when Timer 1 expires.  Start at Reflex 100 above and trace the flow of the routine.  Reflex 0 must start Timer 1 to keep the reflexive routine going.

The Commands

In the reflexive routine described above, there are really only four unique commands.  These four commands, complete with address byte and size byte, are listed below.  They are numbered from 0 to 3.  Note the (0) in command 1.  It represents a byte that will be a modifiable parameter in the final reflexive routine.

0 Take a reading from analog input 0 and issue Reflex 0 with that reading as input. 2 2 25 64
1 Modify parameter (copy A2D reading) in debug packet and send it to host.
2 2 23 (0)
2 Set Timer 1 for 0.5 seconds
2 4 39 1 19 136
3 Issue Reflex 100.
2 3 38 100 0

The cmdA2D_RD command initiates an A2D reading.  If the data is going to a reflex, a cmdRAW_INPUT command goes onto the command queue when the A2D conversion is done.  The A2D conversion is 10 bits so it is a 2-byte result.  Fortunately, the first byte has the most significant bits of the result.  That means the first byte is the result scaled from 0-255.  The second byte can be ignored.

The cmdDEBUG command shown above outputs a 0.  This looks like a problem.  There is no way for this single command to send data other than a 0 back to the host.  Fortunately, the reflex vector may be configured so that an input byte can replace that 0.

The cmdTMR_SET command starts a timer.  The GP module has 24 timers for use by reflexes.  The resolution is 0.1ms so the timer must be set to 5000 to get a 0.5 second delay.  This time is in a high-byte low-byte format (19*256+136=5000).  After starting the timer, the reflex is stalled and the processor is free to work on other tasks.  Once Timer 1 expires and starts reflex 20, the reflexive routine can take a new reading.

These commands go in reflex command slots 0-3.  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 2 25 64
Reflex CMD 1 2 12 128 1 2 2 23 0
Reflex CMD 2 2 12 0 2 4 39 1 19 136
2 12 134 2
Reflex CMD 3 2 12 128 3 2 3 38 100 0

The Vectors

Now that the reflex commands are in the EEPROM, we need to define the reflex vector data for reflexes 100, 0, and 20.  A reflex vector is an array of up to eight 2-byte records.  Each record contains a reflex command index and control fields for executing that command.  Since vectors 100 and 20 have only one command, they only have one record each.  Vector 0 has two commands and two records.  The binary contents of each reflex vector along with a description of what the contents mean is listed below:

Reflex Vector 100 Cmd 0, (last in vector)
[ 1 | 0000000 ] [ 1 | 000 | 0000 ]
128 128
Reflex Vector 0 Cmd 1, byte[3]=byte[3]+input
Cmd 2, (last in vector)
[ 0 | 0000001 ] [ 1 | 001 | 0011 ]
[ 1 | 0000010 ] [ 1 | 000 | 0000 ]
1 147
130 128
Reflex Vector 20 Cmd 3, (last in vector)
[ 1 | 0000011 ] [ 1 | 000 | 0000 ]
131 128

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

Vector 100 2 11 128 100 128 128
Vector 0 2 11 128 0 1 147 130 128
Vector 20 2 11 128 20 131 128

The Results

The reflexive A2D sampling 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 "samp.txt" in your "aUser" directory.  The Console can download the file to the BrainStem.  Just enter batch "samp.txt" at the prompt.

To test the routine, connect some kind of analog device to Analog input 0.  This could be a potentiometer.  Enter the following command to start the reflex:

You should see debug packets appear in the Console every half second.  The last byte in the packet will be the analog value in hexadecimal.  Vary the analog input.  You should see the contents of the packet change.  To stop this reflexive routine, you will need to reset the BrainStem with the cmdRESET command, or turn off the power.

This example demonstrates how reflexes can loop, delay, and modify their commands based on inputs.

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

ex2_loop.leaf

Download "ex2_loop.leaf" here
/* file: ex2_loop.leaf */

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

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

/* message ids */
#define mGETA2D 0
#define mTOHOST 1
#define mSETTMR 2
#define mRAW100 3

module[MODULE] {

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

  message[mGETA2D] {
    MODULE, cmdA2D_RD, 64
  }

  message[mTOHOST] {
    MODULE, cmdDEBUG, 0
  }

  message[mSETTMR] {
    MODULE, cmdTMR_SET, 1, (int)5000
  }

  message[mRAW100] {
    MODULE, cmdRAW_INPUT, 100, 0
  }

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

  vector[100] {
    mGETA2D
  }

  vector[aGP_RFX_A2D_0] {
    char + mTOHOST[3],
    mSETTMR
  }

  vector[aGP_RFX_TIMER_1] {
    mRAW100
  }
}

Here is the BAG output from the LEAF compiler:

ex2_loop.bag

Download "ex2_loop.bag" here

// module 2 (4 messages, 3 vectors)

// message 0
2 12 128 0 2 2 25 64
// message 1
2 12 128 1 2 2 23 0
// message 2
2 12 0 2 4 39 1 19 136
2 12 134 2
// message 3
2 12 128 3 2 3 38 100 0
// vector 0
2 11 128 0 1 147 130 128
// vector 20
2 11 128 20 131 128
// vector 100
2 11 128 100 128 128

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

Revisions

  • 10/13/01 - Fixed a bug discovered by Jeff Gould.  One of the cmdSET_TMR commands did not have an index byte.
    version: 1.0, build 80903
    © Copyright 1994-2008 Acroname, Inc., Boulder, Colorado.  All rights reserved.