Reflex Example 8: Link-down ReflexIndex

The Problem

Suppose a BrainStem must perform a task when the serial link to the host goes down.  The task must repeat until the link is restored.

The Solution

A BrainStem module can issue a reflex when the serial link goes down.  Since this is just an example, the task to perform when the link goes down will be very simple: blink the heartbeat light rapidly.  After a few seconds of rapid blinking, the module will check if the link is re-established.

The Details

We need to describe this logic in terms that can be directly translated into BrainStem commands.  System parameters may be changed with the cmdVAL_SET command and saved with the cmdVAL_SAV command.  System parameter 7 is a flag that controls the link-down reflex.  System parameter 2 is the heartbeat rate.  System parameter 5 enableds internal heartbeat mode which will make the heartbeat blink by itself without any dependence on the serial link.  Implementing the desired response will require 2 reflexes:

Reflex 126 Switch to internal heartbeat mode.
  Blink heartbeat rapidly.
  Set timer 20 for 2 seconds.
  Timer 20 Switch back to link heartbeat.
  Blink heartbeat at normal rate.
  Check link.

The "check link" statement above requires a trick.  There is no reflex associated with the serial link being restored, nor is there a direct way to reset the link-down check.  By issuing a host-to-stem heartbeat to itself, the module can temporarily fake a restored link.  This will reset the link-down check.  If the link is still down, the link-down reflex will be activated again.  This cycle will repeat until the serial link is restored.

The Code

In the reflexive routine described above, there are six unique commands and two vectors.  The previous examples have described the low-level bit and byte manipulations required for encoding a reflex.  So for this example, the encoding details will be skipped.  Here is a LEAF file with the reflex source code:

ex8_linkrflx.leaf

Download "ex8_linkrflx.leaf" here
#include <aCmd.tea>
#include <aGPReflexes.tea>

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

module[MODULE] {

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

  // internal heartbeat
  message[0] {
    MODULE, cmdVAL_SET, 5, 1
  }

  // rapid heartbeat
  message[1] {
    MODULE, cmdVAL_SET, 2, 4
  }

  // set timer 20 for approx 2 seconds
  message[2] {
    MODULE, cmdTMR_SET, 20, 80, 0
  }

  // back to link heartbeat
  message[3] {
    MODULE, cmdVAL_SET, 5, 0
  }

  // normal heartbeat rate
  message[4] {
    MODULE, cmdVAL_SET, 2, 10
  }

  // send self a host-to-stem heartbeat
  message[5] {
    MODULE, cmdHB, 2, 2
  }

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

  vector[aGP_RFX_LINKDOWN] {
    0,
    1,
    2
  }

  vector[aGP_RFX_TIMER_20] {
    3,
    4,
    5
  }
}

The Results

The reflexive control routine is now completely defined.  The leaf compiler will take the file and do all the number crunching.  Save the "ex8_linkrflx.leaf" file to your "aUser" directory.  Run the Console and enter leaf "ex8_linkrflx" at the prompt.  This will create a BAG file in the "aUser" directory.  Here is what the compiled file will look like:

ex8_linkrflx.bag

Download "ex8_linkrflx.bag" here

// module 2 (6 messages, 2 vectors)

// message 0
2 12 128 0 2 3 18 5 1
// message 1
2 12 128 1 2 3 18 2 4
// message 2
2 12 0 2 4 39 20 80 0
2 12 134 2
// message 3
2 12 128 3 2 3 18 5 0
// message 4
2 12 128 4 2 3 18 2 10
// message 5
2 12 128 5 2 3 0 2 2
// vector 39
2 11 0 3 128 4 128 133 128
2 11 134 39
// vector 126
2 11 0 0 128 1 128 130 128
2 11 134 126

Then enter batch "ex8_linkrflx.bag" to save the reflex to the module.

To test the routine, enable the link-down reflex by entering the following two commands:

Close the Console program.  The link will not be present so the link-down reflex will be activated.  The module will blink rapidly for a few seconds, then check the link.  It will continue doing this indefinitely.  Now open the Console program again.  The normal heartbeat will be restored.

In some applications, it may be sufficient to perform one task when the link goes down without continuing to check the link status.  For example, a tele-operated vehicle may simply stop when the link goes down.  The host will continue transmitting data and regain control of the vehicle when the link is restored.  In other applications, periodic link checks may be needed.  A semi-autonomous vehicle may need to wander around for a while until the link is restored.  Once the link is working again, it can receive instructions from the host.


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