Thursday, July 9, 2009

NXT Bluetooth and RobotC


Bluetooth is a technology that makes it possible to send and receive data without using wired or cables. Using Bluetooth, you can exchange programs between your NXT and other NXT’s. You can establish a wireless connection between your computer and your robot. Try out programs instantly, even with your robot on the other side of the room.

The LEGO NXT system's Bluetooth module allows the user to transmit up to 10 different variables between the two units using a 'mailbox' organizational scheme. This data can be an integer, text, or logical (Boolean) data.

NXT Bluetooth functionality

  • Communicate with one Bluetooth device at the time
  • Can be connected with up to three other devices at a time
  • Search and connect to other Bluetooth devices
  • Remember previous connected device for quick reconnection
  • Set NXT brick visible or invisible to other devices
  • Turn NXT Bluetooth On/Off for power saving

The code below demonstrates the many different functions that can be achieved in RobotC and the coding needed.

////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Bluetooth Operational Tests

//

// ROBOTC provides access and control over the NXT Bluetooth link. This program contains many samples

// to illustrate this functionality.

//

////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma platform(NXT)

long nElapsedTime = 0;

float fThroughput = 0;

long nSendTotal = 0;

long nSendGood = 0;

long nSendBad = 0;

long nSendBusy1 = 0;

long nSendBusy2 = 0;

long nRcvTries = 0;

long nReadCnt = 0;

long nReadBad = 0;

long nLastXmitTimeStamp = nPgmTime;

long nLastRcvdTimeStamp = nPgmTime;

long nDeltaTime = 0;

const int kTimestampHistogramSize = 41;

int nRcvHistogram[kTimestampHistogramSize];

int nXmitHistogram[kTimestampHistogramSize];

const int kTimeBetweenXmit = 30;

////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Send Messages Task

//

////////////////////////////////////////////////////////////////////////////////////////////////////////

const int kMaxSizeOfMessage = 5;

const TMailboxIDs kQueueID = mailbox1;

void readDataMsg();

void sendDataMsg()

{

ubyte nXmitBuffer[kMaxSizeOfMessage] = {0x01, 0x02, 0x03, 0x04, 0x00}; // For NXT-G compatability, last byte of message must be zero because of string messsages.

const bool bWaitForReply = false;

TFileIOResult nBTCmdErrorStatus;

nxtDisplayTextLine(1, "Send %6d", nSendTotal);

nDeltaTime = nPgmTime - nLastXmitTimeStamp;

if (nDeltaTime <>

{

nxtDisplayTextLine(2, "Bsy%6d %6d", nSendBusy1, ++nSendBusy2);

return;

}

if (bBTBusy)

{

nxtDisplayTextLine(2, "Bsy%6d %6d", ++nSendBusy1, nSendBusy2);

return;

}

nBTCmdErrorStatus = cCmdMessageWriteToBluetooth(nXmitBuffer, kMaxSizeOfMessage, kQueueID);

switch (nBTCmdErrorStatus)

{

case ioRsltSuccess:

case ioRsltCommPending:

nxtDisplayTextLine(3, "Send OK %6d", ++nSendGood);

nDeltaTime = nPgmTime - nLastXmitTimeStamp;

nLastXmitTimeStamp = nPgmTime;

if (nDeltaTime >= kTimestampHistogramSize)

nDeltaTime = kTimestampHistogramSize - 1;

++nXmitHistogram[nDeltaTime];

break;

case ioRsltCommChannelBad:

default:

nxtDisplayTextLine(4, "Send Bad %6d", ++nSendBad);

break;

}

return;

}

void checkBTLinkConnected()

{

if (nBTCurrentStreamIndex >= 0)

return;

PlaySound(soundLowBuzz);

PlaySound(soundLowBuzz);

eraseDisplay();

nxtDisplayCenteredTextLine(3, "BT not");

nxtDisplayCenteredTextLine(4, "Connected");

wait1Msec(3000);

StopAllTasks();

}

////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Receive Messages Task

//

////////////////////////////////////////////////////////////////////////////////////////////////////////

void readDataMsg()

{

const bool bWaitForReply = false;

TFileIOResult nBTCmdRdErrorStatus;

int nSizeOfMessage;

ubyte nRcvBuffer[kMaxSizeOfMessage * 5];

while (true)

{

nxtDisplayTextLine(5, "Rd Tries %6d", ++nRcvTries);

// Check to see if a message is available

nSizeOfMessage = cCmdMessageGetSize(kQueueID);

if (nSizeOfMessage <= 0)

{

wait1Msec(1); // Give other tasks a chance to run

break; // No message this time

}

if (nSizeOfMessage > kMaxSizeOfMessage)

nSizeOfMessage = kMaxSizeOfMessage;

nBTCmdRdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, kQueueID);

if (nBTCmdRdErrorStatus == ioRsltSuccess)

{

nDeltaTime = nPgmTime - nLastRcvdTimeStamp;

nLastRcvdTimeStamp = nPgmTime;

if (nDeltaTime >= kTimestampHistogramSize)

nDeltaTime = kTimestampHistogramSize - 1;

++nRcvHistogram[nDeltaTime];

// Keep a running count of the number of messages successfully read

nxtDisplayTextLine(6, "Read OK %6d", ++nReadCnt);

fThroughput = nElapsedTime / (float) nReadCnt;

}

else

nxtDisplayTextLine(7, "Read Bad %6d", ++nReadBad);

}

return;

}

void sendMessages()

{

//

// Send and receive 1M messages

//

for (nSendTotal = 0; nSendTotal <>

{

checkBTLinkConnected();

sendDataMsg();

readDataMsg();

nElapsedTime = nPgmTime;

wait1Msec(1);

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Main Task

//

////////////////////////////////////////////////////////////////////////////////////////////////////////

task main()

{

bNxtLCDStatusDisplay = true;

memset(nRcvHistogram, 0, sizeof(nRcvHistogram));

memset(nXmitHistogram, 0, sizeof(nXmitHistogram));

wait1Msec(2000);

sendMessages();

return;

}

Time: 6.5hrs

No comments:

Post a Comment