Configuring and Using IR with Reflexes
Last Modified: 2008-05-07
find:

basket

Acroname Robotics PDF webpage version Configuring and Using IR with Reflexes PDF

Related
Products

Product image for BrainStem GP 2.0
BrainStem GP 2.0
Product image for IR Remote
IR Remote

Contents

Introduction

The BrainStem GP 2.0 module has built in hardware and firmware that can be used to manipulate IR (Infrared) data.  Although the capability exists for transmitting and receiving IR data, each appropriate respective pin needs configuration before IR functionality is exposed. 

Reflexes are ideal for small tight-loop control that takes place without the interaction of the host or other processes.  Configuring the IR transmit and receive pins and reacting to IR input are simple tasks that can easily be handled with reflexes. 

This example uses a BrainStem GP 2.0 and a NEC remote control for hardware. 

Source Code - Reflex

Reflexes are set up to configure pin functionality and ready an additional reflex to perform an action.  The reflex vectors in this example are commonly used vector IDs; the main reflex entry ID is picked so it can be configured to run on start up.  The other vector defines the behavior that happens when a PTimer event is observed. 

The following flowchart outlines the state machine process for this example. 

Reflex graph for this example.
Flowchart graph of reflex vectors and messages.

A reflex vector at aGP_RFX_BOOT can be configured to run automatically by setting the Reflex Boot Flag to 1 using the cmdVAL_SET command.  The aGP_RFX_BOOT vector in this example consists of three reflex messages.  Two o the messages are very similar that handle the IR pin configurations.  Additional information on the command values inside each reflex message can be found in the reference manual. 

The message mIRRXPTIMER in this example is set to trigger a reflex with the data output.  Setting fourth parameter to 0xC4 (the host bit is set), the data received from the IR RX can be immediately sent back to the host. 

Ptimer output is set to run the reflex message mRUNTEA.  The message mRUNTEA takes the result from the IR RX (the digital Ptimer) and passes input data to a TEA file for higher level data processing.  A TEA example follows the reflex example. 

/* file: ircfg.leaf */ /* Reflex file to get launch on startup to configure IR pins for NEC */ #include <aCmd.tea> #include <aGPReflexes.tea> /* assume the BrainStem has address 2 */ #define MODULE 2 /* message ids */ #define mIRTXCFG 0 #define mIRRXCFG 1 #define mIRRXPTIMER 2 #define mRUNTEA 3 module[MODULE] { /************* REFLEX COMMANDS *************/ /* * send message back to reflex. See aGP_RFX_DRANS_4 vector. * 0x44 = 0b01000100 (reflex bit set, timer 4) * alternatively, one could use: * 0xC4 = 0b11000100 (host bit, reflex bit, timer 4) */ message[mIRRXPTIMER] { MODULE, cmdPTIME_RD,(unsigned char)0x44 } /* IR TX is enabled for the NEC protocol. */ message[mIRTXCFG]{ MODULE,cmdIRP_CFG,5,8 } /* IR RX is enabled for NEC protocol. */ message[mIRRXCFG]{ MODULE,cmdIRP_CFG,4,8 } /* * This message launches a VM process from the file slot 11. The third * parameter is the RUNCTRL byte. The fourth byte is the file slot location. * The last few bytes are dummy bytes */ message[mRUNTEA]{ MODULE,cmdVM_RUN,3,11,0,0 } /************* REFLEX VECTORS *************/ /* * This reflex vector configures the IR RX and TX pins for general use. * The boot reflex may be configured to execute on power up. This can, * for example, be done by sending the cmdVAL_SET from the console. * Once this is configured, it will boot with that reflex every time until * re-configured, that is the boot reflex flag is disabled. */ vector[aGP_RFX_BOOT] { mIRTXCFG, mIRRXCFG, mIRRXPTIMER } /* * This vector gets triggered after ptimer event is triggered on the * IR RX pin. Stuff the result into a packet that gets starts * the TEA process stored in file slot 11. */ vector[aGP_RFX_DTRANS_4]{ mRUNTEA[5] = int } }

