| IR with the BrainStem GP 2.0 Last Modified: 2008-05-15 | | |
| Acroname Robotics | PDF webpage version | ||
| Introduction The BrainStem General Purpose (GP) 2.0 module saw the addition of infrared transmit and receive capabilities. This example introduces IR communication with the GP 2.0 and presents several sample TEA programs. Infrared communication consists of an infrared transmitter, including a driving circuit and an infrared LED, and a receiver, which is sensitive to light in the infrared spectrum. The transmitter sends out a series of pulses in a pattern determined by an infrared protocol, and the receiver translates these pulses back to 1s and 0s. These protocols help to insure that background infrared light and infrared from sensors are not mis-read as data, but mean that both the transmitter and receiver must be set to the same protocol to communicate. The NEC and RC5 protocols are built into the BrainStem GP 2.0 firmware. The IR receiver is a black dome with an 'X' across it, next to the prototyping area. The IR transmit LED is located at the top of the board, and the control circuitry is spread out on the board. IR communication requires a line-of-sight between the transmitter and the receiver. However, the transmit LED on the GP 2.0 has been made quite powerful, and can communicate with other modules indoors by bouncing off of walls, ceilings, and furniture up to a few feet. Because IR is omnidirectional, the receiver will pick up the signals from the transmitter. You can account for this by turning off the receiver while transmitting, or writing code to ignore the bytes you send. IR Testing and Configuration from Console IR functionality can be tested by issuing only a few commands to a BrainStem using the Console application. First, the IR configuration command needs to get sent to configure the digital pin (transmit or receive) for the desired IR protocol to use. Configuring the hardware for IR communication takes a single command. From the Console command line, issue a "cmdIRP_CFG" packet. The value for the IRPCFG byte must have the enable bit (bit 3) set. This example is configuring for the NEC protocol for the hardware built in receive. The command sets the IRPCFG bits 2,1,0 to 0. IRPCFG value becomes 0b00001000, which equals the decimal number 8. The resulting command looks like: /* Packet to configure IR RX using NEC protocol */
/* address cmdIRP_CFG DigID IRPCFG */
2 70 4 8
Once the digital pin is properly configured for IR communication, a logic event timer needs to get set up and running. The command to trigger a logic event timer number 4 to the digital pin can be issued by typing the following command in the Console: /* Send the received data back to the host */
/* See cmdIRP_CFG for more information */
2 29 132
Now, using a NEC compatible remote control, send a value to the BrainStem by pressing the number key 2. The Console output should look like: <02:04,13,00,15
Source Code - Transmitting IR using TEA This code uses TEA syntax to wrap the packet commands into reusable functions. It runs with the steep compiler included in the BrainStem console application. Information on TEA and the Console is available in the BrainStem Reference , and Getting Started Guides are available from the BrainStem Resources center. This example requires the console application, build 80507 or later, and its associated libraries (specifically the aIR library), which is available in the Acroname Download Center . The following program demonstrates using TEA functions available in the aIR.tea library for IR communications. The digital pin 5 is set to transmit, configured for NEC protocol and sends an integer value (two bytes). /* file: ir-transmit.tea */
/* This program shows setting up IR transmit. */
#include <aCore.tea>
#include <aIR.tea>
/* ********************************************************************* */
void main()
{
/* set up variables */
int alpha;
alpha = 11111;
/* Set up the IR for transmit. Use NEC protocol. */
aIR_ConfigTX(aIR_PROTOCOL_NEC);
/* Wait 50 milliseconds */
aCore_Sleep(500);
/* Transmit an int. */
aIR_TXInt(alpha);
} /* main */
Source Code - Receiving and IR Value using TEA while the second receives an integer and returns the result to the TEA program. If you have an infrared remote (many TV and other household remotes will work) or another BrainStem module, you can test the receive function by printing the received integer to the console as described below. Note that if you do not send the program any data, it will wait indefinitely at this line of code. Additional documentation on the IR protocols is available in the BrainStem Reference pages. The functions used in this program are listed under the TEA submenu and are described in detail. You can also find more information in the aIR.tea file, located in the aSystem folder of your brainstem directory. /* file: ir-receive.tea */
/* This program shows setting up IR transmit. */
#include <aCore.tea>
#include <aPrint.tea>
#include <aIR.tea>
/* ********************************************************************* */
void main()
{
/* set up variables */
int alpha;
alpha = 11111;
/* Set up the IR for receive. Use NEC protocol, */
/* send incoming data to a TEA program. */
aIR_ConfigRX(aIR_PROTOCOL_NEC,aIR_REPLY_TEA);
/* Wait .0500 seconds */
aCore_Sleep(500);
/* Receive an int. Note: the program will pause */
/* here until a value is received */
alpha = aIR_RXInt();
aPrint_IntUdec(alpha);
aPrint_Char('\n');
} /* main */
Source Code - Reading IR Continuously using TEA This code uses TEA syntax. It runs with the steep compiler included in the BrainStem console application. Information on TEA and the Console is available in the BrainStem Reference , and Getting Started Guides are available from the BrainStem Resources center. This example requires the console application, build 8.00.00 (80000) or later, and its associated libraries (specifically the aIR library), which is available in the Acroname Download Center . The following two TEA programs outline using the aIR.tea library for IR communications and multiple processes to communicate and share information. The first is a continuous program to check for infrared input and write the result to a spot in memory (the Scratch Pad is universally accessible memory, and more information on it can be found in aPad.tea). The "aIRCont.tea" program should be loaded to slot 1 on the BrainStem GP 2.0 module. /* file: aIRCont.tea */
/* Routines for a process that takes continuous IR readings and writes */
/* them to 2 bytes in memory. */
#include <aPad.tea>
#include <aIR.tea>
#include <aMulti.tea>
/* ********************************************************************* */
void main(char callingProcID)
{
int data; /* variable to hold IR data */
/* Configure the pin for NEC IR protocol */
aIR_ConfigRX(aIR_PROTOCOL_NEC,aIR_REPLY_TEA);
/* clear a spot in memory at byte 0 */
/* see the Refennce pages under TEA, aPad functions for using
* Scratchpad memory
*/
aPad_WriteInt(0,0);
while (1)
{
/* read in an IR value and store it
* note: this program will pause here until a value
* is received
*/
data = aIR_RXInt();
aPad_WriteInt(0,data);
} /* while loop */
} /* main */
The following is a test program that launches the continuously looping program "aIRCont.tea", sends test data, and prints the result as verification. The "aIRTest.tea" program can be loaded into any available module slot (for this example, we suggest slot 0). Information on loading programs can be found in the Console Getting Started guide. /* file: aIRTest.tea */
/* This program demonstrates how to set up a */
/* process to continually listen for IR input, and */
/* tests its function by sending an int and */
/* printing the result to the console. */
#include <aCore.tea>
#include <aPrint.tea>
#include <aIR.tea>
#include <aMulti.tea>
/* ********************************************************************* */
void main()
{
/* set up variables */
int alpha;
char beta;
int a;
char b;
alpha = 32767;
beta = 100;
/* Set up the IR for transmit and receive. Use */
/* NEC protocol for both, send incoming data to */
/* a TEA program (see aIR.tea for more info). */
aIR_ConfigRX(aIR_PROTOCOL_NEC,aIR_REPLY_TEA);
aIR_ConfigTX(aIR_PROTOCOL_NEC);
/* wait .1000 seconds */
aCore_Sleep(1000);
/* Launch the continuous IR program concurrently */
/* IR prog takes continuous IR readings and */
/* writes the values to a memory slot in the pad */
/* IR prog must be in slot 1, runs as process 3. */
aMulti_Spawn(1,3);
/* Wait 50 milliseconds */
aCore_Sleep(500);
/* send alpha */
aIR_TXInt(alpha);
/* wait for result */
while(aPad_ReadInt(0)==0){aCore_Sleep(500);}
/* read from byte 0 of the pad, print result */
a = aPad_ReadInt(0);
aPad_WriteInt(0,0);
aPrint_IntUdec(a);
aPrint_Char('\n');
/* same for beta */
aIR_TXInt(beta);
while(aPad_ReadInt(0)==0){aCore_Sleep(500);}
b = aPad_ReadChar(1);
aPad_WriteInt(0,0);
aPrint_CharUdec(b);
aPrint_Char('\n');
/* end the continuous process (process 3) */
aMulti_Kill(3);
} /* main */
To run the test program, load "aIRCont" to slot 1 on the GP 2.0, and "aIRTest" to module slot 0. Then run "aIRTest" using the 'launch' command from the Console, and you should see the transmitted numbers printed back to the Console. What Next? The "aIRCont.tea" file is completely independent of the "aIRTest.tea" file, and can be reused with your own code. With these examples, you should have all the tools necessary to transmit and receive between multiple BrainStem GP 2.0 modules, use a remote with your GP 2.0, and communicate with other IR systems. 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-2009 Acroname, Inc., Boulder, Colorado. All rights reserved. |