NXTBee's and RobotC 3.5

RobotC 3.6A quick update on using High Speed Serial commands in RobotC.  Many users have pointed out they can’t use some of the NXTBee functions with the latest version of RobotC.  There were some changes made to RobotC  and we need to update the code to work properly.

We’re currently in the process of doing that, but we wanted to get some code out there for folks to use asap.  Here are three examples of initialization, sending data, and receiving data.

Special thanks to Tim over at Robomatter who touched these examples up for us!

We have three examples that can be downloaded here:  NXTBeeExampleCodeForRobotC3.5.zip

The example code is also below!

XBee_example_one_RX.c

[sourcecode language=”cpp”]

/**********************************************
|* This program will continuously read in *|
|* bytes over the XBee connection, flushing *|
|* and displaying them to the LCD one at a *|
|* time as long as there are bytes to read. *|
**********************************************/

task main()
{
nxtEnableHSPort(); //Enable High Speed Port #4
nxtSetHSBaudRate(9600); //Xbee Default Speed
nxtHS_Mode = hsRawMode; //Set to Raw Mode (vs. Master/Slave Mode)

short bytesRead;
ubyte incomingData;

while(true)
{
if(nxtGetAvailHSBytes()) //Check to see if we have any data coming in
{
if(nxtReadRawHS(&incomingData, 1)) //Ask for the data, save to "incomingDate".
{
//If we actually got data, display it to the LCD
nxtDisplayTextLine(1, "Receive: %c, %i", (char)incomingData, (int)incomingData);
}
}
else //No Data, Wait a little before trying again
{
wait1Msec(25); //Wait 25ms before checking again.
}
}
nxtDisableHSPort(); //Disable HS Port #4
}

[/sourcecode]

XBee_example_two_RX.c

[sourcecode language=”cpp”]

/**********************************************
|* This program will continuously read in *|
|* bytes over the XBee connection, waiting *|
|* until the buffer has at least 5 bytes *|
|* in it, and then flushes them to the LCD. *|
**********************************************/

#define FLUSH_SIZE 5 // how many bytes we want to flush later on

// task to display buffer:
task displayBuffer()
{
while(true)
{
nxtDisplayTextLine(1, "buffer size: %d", nxtGetAvailHSBytes()); // print current size of buffer
wait1Msec(50); // add a pause to let the LCD display correctly
}
}

// main task:
task main()
{
nxtEnableHSPort(); // enable High Speed Port #4
nxtSetHSBaudRate(9600); // Xbee Default Speed
nxtHS_Mode = hsRawMode; // set to Raw Mode (vs. Master/Slave Mode)

int i;
string str = "";
StartTask(displayBuffer); // start the display task
nxtDisplayTextLine(6, "str: "); // display "str: " on line 6

ubyte incomingData; // this will store our incoming data

while(true)
{
if(nxtGetAvailHSBytes() >= FLUSH_SIZE) // if size of buffer is >= FLUSH_SIZE:
{
nxtDisplayTextLine(4, "flush"); // display "flush" to line 4
for(i=FLUSH_SIZE; i>0; i–)
{
nxtReadRawHS(&incomingData, 1); // grab byte from buffer
str += incomingData; // append it to our string
}
nxtDisplayTextLine(6, "str: %s",str); // display our string on line 6
str = ""; // erase our string
}
wait1Msec(250); // add some time so lcd can display "flush"
nxtDisplayTextLine(4, ""); // erase line 4
}
nxtDisableHSPort(); //Disable HS Port #4
}

[/sourcecode]

XBee_example_TX.c

[sourcecode language=”cpp”]

/**********************************************
|* This program will send bytes from 0x30 to *|
|* 0xFF, one at a time, twice a second, *|
|* displaying them on the LCD as a char and *|
|* as an integer. *|
|* Use this for both example RX programs. *|
**********************************************/

