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
}