Post a new topicPost a reply Page 1 of 1   [ 6 posts ]
Author Message
 Post subject: Using packet in BROADCAST mode for synchronisation
PostPosted: Wed Dec 21, 2011 3:13 pm 

Joined: Wed Dec 21, 2011 3:01 pm
Posts: 15
Hola,
I am sending a broadcast message to synchronise devices so that each sender
has a guaranteed time slot. The code below seems to work but benchmarking
the time it takes to send a broadcast package gives results from 14mS to 2019mS.

In my code below the beacon is sent using broadcastBeacon and benchmarked
as follows:

Code:
t1 = millis();
state = xbeeZB.sendXBee(syncBeacon);
t2 = millis();
sprintf(temp80, "\r\n-[ SYNC REQUEST BRDCST COMPLETED IN (mS): %u STATE: %u", (t2-t1),


Can someone help find a solution as I need the beacon send time to be as
repeatible as possible and ideally as low as possible - perhaps 15 or 20mS if it
is achievable.

Julia x

Code:
/*
 *  Creating a network with Synchronisation beacon.
 *
 *  Version: 0.1
 *  Date:    20/12/2011
 */

#define  MAX_DATA   200
#define  MAC_END    "0013A20040690224"
#define  GNSSMAC_P  "0013A200406972C3"
#define  GNSSMAC_Q  "0013A200406972D1"
#define  GNSSMAC_R  "0013A200406972C8"
#define  GNSSMAC_S  "0013A200406972C7"
#define  GNSSMAC_T  "0013A200406972C4"

uint8_t  PANID[8]={0x12,0x34,0,0,0,0,0,0};
char       myMac[20];   // Full mac address
char       macLow[20];
char      macHigh[20];
char       temp80[80];
char       temp20[20];

unsigned long previous   = 0;
unsigned long t1, t2, t3;          // used to benchmark code throughput
unsigned broadcastCounter = 0;
packetXBee  * syncBeacon;     // Must be global

//********************************************************************
// Start of function declarations
//*******************************************************************/
void  toggleLEDs(uint8_t  times)
{     while(times--)
       {  Utils.setLED(LED0, LED_ON);   Utils.setLED(LED1, LED_OFF);  delay(20);
           Utils.setLED(LED0, LED_OFF);  Utils.setLED(LED1, LED_ON);   delay(20);
       }
}

void  broadcastBeacon()
{
uint8_t   state;
t1 = millis();
state = xbeeZB.sendXBee(syncBeacon);
t2 = millis();
sprintf(temp80, "\r\n-[ SYNC REQUEST BRDCST COMPLETED IN (mS): %u STATE: %u", (t2-t1), state);
printString(temp80, 1);
if (!state) {  printString("\r\n-[ RETRIES: ", 1);
               printInteger(syncBeacon->retries, 1);
               printString("\r\n-[ STATUS: ", 1);
               printInteger(syncBeacon->deliv_status, 1);
            }
}

void     formBroadcastPacket(uint8_t gts)
{
unsigned t1, t2;
if (gts > 10000) gts = 10000;    // Error bounds
t1=millis();
sprintf(temp20, "SYNC,%u", gts);
syncBeacon=(packetXBee *)calloc(1, sizeof(packetXBee));
syncBeacon->mode=BROADCAST   ;
syncBeacon->MY_known = 0     ;
syncBeacon->packetID = 0x52  ;
syncBeacon->opt      = 0x0   ;
xbeeZB.hops          = 1     ;
xbeeZB.setOriginParams(syncBeacon, "", MAC_TYPE);
xbeeZB.setDestinationParams(syncBeacon, "0000000000000000FFFF", temp20, MAC_TYPE, DATA_ABSOLUTE);
printString("\r\n -[ DEBUG: BROADCAST MESSAGE FORMED IN (mS): ", 1);
printInteger(millis()-t1, 1);
}

// Function to delete the broadcasted packet and free memory
void   deleteBroadcastPacket()
{      free(syncBeacon);
       syncBeacon = NULL; 
}

// Function to display Mac Address of current XBee.

void   displayOwnMac()
{      xbeeZB.getOwnMacHigh();
       xbeeZB.getOwnMacLow();
       sprintf(macHigh, "%02X%02X%02X%02X", xbeeZB.sourceMacHigh[0], xbeeZB.sourceMacHigh[1], xbeeZB.sourceMacHigh[2], xbeeZB.sourceMacHigh[3]);
       sprintf(macLow,  "%02X%02X%02X%02X", xbeeZB.sourceMacLow[0],  xbeeZB.sourceMacLow[1],  xbeeZB.sourceMacLow[2],  xbeeZB.sourceMacLow[3]);
       printString("\r\n-[ DEBUG: Gateway MAC is ", 1);
       strcpy(myMac, strcat(macHigh, macLow));
       printString(myMac, 1);
}
// Start of main program with setup first

void setup()
{ beginSerial(38400, 1);
  Utils.setMuxAux1();
  USB.begin();                                      // starts using the serial port
  xbeeZB.init(ZIGBEE,FREQ2_4G,PRO);              // Powers XBee
  // xbeeZB.init(ZIGBEE,FREQ2_4G,PRO);
  xbeeZB.ON();                                      // Getting Channel
  xbeeZB.setAPSencryption(XBEE_OFF);
  printString("\r\n************************************************", 1);
  printString("\r\n**[ INITIALISING DEBUG INFORMATION ON BEACON ]**", 1);
  printString("\r\n************************************************", 1);
  xbeeZB.setPAN(PANID);
  if (!xbeeZB.error_AT)     printString("\r\n-[ DEBUG: Beacon PANID set ok", 1);
  else                      {
                            printString("\r\n-[ DEBUG: Error setting PANID", 1);
                            printString("\r\n-[ DEBUG: Insert module, reset in 5secs ", 1);
                            delay(5000);
                            PWR.reboot();
                            }
  printString("\r\n-[ DEBUG: Receiver Battery ", 1);
  sprintf(temp80, "%u %c ", (unsigned)PWR.getBatteryLevel(),'\%');
  printString(temp80, 1);
  xbeeZB.encryptionMode(0);
  if( !xbeeZB.error_AT )    printString("\r\n-[ DEBUG: Security disabled ]-",     1);
  else                      printString("\r\n-[ DEBUG: Error while enabling security ]-", 1);      // Configuring Trust Center
 
  xbeeZB.setNodeIdentifier("NODE-0");
  xbeeZB.getNodeIdentifier();
  printString("\r\n-[ DEBUG: Name of node: ", 1);
  printString(xbeeZB.nodeID, 1);
  // xbeeZB.setNetworkRetries(3);
  xbeeZB.setPowerLevel(1);           // set to maximum power (+2dBm).
  xbeeZB.setStackProfile(2);
  xbeeZB.setScanningChannels(0xff,0xff);
  xbeeZB.setDurationEnergyChannels(3);
  xbeeZB.setJoinTime(0xff);          //  Always allow joining
  xbeeZB.setJoinNotification(1);

  printString("\r\n-[ DEBUG: Network Initialisation (3 seconds) ]-",       1);
  displayOwnMac();
  xbeeZB.getChannel();
  sprintf(temp80, "\r\n-[ DEBUG: Beacon init on chan: %02u ]-", (unsigned)xbeeZB.channel);
  printString(temp80, 1);
  xbeeZB.setMaxUnicastHops(1);      //  Star so do not route through other device
  xbeeZB.setMaxBroadcastHops(1);   //

  xbeeZB.writeValues();
  if (!xbeeZB.error_AT )    printString("\r\n-[ DEBUG: XBee Changes stored OK",     1);
  else                            printString("\r\n-[ DEBUG: Error while storing values", 1);

  unsigned counter = 0;            // This is 10 seconds - waiting for nodes to join !
  printString("\r\n-[ DEBUG: Waiting to join PAN .. ", 1);
  counter = 0;
  // Wait for first device to join to coordinator...
  do {  xbeeZB.getAssociationIndication();     
        if (xbeeZB.associationIndication != 0x2B) 
             printString("\r\n-[ DEBUG: 1st Pass waiting for network association (code): ", 1);
        delay(250);
        printInteger(xbeeZB.associationIndication, 1);
     }  while(xbeeZB.associationIndication > 0);
     
  uint8_t  bros;
  xbeeZB.scanNetwork();               
  if (xbeeZB.totalScannedBrothers > 0)
    {   printString("\r\n-[ DEBUG: MAC Joined: ", 1);
        bros = xbeeZB.totalScannedBrothers-1;
        sprintf(macHigh, "%02X%02X%02X%02X", xbeeZB.scannedBrothers[bros].SH[0],   
.                 xbeeZB.scannedBrothers[bros].SH[1], xbeeZB.scannedBrothers[bros].SH[2], 
.                 xbeeZB.scannedBrothers[bros].SH[3]);
        sprintf(macLow , "%02X%02X%02X%02X", xbeeZB.scannedBrothers[bros].SL[0],
.                 xbeeZB.scannedBrothers[bros].SL[1], xbeeZB.scannedBrothers[bros].SL[2],
.                 xbeeZB.scannedBrothers[bros].SL[3]);
        sprintf(temp80, "%s-%s", macHigh, macLow);
        printString(temp80, 1);
    }
  else
    {   printString("\r\n-[ DEBUG: Network association failed, rebooting (5 secs)", 1);
        delay(5000);
        PWR.reboot();
    }
  do {  xbeeZB.getAssociationIndication();
        printString("\r\n-[ DEBUG: 2nd Pass rechecking network association (code): ", 1);
        printInteger(xbeeZB.associationIndication, 1);
     }  while(xbeeZB.associationIndication > 0);  // stop if network association still good
        sprintf(temp80, "\r\n-[ DEBUG: Brothers %u ", xbeeZB.totalScannedBrothers);
        printString(temp80, 1);
        printString("\r\n-[ DEBUG: Starting beacon (3 secs) ]-", 1);
        formBroadcastPacket(200);
        delay(3000);
}
 
uint8_t code      = 0;
uint8_t beacons = 0;
uint8_t value     = 0;
uint8_t stime     = 0;

void loop()               // This loop continually forces the network PANID
{ unsigned long tscan;
  t3=millis();
  toggleLEDs(3);
  if (beacons++ > 20)
    {  beacons = 0;
       xbeeZB.setPAN(PANID);
       do {  xbeeZB.getAssociationIndication();
          }  while(xbeeZB.associationIndication > 0);
       printString("\r\nDevice associations ok", 1);
    }
  broadcastBeacon();
  printString("\r\nFree memory: ", 1);
  printInteger(freeMemory(), 1);      // Check for memory leaks. */
  while(millis()-t3 < 1000);          // Synchronise beacon for 1Hz
}


Top
 Profile  
 
 Post subject: Re: Using packet in BROADCAST mode for synchronisation
PostPosted: Tue Jan 03, 2012 12:08 pm 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi,

Maybe, it is hard to achieve with the API and this kind of XBee protocol. Perhaps, you could study how to change the API in order to get the maximum throughput you can.

Best regards


Top
 Profile  
 
 Post subject: Re: Using packet in BROADCAST mode for synchronisation
PostPosted: Fri Jan 06, 2012 12:51 pm 

Joined: Wed Dec 21, 2011 3:01 pm
Posts: 15
To help others who may need to do this I have included my findings. Firstly the code
above had a problem as the address for a broadcast has 16 leading zeros not 20. i.e.
it should have been 000000000000FFFF.

Regarding increasing the throughput this can be achieved by disabling receiver
MAC acknowledge handshaking and using a UNICAST for each node that synchronisation
is required. So in other words set the opt parameter = 1.

syncBeacon=(packetXBee *)calloc(1, sizeof(packetXBee));
syncBeacon->mode=UNICAST ;
syncBeacon->MY_known = 0 ;
syncBeacon->packetID = 0x52 ; // Zigbee TX request 0x10
syncBeacon->opt = 1 ; // 1 disabled acknowledgements for beacons
xbeeZB.hops = 1 ;
xbeeZB.setOriginParams(syncBeacon, "0000000000000000", MY_TYPE);
xbeeZB.setDestinationParams(syncBeacon, "your mac address", "SYNC", MAC_TYPE, DATA_ABSOLUTE);

Julia x


Top
 Profile  
 
 Post subject: Re: Using packet in BROADCAST mode for synchronisation
PostPosted: Tue Jan 10, 2012 11:09 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi Julia,

Your answer is well appreciated. Did you get the time constraints your proposed as goal for your project?

Regards


Top
 Profile  
 
 Post subject: Re: Using packet in BROADCAST mode for synchronisation
PostPosted: Tue Jan 10, 2012 12:36 pm 

Joined: Wed Dec 21, 2011 3:01 pm
Posts: 15
I changed to using UNICAST and get 20mS throughput reliably, I will be testing again
with BROADCAST but CLUSTER would be better so I can send a message to synchronise
a set of devices.

Havent had sufficient time to look into CLUSTER, BROADCAST seemed to periodically
get good throughput but my enquiries suggested that BROADCAST sends three
transmissions whereas CLUSTER will send only one which is better.

There is an example in the LIBELIUM ZigBee documentation for CLUSTER mode which
sets both the 16 bit and 64 bit addresses for a node as well as the CID and PID.

I am guessing that any data sent to this node will be relayed onto other nodes in the
cluster. Do you know if this is correct ?

Julia x


Top
 Profile  
 
 Post subject: Re: Using packet in BROADCAST mode for synchronisation
PostPosted: Fri Jan 13, 2012 12:02 pm 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi,
Julia wrote:
I am guessing that any data sent to this node will be relayed onto other nodes in the
cluster. Do you know if this is correct ?
In case it is a broadcast transmission, it will.

Best regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 6 posts ]


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

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use