Hello I am confused that how can I write the code which has all 3 anchors data and showing 3 distances from each anchor to the tag on serial monitor. Actually I have a single anchor code for each of 3 anchors and a tag code for each anchor and I want a combined code in which all 3 anchors data can be fixed so by opening serial monitor I can see 3 distances from each anchor to tag with a single microcontroller. The codes for all anchors and tag are given below:
Anchor 1 Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
// leftmost two bytes below will become the “short address”
char anchor_addr = “02:00:00:00:00:00:00:01”; //#4
//calibrated Antenna Delay setting for this anchor
uint16_t Adelay = 16430;
// previously determined calibration results for antenna delay
// #1 16630
// #2 16610
// #3 16607
// #4 16580
// calibration distance
float dist_m = 1; //meters
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 33
// connection pins
const uint8_t PIN_RST = 32; // reset pin
const uint8_t PIN_IRQ = 36; // irq pin
const uint8_t PIN_SS = 33; // spi select pin
void setup()
{
Serial.begin(115200);
delay(1000); //wait for serial monitor to connect
Serial.println(“Anchor config and start”);
Serial.print("Antenna delay ");
Serial.println(Adelay);
Serial.print("Calibration distance ");
Serial.println(dist_m);
//init the configuration
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
// set antenna delay for anchors only. Tag is default (16384)
DW1000.setAntennaDelay(Adelay);
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
//start the module as an anchor, do not assign random short address
DW1000Ranging.startAsAnchor(anchor_addr, DW1000.MODE_LONGDATA_FAST_ACCURACY, false);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_ACCURACY);
}
void loop()
{
DW1000Ranging.loop();
// newRange();
}
void newRange()
{
// Serial.print("from: “);
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(”, ");
#define NUMBER_OF_DISTANCES 1
float dist = 0.0;
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
dist += DW1000Ranging.getDistantDevice()->getRange();
}
dist = dist/NUMBER_OF_DISTANCES;
Serial.println(dist);
}
void newDevice(DW1000Device *device)
{
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
void inactiveDevice(DW1000Device *device)
{
Serial.print("Delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
Anchor 2 Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
// leftmost two bytes below will become the “short address”
char anchor_addr = “02:00:00:00:00:00:00:02”; //#4
//calibrated Antenna Delay setting for this anchor
uint16_t Adelay = 16467;
// previously determined calibration results for antenna delay
// #1 16630
// #2 16610
// #3 16607
// #4 16580
// calibration distance
float dist_m = 1; //meters
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 4
// connection pins
const uint8_t PIN_RST = 25; // reset pin
const uint8_t PIN_IRQ = 27; // irq pin
const uint8_t PIN_SS = 4; // spi select pin
void setup()
{
Serial.begin(115200);
delay(1000); //wait for serial monitor to connect
Serial.println(“Anchor config and start”);
Serial.print("Antenna delay ");
Serial.println(Adelay);
Serial.print("Calibration distance ");
Serial.println(dist_m);
//init the configuration
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
// set antenna delay for anchors only. Tag is default (16384)
DW1000.setAntennaDelay(Adelay);
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
//start the module as an anchor, do not assign random short address
DW1000Ranging.startAsAnchor(anchor_addr, DW1000.MODE_LONGDATA_FAST_ACCURACY, false);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_ACCURACY);
}
void loop()
{
DW1000Ranging.loop();
}
void newRange()
{
// Serial.print("from: “);
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(”, ");
#define NUMBER_OF_DISTANCES 1
float dist = 0.0;
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
dist += DW1000Ranging.getDistantDevice()->getRange();
}
dist = dist/NUMBER_OF_DISTANCES;
Serial.println(dist);
}
void newDevice(DW1000Device *device)
{
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
void inactiveDevice(DW1000Device *device)
{
Serial.print("Delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
Anchor 3 Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
// leftmost two bytes below will become the “short address”
char anchor_addr = “02:00:00:00:00:00:00:03”; //#4
//calibrated Antenna Delay setting for this anchor
uint16_t Adelay = 16458;
// previously determined calibration results for antenna delay
// #1 16630
// #2 16610
// #3 16607
// #4 16580
// calibration distance
float dist_m = 1; //meters
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 15
// connection pins
const uint8_t PIN_RST = 26; // reset pin
const uint8_t PIN_IRQ = 14; // irq pin
const uint8_t PIN_SS = 15; // spi select pin
void setup()
{
Serial.begin(115200);
delay(1000); //wait for serial monitor to connect
Serial.println(“Anchor config and start”);
Serial.print("Antenna delay ");
Serial.println(Adelay);
Serial.print("Calibration distance ");
Serial.println(dist_m);
//init the configuration
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
// set antenna delay for anchors only. Tag is default (16384)
DW1000.setAntennaDelay(Adelay);
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
//start the module as an anchor, do not assign random short address
DW1000Ranging.startAsAnchor(anchor_addr, DW1000.MODE_LONGDATA_FAST_ACCURACY, false);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_LOWPOWER);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_ACCURACY);
// DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_ACCURACY);
}
void loop()
{
DW1000Ranging.loop();
}
void newRange()
{
// Serial.print("from: “);
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(”, ");
#define NUMBER_OF_DISTANCES 1
float dist = 0.0;
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
dist += DW1000Ranging.getDistantDevice()->getRange();
}
dist = dist/NUMBER_OF_DISTANCES;
Serial.println(dist);
}
void newDevice(DW1000Device *device)
{
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
void inactiveDevice(DW1000Device *device)
{
Serial.print("Delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
Tag Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 5
// connection pins
const uint8_t PIN_RST = 16; // reset pin
const uint8_t PIN_IRQ = 17; // irq pin
const uint8_t PIN_SS = 5; // spi select pin
// TAG antenna delay defaults to 16384
// leftmost two bytes below will become the “short address”
char tag_addr = “02:00:00:00:00:00:10:01”;
void setup()
{
Serial.begin(115200);
delay(1000);
//init the configuration
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
// start as tag, do not assign random short address
// DW1000Ranging.startAsTag(tag_addr, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
DW1000Ranging.startAsTag(tag_addr, DW1000.MODE_LONGDATA_FAST_ACCURACY, false);
}
void loop()
{
DW1000Ranging.loop();
// newRange();
}
void newRange()
{
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(“,”);
Serial.println(DW1000Ranging.getDistantDevice()->getRange());
}
void newDevice(DW1000Device *device)
{
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
void inactiveDevice(DW1000Device *device)
{
Serial.print("delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
So please help me to give a combined code for anchors. And another question is there any need tp change the tag code?