View unanswered posts | View active topics It is currently Sun Dec 21, 2014 1:31 am






Reply to topic  [ 7 posts ] 
BT connection betwenn 2 NXT 
Author Message
Rookie

Joined: Sun Mar 09, 2008 12:51 pm
Posts: 1
Post BT connection betwenn 2 NXT
I want to connect 2 NXT with BT. I did this in the menu.
Now I want use the sensor on NXT2 on port1 on the NXT1.
Please help me, how it works.


Sun Mar 09, 2008 12:57 pm
Profile
Rookie

Joined: Sun Mar 02, 2008 9:41 am
Posts: 25
Post 
Hi,

I have never done that but i guess that you would need a function, or a better a task which would be executing every n period of time. This task should be getting the data from the sensor and feed it into the other nxt.

I hope this help.

Regards,
Giannis


Sun Mar 09, 2008 2:56 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: BT connection betwenn 2 NXT
wusli wrote:
I want to connect 2 NXT with BT. I did this in the menu.
Now I want use the sensor on NXT2 on port1 on the NXT1.
Please help me, how it works.

You cannot do this directly.
What you need to do is the following:
1. Send message to NXT2 telling it to properly configure the sensor and type. Alternatively you can hardcode this in NXT2 program.
2. NXT2 should periodically send the value of the sensor as a mesasge back to NXT1. Say every 50 to 100 msec.
3. Alternatively you could send a message from NXT1 to NXT2 asking it to send a message back to NXT1 with the sensor value. Whenever you get a reply you could repeat this step. You should find that this will take 50+ milliseconds because of inherent delays in the BT. With errors and retransmissions, I've found it takes up to 120 msec.
4. WHen NXT1 receives message from NXT with sensor value, it should store the value.
5. THe above process should both be implemented as separate tasks on NXT1 and NXT2.
6. When you main program on NXT1 wants to utilize the sensor value, it should simply use the value that was stashed in step (4).

If you really want to minimize the transit time, you'll find that master-slave messages have lower transit times than slave-master. So set up NXT2 as the connection master and use the method in step (2) which only requires one message per update and not 2.


Wed Mar 12, 2008 3:38 am
Profile
Rookie

Joined: Wed Dec 12, 2007 3:32 pm
Posts: 8
Post 
Hi,

I wrote this simple programm to test bluetooth before I startet my own project. The functions I used are based on the RobotC sample Programm "NXT BT Messaging No Error Checking". It is only to show and to understand how bluetooth works on the bricks.

The Source code for the Master NXT:

Code:
//*!!Sensor,    S1,               Touch1, sensorTouch,      ,                    !!*//
//*!!Sensor,    S2,               Touch2, sensorTouch,      ,                    !!*//
//*!!                                                                            !!*//
//*!!Start automatically generated configuration code.                           !!*//
const tSensors NextCmd                   = (tSensors) S1;   //sensorTouch        //*!!!!*//
const tSensors NextSlave               = (tSensors) S2;   //sensorTouch        //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration.                !!*//

// load on Master NXT

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                    Bluetooth Sample Program
//
// ROBOTC provides easy send and receive of messages over Bluetooth. This sample program illustrate the
// basic concept. Bluetooth error checking has been removed from the program to make it easier to
// follow the program logic.
//
// There are other sample programs in the ROBOTC distribution that include Bluetooth using error
// checking.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma platform(NXT)

long nLastXmitTimeStamp = nPgmTime;
long nDeltaTime         = 0;

const int kTimeBetweenXmit = 30;

const int kMaxSizeOfMessage = 2;
const int kQueueID = 0;

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

ubyte nRcvBuffer[kMaxSizeOfMessage];

int icounttouch = -1;


bool mainprocedure();

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                           Check to See if Bluetooth Link is Connection
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void checkBTLinkConnected()
{
  if (nBTCurrentStreamIndex >= 0)
    return;

  PlaySound(soundLowBuzz);
  PlaySound(soundLowBuzz);
  eraseDisplay();
  nxtDisplayCenteredTextLine(3, "BT not");
  nxtDisplayCenteredTextLine(4, "Connected");
  wait1Msec(3000);
  StopAllTasks();
}