task main()
{
nxtEnableHSPort(); //Enable High Speed Port #4
nxtSetHSBaudRate(9600); //Xbee Default Speed
nxtHS_Mode = hsRawMode; //Set to Raw Mode (vs. Master/Slave Mode)

for(ubyte myChar = 0x30; myChar < 0xFF; myChar++) //Send ASCII Character in a loop
{
nxtDisplayTextLine(1, "Sending: %c, %i", (char)myChar, (int)myChar); //Display what is being sent
nxtWriteRawHS(&myChar, 1); //Write the data (paras: char to send, length of data)
wait1Msec(500); //Half Second Delay (can speed this up)
}

nxtDisableHSPort(); //Disable HS Port #4
}

[/sourcecode]

XbeeTools.h

[sourcecode language=”cpp”] #pragma systemFile
#define maxSize 19

/*
Developed by T.Friez
Last Updated: 11/11/2011 5:00pm EST
Version #: 1.02
For use with Digi Xbee Radios (Tested with Series 1)
NXT Hardware: http://www.dexterindustries.com/NXTBee.html
VEX Hardware: http://www.sparkfun.com/products/9132
*/

/////////////////////////
// Initalization Routines
/////////////////////////
#if defined(NXT) || defined(TETRIX)
void InitRS485(long nBaudRate = 9600)
{
nxtEnableHSPort(); //Enable High Speed Port #4
nxtSetHSBaudRate(nBaudRate); //Xbee Default Speed
nxtHS_Mode = hsRawMode; //Set to Raw Mode (vs. Master/Slave Mode)
}
#elif defined(VEX2)
void InitRS232(TUARTs tSelectedUART = UART1, TBaudRate tSelectBaud = baudRate9600)
{
configureSerialPort(tSelectedUART, uartUserControl);
setBaudRate(tSelectedUART, tSelectBaud);
}
#endif

/////////////////////////
// Sending a String
/////////////////////////
#if defined(NXT) || defined(TETRIX)
void SendString(const char* sOutgoing)
{
for(int i = 0; i < strlen(sOutgoing); i++)
{
nxtWriteCharRawHS(sOutgoing[i]);
wait1Msec(2);
}
}
#elif defined(VEX2)
void SendString(const char* sOutgoing, TUARTs tSelectedUART = UART1)
{
for(int i = 0; i < strlen(sOutgoing); i++) { sendChar(tSelectedUART, sOutgoing[i]); while(!bXmitComplete(tSelectedUART)) wait1Msec(1); } } #endif ///////////////////////// // Receiving a String ///////////////////////// #if defined(NXT) || defined(TETRIX) void ReceiveString(const char* cProcessing, bool bClearBuffer = true, short nTimeoutInMS = -1) { ubyte cIncomingChar; int nCounter = 0; memset(cProcessing, 0, sizeof(cProcessing)); if(bClearBuffer) { while(nxtGetAvailHSBytes()) { nxtReadRawHS(&cIncomingChar, 1); noOp(); } } ClearTimer(T4); while(!nxtGetAvailHSBytes()) { if((time1[T4] > nTimeoutInMS) && (nTimeoutInMS != -1))
return;
noOp();
}

wait1Msec(1);

while(nxtGetAvailHSBytes() && nCounter < (maxSize)) { nxtReadRawHS(&cIncomingChar, 1); cProcessing[nCounter] = cIncomingChar; nCounter++; wait1Msec(1); } } #elif defined(VEX2) void ReceiveString(const char* cProcessing,TUARTs tSelectedUART = UART1, bool bClearBuffer = true, short nTimeoutInMS = -1) { char incomingChar = -1; int nCounter = 0; memset(cProcessing, 0, sizeof(cProcessing)); if(bClearBuffer) { while(incomingChar != -1) { incomingChar = getChar(tSelectedUART); wait1Msec(1); } } ClearTimer(T4); while(incomingChar == -1) { if((time1[T4] > nTimeoutInMS) && (nTimeoutInMS != -1))
return;

incomingChar = getChar(tSelectedUART);
wait1Msec(1);
}

cProcessing[nCounter] = incomingChar;
wait1Msec(1);

while(incomingChar != -1 && nCounter < maxSize)
{
incomingChar = getChar(tSelectedUART);
nCounter++;
if(incomingChar != -1)
{
cProcessing[nCounter] = incomingChar;
wait1Msec(1);
}
}
}
#endif
[/sourcecode]

0 Comments

Leave a reply