Compile the reflex using the Leaf compiler built into the Console application.  Detailed steps are outlined in the Reflexes Getting Started Guide.  Once you upload the compiled reflexes stored in the bag file, you can launch the reflex now at vector 127.  Do this by typing:

2 38 127 0 /* address cmdRAW_INPUT MSG INPUT */

This launches the reflex at vector 127 which configures the TX and RX pins for the NEC protocol.  Additionally, the 3rd message is to configures and starts the Ptimer on digital pin 4 (the pin connected to the hardware RX).  Send an IR command using the remote control to the module and the Console should output:

<02:15,00 vm exited: 2,0,11,void

This is okay, as the Console output is telling us that we have not yet put a TEA file with correct input parameters on the BrainStem - yet.  But, this does demonstrate that the reflex vector did configure the IR receive pin, launch the Ptimer and try to execute a VM. 

We can take this a step further and make this reflex work on startup by using the cmdVAL_SET command in the Console to set the boot reflex flag for vector 127 (aka, aGP_RFX_BOOT) on startup.  This allows us to use the IR capabilities at our discretion. 

Using the Console program, type the following to configure reflex execution on boot up:

2 18 6 1 /* address cmdVAL_SET ParamID VALUE */ 2 19 /* address cmdVAL_SAV */

Source Code - TEA File with Parameter Input

A TEA file can be used as utility to perform richer tasks.  This example demonstrates accepting the IR data as a parameter to the main function.  The input values are analyzed and correlated to which IR remote button was pressed. 

The VM is launched after the Ptimer 4 reflex expires.  IR data collected on DIO 4 (the RX) pin is passed into the main calling function.  The main routine correlates the IR values from the remote control to the button number on the remote.  The final result is printed out to the Console window. 

In the reflex definition file, the VM slot was hard coded as file slot 11. 

/* file: irrflx.tea */ /* TEA program that gets launched by reflex. */ #include <aCore.tea> #include <aPrint.tea> /* ********************************************************************* */ /* The main function takes the IR data input from the reflex and prints the * corresponding remote button number to the Console, or host. */ int main(int i) { /* Output the information to the Console */ aPrint_String("\n"); aPrint_String("Pressed remote button: "); aPrint_CharDec(remoteButton(i)); /* Let the data get completely sent to the console */ aCore_Sleep(1000); // Return the value that was passed into the VM return i; } /* end of main calling routine */ /* ********************************************************************* */ /* Function to decode the NEC value from the remote. This function maps the * value received to the button number on the remote. */ char remoteButton(int i) { char result = 0; switch(i){ case 0x0016: result = 1; break; case 0x0015: result = 2; break; case 0x0014: result = 3; break; case 0x000E: result = 4; break; case 0x000D: result = 5; break; case 0x000C: result = 6; break; case 0x000A: result = 7; break; case 0x0009: result = 8; break; case 0x0008: result = 9; break; case 0x0006: result = 10; break; case 0x0005: result = 11; break; case 0x0004: result = 12; break; case 0x0001: result = 13; break; default: result = 0; break; } /* End of switch statement */ return result; } /* end of remoteButton function */

Save the file "irrfx.tea" into your aUser directory.  Compile, load and launch the program.  In the Console program, type the following commands:

steep "irrflx" load "irrflx" 2 11 launch 2 11 0 10

Inside the Console window, the decoded remote button will be indicated.  Now, send IR data from the remote control to the BrainStem.  If all goes well, the Ptimer reflex vector will get triggered and the VM in file slot 11 should get launched.  Notice, the vm exited message also indicates the two byte parameter that was passed into the main calling routine by the reflex.  For example, when the remote button 7 is pressed, the message "vm exited: 2,0,0,10" is printed.  The two bytes 0,10 are the decimal IR byte values measured by the Ptimer. 

Revision History:

  • 2008-05-07: New Example
 

Related Links:

Getting Started Guide: BrainStem Reflexes

Related Examples:

Multi-file TEA Programming Using Reflexes Example

Generating Tones with Reflexes Example

BrainStem GP 2.0 - IR Transmit / Receive Example

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.