Skip to content

Commit

Permalink
fixed DW1000Time (now uint64_t wrapper) - towards first useful rangin…
Browse files Browse the repository at this point in the history
…g results
  • Loading branch information
thotro committed Jul 2, 2015
1 parent 3822c87 commit 7be3030
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define RECEIVER false
// toggle and message RX/TX
// NOTE: the other Arduino needs to be configured with RECEIVER
volatile boolean trxToggle = SENDER;
volatile boolean trxToggle = RECEIVER;
volatile boolean trxAck = false;
volatile boolean rxError = false;
String msg;
Expand All @@ -44,7 +44,6 @@ void setup() {
DW1000.setDefaults();
DW1000.setDeviceAddress(1);
DW1000.setNetworkId(10);
DW1000.setFrameFilter(false);
DW1000.commitConfiguration();
Serial.println("Committed configuration ...");
// DEBUG chip info and registers pretty printed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* Complements the "DW1000-arduino-ranging-tag" sketch.
*/

/*
* TODO: ranging needs to be done more often and results have to be taken
* on average.
*/

#include <SPI.h>
#include <DW1000.h>

Expand Down Expand Up @@ -118,51 +123,12 @@ void receiver() {
DW1000.startReceive();
}

float getRange() {
DW1000Time getRange() {
// correct timestamps (in case system time counter wrap-arounds occured)
// TODO
// two roundtrip times - each minus message preparation times / 4
Serial.println("START DATA");
byte d[5];
char msgBuf[1024];
/*Serial.print("POLL RECEIVED ");
timePollReceived.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);
Serial.print("POLL ACK SENT ");
timePollAckSent.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);
Serial.print("RANGE RECEIVED ");
timeRangeReceived.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);
Serial.print("POLL SENT ");
timePollSent.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);
Serial.print("POLL ACK RECEIVED ");
timePollAckReceived.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);
Serial.print("RANGE SENT ");
timeRangeSent.getAsBytes(d);
memset(msgBuf, 0, 1024);
DW1000.getPrettyBytes(d, msgBuf, 5);
Serial.println(msgBuf);*/

return ((timePollAckReceived.getAsFloat()-timePollSent.getAsFloat())-(timePollAckSent.getAsFloat()-timePollReceived.getAsFloat()) +
(timeRangeReceived.getAsFloat()-timePollAckSent.getAsFloat())-(timeRangeSent.getAsFloat()-timePollAckReceived.getAsFloat()));// / 4;

/*DW1000Time timeOfFlight = ((timePollAckReceived-timePollSent)-(timePollAckSent-timePollReceived) +
(timeRangeReceived-timePollAckSent)-(timeRangeSent-timePollAckReceived));// / 4;
return timeOfFlight.getAsFloat();*/
return ((timePollAckReceived-timePollSent)-(timePollAckSent-timePollReceived) +
(timeRangeReceived-timePollAckSent)-(timeRangeSent-timePollAckReceived)) / 4;
}

void loop() {
Expand Down Expand Up @@ -197,16 +163,17 @@ void loop() {
DW1000.getReceiveTimestamp(timeRangeReceived);
expectedMsgId = POLL;
if(!protocolFailed) {
timePollSent.setFromBytes(data+1);
timePollAckReceived.setFromBytes(data+6);
timeRangeSent.setFromBytes(data+11);
timePollSent.setTimestamp(data+1);
timePollAckReceived.setTimestamp(data+6);
timeRangeSent.setTimestamp(data+11);
Serial.print("Received RANGE @ "); Serial.println(timeRangeReceived.getAsFloat());
Serial.print("POLL sent @ "); Serial.println(timePollSent.getAsFloat());
Serial.print("POLL ACK received @ "); Serial.println(timePollAckReceived.getAsFloat());
Serial.print("RANGE sent @ "); Serial.println(timeRangeSent.getAsFloat());
float curRange = getRange();
Serial.print("Range time is "); Serial.println(curRange);
transmitRangeReport(curRange);
DW1000Time curRange = getRange();
Serial.print("Range time is "); Serial.println(curRange.getAsFloat(), 4);
Serial.print("Range is "); Serial.println(curRange.getAsMeters());
transmitRangeReport(curRange.getAsFloat());
} else {
transmitRangeFailed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* Complements the "DW1000-arduino-ranging-anchor" sketch.
*/

/*
* TODO: either ACK frames to check if message loss or watchdog timer to recover
* from protocol failure on tag side.
*/

#include <SPI.h>
#include <DW1000.h>

Expand Down Expand Up @@ -91,14 +96,14 @@ void transmitRange() {
DW1000.setDefaults();
data[0] = RANGE;
// delay sending the message and remember expected future sent timestamp
DW1000Time deltaTime = DW1000Time(50, DW1000Time::MILLISECONDS);
DW1000Time deltaTime = DW1000Time(10, DW1000Time::MILLISECONDS);
timeRangeSent = DW1000.setDelay(deltaTime);
Serial.print("Expect RANGE to be sent @ "); Serial.println(timeRangeSent.getAsFloat());
timePollSent.getAsBytes(data+1);
timePollAckReceived.getAsBytes(data+6);
timeRangeSent.getAsBytes(data+11);
timePollSent.getTimestamp(data+1);
timePollAckReceived.getTimestamp(data+6);
timeRangeSent.getTimestamp(data+11);
DW1000.setData(data, LEN_DATA);
DW1000.startTransmit();
Serial.print("Expect RANGE to be sent @ "); Serial.println(timeRangeSent.getAsFloat());
}

void receiver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ void transmitter() {
String msg = "Hello DW1000, it's #"; msg += sentNum;
DW1000.setData(msg);
// delay sending the message for the given amount
DW1000Time deltaTime = DW1000Time(500, DW1000Time::MILLISECONDS);
//DW1000Time deltaTime = DW1000Time(500, DW1000Time::MILLISECONDS);
DW1000Time deltaTime = DW1000Time(10, DW1000Time::MILLISECONDS);
DW1000.setDelay(deltaTime);
DW1000.startTransmit();
delaySent = millis();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2015 by Thomas Trojer <thomas@trojer.net>
* Decawave DW1000 library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*
* This is a simple unit test for the DW1000Time class. This test
* has no actual use to the operation of the DW1000.
*/

#include <SPI.h>
#include <DW1000.h>

void setup() {
Serial.begin(9600);
Serial.println("### DW1000Time-arduino-test ###");
}

void loop() {
// variables for the test
DW1000Time time;
DW1000Time time2;
byte stamp[LEN_STAMP];
// unit test
Serial.print("Time is [us] ... "); Serial.println(time.getAsFloat(), 4);
time += DW1000Time(10, DW1000Time::MICROSECONDS);
Serial.print("Time is [us] ... "); Serial.println(time.getAsFloat(), 4);
time -= DW1000Time(500, DW1000Time::NANOSECONDS);
Serial.print("Time is [us] ... "); Serial.println(time.getAsFloat(), 4);
time2 = time;
time2 += DW1000Time(10.0f);
Serial.print("Time2 == Time1 ... "); Serial.println(time == time2 ? "YES" : "NO");
time += DW1000Time(10000, DW1000Time::NANOSECONDS);
Serial.print("Time2 == Time1 ... "); Serial.println(time == time2 ? "YES" : "NO");
memset(stamp, 0, LEN_STAMP);
stamp[1] = 0x02; // = 512
time2 = DW1000Time(stamp);
Serial.print("Time2 is [us] ... "); Serial.println(time2.getAsFloat(), 4);
Serial.print("Time2 range is [m] ... "); Serial.println(time2.getAsMeters(), 4);
// keep calm
delay(1000);
}

14 changes: 5 additions & 9 deletions DW1000/DW1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ void DW1000Class::commitConfiguration() {
byte antennaDelayBytes[LEN_STAMP];
// TODO setter + check not larger two bytes integer
writeValueToBytes(antennaDelayBytes, 16384, LEN_STAMP);
_antennaDelay.setFromBytes(antennaDelayBytes);
_antennaDelay.setTimestamp(antennaDelayBytes);
writeBytes(TX_ANTD, NO_SUB, antennaDelayBytes, LEN_TX_ANTD);
writeBytes(LDE_IF, LDE_RXANTD_SUB, antennaDelayBytes, LEN_LDE_RXANTD);
// write all configurations back to device
Expand Down Expand Up @@ -781,7 +781,7 @@ DW1000Time DW1000Class::setDelay(const DW1000Time& delay) {
DW1000Time futureTime;
getSystemTimestamp(futureTime);
futureTime += delay;
futureTime.getAsBytes(delayBytes);
futureTime.getTimestamp(delayBytes);
delayBytes[0] = 0;
delayBytes[1] &= 0xFE;
writeBytes(DX_TIME, NO_SUB, delayBytes, LEN_DX_TIME);
Expand Down Expand Up @@ -947,19 +947,19 @@ void DW1000Class::getData(String& data) {
void DW1000Class::getTransmitTimestamp(DW1000Time& time) {
byte txTimeBytes[LEN_TX_STAMP];
readBytes(TX_TIME, TX_STAMP_SUB, txTimeBytes, LEN_TX_STAMP);
time.setFromBytes(txTimeBytes);
time.setTimestamp(txTimeBytes);
}

void DW1000Class::getReceiveTimestamp(DW1000Time& time) {
byte rxTimeBytes[LEN_RX_STAMP];
readBytes(RX_TIME, RX_STAMP_SUB, rxTimeBytes, LEN_RX_STAMP);
time.setFromBytes(rxTimeBytes);
time.setTimestamp(rxTimeBytes);
}

void DW1000Class::getSystemTimestamp(DW1000Time& time) {
byte sysTimeBytes[LEN_SYS_TIME];
readBytes(SYS_TIME, NO_SUB, sysTimeBytes, LEN_SYS_TIME);
time.setFromBytes(sysTimeBytes);
time.setTimestamp(sysTimeBytes);
}

void DW1000Class::getTransmitTimestamp(byte data[]) {
Expand All @@ -982,10 +982,6 @@ boolean DW1000Class::isReceiveTimestampAvailable() {
return getBit(_sysstatus, LEN_SYS_STATUS, LDEDONE_BIT);
}

boolean DW1000Class::isLDEDone() {
return getBit(_sysstatus, LEN_SYS_STATUS, LDEDONE_BIT);
}

boolean DW1000Class::isReceiveDone() {
if(_frameCheck) {
return getBit(_sysstatus, LEN_SYS_STATUS, RXFCG_BIT);
Expand Down
1 change: 0 additions & 1 deletion DW1000/DW1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ class DW1000Class {
/* device status flags */
static boolean isReceiveTimestampAvailable();
static boolean isTransmitDone();
static boolean isLDEDone();
static boolean isReceiveDone();
static boolean isReceiveError();
static boolean isReceiveTimeout();
Expand Down
Loading

0 comments on commit 7be3030

Please sign in to comment.