Skip to content

Commit

Permalink
fixed ping pong test
Browse files Browse the repository at this point in the history
  • Loading branch information
thotro committed Jun 19, 2015
1 parent ea072f8 commit 1021e31
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// NOTE: the other Arduino needs to be configured with RECEIVER
volatile boolean trxToggle = SENDER;
volatile boolean trxAck = false;
volatile boolean rxError = false;
String msg;
// reset line to the chip
int RST = 9;
Expand All @@ -36,7 +37,7 @@ void setup() {
Serial.println("### DW1000-arduino-ping-pong-test ###");
// initialize the driver
DW1000.begin(0, RST);
DW1000.select(SS);;
DW1000.select(SS);
Serial.println("DW1000 initialized ...");
// general configuration
DW1000.newConfiguration();
Expand All @@ -53,13 +54,14 @@ void setup() {
// attach callback for (successfully) sent and received messages
DW1000.attachSentHandler(handleSent);
DW1000.attachReceivedHandler(handleReceived);
// anchor starts by transmitting a POLL message
DW1000.attachReceiveErrorHandler(handleReceiveError);
// sender starts by sending a PING message, receiver starts listening
if(trxToggle == SENDER) {
msg = "Ping";
msg = "Ping ...";
receiver();
transmit();
} else {
msg = "Pong";
msg = "... and Pong";
receiver();
}
}
Expand All @@ -74,6 +76,11 @@ void handleReceived() {
trxAck = true;
}

void handleReceiveError() {
// error flag
rxError = true;
}

void transmit() {
DW1000.newTransmit();
DW1000.setDefaults();
Expand All @@ -90,20 +97,26 @@ void receiver() {
}

void loop() {
if(rxError) {
Serial.println("Failed to properly receive message.");
rxError = false;
return;
}
if(!trxAck) {
return;
}
// continue on any success confirmation
trxAck = false;
// a sender will be a receiver and vice versa
trxToggle = !trxToggle;
if(trxToggle == SENDER) {
// fomerly in receiving mode
// formerly a receiver
String rxMsg;
DW1000.getData(rxMsg);
Serial.print("Received | "); Serial.println(rxMsg);
Serial.print("Received: "); Serial.println(rxMsg);
transmit();
} else {
Serial.print("Transmitted | "); Serial.println(msg);
Serial.print("Transmitted: "); Serial.println(msg);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ void loop() {
numReceived++;
// get data as string
DW1000.getData(message);
//Serial.print("Received message ... #"); Serial.println(numReceived);
//Serial.print("Data is ... "); Serial.println(message);
if(numReceived % 500 == 0) {
Serial.print("Received message ... #"); Serial.println(numReceived);
Serial.print("Data is ... "); Serial.println(message);
}
Serial.print("Received message ... #"); Serial.println(numReceived);
Serial.print("Data is ... "); Serial.println(message);
received = false;
}
if(error) {
//Serial.println("Error receiving a message");
Serial.println("Error receiving a message");
error = false;
DW1000.getData(message);
//Serial.print("Error data is ... "); Serial.println(message);
Serial.print("Error data is ... "); Serial.println(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void handleSent() {

void transmitter() {
// transmit some data
//Serial.print("Transmitting packet ... #"); Serial.println(sentNum);
Serial.print("Transmitting packet ... #"); Serial.println(sentNum);
DW1000.newTransmit();
DW1000.setDefaults();
String msg = "Hello DW1000, it's #"; msg += sentNum;
DW1000.setData(msg);
// delay sending the message for the given amount
//DW1000.setDelay(500, DW1000.MILLISECONDS);
DW1000.setDelay(500, DW1000.MILLISECONDS);
DW1000.startTransmit();
delaySent = millis();
}
Expand All @@ -75,12 +75,12 @@ void loop() {
// (we are here after the given amount of send delay time has passed)
sentAck = false;
// update and print some information about the sent message
//Serial.print("Delay sent [ms] ... "); Serial.println(millis() - delaySent);
Serial.print("Delay sent [ms] ... "); Serial.println(millis() - delaySent);
unsigned long newSentTime = DW1000.getTransmitTimestamp();
//Serial.print("Processed packet ... #"); Serial.println(sentNum);
//Serial.print("Sent timestamp ... "); Serial.println(newSentTime);
Serial.print("Processed packet ... #"); Serial.println(sentNum);
Serial.print("Sent timestamp ... "); Serial.println(newSentTime);
// note: delta is just for simple demo as not correct on system time counter wrap-around
//Serial.print("Delta send time [s] ... "); Serial.println((newSentTime - sentTime) * 1.0e-6);
Serial.print("Delta send time [s] ... "); Serial.println((newSentTime - sentTime) * 1.0e-6);
sentTime = newSentTime;
sentNum++;
// again, transmit some data
Expand Down
13 changes: 5 additions & 8 deletions DW1000/DW1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,32 +521,28 @@ void DW1000Class::tune() {
void DW1000Class::handleInterrupt() {
// read current status and handle via callbacks
readSystemEventStatusRegister();
if(isTransmitDone() && _handleSent != 0) {
if(isTransmitDone() &&_handleSent != 0) {
(*_handleSent)();
clearTransmitStatus();
}
if(isReceiveError() && _handleReceiveError != 0) {
(*_handleReceiveError)();
clearReceiveStatus();
if(_permanentReceive) {
memset(_sysctrl, 0, LEN_SYS_CTRL);
_deviceMode = RX_MODE;
/*memset(_sysctrl, 0, LEN_SYS_CTRL);
_deviceMode = RX_MODE;*/
startReceive();
}
} else if(isReceiveTimeout() && _handleReceiveTimeout != 0) {
(*_handleReceiveTimeout)();
clearReceiveStatus();
if(_permanentReceive) {
memset(_sysctrl, 0, LEN_SYS_CTRL);
_deviceMode = RX_MODE;
startReceive();
}
} else if(isReceiveDone() && _handleReceived != 0) {
(*_handleReceived)();
clearReceiveStatus();
if(_permanentReceive) {
memset(_sysctrl, 0, LEN_SYS_CTRL);
_deviceMode = RX_MODE;
startReceive();
}
}
Expand Down Expand Up @@ -727,7 +723,8 @@ void DW1000Class::startTransmit() {
setBit(_sysctrl, LEN_SYS_CTRL, TXSTRT_BIT, true);
writeBytes(SYS_CTRL, NO_SUB, _sysctrl, LEN_SYS_CTRL);
if(_permanentReceive) {
//memset(_sysctrl, 0, LEN_SYS_CTRL);
memset(_sysctrl, 0, LEN_SYS_CTRL);
_deviceMode = RX_MODE;
startReceive();
} else {
_deviceMode = IDLE_MODE;
Expand Down
42 changes: 21 additions & 21 deletions DW1000/DW1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,26 +252,6 @@ class DW1000Class {
static float getReceiveTimestamp();
static float getSystemTimestamp();

// device status flags
static boolean isReceiveTimestampAvailable();
static boolean isTransmitDone();
static boolean isReceiveDone();
static boolean isReceiveError();
static boolean isReceiveTimeout();

// interrupt handling
static void interruptOnSent(boolean val);
static void interruptOnReceived(boolean val);
static void interruptOnReceiveError(boolean val);
static void interruptOnReceiveTimeout(boolean val);
static void interruptOnReceiveTimestampAvailable(boolean val);
static void interruptOnAutomaticAcknowledgeTrigger(boolean val);
static void clearInterrupts();
static void clearAllStatus();
static void clearReceiveStatus();
static void clearReceiveTimestampAvailableStatus();
static void clearTransmitStatus();

// callback handler management
static void attachSentHandler(void (*handleSent)(void)) {
_handleSent = handleSent;
Expand Down Expand Up @@ -442,9 +422,29 @@ class DW1000Class {
// whether RX or TX is active
static int _deviceMode;

/* ISR. */
/* Arduino interrupt handler */
static void handleInterrupt();

/* device status flags */
static boolean isReceiveTimestampAvailable();
static boolean isTransmitDone();
static boolean isReceiveDone();
static boolean isReceiveError();
static boolean isReceiveTimeout();

/* interrupt configuration and handling */
static void interruptOnSent(boolean val);
static void interruptOnReceived(boolean val);
static void interruptOnReceiveError(boolean val);
static void interruptOnReceiveTimeout(boolean val);
static void interruptOnReceiveTimestampAvailable(boolean val);
static void interruptOnAutomaticAcknowledgeTrigger(boolean val);
static void clearInterrupts();
static void clearAllStatus();
static void clearReceiveStatus();
static void clearReceiveTimestampAvailableStatus();
static void clearTransmitStatus();

/* internal helper to read/write system registers. */
static void readSystemEventStatusRegister();
static void readSystemConfigurationRegister();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ DW1000.newTransmit();
// configure specific aspects and/or choose defaults
DW1000.setDefaults();
DW1000.setData(some_data);
DW1000.setDelay(100, DW1000.MILLISECONDS);
[float futureTimestamp = ]DW1000.setDelay(100, DW1000.MILLISECONDS);
// ... and other stuff - finally start the transmission
DW1000.startTransmit();
...
Expand Down

0 comments on commit 1021e31

Please sign in to comment.