| Reflexes - Getting Started Last Modified: 2008-01-24 | | |
| Acroname Robotics | PDF webpage version | ||
| Introduction This getting started guide walks you through how reflexes can perform many basic control, monitoring, and crisis functions within the BrainStem architecture. For complete documentation of reflexes, including examples, refer to the BrainStem Reference on our website. Reflexes are simply reactions to other actions. When a sensor returns a value, a timer expires, or a user-defined condition occurs, reflexes propagate commands. These commands may trip additional commands. Reflexes are ideal for small tight-loop control that takes place without the interaction of the host or other processes. The host can initiate reflexes and change the state of reflex loops but typically isn't involved with the reflex actions. Functions that are ideally suited to reflex loops include:
Before You Begin This guide assumes you have downloaded the Console application and understand its use. If you have not already done so, download the Console application and complete the Console Getting Started Guide before you begin working with reflexes. How do I use reflexes? Think of reflexes as a connected graph of nodes. In fact, reflexes are much like the finite state machines found in computer science textbooks. Each node represents a command and the directed lines between them represent what happens when one of the nodes "fires". Many elements can be incorporated into reflexes. All the I/O available on a particular BrainStem module can be accessed via reflexes. Most inputs can also propagate reflexes. In addition to hardware inputs, "virtual" reflex elements can perform simple arithmetic, perform comparisons and branching, count events, enable and disable reflexes, and use the scratch pad. These reflex elements don't actually tie to real I/O but rather act as intermediate nodes in the reflex graph. Compiling Reflexes After designing a reflex routine, you can implement this graph of nodes and connections using the Little Embedded Application Fragment (LEAF) compiler, which is included in the Console application. The LEAF compiler acts like an assembler and manages many of the flag bits and constraints of the reflex graph for you. Compiling a LEAF file (typically with a .leaf extension) creates a .bag (basic action graph) file, which contains simple console command definitions. The specified reflex graph can then be loaded onto a BrainStem Module using the batch command from within the Console. A Basic Example A very common task in robots or control systems is monitoring a sensor input. This example describes a reflex that takes an analog reading once per second and displays the result as a debug packet in the Console output window. Here is what the graph of this reflex would look like. ![]() Basic Example Reflex Flowchart Source Code - Basic Example Reflex Program When the timer expires, it initiates the A/D reading. When the A/D reading is complete, it initializes the timer and fires the debug packet, which is sent to the Console. The reflex.leaf file describes this graph. The message definitions correspond to the blue, rounded rectangles in the reflex graph above and the vector definitions correspond to the orange, rectangular blocks. Each arrow on the graph corresponds to a vector entry. /* file: reflex.leaf */
#include <aCmd.tea>
#include <aGPReflexes.tea>
/* assume the BrainStem has address 2 */
#define MODULE 2
/* message ids */
#define TIMER_1 0
#define A2D_0 1
#define DBG_PKT 2
module[MODULE] {
/************* REFLEX COMMANDS *************/
/* 0x2710 is 10000 1/10ms increments */
message[TIMER_1] {
MODULE, cmdTMR_SET, 1, 10000
}
/* 0x40 defines A/D 0 with reflex propagation bit set */
message[A2D_0] {
MODULE, cmdA2D_RD, 0x40
}
/* the zero gets filled in by the vector */
message[DBG_PKT] {
MODULE, cmdDEBUG, 0
}
/************* REFLEX VECTORS *************/
/* trip the A/D reading when the timer expires */
vector[aGP_RFX_TIMER_1] {
A2D_0
}
/* restart the timer and fill in the debug value */
vector[aGP_RFX_A2D_0] {
TIMER_1,
DBG_PKT[3] = char
}
}
Compiling Basic Example Reflex To assemble this file, place it in the aUser subdirectory of your main brainstem directory. Run the Console and then type: leaf "reflex"
The leaf assembler assumes the filename has a .leaf extension. You can type the extension if desired. The leaf assembler also accepts file names with other extensions. This operation creates the "reflex.bag" file, which will be placed in the aUser directory. You can store the reflex on the BrainStem module using the batch command within the Console application. In the Console, type: batch "reflex.bag"
The .bag extension is required in this case. Launching Basic Example Reflex The last thing to do is to initiate the reflex loop. You can do this through the Console application by starting timer 1 using the cmdTMR_SET command. Type: 2 39 1 0x27 0x10
which roughly translates to "run timer with 1 second firing time". This forces the reflex loop to start and gives you debug information for A/D channel 0 once per second in the Console output area. Once the loop is started, it runs until the module is reset or turned off. After a reset or when power is turned back on, the reflex is still in place but needs to be initialized with the cmdTMR_SET command. Source Code - Automatic Reflex Execution on Bootup A boot reflex can execute automatically when the BrainStem is powered up. This reflex is associated with the identifier aGP_RFX_BOOT. The "reflex_boot.leaf" file is a modified version of the "reflex.leaf" file. It includes the boot reflex action. /* file: reflex_boot.leaf */
#include <aCmd.tea>
#include <aGPReflexes.tea>
/* assume the BrainStem has address 2 */
#define MODULE 2
/* message ids */
#define TIMER_1 0
#define A2D_0 1
#define DBG_PKT 2
module[MODULE] {
/************* REFLEX COMMANDS *************/
/* 0x2710 is 10000 1/10ms increments */
message[TIMER_1] {
MODULE, cmdTMR_SET, 1, 10000
}
/* 0x40 defines A/D 0 with reflex propagation bit set */
message[A2D_0] {
MODULE, cmdA2D_RD, 0x40
}
/* the zero gets filled in by the vector */
message[DBG_PKT] {
MODULE, cmdDEBUG, 0
}
/************* REFLEX VECTORS *************/
/* trip the A/D reading when the timer expires */
vector[aGP_RFX_TIMER_1] {
A2D_0
}
/* restart the timer and fill in the debug value */
vector[aGP_RFX_A2D_0] {
TIMER_1,
DBG_PKT[3] = char
}
/* just initiate the timer on boot (boot must be configured) */
vector[aGP_RFX_BOOT] {
TIMER_1
}
}
To enable the boot reflex, you need to change a system setting using the cmdVAL_SET command. Then save the updated system settings with the cmdVAL_SAV command. The new settings are stored in the EEPROM on the BrainStem and are preserved even when power is turned off. Type these two lines into the Console to enable the boot reflex: 2 18 6 1
2 19
In the first line, the numbers have the following meanings:
In the second line, the numbers have the following meanings:
Console commands and system parameters are documented in the BrainStem Reference . This boot reflex will run each time the BrainStem is powered up until the configuration is changed using cmdVAL_SET and cmdVAL_SAV. aGP_RFX_LINKDOWN is the identifier for a reflex that fires when the link goes down. For example, this reflex can shut off the motors on a moving robot when the link fails, preventing a robot from moving without control. Configuring this reflex to automatically fire when the link goes down is very similar to setting up the boot reflex. What Else Can Reflexes Do? Reflexes can be much more sophisticated than the example above. Reflex elements such as enablers, comparators, counters, rails, amplifiers, functions, and attenuators can be used to build reflex graphs that include some basic decision constructs, timing delays, and more complicated behaviors. The list of reflex elements is listed in the Reflex Element section of the BrainStem Reference. This introduction and simple example gives you an idea of the structure of reflexes within the BrainStem architecture. Very sophisticated interactions are possible using reflexes to initiate TEA programs or using TEA programs to initiate reflexes. Additional Resources The following are good next resources for understanding reflexes and the BrainStem architecture in general.
Revision History:
|
| |||
Related Examples: Using Reflexes to configure IR transmit, receive and triggering a TEA program. | ||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2008 Acroname, Inc., Boulder, Colorado. All rights reserved. |