void connectto(string sName)
{
   icounttouch = -1;

   wait10Msec(250);
   btDisconnect(1);
   wait10Msec(250);

   btConnect(1, sName);
   wait10Msec(500);
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Send Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void sendDataMsg()
{
  const bool bWaitForReply = false;
  TFileIOResult nBTCmdErrorStatus;

  nDeltaTime = nPgmTime - nLastXmitTimeStamp;
  if (nDeltaTime < kTimeBetweenXmit)
    return;

  if (bBTBusy)
    return;

  nBTCmdErrorStatus = cCmdMessageWriteToBluetooth(nXmitBuffer, kMaxSizeOfMessage, kQueueID);
  switch (nBTCmdErrorStatus)
  {
  case ioRsltSuccess:
  case ioRsltCommPending:
    nLastXmitTimeStamp = nPgmTime;
    break;

  case ioRsltCommChannelBad:
  default:
    break;
  }

  return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Receive Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void readMultipleDataMsgs()
{
  int nSizeOfMessage;

  while (true)
  {
    // 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 more message this time
    }

    if (nSizeOfMessage > kMaxSizeOfMessage)
       nSizeOfMessage = kMaxSizeOfMessage;

    cCmdMessageRead(nRcvBuffer, nSizeOfMessage, kQueueID);

    }
  return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                             Main
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

task endProg()
{
   while( !(SensorValue(NextCmd) && SensorValue(NextSlave)) )
   {}

   PlayImmediateTone(1000, 50);
   btDisconnectAll();
   wait10Msec(250);
   StopAllTasks();
}

// Messages: nXmitBuffer[0]
// 0x01: Play Sound1
// 0x02: Play Sound2
// 0x03: SendBack
// 0xFF: Disconnect

task main()
{
  bNxtLCDStatusDisplay = true;

  StartTask(endProg);

  while(true)
  {

     connectto("NXT S1");

      while( mainprocedure() )
      {}

      connectto("NXT S2");

      while( mainprocedure() )
      {}
   }

  return;
}

bool mainprocedure()
{
     if ( SensorValue(NextCmd) )
     {
        wait10Msec(100);   // entprellen
        icounttouch++;

        if( icounttouch == 3)
           icounttouch = 0;

        switch(icounttouch)
        {
            case 0 : nXmitBuffer[0] = 0x01; break;
            case 1 : nXmitBuffer[0] = 0x02; break;
            case 2 : nXmitBuffer[0] = 0x03; break;
            default: nXmitBuffer[0] = 0x00; break;
        }

        checkBTLinkConnected();
       sendDataMsg();

       wait1Msec(1);
     }

    if( SensorValue(NextSlave) )   // Disconnect
    {
       nXmitBuffer[0] = 0xFF;
       checkBTLinkConnected();
       sendDataMsg();
       return false;
    }

    readMultipleDataMsgs();
    if (nRcvBuffer[0])
    {
       PlayImmediateTone(1000, 10);
       nRcvBuffer[0]=0;
    }

    return true;
}


The source code on the slave: (I had 2 Slavebricks)

Code:
// load on "NXT S1" and "NXT S2"

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                    Bluetooth Sample Program
//
// ROBOTC provides easy send and receive of messages over Bluetooth. This sample program illustrate the
// basic concept. Bluetooth error checking has been removed from the program to make it easier to
// follow the program logic.
//
// There are other sample programs in the ROBOTC distribution that include Bluetooth using error
// checking.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma platform(NXT)


long nLastXmitTimeStamp = nPgmTime;
long nDeltaTime         = 0;

const int kTimeBetweenXmit = 30;

const int kMaxSizeOfMessage = 2;
const int kQueueID = 0;

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

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                           Check to See if Bluetooth Link is Connection
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void checkBTLinkConnected()
{
  if (nBTCurrentStreamIndex >= 0)
    return;

  PlaySound(soundLowBuzz);
  PlaySound(soundLowBuzz);
  eraseDisplay();
  nxtDisplayCenteredTextLine(3, "BT not");
  nxtDisplayCenteredTextLine(4, "Connected");
  wait1Msec(3000);
  StopAllTasks();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Send Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void sendDataMsg()
{
  const bool bWaitForReply = false;
  TFileIOResult nBTCmdErrorStatus;

  nDeltaTime = nPgmTime - nLastXmitTimeStamp;
  if (nDeltaTime < kTimeBetweenXmit)
    return;

  if (bBTBusy)
    return;

  nBTCmdErrorStatus = cCmdMessageWriteToBluetooth(nXmitBuffer, kMaxSizeOfMessage, kQueueID);
  switch (nBTCmdErrorStatus)
  {
  case ioRsltSuccess:
  case ioRsltCommPending:
    nLastXmitTimeStamp = nPgmTime;
    break;

  case ioRsltCommChannelBad:
  default:
    break;
  }

  return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Receive Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void readMultipleDataMsgs()
{
  int nSizeOfMessage;

  while (true)
  {
    // 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 more message this time
    }

    if (nSizeOfMessage > kMaxSizeOfMessage)
       nSizeOfMessage = kMaxSizeOfMessage;
    cCmdMessageRead(nRcvBuffer, nSizeOfMessage, kQueueID);

    }
  return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Main Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////


// Messages: nRcvBuffer[0]
// 0x01: Play Sound1
// 0x02: Play Sound2
// 0x03: SendBack
// 0xFF: Disconnect
task main()
{
  bNxtLCDStatusDisplay = true;
  nRcvBuffer[0] = 0x00;

  wait1Msec(2000); // Give time to start the program at the far end as well

  while (true)
  {
//     checkBTLinkConnected();

     while (nBTCurrentStreamIndex < 0)
     {
        eraseDisplay();
        nxtDisplayCenteredTextLine(3, "BT not");
        nxtDisplayCenteredTextLine(4, "Connected");
        wait10Msec(5);
       }

       eraseDisplay();
     nxtDisplayCenteredTextLine(3, "Connected");

    readMultipleDataMsgs();
    wait1Msec(3);

    switch(nRcvBuffer[0])
    {
       case 0x01:   PlayTone(10, 10); nRcvBuffer[0]= 0x00; eraseDisplay(); nxtDisplayString(1, "T1"); wait10Msec(3); break;
       case 0x02:   PlayTone(1000, 10); nRcvBuffer[0]= 0x00; eraseDisplay(); nxtDisplayString(1, "T2"); wait10Msec(3); break;
       case 0x03:   nXmitBuffer[0] = 0x01; sendDataMsg(); break;
       case 0xFF:   eraseDisplay(); nxtDisplayString(1, "Close Connection"); break;
       default: break;
    }
  }

  btDisconnectAll();
  wait10Msec(1);

  return;
}


You only have to put the sensorvalues in nXmitBuffer[] and on the other brick you have them after reading in nRcvBuffer[].
Note that kMaxSizeOfMessage can also be >2!

The programm was only a small one I wrote quickly and it may still have some bugs (e.g. close connection...) but I hope it helps you to write your own bluetooth functions.


Wed Mar 12, 2008 3:40 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
hi,
what means:
"*!CLICK to edit 'wizard' created sensor & motor configuration. "
?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Mar 12, 2008 7:23 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 616
Post 
Ford Prefect wrote:
hi,
what means:
"*!CLICK to edit 'wizard' created sensor & motor configuration. "
?


You can double click on the Motors and Sensor Setup code to open the Motors and Sensor Setup window. This is provided as a shortcut, rather than going through the Robot menu.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Wed Mar 12, 2008 10:02 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
aha.
never tried this, thx.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Mar 12, 2008 10:46 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.